job.texi 45 KB

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