kernel - Fix deadlock in sound system
[dragonfly.git] / tools / regression / priv / test.c
blob3749c21d340936ead24b06b65c6b3cc02a51e10a
1 #include <sys/types.h>
2 #include <sys/jail.h>
3 #include <sys/wait.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <stdlib.h>
8 void setup();
9 void teardown();
11 uid_t unpriv_uid = 5000;
12 gid_t unpriv_gid = 5000;
14 void
15 test(int (*fn)(), int expected, char *msg, char *msg2)
17 int retval;
19 setup();
20 retval = fn();
21 teardown();
23 printf("%s (%s): ", msg, msg2);
25 if (retval == expected) {
26 printf("OK\n");
27 } else {
28 printf("FAILED (was: %d, expected: %d)\n", retval, expected);
30 fflush(stdout);
33 void
34 test_as_root(int (*fn)(), int expected, char *msg)
36 if (getuid() != 0) {
37 fprintf(stderr, "must be run as root\n");
38 exit(-1);
41 test(fn, expected, msg, "as root");
44 void
45 test_as_jailed_root(int (*fn)(), int expected, char *msg)
47 if (getuid() != 0) {
48 fprintf(stderr, "must be run as root\n");
49 exit(-1);
52 int child = fork();
54 if (child == -1) {
55 fprintf(stderr, "fork failed\n");
56 exit(-2);
59 if (child) {
60 struct jail j;
61 j.version = 1;
62 j.path = "/";
63 j.hostname = "jail";
64 j.n_ips = 0;
66 int jid = jail(&j);
67 if (jid < 0) {
68 fprintf(stderr, "jail failed\n");
69 exit(-1); // TODO
71 test(fn, expected, msg, "as jailed root");
72 exit(0);
74 else {
75 waitpid(child, NULL, 0);
79 void
80 test_as_unpriv(int (*fn)(), int expected, char *msg)
82 if (getuid() != 0) {
83 fprintf(stderr, "must be run as root\n");
84 exit(-1);
87 int child = fork();
89 if (child == -1) {
90 fprintf(stderr, "fork failed\n");
91 exit(-2);
94 if (child) {
95 setgid(unpriv_gid);
96 setuid(unpriv_uid);
98 if (getuid() != unpriv_uid || getgid() != unpriv_gid) {
99 fprintf(stderr, "setuid/gid failed\n");
100 exit(-1); // TODO
102 test(fn, expected, msg, "as unpriv");
103 exit(0);
105 else {
106 waitpid(child, NULL, 0);
110 void
111 test_as_jailed_unpriv(int (*fn)(), int expected, char *msg)
113 if (getuid() != 0) {
114 fprintf(stderr, "must be run as root\n");
115 exit(-1);
118 int child = fork();
120 if (child == -1) {
121 fprintf(stderr, "fork failed\n");
122 exit(-2);
125 if (child) {
126 struct jail j;
127 j.version = 1;
128 j.path = "/";
129 j.hostname = "jail";
130 j.n_ips = 0;
132 int jid = jail(&j);
133 if (jid < 0) {
134 fprintf(stderr, "jail failed\n");
135 exit(-1); // TODO
138 setgid(unpriv_gid);
139 setuid(unpriv_uid);
141 if (getuid() != unpriv_uid || getgid() != unpriv_gid) {
142 fprintf(stderr, "setuid/gid failed\n");
143 exit(-1); // TODO
145 test(fn, expected, msg, "as jailed unpriv");
146 exit(0);
148 else {
149 waitpid(child, NULL, 0);