And from an Upstream POV, there's an even more brittle case. Ubuntu happens to have /var/run on a tmpfs, so /var/run/utmp either doesn't exist, is empty, or contains a runlevel (after rcS.d has finished).
But that's fairly unique to Ubuntu; other distros like Debian and Fedora probably don't do this.
So during rcS.d, instead of not existing, /var/run/utmp exists and contains data from when the machine was last booted!
During shutdown we do put a record in there to wipe the runlevel information, but if the machine wasn't cleanly shut down, runlevel might return "N 2" during rcS.d before /var/run/utmp is wiped!
And from an Upstream POV, there's an even more brittle case. Ubuntu happens to have /var/run on a tmpfs, so /var/run/utmp either doesn't exist, is empty, or contains a runlevel (after rcS.d has finished).
But that's fairly unique to Ubuntu; other distros like Debian and Fedora probably don't do this.
So during rcS.d, instead of not existing, /var/run/utmp exists and contains data from when the machine was last booted!
During shutdown we do put a record in there to wipe the runlevel information, but if the machine wasn't cleanly shut down, runlevel might return "N 2" during rcS.d before /var/run/utmp is wiped!
So, in summary, don't call runlevel during rcS.d