Split timed-wait functions out of nptl/lowlevellock.c.
[glibc.git] / nptl / tst-setuid3.c
blob553febc7dcdcab59458d716adbd72d575fc08ddc
1 /* Copyright (C) 2014-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <stdio.h>
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stdbool.h>
22 #include <unistd.h>
24 /* The test must run under a non-privileged user ID. */
25 static const uid_t test_uid = 1;
27 static pthread_barrier_t barrier1;
28 static pthread_barrier_t barrier2;
30 #define FAIL(fmt, ...) \
31 do { printf ("FAIL: " fmt "\n", __VA_ARGS__); _exit (1); } while (0)
33 #define FAIL_ERR(fmt, ...) \
34 do { printf ("FAIL: " fmt ": %m\n", __VA_ARGS__); _exit (1); } while (0)
36 static void *
37 thread_func (void *ctx __attribute__ ((unused)))
39 int ret = pthread_barrier_wait (&barrier1);
40 if (ret != PTHREAD_BARRIER_SERIAL_THREAD && ret != 0)
41 FAIL ("pthread_barrier_wait (barrier1) (on thread): %d", ret);
42 ret = pthread_barrier_wait (&barrier2);
43 if (ret != PTHREAD_BARRIER_SERIAL_THREAD && ret != 0)
44 FAIL ("pthread_barrier_wait (barrier2) (on thread): %d", ret);
45 return NULL;
48 static void
49 setuid_failure (int phase)
51 int ret = setuid (0);
52 switch (ret)
54 case 0:
55 FAIL ("setuid succeeded unexpectedly in phase %d", phase);
56 case -1:
57 if (errno != EPERM)
58 FAIL_ERR ("setuid phase %d", phase);
59 break;
60 default:
61 FAIL ("invalid setuid return value in phase %d: %d", phase, ret);
65 static int
66 do_test (void)
68 if (getuid () == 0)
69 if (setuid (test_uid) != 0)
70 FAIL_ERR ("setuid (%u)", (unsigned) test_uid);
71 if (setuid (getuid ()))
72 FAIL_ERR ("setuid (%s)", "getuid ()");
73 setuid_failure (1);
75 int ret = pthread_barrier_init (&barrier1, NULL, 2);
76 if (ret != 0)
77 FAIL ("pthread_barrier_init (barrier1): %d", ret);
78 ret = pthread_barrier_init (&barrier2, NULL, 2);
79 if (ret != 0)
80 FAIL ("pthread_barrier_init (barrier2): %d", ret);
82 pthread_t thread;
83 ret = pthread_create (&thread, NULL, thread_func, NULL);
84 if (ret != 0)
85 FAIL ("pthread_create: %d", ret);
87 /* Ensure that the thread is running properly. */
88 ret = pthread_barrier_wait (&barrier1);
89 if (ret != 0)
90 FAIL ("pthread_barrier_wait (barrier1): %d", ret);
92 setuid_failure (2);
94 /* Check success case. */
95 if (setuid (getuid ()) != 0)
96 FAIL_ERR ("setuid (%s)", "getuid ()");
98 /* Shutdown. */
99 ret = pthread_barrier_wait (&barrier2);
100 if (ret != PTHREAD_BARRIER_SERIAL_THREAD && ret != 0)
101 FAIL ("pthread_barrier_wait (barrier2): %d", ret);
103 if (ret != PTHREAD_BARRIER_SERIAL_THREAD && ret != 0)
104 FAIL ("pthread_join: %d", ret);
106 return 0;
109 #define TEST_FUNCTION do_test ()
110 #include "../test-skeleton.c"