[apparmor] [PATCH 4/5] Add the ability to read proc attr interfaces
John Johansen
john.johansen at canonical.com
Tue Jul 12 18:49:04 UTC 2011
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
More information about the AppArmor
mailing list