]> git.proxmox.com Git - wasi-libc.git/blame - libc-top-half/musl/src/thread/pthread_setname_np.c
bump version to 0.0~git20240411.9e8c542-3~bpo12+pve1
[wasi-libc.git] / libc-top-half / musl / src / thread / pthread_setname_np.c
CommitLineData
320054e8
DG
1#define _GNU_SOURCE
2#include <fcntl.h>
3#include <string.h>
4#include <unistd.h>
5#include <sys/prctl.h>
6
7#include "pthread_impl.h"
8
9int pthread_setname_np(pthread_t thread, const char *name)
10{
11 int fd, cs, status = 0;
12 char f[sizeof "/proc/self/task//comm" + 3*sizeof(int)];
13 size_t len;
14
15 if ((len = strnlen(name, 16)) > 15) return ERANGE;
16
17 if (thread == pthread_self())
18 return prctl(PR_SET_NAME, (unsigned long)name, 0UL, 0UL, 0UL) ? errno : 0;
19
20 snprintf(f, sizeof f, "/proc/self/task/%d/comm", thread->tid);
21 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
5d8a1409 22 if ((fd = open(f, O_WRONLY|O_CLOEXEC)) < 0 || write(fd, name, len) < 0) status = errno;
320054e8
DG
23 if (fd >= 0) close(fd);
24 pthread_setcancelstate(cs, 0);
25 return status;
26}