Sunday, February 22, 2009

getty login

Login in from terminal or modem (remote) connecting via RS232 connection comes from terminal driver in the kernel. Terminal device configuration are defined in /etc/ttys by the administrator. When the system boots, it creates the first process, init (PID=1). init reads /etc/ttys and for each termial devices that allows login, init fork-exec program getty.

getty is run with superuser privilege (UID=0). It open the terminal in RW mode. getty sets file descriptor 0, 1 and 2 to the terminal device driver. It then outputs the login prompt. Once the user enters its userid, getty exec program login to handle the input.

login prompts the user for the password and uses crypt to encrypt the password entered for checking. If the password is invalid, login will exit with code 1 after a few tries. Control passed back to init and it fork-exec the getty again to restart the process. If the password check is successful, login will set up the user environment and changes to the user's ID before it invoke the shell using execl call - ("/bin/sh", "-sh", (char *) 0). The minus sign is a flag to tell all shells that they are being invoked as a login shell. Finally, the login shell reads the start up files (e.g. .profile) before displaying the first prompt for user.

No comments: