X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Futil.c;h=f59897105a6354e1cea1c7261517e2a556c660ce;hb=8674debc67cdad04d930d931405a360f15e5cb28;hp=c396fc7c6d4ccdbee1ea35f2239f059053034130;hpb=92d75ca419994f3924dc14117b71f8706d9e1f57;p=elogind.git diff --git a/src/shared/util.c b/src/shared/util.c index c396fc7c6..f59897105 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -86,7 +86,7 @@ static volatile unsigned cached_columns = 0; static volatile unsigned cached_lines = 0; size_t page_size(void) { - static __thread size_t pgsz = 0; + static thread_local size_t pgsz = 0; long r; if (_likely_(pgsz > 0)) @@ -2737,9 +2737,9 @@ int rm_rf_children_dangerous(int fd, bool only_dirs, bool honour_sticky, struct _pure_ static int is_temporary_fs(struct statfs *s) { assert(s); - return - F_TYPE_EQUAL(s->f_type, TMPFS_MAGIC) || - F_TYPE_EQUAL(s->f_type, RAMFS_MAGIC); + + return F_TYPE_EQUAL(s->f_type, TMPFS_MAGIC) || + F_TYPE_EQUAL(s->f_type, RAMFS_MAGIC); } int rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev) { @@ -3469,7 +3469,7 @@ int wait_for_terminate_and_warn(const char *name, pid_t pid) { return -EPROTO; } -_noreturn_ void freeze(void) { +noreturn void freeze(void) { /* Make sure nobody waits for us on a socket anymore */ close_all_fds(NULL, 0); @@ -4580,7 +4580,7 @@ char *strjoin(const char *x, ...) { } bool is_main_thread(void) { - static __thread int cached = 0; + static thread_local int cached = 0; if (_unlikely_(cached == 0)) cached = getpid() == gettid() ? 1 : -1; @@ -4798,7 +4798,7 @@ static const char *const __signal_table[] = { DEFINE_PRIVATE_STRING_TABLE_LOOKUP(__signal, int); const char *signal_to_string(int signo) { - static __thread char buf[sizeof("RTMIN+")-1 + DECIMAL_STR_MAX(int) + 1]; + static thread_local char buf[sizeof("RTMIN+")-1 + DECIMAL_STR_MAX(int) + 1]; const char *name; name = __signal_to_string(signo); @@ -5154,7 +5154,7 @@ bool is_valid_documentation_url(const char *url) { } bool in_initrd(void) { - static __thread int saved = -1; + static int saved = -1; struct statfs s; if (saved >= 0) @@ -6029,18 +6029,24 @@ int container_get_leader(const char *machine, pid_t *pid) { return 0; } -int namespace_open(pid_t pid, int *namespace_fd, int *root_fd) { - _cleanup_close_ int nsfd = -1; - const char *ns, *root; +int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *root_fd) { + _cleanup_close_ int pidnsfd = -1, mntnsfd = -1; + const char *pidns, *mntns, *root; int rfd; assert(pid >= 0); - assert(namespace_fd); + assert(pidns_fd); + assert(mntns_fd); assert(root_fd); - ns = procfs_file_alloca(pid, "ns/mnt"); - nsfd = open(ns, O_RDONLY|O_NOCTTY|O_CLOEXEC); - if (nsfd < 0) + mntns = procfs_file_alloca(pid, "ns/mnt"); + mntnsfd = open(mntns, O_RDONLY|O_NOCTTY|O_CLOEXEC); + if (mntnsfd < 0) + return -errno; + + pidns = procfs_file_alloca(pid, "ns/pid"); + pidnsfd = open(pidns, O_RDONLY|O_NOCTTY|O_CLOEXEC); + if (pidnsfd < 0) return -errno; root = procfs_file_alloca(pid, "root"); @@ -6048,18 +6054,24 @@ int namespace_open(pid_t pid, int *namespace_fd, int *root_fd) { if (rfd < 0) return -errno; - *namespace_fd = nsfd; + *pidns_fd = pidnsfd; + *mntns_fd = mntnsfd; *root_fd = rfd; - nsfd = -1; + pidnsfd = -1; + mntnsfd = -1; return 0; } -int namespace_enter(int namespace_fd, int root_fd) { - assert(namespace_fd >= 0); +int namespace_enter(int pidns_fd, int mntns_fd, int root_fd) { + assert(pidns_fd >= 0); + assert(mntns_fd >= 0); assert(root_fd >= 0); - if (setns(namespace_fd, CLONE_NEWNS) < 0) + if (setns(pidns_fd, CLONE_NEWPID) < 0) + return -errno; + + if (setns(mntns_fd, CLONE_NEWNS) < 0) return -errno; if (fchdir(root_fd) < 0) @@ -6076,3 +6088,13 @@ int namespace_enter(int namespace_fd, int root_fd) { return 0; } + +bool pid_valid(pid_t pid) { + if (pid <= 0) + return false; + + if (kill(pid, 0) >= 0) + return true; + + return errno != ESRCH; +}