]>
Commit | Line | Data |
---|---|---|
1db94db1 | 1 | /* Test pthread_setname_np and pthread_getname_np. |
d4697bc9 | 2 | Copyright (C) 2013-2014 Free Software Foundation, Inc. |
1db94db1 CD |
3 | This file is part of the GNU C Library. |
4 | ||
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 License as | |
7 | published by the Free Software Foundation; either version 2.1 of the | |
8 | License, or (at your option) any later version. | |
9 | ||
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. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; see the file COPYING.LIB. If | |
17 | not, see <http://www.gnu.org/licenses/>. */ | |
18 | #include <stdio.h> | |
19 | #include <stdlib.h> | |
20 | #include <pthread.h> | |
21 | #include <string.h> | |
22 | #include <sys/syscall.h> | |
23 | #include <unistd.h> | |
24 | #include <fcntl.h> | |
25 | #include <errno.h> | |
26 | ||
27 | /* New name of process. */ | |
28 | #define NEW_NAME "setname" | |
29 | ||
30 | /* Name of process which is one byte too big | |
31 | e.g. 17 bytes including null-terminator */ | |
32 | #define BIG_NAME "....V....X....XV" | |
33 | ||
34 | /* Longest name of a process | |
35 | e.g. 16 bytes including null-terminator. */ | |
36 | #define LONGEST_NAME "....V....X....X" | |
37 | ||
38 | /* One less than longest name with unique | |
39 | characters to detect modification. */ | |
40 | #define CANARY_NAME "abcdefghijklmn" | |
41 | ||
42 | /* On Linux the maximum length of the name of a task *including* the null | |
43 | terminator. */ | |
44 | #define TASK_COMM_LEN 16 | |
45 | ||
46 | long | |
47 | gettid (void) | |
48 | { | |
49 | return syscall(__NR_gettid); | |
50 | } | |
51 | ||
52 | /* On Linux we can read this task's name from /proc. */ | |
53 | int | |
54 | get_self_comm (long tid, char *buf, size_t len) | |
55 | { | |
56 | int res = 0; | |
57 | #define FMT "/proc/self/task/%lu/comm" | |
58 | char fname[sizeof (FMT) + 8]; | |
59 | sprintf (fname, FMT, (unsigned long) tid); | |
60 | ||
61 | int fd = open (fname, O_RDONLY); | |
62 | if (fd == -1) | |
63 | return errno; | |
64 | ||
65 | ssize_t n = read (fd, (void *) buf, len); | |
66 | if (n < 0) | |
67 | res = errno; | |
68 | else | |
69 | { | |
70 | if (buf[n - 1] == '\n') | |
71 | buf[n - 1] = '\0'; | |
72 | else if (n == len) | |
73 | res = ERANGE; | |
74 | else | |
75 | buf[n] = '\0'; | |
76 | } | |
77 | ||
78 | close (fd); | |
79 | return res; | |
80 | } | |
81 | ||
82 | int | |
83 | do_test (int argc, char **argv) | |
84 | { | |
85 | pthread_t self; | |
86 | int res; | |
87 | int ret = 0; | |
88 | char name[TASK_COMM_LEN]; | |
89 | char name_check[TASK_COMM_LEN]; | |
90 | ||
91 | memset (name, '\0', TASK_COMM_LEN); | |
92 | memset (name_check, '\0', TASK_COMM_LEN); | |
93 | ||
94 | /* Test 1: Get the name of the task via pthread_getname_np and /proc | |
95 | and verify that they both match. */ | |
96 | self = pthread_self (); | |
97 | res = pthread_getname_np (self, name, TASK_COMM_LEN); | |
98 | ||
99 | if (res == 0) | |
100 | { | |
101 | res = get_self_comm (gettid (), name_check, TASK_COMM_LEN); | |
102 | if (res == 0) | |
103 | { | |
104 | if (strncmp (name, name_check, strlen (BIG_NAME)) == 0) | |
105 | printf ("PASS: Test 1 - pthread_getname_np and /proc agree.\n"); | |
106 | else | |
107 | { | |
108 | printf ("FAIL: Test 1 - pthread_getname_np and /proc differ" | |
109 | " i.e. %s != %s\n", name, name_check); | |
110 | ret++; | |
111 | } | |
112 | } | |
113 | else | |
114 | { | |
115 | printf ("FAIL: Test 1 - unable read task name via proc.\n"); | |
116 | ret++; | |
117 | } | |
118 | } | |
119 | else | |
120 | { | |
121 | printf ("FAIL: Test 1 - pthread_getname_np failed with error %d\n", res); | |
122 | ret++; | |
123 | } | |
124 | ||
125 | /* Test 2: Test setting the name and then independently verify it | |
126 | was set. */ | |
127 | res = pthread_setname_np (self, NEW_NAME); | |
128 | ||
129 | if (res == 0) | |
130 | { | |
131 | res = get_self_comm (gettid (), name_check, TASK_COMM_LEN); | |
132 | if (res == 0) | |
133 | { | |
134 | if (strncmp (NEW_NAME, name_check, strlen (BIG_NAME)) == 0) | |
135 | printf ("PASS: Test 2 - Value used in pthread_setname_np and" | |
136 | " /proc agree.\n"); | |
137 | else | |
138 | { | |
139 | printf ("FAIL: Test 2 - Value used in pthread_setname_np" | |
140 | " and /proc differ i.e. %s != %s\n", | |
141 | NEW_NAME, name_check); | |
142 | ret++; | |
143 | } | |
144 | } | |
145 | else | |
146 | { | |
147 | printf ("FAIL: Test 2 - unable to read task name via proc.\n"); | |
148 | ret++; | |
149 | } | |
150 | } | |
151 | else | |
152 | { | |
153 | printf ("FAIL: Test 2 - pthread_setname_np failed with error %d\n", res); | |
154 | ret++; | |
155 | } | |
156 | ||
157 | /* Test 3: Test setting a name that is one-byte too big. */ | |
158 | res = pthread_getname_np (self, name, TASK_COMM_LEN); | |
159 | ||
160 | if (res == 0) | |
161 | { | |
162 | res = pthread_setname_np (self, BIG_NAME); | |
163 | if (res != 0) | |
164 | { | |
165 | if (res == ERANGE) | |
166 | { | |
167 | printf ("PASS: Test 3 - pthread_setname_np returned ERANGE" | |
168 | " for a process name that was too long.\n"); | |
169 | ||
170 | /* Verify the old name didn't change. */ | |
171 | res = get_self_comm (gettid (), name_check, TASK_COMM_LEN); | |
172 | if (res == 0) | |
173 | { | |
174 | if (strncmp (name, name_check, strlen (BIG_NAME)) == 0) | |
175 | printf ("PASS: Test 3 - Original name unchanged after" | |
176 | " pthread_setname_np returned ERANGE.\n"); | |
177 | else | |
178 | { | |
179 | printf ("FAIL: Test 3 - Original name changed after" | |
180 | " pthread_setname_np returned ERANGE" | |
181 | " i.e. %s != %s\n", | |
182 | name, name_check); | |
183 | ret++; | |
184 | } | |
185 | } | |
186 | else | |
187 | { | |
188 | printf ("FAIL: Test 3 - unable to read task name.\n"); | |
189 | ret++; | |
190 | } | |
191 | } | |
192 | else | |
193 | { | |
194 | printf ("FAIL: Test 3 - Wrong error returned" | |
195 | " i.e. ERANGE != %d\n", res); | |
196 | ret++; | |
197 | } | |
198 | } | |
199 | else | |
200 | { | |
201 | printf ("FAIL: Test 3 - Too-long name accepted by" | |
202 | " pthread_setname_np.\n"); | |
203 | ret++; | |
204 | } | |
205 | } | |
206 | else | |
207 | { | |
208 | printf ("FAIL: Test 3 - Unable to get original name.\n"); | |
209 | ret++; | |
210 | } | |
211 | ||
212 | /* Test 4: Verify that setting the longest name works. */ | |
213 | res = pthread_setname_np (self, LONGEST_NAME); | |
214 | ||
215 | if (res == 0) | |
216 | { | |
217 | res = get_self_comm (gettid (), name_check, TASK_COMM_LEN); | |
218 | if (res == 0) | |
219 | { | |
220 | if (strncmp (LONGEST_NAME, name_check, strlen (BIG_NAME)) == 0) | |
221 | printf ("PASS: Test 4 - Longest name set via pthread_setname_np" | |
222 | " agrees with /proc.\n"); | |
223 | else | |
224 | { | |
225 | printf ("FAIL: Test 4 - Value used in pthread_setname_np and /proc" | |
226 | " differ i.e. %s != %s\n", LONGEST_NAME, name_check); | |
227 | ret++; | |
228 | } | |
229 | } | |
230 | else | |
231 | { | |
232 | printf ("FAIL: Test 4 - unable to read task name via proc.\n"); | |
233 | ret++; | |
234 | } | |
235 | } | |
236 | else | |
237 | { | |
238 | printf ("FAIL: Test 4 - pthread_setname_np failed with error %d\n", res); | |
239 | ret++; | |
240 | } | |
241 | ||
242 | /* Test 5: Verify that getting a long name into a small buffer fails. */ | |
243 | strncpy (name, CANARY_NAME, strlen (CANARY_NAME) + 1); | |
244 | ||
245 | /* Claim the buffer length is strlen (LONGEST_NAME). This is one character | |
246 | too small to hold LONGEST_NAME *and* the null terminator. We should get | |
247 | back ERANGE and name should be unmodified. */ | |
248 | res = pthread_getname_np (self, name, strlen (LONGEST_NAME)); | |
249 | ||
250 | if (res != 0) | |
251 | { | |
252 | if (res == ERANGE) | |
253 | { | |
254 | if (strncmp (CANARY_NAME, name, strlen (BIG_NAME)) == 0) | |
255 | { | |
256 | printf ("PASS: Test 5 - ERANGE and buffer unmodified.\n"); | |
257 | } | |
258 | else | |
259 | { | |
260 | printf ("FAIL: Test 5 - Original buffer modified.\n"); | |
261 | ret++; | |
262 | } | |
263 | } | |
264 | else | |
265 | { | |
266 | printf ("FAIL: Test 5 - Did not return ERANGE for small buffer.\n"); | |
267 | ret++; | |
268 | } | |
269 | } | |
270 | else | |
271 | { | |
272 | printf ("FAIL: Test 5 - Returned name longer than buffer.\n"); | |
273 | ret++; | |
274 | } | |
275 | ||
276 | /* Test 6: Lastly make sure we can read back the longest name. */ | |
277 | res = pthread_getname_np (self, name, strlen (LONGEST_NAME) + 1); | |
278 | ||
279 | if (res == 0) | |
280 | { | |
281 | if (strncmp (LONGEST_NAME, name, strlen (BIG_NAME)) == 0) | |
282 | { | |
283 | printf ("PASS: Test 6 - Read back longest name correctly.\n"); | |
284 | } | |
285 | else | |
286 | { | |
287 | printf ("FAIL: Test 6 - Read \"%s\" instead of longest name.\n", | |
288 | name); | |
289 | ret++; | |
290 | } | |
291 | } | |
292 | else | |
293 | { | |
294 | printf ("FAIL: Test 6 - pthread_getname_np failed with error %d\n", res); | |
295 | ret++; | |
296 | } | |
297 | ||
298 | return ret; | |
299 | } | |
300 | ||
301 | #include <test-skeleton.c> |