123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- #include <errno.h>
- #include <fcntl.h>
- #include <pthreadP.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/prctl.h>
- #include <not-cancel.h>
- int
- pthread_setname_np (pthread_t th, const char *name)
- {
- const struct pthread *pd = (const struct pthread *) th;
-
- #define TASK_COMM_LEN 16
- size_t name_len = strlen (name);
- if (name_len >= TASK_COMM_LEN)
- return ERANGE;
- if (pd == THREAD_SELF)
- return prctl (PR_SET_NAME, name) ? errno : 0;
- #define FMT "/proc/self/task/%u/comm"
- char fname[sizeof (FMT) + 8];
- sprintf (fname, FMT, (unsigned int) pd->tid);
- int fd = open_not_cancel_2 (fname, O_RDWR);
- if (fd == -1)
- return errno;
- int res = 0;
- ssize_t n = TEMP_FAILURE_RETRY (write_not_cancel (fd, name, name_len));
- if (n < 0)
- res = errno;
- else if (n != name_len)
- res = EIO;
- close_not_cancel_no_status (fd);
- return res;
- }
|