1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
4 */
5
6 #include <sys/types.h>
7 #include <sys/wait.h>
8 #include <stdio.h>
9 #define TST_NO_DEFAULT_MAIN
10 #include "tst_test.h"
11
12 static char buf[32];
13
exited(int status)14 const char *exited(int status)
15 {
16 snprintf(buf, sizeof(buf), "exited with %i", WEXITSTATUS(status));
17
18 return buf;
19 }
20
signaled(int status)21 const char *signaled(int status)
22 {
23 snprintf(buf, sizeof(buf), "killed by %s",
24 tst_strsig(WTERMSIG(status)));
25
26 return buf;
27 }
28
invalid(int status)29 const char *invalid(int status)
30 {
31 snprintf(buf, sizeof(buf), "invalid status 0x%x", status);
32
33 return buf;
34 }
35
tst_strstatus(int status)36 const char *tst_strstatus(int status)
37 {
38 if (WIFEXITED(status))
39 return exited(status);
40
41 if (WIFSIGNALED(status))
42 return signaled(status);
43
44 if (WIFSTOPPED(status))
45 return "is stopped";
46
47 if (WIFCONTINUED(status))
48 return "is resumed";
49
50 return invalid(status);
51 }
52
tst_validate_children_(const char * file,const int lineno,unsigned int count)53 int tst_validate_children_(const char *file, const int lineno,
54 unsigned int count)
55 {
56 unsigned int i;
57 int status;
58 pid_t pid;
59
60 for (i = 0; i < count; i++) {
61 pid = SAFE_WAITPID(-1, &status, 0);
62
63 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
64 tst_res_(file, lineno, TFAIL, "Child %d: %s", pid,
65 tst_strstatus(status));
66 return 1;
67 }
68 }
69
70 return 0;
71 }
72