]> granicus.if.org Git - strace/blob - tests/setgroups.c
Fix preprocessor indentation
[strace] / tests / setgroups.c
1 /*
2  * Check decoding of setgroups/setgroups32 syscalls.
3  *
4  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2016-2018 The strace developers.
6  * All rights reserved.
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #ifdef __NR_setgroups32
12
13 # define SYSCALL_NR     __NR_setgroups32
14 # define SYSCALL_NAME   "setgroups32"
15 # define GID_TYPE       unsigned int
16
17 #else
18
19 # include "tests.h"
20 # include "scno.h"
21
22 # ifdef __NR_setgroups
23
24 #  define SYSCALL_NR    __NR_setgroups
25 #  define SYSCALL_NAME  "setgroups"
26 #  if defined __NR_setgroups32 && __NR_setgroups != __NR_setgroups32
27 #   define GID_TYPE     unsigned short
28 #  else
29 #   define GID_TYPE     unsigned int
30 #  endif
31
32 # endif
33
34 #endif
35
36 #ifdef GID_TYPE
37
38 # include <stdio.h>
39 # include <unistd.h>
40
41 void
42 printuid(GID_TYPE id)
43 {
44         if (id == (GID_TYPE) -1U)
45                 printf("-1");
46         else
47                 printf("%u", id);
48 }
49
50 int
51 main(void)
52 {
53         const char *errstr;
54
55         /* check how the first argument is decoded */
56         long rc = syscall(SYSCALL_NR, 0, 0);
57         printf("%s(0, NULL) = %s\n", SYSCALL_NAME, sprintrc(rc));
58
59         rc = syscall(SYSCALL_NR, F8ILL_KULONG_MASK, 0);
60         printf("%s(0, NULL) = %s\n", SYSCALL_NAME, sprintrc(rc));
61
62         rc = syscall(SYSCALL_NR, 1, 0);
63         printf("%s(1, NULL) = %s\n", SYSCALL_NAME, sprintrc(rc));
64
65         rc = syscall(SYSCALL_NR, (long) 0xffffffff00000001ULL, 0);
66         printf("%s(1, NULL) = %s\n", SYSCALL_NAME, sprintrc(rc));
67
68         rc = syscall(SYSCALL_NR, -1U, 0);
69         printf("%s(%d, NULL) = %s\n", SYSCALL_NAME, -1, sprintrc(rc));
70
71         rc = syscall(SYSCALL_NR, -1L, 0);
72         printf("%s(%d, NULL) = %s\n", SYSCALL_NAME, -1, sprintrc(rc));
73
74         /* check how the second argument is decoded */
75         TAIL_ALLOC_OBJECT_CONST_PTR(const GID_TYPE, g1);
76         GID_TYPE *const g2 = tail_alloc(sizeof(*g2) * 2);
77         GID_TYPE *const g3 = tail_alloc(sizeof(*g3) * 3);
78
79         rc = syscall(SYSCALL_NR, 0, g1 + 1);
80         printf("%s(0, []) = %s\n", SYSCALL_NAME, sprintrc(rc));
81
82         rc = syscall(SYSCALL_NR, 1, g1);
83         errstr = sprintrc(rc);
84         printf("%s(1, [", SYSCALL_NAME);
85         printuid(*g1);
86         printf("]) = %s\n", errstr);
87
88         rc = syscall(SYSCALL_NR, 1, g1 + 1);
89         printf("%s(1, %p) = %s\n", SYSCALL_NAME, g1 + 1, sprintrc(rc));
90
91         rc = syscall(SYSCALL_NR, 1, -1L);
92         printf("%s(1, %#lx) = %s\n", SYSCALL_NAME, -1L, sprintrc(rc));
93
94         rc = syscall(SYSCALL_NR, 2, g1);
95         errstr = sprintrc(rc);
96         printf("%s(2, [", SYSCALL_NAME);
97         printuid(*g1);
98         printf(", ... /* %p */]) = %s\n", g1 + 1, errstr);
99
100         g2[0] = -2;
101         g2[1] = -3;
102         rc = syscall(SYSCALL_NR, 2, g2);
103         errstr = sprintrc(rc);
104         printf("%s(2, [", SYSCALL_NAME);
105         printuid(g2[0]);
106         printf(", ");
107         printuid(g2[1]);
108         printf("]) = %s\n", errstr);
109
110         rc = syscall(SYSCALL_NR, 3, g2);
111         errstr = sprintrc(rc);
112         printf("%s(3, [", SYSCALL_NAME);
113         printuid(g2[0]);
114         printf(", ");
115         printuid(g2[1]);
116         printf(", ... /* %p */]) = %s\n", g2 + 2, errstr);
117
118         g3[0] = 0;
119         g3[1] = 1;
120         rc = syscall(SYSCALL_NR, 3, g3);
121         errstr = sprintrc(rc);
122         printf("%s(3, [", SYSCALL_NAME);
123         printuid(g3[0]);
124         printf(", ");
125         printuid(g3[1]);
126         printf(", ...]) = %s\n", errstr);
127
128         rc = syscall(SYSCALL_NR, 4, g3);
129         errstr = sprintrc(rc);
130         printf("%s(4, [", SYSCALL_NAME);
131         printuid(g3[0]);
132         printf(", ");
133         printuid(g3[1]);
134         printf(", ...]) = %s\n", errstr);
135
136         rc = sysconf(_SC_NGROUPS_MAX);
137         const unsigned ngroups_max = rc;
138
139         if ((unsigned long) rc == ngroups_max && (int) ngroups_max > 0) {
140                 rc = syscall(SYSCALL_NR, ngroups_max, g3);
141                 errstr = sprintrc(rc);
142                 printf("%s(%d, [", SYSCALL_NAME, ngroups_max);
143                 printuid(g3[0]);
144                 printf(", ");
145                 printuid(g3[1]);
146                 printf(", ...]) = %s\n", errstr);
147
148                 rc = syscall(SYSCALL_NR, F8ILL_KULONG_MASK | ngroups_max, g3);
149                 errstr = sprintrc(rc);
150                 printf("%s(%d, [", SYSCALL_NAME, ngroups_max);
151                 printuid(g3[0]);
152                 printf(", ");
153                 printuid(g3[1]);
154                 printf(", ...]) = %s\n", errstr);
155
156                 rc = syscall(SYSCALL_NR, ngroups_max + 1, g3);
157                 printf("%s(%d, %p) = %s\n", SYSCALL_NAME,
158                        ngroups_max + 1, g3, sprintrc(rc));
159         }
160
161         puts("+++ exited with 0 +++");
162         return 0;
163 }
164
165 #else
166
167 SKIP_MAIN_UNDEFINED("__NR_setgroups")
168
169 #endif