int ret;
char *type = "debugfs";
- ret = mkdir_recursive(path, S_IRWXU | S_IRWXG, geteuid(), getegid());
+ ret = mkdir_recursive_run_as(path, S_IRWXU | S_IRWXG, geteuid(), getegid());
if (ret < 0) {
PERROR("Cannot create debugfs path");
goto error;
goto error;
}
- ret = mkdir_recursive(lus->pathname, S_IRWXU | S_IRWXG,
+ ret = mkdir_recursive_run_as(lus->pathname, S_IRWXU | S_IRWXG,
session->uid, session->gid);
if (ret < 0) {
if (ret != -EEXIST) {
session->kernel_session->consumer_fd = kconsumer_data.cmd_sock;
}
- ret = mkdir_recursive(session->kernel_session->trace_path,
+ ret = mkdir_recursive_run_as(session->kernel_session->trace_path,
S_IRWXU | S_IRWXG, session->uid, session->gid);
if (ret < 0) {
if (ret != -EEXIST) {
#include "ust-app.h"
#include "ust-consumer.h"
#include "ust-ctl.h"
+#include "utils.h"
/*
* Delete ust context safely. RCU read lock must be held before calling
char *pathname, struct ust_app *app)
{
int ret = 0;
- mode_t old_umask;
if (ua_sess->metadata == NULL) {
/* Allocate UST metadata */
goto error;
}
- old_umask = umask(0);
- ret = mkdir(ua_sess->path, S_IRWXU | S_IRWXG);
+ ret = mkdir_run_as(ua_sess->path, S_IRWXU | S_IRWXG,
+ ua_sess->uid, ua_sess->gid);
if (ret < 0) {
PERROR("mkdir UST metadata");
goto error;
}
- ret = chown(ua_sess->path, ua_sess->uid, ua_sess->gid);
- if (ret < 0) {
- ERR("Unable to change owner of %s", ua_sess->path);
- perror("chown");
- }
- umask(old_umask);
ret = snprintf(ua_sess->metadata->pathname, PATH_MAX,
"%s/metadata", ua_sess->path);
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
+#include <fcntl.h>
#include <lttngerr.h>
#include "utils.h"
+struct mkdir_data {
+ const char *path;
+ mode_t mode;
+};
+
+struct open_data {
+ const char *path;
+ int flags;
+ mode_t mode;
+};
+
/*
* Write to writable pipe used to notify a thread.
*/
* Create recursively directory using the FULL path.
*/
static
-int _mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
+int _mkdir_recursive(void *_data)
{
- int ret;
+ struct mkdir_data *data = _data;
+ const char *path;
char *p, tmp[PATH_MAX];
+ struct stat statbuf;
+ mode_t mode;
size_t len;
- mode_t old_umask;
+ int ret;
+
+ path = data->path;
+ mode = data->mode;
ret = snprintf(tmp, sizeof(tmp), "%s", path);
if (ret < 0) {
tmp[len - 1] = 0;
}
- old_umask = umask(0);
for (p = tmp + 1; *p; p++) {
if (*p == '/') {
*p = 0;
- ret = mkdir(tmp, mode);
+ ret = stat(tmp, &statbuf);
if (ret < 0) {
- if (!(errno == EEXIST)) {
- PERROR("mkdir recursive");
- ret = -errno;
- goto umask_error;
+ ret = mkdir(tmp, mode);
+ if (ret < 0) {
+ if (!(errno == EEXIST)) {
+ PERROR("mkdir recursive");
+ ret = -errno;
+ goto error;
+ }
}
}
*p = '/';
}
}
-umask_error:
- umask(old_umask);
error:
return ret;
}
static
-int run_as(int (*cmd)(const char *path, mode_t mode, uid_t uid, gid_t gid),
- const char *path, mode_t mode, uid_t uid, gid_t gid)
+int _mkdir(void *_data)
+{
+ struct mkdir_data *data = _data;
+ return mkdir(data->path, data->mode);
+}
+
+static
+int _open(void *_data)
+{
+ struct open_data *data = _data;
+ return open(data->path, data->flags, data->mode);
+}
+
+static
+int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
{
int ret = 0;
pid_t pid;
uid, geteuid());
return -EPERM;
}
- return (*cmd)(path, mode, uid, gid);
+ return (*cmd)(data);
}
pid = fork();
perror("seteuid");
exit(EXIT_FAILURE);
}
- ret = (*cmd)(path, mode, uid, gid);
+ umask(0);
+ ret = (*cmd)(data);
if (!ret)
exit(EXIT_SUCCESS);
else
return ret;
}
-int mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
+int mkdir_recursive_run_as(const char *path, mode_t mode, uid_t uid, gid_t gid)
{
+ struct mkdir_data data;
+
DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
path, mode, uid, gid);
- return run_as(_mkdir_recursive, path, mode, uid, gid);
+ data.path = path;
+ data.mode = mode;
+ return run_as(_mkdir_recursive, &data, uid, gid);
+}
+
+int mkdir_run_as(const char *path, mode_t mode, uid_t uid, gid_t gid)
+{
+ struct mkdir_data data;
+
+ DBG3("mkdir() %s with mode %d for uid %d and gid %d",
+ path, mode, uid, gid);
+ data.path = path;
+ data.mode = mode;
+ return run_as(_mkdir, &data, uid, gid);
+}
+
+int open_run_as(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
+{
+ struct open_data data;
+
+ DBG3("open() %s with flags %d mode %d for uid %d and gid %d",
+ path, flags, mode, uid, gid);
+ data.path = path;
+ data.flags = flags;
+ data.mode = mode;
+ return run_as(_open, &data, uid, gid);
}
#define __stringify(x) __stringify1(x)
#endif
-int mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid);
+int mkdir_recursive_run_as(const char *path, mode_t mode, uid_t uid, gid_t gid);
+int mkdir_run_as(const char *path, mode_t mode, uid_t uid, gid_t gid);
+int open_run_as(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid);
+
const char *get_home_dir(void);
int notify_thread_pipe(int wpipe);