summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeffrey Vander Stoep <jeffv@google.com>2015-11-17 17:17:58 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2015-11-17 17:17:58 +0000
commit02df2e30820051a4b592071946521d5c9f7eb74b (patch)
tree4d4080da123164fd38e1d3fff3787991df14977e
parentc821cc2e190607db18e1e1cbae126de57383fabd (diff)
parentc6d24556c8b9f59ec0d716936656a6e2c0de2251 (diff)
downloadlibselinux-02df2e30820051a4b592071946521d5c9f7eb74b.tar.gz
Merge "libselinux: use /proc/thread-self when available"
-rw-r--r--src/procattr.c47
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)