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
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.
43 main(int argc
, char *argv
[])
45 char name
[PTHREAD_MAX_NAMELEN_NP
];
54 /* Default thread name is empty string. */
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
);
65 (void) strlcpy(name
, "main", sizeof (name
));
66 rc
= pthread_setname_np(pthread_self(), name
);
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
);
79 rc
= pthread_getname_np(pthread_self(), name
, 2);
82 errx(EXIT_FAILURE
, "test %d failed with %d", test
, rc
);
87 rc
= pthread_getname_np(pthread_self(), NULL
, sizeof (name
));
90 errx(EXIT_FAILURE
, "test %d failed with %d", test
, rc
);
92 /* can clear thread name. */
95 rc
= pthread_setname_np(pthread_self(), NULL
);
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. */
108 rc
= pthread_getname_np(808, name
, sizeof (name
));
111 errx(EXIT_FAILURE
, "test %d failed with %d", test
, rc
);
113 rc
= pthread_setname_np(808, "state");
116 errx(EXIT_FAILURE
, "test %d failed with %d", test
, rc
);
118 /* too long a name. */
121 rc
= pthread_setname_np(pthread_self(),
122 "12345678901234567890123456789012");
125 errx(EXIT_FAILURE
, "test %d failed with %d", test
, rc
);
127 /* can name another thread. */
130 rc
= pthread_create(&tid
, NULL
, thr
, NULL
);
133 errx(EXIT_FAILURE
, "test %d failed with %d", test
, rc
);
135 rc
= pthread_setname_np(tid
, "otherthread");
138 errx(EXIT_FAILURE
, "test %d failed with %d", test
, rc
);
143 (void) pthread_attr_init(&attr
);
145 rc
= pthread_attr_setname_np(&attr
,
146 "12345678901234567890123456789012");
149 errx(EXIT_FAILURE
, "test %d failed with %d", test
, rc
);
151 rc
= pthread_attr_setname_np(&attr
, "thread2");
154 errx(EXIT_FAILURE
, "test %d failed with %d", test
, rc
);
156 rc
= pthread_attr_getname_np(&attr
, NULL
, sizeof (name
));
159 errx(EXIT_FAILURE
, "test %d failed with %d", test
, rc
);
161 rc
= pthread_attr_getname_np(&attr
, name
, 2);
164 errx(EXIT_FAILURE
, "test %d failed with %d", test
, rc
);
166 /* does the attr actually apply? */
169 rc
= pthread_create(&tid
, &attr
, thr
, NULL
);
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 */
182 (void) snprintf(path
, sizeof (path
),
183 "/proc/self/lwp/%d/lwpname", (int)tid
);
185 fd
= open(path
, O_RDWR
);
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 */
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
);
244 rc
= thr_setname(thr_self(), "main");
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
);
257 rc
= thr_setname(thr_self(), "\033]0;messeduptitle\a");
260 errx(EXIT_FAILURE
, "test %d failed with %d", test
, rc
);
262 rc
= thr_setname(thr_self(), "ab\177\177\n");
265 errx(EXIT_FAILURE
, "test %d failed with %d", test
, rc
);
267 rc
= pthread_attr_setname_np(&attr
, "\033]0;messeduptitle\a");
270 errx(EXIT_FAILURE
, "test %d failed with %d", test
, rc
);
272 rc
= pthread_attr_setname_np(&attr
, "ab\177\177\n");
275 errx(EXIT_FAILURE
, "test %d failed with %d", test
, rc
);
277 return (EXIT_SUCCESS
);