Merge branch 'master' of git://git.lttng.org/lttng-tools
authorDavid Goulet <dgoulet@efficios.com>
Fri, 2 Mar 2012 20:00:16 +0000 (15:00 -0500)
committerDavid Goulet <dgoulet@efficios.com>
Fri, 2 Mar 2012 20:00:16 +0000 (15:00 -0500)
1  2 
src/bin/lttng-sessiond/main.c
src/bin/lttng-sessiond/shm.c
src/common/compat/poll.h
src/common/consumer.c
src/common/error.h
src/common/runas.c
src/common/sessiond-comm/sessiond-comm.c
src/common/ust-consumer/ust-consumer.c

index 8654031f1becce5d4fa497ddba1c70b35850d25f,17ef7f71c318b60990f36ebe6ea2b6bde326e647..2ef18ae09f5a9291073c6763c863a1e5a610048d
@@@ -17,6 -17,7 +17,6 @@@
   */
  
  #define _GNU_SOURCE
 -#include <fcntl.h>
  #include <getopt.h>
  #include <grp.h>
  #include <limits.h>
@@@ -39,7 -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>
@@@ -122,22 -122,22 +122,22 @@@ static char client_unix_sock_path[PATH_
  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;
@@@ -295,22 -295,14 +295,22 @@@ static gid_t allowed_group(void
   */
  static int init_thread_quit_pipe(void)
  {
 -      int ret;
 +      int ret, i;
  
 -      ret = pipe2(thread_quit_pipe, O_CLOEXEC);
 +      ret = pipe(thread_quit_pipe);
        if (ret < 0) {
                PERROR("thread quit pipe");
                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;
  }
@@@ -386,7 -378,7 +386,7 @@@ static void stop_threads(void
   */
  static void cleanup(void)
  {
-       int ret;
+       int ret, i;
        char *cmd;
        struct ltt_session *sess, *stmp;
  
  
        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");
+                       }
+               }
+       }
  
        /* <fun> */
        DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
@@@ -497,7 -520,7 +528,7 @@@ static int send_kconsumer_channel_strea
        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;
        }
  
                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;
                }
        }
@@@ -564,7 -587,7 +595,7 @@@ static int send_kconsumer_session_strea
                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;
                }
  
                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;
                }
        }
@@@ -648,7 -671,7 +679,7 @@@ static int setup_lttng_msg(struct comma
  
        cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
        if (cmd_ctx->llm == NULL) {
-               perror("zmalloc");
+               PERROR("zmalloc");
                ret = -ENOMEM;
                goto error;
        }
@@@ -808,7 -831,7 +839,7 @@@ static void *thread_manage_kernel(void 
  
        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);
        }
  
  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;
  }
  
   */
  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;
  
        ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
        if (ret < 0) {
-               goto error;
+               goto error_listen;
        }
  
        /*
         */
        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);
@@@ -1058,16 -1078,33 +1086,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;
  }
@@@ -1089,7 -1126,7 +1134,7 @@@ static void *thread_manage_apps(void *d
  
        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);
                                        /* 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;
                                        }
  
        }
  
  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;
@@@ -1261,7 -1295,7 +1303,7 @@@ static void *thread_dispatch_ust_regist
                        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
@@@ -1289,7 -1323,7 +1331,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;
        /*
  
        ret = lttcomm_listen_unix_sock(apps_sock);
        if (ret < 0) {
-               goto error;
+               goto error_listen;
        }
  
        /*
         */
        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 */
                                        /* 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;
                                        }
  
                                                        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;
                                        }
  
        }
  
  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 (clock >= 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;
  }
@@@ -1667,17 -1717,17 +1725,17 @@@ static pid_t spawn_consumerd(struct con
                        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:
@@@ -1768,19 -1818,25 +1826,25 @@@ 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;
+       kernel_tracer_fd = -1;
        return LTTCOMM_KERN_NA;
  }
  
@@@ -2917,12 -2973,11 +2981,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;
        }
@@@ -2951,7 -3006,7 +3015,7 @@@ static int cmd_destroy_session(struct l
         */
        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);
@@@ -3258,7 -3313,7 +3322,7 @@@ static int process_client_msg(struct co
                }
  
                /* 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) {
@@@ -3348,8 -3403,7 +3412,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;
                }
                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) {
  
                /* 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();
  
@@@ -3602,7 -3653,7 +3666,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;
                /* 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;
                }
  
                                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;
                }
                }
  
                /* End of transmission */
-               close(sock);
+               ret = close(sock);
+               if (ret) {
+                       PERROR("close");
+               }
+               sock = -1;
  
                clean_command_ctx(&cmd_ctx);
        }
  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);
@@@ -3945,7 -4014,7 +4027,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;
        }
  
                        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;
        }
  
@@@ -4007,42 -4076,42 +4089,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 | 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");
  
  /*
   * 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;
  }
  
  /*
@@@ -4219,7 -4256,7 +4303,7 @@@ static int set_signal_handler(void
        sigset_t sigset;
  
        if ((ret = sigemptyset(&sigset)) < 0) {
-               perror("sigemptyset");
+               PERROR("sigemptyset");
                return ret;
        }
  
        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;
        }
  
@@@ -4261,7 -4298,7 +4345,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");
        }
  }
  
@@@ -4295,7 -4332,7 +4379,7 @@@ int main(int argc, char **argv
        if (opt_daemon) {
                ret = daemon(0, 0);
                if (ret < 0) {
-                       perror("daemon");
+                       PERROR("daemon");
                        goto error;
                }
        }
        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;
        }
  
        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;
        }
  
        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;
        }
  
        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;
        }
  
        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;
        }
  
        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 55d00e67fdd1cf443ac82c4160be3a003d6f0b18,ce1844fe887e654c26847e0842759485fced349f..432d07ecea5eb4462d36a6a4727737532e97791f
@@@ -51,7 -51,7 +51,7 @@@ static int get_wait_shm(char *shm_path
                ret = chown(shm_path, 0, 0);
                if (ret < 0) {
                        if (errno != ENOENT) {
-                               perror("chown wait shm");
+                               PERROR("chown wait shm");
                                goto error;
                        }
                }
@@@ -65,7 -65,7 +65,7 @@@
                ret = chown(shm_path, getuid(), getgid());
                if (ret < 0) {
                        if (errno != ENOENT) {
-                               perror("chown wait shm");
+                               PERROR("chown wait shm");
                                goto error;
                        }
                }
@@@ -77,7 -77,7 +77,7 @@@
        ret = chmod(shm_path, mode);
        if (ret < 0) {
                if (errno != ENOENT) {
-                       perror("chmod wait shm");
+                       PERROR("chmod wait shm");
                        goto error;
                }
        }
         */
        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
  
        DBG("Got the wait shm fd %d", wait_shm_fd);
  
@@@ -149,7 -145,7 +149,7 @@@ char *shm_ust_get_mmap(char *shm_path, 
        /* 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) {
diff --combined src/common/compat/poll.h
index bb046c3427af53f49a76997874a3d43b1808ba37,7af2ef050bdbf9339b0aa2243ea30ab2f2c18ddb..e3a13cedecfeda3768b2df2718ccf0e5af3b4d04
@@@ -50,6 -50,7 +50,7 @@@ static inline void __lttng_poll_free(vo
   */
  #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 +147,13 @@@ static inline void lttng_poll_reset(str
   */
  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,17 -185,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 --combined src/common/consumer.c
index ae59b6b602909a2752dc142d857dbe1fb503c703,9c54b84cf963bb5f0ae853248023216f0d7e1cdb..e7d51078e64b9b5209d4e8208a61a25d9e0bef38
@@@ -19,6 -19,7 +19,6 @@@
  
  #define _GNU_SOURCE
  #include <assert.h>
 -#include <fcntl.h>
  #include <poll.h>
  #include <pthread.h>
  #include <stdlib.h>
@@@ -173,13 -174,22 +173,22 @@@ void consumer_del_stream(struct lttng_c
                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;
@@@ -360,10 -370,16 +369,16 @@@ void consumer_del_channel(struct lttng_
                }
        }
        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);
  end:
@@@ -607,7 -623,7 +622,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
@@@ -698,14 -714,18 +713,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);
@@@ -718,13 -738,36 +737,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);
  }
@@@ -748,8 -791,6 +790,8 @@@ ssize_t lttng_consumer_on_read_subbuffe
                ERR("Unknown consumer_data type");
                assert(0);
        }
 +
 +      return 0;
  }
  
  /*
diff --combined src/common/error.h
index 14b5e7c67ea1eda8f519f2cfe42a48cd32a3a7ca,93c947c0d92ce79cf88b6aa7952762b2e35ff85f..e5255e4a42adaf9479c412679f812cd8064fa47b
  #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
@@@ -40,21 -44,22 +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 (opt_quiet == 0 && type == PRINT_MSG) {                      \
+                       fprintf(stdout, fmt, ## args);                          \
+               } else if (opt_quiet == 0 &&                                    \
+                               (((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 (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...) \
                " [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 { \
                char *buf; \
                buf = strerror_r(errno, tmp, sizeof(tmp)); \
                _PERROR(call ": %s", ## args, buf); \
        } while(0);
 +#endif
  
  #endif /* _ERROR_H */
diff --combined src/common/runas.c
index 4742a792eca105f35f1a8e2d194db9aeecd55a66,60e02a2bf85a4e22ff363d2a6ede3cfc1a0fd277..e0c317c4e40347fb7001b74fb9900e9bcfafe32f
  #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 {
@@@ -167,14 -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;
                }
        }
                writelen = write(data->retval_pipe, &sendret.c[index],
                                writeleft);
                if (writelen < 0) {
-                       perror("write");
+                       PERROR("write");
                        return EXIT_FAILURE;
                }
                writeleft -= writelen;
  }
  
  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;
  
        ret = pipe(retval_pipe);
        if (ret < 0) {
-               perror("pipe");
+               PERROR("pipe");
                retval.i = ret;
                goto end;
        }
        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;
        }
         * 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;
        }
        do {
                readlen = read(retval_pipe[0], &retval.c[index], readleft);
                if (readlen < 0) {
-                       perror("read");
+                       PERROR("read");
                        ret = -1;
                        break;
                }
         */
        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 3b224c6ff76e7414c7e140b955bdb27c16d60e8e,8a5ff1d3f10520efb26bef784dc7847fd7fba033..aa24e82ddff31d80f6d0d15fe969aca8dc80f49e
  #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"
  
@@@ -140,12 -142,11 +141,11 @@@ const char *lttcomm_get_readable_code(e
  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;
        }
        return fd;
  
  error_connect:
-       close(fd);
+       closeret = close(fd);
+       if (closeret) {
+               PERROR("close");
+       }
  error:
        return ret;
  }
@@@ -185,7 -189,7 +188,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;
@@@ -203,7 -207,7 +206,7 @@@ int lttcomm_create_unix_sock(const cha
  
        /* Create server socket */
        if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
-               perror("socket");
+               PERROR("socket");
                goto error;
        }
  
        (void) unlink(pathname);
        ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun));
        if (ret < 0) {
-               perror("bind");
+               PERROR("bind");
                goto error;
        }
  
@@@ -235,7 -239,7 +238,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;
@@@ -262,7 -266,7 +265,7 @@@ ssize_t lttcomm_recv_unix_sock(int sock
  
        ret = recvmsg(sock, &msg, MSG_WAITALL);
        if (ret < 0) {
-               perror("recvmsg");
+               PERROR("recvmsg");
        }
  
        return ret;
@@@ -288,7 -292,7 +291,7 @@@ ssize_t lttcomm_send_unix_sock(int sock
  
        ret = sendmsg(sock, &msg, 0);
        if (ret < 0) {
-               perror("sendmsg");
+               PERROR("sendmsg");
        }
  
        return ret;
   */
  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;
  }
@@@ -350,7 -357,7 +356,7 @@@ ssize_t lttcomm_send_fds_unix_sock(int 
  
        ret = sendmsg(sock, &msg, 0);
        if (ret < 0) {
-               perror("sendmsg");
+               PERROR("sendmsg");
        }
        return ret;
  }
@@@ -385,7 -392,7 +391,7 @@@ ssize_t lttcomm_recv_fds_unix_sock(int 
  
        ret = recvmsg(sock, &msg, 0);
        if (ret < 0) {
-               perror("recvmsg fds");
+               PERROR("recvmsg fds");
                goto end;
        }
        if (ret != 1) {
        }
        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;
        }
@@@ -429,14 -436,12 +435,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));
  
        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");
+               PERROR("sendmsg");
        }
  
        return ret;
   * 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));
  
        msg.msg_iov = iov;
        msg.msg_iovlen = 1;
  
 +#ifdef __linux__
        msg.msg_control = anc_buf;
        msg.msg_controllen = sizeof(anc_buf);
 +#endif /* __linux__ */
  
        ret = recvmsg(sock, &msg, 0);
        if (ret < 0) {
-               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;
        }
  
        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;
  
        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;
  /*
   * Set socket option to use credentials passing.
   */
 +#ifdef __linux__
  int lttcomm_setsockopt_creds_unix_sock(int sock)
  {
        int ret, on = 1;
        /* 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 c8ba084606180790b51fc7d11ba8ea5075f019c4,20d0405a3b40e8d828c883dd3b57606583e2d770..8dec2d78366af35793e56797c0db537fcfd6bc6a
  
  #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"
  
@@@ -59,7 -58,7 +59,7 @@@ ssize_t lttng_ustconsumer_on_read_subbu
                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) {
                        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;
        }
@@@ -110,7 -109,7 +110,7 @@@ int lttng_ustconsumer_take_snapshot(str
        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;
@@@ -132,7 -131,7 +132,7 @@@ int lttng_ustconsumer_get_produced_snap
                        stream->buf, pos);
        if (ret != 0) {
                errno = -ret;
-               perror("kernctl_snapshot_get_produced");
+               PERROR("kernctl_snapshot_get_produced");
        }
  
        return ret;
@@@ -261,7 -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;
@@@ -401,7 -400,7 +401,7 @@@ int lttng_ustconsumer_on_recv_stream(st
                                stream->uid, stream->gid);
                if (ret < 0) {
                        ERR("Opening %s", stream->path_name);
-                       perror("open");
+                       PERROR("open");
                        goto error;
                }
                stream->out_fd = ret;
This page took 0.055482 seconds and 4 git commands to generate.