分类:
2008-11-28 13:58:46
chapter 9 exercises
|
Refer back to our discussion of the utmp and wtmp files in . Why are the logout records written by the init process? Is this
handled the same way for a network login? |
答案:utmp和wtmp记录了所有用的的login和logout信息。对于使用terminal device进行的登陆,由于每个terminal对应一个getty,后来又exec成了login shell, 当这个用户logout时,login shell退出,init会收到SIGCHLD信号。所以init就可以写入用户登出信息。
对于使用network login的用户,他们也有login shell,但是这个login shell并不是由init直接 fork出来的getty exec得到的。而是由init fork出一个进程执行/bin/sh,然后执行/etc/rc,它spawn出一个daemon即inetd, 然后inetd fork出telnetd, telnetd进程fork出的进程执行login然后执行login shell得到的。所以network登陆的login shell的父亲是telnetd。所以网络登陆的信息由telnetd负责填写
9.2 |
Write a small program that calls fork and has the child create a new
session. Verify that the child becomes a process group leader and that the
child no longer has a controlling terminal. |
。#include
#include
#include
#include
int main()
{
int pid = fork();
if( pid < 0 )
exit(0);
setbuf(stdout,NULL);
if( pid == 0 )
{
//child
puts("this is the child");
pid_t sid = setsid();
if( sid>0 )
printf("chid session id : %d, pid : %d, ppid: %d, pgid:%d, freground grp id:%d\n", sid, getpid(), getppid(), getpgid(getpid()), tcgetpgrp(STDIN_FILENO) );
if( errno == ENOTTY )
puts("child has no tty");
puts("chid exit");
exit(0);
}
printf("father session id : %d, pid : %d, ppid: %d, pgid:%d, freground grp id:%d\n", getsid(getpid()), getpid(), getppid(), getpgid(getpid()), tcgetpgrp(STDIN_FILENO) );
}
本程序通过调用tcgetpgrp(STDIN_FILENO),由于该函数出错,然后产看errno,当等于ENOTTY时及表明STDIN_FILENO就不是controlling terminal。由于我们并没有进行任何redirection,因此可见这个session已经断开了与controlling terminal的连接。
除此方法,没有发现别的能够检查是否有controlling terminal的方法。
输出结果:
shaoting@desktopbj-LabSD:/home/shaoting/mytest> ./a.out
this is the child
chid session id : 6717, pid : 6717, ppid: 6716, pgid:6717, freground grp id:-1
child has no tty
chid exit
father session id : 5086, pid : 6716, ppid: 5094, pgid:6716, freground grp id:6716
shaoting@desktopbj-LabSD:/home/shaoting/mytest>