[apparmor] [PATCH 4/5] Add the ability to read proc attr interfaces
Seth Arnold
seth.arnold at gmail.com
Tue Jul 12 19:01:52 UTC 2011
The ret=1 just before the for loop isn't needed (except to quiet warnings?)
I think the entire for loop would be easier read as a while loop.
And no need to test if ctl is non-null, free() already does that.
-----Original Message-----
From: John Johansen <john.johansen at canonical.com>
Sender: apparmor-bounces at lists.ubuntu.com
Date: Tue, 12 Jul 2011 11:49:04
To: <apparmor at lists.ubuntu.com>
Subject: [apparmor] [PATCH 4/5] Add the ability to read proc attr interfaces
Signed-off-by: John Johansen <john.johansen at canonical.com>
---
libraries/libapparmor/src/kernel_interface.c | 56 ++++++++++++++++++++++++++
1 files changed, 56 insertions(+), 0 deletions(-)
diff --git a/libraries/libapparmor/src/kernel_interface.c b/libraries/libapparmor/src/kernel_interface.c
index 4e6c1e9..8e275f2 100644
--- a/libraries/libapparmor/src/kernel_interface.c
+++ b/libraries/libapparmor/src/kernel_interface.c
@@ -54,6 +54,62 @@ static char *procattr_path(pid_t pid, const char *attr)
return NULL;
}
+/**
+ * getprocattr - get the contents of @attr for @tid into @buf
+ * @tid: tid of task to query
+ * @attr: which /proc/<tid>/current/attr to query
+ * @buf: buffer to store the result in
+ * @len: size of the buffer
+ *
+ * Returns: size of data read or -1 on error, and sets errno
+ */
+static int getprocattr(pid_t tid, const char *attr, char *buf, int len)
+{
+ int rc = -1;
+ int fd, ret;
+ char *ctl = NULL;
+ int size = 0;
+
+ if (!buf) {
+ errno = EINVAL;
+ goto out;
+ }
+
+ ctl = procattr_path(tid, attr);
+ if (!ctl)
+ goto out;
+
+ fd = open(ctl, O_RDONLY);
+ if (fd == -1) {
+ goto out;
+ }
+
+ ret = 1;
+ for (ret = 1; ret > 0 && len > 0; buf += ret, len -= ret, size += ret) {
+ ret = read(fd, buf, len);
+ }
+
+ if (ret < 0) {
+ int saved;
+ if (ret != -1) {
+ errno = EPROTO;
+ }
+ saved = errno;
+ (void)close(fd);
+ errno = saved;
+ goto out;
+ }
+
+ (void)close(fd);
+
+ rc = size;
+out:
+ if (ctl) {
+ free(ctl);
+ }
+ return rc;
+}
+
static int setprocattr(pid_t tid, const char *attr, const char *buf, int len)
{
int rc = -1;
--
1.7.5.4
--
AppArmor mailing list
AppArmor at lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/apparmor
More information about the AppArmor
mailing list