[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: who -r

From: Bob Proulx
Subject: Re: who -r
Date: Sun, 16 Apr 2006 12:06:14 -0600
User-agent: Mutt/1.5.9i

ray hammond wrote:
> After I emailed you I grabbed the source for both. 
> Extracts from the source are below.  
> ---------------------------------------------
> runlevel source
> prev = ut->ut_pid / 256;
> if (prev == 0) prev = 'N';
> printf("%c %c\n", prev, ut->ut_pid % 256);
> ---------------------------------------------
> who -r source
> unsigned char last = UT_PID (utmp_ent) / 256;
> sprintf (comment, "%s%c", _("last="), (last == 'N') ? 'S' : last);

I will deduce from this that the level logged to utmp for last on your
system must have been 'N'.

> As you can see, both commands print different values for the previous
> runlevel.  It would be great if both adhered to the same logic, however as
> you indicate "legacy" must explain the differences.

Yes that would be nice.  I tried this on HP-UX as an example of a
system representing the legacy Unix behavior and it printed S for the
previous run level.  The 'runlevel' command is newer and does not
appear there.  I would assume that compatibility was the underlying
reason for this behavior.

HP-UX who -r output:
   .       run-level 3  Mar 25 13:57    3    0    S

BSD does not include a -r option.  FreeBSD who -r output:
  who: illegal option -- r
  usage: who [-HmqsTu] [am I] [file]

The standards only poorly describe the behavior of who.


Without any standards to guide us this really must be taken
implementation by implementation.

> Also you pointed out that the manual for runlevel stats "If there is no
> previous system runlevel, the letter N will be printed instead".  

That is an interesting test.  Because runlevel tests for a zero
character value.  This is a different test than who's test for a 'N'
character value.  In fact I think it is not a useful test.  Because
if the previous value were really zero then we would see a null
character written from who's output.  Because we don't see that I
presume that systems don't actually return a zero character for the
last run level.  That test in runlevel is very likely an untriggered
test on systems.  It probably does not need to be there.  But being a
paranoid programmer is often a good thing.

> My understanding of the code for "who -r" is that the code uses tenary
> operator i.e. (last == 'N') ? 'S' : last)), to substitute the value of "N"
> (when found) for the value of "S".

It is the other way around.  If the value of last is 'N' then return
'S', otherwise return the value of last.  The Ns become Ss in that

> Again I guess legacy, or do all Linux/Unix O/S boot into single user
> mode first?

As I recall init moves through intervening run levels when transiting
to a new run level.  Therefore moving to run level 2 (multi user mode)
would move through run level 1 (single user mode).  That implies to me
that most systems would have been in single user mode before being in
multi user mode.  But I could see a system implementing it differently.

Run levels are really nothing more than an artificial construct of the
init process to describe what to do when bringing the system online.
They have been very useful.  The style definitely reflects the small
machine programming model of the 1970s.  But there is a lot of recent
discussion that their time is past and that they should be replaced
with something more spiffy.  I personally don't think about run levels
very much.  The system works.  (shrug)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]