xref: /dflybsd-src/lib/libc/gen/waitid.c (revision 33b81dc9633739f35a61170c31e64dd522fec577)
1*33b81dc9SMatthew Dillon /*-
2*33b81dc9SMatthew Dillon  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*33b81dc9SMatthew Dillon  *
4*33b81dc9SMatthew Dillon  * Copyright (c) 2012 Jukka A. Ukkonen
5*33b81dc9SMatthew Dillon  * All rights reserved.
6*33b81dc9SMatthew Dillon  *
7*33b81dc9SMatthew Dillon  * This software was developed by Jukka Ukkonen for FreeBSD.
8*33b81dc9SMatthew Dillon  *
9*33b81dc9SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
10*33b81dc9SMatthew Dillon  * modification, are permitted provided that the following conditions
11*33b81dc9SMatthew Dillon  * are met:
12*33b81dc9SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
13*33b81dc9SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
14*33b81dc9SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
15*33b81dc9SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
16*33b81dc9SMatthew Dillon  *    documentation and/or other materials provided with the distribution.
17*33b81dc9SMatthew Dillon  *
18*33b81dc9SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19*33b81dc9SMatthew Dillon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*33b81dc9SMatthew Dillon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*33b81dc9SMatthew Dillon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22*33b81dc9SMatthew Dillon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*33b81dc9SMatthew Dillon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*33b81dc9SMatthew Dillon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*33b81dc9SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*33b81dc9SMatthew Dillon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*33b81dc9SMatthew Dillon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*33b81dc9SMatthew Dillon  * SUCH DAMAGE.
29*33b81dc9SMatthew Dillon  */
30*33b81dc9SMatthew Dillon 
31*33b81dc9SMatthew Dillon #include <sys/cdefs.h>
32*33b81dc9SMatthew Dillon #include "namespace.h"
33*33b81dc9SMatthew Dillon #include <sys/types.h>
34*33b81dc9SMatthew Dillon #include <sys/wait.h>
35*33b81dc9SMatthew Dillon #include <stddef.h>
36*33b81dc9SMatthew Dillon #include <string.h>
37*33b81dc9SMatthew Dillon #include <signal.h>
38*33b81dc9SMatthew Dillon #include <errno.h>
39*33b81dc9SMatthew Dillon #include "un-namespace.h"
40*33b81dc9SMatthew Dillon #include "libc_private.h"
41*33b81dc9SMatthew Dillon 
42*33b81dc9SMatthew Dillon int __waitid(idtype_t, id_t, siginfo_t *, int);
43*33b81dc9SMatthew Dillon 
44*33b81dc9SMatthew Dillon int
__waitid(idtype_t idtype,id_t id,siginfo_t * info,int flags)45*33b81dc9SMatthew Dillon __waitid(idtype_t idtype, id_t id, siginfo_t *info, int flags)
46*33b81dc9SMatthew Dillon {
47*33b81dc9SMatthew Dillon 	int status;
48*33b81dc9SMatthew Dillon 	pid_t ret;
49*33b81dc9SMatthew Dillon 
50*33b81dc9SMatthew Dillon 	ret = _wait6(idtype, id, &status, flags, NULL, info);
51*33b81dc9SMatthew Dillon 
52*33b81dc9SMatthew Dillon 	/*
53*33b81dc9SMatthew Dillon 	 * According to SUSv4, waitid() shall not return a PID when a
54*33b81dc9SMatthew Dillon 	 * process is found, but only 0.  If a process was actually
55*33b81dc9SMatthew Dillon 	 * found, siginfo_t fields si_signo and si_pid will be
56*33b81dc9SMatthew Dillon 	 * non-zero.  In case WNOHANG was set in the flags and no
57*33b81dc9SMatthew Dillon 	 * process was found those fields are set to zero using
58*33b81dc9SMatthew Dillon 	 * memset() below.
59*33b81dc9SMatthew Dillon 	 */
60*33b81dc9SMatthew Dillon 	if (ret == 0 && info != NULL)
61*33b81dc9SMatthew Dillon 		memset(info, 0, sizeof(*info));
62*33b81dc9SMatthew Dillon 	else if (ret > 0)
63*33b81dc9SMatthew Dillon 		ret = 0;
64*33b81dc9SMatthew Dillon 	return (ret);
65*33b81dc9SMatthew Dillon }
66*33b81dc9SMatthew Dillon 
67*33b81dc9SMatthew Dillon __weak_reference(__waitid, waitid);
68*33b81dc9SMatthew Dillon __weak_reference(__waitid, _waitid);
69