Title: Lecture 18 Process Relationships
1Lecture 18Process Relationships
CSCE 510 Systems Programming
- Topics
- Logins
- Process Groups and Sessions
- Controlling terminals
- Job Control
- Readings Chapter 9
October 25, 2005
2- Last Time
- x
- Today
- Terminal logins
- Network logins
- Process groups
- Process sessions
- Controlling terminals
3Terminal logins
- BSD logins
- Real user ID 0
- Effective user ID 0
- Getty calls to open the terminal
- File descriptors 0,1,2 set to the device
Process ID 1
init
Forks once per terminal
fork
init
Each child execs getty
exec
getty
4Terminal logins continued
Process ID 1
Reads /etc/ttys Forks once per
terminal Creates empty env
init
- Getty reads login_name
- Execs login
fork
init
exec
Opens /dev/ttyx File descr. 0,1,2 Reads user name
getty
exec
login
5Logins that Fail
- Getpwname to fetch passwd entry
- Call getpass to read passwd without echoing
- Call crypt to encrypt the password entered
- Check against encrypted pw_passwd from passwd
entry of the shadow file - Failure ?
- login calls exit(1)
- Child send signal SIGCHLD to init
- Init forks again followed by the exec of getty
6Logins that Suceed
- Chdir to home directory
- Chown on the terminal so user owns it
- Change access permissions so user can read and
write to terminal - Set group IDs setgid, initgroups
- Initialize Environment from pw entry
- HOME
- SHELL
- USER
- PATH
- Change user ID using setuid
- Exec shell
7After Terminal login
Process ID 1
init
- BSD logins
- Real user ID 0
- Effective user ID 0
- Getty calls to open the terminal
fork
Login shell
fd 0,1,2
Terminal Device driver
User at the terminal
8Linux and Solaris terminal logins
- Linux - Similar but look at /etc/inittab for
information specifying the terminal devices for
which init should start a getty process - Solaris ttymon part of a facility to provide
consistent administrative services - - overall procedure is same but the parent
of the logon process will be ttymon instead of
init
9Network Logins Set up
10Network Logins After Set Up
11Process groups
- In addition to unique process ID each process
belongs to a process group - Each process group has a leader
- A process can join a group using the setpgid
syscall - In setpgid(pid_t pid, pid_t pgid)
- Children inherit the group from the parent
- A process can set the process group ID of its
children up until they exec -
12Process Sessions
- A session is one or more process groups
- Example figure 9.6
- proc1 proc2 / put in
background / - proc3 proc4 proc5
shell
proc1
proc2
proc3
proc4
proc5
Process group1
Process group2
Process group3
A Session (from APU2E fig 9.6)
13Setsid -
- pid_t setsid(void)
- Process becomes the session leader
- Process becomes new group leader
- Process has no controlling terminal
- System V Release 4 (SVR4) concept, eventually
integrated into BSD - pid_t getsid(pid_t pid) return session leaders
group ID - Security issues - restrictions
14Controlling Terminal
- A session can have a single controlling terminal
- This is usually the terminal or psuedo-terminal
on which we log in - The session leader that establishes connection is
called the controlling process - Process groups
- Then it has a single foreground process group
- One or more background groups
- If session has controlling terminal then it has a
single foreground process group
15Signals from Controlling Terminal
- Certain keys cause the controlling terminal to
send signals to all the processes in the
foreground group - C (at USC sometimes DELETE) sends SIGINT
- \ causes a SIGQUIT to be sent
- If a modem or network disconnect is detected the
controlling terminal sends a SIGHUP to the
controlling process
16Signals from Controlling Terminal
Session
shell
proc1
proc2
proc3
proc4
proc5
Background Process group Session leader
Background Process group
Foreground Process group
Terminal input Terminal-generated
signals
SIGHUP On modem or network disconnect
Controlling terminal
17Opening the Controlling Terminal
- Sometimes a process needs to make sure it is
talking to the controlling terminal - man -s 3 getpass
- This function is obsolete. Do not use it!
- The getpass() function
- opens /dev/tty (the controlling terminal of the
process), - outputs the string prompt,
- turns off echoing,
- reads one line (the "password"),
- restores the terminal state and
- closes /dev/tty again.
18Crypt and its Problems
- Practical Unix Internet Security, 3rd Edition
(Paperback)by Simson Garfinkel, Gene Spafford,
Alan Schwartz
19Functions Communicating Foreground Status
- pid_t tcgetpgrp (int filedes)
- Returns PID of foreground process group
- int tcsetpgrp (int filedes, pid_t pgrpid)
- pid_t tcgetsid (int filedes)
- Returns session leaders process group
20Job Control
- Started in BSD around 1980
- on end of command puts it in background
- Z (suspend character) to send SIGTSTP to stop
foreground process - bg puts the job stopped into background
- jobs command allows bringing background job to
foreground
21Support for Job Control
- Jobs control requires support at three levels
- The kernel must support job control signals
- The terminal driver in the kernel must support
job control - A shell must support job control
22Job Control Example
- thetisgt cat gt ooo
- 1 24839
- thetisgt jobs
- 1 Stopped cat gtooo
- thetisgt fg 1
- cat gtooo
- hello world
- D // D makes read return EOF
- thetisgt
23Summary of Job Control Features
24Orphan3.c
- include "apue.h"
- include lterrno.hgt
- static void
- sig_hup(int signo)
-
- printf("SIGHUP received, pid d\n", getpid())
-
- static void
- pr_ids(char name)
-
- printf("s pid d, ppid d, pgrp d,
tpgrp d\n", - name, getpid(), getppid(), getpgrp(),
tcgetpgrp(STDIN_FILENO)) - fflush(stdout)
25Orphan3.c cont.
- Int main(void)
- char c
- pid_t pid
- pr_ids("parent")
- if ((pid fork()) lt 0) err_sys("fork
error") - else if (pid gt 0) / parent /
- sleep(5) / sleep to let child stop itself /
- exit(0) / then parent exits /
- else / child /
- pr_ids("child")
- signal(SIGHUP, sig_hup) / setup signal
handler / - kill(getpid(), SIGTSTP) / stop ourself /
- pr_ids("child") / prints only if we're
continued / - if (read(STDIN_FILENO, c, 1) ! 1)
- printf("read error from controlling TTY,
- errno d\n", errno)
- exit(0)
-
26Next Time Daemon Processes
- Daemons are processes that live for a long time
- Usually start when the system is booted and pny
terminate just before the system shutsdown - They dont have a controlling terminal
- No SIGINTs etc generate from keyboard
- Run in background
27(No Transcript)