/*******************************************mingetty.c********************************************/
/* name of this program (argv[0]) */
static char *progname;
/* on which tty line are we sitting? (e.g. tty1) */
static char *tty;
/* some information about this host */
static struct utsname uts;
/* the hostname */
static char hn[MAXHOSTNAMELEN + 1];
/* process and session ID of this program */
static pid_t pid, sid;
/* login program invoked */
static char *loginprog = "/bin/login";
/* Do not send a reset string to the terminal. */
static int noclear = 0;
/* Do not print a newline. */
static int nonewline = 0;
/* Do not print /etc/issue. */
static int noissue = 0;
/* Do not call vhangup() on the tty. */
static int nohangup = 0;
/* Do not print any hostname. */
static int nohostname = 0;
/* Print the whole string of gethostname() instead of just until the next "." */
static int longhostname = 0;
/* time to wait, seconds */
static int delay = 0;
/* chroot directory */
static char *ch_root = NULL;
/* working directory to change into */
static char *ch_dir = NULL;
/* 'nice' level of the program */
static int priority = 0;
/* automatic login with this user */
static char *autologin = NULL;
/* update_utmp() - update our utmp entry */
static void update_utmp (void)
{
struct utmp ut;
struct utmp *utp;
time_t cur_time;
setutent ();
while ((utp = getutent ()))
if (utp->ut_type == INIT_PROCESS && utp->ut_pid == pid)
break;
if (utp) {
memcpy (&ut, utp, sizeof (ut));
} else {
/* some inits don't initialize utmp... */
const char *x = tty;
memset (&ut, 0, sizeof (ut));
if (strncmp (x, "tty", 3) == 0)
x += 3;
if (strlen (x) > sizeof (ut.ut_id))
x += strlen (x) - sizeof (ut.ut_id);
strncpy (ut.ut_id, x, sizeof (ut.ut_id));
}
strncpy (ut.ut_user, "LOGIN", sizeof (ut.ut_user));
strncpy (ut.ut_line, tty, sizeof (ut.ut_line));
time (&cur_time);
ut.ut_time = cur_time;
ut.ut_type = LOGIN_PROCESS;
ut.ut_pid = pid;
ut.ut_session = sid;
pututline (&ut);
endutent ();
updwtmp (_PATH_WTMP, &ut);
}
/* open_tty - set up tty as standard { input, output, error } */
static void open_tty (void)
{
struct sigaction sa, sa_old;
char buf[40];
int fd;
//得到要打开的tty终端名
if (tty[0] == '/')
strcpy (buf, tty);
else {
strcpy (buf, "/dev/");
strcat (buf, tty);
}
//修改设备文件属性,使其可以访问
if (chown (buf, 0, 0) || chmod (buf, 0600))
if (errno != EROFS)
error ("%s: %s", tty, strerror (errno));
sa.sa_handler = SIG_IGN;
sa.sa_flags = 0;
sigemptyset (&sa.sa_mask);
sigaction (SIGHUP, &sa, &sa_old);//终端关闭发出SIGHUP信号,忽略此信号
//打开tty终端设备,缺省波特率是115200
if ((fd = open (buf, O_RDWR, 0)) < 0)
error ("%s: cannot open tty: %s", tty, strerror (errno));
if (ioctl (fd, TIOCSCTTY, (void *) 1) == -1)
error ("%s: no controlling tty: %s", tty, strerror (errno));
if (!isatty (fd))
error ("%s: not a tty", tty);
//
if (nohangup == 0) {
if (vhangup ())
error ("%s: vhangup() failed", tty);
close (2);
close (1);
close (0);
close (fd);
if ((fd = open (buf, O_RDWR, 0)) != 0)
error ("%s: cannot open tty: %s", tty,strerror (errno));
if (ioctl (fd, TIOCSCTTY, (void *) 1) == -1)
error ("%s: no controlling tty: %s", tty,strerror (errno));
}
//将标准输入输出出错都复制给tty终端
if (dup2 (fd, 0) != 0 || dup2 (fd, 1) != 1 || dup2 (fd, 2) != 2)
error ("%s: dup2(): %s", tty, strerror (errno));
if (fd > 2)
close (fd);
if (noclear == 0)
write (0, "\033c", 2);
//恢复原来SIGHUP的信号处理
sigaction (SIGHUP, &sa_old, NULL);
}
static void output_special_char (unsigned char c)
{
switch (c) {
case 's':
printf ("%s", uts.sysname);
break;
case 'n':
printf ("%s", uts.nodename);
break;
case 'r':
printf ("%s", uts.release);
break;
case 'v':
printf ("%s", uts.version);
break;
case 'm':
printf ("%s", uts.machine);
break;
case 'o':
printf ("%s", uts.domainname);
break;
case 'd':
case 't':
{
time_t cur_time;
struct tm *tm;
#if 0
char buff[20];
time (&cur_time);
tm = localtime (&cur_time);
strftime (buff, sizeof (buff),
c == 'd'? "%a %b %d %Y" : "%X", tm);
fputs (buff, stdout);
break;
#else
time (&cur_time);
tm = localtime (&cur_time);
if (c == 'd') /* ISO 8601 */
printf ("%d-%02d-%02d", 1900 + tm->tm_year,tm->tm_mon + 1, tm->tm_mday);
else
printf ("%02d:%02d:%02d", tm->tm_hour,tm->tm_min, tm->tm_sec);
break;
#endif
}
case 'l':
printf ("%s", tty);
break;
case 'u':
case 'U':
{
int users = 0;
struct utmp *ut;
setutent ();
while ((ut = getutent ()))
if (ut->ut_type == USER_PROCESS)
users++;
endutent ();
printf ("%d", users);
if (c == 'U')
printf (" user%s", users == 1 ? "" : "s");
break;
}
default:
putchar (c);
}
}
static void do_prompt (int showlogin)
{
FILE *fd;
int c;
if (nonewline == 0)
putchar ('\n');
if (noissue == 0 && (fd = fopen ("/etc/issue", "r"))) {//存有Arago图案,打印此登录图案
while ((c = getc (fd)) != EOF) {
if (c == '\\')
output_special_char (getc (fd));
else
putchar (c);
}
fclose (fd);
}
if (nohostname == 0)
printf ("%s ", hn);
if (showlogin)
printf ("login: ");
fflush (stdout);
}
static char *get_logname (void)
{
static char logname[40];
char *bp;
unsigned char c;
tcflush (0, TCIFLUSH); /* flush pending input */
for (*logname = 0; *logname == 0;) {
do_prompt (1);//打印登录图案,argo:
for (bp = logname;;) {
if (read (0, &c, 1) < 1) {//从标准输入里读入一个字符
if (errno == EINTR || errno == EIO|| errno == ENOENT)
exit (EXIT_SUCCESS);
error ("%s: read: %s", tty, strerror (errno));
}
if (c == '\n' || c == '\r') {//回车或换行符号表示结束
*bp = 0;
break;
} else if (!isprint (c))
error ("%s: invalid character 0x%x in login"" name", tty, c);
else if ((size_t)(bp - logname) >= sizeof (logname) - 1)
error ("%s: too long login name", tty);
else
*bp++ = c;
}
}
return logname;
}
static struct option const long_options[] = {
{ "autologin", required_argument, NULL, 'a' },
{ "chdir", required_argument, NULL, 'w' },
{ "chroot", required_argument, NULL, 'r' },
{ "delay", required_argument, NULL, 'd' },
{ "noclear", no_argument, &noclear, 1 },
{ "nonewline", no_argument, &nonewline, 1 },
{ "noissue", no_argument, &noissue, 1 },
{ "nohangup", no_argument, &nohangup, 1 },
{ "no-hostname", no_argument, &nohostname, 1 }, /* compat option */
{ "nohostname", no_argument, &nohostname, 1 },
{ "loginprog", required_argument, NULL, 'l' },
{ "long-hostname", no_argument, &longhostname, 1 },
{ "nice", required_argument, NULL, 'n' },
{ 0, 0, 0, 0 }
};
int main (int argc, char **argv)
{
char *logname, *s;
int c;
progname = argv[0];//程序名称,即mingetty
if (!progname)
progname = "mingetty";
/*struct utsname
{
char sysname[_UTSNAME_SYSNAME_LENGTH];//当前操作系统名
char nodename[_UTSNAME_NODENAME_LENGTH];//网络上的名称
char release[_UTSNAME_RELEASE_LENGTH];//当前发布级别
char version[_UTSNAME_VERSION_LENGTH];//当前发布版本
char machine[_UTSNAME_MACHINE_LENGTH];//当前硬件体系类型
char domainname[_UTSNAME_DOMAIN_LENGTH]; //当前域名
};*/
uname (&uts);//获取当前内核名称和其它信息,并存于utsname结构中
gethostname (hn, MAXHOSTNAMELEN);//本地主机的标准主机名,存到数组hn中
hn[MAXHOSTNAMELEN] = '\0';
pid = getpid ();//取得进程ID
sid = getsid (0);//get the process group ID of session leader
putenv ("TERM=linux");//把字符串加到当前环境中,设置的环境仅对程序本身有效
//解析命令行选项参数。
//字符串optstring可以下列元素:
//单个字符,表示选项,
//单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。
//单个字符后跟两个冒号,表示该选项后可以有参数也可以没有参数。如果有参数,参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(这个特性是GNU的扩张)。
while ((c = getopt_long (argc, argv, "a:d:l:n:w:r:", long_options,(int *) 0)) != EOF) {
switch (c) {
case 0:
break;
case 'a':
autologin = optarg;//获得参数-a后面的参数
break;
case 'd':
delay = atoi (optarg);
break;
case 'l':
loginprog = optarg;
break;
case 'n':
priority = atoi (optarg);
break;
case 'r':
ch_root = optarg;
break;
case 'w':
ch_dir = optarg;
break;
default:
usage ();
}
}
//s指针保存主机名
if (longhostname == 0 && (s = strchr (hn, '.')))
*s = '\0';
//获得终端结构,例如ttyS0等
tty = argv[optind];
if (!tty)
usage ();
//终端名添加"/dev/",变成/dev/ttyS0
if (strncmp (tty, "/dev/", 5) == 0)
tty += 5;
update_utmp ();//更新登录信息
if (delay)
sleep (delay);
open_tty ();//打开终端设备
if (autologin) { //如果前边参数为-a且-a后边带有登录名,则会设置autologin即自动登录
do_prompt (0);//打印登录图形
printf ("login: %s (automatic login)\n", autologin);
logname = autologin;//获得自动登录名
} else//不是自动登录
while ((logname = get_logname ()) == 0);//获得登录名
if (ch_root)
chroot (ch_root);
if (ch_dir)
chdir (ch_dir);
if (priority)
nice (priority);
//带着登录名参数,执行loginprog=/bin/login程序登录,即login--logname
execl (loginprog, loginprog, autologin? "-f" : "--", logname, NULL);
/*login函数片段
。。。。。。。
strcpy(buff, "exec ");
strcat(buff, pwd->pw_shell);
childArgv[childArgc++] = "/bin/sh";
childArgv[childArgc++] = "-sh";
childArgv[childArgc++] = "-c";
childArgv[childArgc++] = buff;
childArgv[childArgc++] = NULL;
//登录成功,执行/bin/sh进入shell
execvp(childArgv[0], childArgv + 1);
*/
//启动shell后,首先启动 /etc/profile 文件,然后再启动用户目录下的 ~/.bash_profile、 ~/.bash_login或 ~/.profile文件中的其中一个,执行的顺序为:~/.bash_profile、 ~/.bash_login、 ~/.profile。如果 ~/.bash_profile文件存在的话,一般还会执行 ~/.bashrc文件。
error ("%s: can't exec %s: %s", tty, loginprog, strerror (errno));
sleep (5);
exit (EXIT_FAILURE);
}