From: Mathieu Desnoyers Date: Mon, 8 Aug 2011 06:13:27 +0000 (-0400) Subject: Allow tracing group users to read trace files they generated X-Git-Tag: v2.0-pre8~2 X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=commitdiff_plain;h=996b65c843a7ef76769e4f6cf66055c7c4acf3d0 Allow tracing group users to read trace files they generated Signed-off-by: Mathieu Desnoyers --- diff --git a/ltt-sessiond/main.c b/ltt-sessiond/main.c index 1e2a0831d..d10483066 100644 --- a/ltt-sessiond/main.c +++ b/ltt-sessiond/main.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2011 - David Goulet + * Copyright (C) 2011 - Mathieu Desnoyers * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -114,6 +115,20 @@ static int modprobe_remove_kernel_modules(void); */ static struct ltt_session_list *session_list_ptr; +static gid_t allowed_group(void) +{ + struct group *grp; + + grp = (opt_tracing_group != NULL) ? + (grp = getgrnam(opt_tracing_group)) : + (grp = getgrnam(default_tracing_group)); + if (!grp) { + return -1; + } else { + return grp->gr_gid; + } +} + /* * Init quit pipe. * @@ -1045,7 +1060,7 @@ static int mount_debugfs(char *path) int ret; char *type = "debugfs"; - ret = mkdir_recursive(path, S_IRWXU | S_IRWXG); + ret = mkdir_recursive(path, S_IRWXU | S_IRWXG, geteuid(), getegid()); if (ret < 0) { goto error; } @@ -1226,9 +1241,10 @@ static int create_kernel_session(struct ltt_session *session) goto error; } - ret = mkdir_recursive(session->path, S_IRWXU | S_IRWXG ); + ret = mkdir_recursive(session->path, S_IRWXU | S_IRWXG, + geteuid(), allowed_group()); if (ret < 0) { - if (ret != EEXIST) { + if (ret != -EEXIST) { ERR("Trace directory creation error"); goto error; } @@ -2445,13 +2461,10 @@ static int set_permissions(void) { int ret; struct group *grp; + gid_t gid; - /* Decide which group name to use */ - (opt_tracing_group != NULL) ? - (grp = getgrnam(opt_tracing_group)) : - (grp = getgrnam(default_tracing_group)); - - if (grp == NULL) { + gid = allowed_group(); + if (gid < 0) { if (is_root) { WARN("No tracing group detected"); ret = 0; @@ -2463,21 +2476,21 @@ static int set_permissions(void) } /* Set lttng run dir */ - ret = chown(LTTNG_RUNDIR, 0, grp->gr_gid); + ret = chown(LTTNG_RUNDIR, 0, gid); if (ret < 0) { ERR("Unable to set group on " LTTNG_RUNDIR); perror("chown"); } /* lttng client socket path */ - ret = chown(client_unix_sock_path, 0, grp->gr_gid); + ret = chown(client_unix_sock_path, 0, gid); if (ret < 0) { ERR("Unable to set group on %s", client_unix_sock_path); perror("chown"); } /* kconsumerd error socket path */ - ret = chown(kconsumerd_err_unix_sock_path, 0, grp->gr_gid); + ret = chown(kconsumerd_err_unix_sock_path, 0, gid); if (ret < 0) { ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path); perror("chown"); diff --git a/ltt-sessiond/utils.c b/ltt-sessiond/utils.c index ff36ab187..5b8e31b8d 100644 --- a/ltt-sessiond/utils.c +++ b/ltt-sessiond/utils.c @@ -1,5 +1,6 @@ /* - * Copyright (C) 2011 - David Goulet + * Copyright (C) 2011 - David Goulet + * Copyright (C) 2011 - Mathieu Desnoyers * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -44,7 +45,7 @@ const char *get_home_dir(void) * * Create recursively directory using the FULL path. */ -int mkdir_recursive(const char *path, mode_t mode) +int mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid) { int ret; char *p, tmp[PATH_MAX]; @@ -70,7 +71,19 @@ int mkdir_recursive(const char *path, mode_t mode) if (ret < 0) { if (!(errno == EEXIST)) { perror("mkdir recursive"); - ret = errno; + ret = -errno; + goto umask_error; + } + } else if (ret == 0) { + /* + * We created the directory. Set its + * ownership to the user/group + * specified. + */ + ret = chown(tmp, uid, gid); + if (ret < 0) { + perror("chown in mkdir recursive"); + ret = -errno; goto umask_error; } } @@ -80,7 +93,18 @@ int mkdir_recursive(const char *path, mode_t mode) ret = mkdir(tmp, mode); if (ret < 0) { - ret = errno; + ret = -errno; + } else if (ret == 0) { + /* + * We created the directory. Set its ownership to the + * user/group specified. + */ + ret = chown(tmp, uid, gid); + if (ret < 0) { + perror("chown in mkdir recursive"); + ret = -errno; + goto umask_error; + } } umask_error: diff --git a/ltt-sessiond/utils.h b/ltt-sessiond/utils.h index d451eb63a..41aad2557 100644 --- a/ltt-sessiond/utils.h +++ b/ltt-sessiond/utils.h @@ -1,5 +1,9 @@ +#ifndef _LTT_UTILS_H +#define _LTT_UTILS_H + /* * Copyright (C) 2011 - David Goulet + * Copyright (C) 2011 - Mathieu Desnoyers * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -16,14 +20,13 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#ifndef _LTT_UTILS_H -#define _LTT_UTILS_H +#include #ifndef ARRAY_SIZE #define ARRAY_SIZE(array) (sizeof(array) / (sizeof((array)[0]))) #endif -int mkdir_recursive(const char *path, mode_t mode); +int mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid); const char *get_home_dir(void); #endif /* _LTT_UTILS_H */