Merge branch 'master' into benchmark
authorDavid Goulet <dgoulet@efficios.com>
Thu, 8 Mar 2012 23:13:18 +0000 (18:13 -0500)
committerDavid Goulet <dgoulet@efficios.com>
Thu, 8 Mar 2012 23:13:18 +0000 (18:13 -0500)
53 files changed:
ChangeLog
configure.ac
doc/man/lttng-sessiond.8
include/lttng/lttng.h
src/bin/lttng-consumerd/lttng-consumerd.c
src/bin/lttng-sessiond/channel.c
src/bin/lttng-sessiond/kernel.c
src/bin/lttng-sessiond/lttng-sessiond.h
src/bin/lttng-sessiond/main.c
src/bin/lttng-sessiond/modprobe.c
src/bin/lttng-sessiond/session.c
src/bin/lttng-sessiond/shm.c
src/bin/lttng-sessiond/trace-kernel.c
src/bin/lttng-sessiond/trace-ust.c
src/bin/lttng-sessiond/ust-app.c
src/bin/lttng-sessiond/ust-consumer.c
src/bin/lttng/lttng.c
src/bin/lttng/utils.c
src/common/Makefile.am
src/common/compat/clone.h [new file with mode: 0644]
src/common/compat/compat-epoll.c
src/common/compat/compat-fcntl.c [new file with mode: 0644]
src/common/compat/compat-poll.c
src/common/compat/endian.h [new file with mode: 0644]
src/common/compat/fcntl.h [new file with mode: 0644]
src/common/compat/mman.h [new file with mode: 0644]
src/common/compat/poll.h
src/common/compat/socket.h [new file with mode: 0644]
src/common/consumer.c
src/common/consumer.h
src/common/error.h
src/common/hashtable/hashtable.c
src/common/hashtable/hashtable.h
src/common/hashtable/rculfhash-mm-mmap.c
src/common/hashtable/utils.c
src/common/kernel-consumer/kernel-consumer.c
src/common/kernel-consumer/kernel-consumer.h
src/common/runas.c
src/common/sessiond-comm/sessiond-comm.c
src/common/sessiond-comm/sessiond-comm.h
src/common/ust-consumer/ust-consumer.c
src/common/ust-consumer/ust-consumer.h
src/lib/lttng-ctl/lttng-ctl.c
tests/Makefile.am
tests/lttng/kernel_all_events_basic.c
tests/lttng/kernel_event_basic.c
tests/lttng/ust_global_all_events_basic.c
tests/lttng/ust_global_event_basic.c
tests/test_kernel_data_trace.c
tests/test_sessions.c
tests/test_ust_data_trace.c
tests/ust-nevents/Makefile.am
tests/ust-nprocesses/Makefile.am

index 4f4705d2a966273e091d6f1f1db091f0f236b930..a645bb746a1d016b0b9a84b3c43a5a3dfe114f41 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,40 @@
+2012-03-02 lttng-tools 2.0.0-rc2
+       * Fix: meaningful error message
+       * Fix: UST consumer need to iterate on streams, just change their key
+       * Fix: add missing rcu read lock across RCU HT iteration
+       * Fix: kernel session closes fd 0 after create
+       * Fix: sendmsg EPIPE should be quiet by default (expected)
+       * Fix: thread_registration_apps should set its local sock to -1 when passing it
+       * Fix: clock -> sock typo
+       * Fix: consumer race: should allow reuse of FD key
+       * Fix: Use PERROR all across lttng-tools, never make it quiet
+       * Fix: test all close return values in sessiond
+       * Fix: All perror turned into PERROR to show file and line number
+       * Fix: large audit of close() use in sessiond main.c
+       * Fix: main.c client/apps sockets and kernel_trace_fd close(0)
+       * Fix: incorrect close of fd 0 for syscall kernel event destroy
+       * Fix: sessiond has incorrect missing 0 value in FD check
+       * Fix: sessiond app listening: use posix-compliant poll flags
+       * Fix: consumer printf type should match ssize_t (%zd)
+       * Fix: make ust consumer posix compliant for poll flags
+       * Fix security permission on lttng run directory
+       * Fix: Display right loglevel_type in error message
+       * Fix documentation in lttng.h
+       * Fix: lttng UST and kernel consumer: fix ret vs errno mixup
+       * Fix: restart consumerd and sessiond when interrupted in poll()
+       * Fix: handling bad channel when sending to consumer
+       * Fix useless variable
+       * Fix add-context returned error
+       * fix: add missing break in command handling
+       * fix: command handling: do not check domain for commands not requiring domain
+       * fix: if tracing group does not exist, do not report a client error
+       * Fix: run_as error handling
+       * Fix usage note on -a
+       * Revert FreeBSD compatibility layer
+       * Fix: documented number of subbuffers is incorrect
+       * Document that num-subbuf and subbuf-size need to be power of 2
+       * Merge branch 'master' of git://git.lttng.org/lttng-tools
+
 2012-02-20 lttng-tools 2.0.0-rc1
        * Fix lttcomm_close_unix_sock to actually close the socket
        * lttng-sessiond: Set group permissions explicitly
index 322d9f21e17df45608054eda6ae04f4f9b244679..4bfff84bab4c99fb01e4fb4219d85fc714c9707d 100644 (file)
@@ -1,4 +1,4 @@
-AC_INIT([lttng-tools],[2.0.0-rc1],[dgoulet@efficios.com],[],[http://lttng.org])
+AC_INIT([lttng-tools],[2.0.0-rc2],[dgoulet@efficios.com],[],[http://lttng.org])
 AC_CONFIG_AUX_DIR([config])
 AC_CANONICAL_TARGET
 AC_CANONICAL_HOST
@@ -121,6 +121,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]]),
index 8a235f94130dcb2c5353807d374cd49719d4526a..50a03b029d54bf0f45b4744310ac5c2b7a512bc4 100644 (file)
@@ -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
index 065caee9146a12d2bf9f63dfff3aa0511c611641..05ba6d3a812eea5b3b24e5458b913e2d051851e7 100644 (file)
@@ -416,7 +416,7 @@ extern int lttng_add_context(struct lttng_handle *handle,
                const char *channel_name);
 
 /*
- * Create or enable a kernel event (or events) for a channel.
+ * Create or enable an event (or events) for a channel.
  *
  * If the event you are trying to enable does not exist, it will be created,
  * else it is enabled.
@@ -427,14 +427,14 @@ extern int lttng_enable_event(struct lttng_handle *handle,
                struct lttng_event *ev, const char *channel_name);
 
 /*
- * Create or enable a kernel channel.
+ * Create or enable a channel.
  * The channel name cannot be NULL.
  */
 extern int lttng_enable_channel(struct lttng_handle *handle,
                struct lttng_channel *chan);
 
 /*
- * Disable kernel event(s) of a channel and domain.
+ * Disable event(s) of a channel and domain.
  *
  * If event_name is NULL, all events are disabled.
  * If channel_name is NULL, the default channel is used (channel0).
@@ -443,7 +443,7 @@ extern int lttng_disable_event(struct lttng_handle *handle,
                const char *name, const char *channel_name);
 
 /*
- * Disable kernel channel.
+ * Disable channel.
  *
  * The channel name cannot be NULL.
  */
index 7e3bf3cfbad8c99976cd5b1e8813ad2adfa882a5..52b910ac1385ee8bf92864eb158541b12e6c1f70 100644 (file)
@@ -58,8 +58,8 @@ static pthread_t threads[2];
 static int sigintcount = 0;
 
 /* Argument variables */
-int opt_quiet;
-int opt_verbose;
+int lttng_opt_quiet;    /* not static in error.h */
+int lttng_opt_verbose;  /* not static in error.h */
 static int opt_daemon;
 static const char *progname;
 static char command_sock_path[PATH_MAX]; /* Global command socket path */
@@ -199,10 +199,10 @@ static void parse_args(int argc, char **argv)
                        usage(stdout);
                        exit(EXIT_SUCCESS);
                case 'q':
-                       opt_quiet = 1;
+                       lttng_opt_quiet = 1;
                        break;
                case 'v':
-                       opt_verbose = 1;
+                       lttng_opt_verbose = 1;
                        break;
                case 'V':
                        fprintf(stdout, "%s\n", VERSION);
index 54345d9c94bfa96a70ab9cd0ab6ffb925a9d4953..58a36b97c8b2f54085ed0b2ad463ef005c68d53a 100644 (file)
@@ -235,6 +235,21 @@ int channel_ust_create(struct ltt_ust_session *usess, int domain,
                attr = defattr;
        }
 
+       /*
+        * Validate UST buffer size and number of buffers: must both be
+        * power of 2 and nonzero. We validate right here for UST,
+        * because applications will not report the error to the user
+        * (unlike kernel tracing).
+        */
+       if (!attr->attr.subbuf_size || (attr->attr.subbuf_size & (attr->attr.subbuf_size - 1))) {
+               ret = LTTCOMM_INVALID;
+               goto error;
+       }
+       if (!attr->attr.num_subbuf || (attr->attr.num_subbuf & (attr->attr.num_subbuf - 1))) {
+               ret = LTTCOMM_INVALID;
+               goto error;
+       }
+
        /* Create UST channel */
        uchan = trace_ust_create_channel(attr, usess->pathname);
        if (uchan == NULL) {
index 8e960dfed8fe59786a3de2436aae3155db9851d6..fea51b761c8ea2f0b224f2dfd2dc8589a9c8ad8b 100644 (file)
@@ -42,7 +42,7 @@ int kernel_add_channel_context(struct ltt_kernel_channel *chan,
        ret = kernctl_add_context(chan->fd, ctx);
        if (ret < 0) {
                if (errno != EEXIST) {
-                       perror("add context ioctl");
+                       PERROR("add context ioctl");
                } else {
                        /* If EEXIST, we just ignore the error */
                        ret = 0;
@@ -52,7 +52,7 @@ int kernel_add_channel_context(struct ltt_kernel_channel *chan,
 
        chan->ctx = zmalloc(sizeof(struct lttng_kernel_context));
        if (chan->ctx == NULL) {
-               perror("zmalloc event context");
+               PERROR("zmalloc event context");
                goto error;
        }
 
@@ -75,13 +75,13 @@ int kernel_add_event_context(struct ltt_kernel_event *event,
        DBG("Adding context to event %s", event->event->name);
        ret = kernctl_add_context(event->fd, ctx);
        if (ret < 0) {
-               perror("add context ioctl");
+               PERROR("add context ioctl");
                goto error;
        }
 
        event->ctx = zmalloc(sizeof(struct lttng_kernel_context));
        if (event->ctx == NULL) {
-               perror("zmalloc event context");
+               PERROR("zmalloc event context");
                goto error;
        }
 
@@ -112,7 +112,7 @@ int kernel_create_session(struct ltt_session *session, int tracer_fd)
        /* Kernel tracer session creation */
        ret = kernctl_create_session(tracer_fd);
        if (ret < 0) {
-               perror("ioctl kernel create session");
+               PERROR("ioctl kernel create session");
                goto error;
        }
 
@@ -120,7 +120,7 @@ int kernel_create_session(struct ltt_session *session, int tracer_fd)
        /* Prevent fd duplication after execlp() */
        ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
        if (ret < 0) {
-               perror("fcntl session fd");
+               PERROR("fcntl session fd");
        }
 
        lks->consumer_fds_sent = 0;
@@ -153,7 +153,7 @@ int kernel_create_channel(struct ltt_kernel_session *session,
        /* Kernel tracer channel creation */
        ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
        if (ret < 0) {
-               perror("ioctl kernel create channel");
+               PERROR("ioctl kernel create channel");
                goto error;
        }
 
@@ -162,7 +162,7 @@ int kernel_create_channel(struct ltt_kernel_session *session,
        /* Prevent fd duplication after execlp() */
        ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
        if (ret < 0) {
-               perror("fcntl session fd");
+               PERROR("fcntl session fd");
        }
 
        /* Add channel to session */
@@ -208,6 +208,11 @@ int kernel_create_event(struct lttng_event *ev,
         */
        if (ret == 0 && event->event->instrumentation == LTTNG_KERNEL_SYSCALL) {
                DBG2("Kernel event syscall creation success");
+               /*
+                * We use fd == -1 to ensure that we never trigger a close of fd
+                * 0.
+                */
+               event->fd = -1;
                goto add_list;
        }
 
@@ -215,7 +220,7 @@ int kernel_create_event(struct lttng_event *ev,
        /* Prevent fd duplication after execlp() */
        ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
        if (ret < 0) {
-               perror("fcntl session fd");
+               PERROR("fcntl session fd");
        }
 
 add_list:
@@ -242,7 +247,7 @@ int kernel_disable_channel(struct ltt_kernel_channel *chan)
 
        ret = kernctl_disable(chan->fd);
        if (ret < 0) {
-               perror("disable chan ioctl");
+               PERROR("disable chan ioctl");
                ret = errno;
                goto error;
        }
@@ -265,7 +270,7 @@ int kernel_enable_channel(struct ltt_kernel_channel *chan)
 
        ret = kernctl_enable(chan->fd);
        if (ret < 0 && errno != EEXIST) {
-               perror("Enable kernel chan");
+               PERROR("Enable kernel chan");
                goto error;
        }
 
@@ -287,7 +292,7 @@ int kernel_enable_event(struct ltt_kernel_event *event)
 
        ret = kernctl_enable(event->fd);
        if (ret < 0 && errno != EEXIST) {
-               perror("enable kernel event");
+               PERROR("enable kernel event");
                goto error;
        }
 
@@ -309,7 +314,7 @@ int kernel_disable_event(struct ltt_kernel_event *event)
 
        ret = kernctl_disable(event->fd);
        if (ret < 0 && errno != EEXIST) {
-               perror("disable kernel event");
+               PERROR("disable kernel event");
                goto error;
        }
 
@@ -347,7 +352,7 @@ int kernel_open_metadata(struct ltt_kernel_session *session, char *path)
        /* Prevent fd duplication after execlp() */
        ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
        if (ret < 0) {
-               perror("fcntl session fd");
+               PERROR("fcntl session fd");
        }
 
        session->metadata = lkm;
@@ -369,7 +374,7 @@ int kernel_start_session(struct ltt_kernel_session *session)
 
        ret = kernctl_start_session(session->fd);
        if (ret < 0) {
-               perror("ioctl start session");
+               PERROR("ioctl start session");
                goto error;
        }
 
@@ -392,7 +397,7 @@ void kernel_wait_quiescent(int fd)
 
        ret = kernctl_wait_quiescent(fd);
        if (ret < 0) {
-               perror("wait quiescent ioctl");
+               PERROR("wait quiescent ioctl");
                ERR("Kernel quiescent wait failed");
        }
 }
@@ -406,7 +411,7 @@ int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
 
        ret = kernctl_calibrate(fd, calibrate);
        if (ret < 0) {
-               perror("calibrate ioctl");
+               PERROR("calibrate ioctl");
                return -1;
        }
 
@@ -443,7 +448,7 @@ int kernel_flush_buffer(struct ltt_kernel_channel *channel)
                DBG("Flushing channel stream %d", stream->fd);
                ret = kernctl_buffer_flush(stream->fd);
                if (ret < 0) {
-                       perror("ioctl");
+                       PERROR("ioctl");
                        ERR("Fail to flush buffer for stream %d (ret: %d)",
                                        stream->fd, ret);
                }
@@ -483,10 +488,13 @@ int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
        int ret;
        struct ltt_kernel_stream *lks;
 
-       while ((ret = kernctl_create_stream(channel->fd)) > 0) {
+       while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
                lks = trace_kernel_create_stream();
                if (lks == NULL) {
-                       close(ret);
+                       ret = close(ret);
+                       if (ret) {
+                               PERROR("close");
+                       }
                        goto error;
                }
 
@@ -494,13 +502,13 @@ int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
                /* Prevent fd duplication after execlp() */
                ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
                if (ret < 0) {
-                       perror("fcntl session fd");
+                       PERROR("fcntl session fd");
                }
 
                ret = asprintf(&lks->pathname, "%s/%s_%d",
                                channel->pathname, channel->channel->name, channel->stream_count);
                if (ret < 0) {
-                       perror("asprintf kernel create stream");
+                       PERROR("asprintf kernel create stream");
                        goto error;
                }
 
@@ -527,7 +535,7 @@ int kernel_open_metadata_stream(struct ltt_kernel_session *session)
 
        ret = kernctl_create_stream(session->metadata->fd);
        if (ret < 0) {
-               perror("kernel create metadata stream");
+               PERROR("kernel create metadata stream");
                goto error;
        }
 
@@ -536,7 +544,7 @@ int kernel_open_metadata_stream(struct ltt_kernel_session *session)
        /* Prevent fd duplication after execlp() */
        ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
        if (ret < 0) {
-               perror("fcntl session fd");
+               PERROR("fcntl session fd");
        }
 
        return 0;
@@ -550,7 +558,7 @@ error:
  */
 ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
 {
-       int fd, pos;
+       int fd, pos, ret;
        char *event;
        size_t nbmem, count = 0;
        ssize_t size;
@@ -559,13 +567,13 @@ ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
 
        fd = kernctl_tracepoint_list(tracer_fd);
        if (fd < 0) {
-               perror("kernel tracepoint list");
+               PERROR("kernel tracepoint list");
                goto error;
        }
 
        fp = fdopen(fd, "r");
        if (fp == NULL) {
-               perror("kernel tracepoint list fdopen");
+               PERROR("kernel tracepoint list fdopen");
                goto error_fp;
        }
 
@@ -584,7 +592,7 @@ ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
                        nbmem <<= 1;
                        elist = realloc(elist, nbmem * sizeof(struct lttng_event));
                        if (elist == NULL) {
-                               perror("realloc list events");
+                               PERROR("realloc list events");
                                count = -ENOMEM;
                                goto end;
                        }
@@ -598,11 +606,17 @@ ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
        *events = elist;
        DBG("Kernel list events done (%zu events)", count);
 end:
-       fclose(fp);     /* closes both fp and fd */
+       ret = fclose(fp);       /* closes both fp and fd */
+       if (ret) {
+               PERROR("fclose");
+       }
        return count;
 
 error_fp:
-       close(fd);
+       ret = close(fd);
+       if (ret) {
+               PERROR("close");
+       }
 error:
        return -1;
 }
@@ -664,8 +678,10 @@ int init_kernel_workarounds(void)
                        /* Ignore error, we don't really care */
                }
        }
-       fclose(fp);
+       ret = fclose(fp);
+       if (ret) {
+               PERROR("fclose");
+       }
 end_boot_id:
-
        return 0;
 }
index 2ba2550c7a1bda4cdb6fa3d1ff2f1f9313ad35ae..79e7fcb4383b1765a870a44c2c6576f5c6c7b92c 100644 (file)
@@ -24,6 +24,7 @@
 #include <urcu/wfqueue.h>
 
 #include <common/sessiond-comm/sessiond-comm.h>
+#include <common/compat/socket.h>
 
 #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 {
index 09353374e0be885e2d9775f265025bc7401ca9d0..e341233ff0ae6cee85e04f85788716ef476779b2 100644 (file)
@@ -17,7 +17,6 @@
  */
 
 #define _GNU_SOURCE
-#include <fcntl.h>
 #include <getopt.h>
 #include <grp.h>
 #include <limits.h>
@@ -40,6 +39,7 @@
 
 #include <common/common.h>
 #include <common/compat/poll.h>
+#include <common/compat/socket.h>
 #include <common/defaults.h>
 #include <common/kernel-consumer/kernel-consumer.h>
 #include <common/ust-consumer/ust-consumer.h>
@@ -83,14 +83,10 @@ const char default_tracing_group[] = DEFAULT_TRACING_GROUP;
 const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR;
 const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE;
 
-/* Variables */
-int opt_verbose;    /* Not static for lttngerr.h */
-int opt_verbose_consumer;    /* Not static for lttngerr.h */
-int opt_quiet;      /* Not static for lttngerr.h */
-
 const char *progname;
 const char *opt_tracing_group;
 static int opt_sig_parent;
+static int opt_verbose_consumer;
 static int opt_daemon;
 static int opt_no_kernel;
 static int is_root;                    /* Set to 1 if the daemon is running as root */
@@ -102,16 +98,22 @@ static struct consumer_data kconsumer_data = {
        .type = LTTNG_CONSUMER_KERNEL,
        .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
        .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
+       .err_sock = -1,
+       .cmd_sock = -1,
 };
 static struct consumer_data ustconsumer64_data = {
        .type = LTTNG_CONSUMER64_UST,
        .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
        .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
+       .err_sock = -1,
+       .cmd_sock = -1,
 };
 static struct consumer_data ustconsumer32_data = {
        .type = LTTNG_CONSUMER32_UST,
        .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
        .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
+       .err_sock = -1,
+       .cmd_sock = -1,
 };
 
 static int dispatch_thread_exit;
@@ -124,22 +126,22 @@ static char client_unix_sock_path[PATH_MAX];
 static char wait_shm_path[PATH_MAX];
 
 /* Sockets and FDs */
-static int client_sock;
-static int apps_sock;
-static int kernel_tracer_fd;
-static int kernel_poll_pipe[2];
+static int client_sock = -1;
+static int apps_sock = -1;
+static int kernel_tracer_fd = -1;
+static int kernel_poll_pipe[2] = { -1, -1 };
 
 /*
  * Quit pipe for all threads. This permits a single cancellation point
  * for all threads when receiving an event on the pipe.
  */
-static int thread_quit_pipe[2];
+static int thread_quit_pipe[2] = { -1, -1 };
 
 /*
  * This pipe is used to inform the thread managing application communication
  * that a command is queued and ready to be processed.
  */
-static int apps_cmd_pipe[2];
+static int apps_cmd_pipe[2] = { -1, -1 };
 
 /* Pthread, Mutexes and Semaphores */
 static pthread_t apps_thread;
@@ -297,14 +299,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;
 }
@@ -326,7 +336,8 @@ static void teardown_kernel_session(struct ltt_session *session)
         * If a custom kernel consumer was registered, close the socket before
         * tearing down the complete kernel session structure
         */
-       if (session->kernel_session->consumer_fd != kconsumer_data.cmd_sock) {
+       if (kconsumer_data.cmd_sock >= 0 &&
+                       session->kernel_session->consumer_fd != kconsumer_data.cmd_sock) {
                lttcomm_close_unix_sock(session->kernel_session->consumer_fd);
        }
 
@@ -380,7 +391,7 @@ static void stop_threads(void)
  */
 static void cleanup(void)
 {
-       int ret;
+       int ret, i;
        char *cmd;
        struct ltt_session *sess, *stmp;
 
@@ -421,13 +432,44 @@ static void cleanup(void)
 
        if (is_root && !opt_no_kernel) {
                DBG2("Closing kernel fd");
-               close(kernel_tracer_fd);
+               if (kernel_tracer_fd >= 0) {
+                       ret = close(kernel_tracer_fd);
+                       if (ret) {
+                               PERROR("close");
+                       }
+               }
                DBG("Unloading kernel modules");
                modprobe_remove_lttng_all();
        }
 
-       close(thread_quit_pipe[0]);
-       close(thread_quit_pipe[1]);
+       /*
+        * Closing all pipes used for communication between threads.
+        */
+       for (i = 0; i < 2; i++) {
+               if (kernel_poll_pipe[i] >= 0) {
+                       ret = close(kernel_poll_pipe[i]);
+                       if (ret) {
+                               PERROR("close");
+                       }
+                       
+               }
+       }
+       for (i = 0; i < 2; i++) {
+               if (thread_quit_pipe[i] >= 0) {
+                       ret = close(thread_quit_pipe[i]);
+                       if (ret) {
+                               PERROR("close");
+                       }
+               }
+       }
+       for (i = 0; i < 2; i++) {
+               if (apps_cmd_pipe[i] >= 0) {
+                       ret = close(apps_cmd_pipe[i]);
+                       if (ret) {
+                               PERROR("close");
+                       }
+               }
+       }
 
        /* OUTPUT BENCHMARK RESULTS */
        bench_init();
@@ -510,7 +552,7 @@ static int send_kconsumer_channel_streams(struct consumer_data *consumer_data,
        DBG("Sending channel %d to consumer", lkm.u.channel.channel_key);
        ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm));
        if (ret < 0) {
-               perror("send consumer channel");
+               PERROR("send consumer channel");
                goto error;
        }
 
@@ -532,12 +574,12 @@ static int send_kconsumer_channel_streams(struct consumer_data *consumer_data,
                DBG("Sending stream %d to consumer", lkm.u.stream.stream_key);
                ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm));
                if (ret < 0) {
-                       perror("send consumer stream");
+                       PERROR("send consumer stream");
                        goto error;
                }
                ret = lttcomm_send_fds_unix_sock(sock, &stream->fd, 1);
                if (ret < 0) {
-                       perror("send consumer stream ancillary data");
+                       PERROR("send consumer stream ancillary data");
                        goto error;
                }
        }
@@ -563,12 +605,12 @@ static int send_kconsumer_session_streams(struct consumer_data *consumer_data,
 
        DBG("Sending metadata stream fd");
 
-       /* Extra protection. It's NOT supposed to be set to 0 at this point */
-       if (session->consumer_fd == 0) {
+       /* Extra protection. It's NOT supposed to be set to -1 at this point */
+       if (session->consumer_fd < 0) {
                session->consumer_fd = consumer_data->cmd_sock;
        }
 
-       if (session->metadata_stream_fd != 0) {
+       if (session->metadata_stream_fd >= 0) {
                /* Send metadata channel fd */
                lkm.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL;
                lkm.u.channel.channel_key = session->metadata->fd;
@@ -577,7 +619,7 @@ static int send_kconsumer_session_streams(struct consumer_data *consumer_data,
                DBG("Sending metadata channel %d to consumer", lkm.u.stream.stream_key);
                ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm));
                if (ret < 0) {
-                       perror("send consumer channel");
+                       PERROR("send consumer channel");
                        goto error;
                }
 
@@ -595,12 +637,12 @@ static int send_kconsumer_session_streams(struct consumer_data *consumer_data,
                DBG("Sending metadata stream %d to consumer", lkm.u.stream.stream_key);
                ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm));
                if (ret < 0) {
-                       perror("send consumer stream");
+                       PERROR("send consumer stream");
                        goto error;
                }
                ret = lttcomm_send_fds_unix_sock(sock, &session->metadata_stream_fd, 1);
                if (ret < 0) {
-                       perror("send consumer stream");
+                       PERROR("send consumer stream");
                        goto error;
                }
        }
@@ -665,7 +707,7 @@ static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
 
        cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
        if (cmd_ctx->llm == NULL) {
-               perror("zmalloc");
+               PERROR("zmalloc");
                ret = -ENOMEM;
                goto error;
        }
@@ -746,8 +788,8 @@ static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
                        continue;
                }
 
-               /* This is not suppose to be 0 but this is an extra security check */
-               if (session->kernel_session->consumer_fd == 0) {
+               /* This is not suppose to be -1 but this is an extra security check */
+               if (session->kernel_session->consumer_fd < 0) {
                        session->kernel_session->consumer_fd = consumer_data->cmd_sock;
                }
 
@@ -827,7 +869,7 @@ static void *thread_manage_kernel(void *data)
 
        ret = create_thread_poll_set(&events, 2);
        if (ret < 0) {
-               goto error;
+               goto error_poll_create;
        }
 
        ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
@@ -914,12 +956,9 @@ static void *thread_manage_kernel(void *data)
        }
 
 error:
-       DBG("Kernel thread dying");
-       close(kernel_poll_pipe[0]);
-       close(kernel_poll_pipe[1]);
-
        lttng_poll_clean(&events);
-
+error_poll_create:
+       DBG("Kernel thread dying");
        return NULL;
 }
 
@@ -928,7 +967,7 @@ error:
  */
 static void *thread_manage_consumer(void *data)
 {
-       int sock = 0, i, ret, pollfd;
+       int sock = -1, i, ret, pollfd;
        uint32_t revents, nb_fd;
        enum lttcomm_return_code code;
        struct lttng_poll_event events;
@@ -940,7 +979,7 @@ static void *thread_manage_consumer(void *data)
 
        ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
        if (ret < 0) {
-               goto error;
+               goto error_listen;
        }
 
        /*
@@ -949,7 +988,7 @@ static void *thread_manage_consumer(void *data)
         */
        ret = create_thread_poll_set(&events, 2);
        if (ret < 0) {
-               goto error;
+               goto error_poll;
        }
 
        ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
@@ -1083,16 +1122,33 @@ restart_poll:
        ERR("consumer return code : %s", lttcomm_get_readable_code(-code));
 
 error:
-       DBG("consumer thread dying");
-       close(consumer_data->err_sock);
-       close(consumer_data->cmd_sock);
-       close(sock);
+       if (consumer_data->err_sock >= 0) {
+               ret = close(consumer_data->err_sock);
+               if (ret) {
+                       PERROR("close");
+               }
+       }
+       if (consumer_data->cmd_sock >= 0) {
+               ret = close(consumer_data->cmd_sock);
+               if (ret) {
+                       PERROR("close");
+               }
+       }
+       if (sock >= 0) {
+               ret = close(sock);
+               if (ret) {
+                       PERROR("close");
+               }
+       }
 
        unlink(consumer_data->err_unix_sock_path);
        unlink(consumer_data->cmd_unix_sock_path);
        consumer_data->pid = 0;
 
        lttng_poll_clean(&events);
+error_poll:
+error_listen:
+       DBG("consumer thread cleanup completed");
 
        return NULL;
 }
@@ -1116,7 +1172,7 @@ static void *thread_manage_apps(void *data)
 
        ret = create_thread_poll_set(&events, 2);
        if (ret < 0) {
-               goto error;
+               goto error_poll_create;
        }
 
        ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
@@ -1169,7 +1225,7 @@ static void *thread_manage_apps(void *data)
                                        /* Empty pipe */
                                        ret = read(apps_cmd_pipe[0], &ust_cmd, sizeof(ust_cmd));
                                        if (ret < 0 || ret < sizeof(ust_cmd)) {
-                                               perror("read apps cmd pipe");
+                                               PERROR("read apps cmd pipe");
                                                goto error;
                                        }
                                        tracepoint(ust_register_read_stop);
@@ -1210,8 +1266,10 @@ static void *thread_manage_apps(void *data)
                                                /*
                                                 * We just need here to monitor the close of the UST
                                                 * socket and poll set monitor those by default.
+                                                * Listen on POLLIN (even if we never expect any
+                                                * data) to ensure that hangup wakes us.
                                                 */
-                                               ret = lttng_poll_add(&events, ust_cmd.sock, 0);
+                                               ret = lttng_poll_add(&events, ust_cmd.sock, LPOLLIN);
                                                if (ret < 0) {
                                                        goto error;
                                                }
@@ -1246,12 +1304,9 @@ static void *thread_manage_apps(void *data)
        }
 
 error:
-       DBG("Application communication apps dying");
-       close(apps_cmd_pipe[0]);
-       close(apps_cmd_pipe[1]);
-
        lttng_poll_clean(&events);
-
+error_poll_create:
+       DBG("Application communication apps thread cleanup complete");
        rcu_thread_offline();
        rcu_unregister_thread();
        return NULL;
@@ -1304,7 +1359,7 @@ static void *thread_dispatch_ust_registration(void *data)
                        ret = write(apps_cmd_pipe[1], ust_cmd,
                                        sizeof(struct ust_command));
                        if (ret < 0) {
-                               perror("write apps cmd pipe");
+                               PERROR("write apps cmd pipe");
                                if (errno == EBADF) {
                                        /*
                                         * We can't inform the application thread to process
@@ -1334,7 +1389,7 @@ error:
  */
 static void *thread_registration_apps(void *data)
 {
-       int sock = 0, i, ret, pollfd;
+       int sock = -1, i, ret, pollfd;
        uint32_t revents, nb_fd;
        struct lttng_poll_event events;
        /*
@@ -1349,7 +1404,7 @@ static void *thread_registration_apps(void *data)
 
        ret = lttcomm_listen_unix_sock(apps_sock);
        if (ret < 0) {
-               goto error;
+               goto error_listen;
        }
 
        /*
@@ -1358,13 +1413,13 @@ static void *thread_registration_apps(void *data)
         */
        ret = create_thread_poll_set(&events, 2);
        if (ret < 0) {
-               goto error;
+               goto error_create_poll;
        }
 
        /* Add the application registration socket */
        ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
        if (ret < 0) {
-               goto error;
+               goto error_poll_add;
        }
 
        /* Notify all applications to register */
@@ -1423,7 +1478,7 @@ static void *thread_registration_apps(void *data)
                                        /* Create UST registration command for enqueuing */
                                        ust_cmd = zmalloc(sizeof(struct ust_command));
                                        if (ust_cmd == NULL) {
-                                               perror("ust command zmalloc");
+                                               PERROR("ust command zmalloc");
                                                goto error;
                                        }
 
@@ -1435,16 +1490,21 @@ static void *thread_registration_apps(void *data)
                                                        sizeof(struct ust_register_msg));
                                        if (ret < 0 || ret < sizeof(struct ust_register_msg)) {
                                                if (ret < 0) {
-                                                       perror("lttcomm_recv_unix_sock register apps");
+                                                       PERROR("lttcomm_recv_unix_sock register apps");
                                                } else {
                                                        ERR("Wrong size received on apps register");
                                                }
                                                free(ust_cmd);
-                                               close(sock);
+                                               ret = close(sock);
+                                               if (ret) {
+                                                       PERROR("close");
+                                               }
+                                               sock = -1;
                                                continue;
                                        }
 
                                        ust_cmd->sock = sock;
+                                       sock = -1;
 
                                        DBG("UST registration received with pid:%d ppid:%d uid:%d"
                                                        " gid:%d sock:%d name:%s (version %d.%d)",
@@ -1472,16 +1532,28 @@ static void *thread_registration_apps(void *data)
        }
 
 error:
-       DBG("UST Registration thread dying");
-
        /* Notify that the registration thread is gone */
        notify_ust_apps(0);
 
-       close(apps_sock);
-       close(sock);
+       if (apps_sock >= 0) {
+               ret = close(apps_sock);
+               if (ret) {
+                       PERROR("close");
+               }
+       }
+       if (sock >= 0) {
+               ret = close(sock);
+               if (ret) {
+                       PERROR("close");
+               }
+       }
        unlink(apps_unix_sock_path);
 
+error_poll_add:
        lttng_poll_clean(&events);
+error_listen:
+error_create_poll:
+       DBG("UST Registration thread cleanup complete");
 
        return NULL;
 }
@@ -1721,17 +1793,17 @@ static pid_t spawn_consumerd(struct consumer_data *consumer_data)
                        break;
                }
                default:
-                       perror("unknown consumer type");
+                       PERROR("unknown consumer type");
                        exit(EXIT_FAILURE);
                }
                if (errno != 0) {
-                       perror("kernel start consumer exec");
+                       PERROR("kernel start consumer exec");
                }
                exit(EXIT_FAILURE);
        } else if (pid > 0) {
                ret = pid;
        } else {
-               perror("start consumer fork");
+               PERROR("start consumer fork");
                ret = -errno;
        }
 error:
@@ -1822,20 +1894,30 @@ static int init_kernel_tracer(void)
 
 error_version:
        modprobe_remove_lttng_control();
-       close(kernel_tracer_fd);
-       kernel_tracer_fd = 0;
+       ret = close(kernel_tracer_fd);
+       if (ret) {
+               PERROR("close");
+       }
+       kernel_tracer_fd = -1;
        return LTTCOMM_KERN_VERSION;
 
 error_modules:
-       close(kernel_tracer_fd);
+       ret = close(kernel_tracer_fd);
+       if (ret) {
+               PERROR("close");
+       }
 
 error_open:
        modprobe_remove_lttng_control();
 
 error:
        WARN("No kernel tracer available");
-       kernel_tracer_fd = 0;
-       return LTTCOMM_KERN_NA;
+       kernel_tracer_fd = -1;
+       if (!is_root) {
+               return LTTCOMM_NEED_ROOT_SESSIOND;
+       } else {
+               return LTTCOMM_KERN_NA;
+       }
 }
 
 /*
@@ -1848,10 +1930,10 @@ static int init_kernel_tracing(struct ltt_kernel_session *session)
        if (session->consumer_fds_sent == 0) {
                /*
                 * Assign default kernel consumer socket if no consumer assigned to the
-                * kernel session. At this point, it's NOT suppose to be 0 but this is
+                * kernel session. At this point, it's NOT supposed to be -1 but this is
                 * an extra security check.
                 */
-               if (session->consumer_fd == 0) {
+               if (session->consumer_fd < 0) {
                        session->consumer_fd = kconsumer_data.cmd_sock;
                }
 
@@ -1939,7 +2021,7 @@ static int create_kernel_session(struct ltt_session *session)
        }
 
        /* Set kernel consumer socket fd */
-       if (kconsumer_data.cmd_sock) {
+       if (kconsumer_data.cmd_sock >= 0) {
                session->kernel_session->consumer_fd = kconsumer_data.cmd_sock;
        }
 
@@ -2828,7 +2910,8 @@ static int cmd_start_trace(struct ltt_session *session)
        usess = session->ust_session;
 
        if (session->enabled) {
-               ret = LTTCOMM_UST_START_FAIL;
+               /* Already started. */
+               ret = LTTCOMM_TRACE_ALREADY_STARTED;
                goto error;
        }
 
@@ -2848,7 +2931,7 @@ static int cmd_start_trace(struct ltt_session *session)
                }
 
                /* Open kernel metadata stream */
-               if (ksession->metadata_stream_fd == 0) {
+               if (ksession->metadata_stream_fd < 0) {
                        ret = kernel_open_metadata_stream(ksession);
                        if (ret < 0) {
                                ERR("Kernel create metadata stream failed");
@@ -2920,7 +3003,7 @@ static int cmd_stop_trace(struct ltt_session *session)
        usess = session->ust_session;
 
        if (!session->enabled) {
-               ret = LTTCOMM_UST_STOP_FAIL;
+               ret = LTTCOMM_TRACE_ALREADY_STOPPED;
                goto error;
        }
 
@@ -2971,11 +3054,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;
        }
@@ -3004,7 +3088,7 @@ static int cmd_destroy_session(struct ltt_session *session, char *name)
         */
        ret = notify_thread_pipe(kernel_poll_pipe[1]);
        if (ret < 0) {
-               perror("write kernel poll pipe");
+               PERROR("write kernel poll pipe");
        }
 
        ret = session_destroy(session);
@@ -3244,7 +3328,11 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
 
        if (opt_no_kernel && need_domain
                        && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
-               ret = LTTCOMM_KERN_NA;
+               if (!is_root) {
+                       ret = LTTCOMM_NEED_ROOT_SESSIOND;
+               } else {
+                       ret = LTTCOMM_KERN_NA;
+               }
                goto error;
        }
 
@@ -3306,12 +3394,12 @@ static int process_client_msg(struct command_ctx *cmd_ctx)
        switch (cmd_ctx->lsm->domain.type) {
        case LTTNG_DOMAIN_KERNEL:
                if (!is_root) {
-                       ret = LTTCOMM_KERN_NA;
+                       ret = LTTCOMM_NEED_ROOT_SESSIOND;
                        goto error;
                }
 
                /* Kernel tracer check */
-               if (kernel_tracer_fd == 0) {
+               if (kernel_tracer_fd == -1) {
                        /* Basically, load kernel tracer modules */
                        ret = init_kernel_tracer();
                        if (ret != 0) {
@@ -3401,7 +3489,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;
                }
@@ -3598,7 +3687,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) {
@@ -3608,7 +3699,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();
 
@@ -3655,7 +3747,7 @@ init_setup_error:
  */
 static void *thread_manage_clients(void *data)
 {
-       int sock = 0, ret, i, pollfd;
+       int sock = -1, ret, i, pollfd;
        uint32_t revents, nb_fd;
        struct command_ctx *cmd_ctx = NULL;
        struct lttng_poll_event events;
@@ -3749,14 +3841,14 @@ static void *thread_manage_clients(void *data)
                /* Allocate context command to process the client request */
                cmd_ctx = zmalloc(sizeof(struct command_ctx));
                if (cmd_ctx == NULL) {
-                       perror("zmalloc cmd_ctx");
+                       PERROR("zmalloc cmd_ctx");
                        goto error;
                }
 
                /* Allocate data buffer for reception */
                cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
                if (cmd_ctx->lsm == NULL) {
-                       perror("zmalloc cmd_ctx->lsm");
+                       PERROR("zmalloc cmd_ctx->lsm");
                        goto error;
                }
 
@@ -3773,7 +3865,11 @@ static void *thread_manage_clients(void *data)
                                sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
                if (ret <= 0) {
                        DBG("Nothing recv() from client... continuing");
-                       close(sock);
+                       ret = close(sock);
+                       if (ret) {
+                               PERROR("close");
+                       }
+                       sock = -1;
                        free(cmd_ctx);
                        continue;
                }
@@ -3809,7 +3905,11 @@ static void *thread_manage_clients(void *data)
                }
 
                /* End of transmission */
-               close(sock);
+               ret = close(sock);
+               if (ret) {
+                       PERROR("close");
+               }
+               sock = -1;
 
                clean_command_ctx(&cmd_ctx);
        }
@@ -3817,8 +3917,18 @@ static void *thread_manage_clients(void *data)
 error:
        DBG("Client thread dying");
        unlink(client_unix_sock_path);
-       close(client_sock);
-       close(sock);
+       if (client_sock >= 0) {
+               ret = close(client_sock);
+               if (ret) {
+                       PERROR("close");
+               }
+       }
+       if (sock >= 0) {
+               ret = close(sock);
+               if (ret) {
+                       PERROR("close");
+               }
+       }
 
        lttng_poll_clean(&events);
        clean_command_ctx(&cmd_ctx);
@@ -3947,11 +4057,11 @@ static int parse_args(int argc, char **argv)
                        opt_no_kernel = 1;
                        break;
                case 'q':
-                       opt_quiet = 1;
+                       lttng_opt_quiet = 1;
                        break;
                case 'v':
                        /* Verbose level can increase using multiple -v */
-                       opt_verbose += 1;
+                       lttng_opt_verbose += 1;
                        break;
                case 'Z':
                        opt_verbose_consumer += 1;
@@ -4002,7 +4112,7 @@ static int init_daemon_socket(void)
        ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
        if (ret < 0) {
                ERR("Set file permissions failed: %s", client_unix_sock_path);
-               perror("chmod");
+               PERROR("chmod");
                goto end;
        }
 
@@ -4019,7 +4129,7 @@ static int init_daemon_socket(void)
                        S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
        if (ret < 0) {
                ERR("Set file permissions failed: %s", apps_unix_sock_path);
-               perror("chmod");
+               PERROR("chmod");
                goto end;
        }
 
@@ -4064,42 +4174,42 @@ static int set_permissions(char *rundir)
        ret = chown(rundir, 0, gid);
        if (ret < 0) {
                ERR("Unable to set group on %s", rundir);
-               perror("chown");
+               PERROR("chown");
        }
 
        /* Ensure tracing group can search the run dir */
-       ret = chmod(rundir, S_IRWXU | S_IXGRP);
+       ret = chmod(rundir, S_IRWXU | S_IXGRP | S_IXOTH);
        if (ret < 0) {
                ERR("Unable to set permissions on %s", rundir);
-               perror("chmod");
+               PERROR("chmod");
        }
 
        /* lttng client socket path */
        ret = chown(client_unix_sock_path, 0, gid);
        if (ret < 0) {
                ERR("Unable to set group on %s", client_unix_sock_path);
-               perror("chown");
+               PERROR("chown");
        }
 
        /* kconsumer error socket path */
        ret = chown(kconsumer_data.err_unix_sock_path, 0, gid);
        if (ret < 0) {
                ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
-               perror("chown");
+               PERROR("chown");
        }
 
        /* 64-bit ustconsumer error socket path */
        ret = chown(ustconsumer64_data.err_unix_sock_path, 0, gid);
        if (ret < 0) {
                ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
-               perror("chown");
+               PERROR("chown");
        }
 
        /* 32-bit ustconsumer compat32 error socket path */
        ret = chown(ustconsumer32_data.err_unix_sock_path, 0, gid);
        if (ret < 0) {
                ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
-               perror("chown");
+               PERROR("chown");
        }
 
        DBG("All permissions are set");
@@ -4110,18 +4220,54 @@ end:
 
 /*
  * Create the pipe used to wake up the kernel thread.
+ * Closed in cleanup().
  */
 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;
 }
 
 /*
  * Create the application command pipe to wake thread_manage_apps.
+ * Closed in cleanup().
  */
 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;
 }
 
 /*
@@ -4178,10 +4324,11 @@ static int set_consumer_sockets(struct consumer_data *consumer_data,
        ret = mkdir(path, S_IRWXU);
        if (ret < 0) {
                if (errno != EEXIST) {
+                       PERROR("mkdir");
                        ERR("Failed to create %s", path);
                        goto error;
                }
-               ret = 0;
+               ret = -1;
        }
 
        /* Create the kconsumerd error unix socket */
@@ -4242,7 +4389,7 @@ static int set_signal_handler(void)
        sigset_t sigset;
 
        if ((ret = sigemptyset(&sigset)) < 0) {
-               perror("sigemptyset");
+               PERROR("sigemptyset");
                return ret;
        }
 
@@ -4250,17 +4397,17 @@ static int set_signal_handler(void)
        sa.sa_mask = sigset;
        sa.sa_flags = 0;
        if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
-               perror("sigaction");
+               PERROR("sigaction");
                return ret;
        }
 
        if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
-               perror("sigaction");
+               PERROR("sigaction");
                return ret;
        }
 
        if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
-               perror("sigaction");
+               PERROR("sigaction");
                return ret;
        }
 
@@ -4284,7 +4431,7 @@ static void set_ulimit(void)
 
        ret = setrlimit(RLIMIT_NOFILE, &lim);
        if (ret < 0) {
-               perror("failed to set open files limit");
+               PERROR("failed to set open files limit");
        }
 }
 
@@ -4320,7 +4467,7 @@ int main(int argc, char **argv)
        if (opt_daemon) {
                ret = daemon(0, 0);
                if (ret < 0) {
-                       perror("daemon");
+                       PERROR("daemon");
                        goto error;
                }
        }
@@ -4524,7 +4671,7 @@ int main(int argc, char **argv)
        ret = pthread_create(&client_thread, NULL,
                        thread_manage_clients, (void *) NULL);
        if (ret != 0) {
-               perror("pthread_create clients");
+               PERROR("pthread_create clients");
                goto exit_client;
        }
 
@@ -4532,7 +4679,7 @@ int main(int argc, char **argv)
        ret = pthread_create(&dispatch_thread, NULL,
                        thread_dispatch_ust_registration, (void *) NULL);
        if (ret != 0) {
-               perror("pthread_create dispatch");
+               PERROR("pthread_create dispatch");
                goto exit_dispatch;
        }
 
@@ -4540,7 +4687,7 @@ int main(int argc, char **argv)
        ret = pthread_create(&reg_apps_thread, NULL,
                        thread_registration_apps, (void *) NULL);
        if (ret != 0) {
-               perror("pthread_create registration");
+               PERROR("pthread_create registration");
                goto exit_reg_apps;
        }
 
@@ -4548,7 +4695,7 @@ int main(int argc, char **argv)
        ret = pthread_create(&apps_thread, NULL,
                        thread_manage_apps, (void *) NULL);
        if (ret != 0) {
-               perror("pthread_create apps");
+               PERROR("pthread_create apps");
                goto exit_apps;
        }
 
@@ -4556,7 +4703,7 @@ int main(int argc, char **argv)
        ret = pthread_create(&kernel_thread, NULL,
                        thread_manage_kernel, (void *) NULL);
        if (ret != 0) {
-               perror("pthread_create kernel");
+               PERROR("pthread_create kernel");
                goto exit_kernel;
        }
 
@@ -4564,41 +4711,41 @@ int main(int argc, char **argv)
 
        ret = pthread_join(kernel_thread, &status);
        if (ret != 0) {
-               perror("pthread_join");
+               PERROR("pthread_join");
                goto error;     /* join error, exit without cleanup */
        }
 
 exit_kernel:
        ret = pthread_join(apps_thread, &status);
        if (ret != 0) {
-               perror("pthread_join");
+               PERROR("pthread_join");
                goto error;     /* join error, exit without cleanup */
        }
 
 exit_apps:
        ret = pthread_join(reg_apps_thread, &status);
        if (ret != 0) {
-               perror("pthread_join");
+               PERROR("pthread_join");
                goto error;     /* join error, exit without cleanup */
        }
 
 exit_reg_apps:
        ret = pthread_join(dispatch_thread, &status);
        if (ret != 0) {
-               perror("pthread_join");
+               PERROR("pthread_join");
                goto error;     /* join error, exit without cleanup */
        }
 
 exit_dispatch:
        ret = pthread_join(client_thread, &status);
        if (ret != 0) {
-               perror("pthread_join");
+               PERROR("pthread_join");
                goto error;     /* join error, exit without cleanup */
        }
 
        ret = join_consumer_thread(&kconsumer_data);
        if (ret != 0) {
-               perror("join_consumer");
+               PERROR("join_consumer");
                goto error;     /* join error, exit without cleanup */
        }
 
index a87d7075489d1cd640a5b91e23872abecb8f64fe..65032e41409637338eadea6a53951e42db4d0d9c 100644 (file)
@@ -101,7 +101,7 @@ void modprobe_remove_lttng_data(void)
                                "/sbin/modprobe -r -q %s",
                                kern_modules_list[i].name);
                if (ret < 0) {
-                       perror("snprintf modprobe -r");
+                       PERROR("snprintf modprobe -r");
                        goto error;
                }
                modprobe[sizeof(modprobe) - 1] = '\0';
@@ -182,7 +182,7 @@ int modprobe_lttng_data(void)
                                kern_modules_list[i].required ? "" : "-q ",
                                kern_modules_list[i].name);
                if (ret < 0) {
-                       perror("snprintf modprobe");
+                       PERROR("snprintf modprobe");
                        goto error;
                }
                modprobe[sizeof(modprobe) - 1] = '\0';
index e890d07bac3b4d359c788152ea244ca96582bb31..e7fc7c35ade4ee4fa32bd778b8a2e927583957aa 100644 (file)
@@ -177,7 +177,7 @@ int session_create(char *name, char *path, uid_t uid, gid_t gid)
        /* Allocate session data structure */
        new_session = zmalloc(sizeof(struct ltt_session));
        if (new_session == NULL) {
-               perror("zmalloc");
+               PERROR("zmalloc");
                ret = LTTCOMM_FATAL;
                goto error_malloc;
        }
index 0bf3ff58f0dd138e553f9c1dafaa38d418b1d09c..7f6040c4b65360c67df2a7b072ca47e66438705f 100644 (file)
@@ -55,7 +55,7 @@ static int get_wait_shm(char *shm_path, size_t mmap_size, int global)
                ret = chown(shm_path, 0, 0);
                if (ret < 0) {
                        if (errno != ENOENT) {
-                               perror("chown wait shm");
+                               PERROR("chown wait shm");
                                goto error;
                        }
                }
@@ -69,7 +69,7 @@ static int get_wait_shm(char *shm_path, size_t mmap_size, int global)
                ret = chown(shm_path, getuid(), getgid());
                if (ret < 0) {
                        if (errno != ENOENT) {
-                               perror("chown wait shm");
+                               PERROR("chown wait shm");
                                goto error;
                        }
                }
@@ -81,7 +81,7 @@ static int get_wait_shm(char *shm_path, size_t mmap_size, int global)
        ret = chmod(shm_path, mode);
        if (ret < 0) {
                if (errno != ENOENT) {
-                       perror("chmod wait shm");
+                       PERROR("chmod wait shm");
                        goto error;
                }
        }
@@ -101,21 +101,25 @@ static int get_wait_shm(char *shm_path, size_t mmap_size, int global)
         */
        wait_shm_fd = shm_open(shm_path, O_RDWR | O_CREAT, mode);
        if (wait_shm_fd < 0) {
-               perror("shm_open wait shm");
+               PERROR("shm_open wait shm");
                goto error;
        }
 
        ret = ftruncate(wait_shm_fd, mmap_size);
        if (ret < 0) {
-               perror("ftruncate wait shm");
+               PERROR("ftruncate wait shm");
                exit(EXIT_FAILURE);
        }
 
+#ifndef __FreeBSD__
        ret = fchmod(wait_shm_fd, mode);
        if (ret < 0) {
-               perror("fchmod");
+               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
 
        tracepoint(ust_notify_shm_stop);
 
@@ -157,7 +161,7 @@ char *shm_ust_get_mmap(char *shm_path, int global)
        /* close shm fd immediately after taking the mmap reference */
        ret = close(wait_shm_fd);
        if (ret) {
-               perror("Error closing fd");
+               PERROR("Error closing fd");
        }
 
        if (wait_shm_mmap == MAP_FAILED) {
index 36f7c9708a8f47e0fa33d9ce579a33c0bf607e08..7789d6c208a09fa8aa1fec59f7b5f2f6816bcecc 100644 (file)
@@ -91,23 +91,23 @@ struct ltt_kernel_session *trace_kernel_create_session(char *path)
        /* Allocate a new ltt kernel session */
        lks = zmalloc(sizeof(struct ltt_kernel_session));
        if (lks == NULL) {
-               perror("create kernel session zmalloc");
+               PERROR("create kernel session zmalloc");
                goto error;
        }
 
        /* Init data structure */
-       lks->fd = 0;
-       lks->metadata_stream_fd = 0;
+       lks->fd = -1;
+       lks->metadata_stream_fd = -1;
        lks->channel_count = 0;
        lks->stream_count_global = 0;
        lks->metadata = NULL;
-       lks->consumer_fd = 0;
+       lks->consumer_fd = -1;
        CDS_INIT_LIST_HEAD(&lks->channel_list.head);
 
        /* Set session path */
        ret = asprintf(&lks->trace_path, "%s/kernel", path);
        if (ret < 0) {
-               perror("asprintf kernel traces path");
+               PERROR("asprintf kernel traces path");
                goto error;
        }
 
@@ -129,18 +129,18 @@ struct ltt_kernel_channel *trace_kernel_create_channel(struct lttng_channel *cha
 
        lkc = zmalloc(sizeof(struct ltt_kernel_channel));
        if (lkc == NULL) {
-               perror("ltt_kernel_channel zmalloc");
+               PERROR("ltt_kernel_channel zmalloc");
                goto error;
        }
 
        lkc->channel = zmalloc(sizeof(struct lttng_channel));
        if (lkc->channel == NULL) {
-               perror("lttng_channel zmalloc");
+               PERROR("lttng_channel zmalloc");
                goto error;
        }
        memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
 
-       lkc->fd = 0;
+       lkc->fd = -1;
        lkc->stream_count = 0;
        lkc->event_count = 0;
        lkc->enabled = 1;
@@ -151,7 +151,7 @@ struct ltt_kernel_channel *trace_kernel_create_channel(struct lttng_channel *cha
        /* Set default trace output path */
        ret = asprintf(&lkc->pathname, "%s", path);
        if (ret < 0) {
-               perror("asprintf kernel create channel");
+               PERROR("asprintf kernel create channel");
                goto error;
        }
 
@@ -174,7 +174,7 @@ struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev)
        lke = zmalloc(sizeof(struct ltt_kernel_event));
        attr = zmalloc(sizeof(struct lttng_kernel_event));
        if (lke == NULL || attr == NULL) {
-               perror("kernel event zmalloc");
+               PERROR("kernel event zmalloc");
                goto error;
        }
 
@@ -221,7 +221,7 @@ struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev)
        attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
 
        /* Setting up a kernel event */
-       lke->fd = 0;
+       lke->fd = -1;
        lke->event = attr;
        lke->enabled = 1;
        lke->ctx = NULL;
@@ -246,7 +246,7 @@ struct ltt_kernel_metadata *trace_kernel_create_metadata(char *path)
        lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
        chan = zmalloc(sizeof(struct lttng_channel));
        if (lkm == NULL || chan == NULL) {
-               perror("kernel metadata zmalloc");
+               PERROR("kernel metadata zmalloc");
                goto error;
        }
 
@@ -259,12 +259,12 @@ struct ltt_kernel_metadata *trace_kernel_create_metadata(char *path)
        chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
 
        /* Init metadata */
-       lkm->fd = 0;
+       lkm->fd = -1;
        lkm->conf = chan;
        /* Set default metadata path */
        ret = asprintf(&lkm->pathname, "%s/metadata", path);
        if (ret < 0) {
-               perror("asprintf kernel metadata");
+               PERROR("asprintf kernel metadata");
                goto error;
        }
 
@@ -286,12 +286,12 @@ struct ltt_kernel_stream *trace_kernel_create_stream(void)
 
        lks = zmalloc(sizeof(struct ltt_kernel_stream));
        if (lks == NULL) {
-               perror("kernel stream zmalloc");
+               PERROR("kernel stream zmalloc");
                goto error;
        }
 
        /* Init stream */
-       lks->fd = 0;
+       lks->fd = -1;
        lks->pathname = NULL;
        lks->state = 0;
 
@@ -306,9 +306,16 @@ error:
  */
 void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
 {
+       int ret;
+
        DBG("[trace] Closing stream fd %d", stream->fd);
        /* Close kernel fd */
-       close(stream->fd);
+       if (stream->fd >= 0) {
+               ret = close(stream->fd);
+               if (ret) {
+                       PERROR("close");
+               }
+       }
        /* Remove from stream list */
        cds_list_del(&stream->list);
 
@@ -321,9 +328,18 @@ void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
  */
 void trace_kernel_destroy_event(struct ltt_kernel_event *event)
 {
-       DBG("[trace] Closing event fd %d", event->fd);
-       /* Close kernel fd */
-       close(event->fd);
+       int ret;
+
+       if (event->fd >= 0) {
+               DBG("[trace] Closing event fd %d", event->fd);
+               /* Close kernel fd */
+               ret = close(event->fd);
+               if (ret) {
+                       PERROR("close");
+               }
+       } else {
+               DBG("[trace] Tearing down event (no associated fd)");
+       }
 
        /* Remove from event list */
        cds_list_del(&event->list);
@@ -340,10 +356,16 @@ void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
 {
        struct ltt_kernel_stream *stream, *stmp;
        struct ltt_kernel_event *event, *etmp;
+       int ret;
 
        DBG("[trace] Closing channel fd %d", channel->fd);
        /* Close kernel fd */
-       close(channel->fd);
+       if (channel->fd >= 0) {
+               ret = close(channel->fd);
+               if (ret) {
+                       PERROR("close");
+               }
+       }
 
        /* For each stream in the channel list */
        cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
@@ -369,9 +391,16 @@ void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
  */
 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
 {
+       int ret;
+
        DBG("[trace] Closing metadata fd %d", metadata->fd);
        /* Close kernel fd */
-       close(metadata->fd);
+       if (metadata->fd >= 0) {
+               ret = close(metadata->fd);
+               if (ret) {
+                       PERROR("close");
+               }
+       }
 
        free(metadata->conf);
        free(metadata->pathname);
@@ -384,14 +413,23 @@ void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
 void trace_kernel_destroy_session(struct ltt_kernel_session *session)
 {
        struct ltt_kernel_channel *channel, *ctmp;
+       int ret;
 
        DBG("[trace] Closing session fd %d", session->fd);
        /* Close kernel fds */
-       close(session->fd);
+       if (session->fd >= 0) {
+               ret = close(session->fd);
+               if (ret) {
+                       PERROR("close");
+               }
+       }
 
-       if (session->metadata_stream_fd != 0) {
+       if (session->metadata_stream_fd >= 0) {
                DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
-               close(session->metadata_stream_fd);
+               ret = close(session->metadata_stream_fd);
+               if (ret) {
+                       PERROR("close");
+               }
        }
 
        if (session->metadata != NULL) {
index ce6332d931618178948c64095abe1264cbc35315..9c7cfb690221f11778196bb83fa98209162c4091 100644 (file)
@@ -139,7 +139,7 @@ struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
 
        luc = zmalloc(sizeof(struct ltt_ust_channel));
        if (luc == NULL) {
-               perror("ltt_ust_channel zmalloc");
+               PERROR("ltt_ust_channel zmalloc");
                goto error;
        }
 
@@ -171,7 +171,7 @@ struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
        /* Set trace output path */
        ret = snprintf(luc->pathname, PATH_MAX, "%s", path);
        if (ret < 0) {
-               perror("asprintf ust create channel");
+               PERROR("asprintf ust create channel");
                goto error_free_channel;
        }
 
@@ -236,7 +236,7 @@ struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev)
                lue->attr.loglevel = ev->loglevel;
                break;
        default:
-               ERR("Unknown ust loglevel type (%d)", ev->type);
+               ERR("Unknown ust loglevel type (%d)", ev->loglevel_type);
                goto error_free_event;
        }
 
@@ -270,7 +270,7 @@ struct ltt_ust_metadata *trace_ust_create_metadata(char *path)
 
        lum = zmalloc(sizeof(struct ltt_ust_metadata));
        if (lum == NULL) {
-               perror("ust metadata zmalloc");
+               PERROR("ust metadata zmalloc");
                goto error;
        }
 
@@ -286,7 +286,7 @@ struct ltt_ust_metadata *trace_ust_create_metadata(char *path)
        /* Set metadata trace path */
        ret = snprintf(lum->pathname, PATH_MAX, "%s/metadata", path);
        if (ret < 0) {
-               perror("asprintf ust metadata");
+               PERROR("asprintf ust metadata");
                goto error_free_metadata;
        }
 
index 030358241f663548265493bd2459c00977be0013..6e1dd60fd6b060a1437589a6594b30c0e69deefe 100644 (file)
@@ -198,7 +198,10 @@ void delete_ust_app(struct ust_app *app)
         * closing this socket, otherwise an application could re-use the socket ID
         * and race with the teardown, using the same hash table entry.
         */
-       close(sock);
+       ret = close(sock);
+       if (ret) {
+               PERROR("close");
+       }
 
        DBG2("UST app pid %d deleted", app->key.pid);
        free(app);
@@ -1268,20 +1271,27 @@ error:
 int ust_app_register(struct ust_register_msg *msg, int sock)
 {
        struct ust_app *lta;
+       int ret;
 
        if ((msg->bits_per_long == 64 && ust_consumerd64_fd == -EINVAL)
                        || (msg->bits_per_long == 32 && ust_consumerd32_fd == -EINVAL)) {
                ERR("Registration failed: application \"%s\" (pid: %d) has "
                        "%d-bit long, but no consumerd for this long size is available.\n",
                        msg->name, msg->pid, msg->bits_per_long);
-               close(sock);
+               ret = close(sock);
+               if (ret) {
+                       PERROR("close");
+               }
                return -EINVAL;
        }
        if (msg->major != LTTNG_UST_COMM_MAJOR) {
                ERR("Registration failed: application \"%s\" (pid: %d) has "
                        "communication protocol version %u.%u, but sessiond supports 2.x.\n",
                        msg->name, msg->pid, msg->major, msg->minor);
-               close(sock);
+               ret = close(sock);
+               if (ret) {
+                       PERROR("close");
+               }
                return -EINVAL;
        }
        lta = zmalloc(sizeof(struct ust_app));
index d758c8821cdb295ac4fa31cebad3aef13993ac3a..ee54e79850560d6f4866c177716aeddae3b542df 100644 (file)
@@ -58,13 +58,13 @@ static int send_channel_streams(int sock,
        DBG("Sending channel %d to consumer", lum.u.channel.channel_key);
        ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
        if (ret < 0) {
-               perror("send consumer channel");
+               PERROR("send consumer channel");
                goto error;
        }
        fd = uchan->obj->shm_fd;
        ret = lttcomm_send_fds_unix_sock(sock, &fd, 1);
        if (ret < 0) {
-               perror("send consumer channel ancillary data");
+               PERROR("send consumer channel ancillary data");
                goto error;
        }
 
@@ -91,7 +91,7 @@ static int send_channel_streams(int sock,
                DBG("Sending stream %d to consumer", lum.u.stream.stream_key);
                ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
                if (ret < 0) {
-                       perror("send consumer stream");
+                       PERROR("send consumer stream");
                        goto error;
                }
 
@@ -99,7 +99,7 @@ static int send_channel_streams(int sock,
                fds[1] = stream->obj->wait_fd;
                ret = lttcomm_send_fds_unix_sock(sock, fds, 2);
                if (ret < 0) {
-                       perror("send consumer stream ancillary data");
+                       PERROR("send consumer stream ancillary data");
                        goto error;
                }
        }
@@ -142,13 +142,13 @@ int ust_consumer_send_session(int consumer_fd, struct ust_app_session *usess)
                DBG("Sending metadata channel %d to consumer", lum.u.channel.channel_key);
                ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
                if (ret < 0) {
-                       perror("send consumer channel");
+                       PERROR("send consumer channel");
                        goto error;
                }
                fd = usess->metadata->obj->shm_fd;
                ret = lttcomm_send_fds_unix_sock(sock, &fd, 1);
                if (ret < 0) {
-                       perror("send consumer metadata channel");
+                       PERROR("send consumer metadata channel");
                        goto error;
                }
 
@@ -166,14 +166,14 @@ int ust_consumer_send_session(int consumer_fd, struct ust_app_session *usess)
                DBG("Sending metadata stream %d to consumer", lum.u.stream.stream_key);
                ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
                if (ret < 0) {
-                       perror("send consumer metadata stream");
+                       PERROR("send consumer metadata stream");
                        goto error;
                }
                fds[0] = usess->metadata->stream_obj->shm_fd;
                fds[1] = usess->metadata->stream_obj->wait_fd;
                ret = lttcomm_send_fds_unix_sock(sock, fds, 2);
                if (ret < 0) {
-                       perror("send consumer stream");
+                       PERROR("send consumer stream");
                        goto error;
                }
        }
index 1349c20a726f0cb2b51a88d25c113928522f147c..10b5d191e84dc068a0ed713760990ac4e798ce27 100644 (file)
@@ -26,6 +26,7 @@
 #include <sys/wait.h>
 #include <unistd.h>
 #include <config.h>
+#include <ctype.h>
 
 #include <lttng/lttng.h>
 #include <common/error.h>
@@ -34,9 +35,6 @@
 
 /* Variables */
 static char *progname;
-
-int opt_quiet;
-int opt_verbose;
 static int opt_no_sessiond;
 static char *opt_sessiond_path;
 static pid_t sessiond_pid;
@@ -431,10 +429,10 @@ static int parse_args(int argc, char **argv)
                        ret = 0;
                        goto end;
                case 'v':
-                       opt_verbose += 1;
+                       lttng_opt_verbose += 1;
                        break;
                case 'q':
-                       opt_quiet = 1;
+                       lttng_opt_quiet = 1;
                        break;
                case 'g':
                        lttng_set_tracing_group(optarg);
@@ -461,8 +459,8 @@ static int parse_args(int argc, char **argv)
        }
 
        /* If both options are specified, quiet wins */
-       if (opt_verbose && opt_quiet) {
-               opt_verbose = 0;
+       if (lttng_opt_verbose && lttng_opt_quiet) {
+               lttng_opt_verbose = 0;
        }
 
        /* Spawn session daemon if needed */
index 1f491db1b89e0095d33ce7ee603338f4f7f400c2..514bfe645a40363cb4cd6526b1c09c8e13d025b2 100644 (file)
@@ -16,6 +16,7 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+#define _GNU_SOURCE
 #include <stdlib.h>
 #include <ctype.h>
 
index 37f9acb9ab144b9a7ce711b4062d62a82bc78a4a..ba93810a8c3fe6af5bf8bd7c4188d037de4421ec 100644 (file)
@@ -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 (file)
index 0000000..c360508
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2011 - David Goulet <dgoulet@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.
+ */
+
+#ifndef _COMPAT_CLONE_H
+#define _COMPAT_CLONE_H
+
+#ifdef __linux__
+
+#include <sched.h>
+
+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 <unistd.h>
+
+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 */
index 77f5b922abcb7bc03c90ce8f574f5f4c94a9e6ed..008dba7028dff9d0756914d50c4a1baf7effdbe7 100644 (file)
@@ -15,6 +15,7 @@
  * Place - Suite 330, Boston, MA  02111-1307, USA.
  */
 
+#define _GNU_SOURCE
 #include <fcntl.h>
 #include <limits.h>
 #include <stdlib.h>
@@ -49,7 +50,7 @@ int compat_epoll_create(struct lttng_poll_event *events, int size, int flags)
        ret = epoll_create1(flags);
        if (ret < 0) {
                /* At this point, every error is fatal */
-               perror("epoll_create1");
+               PERROR("epoll_create1");
                goto error;
        }
 
@@ -58,7 +59,7 @@ int compat_epoll_create(struct lttng_poll_event *events, int size, int flags)
        /* This *must* be freed by using lttng_poll_free() */
        events->events = zmalloc(size * sizeof(struct epoll_event));
        if (events->events == NULL) {
-               perror("zmalloc epoll set");
+               PERROR("zmalloc epoll set");
                goto error_close;
        }
 
@@ -68,7 +69,10 @@ int compat_epoll_create(struct lttng_poll_event *events, int size, int flags)
        return 0;
 
 error_close:
-       close(events->epfd);
+       ret = close(events->epfd);
+       if (ret) {
+               PERROR("close");
+       }
 error:
        return -1;
 }
@@ -97,11 +101,11 @@ int compat_epoll_add(struct lttng_poll_event *events, int fd, uint32_t req_event
                        goto end;
                case ENOSPC:
                case EPERM:
-                       /* Print perror and goto end not failing. Show must go on. */
-                       perror("epoll_ctl ADD");
+                       /* Print PERROR and goto end not failing. Show must go on. */
+                       PERROR("epoll_ctl ADD");
                        goto end;
                default:
-                       perror("epoll_ctl ADD fatal");
+                       PERROR("epoll_ctl ADD fatal");
                        goto error;
                }
        }
@@ -112,7 +116,7 @@ int compat_epoll_add(struct lttng_poll_event *events, int fd, uint32_t req_event
                new_size = 2 * events->events_size;
                ptr = realloc(events->events, new_size * sizeof(struct epoll_event));
                if (ptr == NULL) {
-                       perror("realloc epoll add");
+                       PERROR("realloc epoll add");
                        goto error;
                }
                events->events = ptr;
@@ -142,14 +146,14 @@ int compat_epoll_del(struct lttng_poll_event *events, int fd)
                switch (errno) {
                case ENOENT:
                case EPERM:
-                       /* Print perror and goto end not failing. Show must go on. */
-                       perror("epoll_ctl DEL");
+                       /* Print PERROR and goto end not failing. Show must go on. */
+                       PERROR("epoll_ctl DEL");
                        goto end;
                default:
-                       perror("epoll_ctl DEL fatal");
+                       PERROR("epoll_ctl DEL fatal");
                        goto error;
                }
-               perror("epoll_ctl del");
+               PERROR("epoll_ctl del");
                goto error;
        }
 
@@ -180,7 +184,7 @@ int compat_epoll_wait(struct lttng_poll_event *events, int timeout)
        } while (ret == -1 && errno == EINTR);
        if (ret < 0) {
                /* At this point, every error is fatal */
-               perror("epoll_wait");
+               PERROR("epoll_wait");
                goto error;
        }
 
@@ -207,7 +211,7 @@ void compat_epoll_set_max_size(void)
 
        ret = read(fd, buf, sizeof(buf));
        if (ret < 0) {
-               perror("read set max size");
+               PERROR("read set max size");
                goto error;
        }
 
@@ -220,5 +224,8 @@ void compat_epoll_set_max_size(void)
        DBG("epoll set max size is %d", poll_max_size);
 
 error:
-       close(fd);
+       ret = close(fd);
+       if (ret) {
+               PERROR("close");
+       }
 }
diff --git a/src/common/compat/compat-fcntl.c b/src/common/compat/compat-fcntl.c
new file mode 100644 (file)
index 0000000..587df67
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2011 - David Goulet <dgoulet@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 <common/compat/fcntl.h>
+
+#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__ */
index 51bf22c865ddd6dff74359e39e54e5a8adf22e94..5aae4c4491bc95c8581204ef68326de98876f52d 100644 (file)
@@ -15,6 +15,7 @@
  * Place - Suite 330, Boston, MA  02111-1307, USA.
  */
 
+#define _GNU_SOURCE
 #include <stdlib.h>
 #include <sys/resource.h>
 #include <sys/time.h>
diff --git a/src/common/compat/endian.h b/src/common/compat/endian.h
new file mode 100644 (file)
index 0000000..b4426eb
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2011 - David Goulet <dgoulet@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.
+ */
+
+#ifdef _COMPAT_ENDIAN_H
+#define _COMPAT_ENDIAN_H
+
+#ifdef __linux__
+#include <endian.h>
+#elif defined(__FreeBSD__)
+#include <machine/endian.h>
+#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 (file)
index 0000000..f1c07a4
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2011 - David Goulet <dgoulet@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.
+ */
+
+#ifndef _COMPAT_FCNTL_H
+#define _COMPAT_FCNTL_H
+
+#include <fcntl.h>
+#include <sys/types.h>
+
+#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 <sys/errno.h>
+
+/*
+ * 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 (file)
index 0000000..7002e16
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2011 - David Goulet <dgoulet@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.
+ */
+
+#ifndef _COMPAT_MMAN_H
+#define _COMPAT_MMAN_H
+
+#include <sys/mman.h>
+
+#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 */
index 1580a4fb211a24def1146d7ec5c721431c34e3b4..e3a13cedecfeda3768b2df2718ccf0e5af3b4d04 100644 (file)
@@ -50,6 +50,7 @@ static inline void __lttng_poll_free(void *events)
  */
 #ifdef HAVE_EPOLL
 #include <sys/epoll.h>
+#include <stdio.h>
 
 /* See man epoll(7) for this define path */
 #define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
@@ -146,8 +147,13 @@ static inline void lttng_poll_reset(struct lttng_poll_event *events)
  */
 static inline void lttng_poll_clean(struct lttng_poll_event *events)
 {
+       int ret;
+
        if (events) {
-               close(events->epfd);
+               ret = close(events->epfd);
+               if (ret) {
+                       perror("close");
+               }
                __lttng_poll_free((void *) events->events);
        }
 }
@@ -179,10 +185,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 (file)
index 0000000..7356324
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2011 - David Goulet <dgoulet@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.
+ */
+
+#ifndef _COMPAT_SOCKET_H
+#define _COMPAT_SOCKET_H
+
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include <common/macros.h>
+
+#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 */
index ec59401053a18bf510c2748fff843b137e5ccee2..b1057aaeb139f1fda7681b076a96643f5604ff5f 100644 (file)
@@ -19,7 +19,6 @@
 
 #define _GNU_SOURCE
 #include <assert.h>
-#include <fcntl.h>
 #include <poll.h>
 #include <pthread.h>
 #include <stdlib.h>
@@ -86,9 +85,18 @@ static void consumer_steal_stream_key(int key)
 {
        struct lttng_consumer_stream *stream;
 
+       rcu_read_lock();
        stream = consumer_find_stream(key);
-       if (stream)
+       if (stream) {
                stream->key = -1;
+               /*
+                * We don't want the lookup to match, but we still need
+                * to iterate on this stream when iterating over the hash table. Just
+                * change the node key.
+                */
+               stream->node.key = -1;
+       }
+       rcu_read_unlock();
 }
 
 static struct lttng_consumer_channel *consumer_find_channel(int key)
@@ -119,9 +127,29 @@ static void consumer_steal_channel_key(int key)
 {
        struct lttng_consumer_channel *channel;
 
+       rcu_read_lock();
        channel = consumer_find_channel(key);
-       if (channel)
+       if (channel) {
                channel->key = -1;
+               /*
+                * We don't want the lookup to match, but we still need
+                * to iterate on this channel when iterating over the hash table. Just
+                * change the node key.
+                */
+               channel->node.key = -1;
+       }
+       rcu_read_unlock();
+}
+
+static
+void consumer_free_stream(struct rcu_head *head)
+{
+       struct lttng_ht_node_ulong *node =
+               caa_container_of(head, struct lttng_ht_node_ulong, head);
+       struct lttng_consumer_stream *stream =
+               caa_container_of(node, struct lttng_consumer_stream, node);
+
+       free(stream);
 }
 
 /*
@@ -156,11 +184,7 @@ void consumer_del_stream(struct lttng_consumer_stream *stream)
        }
 
        rcu_read_lock();
-
-       /* Get stream node from hash table */
-       lttng_ht_lookup(consumer_data.stream_ht,
-                       (void *)((unsigned long) stream->key), &iter);
-       /* Remove stream node from hash table */
+       iter.iter.node = &stream->node.node;
        ret = lttng_ht_del(consumer_data.stream_ht, &iter);
        assert(!ret);
 
@@ -174,17 +198,27 @@ void consumer_del_stream(struct lttng_consumer_stream *stream)
                goto end;
        }
        if (stream->out_fd >= 0) {
-               close(stream->out_fd);
+               ret = close(stream->out_fd);
+               if (ret) {
+                       PERROR("close");
+               }
        }
        if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) {
-               close(stream->wait_fd);
+               ret = close(stream->wait_fd);
+               if (ret) {
+                       PERROR("close");
+               }
        }
        if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) {
-               close(stream->shm_fd);
+               ret = close(stream->shm_fd);
+               if (ret) {
+                       PERROR("close");
+               }
        }
        if (!--stream->chan->refcount)
                free_chan = stream->chan;
-       free(stream);
+
+       call_rcu(&stream->node.head, consumer_free_stream);
 end:
        consumer_data.need_update = 1;
        pthread_mutex_unlock(&consumer_data.lock);
@@ -193,16 +227,6 @@ end:
                consumer_del_channel(free_chan);
 }
 
-static void consumer_del_stream_rcu(struct rcu_head *head)
-{
-       struct lttng_ht_node_ulong *node =
-               caa_container_of(head, struct lttng_ht_node_ulong, head);
-       struct lttng_consumer_stream *stream =
-               caa_container_of(node, struct lttng_consumer_stream, node);
-
-       consumer_del_stream(stream);
-}
-
 struct lttng_consumer_stream *consumer_allocate_stream(
                int channel_key, int stream_key,
                int shm_fd, int wait_fd,
@@ -301,6 +325,7 @@ int consumer_add_stream(struct lttng_consumer_stream *stream)
 
 end:
        pthread_mutex_unlock(&consumer_data.lock);
+
        return ret;
 }
 
@@ -321,6 +346,17 @@ void consumer_change_stream_state(int stream_key,
        pthread_mutex_unlock(&consumer_data.lock);
 }
 
+static
+void consumer_free_channel(struct rcu_head *head)
+{
+       struct lttng_ht_node_ulong *node =
+               caa_container_of(head, struct lttng_ht_node_ulong, head);
+       struct lttng_consumer_channel *channel =
+               caa_container_of(node, struct lttng_consumer_channel, node);
+
+       free(channel);
+}
+
 /*
  * Remove a channel from the global list protected by a mutex. This
  * function is also responsible for freeing its data structures.
@@ -346,12 +382,9 @@ void consumer_del_channel(struct lttng_consumer_channel *channel)
        }
 
        rcu_read_lock();
-
-       lttng_ht_lookup(consumer_data.channel_ht,
-                       (void *)((unsigned long) channel->key), &iter);
+       iter.iter.node = &channel->node.node;
        ret = lttng_ht_del(consumer_data.channel_ht, &iter);
        assert(!ret);
-
        rcu_read_unlock();
 
        if (channel->mmap_base != NULL) {
@@ -361,26 +394,23 @@ void consumer_del_channel(struct lttng_consumer_channel *channel)
                }
        }
        if (channel->wait_fd >= 0 && !channel->wait_fd_is_copy) {
-               close(channel->wait_fd);
+               ret = close(channel->wait_fd);
+               if (ret) {
+                       PERROR("close");
+               }
        }
        if (channel->shm_fd >= 0 && channel->wait_fd != channel->shm_fd) {
-               close(channel->shm_fd);
+               ret = close(channel->shm_fd);
+               if (ret) {
+                       PERROR("close");
+               }
        }
-       free(channel);
+
+       call_rcu(&channel->node.head, consumer_free_channel);
 end:
        pthread_mutex_unlock(&consumer_data.lock);
 }
 
-static void consumer_del_channel_rcu(struct rcu_head *head)
-{
-       struct lttng_ht_node_ulong *node =
-               caa_container_of(head, struct lttng_ht_node_ulong, head);
-       struct lttng_consumer_channel *channel=
-               caa_container_of(node, struct lttng_consumer_channel, node);
-
-       consumer_del_channel(channel);
-}
-
 struct lttng_consumer_channel *consumer_allocate_channel(
                int channel_key,
                int shm_fd, int wait_fd,
@@ -444,6 +474,7 @@ int consumer_add_channel(struct lttng_consumer_channel *channel)
        lttng_ht_add_unique_ulong(consumer_data.channel_ht, &channel->node);
        rcu_read_unlock();
        pthread_mutex_unlock(&consumer_data.lock);
+
        return 0;
 }
 
@@ -463,6 +494,7 @@ int consumer_update_poll_array(
        struct lttng_consumer_stream *stream;
 
        DBG("Updating poll fd array");
+       rcu_read_lock();
        cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, stream,
                        node.node) {
                if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM) {
@@ -474,6 +506,7 @@ int consumer_update_poll_array(
                local_stream[i] = stream;
                i++;
        }
+       rcu_read_unlock();
 
        /*
         * Insert the consumer_poll_pipe at the end of the array and don't
@@ -554,7 +587,6 @@ int lttng_consumer_send_error(
  */
 void lttng_consumer_cleanup(void)
 {
-       int ret;
        struct lttng_ht_iter iter;
        struct lttng_ht_node_ulong *node;
 
@@ -566,16 +598,16 @@ void lttng_consumer_cleanup(void)
         */
        cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, node,
                        node) {
-               ret = lttng_ht_del(consumer_data.stream_ht, &iter);
-               assert(!ret);
-               call_rcu(&node->head, consumer_del_stream_rcu);
+               struct lttng_consumer_stream *stream =
+                       caa_container_of(node, struct lttng_consumer_stream, node);
+               consumer_del_stream(stream);
        }
 
        cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, node,
                        node) {
-               ret = lttng_ht_del(consumer_data.channel_ht, &iter);
-               assert(!ret);
-               call_rcu(&node->head, consumer_del_channel_rcu);
+               struct lttng_consumer_channel *channel =
+                       caa_container_of(node, struct lttng_consumer_channel, node);
+               consumer_del_channel(channel);
        }
 
        rcu_read_unlock();
@@ -608,7 +640,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
@@ -647,7 +679,7 @@ void lttng_consumer_sync_trace_file(
  */
 struct lttng_consumer_local_data *lttng_consumer_create(
                enum lttng_consumer_type type,
-               int (*buffer_ready)(struct lttng_consumer_stream *stream,
+               ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
                        struct lttng_consumer_local_data *ctx),
                int (*recv_channel)(struct lttng_consumer_channel *channel),
                int (*recv_stream)(struct lttng_consumer_stream *stream),
@@ -699,14 +731,18 @@ error_thread_pipe:
                int err;
 
                err = close(ctx->consumer_should_quit[i]);
-               assert(!err);
+               if (err) {
+                       PERROR("close");
+               }
        }
 error_quit_pipe:
        for (i = 0; i < 2; i++) {
                int err;
 
                err = close(ctx->consumer_poll_pipe[i]);
-               assert(!err);
+               if (err) {
+                       PERROR("close");
+               }
        }
 error_poll_pipe:
        free(ctx);
@@ -719,13 +755,36 @@ error:
  */
 void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx)
 {
-       close(ctx->consumer_error_socket);
-       close(ctx->consumer_thread_pipe[0]);
-       close(ctx->consumer_thread_pipe[1]);
-       close(ctx->consumer_poll_pipe[0]);
-       close(ctx->consumer_poll_pipe[1]);
-       close(ctx->consumer_should_quit[0]);
-       close(ctx->consumer_should_quit[1]);
+       int ret;
+
+       ret = close(ctx->consumer_error_socket);
+       if (ret) {
+               PERROR("close");
+       }
+       ret = close(ctx->consumer_thread_pipe[0]);
+       if (ret) {
+               PERROR("close");
+       }
+       ret = close(ctx->consumer_thread_pipe[1]);
+       if (ret) {
+               PERROR("close");
+       }
+       ret = close(ctx->consumer_poll_pipe[0]);
+       if (ret) {
+               PERROR("close");
+       }
+       ret = close(ctx->consumer_poll_pipe[1]);
+       if (ret) {
+               PERROR("close");
+       }
+       ret = close(ctx->consumer_should_quit[0]);
+       if (ret) {
+               PERROR("close");
+       }
+       ret = close(ctx->consumer_should_quit[1]);
+       if (ret) {
+               PERROR("close");
+       }
        unlink(ctx->consumer_command_sock_path);
        free(ctx);
 }
@@ -735,7 +794,7 @@ void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx)
  *
  * Returns the number of bytes written
  */
-int lttng_consumer_on_read_subbuffer_mmap(
+ssize_t lttng_consumer_on_read_subbuffer_mmap(
                struct lttng_consumer_local_data *ctx,
                struct lttng_consumer_stream *stream, unsigned long len)
 {
@@ -749,6 +808,8 @@ int lttng_consumer_on_read_subbuffer_mmap(
                ERR("Unknown consumer_data type");
                assert(0);
        }
+
+       return 0;
 }
 
 /*
@@ -756,7 +817,7 @@ int lttng_consumer_on_read_subbuffer_mmap(
  *
  * Returns the number of bytes spliced.
  */
-int lttng_consumer_on_read_subbuffer_splice(
+ssize_t lttng_consumer_on_read_subbuffer_splice(
                struct lttng_consumer_local_data *ctx,
                struct lttng_consumer_stream *stream, unsigned long len)
 {
@@ -902,6 +963,10 @@ void *lttng_consumer_thread_poll_fds(void *data)
                }
                pthread_mutex_unlock(&consumer_data.lock);
 
+               /* No FDs and consumer_quit, consumer_cleanup the thread */
+               if (nb_fd == 0 && consumer_quit == 1) {
+                       goto end;
+               }
                /* poll on the array of fds */
        restart:
                DBG("polling on %d fd", nb_fd + 1);
@@ -922,11 +987,6 @@ void *lttng_consumer_thread_poll_fds(void *data)
                        goto end;
                }
 
-               /* No FDs and consumer_quit, consumer_cleanup the thread */
-               if (nb_fd == 0 && consumer_quit == 1) {
-                       goto end;
-               }
-
                /*
                 * If the consumer_poll_pipe triggered poll go
                 * directly to the beginning of the loop to update the
@@ -945,69 +1005,82 @@ void *lttng_consumer_thread_poll_fds(void *data)
                /* Take care of high priority channels first. */
                for (i = 0; i < nb_fd; i++) {
                        if (pollfd[i].revents & POLLPRI) {
+                               ssize_t len;
+
                                DBG("Urgent read on fd %d", pollfd[i].fd);
                                high_prio = 1;
-                               ret = ctx->on_buffer_ready(local_stream[i], ctx);
+                               len = ctx->on_buffer_ready(local_stream[i], ctx);
                                /* it's ok to have an unavailable sub-buffer */
-                               if (ret == EAGAIN) {
-                                       ret = 0;
-                               }
-                       } else if (pollfd[i].revents & POLLERR) {
-                               ERR("Error returned in polling fd %d.", pollfd[i].fd);
-                               rcu_read_lock();
-                               consumer_del_stream_rcu(&local_stream[i]->node.head);
-                               rcu_read_unlock();
-                               num_hup++;
-                       } else if (pollfd[i].revents & POLLNVAL) {
-                               ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
-                               rcu_read_lock();
-                               consumer_del_stream_rcu(&local_stream[i]->node.head);
-                               rcu_read_unlock();
-                               num_hup++;
-                       } else if ((pollfd[i].revents & POLLHUP) &&
-                                       !(pollfd[i].revents & POLLIN)) {
-                               if (consumer_data.type == LTTNG_CONSUMER32_UST
-                                               || consumer_data.type == LTTNG_CONSUMER64_UST) {
-                                       DBG("Polling fd %d tells it has hung up. Attempting flush and read.",
-                                               pollfd[i].fd);
-                                       if (!local_stream[i]->hangup_flush_done) {
-                                               lttng_ustconsumer_on_stream_hangup(local_stream[i]);
-                                               /* read after flush */
-                                               do {
-                                                       ret = ctx->on_buffer_ready(local_stream[i], ctx);
-                                               } while (ret == EAGAIN);
-                                       }
-                               } else {
-                                       DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
+                               if (len < 0 && len != -EAGAIN) {
+                                       goto end;
+                               } else if (len > 0) {
+                                       local_stream[i]->data_read = 1;
                                }
-                               rcu_read_lock();
-                               consumer_del_stream_rcu(&local_stream[i]->node.head);
-                               rcu_read_unlock();
-                               num_hup++;
                        }
                }
 
-               /* If every buffer FD has hung up, we end the read loop here */
-               if (nb_fd > 0 && num_hup == nb_fd) {
-                       DBG("every buffer FD has hung up\n");
-                       if (consumer_quit == 1) {
-                               goto end;
-                       }
+               /*
+                * If we read high prio channel in this loop, try again
+                * for more high prio data.
+                */
+               if (high_prio) {
                        continue;
                }
 
                /* Take care of low priority channels. */
-               if (high_prio == 0) {
-                       for (i = 0; i < nb_fd; i++) {
-                               if (pollfd[i].revents & POLLIN) {
-                                       DBG("Normal read on fd %d", pollfd[i].fd);
-                                       ret = ctx->on_buffer_ready(local_stream[i], ctx);
-                                       /* it's ok to have an unavailable subbuffer */
-                                       if (ret == EAGAIN) {
-                                               ret = 0;
-                                       }
+               for (i = 0; i < nb_fd; i++) {
+                       if ((pollfd[i].revents & POLLIN) ||
+                                       local_stream[i]->hangup_flush_done) {
+                               ssize_t len;
+
+                               DBG("Normal read on fd %d", pollfd[i].fd);
+                               len = ctx->on_buffer_ready(local_stream[i], ctx);
+                               /* it's ok to have an unavailable sub-buffer */
+                               if (len < 0 && len != -EAGAIN) {
+                                       goto end;
+                               } else if (len > 0) {
+                                       local_stream[i]->data_read = 1;
+                               }
+                       }
+               }
+
+               /* Handle hangup and errors */
+               for (i = 0; i < nb_fd; i++) {
+                       if (!local_stream[i]->hangup_flush_done
+                                       && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL))
+                                       && (consumer_data.type == LTTNG_CONSUMER32_UST
+                                               || consumer_data.type == LTTNG_CONSUMER64_UST)) {
+                               DBG("fd %d is hup|err|nval. Attempting flush and read.",
+                                       pollfd[i].fd);
+                               lttng_ustconsumer_on_stream_hangup(local_stream[i]);
+                               /* Attempt read again, for the data we just flushed. */
+                               local_stream[i]->data_read = 1;
+                       }
+                       /*
+                        * If the poll flag is HUP/ERR/NVAL and we have
+                        * read no data in this pass, we can remove the
+                        * stream from its hash table.
+                        */
+                       if ((pollfd[i].revents & POLLHUP)) {
+                               DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
+                               if (!local_stream[i]->data_read) {
+                                       consumer_del_stream(local_stream[i]);
+                                       num_hup++;
+                               }
+                       } else if (pollfd[i].revents & POLLERR) {
+                               ERR("Error returned in polling fd %d.", pollfd[i].fd);
+                               if (!local_stream[i]->data_read) {
+                                       consumer_del_stream(local_stream[i]);
+                                       num_hup++;
+                               }
+                       } else if (pollfd[i].revents & POLLNVAL) {
+                               ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
+                               if (!local_stream[i]->data_read) {
+                                       consumer_del_stream(local_stream[i]);
+                                       num_hup++;
                                }
                        }
+                       local_stream[i]->data_read = 0;
                }
        }
 end:
@@ -1139,7 +1212,7 @@ end:
        return NULL;
 }
 
-int lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
+ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
                struct lttng_consumer_local_data *ctx)
 {
        switch (consumer_data.type) {
index dc5fc9968be02fbf45b223ef20a8371fdc4e0005..71ae39903a8477c341aff3c322acf6602f049604 100644 (file)
@@ -26,7 +26,8 @@
 
 #include <lttng/lttng.h>
 
-#include "src/common/hashtable/hashtable.h"
+#include <common/hashtable/hashtable.h>
+#include <common/compat/fcntl.h>
 
 /*
  * When the receiving thread dies, we need to have a way to make the polling
@@ -113,6 +114,7 @@ struct lttng_consumer_stream {
        /* For UST */
        struct lttng_ust_lib_ring_buffer *buf;
        int cpu;
+       int data_read;
        int hangup_flush_done;
        /* UID/GID of the user owning the session to which stream belongs */
        uid_t uid;
@@ -124,8 +126,11 @@ struct lttng_consumer_stream {
  * process.
  */
 struct lttng_consumer_local_data {
-       /* function to call when data is available on a buffer */
-       int (*on_buffer_ready)(struct lttng_consumer_stream *stream,
+       /*
+        * Function to call when data is available on a buffer.
+        * Returns the number of bytes read, or negative error value.
+        */
+       ssize_t (*on_buffer_ready)(struct lttng_consumer_stream *stream,
                        struct lttng_consumer_local_data *ctx);
        /*
         * function to call when we receive a new channel, it receives a
@@ -284,16 +289,16 @@ int consumer_add_channel(struct lttng_consumer_channel *channel);
 
 extern struct lttng_consumer_local_data *lttng_consumer_create(
                enum lttng_consumer_type type,
-               int (*buffer_ready)(struct lttng_consumer_stream *stream,
+               ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
                        struct lttng_consumer_local_data *ctx),
                int (*recv_channel)(struct lttng_consumer_channel *channel),
                int (*recv_stream)(struct lttng_consumer_stream *stream),
                int (*update_stream)(int sessiond_key, uint32_t state));
 extern void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx);
-extern int lttng_consumer_on_read_subbuffer_mmap(
+extern ssize_t lttng_consumer_on_read_subbuffer_mmap(
                struct lttng_consumer_local_data *ctx,
                struct lttng_consumer_stream *stream, unsigned long len);
-extern int lttng_consumer_on_read_subbuffer_splice(
+extern ssize_t lttng_consumer_on_read_subbuffer_splice(
                struct lttng_consumer_local_data *ctx,
                struct lttng_consumer_stream *stream, unsigned long len);
 extern int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
@@ -307,7 +312,7 @@ extern void *lttng_consumer_thread_receive_fds(void *data);
 extern int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
                int sock, struct pollfd *consumer_sockpoll);
 
-int lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
+ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
                struct lttng_consumer_local_data *ctx);
 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream);
 
index 81c20223d124282b59bb8ea23801cd714156f441..f927078f17726f4e4bb4a755ce16c73e3ab93caa 100644 (file)
 #include <stdio.h>
 #include <string.h>
 
+#ifndef _GNU_SOURCE
+#error "lttng-tools error.h needs _GNU_SOURCE"
+#endif
+
 /* Stringify the expansion of a define */
 #define XSTR(d) STR(d)
 #define STR(s) #s
 
-extern int opt_quiet;
-extern int opt_verbose;
+extern int lttng_opt_quiet;
+extern int lttng_opt_verbose;
 
 #define PRINT_ERR   0x1
 #define PRINT_WARN  0x2
@@ -40,21 +44,22 @@ extern int opt_verbose;
 /*
  * Macro for printing message depending on command line option and verbosity.
  */
-#define __lttng_print(type, fmt, args...)                                 \
-       do {                                                                  \
-               if (opt_quiet == 0) {                                             \
-                       if (type == PRINT_MSG) {                                      \
-                               fprintf(stdout, fmt, ## args);                            \
-                       } else if (((type & PRINT_DBG) && opt_verbose == 1) ||        \
-                                       ((type & (PRINT_DBG | PRINT_DBG2)) &&                 \
-                                               opt_verbose == 2) ||                              \
-                                       ((type & (PRINT_DBG | PRINT_DBG2 | PRINT_DBG3)) &&    \
-                                               opt_verbose == 3)) {                              \
-                               fprintf(stderr, fmt, ## args);                            \
-                       } else if (type & (PRINT_ERR | PRINT_WARN | PRINT_BUG)) {     \
-                               fprintf(stderr, fmt, ## args);                            \
-                       }                                                             \
-               }                                                                 \
+#define __lttng_print(type, fmt, args...)                           \
+       do {                                                            \
+               if (lttng_opt_quiet == 0 && type == PRINT_MSG) {            \
+                       fprintf(stdout, fmt, ## args);                          \
+               } else if (lttng_opt_quiet == 0 &&                          \
+                               (((type & PRINT_DBG) && lttng_opt_verbose == 1) ||  \
+                               ((type & (PRINT_DBG | PRINT_DBG2)) &&               \
+                                       lttng_opt_verbose == 2) ||                      \
+                               ((type & (PRINT_DBG | PRINT_DBG2 | PRINT_DBG3)) &&  \
+                                       lttng_opt_verbose == 3))) {                     \
+                       fprintf(stderr, fmt, ## args);                          \
+               } else if (lttng_opt_quiet == 0 && (type & (PRINT_WARN))) { \
+                       fprintf(stderr, fmt, ## args);                          \
+               } else if (type & (PRINT_ERR | PRINT_BUG)) {                \
+                       fprintf(stderr, fmt, ## args);                          \
+               }                                                           \
        } while (0);
 
 #define MSG(fmt, args...) \
@@ -75,14 +80,31 @@ extern int opt_verbose;
                " [in %s() at " __FILE__ ":" XSTR(__LINE__) "]\n", ## args, __func__)
 
 #define _PERROR(fmt, args...) \
-       __lttng_print(PRINT_ERR, "perror " fmt "\n", ## args)
+       __lttng_print(PRINT_ERR, "PERROR: " fmt \
+               " [in %s() at " __FILE__ ":" XSTR(__LINE__) "]\n", ## args, __func__)
+
+#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 */
index 2080107005739f5d4cb61c5f9761a14d7b375998..8d63e8afffdc60c837514359fe6b9383a27647e9 100644 (file)
@@ -199,6 +199,28 @@ void lttng_ht_add_unique_ulong(struct lttng_ht *ht,
        assert(node_ptr == &node->node);
 }
 
+/*
+ * Add replace unsigned long node to hashtable.
+ */
+struct lttng_ht_node_ulong *lttng_ht_add_replace_ulong(struct lttng_ht *ht,
+               struct lttng_ht_node_ulong *node)
+{
+       struct cds_lfht_node *node_ptr;
+       assert(ht);
+       assert(ht->ht);
+       assert(node);
+
+       node_ptr = cds_lfht_add_replace(ht->ht,
+                       ht->hash_fct((void *) node->key, HASH_SEED), ht->match_fct,
+                       (void *) node->key, &node->node);
+       if (!node_ptr) {
+               return NULL;
+       } else {
+               return caa_container_of(node_ptr, struct lttng_ht_node_ulong, node);
+       }
+       assert(node_ptr == &node->node);
+}
+
 /*
  * Delete node from hashtable.
  */
index 1c2889d3e7e09be94d75bf1800197f6a1373ec08..d1dcdb574d9869d116fb43ca648cf4022b3f574c 100644 (file)
@@ -72,6 +72,8 @@ extern void lttng_ht_add_unique_str(struct lttng_ht *ht,
                struct lttng_ht_node_str *node);
 extern void lttng_ht_add_unique_ulong(struct lttng_ht *ht,
                struct lttng_ht_node_ulong *node);
+struct lttng_ht_node_ulong *lttng_ht_add_replace_ulong(struct lttng_ht *ht,
+               struct lttng_ht_node_ulong *node);
 
 extern int lttng_ht_del(struct lttng_ht *ht, struct lttng_ht_iter *iter);
 
index 4554ed6007f20df68bfac3f2fe74a5a9e39c55fc..b136e1ae0eb73b46d2035cf2bbd9e470f96861d3 100644 (file)
 #include <sys/mman.h>
 #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)
 {
index 50cd1ba959cd8dbb838b9785deaa83ffae8e4501..01cc72b3b4f8e5151a815e7461724160331ba82a 100644 (file)
@@ -50,7 +50,6 @@
  */
 
 #include <assert.h>
-#include <endian.h>    /* attempt to define endianness */
 #include <stdint.h>     /* defines uint32_t etc */
 #include <stdio.h>      /* defines printf for tests */
 #include <string.h>
@@ -59,6 +58,7 @@
 #include <urcu/compiler.h>
 
 #include "utils.h"
+#include <common/compat/endian.h>    /* attempt to define endianness */
 
 /*
  * My best guess at if you are big-endian or little-endian.  This may
index bb95529530161b4a45152609a67f8f018eeca6bd..823b4a28aa9df3934df86e55b09c3469857631fc 100644 (file)
@@ -19,7 +19,6 @@
 
 #define _GNU_SOURCE
 #include <assert.h>
-#include <fcntl.h>
 #include <poll.h>
 #include <pthread.h>
 #include <stdlib.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <sys/stat.h>
 
 #include <common/common.h>
 #include <common/kernel-ctl/kernel-ctl.h>
 #include <common/sessiond-comm/sessiond-comm.h>
+#include <common/compat/fcntl.h>
 
 #include "kernel-consumer.h"
 
@@ -44,12 +45,12 @@ extern volatile int consumer_quit;
  *
  * Returns the number of bytes written
  */
-int lttng_kconsumer_on_read_subbuffer_mmap(
+ssize_t lttng_kconsumer_on_read_subbuffer_mmap(
                struct lttng_consumer_local_data *ctx,
                struct lttng_consumer_stream *stream, unsigned long len)
 {
        unsigned long mmap_offset;
-       long ret = 0;
+       ssize_t ret = 0;
        off_t orig_offset = stream->out_fd_offset;
        int fd = stream->wait_fd;
        int outfd = stream->out_fd;
@@ -72,7 +73,7 @@ int 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;
        }
@@ -90,11 +91,11 @@ end:
  *
  * Returns the number of bytes spliced.
  */
-int lttng_kconsumer_on_read_subbuffer_splice(
+ssize_t lttng_kconsumer_on_read_subbuffer_splice(
                struct lttng_consumer_local_data *ctx,
                struct lttng_consumer_stream *stream, unsigned long len)
 {
-       long ret = 0;
+       ssize_t ret = 0;
        loff_t offset = 0;
        off_t orig_offset = stream->out_fd_offset;
        int fd = stream->wait_fd;
@@ -105,7 +106,7 @@ int lttng_kconsumer_on_read_subbuffer_splice(
                                (unsigned long)offset, fd);
                ret = splice(fd, &offset, ctx->consumer_thread_pipe[1], NULL, len,
                                SPLICE_F_MOVE | SPLICE_F_MORE);
-               DBG("splice chan to pipe ret %ld", ret);
+               DBG("splice chan to pipe ret %zd", ret);
                if (ret < 0) {
                        errno = -ret;
                        perror("Error in relay splice");
@@ -114,7 +115,7 @@ int lttng_kconsumer_on_read_subbuffer_splice(
 
                ret = splice(ctx->consumer_thread_pipe[0], NULL, outfd, NULL, ret,
                                SPLICE_F_MOVE | SPLICE_F_MORE);
-               DBG("splice pipe to file %ld", ret);
+               DBG("splice pipe to file %zd", ret);
                if (ret < 0) {
                        errno = -ret;
                        perror("Error in file splice");
@@ -122,7 +123,7 @@ int 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;
        }
@@ -307,12 +308,12 @@ end_nosignal:
 /*
  * Consume data on a file descriptor and write it on a trace file.
  */
-int lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
+ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
                struct lttng_consumer_local_data *ctx)
 {
        unsigned long len;
        int err;
-       long ret = 0;
+       ssize_t ret = 0;
        int infd = stream->wait_fd;
 
        DBG("In read_subbuffer (infd : %d)", infd);
index 6e820044bad2ea6618786ff4673bd009f405d199..f4f46169de2a15bf9548df2d8f554a43681ceef9 100644 (file)
 /*
  * Mmap the ring buffer, read it and write the data to the tracefile.
  *
- * Returns the number of bytes written.
+ * Returns the number of bytes written, or negative value on error.
  */
-extern int lttng_kconsumer_on_read_subbuffer_mmap(
+extern ssize_t lttng_kconsumer_on_read_subbuffer_mmap(
                struct lttng_consumer_local_data *ctx,
                struct lttng_consumer_stream *stream, unsigned long len);
 
 /*
  * Splice the data from the ring buffer to the tracefile.
  *
- * Returns the number of bytes spliced.
+ * Returns the number of bytes spliced, or negative error value on
+ * error.
  */
-extern int lttng_kconsumer_on_read_subbuffer_splice(
+extern ssize_t lttng_kconsumer_on_read_subbuffer_splice(
                struct lttng_consumer_local_data *ctx,
                struct lttng_consumer_stream *stream, unsigned long len);
 
@@ -62,7 +63,7 @@ int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
                int sock, struct pollfd *consumer_sockpoll);
 
 
-int lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
+ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
                struct lttng_consumer_local_data *ctx);
 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream);
 
index 745a6d0d13643d2333ee1bfe5c8b3e4c8fe4fb3b..e0c317c4e40347fb7001b74fb9900e9bcfafe32f 100644 (file)
 #include <unistd.h>
 #include <fcntl.h>
 #include <sched.h>
-#include <sys/mman.h>
+#include <sys/signal.h>
 
 #include <common/error.h>
+#include <common/compat/mman.h>
+#include <common/compat/clone.h>
 
 #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 {
@@ -154,14 +167,14 @@ int child_run_as(void *_data)
        if (data->gid != getegid()) {
                ret = setegid(data->gid);
                if (ret < 0) {
-                       perror("setegid");
+                       PERROR("setegid");
                        return EXIT_FAILURE;
                }
        }
        if (data->uid != geteuid()) {
                ret = seteuid(data->uid);
                if (ret < 0) {
-                       perror("seteuid");
+                       PERROR("seteuid");
                        return EXIT_FAILURE;
                }
        }
@@ -177,7 +190,7 @@ int child_run_as(void *_data)
                writelen = write(data->retval_pipe, &sendret.c[index],
                                writeleft);
                if (writelen < 0) {
-                       perror("write");
+                       PERROR("write");
                        return EXIT_FAILURE;
                }
                writeleft -= writelen;
@@ -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;
@@ -214,7 +227,7 @@ int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
 
        ret = pipe(retval_pipe);
        if (ret < 0) {
-               perror("pipe");
+               PERROR("pipe");
                retval.i = ret;
                goto end;
        }
@@ -225,10 +238,10 @@ 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");
+               PERROR("mmap");
                retval.i = -ENOMEM;
                goto close_pipe;
        }
@@ -236,11 +249,10 @@ 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");
+               PERROR("clone");
                retval.i = pid;
                goto unmap_stack;
        }
@@ -250,7 +262,7 @@ int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
        do {
                readlen = read(retval_pipe[0], &retval.c[index], readleft);
                if (readlen < 0) {
-                       perror("read");
+                       PERROR("read");
                        ret = -1;
                        break;
                }
@@ -264,22 +276,51 @@ int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
         */
        pid = waitpid(pid, &status, 0);
        if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
-               perror("wait");
+               PERROR("wait");
                retval.i = -1;
        }
 unmap_stack:
        ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE);
        if (ret < 0) {
-               perror("munmap");
+               PERROR("munmap");
                retval.i = ret;
        }
 close_pipe:
-       close(retval_pipe[0]);
-       close(retval_pipe[1]);
+       ret = close(retval_pipe[0]);
+       if (ret) {
+               PERROR("close");
+       }
+       ret = close(retval_pipe[1]);
+       if (ret) {
+               PERROR("close");
+       }
 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;
index 0c29003a6267a382b204a6f277a9d0a1468ad182..5f086141dd7b2af44954c9ab93edf0078fdcddd2 100644 (file)
 #include <string.h>
 #include <sys/stat.h>
 #include <sys/types.h>
-#include <sys/un.h>
 #include <unistd.h>
 #include <errno.h>
 
 #include <common/defaults.h>
+#include <common/error.h>
 
 #include "sessiond-comm.h"
 
@@ -101,6 +101,10 @@ static const char *lttcomm_readable_code[] = {
        [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_EVENT_NOT_FOUND)] = "UST event not found",
        [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_EXIST)] = "UST context already exist",
        [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_INVAL)] = "UST invalid context",
+       [ LTTCOMM_ERR_INDEX(LTTCOMM_NEED_ROOT_SESSIOND) ] = "Tracing the kernel requires a root lttng-sessiond daemon and \"tracing\" group user membership",
+       [ LTTCOMM_ERR_INDEX(LTTCOMM_TRACE_ALREADY_STARTED) ] = "Tracing already started",
+       [ LTTCOMM_ERR_INDEX(LTTCOMM_TRACE_ALREADY_STOPPED) ] = "Tracing already stopped",
+
        [ LTTCOMM_ERR_INDEX(CONSUMERD_COMMAND_SOCK_READY) ] = "consumerd command socket ready",
        [ LTTCOMM_ERR_INDEX(CONSUMERD_SUCCESS_RECV_FD) ] = "consumerd success on receiving fds",
        [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_FD) ] = "consumerd error on receiving fds",
@@ -116,6 +120,7 @@ static const char *lttcomm_readable_code[] = {
        [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ENOMEM) ] = "consumerd splice ENOMEM",
        [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ESPIPE) ] = "consumerd splice ESPIPE",
        [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_EVENT) ] = "Event not found",
+       [ LTTCOMM_ERR_INDEX(LTTCOMM_INVALID) ] = "Invalid parameter",
 };
 
 /*
@@ -141,12 +146,11 @@ const char *lttcomm_get_readable_code(enum lttcomm_return_code code)
 int lttcomm_connect_unix_sock(const char *pathname)
 {
        struct sockaddr_un sun;
-       int fd;
-       int ret;
+       int fd, ret, closeret;
 
        fd = socket(PF_UNIX, SOCK_STREAM, 0);
        if (fd < 0) {
-               perror("socket");
+               PERROR("socket");
                ret = fd;
                goto error;
        }
@@ -168,7 +172,10 @@ int lttcomm_connect_unix_sock(const char *pathname)
        return fd;
 
 error_connect:
-       close(fd);
+       closeret = close(fd);
+       if (closeret) {
+               PERROR("close");
+       }
 error:
        return ret;
 }
@@ -186,7 +193,7 @@ int lttcomm_accept_unix_sock(int sock)
        /* Blocking call */
        new_fd = accept(sock, (struct sockaddr *) &sun, &len);
        if (new_fd < 0) {
-               perror("accept");
+               PERROR("accept");
        }
 
        return new_fd;
@@ -204,7 +211,7 @@ int lttcomm_create_unix_sock(const char *pathname)
 
        /* Create server socket */
        if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
-               perror("socket");
+               PERROR("socket");
                goto error;
        }
 
@@ -217,7 +224,7 @@ int lttcomm_create_unix_sock(const char *pathname)
        (void) unlink(pathname);
        ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun));
        if (ret < 0) {
-               perror("bind");
+               PERROR("bind");
                goto error;
        }
 
@@ -236,7 +243,7 @@ int lttcomm_listen_unix_sock(int sock)
 
        ret = listen(sock, LTTNG_SESSIOND_COMM_MAX_LISTEN);
        if (ret < 0) {
-               perror("listen");
+               PERROR("listen");
        }
 
        return ret;
@@ -261,9 +268,11 @@ ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len)
        msg.msg_iov = iov;
        msg.msg_iovlen = 1;
 
-       ret = recvmsg(sock, &msg, MSG_WAITALL);
+       do {
+               ret = recvmsg(sock, &msg, MSG_WAITALL);
+       } while (ret < 0 && errno == EINTR);
        if (ret < 0) {
-               perror("recvmsg");
+               PERROR("recvmsg");
        }
 
        return ret;
@@ -289,7 +298,13 @@ ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len)
 
        ret = sendmsg(sock, &msg, 0);
        if (ret < 0) {
-               perror("sendmsg");
+               /*
+                * Only warn about EPIPE when quiet mode is deactivated.
+                * We consider EPIPE as expected.
+                */
+               if (errno != EPIPE || !lttng_opt_quiet) {
+                       PERROR("sendmsg");
+               }
        }
 
        return ret;
@@ -300,15 +315,18 @@ ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len)
  */
 int lttcomm_close_unix_sock(int sock)
 {
-       int ret;
+       int ret, closeret;
 
        /* Shutdown receptions and transmissions */
        ret = shutdown(sock, SHUT_RDWR);
        if (ret < 0) {
-               perror("shutdown");
+               PERROR("shutdown");
        }
 
-       close(sock);
+       closeret = close(sock);
+       if (closeret) {
+               PERROR("close");
+       }
 
        return ret;
 }
@@ -351,7 +369,13 @@ ssize_t lttcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd)
 
        ret = sendmsg(sock, &msg, 0);
        if (ret < 0) {
-               perror("sendmsg");
+               /*
+                * Only warn about EPIPE when quiet mode is deactivated.
+                * We consider EPIPE as expected.
+                */
+               if (errno != EPIPE || !lttng_opt_quiet) {
+                       PERROR("sendmsg");
+               }
        }
        return ret;
 }
@@ -384,9 +408,11 @@ ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
        msg.msg_control = recv_fd;
        msg.msg_controllen = sizeof(recv_fd);
 
-       ret = recvmsg(sock, &msg, 0);
+       do {
+               ret = recvmsg(sock, &msg, 0);
+       } while (ret < 0 && errno == EINTR);
        if (ret < 0) {
-               perror("recvmsg fds");
+               PERROR("recvmsg fds");
                goto end;
        }
        if (ret != 1) {
@@ -412,7 +438,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 +456,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,25 +472,32 @@ 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) {
-               perror("sendmsg");
+               /*
+                * Only warn about EPIPE when quiet mode is deactivated.
+                * We consider EPIPE as expected.
+                */
+               if (errno != EPIPE || !lttng_opt_quiet) {
+                       PERROR("sendmsg");
+               }
        }
-
        return ret;
 }
 
@@ -472,14 +507,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,15 +532,20 @@ 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);
+       do {
+               ret = recvmsg(sock, &msg, 0);
+       } while (ret < 0 && errno == EINTR);
        if (ret < 0) {
-               perror("recvmsg fds");
+               PERROR("recvmsg fds");
                goto end;
        }
 
+#ifdef __linux__
        if (msg.msg_flags & MSG_CTRUNC) {
                fprintf(stderr, "Error: Control message truncated.\n");
                ret = -1;
@@ -518,7 +560,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 +568,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 +594,7 @@ end:
 /*
  * Set socket option to use credentials passing.
  */
+#ifdef __linux__
 int lttcomm_setsockopt_creds_unix_sock(int sock)
 {
        int ret, on = 1;
@@ -547,8 +602,15 @@ int lttcomm_setsockopt_creds_unix_sock(int sock)
        /* Set socket for credentials retrieval */
        ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
        if (ret < 0) {
-               perror("setsockopt creds unix sock");
+               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__ */
index 31b7fe4661cbd1db37acb52013d31048b873df2e..b98f9fc228fa7dedfee2feab8f28e4eec8de51ba 100644 (file)
@@ -28,7 +28,7 @@
 #define _GNU_SOURCE
 #include <limits.h>
 #include <lttng/lttng.h>
-#include <sys/socket.h>
+#include <common/compat/socket.h>
 
 /* Queue size of listen(2) */
 #define LTTNG_SESSIOND_COMM_MAX_LISTEN 64
@@ -136,6 +136,9 @@ enum lttcomm_return_code {
        LTTCOMM_UST_EVENT_NOT_FOUND,    /* UST event not found */
        LTTCOMM_UST_CONTEXT_EXIST,      /* UST context exist */
        LTTCOMM_UST_CONTEXT_INVAL,      /* UST context invalid */
+       LTTCOMM_NEED_ROOT_SESSIOND,             /* root sessiond is needed */
+       LTTCOMM_TRACE_ALREADY_STARTED,  /* Tracing already started */
+       LTTCOMM_TRACE_ALREADY_STOPPED,  /* Tracing already stopped */
 
        CONSUMERD_COMMAND_SOCK_READY,           /* when consumerd command socket ready */
        CONSUMERD_SUCCESS_RECV_FD,              /* success on receiving fds */
@@ -151,6 +154,8 @@ enum lttcomm_return_code {
        CONSUMERD_SPLICE_EINVAL,                /* EINVAL from splice(2) */
        CONSUMERD_SPLICE_ENOMEM,                /* ENOMEM from splice(2) */
        CONSUMERD_SPLICE_ESPIPE,                /* ESPIPE from splice(2) */
+       LTTCOMM_INVALID,                        /* Invalid parameter */
+
        /* MUST be last element */
        LTTCOMM_NR,                                             /* Last element */
 };
@@ -291,7 +296,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);
index 27601463c22ccb3652d41776234d49cc0e2161a9..8dec2d78366af35793e56797c0db537fcfd6bc6a 100644 (file)
 
 #define _GNU_SOURCE
 #include <assert.h>
-#include <fcntl.h>
 #include <poll.h>
 #include <pthread.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/mman.h>
 #include <sys/socket.h>
+#include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include <lttng/ust-ctl.h>
 
 #include <common/common.h>
 #include <common/sessiond-comm/sessiond-comm.h>
+#include <common/compat/fcntl.h>
 
 #include "ust-consumer.h"
 
@@ -42,9 +43,9 @@ extern volatile int consumer_quit;
 /*
  * Mmap the ring buffer, read it and write the data to the tracefile.
  *
- * Returns the number of bytes written
+ * Returns the number of bytes written, else negative value on error.
  */
-int lttng_ustconsumer_on_read_subbuffer_mmap(
+ssize_t lttng_ustconsumer_on_read_subbuffer_mmap(
                struct lttng_consumer_local_data *ctx,
                struct lttng_consumer_stream *stream, unsigned long len)
 {
@@ -58,7 +59,7 @@ int lttng_ustconsumer_on_read_subbuffer_mmap(
                stream->buf, &mmap_offset);
        if (ret != 0) {
                errno = -ret;
-               perror("ustctl_get_mmap_read_offset");
+               PERROR("ustctl_get_mmap_read_offset");
                goto end;
        }
        while (len > 0) {
@@ -67,11 +68,11 @@ int lttng_ustconsumer_on_read_subbuffer_mmap(
                        len = 0;
                } else if (ret < 0) {
                        errno = -ret;
-                       perror("Error in file write");
+                       PERROR("Error in file write");
                        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;
        }
@@ -89,7 +90,7 @@ end:
  *
  * Returns the number of bytes spliced.
  */
-int lttng_ustconsumer_on_read_subbuffer_splice(
+ssize_t lttng_ustconsumer_on_read_subbuffer_splice(
                struct lttng_consumer_local_data *ctx,
                struct lttng_consumer_stream *stream, unsigned long len)
 {
@@ -109,7 +110,7 @@ int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data *ctx,
        ret = ustctl_snapshot(stream->chan->handle, stream->buf);
        if (ret != 0) {
                errno = -ret;
-               perror("Getting sub-buffer snapshot.");
+               PERROR("Getting sub-buffer snapshot.");
        }
 
        return ret;
@@ -131,7 +132,7 @@ int lttng_ustconsumer_get_produced_snapshot(
                        stream->buf, pos);
        if (ret != 0) {
                errno = -ret;
-               perror("kernctl_snapshot_get_produced");
+               PERROR("kernctl_snapshot_get_produced");
        }
 
        return ret;
@@ -260,7 +261,7 @@ end:
        /* signal the poll thread */
        ret = write(ctx->consumer_poll_pipe[1], "4", 1);
        if (ret < 0) {
-               perror("write consumer poll");
+               PERROR("write consumer poll");
        }
 end_nosignal:
        return 0;
@@ -400,7 +401,7 @@ int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
                                stream->uid, stream->gid);
                if (ret < 0) {
                        ERR("Opening %s", stream->path_name);
-                       perror("open");
+                       PERROR("open");
                        goto error;
                }
                stream->out_fd = ret;
index 76b4eeece093b5804bfe51e4e33a0928a9370649..0089ddf17ab250efa064b539a148cc956cf1034a 100644 (file)
 /*
  * Mmap the ring buffer, read it and write the data to the tracefile.
  *
- * Returns the number of bytes written.
+ * Returns the number of bytes written, else negative value on error.
  */
-extern int lttng_ustconsumer_on_read_subbuffer_mmap(
+extern ssize_t lttng_ustconsumer_on_read_subbuffer_mmap(
                struct lttng_consumer_local_data *ctx,
                struct lttng_consumer_stream *stream, unsigned long len);
 
 /* Not implemented */
-extern int lttng_ustconsumer_on_read_subbuffer_splice(
+extern ssize_t lttng_ustconsumer_on_read_subbuffer_splice(
                struct lttng_consumer_local_data *ctx,
                struct lttng_consumer_stream *stream, unsigned long len);
 
@@ -76,7 +76,7 @@ void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream);
 #else /* HAVE_LIBLTTNG_UST_CTL */
 
 static inline
-int lttng_ustconsumer_on_read_subbuffer_mmap(
+ssize_t lttng_ustconsumer_on_read_subbuffer_mmap(
                struct lttng_consumer_local_data *ctx,
                struct lttng_consumer_stream *stream, unsigned long len)
 {
@@ -84,7 +84,7 @@ int lttng_ustconsumer_on_read_subbuffer_mmap(
 }
 
 static inline
-int lttng_ustconsumer_on_read_subbuffer_splice(
+ssize_t lttng_ustconsumer_on_read_subbuffer_splice(
                struct lttng_consumer_local_data *ctx,
                struct lttng_consumer_stream *uststream, unsigned long len)
 {
index c5887fe65ead21a41f637aafa3adc10d60e0eb45..da93da1cc35078ec232bd0997d023509dec56807 100644 (file)
@@ -41,6 +41,18 @@ static char sessiond_sock_path[PATH_MAX];
 static char *tracing_group;
 static int connected;
 
+/* Global */
+
+/*
+ * Those two variables are used by error.h to silent or control the verbosity of
+ * error message. They are global to the library so application linking with it
+ * are able to compile correctly and also control verbosity of the library.
+ *
+ * Note that it is *not* possible to silent ERR() and PERROR() macros.
+ */
+int lttng_opt_quiet;
+int lttng_opt_verbose;
+
 /*
  * Copy string from src to dst and enforce null terminated byte.
  */
index 1dd8bbea7b5e3d27671c3bec78e8cd53f0acb03d..8dd05a4f0cfb646fdf6cc590a7229fdddeaa3df5 100644 (file)
@@ -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
 
index f556db4fc2afbb796eac6f0245648a8fef0d4bc9..6964a6f97035a81495a0591527e151ef27fc6a90 100644 (file)
@@ -29,6 +29,8 @@
 
 #include "../utils.h"
 
+int lttng_opt_quiet;
+
 int main(int argc, char **argv)
 {
        struct lttng_handle *handle = NULL;
index 36eb039214b0c918e4dd969d1ce51ed2c3ae3057..0af797c054fc0dadc9f6ad50fde5283e45a12c60 100644 (file)
@@ -29,6 +29,8 @@
 
 #include "../utils.h"
 
+int lttng_opt_quiet;
+
 int main(int argc, char **argv)
 {
        struct lttng_handle *handle = NULL;
index 9feaa30575d9accd7bf1aa823db40a5f71a36fb3..03363a86b523dd21203ca35c1847625ba2cd0e98 100644 (file)
@@ -29,6 +29,8 @@
 
 #include "../utils.h"
 
+int lttng_opt_quiet;
+
 int main(int argc, char **argv)
 {
        struct lttng_handle *handle = NULL;
index 867e4391b811b208b425f37d78d15d52430e455b..24dcec53d9d473f9b34f6491dc39fc521aeccabf 100644 (file)
@@ -29,6 +29,8 @@
 
 #include "../utils.h"
 
+int lttng_opt_quiet;
+
 int main(int argc, char **argv)
 {
        struct lttng_handle *handle = NULL;
index 0a534699c5444552669f7c0d9043a528d6579949..6d7f291beefbf56217ce36bbed30fdc6ec195f54 100644 (file)
@@ -36,8 +36,8 @@
 #define RANDOM_STRING_LEN      11
 
 /* For lttngerr.h */
-int opt_quiet = 1;
-int opt_verbose = 0;
+int lttng_opt_quiet = 1;
+int lttng_opt_verbose;
 
 static const char alphanum[] =
        "0123456789"
@@ -72,13 +72,13 @@ static void create_one_kernel_session(void)
        PRINT_OK();
 
        printf("Validating kernel session: ");
-       assert(kern->fd == 0);
-       assert(kern->metadata_stream_fd == 0);
+       assert(kern->fd == -1);
+       assert(kern->metadata_stream_fd == -1);
        assert(kern->consumer_fds_sent == 0);
        assert(kern->channel_count == 0);
        assert(kern->stream_count_global == 0);
        assert(kern->metadata == NULL);
-       assert(kern->consumer_fd == 0);
+       assert(kern->consumer_fd == -1);
        PRINT_OK();
 
        /* Init list in order to avoid sefaults from cds_list_del */
@@ -95,7 +95,7 @@ static void create_kernel_metadata(void)
        PRINT_OK();
 
        printf("Validating kernel session metadata: ");
-       assert(kern->metadata->fd == 0);
+       assert(kern->metadata->fd == -1);
        assert(strlen(kern->metadata->pathname));
        assert(kern->metadata->conf != NULL);
        assert(kern->metadata->conf->attr.overwrite
@@ -128,7 +128,7 @@ static void create_kernel_channel(void)
        PRINT_OK();
 
        printf("Validating kernel channel: ");
-       assert(chan->fd == 0);
+       assert(chan->fd == -1);
        assert(chan->enabled == 1);
        assert(strcmp(PATH1, chan->pathname) == 0);
        assert(chan->stream_count == 0);
@@ -157,7 +157,7 @@ static void create_kernel_event(void)
        PRINT_OK();
 
        printf("Validating kernel event: ");
-       assert(event->fd == 0);
+       assert(event->fd == -1);
        assert(event->enabled == 1);
        assert(event->ctx == NULL);
        assert(event->event->instrumentation == LTTNG_KERNEL_TRACEPOINT);
@@ -179,7 +179,7 @@ static void create_kernel_stream(void)
        PRINT_OK();
 
        printf("Validating kernel stream: ");
-       assert(stream->fd == 0);
+       assert(stream->fd == -1);
        assert(stream->pathname == NULL);
        assert(stream->state == 0);
        PRINT_OK();
index 5134f0b38cc4fda11bd854ea56691b496f456cc2..e55593561891d2827ba8cb8761e188e31c1d3e92 100644 (file)
@@ -55,8 +55,8 @@
 static struct ltt_session_list *session_list;
 
 /* For lttngerr.h */
-int opt_quiet = 1;
-int opt_verbose = 0;
+int lttng_opt_quiet = 1;
+int lttng_opt_verbose = 0;
 
 static const char alphanum[] =
        "0123456789"
index 924cfe4b97f1fde75af52f075969280072eab932..459520db1e547e12fe988b5b5c5dc85d053b27f0 100644 (file)
@@ -38,8 +38,8 @@
 #define RANDOM_STRING_LEN      11
 
 /* For lttngerr.h */
-int opt_quiet = 1;
-int opt_verbose = 0;
+int lttng_opt_quiet = 1;
+int lttng_opt_verbose;
 
 static const char alphanum[] =
        "0123456789"
index 8b7a914ee617b6ad1498fb49ef0faac7a98da898..29652dcbbccb214a828eedc41214c8bd6baddb05 100644 (file)
@@ -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
index 39168d6a2a72363829b98086dd8af93375b937ea..d3fdcbd4120cf5bdcf5c17250fbae09eabffd1dc 100644 (file)
@@ -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
This page took 0.09777 seconds and 4 git commands to generate.