1/* wait.c, created from wait.def. */
2#line 33 "wait.def"
3
4#line 43 "wait.def"
5
6#include <config.h>
7
8#include "../bashtypes.h"
9#include <signal.h>
10
11#if defined (HAVE_UNISTD_H)
12#  include <unistd.h>
13#endif
14
15#include <chartypes.h>
16
17#include "../bashansi.h"
18
19#include "../shell.h"
20#include "../jobs.h"
21#include "common.h"
22#include "bashgetopt.h"
23
24extern int wait_signal_received;
25
26procenv_t wait_intr_buf;
27
28/* Wait for the pid in LIST to stop or die.  If no arguments are given, then
29   wait for all of the active background processes of the shell and return
30   0.  If a list of pids or job specs are given, return the exit status of
31   the last one waited for. */
32
33#define WAIT_RETURN(s) \
34  do \
35    { \
36      interrupt_immediately = old_interrupt_immediately;\
37      return (s);\
38    } \
39  while (0)
40
41int
42wait_builtin (list)
43     WORD_LIST *list;
44{
45  int status, code;
46  volatile int old_interrupt_immediately;
47
48  USE_VAR(list);
49
50  if (no_options (list))
51    return (EX_USAGE);
52  list = loptend;
53
54  old_interrupt_immediately = interrupt_immediately;
55  interrupt_immediately++;
56
57  /* POSIX.2 says:  When the shell is waiting (by means of the wait utility)
58     for asynchronous commands to complete, the reception of a signal for
59     which a trap has been set shall cause the wait utility to return
60     immediately with an exit status greater than 128, after which the trap
61     associated with the signal shall be taken.
62
63     We handle SIGINT here; it's the only one that needs to be treated
64     specially (I think), since it's handled specially in {no,}jobs.c. */
65  code = setjmp (wait_intr_buf);
66  if (code)
67    {
68      status = 128 + wait_signal_received;
69      WAIT_RETURN (status);
70    }
71
72  /* We support jobs or pids.
73     wait <pid-or-job> [pid-or-job ...] */
74
75  /* But wait without any arguments means to wait for all of the shell's
76     currently active background processes. */
77  if (list == 0)
78    {
79      wait_for_background_pids ();
80      WAIT_RETURN (EXECUTION_SUCCESS);
81    }
82
83  status = EXECUTION_SUCCESS;
84  while (list)
85    {
86      pid_t pid;
87      char *w;
88      intmax_t pid_value;
89
90      w = list->word->word;
91      if (DIGIT (*w))
92	{
93	  if (legal_number (w, &pid_value) && pid_value == (pid_t)pid_value)
94	    {
95	      pid = (pid_t)pid_value;
96	      status = wait_for_single_pid (pid);
97	    }
98	  else
99	    {
100	      sh_badpid (w);
101	      WAIT_RETURN (EXECUTION_FAILURE);
102	    }
103	}
104#if defined (JOB_CONTROL)
105      else if (*w && *w == '%')
106	/* Must be a job spec.  Check it out. */
107	{
108	  int job;
109	  sigset_t set, oset;
110
111	  BLOCK_CHILD (set, oset);
112	  job = get_job_spec (list);
113
114	  if (INVALID_JOB (job))
115	    {
116	      if (job != DUP_JOB)
117		sh_badjob (list->word->word);
118	      UNBLOCK_CHILD (oset);
119	      status = 127;	/* As per Posix.2, section 4.70.2 */
120	      list = list->next;
121	      continue;
122	    }
123
124	  /* Job spec used.  Wait for the last pid in the pipeline. */
125	  UNBLOCK_CHILD (oset);
126	  status = wait_for_job (job);
127	}
128#endif /* JOB_CONTROL */
129      else
130	{
131	  sh_badpid (w);
132	  status = EXECUTION_FAILURE;
133	}
134      list = list->next;
135    }
136
137  WAIT_RETURN (status);
138}
139