Fix: agent port file is o+w when launching as root
[lttng-tools.git] / src / bin / lttng-sessiond / client.cpp
index 4c1f6954b952ae1c4d553c5e52a71d48569ffc79..a52062e07f877ddc08ccf84604a56c5f2ff1fd59 100644 (file)
@@ -7,25 +7,25 @@
  *
  */
 
-#include "common/buffer-view.h"
-#include "common/compat/socket.h"
-#include "common/dynamic-array.h"
-#include "common/dynamic-buffer.h"
-#include "common/fd-handle.h"
-#include "common/payload-view.h"
-#include "common/payload.h"
-#include "common/sessiond-comm/sessiond-comm.h"
+#include "common/buffer-view.hpp"
+#include "common/compat/socket.hpp"
+#include "common/dynamic-array.hpp"
+#include "common/dynamic-buffer.hpp"
+#include "common/fd-handle.hpp"
+#include "common/payload-view.hpp"
+#include "common/payload.hpp"
+#include "common/sessiond-comm/sessiond-comm.hpp"
 #include "lttng/lttng-error.h"
 #include "lttng/tracker.h"
-#include <common/compat/getenv.h>
-#include <common/tracker.h>
-#include <common/unix.h>
-#include <common/utils.h>
-#include <lttng/error-query-internal.h>
-#include <lttng/event-internal.h>
-#include <lttng/session-descriptor-internal.h>
-#include <lttng/session-internal.h>
-#include <lttng/userspace-probe-internal.h>
+#include <common/compat/getenv.hpp>
+#include <common/tracker.hpp>
+#include <common/unix.hpp>
+#include <common/utils.hpp>
+#include <lttng/error-query-internal.hpp>
+#include <lttng/event-internal.hpp>
+#include <lttng/session-descriptor-internal.hpp>
+#include <lttng/session-internal.hpp>
+#include <lttng/userspace-probe-internal.hpp>
 #include <pthread.h>
 #include <signal.h>
 #include <stddef.h>
 #include <sys/stat.h>
 #include <unistd.h>
 
-#include "agent-thread.h"
-#include "clear.h"
-#include "client.h"
-#include "cmd.h"
-#include "health-sessiond.h"
-#include "kernel.h"
-#include "lttng-sessiond.h"
-#include "manage-consumer.h"
-#include "save.h"
-#include "testpoint.h"
-#include "utils.h"
-
-static bool is_root;
-
-static struct thread_state {
+#include "agent-thread.hpp"
+#include "clear.hpp"
+#include "client.hpp"
+#include "cmd.hpp"
+#include "health-sessiond.hpp"
+#include "kernel.hpp"
+#include "lttng-sessiond.hpp"
+#include "manage-consumer.hpp"
+#include "save.hpp"
+#include "testpoint.hpp"
+#include "utils.hpp"
+
+namespace {
+bool is_root;
+
+struct thread_state {
        sem_t ready;
        bool running;
        int client_sock;
 } thread_state;
+} /* namespace */
 
 static void set_thread_status(bool running)
 {
@@ -264,7 +266,7 @@ static pid_t spawn_consumerd(struct consumer_data *consumer_data)
                                        tmp = "";
                                }
                                tmplen = strlen(the_config.consumerd64_lib_dir.value) + 1 /* : */ + strlen(tmp);
-                               tmpnew = (char *) zmalloc(tmplen + 1 /* \0 */);
+                               tmpnew = zmalloc<char>(tmplen + 1 /* \0 */);
                                if (!tmpnew) {
                                        ret = -ENOMEM;
                                        goto error;
@@ -306,7 +308,7 @@ static pid_t spawn_consumerd(struct consumer_data *consumer_data)
                                        tmp = "";
                                }
                                tmplen = strlen(the_config.consumerd32_lib_dir.value) + 1 /* : */ + strlen(tmp);
-                               tmpnew = (char *) zmalloc(tmplen + 1 /* \0 */);
+                               tmpnew = zmalloc<char>(tmplen + 1 /* \0 */);
                                if (!tmpnew) {
                                        ret = -ENOMEM;
                                        goto error;
@@ -1751,7 +1753,7 @@ skip_domain:
                        goto error;
                }
 
-               uris = (lttng_uri *) zmalloc(len);
+               uris = calloc<lttng_uri>(nb_uri);
                if (uris == NULL) {
                        ret = LTTNG_ERR_FATAL;
                        goto error;
@@ -1892,31 +1894,35 @@ skip_domain:
        case LTTNG_LIST_SESSIONS:
        {
                unsigned int nr_sessions;
-               lttng_session *sessions_payload;
-               size_t payload_len;
+               lttng_session *sessions_payload = nullptr;
+               size_t payload_len = 0;
 
                session_lock_list();
                nr_sessions = lttng_sessions_count(
                                LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
                                LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
 
-               payload_len = (sizeof(struct lttng_session) * nr_sessions) +
-                               (sizeof(struct lttng_session_extended) * nr_sessions);
-               sessions_payload = (lttng_session *) zmalloc(payload_len);
+               if (nr_sessions > 0) {
+                       payload_len = (sizeof(struct lttng_session) *
+                                                     nr_sessions) +
+                                       (sizeof(struct lttng_session_extended) *
+                                                       nr_sessions);
+                       sessions_payload = zmalloc<lttng_session>(payload_len);
+                       if (!sessions_payload) {
+                               session_unlock_list();
+                               ret = -ENOMEM;
+                               goto setup_error;
+                       }
 
-               if (!sessions_payload) {
-                       session_unlock_list();
-                       ret = -ENOMEM;
-                       goto setup_error;
+                       cmd_list_lttng_sessions(sessions_payload, nr_sessions,
+                                       LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
+                                       LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
                }
 
-               cmd_list_lttng_sessions(sessions_payload, nr_sessions,
-                       LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
-                       LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
                session_unlock_list();
 
-               ret = setup_lttng_msg_no_cmd_header(cmd_ctx, sessions_payload,
-                       payload_len);
+               ret = setup_lttng_msg_no_cmd_header(
+                               cmd_ctx, sessions_payload, payload_len);
                free(sessions_payload);
 
                if (ret < 0) {
@@ -2390,7 +2396,6 @@ init_setup_error:
 static int create_client_sock(void)
 {
        int ret, client_sock;
-       const mode_t old_umask = umask(0);
 
        /* Create client tool unix socket */
        client_sock = lttcomm_create_unix_sock(
@@ -2424,7 +2429,6 @@ static int create_client_sock(void)
        DBG("Created client socket (fd = %i)", client_sock);
        ret = client_sock;
 end:
-       umask(old_umask);
        return ret;
 }
 
This page took 0.026045 seconds and 4 git commands to generate.