diff options
author | Jeffrey Vander Stoep <jeffv@google.com> | 2015-11-18 17:53:52 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2015-11-18 17:53:52 +0000 |
commit | 4caaec565181b1ee6d7ed025239a8834943c1316 (patch) | |
tree | 3b3f76250d50c24abf54b614d6ca25ce5f9aa320 | |
parent | ee9ec6decb70c12c4a02324c02aafe6e8b1a21ab (diff) | |
parent | 02df2e30820051a4b592071946521d5c9f7eb74b (diff) | |
download | libselinux-4caaec565181b1ee6d7ed025239a8834943c1316.tar.gz |
Merge "libselinux: use /proc/thread-self when available"
am: 02df2e3082
* commit '02df2e30820051a4b592071946521d5c9f7eb74b':
libselinux: use /proc/thread-self when available
-rw-r--r-- | src/procattr.c | 47 |
1 files changed, 26 insertions, 21 deletions
diff --git a/src/procattr.c b/src/procattr.c index f350808..8569f82 100644 --- a/src/procattr.c +++ b/src/procattr.c @@ -15,27 +15,44 @@ static pid_t gettid(void) } #endif -static int getprocattrcon(char ** context, - pid_t pid, const char *attr) +static int openattr(pid_t pid, const char *attr, int flags) { - char *path, *buf; - size_t size; int fd, rc; - ssize_t ret; + char *path; pid_t tid; - int errno_hold; if (pid > 0) rc = asprintf(&path, "/proc/%d/attr/%s", pid, attr); else { + rc = asprintf(&path, "/proc/thread-self/attr/%s", attr); + if (rc < 0) + return -1; + fd = open(path, flags | O_CLOEXEC); + if (fd >= 0 || errno != ENOENT) + goto out; + free(path); tid = gettid(); rc = asprintf(&path, "/proc/self/task/%d/attr/%s", tid, attr); } if (rc < 0) return -1; - fd = open(path, O_RDONLY); + fd = open(path, flags | O_CLOEXEC); +out: free(path); + return fd; +} + +static int getprocattrcon(char ** context, + pid_t pid, const char *attr) +{ + char *buf; + size_t size; + int fd; + ssize_t ret; + int errno_hold; + + fd = openattr(pid, attr, O_RDONLY); if (fd < 0) return -1; @@ -76,23 +93,11 @@ static int getprocattrcon(char ** context, static int setprocattrcon(const char * context, pid_t pid, const char *attr) { - char *path; - int fd, rc; - pid_t tid; + int fd; ssize_t ret; int errno_hold; - if (pid > 0) - rc = asprintf(&path, "/proc/%d/attr/%s", pid, attr); - else { - tid = gettid(); - rc = asprintf(&path, "/proc/self/task/%d/attr/%s", tid, attr); - } - if (rc < 0) - return -1; - - fd = open(path, O_RDWR); - free(path); + fd = openattr(pid, attr, O_RDWR); if (fd < 0) return -1; if (context) |