Detect Linux Runlevel

Marko Rauhamaa marko at pacujo.net
Tue Dec 6 17:29:00 EST 2016


Tim Chase <python.list at tim.thechases.com>:

> This works based on my poking at it in both Py2 and Py3:

Great info, Tim.

A word a warning: your code doesn't lock /var/run/utmp before access,
which is a race condition. The file may be updated at any time, and
ordinary file reads may yield corrupted records.

The library functions getutline(), pututline() etc lock the file
internally (although that is not documented). You can see the (advisory
record) locking in action by running the command

   strace who


Since the locking scheme hasn't been documented, the only safe way to
read /var/run/utmp is through the C API functions.

Another thing is that, as stated before, the runlevel business is
legacy. It is still supported by systemd-update-utmp, but for how long
is anybody's guess.


Marko


More information about the Python-list mailing list