Wandering Thoughts archives


Unix's special way of marking login shells goes back to V2 Unix (at least)

Many Unix shells have some command line argument that tells them that they are a login shell; for example, Bash has '-l' (also '--login'), and I think that '-l' has become the de facto standard. However, this argument is not how Unix programs like sshd and login actually tell your shell that it's a login shell; instead, for a long time, a login shell has a '-' as the first character of its program name, or to put it another way, argv[0][0] is '-'.

Recently I wound up wondering how far back this approach goes. The answer turns out to be more interesting than I expected, and what exactly is done has changed over time. First, as you might expect, this behavior is in V7 Unix, where login.c sticks a '-' on the front of your shell's name (and is confident that your /etc/passwd shell entry will never be more than 14 characters long). In V6 and V5, we have the C source for login (V6, V5), and both versions simply set argv[0] to '-'. I don't think we have user level source for V4 or V3, but it turns out that we do have assembly source for V2's login.s, and to my surprise, it is still following the same approach of having argv[0] be '-' (visible in the definitions of 'mshell' and 'shellp').

So this approach to marking login shells goes back to pretty much the dawn of Unix, and might even be in V1 (again, I don't think we have user level source for V1). Unix has been doing it this way since at least mid 1972, or fifty years now, and for incremental historical compatibility, your system is probably still doing it today.

(Even OpenBSD, the modern Unix I would most expect to have modernized how login shells are marked, has left this intact in their login source code.)

PS: To my surprise, as late as 4.3 BSD, login.c is still assuming that your /etc/passwd shell is 14 characters or less (or otherwise it will overflow a buffer). This finally gets fixed in 4.4 BSD. This is also not fixed in the System III login.c. I haven't looked at System V, since source for that is less readily accessible.

unix/LoginShellMarkerHistory written at 21:53:14; Add Comment

Page tools: See As Normal.
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.