blob: 8452e910463f692282e040c6e29682678ac287e7 [file] [log] [blame]
Christian Brauner172bb242019-03-23 12:24:21 +01001/* SPDX-License-Identifier: GPL-2.0 */
2
3#ifndef __PIDFD_H
4#define __PIDFD_H
5
6#define _GNU_SOURCE
7#include <errno.h>
8#include <fcntl.h>
9#include <sched.h>
10#include <signal.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <syscall.h>
15#include <sys/mount.h>
16
17#include "../kselftest.h"
18
19/*
20 * The kernel reserves 300 pids via RESERVED_PIDS in kernel/pid.c
21 * That means, when it wraps around any pid < 300 will be skipped.
22 * So we need to use a pid > 300 in order to test recycling.
23 */
24#define PID_RECYCLE 1000
25
26/*
27 * Define a few custom error codes for the child process to clearly indicate
28 * what is happening. This way we can tell the difference between a system
29 * error, a test error, etc.
30 */
31#define PIDFD_PASS 0
32#define PIDFD_FAIL 1
33#define PIDFD_ERROR 2
34#define PIDFD_SKIP 3
35#define PIDFD_XFAIL 4
36
37int wait_for_pid(pid_t pid)
38{
39 int status, ret;
40
41again:
42 ret = waitpid(pid, &status, 0);
43 if (ret == -1) {
44 if (errno == EINTR)
45 goto again;
46
47 return -1;
48 }
49
50 if (!WIFEXITED(status))
51 return -1;
52
53 return WEXITSTATUS(status);
54}
55
56
57#endif /* __PIDFD_H */