* locales/as_IN: Fix currency_symbol, abday for Sunday, abmon for
[glibc/pb-stable.git] / nptl / tst-setuid1.c
blobf026c576d6d17f94548912c1c5321771966105d3
1 /* Copyright (C) 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jaku@redhat.com>, 2004.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <pthread.h>
21 #include <pwd.h>
22 #include <grp.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
29 static pthread_barrier_t b3, b4;
30 static uid_t prev_ruid, prev_euid, prev_suid, nobody_uid;
31 static gid_t prev_rgid, prev_egid, prev_sgid, nobody_gid;
32 enum ACTION { PREPARE, SET, CHECK_BEFORE, CHECK_AFTER };
33 #define TESTNO(arg) ((long int) (arg) & 0xff)
34 #define THREADNO(arg) ((long int) (arg) >> 8)
37 static void
38 check_prev_uid (int tno)
40 uid_t ruid, euid, suid;
41 if (getresuid (&ruid, &euid, &suid) < 0)
43 printf ("getresuid failed: %d %m\n", tno);
44 exit (1);
47 if (ruid != prev_ruid || euid != prev_euid || suid != prev_suid)
49 printf ("uids before in %d (%d %d %d) != (%d %d %d)\n", tno,
50 ruid, euid, suid, prev_ruid, prev_euid, prev_suid);
51 exit (1);
56 static void
57 check_prev_gid (int tno)
59 gid_t rgid, egid, sgid;
60 if (getresgid (&rgid, &egid, &sgid) < 0)
62 printf ("getresgid failed: %d %m\n", tno);
63 exit (1);
66 if (rgid != prev_rgid || egid != prev_egid || sgid != prev_sgid)
68 printf ("gids before in %d (%d %d %d) != (%d %d %d)\n", tno,
69 rgid, egid, sgid, prev_rgid, prev_egid, prev_sgid);
70 exit (1);
75 static void
76 test_setuid1 (enum ACTION action, int tno)
78 if (action == PREPARE)
79 return;
81 if (action != CHECK_AFTER)
82 check_prev_uid (tno);
84 if (action == SET && setuid (nobody_uid) < 0)
86 printf ("setuid failed: %m\n");
87 exit (1);
90 if (action != CHECK_BEFORE)
92 uid_t ruid, euid, suid;
93 if (getresuid (&ruid, &euid, &suid) < 0)
95 printf ("getresuid failed: %d %m\n", tno);
96 exit (1);
99 if (ruid != nobody_uid || euid != nobody_uid || suid != nobody_uid)
101 printf ("after setuid %d (%d %d %d) != (%d %d %d)\n", tno,
102 ruid, euid, suid, nobody_uid, nobody_uid, nobody_uid);
103 exit (1);
109 static void
110 test_setuid2 (enum ACTION action, int tno)
112 if (action == PREPARE)
114 if (setresuid (nobody_uid, nobody_uid, -1) < 0)
116 printf ("setresuid failed: %m\n");
117 exit (1);
120 prev_ruid = nobody_uid;
121 prev_euid = nobody_uid;
122 return;
125 if (action != CHECK_AFTER)
126 check_prev_uid (tno);
128 if (action == SET && setuid (prev_suid) < 0)
130 printf ("setuid failed: %m\n");
131 exit (1);
134 if (action != CHECK_BEFORE)
136 uid_t ruid, euid, suid;
137 if (getresuid (&ruid, &euid, &suid) < 0)
139 printf ("getresuid failed: %d %m\n", tno);
140 exit (1);
143 if (ruid != nobody_uid || euid != prev_suid || suid != prev_suid)
145 printf ("after setuid %d (%d %d %d) != (%d %d %d)\n", tno,
146 ruid, euid, suid, nobody_uid, prev_suid, prev_suid);
147 exit (1);
153 static void
154 test_seteuid1 (enum ACTION action, int tno)
156 if (action == PREPARE)
157 return;
159 if (action != CHECK_AFTER)
160 check_prev_uid (tno);
162 if (action == SET && seteuid (nobody_uid) < 0)
164 printf ("seteuid failed: %m\n");
165 exit (1);
168 if (action != CHECK_BEFORE)
170 uid_t ruid, euid, suid;
171 if (getresuid (&ruid, &euid, &suid) < 0)
173 printf ("getresuid failed: %d %m\n", tno);
174 exit (1);
177 if (ruid != prev_ruid || euid != nobody_uid || suid != prev_suid)
179 printf ("after seteuid %d (%d %d %d) != (%d %d %d)\n", tno,
180 ruid, euid, suid, prev_ruid, nobody_uid, prev_suid);
181 exit (1);
187 static void
188 test_seteuid2 (enum ACTION action, int tno)
190 if (action == PREPARE)
192 if (setresuid (nobody_uid, nobody_uid, -1) < 0)
194 printf ("setresuid failed: %m\n");
195 exit (1);
198 prev_ruid = nobody_uid;
199 prev_euid = nobody_uid;
200 nobody_uid = prev_suid;
201 return;
204 test_seteuid1 (action, tno);
208 static void
209 test_setreuid1 (enum ACTION action, int tno)
211 if (action == PREPARE)
212 return;
214 if (action != CHECK_AFTER)
215 check_prev_uid (tno);
217 if (action == SET && setreuid (-1, nobody_uid) < 0)
219 printf ("setreuid failed: %m\n");
220 exit (1);
223 if (action != CHECK_BEFORE)
225 uid_t ruid, euid, suid, esuid;
226 if (getresuid (&ruid, &euid, &suid) < 0)
228 printf ("getresuid failed: %d %m\n", tno);
229 exit (1);
232 if (prev_ruid != nobody_uid)
233 esuid = nobody_uid;
234 else
235 esuid = prev_suid;
237 if (ruid != prev_ruid || euid != nobody_uid || suid != esuid)
239 printf ("after setreuid %d (%d %d %d) != (%d %d %d)\n", tno,
240 ruid, euid, suid, prev_ruid, nobody_uid, esuid);
241 exit (1);
247 static void
248 test_setreuid2 (enum ACTION action, int tno)
250 if (action == PREPARE)
251 return;
253 if (action != CHECK_AFTER)
254 check_prev_uid (tno);
256 if (action == SET && setreuid (nobody_uid, -1) < 0)
258 printf ("setreuid failed: %m\n");
259 exit (1);
262 if (action != CHECK_BEFORE)
264 uid_t ruid, euid, suid;
265 if (getresuid (&ruid, &euid, &suid) < 0)
267 printf ("getresuid failed: %d %m\n", tno);
268 exit (1);
271 if (ruid != nobody_uid || euid != prev_euid || suid != prev_euid)
273 printf ("after setreuid %d (%d %d %d) != (%d %d %d)\n", tno,
274 ruid, euid, suid, nobody_uid, prev_euid, prev_euid);
275 exit (1);
281 static void
282 test_setreuid3 (enum ACTION action, int tno)
284 if (action == PREPARE)
285 return;
287 if (action != CHECK_AFTER)
288 check_prev_uid (tno);
290 if (action == SET && setreuid (nobody_uid, nobody_uid) < 0)
292 printf ("setreuid failed: %m\n");
293 exit (1);
296 if (action != CHECK_BEFORE)
298 uid_t ruid, euid, suid;
299 if (getresuid (&ruid, &euid, &suid) < 0)
301 printf ("getresuid failed: %d %m\n", tno);
302 exit (1);
305 if (ruid != nobody_uid || euid != nobody_uid || suid != nobody_uid)
307 printf ("after setreuid %d (%d %d %d) != (%d %d %d)\n", tno,
308 ruid, euid, suid, nobody_uid, nobody_uid, nobody_uid);
309 exit (1);
315 static void
316 test_setreuid4 (enum ACTION action, int tno)
318 if (action == PREPARE)
320 if (setresuid (nobody_uid, nobody_uid, -1) < 0)
322 printf ("setresuid failed: %m\n");
323 exit (1);
326 prev_ruid = nobody_uid;
327 prev_euid = nobody_uid;
328 nobody_uid = prev_suid;
329 return;
332 test_setreuid1 (action, tno);
336 static void
337 test_setresuid1 (enum ACTION action, int tno)
339 if (action == PREPARE)
340 return;
342 if (action != CHECK_AFTER)
343 check_prev_uid (tno);
345 if (action == SET && setresuid (-1, nobody_uid, -1) < 0)
347 printf ("setresuid failed: %m\n");
348 exit (1);
351 if (action != CHECK_BEFORE)
353 uid_t ruid, euid, suid;
354 if (getresuid (&ruid, &euid, &suid) < 0)
356 printf ("getresuid failed: %d %m\n", tno);
357 exit (1);
360 if (ruid != prev_ruid || euid != nobody_uid || suid != prev_suid)
362 printf ("after setresuid %d (%d %d %d) != (%d %d %d)\n", tno,
363 ruid, euid, suid, prev_ruid, nobody_uid, prev_suid);
364 exit (1);
370 static void
371 test_setresuid2 (enum ACTION action, int tno)
373 if (action == PREPARE)
374 return;
376 if (action != CHECK_AFTER)
377 check_prev_uid (tno);
379 if (action == SET && setresuid (prev_euid, nobody_uid, nobody_uid) < 0)
381 printf ("setresuid failed: %m\n");
382 exit (1);
385 if (action != CHECK_BEFORE)
387 uid_t ruid, euid, suid;
388 if (getresuid (&ruid, &euid, &suid) < 0)
390 printf ("getresuid failed: %d %m\n", tno);
391 exit (1);
394 if (ruid != prev_euid || euid != nobody_uid || suid != nobody_uid)
396 printf ("after setresuid %d (%d %d %d) != (%d %d %d)\n", tno,
397 ruid, euid, suid, prev_euid, nobody_uid, nobody_uid);
398 exit (1);
404 static void
405 test_setresuid3 (enum ACTION action, int tno)
407 if (action == PREPARE)
408 return;
410 if (action != CHECK_AFTER)
411 check_prev_uid (tno);
413 if (action == SET && setresuid (nobody_uid, nobody_uid, nobody_uid) < 0)
415 printf ("setresuid failed: %m\n");
416 exit (1);
419 if (action != CHECK_BEFORE)
421 uid_t ruid, euid, suid;
422 if (getresuid (&ruid, &euid, &suid) < 0)
424 printf ("getresuid failed: %d %m\n", tno);
425 exit (1);
428 if (ruid != nobody_uid || euid != nobody_uid || suid != nobody_uid)
430 printf ("after setresuid %d (%d %d %d) != (%d %d %d)\n", tno,
431 ruid, euid, suid, nobody_uid, nobody_uid, nobody_uid);
432 exit (1);
438 static void
439 test_setresuid4 (enum ACTION action, int tno)
441 if (action == PREPARE)
443 if (setresuid (nobody_uid, nobody_uid, -1) < 0)
445 printf ("setresuid failed: %m\n");
446 exit (1);
449 prev_ruid = nobody_uid;
450 prev_euid = nobody_uid;
451 nobody_uid = prev_suid;
452 return;
455 test_setresuid1 (action, tno);
459 static void
460 test_setgid1 (enum ACTION action, int tno)
462 if (action == PREPARE)
463 return;
465 if (action != CHECK_AFTER)
466 check_prev_gid (tno);
468 if (action == SET && setgid (nobody_gid) < 0)
470 printf ("setgid failed: %m\n");
471 exit (1);
474 if (action != CHECK_BEFORE)
476 gid_t rgid, egid, sgid;
477 if (getresgid (&rgid, &egid, &sgid) < 0)
479 printf ("getresgid failed: %d %m\n", tno);
480 exit (1);
483 if (rgid != nobody_gid || egid != nobody_gid || sgid != nobody_gid)
485 printf ("after setgid %d (%d %d %d) != (%d %d %d)\n", tno,
486 rgid, egid, sgid, nobody_gid, nobody_gid, nobody_gid);
487 exit (1);
493 static void
494 test_setgid2 (enum ACTION action, int tno)
496 if (action == PREPARE)
498 if (setresgid (nobody_gid, nobody_gid, -1) < 0)
500 printf ("setresgid failed: %m\n");
501 exit (1);
504 prev_rgid = nobody_gid;
505 prev_egid = nobody_gid;
507 if (setresuid (nobody_uid, nobody_uid, -1) < 0)
509 printf ("setresuid failed: %m\n");
510 exit (1);
513 prev_ruid = nobody_uid;
514 prev_euid = nobody_uid;
515 return;
518 if (action != CHECK_AFTER)
519 check_prev_gid (tno);
521 if (action == SET && setgid (prev_sgid) < 0)
523 printf ("setgid failed: %m\n");
524 exit (1);
527 if (action != CHECK_BEFORE)
529 gid_t rgid, egid, sgid;
530 if (getresgid (&rgid, &egid, &sgid) < 0)
532 printf ("getresgid failed: %d %m\n", tno);
533 exit (1);
536 if (rgid != nobody_gid || egid != prev_sgid || sgid != prev_sgid)
538 printf ("after setgid %d (%d %d %d) != (%d %d %d)\n", tno,
539 rgid, egid, sgid, nobody_gid, prev_sgid, prev_sgid);
540 exit (1);
546 static void
547 test_setegid1 (enum ACTION action, int tno)
549 if (action == PREPARE)
550 return;
552 if (action != CHECK_AFTER)
553 check_prev_gid (tno);
555 if (action == SET && setegid (nobody_gid) < 0)
557 printf ("setegid failed: %m\n");
558 exit (1);
561 if (action != CHECK_BEFORE)
563 gid_t rgid, egid, sgid;
564 if (getresgid (&rgid, &egid, &sgid) < 0)
566 printf ("getresgid failed: %d %m\n", tno);
567 exit (1);
570 if (rgid != prev_rgid || egid != nobody_gid || sgid != prev_sgid)
572 printf ("after setegid %d (%d %d %d) != (%d %d %d)\n", tno,
573 rgid, egid, sgid, prev_rgid, nobody_gid, prev_sgid);
574 exit (1);
580 static void
581 test_setegid2 (enum ACTION action, int tno)
583 if (action == PREPARE)
585 if (setresgid (nobody_gid, nobody_gid, -1) < 0)
587 printf ("setresgid failed: %m\n");
588 exit (1);
591 prev_rgid = nobody_gid;
592 prev_egid = nobody_gid;
593 nobody_gid = prev_sgid;
594 return;
597 test_setegid1 (action, tno);
601 static void
602 test_setregid1 (enum ACTION action, int tno)
604 if (action == PREPARE)
605 return;
607 if (action != CHECK_AFTER)
608 check_prev_gid (tno);
610 if (action == SET && setregid (-1, nobody_gid) < 0)
612 printf ("setregid failed: %m\n");
613 exit (1);
616 if (action != CHECK_BEFORE)
618 gid_t rgid, egid, sgid, esgid;
619 if (getresgid (&rgid, &egid, &sgid) < 0)
621 printf ("getresgid failed: %d %m\n", tno);
622 exit (1);
625 if (prev_rgid != nobody_gid)
626 esgid = nobody_gid;
627 else
628 esgid = prev_sgid;
630 if (rgid != prev_rgid || egid != nobody_gid || sgid != esgid)
632 printf ("after setregid %d (%d %d %d) != (%d %d %d)\n", tno,
633 rgid, egid, sgid, prev_rgid, nobody_gid, esgid);
634 exit (1);
640 static void
641 test_setregid2 (enum ACTION action, int tno)
643 if (action == PREPARE)
644 return;
646 if (action != CHECK_AFTER)
647 check_prev_gid (tno);
649 if (action == SET && setregid (nobody_gid, -1) < 0)
651 printf ("setregid failed: %m\n");
652 exit (1);
655 if (action != CHECK_BEFORE)
657 gid_t rgid, egid, sgid;
658 if (getresgid (&rgid, &egid, &sgid) < 0)
660 printf ("getresgid failed: %d %m\n", tno);
661 exit (1);
664 if (rgid != nobody_gid || egid != prev_egid || sgid != prev_egid)
666 printf ("after setregid %d (%d %d %d) != (%d %d %d)\n", tno,
667 rgid, egid, sgid, nobody_gid, prev_egid, prev_egid);
668 exit (1);
674 static void
675 test_setregid3 (enum ACTION action, int tno)
677 if (action == PREPARE)
678 return;
680 if (action != CHECK_AFTER)
681 check_prev_gid (tno);
683 if (action == SET && setregid (nobody_gid, nobody_gid) < 0)
685 printf ("setregid failed: %m\n");
686 exit (1);
689 if (action != CHECK_BEFORE)
691 gid_t rgid, egid, sgid;
692 if (getresgid (&rgid, &egid, &sgid) < 0)
694 printf ("getresgid failed: %d %m\n", tno);
695 exit (1);
698 if (rgid != nobody_gid || egid != nobody_gid || sgid != nobody_gid)
700 printf ("after setregid %d (%d %d %d) != (%d %d %d)\n", tno,
701 rgid, egid, sgid, nobody_gid, nobody_gid, nobody_gid);
702 exit (1);
708 static void
709 test_setregid4 (enum ACTION action, int tno)
711 if (action == PREPARE)
713 if (setresgid (nobody_gid, nobody_gid, -1) < 0)
715 printf ("setresgid failed: %m\n");
716 exit (1);
719 prev_rgid = nobody_gid;
720 prev_egid = nobody_gid;
721 nobody_gid = prev_sgid;
722 return;
725 test_setregid1 (action, tno);
729 static void
730 test_setresgid1 (enum ACTION action, int tno)
732 if (action == PREPARE)
733 return;
735 if (action != CHECK_AFTER)
736 check_prev_gid (tno);
738 if (action == SET && setresgid (-1, nobody_gid, -1) < 0)
740 printf ("setresgid failed: %m\n");
741 exit (1);
744 if (action != CHECK_BEFORE)
746 gid_t rgid, egid, sgid;
747 if (getresgid (&rgid, &egid, &sgid) < 0)
749 printf ("getresgid failed: %d %m\n", tno);
750 exit (1);
753 if (rgid != prev_rgid || egid != nobody_gid || sgid != prev_sgid)
755 printf ("after setresgid %d (%d %d %d) != (%d %d %d)\n", tno,
756 rgid, egid, sgid, prev_rgid, nobody_gid, prev_sgid);
757 exit (1);
763 static void
764 test_setresgid2 (enum ACTION action, int tno)
766 if (action == PREPARE)
767 return;
769 if (action != CHECK_AFTER)
770 check_prev_gid (tno);
772 if (action == SET && setresgid (prev_egid, nobody_gid, nobody_gid) < 0)
774 printf ("setresgid failed: %m\n");
775 exit (1);
778 if (action != CHECK_BEFORE)
780 gid_t rgid, egid, sgid;
781 if (getresgid (&rgid, &egid, &sgid) < 0)
783 printf ("getresgid failed: %d %m\n", tno);
784 exit (1);
787 if (rgid != prev_egid || egid != nobody_gid || sgid != nobody_gid)
789 printf ("after setresgid %d (%d %d %d) != (%d %d %d)\n", tno,
790 rgid, egid, sgid, prev_egid, nobody_gid, nobody_gid);
791 exit (1);
797 static void
798 test_setresgid3 (enum ACTION action, int tno)
800 if (action == PREPARE)
801 return;
803 if (action != CHECK_AFTER)
804 check_prev_gid (tno);
806 if (action == SET && setresgid (nobody_gid, nobody_gid, nobody_gid) < 0)
808 printf ("setresgid failed: %m\n");
809 exit (1);
812 if (action != CHECK_BEFORE)
814 gid_t rgid, egid, sgid;
815 if (getresgid (&rgid, &egid, &sgid) < 0)
817 printf ("getresgid failed: %d %m\n", tno);
818 exit (1);
821 if (rgid != nobody_gid || egid != nobody_gid || sgid != nobody_gid)
823 printf ("after setresgid %d (%d %d %d) != (%d %d %d)\n", tno,
824 rgid, egid, sgid, nobody_gid, nobody_gid, nobody_gid);
825 exit (1);
831 static void
832 test_setresgid4 (enum ACTION action, int tno)
834 if (action == PREPARE)
836 if (setresgid (nobody_gid, nobody_gid, -1) < 0)
838 printf ("setresgid failed: %m\n");
839 exit (1);
842 prev_rgid = nobody_gid;
843 prev_egid = nobody_gid;
844 nobody_gid = prev_sgid;
845 return;
848 test_setresgid1 (action, tno);
852 static struct setuid_test
854 const char *name;
855 void (*test) (enum ACTION, int tno);
856 } setuid_tests[] =
858 { "setuid1", test_setuid1 },
859 { "setuid2", test_setuid2 },
860 { "seteuid1", test_seteuid1 },
861 { "seteuid2", test_seteuid2 },
862 { "setreuid1", test_setreuid1 },
863 { "setreuid2", test_setreuid2 },
864 { "setreuid3", test_setreuid3 },
865 { "setreuid4", test_setreuid4 },
866 { "setresuid1", test_setresuid1 },
867 { "setresuid2", test_setresuid2 },
868 { "setresuid3", test_setresuid3 },
869 { "setresuid4", test_setresuid4 },
870 { "setgid1", test_setgid1 },
871 { "setgid2", test_setgid2 },
872 { "setegid1", test_setegid1 },
873 { "setegid2", test_setegid2 },
874 { "setregid1", test_setregid1 },
875 { "setregid2", test_setregid2 },
876 { "setregid3", test_setregid3 },
877 { "setregid4", test_setregid4 },
878 { "setresgid1", test_setresgid1 },
879 { "setresgid2", test_setresgid2 },
880 { "setresgid3", test_setresgid3 },
881 { "setresgid4", test_setresgid4 }
885 static void *
886 tf2 (void *arg)
888 int e = pthread_barrier_wait (&b4);
889 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
891 puts ("barrier_wait failed");
892 exit (1);
895 setuid_tests[TESTNO (arg)].test (CHECK_AFTER, THREADNO (arg));
896 return NULL;
900 static void *
901 tf (void *arg)
903 setuid_tests[TESTNO (arg)].test (CHECK_BEFORE, THREADNO (arg));
905 int e = pthread_barrier_wait (&b3);
906 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
908 puts ("barrier_wait failed");
909 exit (1);
912 return tf2 (arg);
916 static int
917 do_one_test (long int testno)
919 printf ("%s test\n", setuid_tests[testno].name);
921 pid_t pid = fork ();
922 if (pid == 0)
924 setuid_tests[testno].test (PREPARE, 0);
925 setuid_tests[testno].test (SET, 0);
926 exit (0);
929 if (pid < 0)
931 printf ("fork failed: %m\n");
932 exit (1);
935 int status;
936 if (waitpid (pid, &status, 0) < 0)
938 printf ("waitpid failed: %m\n");
939 exit (1);
942 if (!WIFEXITED (status))
944 puts ("child did not exit");
945 exit (1);
948 if (WEXITSTATUS (status))
950 printf ("skipping %s test\n", setuid_tests[testno].name);
951 return 0;
954 pid = fork ();
955 if (pid == 0)
957 setuid_tests[testno].test (PREPARE, 0);
959 pthread_t th;
960 int e = pthread_create (&th, NULL, tf, (void *) (testno | 0x100L));
961 if (e != 0)
963 printf ("create failed: %m\n");
964 exit (1);
967 pthread_t th2;
968 e = pthread_create (&th2, NULL, tf, (void *) (testno | 0x200L));
969 if (e != 0)
971 printf ("create failed: %m\n");
972 exit (1);
975 e = pthread_barrier_wait (&b3);
976 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
978 puts ("barrier_wait failed");
979 exit (1);
982 setuid_tests[testno].test (SET, 0);
984 pthread_t th3;
985 e = pthread_create (&th3, NULL, tf2, (void *) (testno | 0x300L));
986 if (e != 0)
988 printf ("create failed: %m\n");
989 exit (1);
992 e = pthread_barrier_wait (&b4);
993 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
995 puts ("barrier_wait failed");
996 exit (1);
999 exit (0);
1002 if (pid < 0)
1004 printf ("fork failed: %m\n");
1005 exit (1);
1008 if (waitpid (pid, &status, 0) < 0)
1010 printf ("waitpid failed: %m\n");
1011 exit (1);
1014 if (!WIFEXITED (status))
1016 puts ("second child did not exit");
1017 exit (1);
1020 if (WEXITSTATUS (status))
1021 exit (WEXITSTATUS (status));
1023 return 0;
1027 static int
1028 do_test (void)
1030 struct passwd *pwd = getpwnam ("nobody");
1031 if (pwd == NULL)
1033 puts ("User nobody doesn't exist");
1034 return 0;
1036 nobody_uid = pwd->pw_uid;
1037 nobody_gid = pwd->pw_gid;
1039 if (getresuid (&prev_ruid, &prev_euid, &prev_suid) < 0)
1041 printf ("getresuid failed: %m\n");
1042 exit (1);
1045 if (getresgid (&prev_rgid, &prev_egid, &prev_sgid) < 0)
1047 printf ("getresgid failed: %m\n");
1048 exit (1);
1051 if (prev_ruid == nobody_uid || prev_euid == nobody_uid
1052 || prev_suid == nobody_uid)
1054 puts ("already running as user nobody, skipping tests");
1055 exit (0);
1058 if (prev_rgid == nobody_gid || prev_egid == nobody_gid
1059 || prev_sgid == nobody_gid)
1061 puts ("already running as group nobody, skipping tests");
1062 exit (0);
1065 if (pthread_barrier_init (&b3, NULL, 3) != 0)
1067 puts ("barrier_init failed");
1068 exit (1);
1071 if (pthread_barrier_init (&b4, NULL, 4) != 0)
1073 puts ("barrier_init failed");
1074 exit (1);
1077 for (unsigned long int testno = 0;
1078 testno < sizeof (setuid_tests) / sizeof (setuid_tests[0]);
1079 ++testno)
1080 do_one_test (testno);
1081 return 0;
1084 #define TEST_FUNCTION do_test ()
1085 #include "../test-skeleton.c"