8158 Want named threads API
[unleashed.git] / usr / src / test / libc-tests / tests / threads / thread_name.c
blobb164272644e66b9d52098f90f73fc6e588c44af0
1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright 2018 Joyent, Inc.
17 * Some basic pthread name API tests.
20 #include <sys/stat.h>
21 #include <pthread.h>
22 #include <limits.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <thread.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <err.h>
33 /*ARGSUSED*/
34 static void *
35 thr(void *unused)
37 (void) sleep(100);
38 return (NULL);
41 /*ARGSUSED*/
42 int
43 main(int argc, char *argv[])
45 char name[PTHREAD_MAX_NAMELEN_NP];
46 pthread_attr_t attr;
47 char path[PATH_MAX];
48 pthread_t tid;
49 ssize_t n;
50 int test;
51 int rc;
52 int fd;
54 /* Default thread name is empty string. */
55 test = 1;
57 rc = pthread_getname_np(pthread_self(), name, sizeof (name));
59 if (rc != 0 || strcmp(name, "") != 0)
60 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
62 /* Can set name. */
63 test = 2;
65 (void) strlcpy(name, "main", sizeof (name));
66 rc = pthread_setname_np(pthread_self(), name);
68 if (rc != 0)
69 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
71 rc = pthread_getname_np(pthread_self(), name, sizeof (name));
73 if (rc != 0 || strcmp(name, "main") != 0)
74 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
76 /* ERANGE check. */
77 test = 3;
79 rc = pthread_getname_np(pthread_self(), name, 2);
81 if (rc != ERANGE)
82 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
84 /* EINVAL check. */
85 test = 4;
87 rc = pthread_getname_np(pthread_self(), NULL, sizeof (name));
89 if (rc != EINVAL)
90 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
92 /* can clear thread name. */
93 test = 5;
95 rc = pthread_setname_np(pthread_self(), NULL);
97 if (rc != 0)
98 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
100 rc = pthread_getname_np(pthread_self(), name, sizeof (name));
102 if (rc != 0 || strcmp(name, "") != 0)
103 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
105 /* non-existent thread check. */
106 test = 6;
108 rc = pthread_getname_np(808, name, sizeof (name));
110 if (rc != ESRCH)
111 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
113 rc = pthread_setname_np(808, "state");
115 if (rc != ESRCH)
116 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
118 /* too long a name. */
119 test = 7;
121 rc = pthread_setname_np(pthread_self(),
122 "12345678901234567890123456789012");
124 if (rc != ERANGE)
125 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
127 /* can name another thread. */
128 test = 8;
130 rc = pthread_create(&tid, NULL, thr, NULL);
132 if (rc != 0)
133 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
135 rc = pthread_setname_np(tid, "otherthread");
137 if (rc != 0)
138 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
140 /* attr tests. */
141 test = 9;
143 (void) pthread_attr_init(&attr);
145 rc = pthread_attr_setname_np(&attr,
146 "12345678901234567890123456789012");
148 if (rc != ERANGE)
149 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
151 rc = pthread_attr_setname_np(&attr, "thread2");
153 if (rc != 0)
154 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
156 rc = pthread_attr_getname_np(&attr, NULL, sizeof (name));
158 if (rc != EINVAL)
159 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
161 rc = pthread_attr_getname_np(&attr, name, 2);
163 if (rc != ERANGE)
164 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
166 /* does the attr actually apply? */
167 test = 10;
169 rc = pthread_create(&tid, &attr, thr, NULL);
171 if (rc != 0)
172 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
174 rc = pthread_getname_np(tid, name, sizeof (name));
176 if (rc != 0 || strcmp(name, "thread2") != 0)
177 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
179 /* proc read tests */
180 test = 11;
182 (void) snprintf(path, sizeof (path),
183 "/proc/self/lwp/%d/lwpname", (int)tid);
185 fd = open(path, O_RDWR);
187 if (fd == -1)
188 errx(EXIT_FAILURE, "test %d failed with %d", test, errno);
190 n = read(fd, name, sizeof (name));
192 if (n != sizeof (name) || strcmp(name, "thread2") != 0)
193 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
195 if (lseek(fd, 0, SEEK_SET) != 0)
196 errx(EXIT_FAILURE, "test %d failed with %d", test, errno);
198 n = read(fd, name, PTHREAD_MAX_NAMELEN_NP * 2);
200 if (n != sizeof (name) || strcmp(name, "thread2") != 0)
201 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
203 if (lseek(fd, 0, SEEK_SET) != 0)
204 errx(EXIT_FAILURE, "test %d failed with %d", test, errno);
206 n = read(fd, name, 4);
208 if (n != 4 || strncmp(name, "thre", 4) != 0)
209 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
211 /* proc write tests */
212 test = 12;
214 if (lseek(fd, 0, SEEK_SET) != 0)
215 errx(EXIT_FAILURE, "test %d failed with %d", test, errno);
217 n = write(fd, "1234567890123456789012345678901",
218 PTHREAD_MAX_NAMELEN_NP);
220 if (n != PTHREAD_MAX_NAMELEN_NP)
221 errx(EXIT_FAILURE, "test %d failed with %d", test, errno);
223 if (lseek(fd, 0, SEEK_SET) != 0)
224 errx(EXIT_FAILURE, "test %d failed with %d", test, errno);
226 n = write(fd, "foo", sizeof ("foo"));
228 if (n != sizeof ("foo"))
229 errx(EXIT_FAILURE, "test %d failed with %d", test, errno);
231 if (lseek(fd, 0, SEEK_SET) != 0)
232 errx(EXIT_FAILURE, "test %d failed with %d", test, errno);
234 n = read(fd, name, sizeof (name));
236 if (n != sizeof (name) || strcmp(name, "foo") != 0)
237 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
239 (void) close(fd);
241 /* thr_* API. */
242 test = 13;
244 rc = thr_setname(thr_self(), "main");
246 if (rc != 0)
247 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
249 rc = thr_getname(thr_self(), name, sizeof (name));
251 if (rc != 0 || strcmp(name, "main") != 0)
252 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
254 /* badness */
255 test = 14;
257 rc = thr_setname(thr_self(), "\033]0;messeduptitle\a");
259 if (rc != EINVAL)
260 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
262 rc = thr_setname(thr_self(), "ab\177\177\n");
264 if (rc != EINVAL)
265 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
267 rc = pthread_attr_setname_np(&attr, "\033]0;messeduptitle\a");
269 if (rc != EINVAL)
270 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
272 rc = pthread_attr_setname_np(&attr, "ab\177\177\n");
274 if (rc != EINVAL)
275 errx(EXIT_FAILURE, "test %d failed with %d", test, rc);
277 return (EXIT_SUCCESS);