]> git.ipfire.org Git - thirdparty/glibc.git/blame - manual/job.texi
posix: Fix tst-spawn.c issue from commit 805334b26c
[thirdparty/glibc.git] / manual / job.texi
CommitLineData
085d0e35 1@node Job Control, Name Service Switch, Inter-Process Communication, Top
7a68c94a 2@c %MENU% All about process groups and sessions
28f540f4
RM
3@chapter Job Control
4
5@cindex process groups
6@cindex job control
7@cindex job
8@cindex session
9@dfn{Job control} refers to the protocol for allowing a user to move
10between multiple @dfn{process groups} (or @dfn{jobs}) within a single
11@dfn{login session}. The job control facilities are set up so that
12appropriate behavior for most programs happens automatically and they
13need not do anything special about job control. So you can probably
14ignore the material in this chapter unless you are writing a shell or
15login program.
16
17You need to be familiar with concepts relating to process creation
18(@pxref{Process Creation Concepts}) and signal handling (@pxref{Signal
19Handling}) in order to understand this material presented in this
20chapter.
21
26756e57
ZW
22@vindex _POSIX_JOB_CONTROL
23Some old systems do not support job control, but @gnusystems{} always
24have, and it is a required feature in the 2001 revision of POSIX.1
25(@pxref{POSIX}). If you need to be portable to old systems, you can
26use the @code{_POSIX_JOB_CONTROL} macro to test at compile-time
27whether the system supports job control. @xref{System Options}.
28
28f540f4
RM
29@menu
30* Concepts of Job Control:: Jobs can be controlled by a shell.
28f540f4
RM
31* Controlling Terminal:: How a process gets its controlling terminal.
32* Access to the Terminal:: How processes share the controlling terminal.
33* Orphaned Process Groups:: Jobs left after the user logs out.
34* Implementing a Shell:: What a shell must do to implement job control.
35* Functions for Job Control:: Functions to control process groups.
36@end menu
37
26756e57 38@node Concepts of Job Control
28f540f4
RM
39@section Concepts of Job Control
40
41@cindex shell
42The fundamental purpose of an interactive shell is to read
43commands from the user's terminal and create processes to execute the
44programs specified by those commands. It can do this using the
45@code{fork} (@pxref{Creating a Process}) and @code{exec}
46(@pxref{Executing a File}) functions.
47
48A single command may run just one process---but often one command uses
49several processes. If you use the @samp{|} operator in a shell command,
50you explicitly request several programs in their own processes. But
51even if you run just one program, it can use multiple processes
52internally. For example, a single compilation command such as @samp{cc
53-c foo.c} typically uses four processes (though normally only two at any
54given time). If you run @code{make}, its job is to run other programs
55in separate processes.
56
57The processes belonging to a single command are called a @dfn{process
58group} or @dfn{job}. This is so that you can operate on all of them at
59once. For example, typing @kbd{C-c} sends the signal @code{SIGINT} to
60terminate all the processes in the foreground process group.
61
62@cindex session
63A @dfn{session} is a larger group of processes. Normally all the
6d52618b 64processes that stem from a single login belong to the same session.
28f540f4
RM
65
66Every process belongs to a process group. When a process is created, it
67becomes a member of the same process group and session as its parent
68process. You can put it in another process group using the
69@code{setpgid} function, provided the process group belongs to the same
70session.
71
72@cindex session leader
73The only way to put a process in a different session is to make it the
74initial process of a new session, or a @dfn{session leader}, using the
75@code{setsid} function. This also puts the session leader into a new
76process group, and you can't move it out of that process group again.
77
78Usually, new sessions are created by the system login program, and the
79session leader is the process running the user's login shell.
80
81@cindex controlling terminal
82A shell that supports job control must arrange to control which job can
83use the terminal at any time. Otherwise there might be multiple jobs
84trying to read from the terminal at once, and confusion about which
85process should receive the input typed by the user. To prevent this,
86the shell must cooperate with the terminal driver using the protocol
87described in this chapter.
88
89@cindex foreground job
90@cindex background job
91The shell can give unlimited access to the controlling terminal to only
92one process group at a time. This is called the @dfn{foreground job} on
93that controlling terminal. Other process groups managed by the shell
94that are executing without such access to the terminal are called
95@dfn{background jobs}.
96
97@cindex stopped job
98If a background job needs to read from its controlling
99terminal, it is @dfn{stopped} by the terminal driver; if the
100@code{TOSTOP} mode is set, likewise for writing. The user can stop
101a foreground job by typing the SUSP character (@pxref{Special
102Characters}) and a program can stop any job by sending it a
103@code{SIGSTOP} signal. It's the responsibility of the shell to notice
104when jobs stop, to notify the user about them, and to provide mechanisms
105for allowing the user to interactively continue stopped jobs and switch
106jobs between foreground and background.
107
108@xref{Access to the Terminal}, for more information about I/O to the
d7245797 109controlling terminal.
28f540f4 110
26756e57 111@node Controlling Terminal
28f540f4
RM
112@section Controlling Terminal of a Process
113
114One of the attributes of a process is its controlling terminal. Child
115processes created with @code{fork} inherit the controlling terminal from
116their parent process. In this way, all the processes in a session
117inherit the controlling terminal from the session leader. A session
118leader that has control of a terminal is called the @dfn{controlling
119process} of that terminal.
120
121@cindex controlling process
122You generally do not need to worry about the exact mechanism used to
123allocate a controlling terminal to a session, since it is done for you
124by the system when you log in.
125@c ??? How does GNU system let a process get a ctl terminal.
126
127An individual process disconnects from its controlling terminal when it
128calls @code{setsid} to become the leader of a new session.
129@xref{Process Group Functions}.
130
131@c !!! explain how it gets a new one (by opening any terminal)
132@c ??? How you get a controlling terminal is system-dependent.
133@c We should document how this will work in the GNU system when it is decided.
134@c What Unix does is not clean and I don't think GNU should use that.
135
136@node Access to the Terminal, Orphaned Process Groups, Controlling Terminal, Job Control
137@section Access to the Controlling Terminal
138@cindex controlling terminal, access to
139
140Processes in the foreground job of a controlling terminal have
6d52618b 141unrestricted access to that terminal; background processes do not. This
28f540f4
RM
142section describes in more detail what happens when a process in a
143background job tries to access its controlling terminal.
144
145@cindex @code{SIGTTIN}, from background job
146When a process in a background job tries to read from its controlling
147terminal, the process group is usually sent a @code{SIGTTIN} signal.
148This normally causes all of the processes in that group to stop (unless
149they handle the signal and don't stop themselves). However, if the
150reading process is ignoring or blocking this signal, then @code{read}
151fails with an @code{EIO} error instead.
152
153@cindex @code{SIGTTOU}, from background job
154Similarly, when a process in a background job tries to write to its
155controlling terminal, the default behavior is to send a @code{SIGTTOU}
156signal to the process group. However, the behavior is modified by the
157@code{TOSTOP} bit of the local modes flags (@pxref{Local Modes}). If
158this bit is not set (which is the default), then writing to the
159controlling terminal is always permitted without sending a signal.
160Writing is also permitted if the @code{SIGTTOU} signal is being ignored
161or blocked by the writing process.
162
163Most other terminal operations that a program can do are treated as
164reading or as writing. (The description of each operation should say
165which.)
166
167For more information about the primitive @code{read} and @code{write}
168functions, see @ref{I/O Primitives}.
169
170
171@node Orphaned Process Groups, Implementing a Shell, Access to the Terminal, Job Control
172@section Orphaned Process Groups
173@cindex orphaned process group
174
175When a controlling process terminates, its terminal becomes free and a
176new session can be established on it. (In fact, another user could log
177in on the terminal.) This could cause a problem if any processes from
178the old session are still trying to use that terminal.
179
180To prevent problems, process groups that continue running even after the
181session leader has terminated are marked as @dfn{orphaned process
182groups}.
183
184When a process group becomes an orphan, its processes are sent a
185@code{SIGHUP} signal. Ordinarily, this causes the processes to
186terminate. However, if a program ignores this signal or establishes a
187handler for it (@pxref{Signal Handling}), it can continue running as in
188the orphan process group even after its controlling process terminates;
189but it still cannot access the terminal any more.
190
191@node Implementing a Shell, Functions for Job Control, Orphaned Process Groups, Job Control
192@section Implementing a Job Control Shell
193
194This section describes what a shell must do to implement job control, by
195presenting an extensive sample program to illustrate the concepts
196involved.
197
198@iftex
199@itemize @bullet
6d52618b 200@item
28f540f4
RM
201@ref{Data Structures}, introduces the example and presents
202its primary data structures.
203
204@item
205@ref{Initializing the Shell}, discusses actions which the shell must
206perform to prepare for job control.
207
208@item
209@ref{Launching Jobs}, includes information about how to create jobs
210to execute commands.
211
212@item
213@ref{Foreground and Background}, discusses what the shell should
214do differently when launching a job in the foreground as opposed to
215a background job.
216
217@item
218@ref{Stopped and Terminated Jobs}, discusses reporting of job status
219back to the shell.
220
221@item
222@ref{Continuing Stopped Jobs}, tells you how to continue jobs that
223have been stopped.
224
225@item
226@ref{Missing Pieces}, discusses other parts of the shell.
227@end itemize
228@end iftex
229
230@menu
231* Data Structures:: Introduction to the sample shell.
232* Initializing the Shell:: What the shell must do to take
233 responsibility for job control.
234* Launching Jobs:: Creating jobs to execute commands.
235* Foreground and Background:: Putting a job in foreground of background.
236* Stopped and Terminated Jobs:: Reporting job status.
237* Continuing Stopped Jobs:: How to continue a stopped job in
238 the foreground or background.
239* Missing Pieces:: Other parts of the shell.
240@end menu
241
242@node Data Structures, Initializing the Shell, , Implementing a Shell
243@subsection Data Structures for the Shell
244
245All of the program examples included in this chapter are part of
246a simple shell program. This section presents data structures
247and utility functions which are used throughout the example.
248
249The sample shell deals mainly with two data structures. The
250@code{job} type contains information about a job, which is a
251set of subprocesses linked together with pipes. The @code{process} type
252holds information about a single subprocess. Here are the relevant
253data structure declarations:
254
255@smallexample
256@group
257/* @r{A process is a single process.} */
258typedef struct process
259@{
260 struct process *next; /* @r{next process in pipeline} */
261 char **argv; /* @r{for exec} */
262 pid_t pid; /* @r{process ID} */
263 char completed; /* @r{true if process has completed} */
264 char stopped; /* @r{true if process has stopped} */
265 int status; /* @r{reported status value} */
266@} process;
267@end group
268
269@group
270/* @r{A job is a pipeline of processes.} */
271typedef struct job
272@{
273 struct job *next; /* @r{next active job} */
274 char *command; /* @r{command line, used for messages} */
275 process *first_process; /* @r{list of processes in this job} */
276 pid_t pgid; /* @r{process group ID} */
277 char notified; /* @r{true if user told about stopped job} */
278 struct termios tmodes; /* @r{saved terminal modes} */
279 int stdin, stdout, stderr; /* @r{standard i/o channels} */
280@} job;
281
282/* @r{The active jobs are linked into a list. This is its head.} */
283job *first_job = NULL;
284@end group
285@end smallexample
286
287Here are some utility functions that are used for operating on @code{job}
288objects.
289
290@smallexample
291@group
292/* @r{Find the active job with the indicated @var{pgid}.} */
293job *
294find_job (pid_t pgid)
295@{
296 job *j;
6d52618b 297
28f540f4
RM
298 for (j = first_job; j; j = j->next)
299 if (j->pgid == pgid)
300 return j;
301 return NULL;
302@}
303@end group
304
305@group
306/* @r{Return true if all processes in the job have stopped or completed.} */
307int
308job_is_stopped (job *j)
309@{
310 process *p;
6d52618b 311
28f540f4
RM
312 for (p = j->first_process; p; p = p->next)
313 if (!p->completed && !p->stopped)
314 return 0;
315 return 1;
316@}
317@end group
318
319@group
320/* @r{Return true if all processes in the job have completed.} */
321int
322job_is_completed (job *j)
323@{
324 process *p;
6d52618b 325
28f540f4
RM
326 for (p = j->first_process; p; p = p->next)
327 if (!p->completed)
328 return 0;
329 return 1;
330@}
331@end group
332@end smallexample
333
334
335@node Initializing the Shell, Launching Jobs, Data Structures, Implementing a Shell
336@subsection Initializing the Shell
337@cindex job control, enabling
338@cindex subshell
339
340When a shell program that normally performs job control is started, it
341has to be careful in case it has been invoked from another shell that is
6d52618b 342already doing its own job control.
28f540f4
RM
343
344A subshell that runs interactively has to ensure that it has been placed
345in the foreground by its parent shell before it can enable job control
346itself. It does this by getting its initial process group ID with the
347@code{getpgrp} function, and comparing it to the process group ID of the
348current foreground job associated with its controlling terminal (which
349can be retrieved using the @code{tcgetpgrp} function).
350
351If the subshell is not running as a foreground job, it must stop itself
352by sending a @code{SIGTTIN} signal to its own process group. It may not
353arbitrarily put itself into the foreground; it must wait for the user to
354tell the parent shell to do this. If the subshell is continued again,
355it should repeat the check and stop itself again if it is still not in
356the foreground.
357
358@cindex job control, enabling
359Once the subshell has been placed into the foreground by its parent
360shell, it can enable its own job control. It does this by calling
361@code{setpgid} to put itself into its own process group, and then
362calling @code{tcsetpgrp} to place this process group into the
363foreground.
364
365When a shell enables job control, it should set itself to ignore all the
366job control stop signals so that it doesn't accidentally stop itself.
367You can do this by setting the action for all the stop signals to
368@code{SIG_IGN}.
369
370A subshell that runs non-interactively cannot and should not support job
371control. It must leave all processes it creates in the same process
372group as the shell itself; this allows the non-interactive shell and its
373child processes to be treated as a single job by the parent shell. This
374is easy to do---just don't use any of the job control primitives---but
375you must remember to make the shell do it.
376
377
378Here is the initialization code for the sample shell that shows how to
379do all of this.
380
381@smallexample
382/* @r{Keep track of attributes of the shell.} */
383
384#include <sys/types.h>
385#include <termios.h>
386#include <unistd.h>
387
388pid_t shell_pgid;
389struct termios shell_tmodes;
390int shell_terminal;
391int shell_is_interactive;
392
393
394/* @r{Make sure the shell is running interactively as the foreground job}
395 @r{before proceeding.} */
396
397void
398init_shell ()
399@{
6d52618b 400
28f540f4
RM
401 /* @r{See if we are running interactively.} */
402 shell_terminal = STDIN_FILENO;
403 shell_is_interactive = isatty (shell_terminal);
404
405 if (shell_is_interactive)
406 @{
407 /* @r{Loop until we are in the foreground.} */
408 while (tcgetpgrp (shell_terminal) != (shell_pgid = getpgrp ()))
409 kill (- shell_pgid, SIGTTIN);
410
411 /* @r{Ignore interactive and job-control signals.} */
412 signal (SIGINT, SIG_IGN);
413 signal (SIGQUIT, SIG_IGN);
414 signal (SIGTSTP, SIG_IGN);
415 signal (SIGTTIN, SIG_IGN);
416 signal (SIGTTOU, SIG_IGN);
417 signal (SIGCHLD, SIG_IGN);
418
419 /* @r{Put ourselves in our own process group.} */
420 shell_pgid = getpid ();
421 if (setpgid (shell_pgid, shell_pgid) < 0)
422 @{
423 perror ("Couldn't put the shell in its own process group");
424 exit (1);
425 @}
426
427 /* @r{Grab control of the terminal.} */
428 tcsetpgrp (shell_terminal, shell_pgid);
429
430 /* @r{Save default terminal attributes for shell.} */
431 tcgetattr (shell_terminal, &shell_tmodes);
432 @}
433@}
434@end smallexample
435
436
437@node Launching Jobs, Foreground and Background, Initializing the Shell, Implementing a Shell
438@subsection Launching Jobs
439@cindex launching jobs
440
441Once the shell has taken responsibility for performing job control on
442its controlling terminal, it can launch jobs in response to commands
443typed by the user.
444
445To create the processes in a process group, you use the same @code{fork}
446and @code{exec} functions described in @ref{Process Creation Concepts}.
447Since there are multiple child processes involved, though, things are a
448little more complicated and you must be careful to do things in the
449right order. Otherwise, nasty race conditions can result.
450
451You have two choices for how to structure the tree of parent-child
452relationships among the processes. You can either make all the
453processes in the process group be children of the shell process, or you
454can make one process in group be the ancestor of all the other processes
455in that group. The sample shell program presented in this chapter uses
456the first approach because it makes bookkeeping somewhat simpler.
457
458@cindex process group leader
459@cindex process group ID
460As each process is forked, it should put itself in the new process group
461by calling @code{setpgid}; see @ref{Process Group Functions}. The first
462process in the new group becomes its @dfn{process group leader}, and its
463process ID becomes the @dfn{process group ID} for the group.
464
465@cindex race conditions, relating to job control
466The shell should also call @code{setpgid} to put each of its child
467processes into the new process group. This is because there is a
468potential timing problem: each child process must be put in the process
469group before it begins executing a new program, and the shell depends on
470having all the child processes in the group before it continues
471executing. If both the child processes and the shell call
472@code{setpgid}, this ensures that the right things happen no matter which
473process gets to it first.
474
475If the job is being launched as a foreground job, the new process group
476also needs to be put into the foreground on the controlling terminal
477using @code{tcsetpgrp}. Again, this should be done by the shell as well
478as by each of its child processes, to avoid race conditions.
479
480The next thing each child process should do is to reset its signal
481actions.
482
483During initialization, the shell process set itself to ignore job
484control signals; see @ref{Initializing the Shell}. As a result, any child
485processes it creates also ignore these signals by inheritance. This is
486definitely undesirable, so each child process should explicitly set the
487actions for these signals back to @code{SIG_DFL} just after it is forked.
488
489Since shells follow this convention, applications can assume that they
490inherit the correct handling of these signals from the parent process.
491But every application has a responsibility not to mess up the handling
492of stop signals. Applications that disable the normal interpretation of
493the SUSP character should provide some other mechanism for the user to
494stop the job. When the user invokes this mechanism, the program should
495send a @code{SIGTSTP} signal to the process group of the process, not
496just to the process itself. @xref{Signaling Another Process}.
497
498Finally, each child process should call @code{exec} in the normal way.
6d52618b 499This is also the point at which redirection of the standard input and
28f540f4
RM
500output channels should be handled. @xref{Duplicating Descriptors},
501for an explanation of how to do this.
502
503Here is the function from the sample shell program that is responsible
504for launching a program. The function is executed by each child process
505immediately after it has been forked by the shell, and never returns.
506
507@smallexample
508void
509launch_process (process *p, pid_t pgid,
510 int infile, int outfile, int errfile,
511 int foreground)
512@{
513 pid_t pid;
514
515 if (shell_is_interactive)
516 @{
517 /* @r{Put the process into the process group and give the process group}
518 @r{the terminal, if appropriate.}
519 @r{This has to be done both by the shell and in the individual}
520 @r{child processes because of potential race conditions.} */
521 pid = getpid ();
522 if (pgid == 0) pgid = pid;
523 setpgid (pid, pgid);
524 if (foreground)
525 tcsetpgrp (shell_terminal, pgid);
526
527 /* @r{Set the handling for job control signals back to the default.} */
528 signal (SIGINT, SIG_DFL);
529 signal (SIGQUIT, SIG_DFL);
530 signal (SIGTSTP, SIG_DFL);
531 signal (SIGTTIN, SIG_DFL);
532 signal (SIGTTOU, SIG_DFL);
533 signal (SIGCHLD, SIG_DFL);
534 @}
535
536 /* @r{Set the standard input/output channels of the new process.} */
537 if (infile != STDIN_FILENO)
538 @{
539 dup2 (infile, STDIN_FILENO);
540 close (infile);
541 @}
542 if (outfile != STDOUT_FILENO)
543 @{
544 dup2 (outfile, STDOUT_FILENO);
545 close (outfile);
546 @}
547 if (errfile != STDERR_FILENO)
548 @{
549 dup2 (errfile, STDERR_FILENO);
550 close (errfile);
6d52618b
UD
551 @}
552
553 /* @r{Exec the new process. Make sure we exit.} */
28f540f4
RM
554 execvp (p->argv[0], p->argv);
555 perror ("execvp");
556 exit (1);
557@}
558@end smallexample
559
560If the shell is not running interactively, this function does not do
561anything with process groups or signals. Remember that a shell not
562performing job control must keep all of its subprocesses in the same
563process group as the shell itself.
564
565Next, here is the function that actually launches a complete job.
566After creating the child processes, this function calls some other
567functions to put the newly created job into the foreground or background;
568these are discussed in @ref{Foreground and Background}.
569
570@smallexample
571void
572launch_job (job *j, int foreground)
573@{
574 process *p;
575 pid_t pid;
576 int mypipe[2], infile, outfile;
6d52618b 577
28f540f4
RM
578 infile = j->stdin;
579 for (p = j->first_process; p; p = p->next)
580 @{
581 /* @r{Set up pipes, if necessary.} */
582 if (p->next)
583 @{
584 if (pipe (mypipe) < 0)
585 @{
586 perror ("pipe");
587 exit (1);
588 @}
589 outfile = mypipe[1];
590 @}
591 else
592 outfile = j->stdout;
593
594 /* @r{Fork the child processes.} */
595 pid = fork ();
596 if (pid == 0)
597 /* @r{This is the child process.} */
598 launch_process (p, j->pgid, infile,
599 outfile, j->stderr, foreground);
600 else if (pid < 0)
601 @{
602 /* @r{The fork failed.} */
603 perror ("fork");
604 exit (1);
605 @}
606 else
607 @{
608 /* @r{This is the parent process.} */
609 p->pid = pid;
610 if (shell_is_interactive)
611 @{
612 if (!j->pgid)
613 j->pgid = pid;
614 setpgid (pid, j->pgid);
615 @}
616 @}
617
618 /* @r{Clean up after pipes.} */
619 if (infile != j->stdin)
620 close (infile);
621 if (outfile != j->stdout)
622 close (outfile);
623 infile = mypipe[0];
624 @}
6d52618b 625
28f540f4
RM
626 format_job_info (j, "launched");
627
628 if (!shell_is_interactive)
629 wait_for_job (j);
630 else if (foreground)
631 put_job_in_foreground (j, 0);
632 else
633 put_job_in_background (j, 0);
634@}
635@end smallexample
636
637
638@node Foreground and Background, Stopped and Terminated Jobs, Launching Jobs, Implementing a Shell
639@subsection Foreground and Background
640
641Now let's consider what actions must be taken by the shell when it
642launches a job into the foreground, and how this differs from what
643must be done when a background job is launched.
644
645@cindex foreground job, launching
646When a foreground job is launched, the shell must first give it access
647to the controlling terminal by calling @code{tcsetpgrp}. Then, the
648shell should wait for processes in that process group to terminate or
649stop. This is discussed in more detail in @ref{Stopped and Terminated
650Jobs}.
651
652When all of the processes in the group have either completed or stopped,
653the shell should regain control of the terminal for its own process
654group by calling @code{tcsetpgrp} again. Since stop signals caused by
655I/O from a background process or a SUSP character typed by the user
656are sent to the process group, normally all the processes in the job
657stop together.
658
659The foreground job may have left the terminal in a strange state, so the
660shell should restore its own saved terminal modes before continuing. In
a496e4ce
UD
661case the job is merely stopped, the shell should first save the current
662terminal modes so that it can restore them later if the job is
28f540f4
RM
663continued. The functions for dealing with terminal modes are
664@code{tcgetattr} and @code{tcsetattr}; these are described in
665@ref{Terminal Modes}.
666
667Here is the sample shell's function for doing all of this.
668
669@smallexample
670@group
671/* @r{Put job @var{j} in the foreground. If @var{cont} is nonzero,}
672 @r{restore the saved terminal modes and send the process group a}
673 @r{@code{SIGCONT} signal to wake it up before we block.} */
674
675void
676put_job_in_foreground (job *j, int cont)
677@{
678 /* @r{Put the job into the foreground.} */
679 tcsetpgrp (shell_terminal, j->pgid);
680@end group
681
682@group
683 /* @r{Send the job a continue signal, if necessary.} */
684 if (cont)
685 @{
686 tcsetattr (shell_terminal, TCSADRAIN, &j->tmodes);
687 if (kill (- j->pgid, SIGCONT) < 0)
688 perror ("kill (SIGCONT)");
689 @}
690@end group
6d52618b 691
28f540f4
RM
692 /* @r{Wait for it to report.} */
693 wait_for_job (j);
6d52618b 694
28f540f4
RM
695 /* @r{Put the shell back in the foreground.} */
696 tcsetpgrp (shell_terminal, shell_pgid);
6d52618b 697
28f540f4
RM
698@group
699 /* @r{Restore the shell's terminal modes.} */
700 tcgetattr (shell_terminal, &j->tmodes);
701 tcsetattr (shell_terminal, TCSADRAIN, &shell_tmodes);
702@}
703@end group
704@end smallexample
705
706@cindex background job, launching
707If the process group is launched as a background job, the shell should
708remain in the foreground itself and continue to read commands from
6d52618b 709the terminal.
28f540f4
RM
710
711In the sample shell, there is not much that needs to be done to put
712a job into the background. Here is the function it uses:
713
714@smallexample
715/* @r{Put a job in the background. If the cont argument is true, send}
716 @r{the process group a @code{SIGCONT} signal to wake it up.} */
717
718void
719put_job_in_background (job *j, int cont)
720@{
721 /* @r{Send the job a continue signal, if necessary.} */
722 if (cont)
723 if (kill (-j->pgid, SIGCONT) < 0)
724 perror ("kill (SIGCONT)");
725@}
726@end smallexample
727
728
729@node Stopped and Terminated Jobs, Continuing Stopped Jobs, Foreground and Background, Implementing a Shell
730@subsection Stopped and Terminated Jobs
731
732@cindex stopped jobs, detecting
733@cindex terminated jobs, detecting
734When a foreground process is launched, the shell must block until all of
735the processes in that job have either terminated or stopped. It can do
736this by calling the @code{waitpid} function; see @ref{Process
737Completion}. Use the @code{WUNTRACED} option so that status is reported
738for processes that stop as well as processes that terminate.
739
740The shell must also check on the status of background jobs so that it
741can report terminated and stopped jobs to the user; this can be done by
742calling @code{waitpid} with the @code{WNOHANG} option. A good place to
743put a such a check for terminated and stopped jobs is just before
744prompting for a new command.
745
746@cindex @code{SIGCHLD}, handling of
747The shell can also receive asynchronous notification that there is
748status information available for a child process by establishing a
749handler for @code{SIGCHLD} signals. @xref{Signal Handling}.
750
751In the sample shell program, the @code{SIGCHLD} signal is normally
752ignored. This is to avoid reentrancy problems involving the global data
753structures the shell manipulates. But at specific times when the shell
754is not using these data structures---such as when it is waiting for
755input on the terminal---it makes sense to enable a handler for
756@code{SIGCHLD}. The same function that is used to do the synchronous
757status checks (@code{do_job_notification}, in this case) can also be
758called from within this handler.
759
760Here are the parts of the sample shell program that deal with checking
761the status of jobs and reporting the information to the user.
762
763@smallexample
764@group
765/* @r{Store the status of the process @var{pid} that was returned by waitpid.}
766 @r{Return 0 if all went well, nonzero otherwise.} */
767
768int
769mark_process_status (pid_t pid, int status)
770@{
771 job *j;
772 process *p;
773@end group
774
775@group
776 if (pid > 0)
777 @{
778 /* @r{Update the record for the process.} */
779 for (j = first_job; j; j = j->next)
780 for (p = j->first_process; p; p = p->next)
781 if (p->pid == pid)
782 @{
783 p->status = status;
784 if (WIFSTOPPED (status))
785 p->stopped = 1;
786 else
787 @{
788 p->completed = 1;
789 if (WIFSIGNALED (status))
790 fprintf (stderr, "%d: Terminated by signal %d.\n",
791 (int) pid, WTERMSIG (p->status));
792 @}
793 return 0;
794 @}
795 fprintf (stderr, "No child process %d.\n", pid);
796 return -1;
797 @}
798@end group
799@group
800 else if (pid == 0 || errno == ECHILD)
801 /* @r{No processes ready to report.} */
802 return -1;
803 else @{
804 /* @r{Other weird errors.} */
805 perror ("waitpid");
806 return -1;
807 @}
808@}
809@end group
810
811@group
812/* @r{Check for processes that have status information available,}
813 @r{without blocking.} */
814
815void
816update_status (void)
817@{
818 int status;
819 pid_t pid;
6d52618b 820
28f540f4
RM
821 do
822 pid = waitpid (WAIT_ANY, &status, WUNTRACED|WNOHANG);
823 while (!mark_process_status (pid, status));
824@}
825@end group
826
827@group
828/* @r{Check for processes that have status information available,}
829 @r{blocking until all processes in the given job have reported.} */
830
831void
832wait_for_job (job *j)
833@{
834 int status;
835 pid_t pid;
6d52618b 836
28f540f4
RM
837 do
838 pid = waitpid (WAIT_ANY, &status, WUNTRACED);
6d52618b
UD
839 while (!mark_process_status (pid, status)
840 && !job_is_stopped (j)
28f540f4
RM
841 && !job_is_completed (j));
842@}
843@end group
844
845@group
846/* @r{Format information about job status for the user to look at.} */
847
848void
849format_job_info (job *j, const char *status)
850@{
851 fprintf (stderr, "%ld (%s): %s\n", (long)j->pgid, status, j->command);
852@}
853@end group
854
855@group
856/* @r{Notify the user about stopped or terminated jobs.}
857 @r{Delete terminated jobs from the active job list.} */
858
859void
860do_job_notification (void)
861@{
862 job *j, *jlast, *jnext;
863 process *p;
864
865 /* @r{Update status information for child processes.} */
866 update_status ();
6d52618b 867
28f540f4
RM
868 jlast = NULL;
869 for (j = first_job; j; j = jnext)
870 @{
871 jnext = j->next;
872
873 /* @r{If all processes have completed, tell the user the job has}
874 @r{completed and delete it from the list of active jobs.} */
875 if (job_is_completed (j)) @{
876 format_job_info (j, "completed");
877 if (jlast)
878 jlast->next = jnext;
879 else
880 first_job = jnext;
881 free_job (j);
882 @}
883
884 /* @r{Notify the user about stopped jobs,}
885 @r{marking them so that we won't do this more than once.} */
886 else if (job_is_stopped (j) && !j->notified) @{
887 format_job_info (j, "stopped");
888 j->notified = 1;
889 jlast = j;
890 @}
891
892 /* @r{Don't say anything about jobs that are still running.} */
893 else
894 jlast = j;
895 @}
896@}
897@end group
898@end smallexample
899
900@node Continuing Stopped Jobs, Missing Pieces, Stopped and Terminated Jobs, Implementing a Shell
901@subsection Continuing Stopped Jobs
902
903@cindex stopped jobs, continuing
904The shell can continue a stopped job by sending a @code{SIGCONT} signal
905to its process group. If the job is being continued in the foreground,
906the shell should first invoke @code{tcsetpgrp} to give the job access to
907the terminal, and restore the saved terminal settings. After continuing
908a job in the foreground, the shell should wait for the job to stop or
909complete, as if the job had just been launched in the foreground.
910
911The sample shell program handles both newly created and continued jobs
912with the same pair of functions, @w{@code{put_job_in_foreground}} and
913@w{@code{put_job_in_background}}. The definitions of these functions
914were given in @ref{Foreground and Background}. When continuing a
915stopped job, a nonzero value is passed as the @var{cont} argument to
916ensure that the @code{SIGCONT} signal is sent and the terminal modes
917reset, as appropriate.
918
919This leaves only a function for updating the shell's internal bookkeeping
920about the job being continued:
921
922@smallexample
923@group
924/* @r{Mark a stopped job J as being running again.} */
925
926void
927mark_job_as_running (job *j)
928@{
929 Process *p;
930
931 for (p = j->first_process; p; p = p->next)
932 p->stopped = 0;
933 j->notified = 0;
934@}
935@end group
936
937@group
938/* @r{Continue the job J.} */
939
940void
941continue_job (job *j, int foreground)
942@{
943 mark_job_as_running (j);
944 if (foreground)
945 put_job_in_foreground (j, 1);
946 else
947 put_job_in_background (j, 1);
948@}
949@end group
950@end smallexample
951
952@node Missing Pieces, , Continuing Stopped Jobs, Implementing a Shell
953@subsection The Missing Pieces
954
955The code extracts for the sample shell included in this chapter are only
956a part of the entire shell program. In particular, nothing at all has
957been said about how @code{job} and @code{program} data structures are
958allocated and initialized.
959
960Most real shells provide a complex user interface that has support for
961a command language; variables; abbreviations, substitutions, and pattern
962matching on file names; and the like. All of this is far too complicated
6d52618b 963to explain here! Instead, we have concentrated on showing how to
28f540f4
RM
964implement the core process creation and job control functions that can
965be called from such a shell.
966
967Here is a table summarizing the major entry points we have presented:
968
969@table @code
970@item void init_shell (void)
971Initialize the shell's internal state. @xref{Initializing the
972Shell}.
973
974@item void launch_job (job *@var{j}, int @var{foreground})
975Launch the job @var{j} as either a foreground or background job.
976@xref{Launching Jobs}.
977
978@item void do_job_notification (void)
979Check for and report any jobs that have terminated or stopped. Can be
980called synchronously or within a handler for @code{SIGCHLD} signals.
981@xref{Stopped and Terminated Jobs}.
982
983@item void continue_job (job *@var{j}, int @var{foreground})
984Continue the job @var{j}. @xref{Continuing Stopped Jobs}.
985@end table
986
987Of course, a real shell would also want to provide other functions for
988managing jobs. For example, it would be useful to have commands to list
989all active jobs or to send a signal (such as @code{SIGKILL}) to a job.
990
991
992@node Functions for Job Control, , Implementing a Shell, Job Control
993@section Functions for Job Control
994@cindex process group functions
995@cindex job control functions
996
997This section contains detailed descriptions of the functions relating
998to job control.
999
1000@menu
1001* Identifying the Terminal:: Determining the controlling terminal's name.
1002* Process Group Functions:: Functions for manipulating process groups.
1003* Terminal Access Functions:: Functions for controlling terminal access.
1004@end menu
1005
1006
1007@node Identifying the Terminal, Process Group Functions, , Functions for Job Control
1008@subsection Identifying the Controlling Terminal
1009@cindex controlling terminal, determining
1010
1011You can use the @code{ctermid} function to get a file name that you can
1f77f049 1012use to open the controlling terminal. In @theglibc{}, it returns
28f540f4
RM
1013the same string all the time: @code{"/dev/tty"}. That is a special
1014``magic'' file name that refers to the controlling terminal of the
1015current process (if it has one). To find the name of the specific
1016terminal device, use @code{ttyname}; @pxref{Is It a Terminal}.
1017
1018The function @code{ctermid} is declared in the header file
1019@file{stdio.h}.
1020@pindex stdio.h
1021
28f540f4 1022@deftypefun {char *} ctermid (char *@var{string})
d08a7e4c 1023@standards{POSIX.1, stdio.h}
7729e0e9 1024@safety{@prelim{}@mtsafe{@mtsposix{/!string}}@assafe{}@acsafe{}}
27bdc63c 1025@c This function is a stub by default; the actual implementation, for
7729e0e9
AO
1026@c posix systems, returns a pointer to a string literal if passed a NULL
1027@c string. It's not clear we want to commit to being MT-Safe in the
1028@c !string case, so maybe add mtasurace{:ctermid/!string} when we take
1029@c prelim out, to make room for using a static buffer in the future.
28f540f4
RM
1030The @code{ctermid} function returns a string containing the file name of
1031the controlling terminal for the current process. If @var{string} is
1032not a null pointer, it should be an array that can hold at least
1033@code{L_ctermid} characters; the string is returned in this array.
1034Otherwise, a pointer to a string in a static area is returned, which
1035might get overwritten on subsequent calls to this function.
1036
1037An empty string is returned if the file name cannot be determined for
1038any reason. Even if a file name is returned, access to the file it
1039represents is not guaranteed.
1040@end deftypefun
1041
28f540f4 1042@deftypevr Macro int L_ctermid
d08a7e4c 1043@standards{POSIX.1, stdio.h}
28f540f4
RM
1044The value of this macro is an integer constant expression that
1045represents the size of a string large enough to hold the file name
1046returned by @code{ctermid}.
1047@end deftypevr
1048
6d52618b 1049See also the @code{isatty} and @code{ttyname} functions, in
28f540f4
RM
1050@ref{Is It a Terminal}.
1051
1052
1053@node Process Group Functions, Terminal Access Functions, Identifying the Terminal, Functions for Job Control
1054@subsection Process Group Functions
1055
1056Here are descriptions of the functions for manipulating process groups.
1057Your program should include the header files @file{sys/types.h} and
1058@file{unistd.h} to use these functions.
1059@pindex unistd.h
1060@pindex sys/types.h
1061
28f540f4 1062@deftypefun pid_t setsid (void)
d08a7e4c 1063@standards{POSIX.1, unistd.h}
27bdc63c
AO
1064@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1065@c This is usually a direct syscall, but if a syscall is not available,
1066@c we use a stub, or Hurd- and BSD-specific implementations. The former
1067@c uses a mutex and a hurd critical section, and the latter issues a few
1068@c syscalls, so both seem safe, the locking on Hurd is safe because of
1069@c the critical section.
28f540f4
RM
1070The @code{setsid} function creates a new session. The calling process
1071becomes the session leader, and is put in a new process group whose
1072process group ID is the same as the process ID of that process. There
1073are initially no other processes in the new process group, and no other
1074process groups in the new session.
1075
1076This function also makes the calling process have no controlling terminal.
1077
1078The @code{setsid} function returns the new process group ID of the
1079calling process if successful. A return value of @code{-1} indicates an
1080error. The following @code{errno} error conditions are defined for this
1081function:
1082
1083@table @code
1084@item EPERM
1085The calling process is already a process group leader, or there is
1086already another process group around that has the same process group ID.
1087@end table
1088@end deftypefun
1089
6444b087 1090@deftypefun pid_t getsid (pid_t @var{pid})
d08a7e4c 1091@standards{SVID, unistd.h}
27bdc63c
AO
1092@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1093@c Stub or direct syscall, except on hurd, where it is equally safe.
6444b087
UD
1094
1095The @code{getsid} function returns the process group ID of the session
1096leader of the specified process. If a @var{pid} is @code{0}, the
1097process group ID of the session leader of the current process is
1098returned.
1099
1100In case of error @code{-1} is returned and @code{errno} is set. The
1101following @code{errno} error conditions are defined for this function:
1102
1103@table @code
1104@item ESRCH
1105There is no process with the given process ID @var{pid}.
1106@item EPERM
1107The calling process and the process specified by @var{pid} are in
1108different sessions, and the implementation doesn't allow to access the
1109process group ID of the session leader of the process with ID @var{pid}
1110from the calling process.
1111@end table
1112@end deftypefun
1113
7011c262 1114@deftypefun pid_t getpgrp (void)
d08a7e4c 1115@standards{POSIX.1, unistd.h}
27bdc63c 1116@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7011c262 1117The @code{getpgrp} function returns the process group ID of
28f540f4 1118the calling process.
7011c262 1119@end deftypefun
28f540f4 1120
7011c262 1121@deftypefun int getpgid (pid_t @var{pid})
d08a7e4c 1122@standards{POSIX.1, unistd.h}
27bdc63c
AO
1123@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1124@c Stub or direct syscall, except on hurd, where it is equally safe.
6444b087 1125
7011c262 1126The @code{getpgid} function
6444b087
UD
1127returns the process group ID of the process @var{pid}. You can supply a
1128value of @code{0} for the @var{pid} argument to get information about
1129the calling process.
1130
1131In case of error @code{-1} is returned and @code{errno} is set. The
1132following @code{errno} error conditions are defined for this function:
1133
1134@table @code
1135@item ESRCH
1136There is no process with the given process ID @var{pid}.
1137The calling process and the process specified by @var{pid} are in
1138different sessions, and the implementation doesn't allow to access the
1139process group ID of the process with ID @var{pid} from the calling
1140process.
1141@end table
7011c262 1142@end deftypefun
6444b087 1143
28f540f4 1144@deftypefun int setpgid (pid_t @var{pid}, pid_t @var{pgid})
d08a7e4c 1145@standards{POSIX.1, unistd.h}
27bdc63c
AO
1146@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1147@c Stub or direct syscall, except on hurd, where it is equally safe.
28f540f4
RM
1148The @code{setpgid} function puts the process @var{pid} into the process
1149group @var{pgid}. As a special case, either @var{pid} or @var{pgid} can
1150be zero to indicate the process ID of the calling process.
1151
28f540f4
RM
1152If the operation is successful, @code{setpgid} returns zero. Otherwise
1153it returns @code{-1}. The following @code{errno} error conditions are
1154defined for this function:
1155
1156@table @code
1157@item EACCES
1158The child process named by @var{pid} has executed an @code{exec}
1159function since it was forked.
1160
1161@item EINVAL
1162The value of the @var{pgid} is not valid.
1163
1164@item ENOSYS
1165The system doesn't support job control.
1166
1167@item EPERM
1168The process indicated by the @var{pid} argument is a session leader,
1169or is not in the same session as the calling process, or the value of
1170the @var{pgid} argument doesn't match a process group ID in the same
1171session as the calling process.
1172
1173@item ESRCH
1174The process indicated by the @var{pid} argument is not the calling
1175process or a child of the calling process.
1176@end table
1177@end deftypefun
1178
28f540f4 1179@deftypefun int setpgrp (pid_t @var{pid}, pid_t @var{pgid})
d08a7e4c 1180@standards{BSD, unistd.h}
27bdc63c
AO
1181@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1182@c Direct syscall or setpgid wrapper.
28f540f4
RM
1183This is the BSD Unix name for @code{setpgid}. Both functions do exactly
1184the same thing.
1185@end deftypefun
1186
1187
1188@node Terminal Access Functions, , Process Group Functions, Functions for Job Control
1189@subsection Functions for Controlling Terminal Access
1190
1191These are the functions for reading or setting the foreground
1192process group of a terminal. You should include the header files
1193@file{sys/types.h} and @file{unistd.h} in your application to use
1194these functions.
1195@pindex unistd.h
1196@pindex sys/types.h
1197
1198Although these functions take a file descriptor argument to specify
1199the terminal device, the foreground job is associated with the terminal
1200file itself and not a particular open file descriptor.
1201
28f540f4 1202@deftypefun pid_t tcgetpgrp (int @var{filedes})
d08a7e4c 1203@standards{POSIX.1, unistd.h}
27bdc63c
AO
1204@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1205@c Stub, or ioctl on BSD and GNU/Linux.
28f540f4
RM
1206This function returns the process group ID of the foreground process
1207group associated with the terminal open on descriptor @var{filedes}.
1208
1209If there is no foreground process group, the return value is a number
1210greater than @code{1} that does not match the process group ID of any
1211existing process group. This can happen if all of the processes in the
1212job that was formerly the foreground job have terminated, and no other
1213job has yet been moved into the foreground.
1214
1215In case of an error, a value of @code{-1} is returned. The
1216following @code{errno} error conditions are defined for this function:
1217
1218@table @code
1219@item EBADF
1220The @var{filedes} argument is not a valid file descriptor.
1221
1222@item ENOSYS
1223The system doesn't support job control.
1224
1225@item ENOTTY
1226The terminal file associated with the @var{filedes} argument isn't the
1227controlling terminal of the calling process.
1228@end table
1229@end deftypefun
1230
28f540f4 1231@deftypefun int tcsetpgrp (int @var{filedes}, pid_t @var{pgid})
d08a7e4c 1232@standards{POSIX.1, unistd.h}
27bdc63c
AO
1233@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1234@c Stub, or ioctl on BSD and GNU/Linux.
28f540f4
RM
1235This function is used to set a terminal's foreground process group ID.
1236The argument @var{filedes} is a descriptor which specifies the terminal;
1237@var{pgid} specifies the process group. The calling process must be a
1238member of the same session as @var{pgid} and must have the same
1239controlling terminal.
1240
1241For terminal access purposes, this function is treated as output. If it
1242is called from a background process on its controlling terminal,
1243normally all processes in the process group are sent a @code{SIGTTOU}
1244signal. The exception is if the calling process itself is ignoring or
1245blocking @code{SIGTTOU} signals, in which case the operation is
1246performed and no signal is sent.
1247
1248If successful, @code{tcsetpgrp} returns @code{0}. A return value of
1249@code{-1} indicates an error. The following @code{errno} error
1250conditions are defined for this function:
1251
1252@table @code
1253@item EBADF
1254The @var{filedes} argument is not a valid file descriptor.
1255
1256@item EINVAL
1257The @var{pgid} argument is not valid.
1258
1259@item ENOSYS
1260The system doesn't support job control.
1261
1262@item ENOTTY
1263The @var{filedes} isn't the controlling terminal of the calling process.
1264
1265@item EPERM
1266The @var{pgid} isn't a process group in the same session as the calling
1267process.
1268@end table
1269@end deftypefun
af6f3906 1270
af6f3906 1271@deftypefun pid_t tcgetsid (int @var{fildes})
d08a7e4c 1272@standards{Unix98, termios.h}
27bdc63c
AO
1273@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1274@c Ioctl call, if available, or tcgetpgrp followed by getsid.
af6f3906 1275This function is used to obtain the process group ID of the session
04b9968b 1276for which the terminal specified by @var{fildes} is the controlling terminal.
af6f3906
UD
1277If the call is successful the group ID is returned. Otherwise the
1278return value is @code{(pid_t) -1} and the global variable @var{errno}
1279is set to the following value:
1280@table @code
1281@item EBADF
1282The @var{filedes} argument is not a valid file descriptor.
1283
1284@item ENOTTY
1285The calling process does not have a controlling terminal, or the file
04b9968b 1286is not the controlling terminal.
af6f3906
UD
1287@end table
1288@end deftypefun