[go: up one dir, main page]

File: wait.c

package info (click to toggle)
cook 2.29-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,656 kB
  • ctags: 4,083
  • sloc: ansic: 47,636; sh: 14,376; makefile: 4,656; yacc: 3,166; perl: 224; awk: 219
file content (338 lines) | stat: -rw-r--r-- 10,092 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 *      cook - file construction tool
 *      Copyright (C) 2000, 2001, 2006, 2007 Peter Miller;
 *      All rights reserved.
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * MANIFEST: functions to manipulate waits
 */

#include <common/ac/stddef.h>
#include <sys/types.h>
#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
#include <sys/time.h>
#include <sys/resource.h>
#endif
#include <sys/wait.h>

#include <common/mem.h>
#include <cook/os/wait.h>
#include <common/trace.h>


typedef struct cache_ty cache_ty;
struct cache_ty
{
    int             pid;
    int             status;
#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
    struct rusage   rusage;
#endif
};

static size_t   length;
static size_t   length_max;
static cache_ty *cache;


/*
 * NAME
 *      os_wait4
 *
 * SYNOPSIS
 *      int os_wait4(int pid, int *status, int options, struct rusage *rusage);
 *
 * DESCRIPTION
 *      The os_wait4 function suspends execution of its calling process
 *      until exit status information and accumulated resource utilization
 *      statistics are available for a terminated child process, or a
 *      signal is received.
 *
 *      Other os_wait* functions are implemented using os_wait4().
 *
 * ARGUMENTS
 *      The pid parameter specifies the set of child processes for which
 *      to wait.  If pid is -1, the call waits for any child process.
 *      If pid is greater than zero, the call waits for the process with
 *      a process id of pid.  Values of pid of 0 or less than -1 are
 *      not supported (this is asymetric with the system wait4 function.)
 *
 *      The status parameter is as defined for the system wait4() function.
 *
 *      The options parameter is as defined for the system wait4() function.
 *
 *      If the rusage parameter is non-zero, a summary of the resources
 *      used by the terminated process and all its children is returned.
 *
 * RETURNS
 *      If an error or interrupt occurs, os_wait4() returns a value of -1,
 *      and errno is set suitably.  When the WNOHANG option is specified and
 *      no processes wish to report status, os_wait4() returns a process
 *      id of 0.  Otherwise, it returns the process id of the relevant
 *      child process.
 */

int
os_wait4(int pid, int *status, int options, struct rusage *rusage)
{
    cache_ty        *cp;
    int             pid2;

    trace(("os_wait4(pid = %d, status = %08lX, options = 0x%X, "
        "rusage = %08lX)\n{\n", pid, status, options, rusage));
    assert(pid == -1 || pid > 0);
    assert(status);
    if (pid == -1)
    {
        if (length > 0)
        {
            trace(("use the cache...\n"));
            --length;
            cp = &cache[length];
            *status = cp->status;
#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
            if (rusage)
                *rusage = cp->rusage;
#endif
            trace(("return %d;\n", cp->pid));
            trace(("}\n"));
            return cp->pid;
        }
        else
        {
            trace(("ask the system...\n"));
#ifdef HAVE_WAIT4
            pid2 = wait4(-1, status, options, rusage);
#else  /* !HAVE_WAIT4 */
#ifdef HAVE_WAIT3
            pid2 = wait3(status, options, rusage);
#else  /* !HAVE_WAIT3 */
            assert(options == 0);
            assert(rusage == 0);
            pid2 = wait(status);
#endif /* !HAVE_WAIT3 */
#endif /* !HAVE_WAIT4 */
            trace(("return %d;\n", pid2));
            trace(("}\n"));
            return pid2;
        }
    }
    else
    {
        size_t          j;

        /*
         * Check to see if we already have it in the cache.
         * (Plug the hole with the last entry.)
         */
        for (j = 0; j < length; --j)
        {
            cp = &cache[j];
            if (pid == cp->pid)
            {
                trace(("use the cache...\n"));
                pid2 = cp->pid;
                *status = cp->status;
#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
                if (rusage)
                    *rusage = cp->rusage;
#endif
                if (j + 1 < length)
                    cache[j] = cache[length - 1];
                --length;
                trace(("return %d;\n", pid2));
                trace(("}\n"));
                return pid2;
            }
        }

        /*
         * Watch them go past.
         */
        for (;;)
        {
#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
            struct rusage   rusage2;
#endif
            int             status2;

            /*
             * ask the operating system to tell us what happened
             */
            trace(("ask the system...\n"));
#ifdef HAVE_WAIT4
            pid2 = wait4(-1, &status2, options, &rusage2);
#else  /* !HAVE_WAIT4 */
#ifdef HAVE_WAIT3
            pid2 = wait3(&status2, options, &rusage2);
#else  /* !HAVE_WAIT3 */
            assert(options == 0);
            assert(rusage == 0);
            pid2 = wait(&status2);
#endif /* !HAVE_WAIT3 */
#endif /* !HAVE_WAIT4 */
            /*
             * Return on -1, for all errors.
             * Return on 0, for the WNOHANG option.
             */
            if (pid2 <= 0)
            {
                trace(("return %d;\n", pid2));
                trace(("}\n"));
                return pid2;
            }

            /*
             * Stop if this is the process we were waiting for.
             */
            if (pid2 == pid)
            {
                *status = status2;
#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
                if (rusage)
                    *rusage = rusage2;
#endif
                trace(("return %d;\n", pid2));
                trace(("}\n"));
                return pid2;
            }

            /*
             * make sure we have enough room in the cache
             */
            if (length >= length_max)
            {
                size_t          nbytes;

                length_max = length_max * 2 + 4;
                nbytes = length_max * sizeof(cache[0]);
                cache = mem_change_size(cache, nbytes);
            }

            /*
             * stash the results of the wait into the cache
             */
            trace(("append to the cache...\n"));
            cp = &cache[length++];
            cp->pid = pid2;
            cp->status = status2;
#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
            cp->rusage = rusage2;
#endif
        }
    }
}



/*
 * NAME
 *      os_wait3
 *
 * SYNOPSIS
 *      int os_wait3(int *status, int options, struct rusage *rusage);
 *
 * DESCRIPTION
 *      The os_wait3 function suspends execution of its calling process
 *      until exit status information and accumulated resource utilization
 *      statistics are available for a terminated child process, or a
 *      signal is received.
 *
 * ARGUMENTS
 *      The status parameter is as defined for the system wait3() function.
 *
 *      The options parameter is as defined for the system wait3() function.
 *
 *      If the rusage parameter is non-zero, a summary of the resources
 *      used by the terminated process and all its children is returned.
 *
 * RETURNS
 *      If an error or interrupt occurs, os_wait3() returns a value of -1,
 *      and errno is set suitably.  When the WNOHANG option is specified and
 *      no processes wish to report status, os_wait3() returns a process
 *      id of 0.  Otherwise, it returns the process id of the relevant
 *      child process.
 */

int
os_wait3(int *status, int options, struct rusage *rusage)
{
    return os_wait4(-1, status, options, rusage);
}


/*
 * NAME
 *      os_waitpid
 *
 * SYNOPSIS
 *      int os_waitpid(int pid, int *status);
 *
 * DESCRIPTION
 *      The os_waitpid function suspends execution of its calling process
 *      until exit status information and accumulated resource utilization
 *      statistics are available for the specified child process, or a
 *      signal is received.
 *
 * ARGUMENTS
 *      The pid parameter specifies the set of child processes for which
 *      to wait.  If pid is -1, the call waits for any child process.
 *      If pid is greater than zero, the call waits for the process with
 *      a process id of pid.  Values of pid of 0 or less than -1 are
 *      not supported (this is asymetric with the system waitpid function.)
 *
 *      The status parameter is as defined for the system waitpid() function.
 *
 * RETURNS
 *      If an error or interrupt occurs, os_waitpid() returns a value
 *      of -1, and errno is set suitably.  Otherwise, it returns the
 *      process id of the terminated child process.
 */

int
os_waitpid(int pid, int *status)
{
    return os_wait4(pid, status, 0, (struct rusage *)0);
}


/*
 * NAME
 *      os_wait
 *
 * SYNOPSIS
 *      int os_wait(int *status);
 *
 * DESCRIPTION
 *      The os_wait function suspends execution of its calling process
 *      until exit status information and accumulated resource utilization
 *      statistics are available for a terminated child process, or a
 *      signal is received.
 *
 * ARGUMENTS
 *      The status parameter is as defined for the system wait() function.
 *
 * RETURNS
 *      If an error or interrupt occurs, os_wait() returns a value of -1,
 *      and errno is set suitably.  Otherwise, it returns the process
 *      id of the a terminated child process.
 */

int
os_wait(int *status)
{
    return os_wait4(-1, status, 0, (struct rusage *)0);
}