[apparmor] [PATCH 4/5] Add the ability to read proc attr interfaces
John Johansen
john.johansen at canonical.com
Tue Jul 12 19:10:53 UTC 2011
On 07/12/2011 12:01 PM, Seth Arnold wrote:
> 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.
>
no it really is required, the condition is checked at the beginning of the
for loop after assignment.
I can rework as a while loop and see how that looks
> And no need to test if ctl is non-null, free() already does that.
true enough
> -----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;
More information about the AppArmor
mailing list