xref: /illumos-gate/usr/src/lib/libc/port/gen/waitpid.c (revision 7c478bd9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 #include "synonyms.h"
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <wait.h>
37 
38 pid_t
39 _waitpid(pid_t pid, int *stat_loc, int options)
40 {
41 	idtype_t idtype;
42 	id_t id;
43 	siginfo_t info;
44 	int error;
45 
46 	if (pid > 0) {
47 		idtype = P_PID;
48 		id = pid;
49 	} else if (pid < -1) {
50 		idtype = P_PGID;
51 		id = -pid;
52 	} else if (pid == -1) {
53 		idtype = P_ALL;
54 		id = 0;
55 	} else {
56 		idtype = P_PGID;
57 		id = getpgid(0);
58 	}
59 
60 	options |= (WEXITED|WTRAPPED);
61 
62 	if ((error = waitid(idtype, id, &info, options)) < 0)
63 		return (error);
64 
65 	if (stat_loc) {
66 
67 		int stat = (info.si_status & 0377);
68 
69 		switch (info.si_code) {
70 		case CLD_EXITED:
71 			stat <<= 8;
72 			break;
73 		case CLD_DUMPED:
74 			stat |= WCOREFLG;
75 			break;
76 		case CLD_KILLED:
77 			break;
78 		case CLD_TRAPPED:
79 		case CLD_STOPPED:
80 			stat <<= 8;
81 			stat |= WSTOPFLG;
82 			break;
83 		case CLD_CONTINUED:
84 			stat = WCONTFLG;
85 			break;
86 		}
87 
88 		*stat_loc = stat;
89 	}
90 
91 	return (info.si_pid);
92 }
93 
94 pid_t
95 _wait(int *stat_loc)
96 {
97 	return (waitpid(-1, stat_loc, 0));
98 }
99