From: David Goulet Date: Thu, 1 Mar 2012 18:04:18 +0000 (-0500) Subject: Merge branch 'master' into compat-freebsd X-Git-Tag: v2.0.0-rc3~20 X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=commitdiff_plain;h=1af0e2e4d4a770e32f4fc169122b0c338bd799f6;hp=ab08a675ad07e246b49e7a00927dfd13c1e58ce4 Merge branch 'master' into compat-freebsd --- diff --git a/configure.ac b/configure.ac index c07052a0f..f8af52c1e 100644 --- a/configure.ac +++ b/configure.ac @@ -116,6 +116,24 @@ AM_CONDITIONAL([HAVE_LIBLTTNG_UST_CTL], [ test "x$ac_cv_lib_lttng_ust_ctl_ustctl AC_CHECK_FUNCS([sched_getcpu sysconf]) +# check for dlopen +AC_CHECK_LIB([dl], [dlopen], +[ + have_libdl=yes +], +[ + #libdl not found, check for dlopen in libc. + AC_CHECK_LIB([c], [dlopen], + [ + have_libc_dl=yes + ], + [ + AC_MSG_ERROR([Cannot find dlopen in libdl nor libc. Use [LDFLAGS]=-Ldir to specify their location.]) + ]) +]) +AM_CONDITIONAL([LTTNG_TOOLS_BUILD_WITH_LIBDL], [test "x$have_libdl" = "xyes"]) +AM_CONDITIONAL([LTTNG_TOOLS_BUILD_WITH_LIBC_DL], [test "x$have_libc_dl" = "xyes"]) + # Option to only build the consumer daemon and its libraries AC_ARG_WITH([consumerd-only], AS_HELP_STRING([--with-consumerd-only],[Only build the consumer daemon [default=no]]), diff --git a/doc/man/lttng-sessiond.8 b/doc/man/lttng-sessiond.8 index 8a235f941..50a03b029 100644 --- a/doc/man/lttng-sessiond.8 +++ b/doc/man/lttng-sessiond.8 @@ -129,6 +129,9 @@ Allow to specifiy the 64-bit library path containing libconsumer.so. .IP "LTTNG_CONSUMERD64_LIBDIR" Allow to specifiy the 32-bit library path containing libconsumer.so. \fB--consumerd64-libdir\fP override this variable. +.IP "LTTNG_DEBUG_NOCLONE" +Debug-mode disabling use of clone/fork. Insecure, but required to allow +debuggers to work with sessiond on some operating systems. .SH "SEE ALSO" .PP diff --git a/src/bin/lttng-sessiond/lttng-sessiond.h b/src/bin/lttng-sessiond/lttng-sessiond.h index 2ba2550c7..79e7fcb43 100644 --- a/src/bin/lttng-sessiond/lttng-sessiond.h +++ b/src/bin/lttng-sessiond/lttng-sessiond.h @@ -24,6 +24,7 @@ #include #include +#include #include "session.h" #include "ust-app.h" @@ -42,10 +43,10 @@ extern const char default_home_dir[], struct command_ctx { int ust_sock; unsigned int lttng_msg_size; - struct ucred creds; struct ltt_session *session; struct lttcomm_lttng_msg *llm; struct lttcomm_session_msg *lsm; + lttng_sock_cred creds; }; struct ust_command { diff --git a/src/bin/lttng-sessiond/main.c b/src/bin/lttng-sessiond/main.c index 3cb970c06..8654031f1 100644 --- a/src/bin/lttng-sessiond/main.c +++ b/src/bin/lttng-sessiond/main.c @@ -17,7 +17,6 @@ */ #define _GNU_SOURCE -#include #include #include #include @@ -40,6 +39,7 @@ #include #include +#include #include #include #include @@ -295,14 +295,22 @@ static gid_t allowed_group(void) */ static int init_thread_quit_pipe(void) { - int ret; + int ret, i; - ret = pipe2(thread_quit_pipe, O_CLOEXEC); + ret = pipe(thread_quit_pipe); if (ret < 0) { - perror("thread quit pipe"); + PERROR("thread quit pipe"); goto error; } + for (i = 0; i < 2; i++) { + ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC); + if (ret < 0) { + PERROR("fcntl"); + goto error; + } + } + error: return ret; } @@ -2909,11 +2917,12 @@ error: /* * Command LTTNG_CREATE_SESSION processed by the client thread. */ -static int cmd_create_session(char *name, char *path, struct ucred *creds) +static int cmd_create_session(char *name, char *path, lttng_sock_cred *creds) { int ret; - ret = session_create(name, path, creds->uid, creds->gid); + ret = session_create(name, path, LTTNG_SOCK_GET_UID_CRED(creds), + LTTNG_SOCK_GET_GID_CRED(creds)); if (ret != LTTCOMM_OK) { goto error; } @@ -3339,7 +3348,8 @@ skip_domain: */ if (need_tracing_session) { if (!session_access_ok(cmd_ctx->session, - cmd_ctx->creds.uid, cmd_ctx->creds.gid)) { + LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), + LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) { ret = LTTCOMM_EPERM; goto error; } @@ -3532,7 +3542,9 @@ skip_domain: unsigned int nr_sessions; session_lock_list(); - nr_sessions = lttng_sessions_count(cmd_ctx->creds.uid, cmd_ctx->creds.gid); + nr_sessions = lttng_sessions_count( + LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), + LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions); if (ret < 0) { @@ -3542,7 +3554,8 @@ skip_domain: /* Filled the session array */ list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload), - cmd_ctx->creds.uid, cmd_ctx->creds.gid); + LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds), + LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)); session_unlock_list(); @@ -4043,7 +4056,24 @@ end: */ static int create_kernel_poll_pipe(void) { - return pipe2(kernel_poll_pipe, O_CLOEXEC); + int ret, i; + + ret = pipe(kernel_poll_pipe); + if (ret < 0) { + PERROR("kernel poll pipe"); + goto error; + } + + for (i = 0; i < 2; i++) { + ret = fcntl(kernel_poll_pipe[i], F_SETFD, FD_CLOEXEC); + if (ret < 0) { + PERROR("fcntl kernel_poll_pipe"); + goto error; + } + } + +error: + return ret; } /* @@ -4051,7 +4081,24 @@ static int create_kernel_poll_pipe(void) */ static int create_apps_cmd_pipe(void) { - return pipe2(apps_cmd_pipe, O_CLOEXEC); + int ret, i; + + ret = pipe(apps_cmd_pipe); + if (ret < 0) { + PERROR("apps cmd pipe"); + goto error; + } + + for (i = 0; i < 2; i++) { + ret = fcntl(apps_cmd_pipe[i], F_SETFD, FD_CLOEXEC); + if (ret < 0) { + PERROR("fcntl apps_cmd_pipe"); + goto error; + } + } + +error: + return ret; } /* diff --git a/src/bin/lttng-sessiond/shm.c b/src/bin/lttng-sessiond/shm.c index 77c09995c..55d00e67f 100644 --- a/src/bin/lttng-sessiond/shm.c +++ b/src/bin/lttng-sessiond/shm.c @@ -104,11 +104,15 @@ static int get_wait_shm(char *shm_path, size_t mmap_size, int global) exit(EXIT_FAILURE); } +#ifndef __FreeBSD__ ret = fchmod(wait_shm_fd, mode); if (ret < 0) { perror("fchmod"); exit(EXIT_FAILURE); } +#else +#warning "FreeBSD does not support setting file mode on shm FD. Remember that for secure use, lttng-sessiond should be started before applications linked on lttng-ust." +#endif DBG("Got the wait shm fd %d", wait_shm_fd); diff --git a/src/bin/lttng/lttng.c b/src/bin/lttng/lttng.c index 1349c20a7..16582b036 100644 --- a/src/bin/lttng/lttng.c +++ b/src/bin/lttng/lttng.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include diff --git a/src/common/Makefile.am b/src/common/Makefile.am index 37f9acb9a..ba93810a8 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -10,6 +10,17 @@ noinst_LTLIBRARIES = libcommon.la libcommon_la_SOURCES = runas.c runas.h common.h +if COMPAT_EPOLL +COMPAT=compat/compat-epoll.c +else +COMPAT=compat/compat-poll.c +endif + +noinst_LTLIBRARIES += libcompat.la + +libcompat_la_SOURCES = compat/poll.h compat/fcntl.h compat/splice.h compat/endian.h \ + compat/socket.h compat/compat-fcntl.c $(COMPAT) + # Consumer library noinst_LTLIBRARIES += libconsumer.la @@ -18,19 +29,10 @@ libconsumer_la_SOURCES = consumer.c consumer.h libconsumer_la_LIBADD = \ $(top_builddir)/src/common/sessiond-comm/libsessiond-comm.la \ $(top_builddir)/src/common/kernel-consumer/libkernel-consumer.la \ - $(top_builddir)/src/common/hashtable/libhashtable.la + $(top_builddir)/src/common/hashtable/libhashtable.la \ + $(top_builddir)/src/common/libcompat.la if HAVE_LIBLTTNG_UST_CTL libconsumer_la_LIBADD += \ $(top_builddir)/src/common/ust-consumer/libust-consumer.la endif - -if COMPAT_EPOLL -COMPAT=compat/compat-epoll.c -else -COMPAT=compat/compat-poll.c -endif - -noinst_LTLIBRARIES += libcompat.la - -libcompat_la_SOURCES = compat/poll.h $(COMPAT) diff --git a/src/common/compat/clone.h b/src/common/compat/clone.h new file mode 100644 index 000000000..c3605080e --- /dev/null +++ b/src/common/compat/clone.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2011 - David Goulet + * + * 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. + */ + +#ifndef _COMPAT_CLONE_H +#define _COMPAT_CLONE_H + +#ifdef __linux__ + +#include + +static inline +pid_t lttng_clone_files(int (*fn)(void *), void *child_stack, void *arg) +{ + return clone(fn, child_stack, CLONE_FILES | SIGCHLD, arg); +} + +#elif defined(__FreeBSD__) + +#include + +static inline +pid_t lttng_clone_files(int (*fn)(void *), void *child_stack, void *arg) +{ + pid_t pid; + + pid = rfork(RFPROC | RFTHREAD); + if (pid == 0) { + /* child */ + int ret; + + ret = fn(arg); + exit(ret); + } else if (pid > 0) { + /* parent */ + /* + * Just return, the caller will wait for the child. + */ + return pid; + } else { + /* Error */ + return pid; + } +} + +#else +#error "Please add support for your OS." +#endif /* __linux__ , __FreeBSD__ */ + +#endif /* _COMPAT_CLONE_H */ diff --git a/src/common/compat/compat-fcntl.c b/src/common/compat/compat-fcntl.c new file mode 100644 index 000000000..587df6782 --- /dev/null +++ b/src/common/compat/compat-fcntl.c @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2011 - David Goulet + * + * 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 + +#ifdef __linux__ + +int compat_sync_file_range(int fd, off64_t offset, off64_t nbytes, + unsigned int flags) +{ + return sync_file_range(fd, offset, nbytes, flags); +} + +#endif /* __linux__ */ diff --git a/src/common/compat/endian.h b/src/common/compat/endian.h new file mode 100644 index 000000000..b4426eb84 --- /dev/null +++ b/src/common/compat/endian.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2011 - David Goulet + * + * 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. + */ + +#ifdef _COMPAT_ENDIAN_H +#define _COMPAT_ENDIAN_H + +#ifdef __linux__ +#include +#elif defined(__FreeBSD__) +#include +#else +#error "Please add support for your OS." +#endif + +#endif /* _COMPAT_ENDIAN_H */ diff --git a/src/common/compat/fcntl.h b/src/common/compat/fcntl.h new file mode 100644 index 000000000..f1c07a4f2 --- /dev/null +++ b/src/common/compat/fcntl.h @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2011 - David Goulet + * + * 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. + */ + +#ifndef _COMPAT_FCNTL_H +#define _COMPAT_FCNTL_H + +#include +#include + +#ifdef __linux__ + +extern int compat_sync_file_range(int fd, off64_t offset, off64_t nbytes, + unsigned int flags); +#define lttng_sync_file_range(fd, offset, nbytes, flags) \ + compat_sync_file_range(fd, offset, nbytes, flags) + +#elif defined(__FreeBSD__) + +typedef long int off64_t; +typedef off64_t loff_t; + +#include + +/* + * Possible flags under Linux. Simply nullify them and avoid wrapper. + */ +#define SYNC_FILE_RANGE_WAIT_AFTER 0 +#define SYNC_FILE_RANGE_WAIT_BEFORE 0 +#define SYNC_FILE_RANGE_WRITE 0 + +/* + * Possible flags under Linux. Simply nullify them and avoid wrappers. + */ +#define SPLICE_F_MOVE 0 +#define SPLICE_F_NONBLOCK 0 +#define SPLICE_F_MORE 0 +#define SPLICE_F_GIFT 0 + +#define POSIX_FADV_DONTNEED 0 + +static inline int lttng_sync_file_range(int fd, off64_t offset, + off64_t nbytes, unsigned int flags) +{ + return -ENOSYS; +} + +static inline ssize_t splice(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, + size_t len, unsigned int flags) +{ + return -ENOSYS; +} + +static inline int posix_fadvise(int fd, off_t offset, off_t len, int advice) +{ + return -ENOSYS; +} + +#else +#error "Please add support for your OS." +#endif /* __linux__ , __FreeBSD__ */ + +#endif /* _COMPAT_FCNTL_H */ diff --git a/src/common/compat/mman.h b/src/common/compat/mman.h new file mode 100644 index 000000000..7002e167c --- /dev/null +++ b/src/common/compat/mman.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2011 - David Goulet + * + * 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. + */ + +#ifndef _COMPAT_MMAN_H +#define _COMPAT_MMAN_H + +#include + +#ifdef __linux__ + +#elif defined(__FreeBSD__) + +#define MAP_GROWSDOWN 0 +#define MAP_ANONYMOUS MAP_ANON + +#else +#error "Please add support for your OS." +#endif /* __linux__ , __FreeBSD__ */ + +#endif /* _COMPAT_MMAN_H */ diff --git a/src/common/compat/poll.h b/src/common/compat/poll.h index 1580a4fb2..bb046c342 100644 --- a/src/common/compat/poll.h +++ b/src/common/compat/poll.h @@ -179,10 +179,17 @@ enum { LPOLLRDBAND = POLLRDBAND, LPOLLWRNORM = POLLWRNORM, LPOLLWRBAND = POLLWRBAND, +#if __linux__ LPOLLMSG = POLLMSG, + LPOLLRDHUP = POLLRDHUP, +#elif defined(__FreeBSD__) + LPOLLMSG = 0, + LPOLLRDHUP = 0, +#else +#error "Please add support for your OS." +#endif /* __linux__ */ LPOLLERR = POLLERR, LPOLLHUP = POLLHUP | POLLNVAL, - LPOLLRDHUP = POLLRDHUP, /* Close on exec feature does not exist for poll(2) */ LTTNG_CLOEXEC = 0xdead, }; diff --git a/src/common/compat/socket.h b/src/common/compat/socket.h new file mode 100644 index 000000000..7356324f7 --- /dev/null +++ b/src/common/compat/socket.h @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2011 - David Goulet + * + * 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. + */ + +#ifndef _COMPAT_SOCKET_H +#define _COMPAT_SOCKET_H + +#include +#include + +#include + +#ifdef __linux__ + +#define LTTNG_SOCK_CREDS SCM_CREDENTIALS + +typedef struct ucred lttng_sock_cred; + +#define LTTNG_SOCK_SET_UID_CRED(c, u) LTTNG_REF(c)->uid = u +#define LTTNG_SOCK_SET_GID_CRED(c, g) LTTNG_REF(c)->gid = g +#define LTTNG_SOCK_SET_PID_CRED(c, p) LTTNG_REF(c)->pid = p + +#define LTTNG_SOCK_GET_UID_CRED(c) LTTNG_REF(c)->uid +#define LTTNG_SOCK_GET_GID_CRED(c) LTTNG_REF(c)->gid +#define LTTNG_SOCK_GET_PID_CRED(c) LTTNG_REF(c)->pid + +#elif defined(__FreeBSD__) + +struct lttng_sock_cred { + uid_t uid; + gid_t gid; +}; + +typedef struct lttng_sock_cred lttng_sock_cred; + +#define LTTNG_SOCK_GET_UID_CRED(c) LTTNG_REF(c)->uid +#define LTTNG_SOCK_GET_GID_CRED(c) LTTNG_REF(c)->gid +#define LTTNG_SOCK_GET_PID_CRED(c) -1 + +#else +#error "Please add support for your OS." +#endif /* __linux__ , __FreeBSD__ */ + +#endif /* _COMPAT_SOCKET_H */ diff --git a/src/common/consumer.c b/src/common/consumer.c index 026d9f0f8..ae59b6b60 100644 --- a/src/common/consumer.c +++ b/src/common/consumer.c @@ -19,7 +19,6 @@ #define _GNU_SOURCE #include -#include #include #include #include @@ -608,7 +607,7 @@ void lttng_consumer_sync_trace_file( if (orig_offset < stream->chan->max_sb_size) { return; } - sync_file_range(outfd, orig_offset - stream->chan->max_sb_size, + lttng_sync_file_range(outfd, orig_offset - stream->chan->max_sb_size, stream->chan->max_sb_size, SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE @@ -749,6 +748,8 @@ ssize_t lttng_consumer_on_read_subbuffer_mmap( ERR("Unknown consumer_data type"); assert(0); } + + return 0; } /* diff --git a/src/common/consumer.h b/src/common/consumer.h index 83b86a39e..71ae39903 100644 --- a/src/common/consumer.h +++ b/src/common/consumer.h @@ -26,7 +26,8 @@ #include -#include "src/common/hashtable/hashtable.h" +#include +#include /* * When the receiving thread dies, we need to have a way to make the polling diff --git a/src/common/error.h b/src/common/error.h index 81c20223d..14b5e7c67 100644 --- a/src/common/error.h +++ b/src/common/error.h @@ -77,12 +77,27 @@ extern int opt_verbose; #define _PERROR(fmt, args...) \ __lttng_print(PRINT_ERR, "perror " fmt "\n", ## args) +#if !defined(__linux__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)) +/* + * Version using XSI strerror_r. + */ +#define PERROR(call, args...) \ + do { \ + char buf[200]; \ + strerror_r(errno, buf, sizeof(buf)); \ + _PERROR(call ": %s", ## args, buf); \ + } while(0); +#else +/* + * Version using GNU strerror_r, for linux with appropriate defines. + */ #define PERROR(call, args...) \ - do { \ + do { \ char *buf; \ char tmp[200]; \ buf = strerror_r(errno, tmp, sizeof(tmp)); \ _PERROR(call ": %s", ## args, buf); \ } while(0); +#endif #endif /* _ERROR_H */ diff --git a/src/common/hashtable/rculfhash-mm-mmap.c b/src/common/hashtable/rculfhash-mm-mmap.c index 4554ed600..b136e1ae0 100644 --- a/src/common/hashtable/rculfhash-mm-mmap.c +++ b/src/common/hashtable/rculfhash-mm-mmap.c @@ -24,6 +24,10 @@ #include #include "rculfhash-internal.h" +#ifndef MAP_ANONYMOUS +#define MAP_ANONYMOUS MAP_ANON +#endif + /* reserve inaccessible memory space without allocation any memory */ static void *memory_map(size_t length) { diff --git a/src/common/hashtable/utils.c b/src/common/hashtable/utils.c index 50cd1ba95..01cc72b3b 100644 --- a/src/common/hashtable/utils.c +++ b/src/common/hashtable/utils.c @@ -50,7 +50,6 @@ */ #include -#include /* attempt to define endianness */ #include /* defines uint32_t etc */ #include /* defines printf for tests */ #include @@ -59,6 +58,7 @@ #include #include "utils.h" +#include /* attempt to define endianness */ /* * My best guess at if you are big-endian or little-endian. This may diff --git a/src/common/kernel-consumer/kernel-consumer.c b/src/common/kernel-consumer/kernel-consumer.c index f7d4540cf..823b4a28a 100644 --- a/src/common/kernel-consumer/kernel-consumer.c +++ b/src/common/kernel-consumer/kernel-consumer.c @@ -19,7 +19,6 @@ #define _GNU_SOURCE #include -#include #include #include #include @@ -28,10 +27,12 @@ #include #include #include +#include #include #include #include +#include #include "kernel-consumer.h" @@ -72,7 +73,7 @@ ssize_t lttng_kconsumer_on_read_subbuffer_mmap( goto end; } /* This won't block, but will start writeout asynchronously */ - sync_file_range(outfd, stream->out_fd_offset, ret, + lttng_sync_file_range(outfd, stream->out_fd_offset, ret, SYNC_FILE_RANGE_WRITE); stream->out_fd_offset += ret; } @@ -122,7 +123,7 @@ ssize_t lttng_kconsumer_on_read_subbuffer_splice( } len -= ret; /* This won't block, but will start writeout asynchronously */ - sync_file_range(outfd, stream->out_fd_offset, ret, + lttng_sync_file_range(outfd, stream->out_fd_offset, ret, SYNC_FILE_RANGE_WRITE); stream->out_fd_offset += ret; } diff --git a/src/common/runas.c b/src/common/runas.c index 745a6d0d1..4742a792e 100644 --- a/src/common/runas.c +++ b/src/common/runas.c @@ -28,16 +28,29 @@ #include #include #include -#include +#include #include +#include +#include #include "runas.h" #define RUNAS_CHILD_STACK_SIZE 10485760 -#ifndef MAP_STACK -#define MAP_STACK 0 +#ifdef __FreeBSD__ +/* FreeBSD MAP_STACK always return -ENOMEM */ +#define LTTNG_MAP_STACK 0 +#else +#define LTTNG_MAP_STACK MAP_STACK +#endif + +#ifndef MAP_GROWSDOWN +#define MAP_GROWSDOWN 0 +#endif + +#ifndef MAP_ANONYMOUS +#define MAP_ANONYMOUS MAP_ANON #endif struct run_as_data { @@ -187,7 +200,7 @@ int child_run_as(void *_data) } static -int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) +int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) { struct run_as_data run_as_data; int ret = 0; @@ -225,7 +238,7 @@ int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) run_as_data.retval_pipe = retval_pipe[1]; /* write end */ child_stack = mmap(NULL, RUNAS_CHILD_STACK_SIZE, PROT_WRITE | PROT_READ, - MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | MAP_STACK, + MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | LTTNG_MAP_STACK, -1, 0); if (child_stack == MAP_FAILED) { perror("mmap"); @@ -236,9 +249,8 @@ int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) * Pointing to the middle of the stack to support architectures * where the stack grows up (HPPA). */ - pid = clone(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2), - CLONE_FILES | SIGCHLD, - &run_as_data, NULL); + pid = lttng_clone_files(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2), + &run_as_data); if (pid < 0) { perror("clone"); retval.i = pid; @@ -280,6 +292,29 @@ end: return retval.i; } +/* + * To be used on setups where gdb has issues debugging programs using + * clone/rfork. Note that this is for debuging ONLY, and should not be + * considered secure. + */ +static +int run_as_noclone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) +{ + return cmd(data); +} + +static +int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) +{ + if (!getenv("LTTNG_DEBUG_NOCLONE")) { + DBG("Using run_as_clone"); + return run_as_clone(cmd, data, uid, gid); + } else { + DBG("Using run_as_noclone"); + return run_as_noclone(cmd, data, uid, gid); + } +} + int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid) { struct run_as_mkdir_data data; diff --git a/src/common/sessiond-comm/sessiond-comm.c b/src/common/sessiond-comm/sessiond-comm.c index 0c29003a6..3b224c6ff 100644 --- a/src/common/sessiond-comm/sessiond-comm.c +++ b/src/common/sessiond-comm/sessiond-comm.c @@ -24,7 +24,6 @@ #include #include #include -#include #include #include @@ -412,7 +411,7 @@ ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd) } if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) { fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", - cmsg->cmsg_len, CMSG_LEN(sizeof_fds)); + (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds)); ret = -1; goto end; } @@ -430,12 +429,14 @@ end: ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len) { struct msghdr msg; - struct cmsghdr *cmptr; struct iovec iov[1]; ssize_t ret = -1; - struct ucred *creds; - size_t sizeof_cred = sizeof(struct ucred); +#ifdef __linux__ + struct cmsghdr *cmptr; + size_t sizeof_cred = sizeof(lttng_sock_cred); char anc_buf[CMSG_SPACE(sizeof_cred)]; + lttng_sock_cred *creds; +#endif /* __linux__ */ memset(&msg, 0, sizeof(msg)); @@ -444,19 +445,21 @@ ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len) msg.msg_iov = iov; msg.msg_iovlen = 1; +#ifdef __linux__ msg.msg_control = (caddr_t) anc_buf; msg.msg_controllen = CMSG_LEN(sizeof_cred); cmptr = CMSG_FIRSTHDR(&msg); cmptr->cmsg_level = SOL_SOCKET; - cmptr->cmsg_type = SCM_CREDENTIALS; + cmptr->cmsg_type = LTTNG_SOCK_CREDS; cmptr->cmsg_len = CMSG_LEN(sizeof_cred); - creds = (struct ucred *) CMSG_DATA(cmptr); + creds = (lttng_sock_cred*) CMSG_DATA(cmptr); - creds->uid = geteuid(); - creds->gid = getegid(); - creds->pid = getpid(); + LTTNG_SOCK_SET_UID_CRED(creds, geteuid()); + LTTNG_SOCK_SET_GID_CRED(creds, getegid()); + LTTNG_SOCK_SET_PID_CRED(creds, getpid()); +#endif /* __linux__ */ ret = sendmsg(sock, &msg, 0); if (ret < 0) { @@ -472,14 +475,16 @@ ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len) * Returns the size of received data, or negative error value. */ ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, - struct ucred *creds) + lttng_sock_cred *creds) { struct msghdr msg; - struct cmsghdr *cmptr; struct iovec iov[1]; ssize_t ret; - size_t sizeof_cred = sizeof(struct ucred); +#ifdef __linux__ + struct cmsghdr *cmptr; + size_t sizeof_cred = sizeof(lttng_sock_cred); char anc_buf[CMSG_SPACE(sizeof_cred)]; +#endif /* __linux__ */ memset(&msg, 0, sizeof(msg)); @@ -495,8 +500,10 @@ ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, msg.msg_iov = iov; msg.msg_iovlen = 1; +#ifdef __linux__ msg.msg_control = anc_buf; msg.msg_controllen = sizeof(anc_buf); +#endif /* __linux__ */ ret = recvmsg(sock, &msg, 0); if (ret < 0) { @@ -504,6 +511,7 @@ ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, goto end; } +#ifdef __linux__ if (msg.msg_flags & MSG_CTRUNC) { fprintf(stderr, "Error: Control message truncated.\n"); ret = -1; @@ -518,7 +526,7 @@ ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, } if (cmptr->cmsg_level != SOL_SOCKET || - cmptr->cmsg_type != SCM_CREDENTIALS) { + cmptr->cmsg_type != LTTNG_SOCK_CREDS) { fprintf(stderr, "Didn't received any credentials\n"); ret = -1; goto end; @@ -526,12 +534,24 @@ ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, if (cmptr->cmsg_len != CMSG_LEN(sizeof_cred)) { fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", - cmptr->cmsg_len, CMSG_LEN(sizeof_cred)); + (size_t) cmptr->cmsg_len, (size_t) CMSG_LEN(sizeof_cred)); ret = -1; goto end; } memcpy(creds, CMSG_DATA(cmptr), sizeof_cred); +#elif defined(__FreeBSD__) + { + int peer_ret; + + peer_ret = getpeereid(sock, &creds->uid, &creds->gid); + if (peer_ret != 0) { + return peer_ret; + } + } +#else +#error "Please implement credential support for your OS." +#endif /* __linux__ */ end: return ret; @@ -540,6 +560,7 @@ end: /* * Set socket option to use credentials passing. */ +#ifdef __linux__ int lttcomm_setsockopt_creds_unix_sock(int sock) { int ret, on = 1; @@ -549,6 +570,13 @@ int lttcomm_setsockopt_creds_unix_sock(int sock) if (ret < 0) { perror("setsockopt creds unix sock"); } - return ret; } +#elif defined(__FreeBSD__) +int lttcomm_setsockopt_creds_unix_sock(int sock) +{ + return 0; +} +#else +#error "Please implement credential support for your OS." +#endif /* __linux__ */ diff --git a/src/common/sessiond-comm/sessiond-comm.h b/src/common/sessiond-comm/sessiond-comm.h index 31b7fe466..e4d81f287 100644 --- a/src/common/sessiond-comm/sessiond-comm.h +++ b/src/common/sessiond-comm/sessiond-comm.h @@ -28,7 +28,7 @@ #define _GNU_SOURCE #include #include -#include +#include /* Queue size of listen(2) */ #define LTTNG_SESSIOND_COMM_MAX_LISTEN 64 @@ -291,7 +291,7 @@ extern ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len); extern ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len); extern ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, - struct ucred *creds); + lttng_sock_cred *creds); extern const char *lttcomm_get_readable_code(enum lttcomm_return_code code); extern int lttcomm_setsockopt_creds_unix_sock(int sock); diff --git a/src/common/ust-consumer/ust-consumer.c b/src/common/ust-consumer/ust-consumer.c index 04c6157e5..c8ba08460 100644 --- a/src/common/ust-consumer/ust-consumer.c +++ b/src/common/ust-consumer/ust-consumer.c @@ -19,19 +19,20 @@ #define _GNU_SOURCE #include -#include #include #include #include #include #include #include +#include #include #include #include #include #include +#include #include "ust-consumer.h" @@ -71,7 +72,7 @@ ssize_t lttng_ustconsumer_on_read_subbuffer_mmap( goto end; } /* This won't block, but will start writeout asynchronously */ - sync_file_range(outfd, stream->out_fd_offset, ret, + lttng_sync_file_range(outfd, stream->out_fd_offset, ret, SYNC_FILE_RANGE_WRITE); stream->out_fd_offset += ret; } diff --git a/tests/Makefile.am b/tests/Makefile.am index 1dd8bbea7..8dd05a4f0 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,6 +1,7 @@ SUBDIRS = . -AM_CFLAGS=-g -Wall -lurcu -lurcu-cds +AM_CFLAGS = -g -Wall +AM_LDFLAGS = -lurcu -lurcu-cds EXTRA_DIST = runall.sh utils.sh lttng/runall.sh lttng/run-kernel-tests.sh diff --git a/tests/ust-nevents/Makefile.am b/tests/ust-nevents/Makefile.am index 8b7a914ee..29652dcbb 100644 --- a/tests/ust-nevents/Makefile.am +++ b/tests/ust-nevents/Makefile.am @@ -1,5 +1,12 @@ AM_CFLAGS = -I. -O2 -AM_LDFLAGS = -llttng-ust -ldl +AM_LDFLAGS = -llttng-ust + +if LTTNG_TOOLS_BUILD_WITH_LIBDL +AM_LDFLAGS += -ldl +endif +if LTTNG_TOOLS_BUILD_WITH_LIBC_DL +AM_LDFLAGS += -lc +endif noinst_PROGRAMS = gen-nevents gen_nevents_SOURCES = gen-nevents.c tp.c ust_gen_nevents.h diff --git a/tests/ust-nprocesses/Makefile.am b/tests/ust-nprocesses/Makefile.am index 39168d6a2..d3fdcbd41 100644 --- a/tests/ust-nprocesses/Makefile.am +++ b/tests/ust-nprocesses/Makefile.am @@ -1,5 +1,12 @@ AM_CFLAGS = -I. -O2 -AM_LDFLAGS = -llttng-ust -ldl +AM_LDFLAGS = -llttng-ust + +if LTTNG_TOOLS_BUILD_WITH_LIBDL +AM_LDFLAGS += -ldl +endif +if LTTNG_TOOLS_BUILD_WITH_LIBC_DL +AM_LDFLAGS += -lc +endif noinst_PROGRAMS = gen-events-time gen_events_time_SOURCES = gen-events-time.c tp.c ust_gen_event.h