+ if (string_match(optname, "client-sock") || opt == 'c') {
+ if (lttng_is_setuid_setgid()) {
+ WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+ "-c, --client-sock");
+ } else {
+ snprintf(client_unix_sock_path, PATH_MAX, "%s", arg);
+ }
+ } else if (string_match(optname, "apps-sock") || opt == 'a') {
+ if (lttng_is_setuid_setgid()) {
+ WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+ "-a, --apps-sock");
+ } else {
+ snprintf(apps_unix_sock_path, PATH_MAX, "%s", arg);
+ }
+ } else if (string_match(optname, "daemonize") || opt == 'd') {
+ opt_daemon = 1;
+ } else if (string_match(optname, "background") || opt == 'b') {
+ opt_background = 1;
+ } else if (string_match(optname, "group") || opt == 'g') {
+ if (lttng_is_setuid_setgid()) {
+ WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+ "-g, --group");
+ } else {
+ /*
+ * If the override option is set, the pointer points to a
+ * *non* const thus freeing it even though the variable type is
+ * set to const.
+ */
+ if (tracing_group_name_override) {
+ free((void *) tracing_group_name);
+ }
+ tracing_group_name = strdup(arg);
+ if (!tracing_group_name) {
+ PERROR("strdup");
+ ret = -ENOMEM;
+ }
+ tracing_group_name_override = 1;
+ }
+ } else if (string_match(optname, "help") || opt == 'h') {
+ usage();
+ exit(EXIT_SUCCESS);
+ } else if (string_match(optname, "version") || opt == 'V') {
+ fprintf(stdout, "%s\n", VERSION);
+ exit(EXIT_SUCCESS);
+ } else if (string_match(optname, "sig-parent") || opt == 'S') {
+ opt_sig_parent = 1;
+ } else if (string_match(optname, "kconsumerd-err-sock")) {
+ if (lttng_is_setuid_setgid()) {
+ WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+ "--kconsumerd-err-sock");
+ } else {
+ snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", arg);
+ }
+ } else if (string_match(optname, "kconsumerd-cmd-sock")) {
+ if (lttng_is_setuid_setgid()) {
+ WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+ "--kconsumerd-cmd-sock");
+ } else {
+ snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
+ }
+ } else if (string_match(optname, "ustconsumerd64-err-sock")) {
+ if (lttng_is_setuid_setgid()) {
+ WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+ "--ustconsumerd64-err-sock");
+ } else {
+ snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", arg);
+ }
+ } else if (string_match(optname, "ustconsumerd64-cmd-sock")) {
+ if (lttng_is_setuid_setgid()) {
+ WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+ "--ustconsumerd64-cmd-sock");
+ } else {
+ snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
+ }
+ } else if (string_match(optname, "ustconsumerd32-err-sock")) {
+ if (lttng_is_setuid_setgid()) {
+ WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+ "--ustconsumerd32-err-sock");
+ } else {
+ snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", arg);
+ }
+ } else if (string_match(optname, "ustconsumerd32-cmd-sock")) {
+ if (lttng_is_setuid_setgid()) {
+ WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+ "--ustconsumerd32-cmd-sock");
+ } else {
+ snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
+ }
+ } else if (string_match(optname, "no-kernel")) {
+ opt_no_kernel = 1;
+ } else if (string_match(optname, "quiet") || opt == 'q') {
+ lttng_opt_quiet = 1;
+ } else if (string_match(optname, "verbose") || opt == 'v') {
+ /* Verbose level can increase using multiple -v */
+ if (arg) {
+ /* Value obtained from config file */
+ lttng_opt_verbose = config_parse_value(arg);
+ } else {
+ /* -v used on command line */
+ lttng_opt_verbose++;
+ }
+ /* Clamp value to [0, 3] */
+ lttng_opt_verbose = lttng_opt_verbose < 0 ? 0 :
+ (lttng_opt_verbose <= 3 ? lttng_opt_verbose : 3);
+ } else if (string_match(optname, "verbose-consumer")) {
+ if (arg) {
+ opt_verbose_consumer = config_parse_value(arg);
+ } else {
+ opt_verbose_consumer += 1;
+ }
+ } else if (string_match(optname, "consumerd32-path")) {
+ if (lttng_is_setuid_setgid()) {
+ WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+ "--consumerd32-path");
+ } else {
+ if (consumerd32_bin_override) {
+ free((void *) consumerd32_bin);
+ }
+ consumerd32_bin = strdup(arg);
+ if (!consumerd32_bin) {
+ PERROR("strdup");
+ ret = -ENOMEM;
+ }
+ consumerd32_bin_override = 1;
+ }
+ } else if (string_match(optname, "consumerd32-libdir")) {
+ if (lttng_is_setuid_setgid()) {
+ WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+ "--consumerd32-libdir");
+ } else {
+ if (consumerd32_libdir_override) {
+ free((void *) consumerd32_libdir);
+ }
+ consumerd32_libdir = strdup(arg);
+ if (!consumerd32_libdir) {
+ PERROR("strdup");
+ ret = -ENOMEM;
+ }
+ consumerd32_libdir_override = 1;
+ }
+ } else if (string_match(optname, "consumerd64-path")) {
+ if (lttng_is_setuid_setgid()) {
+ WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+ "--consumerd64-path");
+ } else {
+ if (consumerd64_bin_override) {
+ free((void *) consumerd64_bin);
+ }
+ consumerd64_bin = strdup(arg);
+ if (!consumerd64_bin) {
+ PERROR("strdup");
+ ret = -ENOMEM;
+ }
+ consumerd64_bin_override = 1;
+ }
+ } else if (string_match(optname, "consumerd64-libdir")) {
+ if (lttng_is_setuid_setgid()) {
+ WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+ "--consumerd64-libdir");
+ } else {
+ if (consumerd64_libdir_override) {
+ free((void *) consumerd64_libdir);
+ }
+ consumerd64_libdir = strdup(arg);
+ if (!consumerd64_libdir) {
+ PERROR("strdup");
+ ret = -ENOMEM;
+ }
+ consumerd64_libdir_override = 1;
+ }
+ } else if (string_match(optname, "pidfile") || opt == 'p') {
+ if (lttng_is_setuid_setgid()) {
+ WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+ "-p, --pidfile");
+ } else {
+ free(opt_pidfile);
+ opt_pidfile = strdup(arg);
+ if (!opt_pidfile) {
+ PERROR("strdup");
+ ret = -ENOMEM;
+ }
+ }
+ } else if (string_match(optname, "agent-tcp-port")) {
+ if (lttng_is_setuid_setgid()) {
+ WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+ "--agent-tcp-port");
+ } else {
+ unsigned long v;
+
+ if (!arg) {
+ ret = -EINVAL;
+ goto end;
+ }
+ errno = 0;
+ v = strtoul(arg, NULL, 0);
+ if (errno != 0 || !isdigit(arg[0])) {
+ ERR("Wrong value in --agent-tcp-port parameter: %s", arg);
+ return -1;
+ }
+ if (v == 0 || v >= 65535) {
+ ERR("Port overflow in --agent-tcp-port parameter: %s", arg);
+ return -1;
+ }
+ agent_tcp_port = (uint32_t) v;
+ DBG3("Agent TCP port set to non default: %u", agent_tcp_port);
+ }
+ } else if (string_match(optname, "load") || opt == 'l') {
+ if (lttng_is_setuid_setgid()) {
+ WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+ "-l, --load");
+ } else {
+ free(opt_load_session_path);
+ opt_load_session_path = strdup(arg);
+ if (!opt_load_session_path) {
+ PERROR("strdup");
+ ret = -ENOMEM;
+ }
+ }
+ } else if (string_match(optname, "kmod-probes")) {
+ if (lttng_is_setuid_setgid()) {
+ WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+ "--kmod-probes");
+ } else {
+ free(kmod_probes_list);
+ kmod_probes_list = strdup(arg);
+ if (!kmod_probes_list) {
+ PERROR("strdup");
+ ret = -ENOMEM;
+ }
+ }
+ } else if (string_match(optname, "extra-kmod-probes")) {
+ if (lttng_is_setuid_setgid()) {
+ WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+ "--extra-kmod-probes");
+ } else {
+ free(kmod_extra_probes_list);
+ kmod_extra_probes_list = strdup(arg);
+ if (!kmod_extra_probes_list) {
+ PERROR("strdup");
+ ret = -ENOMEM;
+ }
+ }
+ } else if (string_match(optname, "config") || opt == 'f') {
+ /* This is handled in set_options() thus silent skip. */
+ goto end;
+ } else {
+ /* Unknown option or other error.
+ * Error is printed by getopt, just return */
+ ret = -1;
+ }
+
+end:
+ if (ret == -EINVAL) {
+ const char *opt_name = "unknown";
+ int i;
+
+ for (i = 0; i < sizeof(long_options) / sizeof(struct option);
+ i++) {
+ if (opt == long_options[i].val) {
+ opt_name = long_options[i].name;
+ break;
+ }
+ }
+
+ WARN("Invalid argument provided for option \"%s\", using default value.",
+ opt_name);
+ }
+
+ return ret;
+}
+
+/*
+ * config_entry_handler_cb used to handle options read from a config file.
+ * See config_entry_handler_cb comment in common/config/config.h for the
+ * return value conventions.
+ */
+static int config_entry_handler(const struct config_entry *entry, void *unused)
+{
+ int ret = 0, i;
+
+ if (!entry || !entry->name || !entry->value) {
+ ret = -EINVAL;
+ goto end;
+ }
+
+ /* Check if the option is to be ignored */
+ for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) {
+ if (!strcmp(entry->name, config_ignore_options[i])) {
+ goto end;
+ }
+ }
+
+ for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1;
+ i++) {
+
+ /* Ignore if not fully matched. */
+ if (strcmp(entry->name, long_options[i].name)) {
+ continue;
+ }
+
+ /*
+ * If the option takes no argument on the command line, we have to
+ * check if the value is "true". We support non-zero numeric values,
+ * true, on and yes.
+ */
+ if (!long_options[i].has_arg) {
+ ret = config_parse_value(entry->value);
+ if (ret <= 0) {
+ if (ret) {
+ WARN("Invalid configuration value \"%s\" for option %s",
+ entry->value, entry->name);
+ }
+ /* False, skip boolean config option. */
+ goto end;
+ }
+ }
+
+ ret = set_option(long_options[i].val, entry->value, entry->name);
+ goto end;
+ }
+
+ WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name);
+
+end:
+ return ret;
+}
+
+/*
+ * daemon configuration loading and argument parsing
+ */
+static int set_options(int argc, char **argv)
+{
+ int ret = 0, c = 0, option_index = 0;
+ int orig_optopt = optopt, orig_optind = optind;
+ char *optstring;
+ const char *config_path = NULL;
+
+ optstring = utils_generate_optstring(long_options,
+ sizeof(long_options) / sizeof(struct option));
+ if (!optstring) {
+ ret = -ENOMEM;
+ goto end;
+ }
+
+ /* Check for the --config option */
+ while ((c = getopt_long(argc, argv, optstring, long_options,
+ &option_index)) != -1) {
+ if (c == '?') {
+ ret = -EINVAL;
+ goto end;
+ } else if (c != 'f') {
+ /* if not equal to --config option. */
+ continue;
+ }
+
+ if (lttng_is_setuid_setgid()) {
+ WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+ "-f, --config");
+ } else {
+ config_path = utils_expand_path(optarg);
+ if (!config_path) {
+ ERR("Failed to resolve path: %s", optarg);
+ }
+ }
+ }
+
+ ret = config_get_section_entries(config_path, config_section_name,
+ config_entry_handler, NULL);
+ if (ret) {
+ if (ret > 0) {
+ ERR("Invalid configuration option at line %i", ret);
+ ret = -1;
+ }
+ goto end;
+ }
+
+ /* Reset getopt's global state */
+ optopt = orig_optopt;
+ optind = orig_optind;
+ while (1) {
+ option_index = -1;
+ /*
+ * getopt_long() will not set option_index if it encounters a
+ * short option.
+ */
+ c = getopt_long(argc, argv, optstring, long_options,
+ &option_index);
+ if (c == -1) {
+ break;
+ }
+
+ /*
+ * Pass NULL as the long option name if popt left the index
+ * unset.
+ */
+ ret = set_option(c, optarg,
+ option_index < 0 ? NULL :
+ long_options[option_index].name);
+ if (ret < 0) {
+ break;
+ }
+ }
+
+end:
+ free(optstring);
+ return ret;
+}
+
+/*
+ * Creates the two needed socket by the daemon.
+ * apps_sock - The communication socket for all UST apps.
+ * client_sock - The communication of the cli tool (lttng).
+ */
+static int init_daemon_socket(void)
+{
+ int ret = 0;
+ mode_t old_umask;
+
+ old_umask = umask(0);
+
+ /* Create client tool unix socket */
+ client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
+ if (client_sock < 0) {
+ ERR("Create unix sock failed: %s", client_unix_sock_path);
+ ret = -1;
+ goto end;
+ }
+
+ /* Set the cloexec flag */
+ ret = utils_set_fd_cloexec(client_sock);
+ if (ret < 0) {
+ ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
+ "Continuing but note that the consumer daemon will have a "
+ "reference to this socket on exec()", client_sock);
+ }
+
+ /* File permission MUST be 660 */
+ 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");
+ goto end;
+ }