What does "w" do with urandom?

> strace w 2>&1 | grep urandom
read(4, "/usr/bin/grepurandom", 2047) = 22

Why does “w” need urandom? How to avoid this?


> strace w 2>&1 | awk '/urandom/'
read(4, "awk/urandom/", 2047)       = 14

so it is the filtering that has something to do with urandom?

> strace who 2>&1 | grep urandom

Then why isn’t “who” affected?

Asked By: Marina Ala


As explained in other answers and comments the reason for what you
observe is the way Bash handles pipes. In order to filter what you
really want in similar situations you can try to enclose the first letter of the grep argument in [] like this:

$ strace w 2>&1 | grep random
read(4, "greprandom", 2047)         = 12
$ strace w 2>&1 | grep '[r]andom'
$ strace w 2>&1 | grep '[c]lose'
close(3)                                = 0
close(3)                                = 0
close(3)                                = 0
close(3)                                = 0
close(3)                                = 0
close(3)                                = 0


As correctly noted by R. in the comment below in fact strace does
not see the other side of the pipe. Similarly to ps aux | grep grep
which also shows grep grep in its output w is is walking through
/proc directory and finds grep process there.

Answered By: Arkadiusz Drabczyk

From the manpage w(1):

w displays information about the users currently on the
machine, and their processes

To display the users’ processes, it goes through all processes running on the machine. Let’s try this:

$ strace -o w.trace w | grep whatever

Inside the trace we find lines like these (on a Linux system):

open("/proc/8286/cmdline", O_RDONLY)    = 4
read(4, "grepwhatever", 2047)       = 14

Which shows w explicitly going through /proc and looking at the command lines of all processes (and other things, not shown). It finds the grep that runs parallel to it and that’s what strace sees it do. The pipe has nothing to do with it, other than starting both processes at the same time. In a way, it is similar to ps |¬†grep seeing the grep itself.

who and most other commands don’t need the information about the processes, and don’t go looking, so you don’t see the same when tracing them.

Answered By: ilkkachu
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.