Move runas to common/
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sat, 7 Jan 2012 21:19:51 +0000 (16:19 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sat, 7 Jan 2012 21:19:51 +0000 (16:19 -0500)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Makefile.am
common/runas.c [new file with mode: 0644]
configure.ac
librunas/Makefile.am [deleted file]
librunas/runas.c [deleted file]
lttng-consumerd/Makefile.am
lttng-sessiond/Makefile.am
tests/Makefile.am

index 39b9f2f94aadc8557955d45cce1713ebbdbc8b20..b744596b1905eecf307aef95254280b2945458d3 100644 (file)
@@ -3,7 +3,6 @@ ACLOCAL_AMFLAGS = -I config
 SUBDIRS = common \
                  liblttng-sessiond-comm \
                  libkernelctl \
-                 librunas \
                  liblttng-kconsumer \
                  liblttng-ustconsumer \
                  liblttng-consumer \
diff --git a/common/runas.c b/common/runas.c
new file mode 100644 (file)
index 0000000..f612ccc
--- /dev/null
@@ -0,0 +1,311 @@
+/*
+ * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
+ *                      Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; only version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place - Suite 330, Boston, MA  02111-1307, USA.
+ */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <sys/mman.h>
+
+#include <lttngerr.h>
+
+#define CHILD_STACK_SIZE       10485760
+
+struct run_as_data {
+       int (*cmd)(void *data);
+       void *data;
+       uid_t uid;
+       gid_t gid;
+       int retval_pipe;
+};
+
+struct mkdir_data {
+       const char *path;
+       mode_t mode;
+};
+
+struct open_data {
+       const char *path;
+       int flags;
+       mode_t mode;
+};
+
+/*
+ * Create recursively directory using the FULL path.
+ */
+static
+int _mkdir_recursive(void *_data)
+{
+       struct mkdir_data *data = _data;
+       const char *path;
+       char *p, tmp[PATH_MAX];
+       struct stat statbuf;
+       mode_t mode;
+       size_t len;
+       int ret;
+
+       path = data->path;
+       mode = data->mode;
+
+       ret = snprintf(tmp, sizeof(tmp), "%s", path);
+       if (ret < 0) {
+               PERROR("snprintf mkdir");
+               goto error;
+       }
+
+       len = ret;
+       if (tmp[len - 1] == '/') {
+               tmp[len - 1] = 0;
+       }
+
+       for (p = tmp + 1; *p; p++) {
+               if (*p == '/') {
+                       *p = 0;
+                       ret = stat(tmp, &statbuf);
+                       if (ret < 0) {
+                               ret = mkdir(tmp, mode);
+                               if (ret < 0) {
+                                       if (!(errno == EEXIST)) {
+                                               PERROR("mkdir recursive");
+                                               ret = -errno;
+                                               goto error;
+                                       }
+                               }
+                       }
+                       *p = '/';
+               }
+       }
+
+       ret = mkdir(tmp, mode);
+       if (ret < 0) {
+               if (!(errno == EEXIST)) {
+                       PERROR("mkdir recursive last piece");
+                       ret = -errno;
+               } else {
+                       ret = 0;
+               }
+       }
+
+error:
+       return ret;
+}
+
+static
+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 child_run_as(void *_data)
+{
+       struct run_as_data *data = _data;
+       size_t writelen, writeleft, index;
+       union {
+               int i;
+               char c[sizeof(int)];
+       } sendret;
+       int ret;
+
+       /*
+        * Child: it is safe to drop egid and euid while sharing the
+        * file descriptors with the parent process, since we do not
+        * drop "uid": therefore, the user we are dropping egid/euid to
+        * cannot attach to this process with, e.g. ptrace, nor map this
+        * process memory.
+        */
+       if (data->gid != getegid()) {
+               ret = setegid(data->gid);
+               if (ret < 0) {
+                       perror("setegid");
+                       return EXIT_FAILURE;
+               }
+       }
+       if (data->uid != geteuid()) {
+               ret = seteuid(data->uid);
+               if (ret < 0) {
+                       perror("seteuid");
+                       return EXIT_FAILURE;
+               }
+       }
+       /*
+        * Also set umask to 0 for mkdir executable bit.
+        */
+       umask(0);
+       sendret.i = (*data->cmd)(data->data);
+       /* send back return value */
+       writeleft = sizeof(sendret);
+       index = 0;
+       do {
+               writelen = write(data->retval_pipe, &sendret.c[index],
+                               writeleft);
+               if (writelen < 0) {
+                       perror("write");
+                       return EXIT_FAILURE;
+               }
+               writeleft -= writelen;
+               index += writelen;
+       } while (writeleft > 0);
+       return EXIT_SUCCESS;
+}
+
+static
+int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
+{
+       struct run_as_data run_as_data;
+       int ret = 0;
+       int status;
+       pid_t pid;
+       int retval_pipe[2];
+       ssize_t readlen, readleft, index;
+       void *child_stack;
+       union {
+               int i;
+               char c[sizeof(int)];
+       } retval;
+
+       /*
+        * If we are non-root, we can only deal with our own uid.
+        */
+       if (geteuid() != 0) {
+               if (uid != geteuid()) {
+                       ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
+                               uid, geteuid());
+                       return -EPERM;
+               }
+       }
+
+       ret = pipe(retval_pipe);
+       if (ret < 0) {
+               perror("pipe");
+               goto end;
+       }
+       run_as_data.data = data;
+       run_as_data.cmd = cmd;
+       run_as_data.uid = uid;
+       run_as_data.gid = gid;
+       run_as_data.retval_pipe = retval_pipe[1];       /* write end */
+       child_stack = mmap(NULL, CHILD_STACK_SIZE,
+               PROT_WRITE | PROT_READ,
+               MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | MAP_STACK,
+               -1, 0);
+       if (child_stack == MAP_FAILED) {
+               perror("mmap");
+               ret = -ENOMEM;
+               goto close_pipe;
+       }
+       /*
+        * Pointing to the middle of the stack to support architectures
+        * where the stack grows up (HPPA).
+        */
+       pid = clone(child_run_as, child_stack + (CHILD_STACK_SIZE / 2),
+               CLONE_FILES | SIGCHLD | CLONE_VM,
+               &run_as_data, NULL);
+       if (pid < 0) {
+               perror("clone");
+               ret = pid;
+               goto unmap_stack;
+       }
+       /* receive return value */
+       readleft = sizeof(retval);
+       index = 0;
+       do {
+               readlen = read(retval_pipe[0], &retval.c[index], readleft);
+               if (readlen < 0) {
+                       perror("read");
+                       ret = -1;
+                       break;
+               }
+               readleft -= readlen;
+               index += readlen;
+       } while (readleft > 0);
+
+       /*
+        * Parent: wait for child to return, in which case the
+        * shared memory map will have been created.
+        */
+       pid = waitpid(pid, &status, 0);
+       if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
+               perror("wait");
+               ret = -1;
+       }
+unmap_stack:
+       ret = munmap(child_stack, CHILD_STACK_SIZE);
+       if (ret < 0) {
+               perror("munmap");
+       }
+close_pipe:
+       close(retval_pipe[0]);
+       close(retval_pipe[1]);
+end:
+       return retval.i;
+}
+
+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);
+       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);
+}
+
+/*
+ * Note: open_run_as is currently not working. We'd need to pass the fd
+ * opened in the child to the parent.
+ */
+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 %X 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);
+}
index 9b8d9d34f68a5590c66eab06912adbeb763fecd0..a55dfd55da66f728b1cbd1cb4a9c58fff1afdb1b 100644 (file)
@@ -139,7 +139,6 @@ AC_CONFIG_FILES([
        liblttng-ustconsumer/Makefile
        liblttngctl/Makefile
        liblttng-sessiond-comm/Makefile
-       librunas/Makefile
        lttng-consumerd/Makefile
        lttng-sessiond/Makefile
        lttng/Makefile
diff --git a/librunas/Makefile.am b/librunas/Makefile.am
deleted file mode 100644 (file)
index 92ec272..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-AM_CPPFLAGS = -I$(top_srcdir)/include
-
-noinst_LTLIBRARIES = librunas.la
-
-librunas_la_SOURCES = runas.c
diff --git a/librunas/runas.c b/librunas/runas.c
deleted file mode 100644 (file)
index f612ccc..0000000
+++ /dev/null
@@ -1,311 +0,0 @@
-/*
- * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
- *                      Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; only version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place - Suite 330, Boston, MA  02111-1307, USA.
- */
-
-#define _GNU_SOURCE
-#include <errno.h>
-#include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/wait.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sched.h>
-#include <sys/mman.h>
-
-#include <lttngerr.h>
-
-#define CHILD_STACK_SIZE       10485760
-
-struct run_as_data {
-       int (*cmd)(void *data);
-       void *data;
-       uid_t uid;
-       gid_t gid;
-       int retval_pipe;
-};
-
-struct mkdir_data {
-       const char *path;
-       mode_t mode;
-};
-
-struct open_data {
-       const char *path;
-       int flags;
-       mode_t mode;
-};
-
-/*
- * Create recursively directory using the FULL path.
- */
-static
-int _mkdir_recursive(void *_data)
-{
-       struct mkdir_data *data = _data;
-       const char *path;
-       char *p, tmp[PATH_MAX];
-       struct stat statbuf;
-       mode_t mode;
-       size_t len;
-       int ret;
-
-       path = data->path;
-       mode = data->mode;
-
-       ret = snprintf(tmp, sizeof(tmp), "%s", path);
-       if (ret < 0) {
-               PERROR("snprintf mkdir");
-               goto error;
-       }
-
-       len = ret;
-       if (tmp[len - 1] == '/') {
-               tmp[len - 1] = 0;
-       }
-
-       for (p = tmp + 1; *p; p++) {
-               if (*p == '/') {
-                       *p = 0;
-                       ret = stat(tmp, &statbuf);
-                       if (ret < 0) {
-                               ret = mkdir(tmp, mode);
-                               if (ret < 0) {
-                                       if (!(errno == EEXIST)) {
-                                               PERROR("mkdir recursive");
-                                               ret = -errno;
-                                               goto error;
-                                       }
-                               }
-                       }
-                       *p = '/';
-               }
-       }
-
-       ret = mkdir(tmp, mode);
-       if (ret < 0) {
-               if (!(errno == EEXIST)) {
-                       PERROR("mkdir recursive last piece");
-                       ret = -errno;
-               } else {
-                       ret = 0;
-               }
-       }
-
-error:
-       return ret;
-}
-
-static
-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 child_run_as(void *_data)
-{
-       struct run_as_data *data = _data;
-       size_t writelen, writeleft, index;
-       union {
-               int i;
-               char c[sizeof(int)];
-       } sendret;
-       int ret;
-
-       /*
-        * Child: it is safe to drop egid and euid while sharing the
-        * file descriptors with the parent process, since we do not
-        * drop "uid": therefore, the user we are dropping egid/euid to
-        * cannot attach to this process with, e.g. ptrace, nor map this
-        * process memory.
-        */
-       if (data->gid != getegid()) {
-               ret = setegid(data->gid);
-               if (ret < 0) {
-                       perror("setegid");
-                       return EXIT_FAILURE;
-               }
-       }
-       if (data->uid != geteuid()) {
-               ret = seteuid(data->uid);
-               if (ret < 0) {
-                       perror("seteuid");
-                       return EXIT_FAILURE;
-               }
-       }
-       /*
-        * Also set umask to 0 for mkdir executable bit.
-        */
-       umask(0);
-       sendret.i = (*data->cmd)(data->data);
-       /* send back return value */
-       writeleft = sizeof(sendret);
-       index = 0;
-       do {
-               writelen = write(data->retval_pipe, &sendret.c[index],
-                               writeleft);
-               if (writelen < 0) {
-                       perror("write");
-                       return EXIT_FAILURE;
-               }
-               writeleft -= writelen;
-               index += writelen;
-       } while (writeleft > 0);
-       return EXIT_SUCCESS;
-}
-
-static
-int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
-{
-       struct run_as_data run_as_data;
-       int ret = 0;
-       int status;
-       pid_t pid;
-       int retval_pipe[2];
-       ssize_t readlen, readleft, index;
-       void *child_stack;
-       union {
-               int i;
-               char c[sizeof(int)];
-       } retval;
-
-       /*
-        * If we are non-root, we can only deal with our own uid.
-        */
-       if (geteuid() != 0) {
-               if (uid != geteuid()) {
-                       ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
-                               uid, geteuid());
-                       return -EPERM;
-               }
-       }
-
-       ret = pipe(retval_pipe);
-       if (ret < 0) {
-               perror("pipe");
-               goto end;
-       }
-       run_as_data.data = data;
-       run_as_data.cmd = cmd;
-       run_as_data.uid = uid;
-       run_as_data.gid = gid;
-       run_as_data.retval_pipe = retval_pipe[1];       /* write end */
-       child_stack = mmap(NULL, CHILD_STACK_SIZE,
-               PROT_WRITE | PROT_READ,
-               MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | MAP_STACK,
-               -1, 0);
-       if (child_stack == MAP_FAILED) {
-               perror("mmap");
-               ret = -ENOMEM;
-               goto close_pipe;
-       }
-       /*
-        * Pointing to the middle of the stack to support architectures
-        * where the stack grows up (HPPA).
-        */
-       pid = clone(child_run_as, child_stack + (CHILD_STACK_SIZE / 2),
-               CLONE_FILES | SIGCHLD | CLONE_VM,
-               &run_as_data, NULL);
-       if (pid < 0) {
-               perror("clone");
-               ret = pid;
-               goto unmap_stack;
-       }
-       /* receive return value */
-       readleft = sizeof(retval);
-       index = 0;
-       do {
-               readlen = read(retval_pipe[0], &retval.c[index], readleft);
-               if (readlen < 0) {
-                       perror("read");
-                       ret = -1;
-                       break;
-               }
-               readleft -= readlen;
-               index += readlen;
-       } while (readleft > 0);
-
-       /*
-        * Parent: wait for child to return, in which case the
-        * shared memory map will have been created.
-        */
-       pid = waitpid(pid, &status, 0);
-       if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
-               perror("wait");
-               ret = -1;
-       }
-unmap_stack:
-       ret = munmap(child_stack, CHILD_STACK_SIZE);
-       if (ret < 0) {
-               perror("munmap");
-       }
-close_pipe:
-       close(retval_pipe[0]);
-       close(retval_pipe[1]);
-end:
-       return retval.i;
-}
-
-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);
-       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);
-}
-
-/*
- * Note: open_run_as is currently not working. We'd need to pass the fd
- * opened in the child to the parent.
- */
-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 %X 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);
-}
index a808e3ca8631b76b28aad03c8e44288ec08b5b26..9e8d036da6c9d74191f1c400ea3ad741c19716f9 100644 (file)
@@ -8,7 +8,7 @@ lttng_consumerd_LDADD = \
           $(top_builddir)/libkernelctl/libkernelctl.la \
           $(top_builddir)/liblttng-consumer/liblttng-consumer.la \
           $(top_builddir)/liblttng-sessiond-comm/liblttng-sessiond-comm.la \
-          $(top_builddir)/librunas/librunas.la
+          $(top_builddir)/common/libcommon.la
 
 if HAVE_LIBLTTNG_UST_CTL
 lttng_consumerd_LDADD += -llttng-ust-ctl
index 00a87f4c3cefd03d4e831035beeb8cdb6e4e7b6c..b853e260b16a7167f83f73a72c4fe915fabb5d90 100644 (file)
@@ -37,7 +37,6 @@ lttng_sessiond_LDADD = -lrt -lurcu-common -lurcu \
                 $(top_builddir)/liblttng-sessiond-comm/liblttng-sessiond-comm.la \
                 $(top_builddir)/libkernelctl/libkernelctl.la \
                 $(top_builddir)/liblttngctl/liblttngctl.la \
-                $(top_builddir)/librunas/librunas.la \
                 $(top_builddir)/common/libcommon.la
 
 if HAVE_LIBLTTNG_UST_CTL
index 21bcace758798c1cb51d06be1d34bdc902bf595e..d775644c56913aadbdb573a043fbe0d3c717c8d9 100644 (file)
@@ -13,7 +13,7 @@ LIBLTTNG=$(top_srcdir)/liblttngctl/lttngctl.c \
                 $(top_srcdir)/liblttng-sessiond-comm/lttng-sessiond-comm.c
 
 test_sessions_SOURCES = test_sessions.c $(UTILS) $(SESSIONS)
-test_sessions_LDADD = $(top_builddir)/common/libcommon.la $(top_builddir)/librunas/librunas.la
+test_sessions_LDADD = $(top_builddir)/common/libcommon.la
 
 test_kernel_data_trace_SOURCES = test_kernel_data_trace.c $(UTILS) $(KERN_DATA_TRACE)
 
This page took 0.048993 seconds and 4 git commands to generate.