Create userspace buffers using ua_sess effective credentials
[lttng-tools.git] / src / common / trace-chunk.c
index a15f0ddaa6e72864bdbfb67eaa5086ffb265b610..488d7ebc4ffffcf19938cdb5f1da4036311417e6 100644 (file)
@@ -209,7 +209,7 @@ void lttng_trace_chunk_init(struct lttng_trace_chunk *chunk)
 {
        urcu_ref_init(&chunk->ref);
        pthread_mutex_init(&chunk->lock, NULL);
-       lttng_dynamic_pointer_array_init(&chunk->top_level_directories);
+       lttng_dynamic_pointer_array_init(&chunk->top_level_directories, free);
 }
 
 static
@@ -224,7 +224,7 @@ void lttng_trace_chunk_fini(struct lttng_trace_chunk *chunk)
        }
        free(chunk->name);
        chunk->name = NULL;
-       lttng_dynamic_pointer_array_reset(&chunk->top_level_directories, free);
+       lttng_dynamic_pointer_array_reset(&chunk->top_level_directories);
        pthread_mutex_destroy(&chunk->lock);
 }
 
@@ -399,6 +399,27 @@ end:
        return status;
 }
 
+static
+bool is_valid_chunk_name(const char *name)
+{
+       size_t len;
+
+       if (!name) {
+               return false;
+       }
+
+       len = strnlen(name, LTTNG_NAME_MAX);
+       if (len == 0 || len == LTTNG_NAME_MAX) {
+               return false;
+       }
+
+       if (strchr(name, '/') || strchr(name, '.')) {
+               return false;
+       }
+
+       return true;
+}
+
 LTTNG_HIDDEN
 enum lttng_trace_chunk_status lttng_trace_chunk_override_name(
                struct lttng_trace_chunk *chunk, const char *name)
@@ -407,7 +428,7 @@ enum lttng_trace_chunk_status lttng_trace_chunk_override_name(
        char *new_name;
        enum lttng_trace_chunk_status status = LTTNG_TRACE_CHUNK_STATUS_OK;
 
-       if (!name || !*name || strnlen(name, LTTNG_NAME_MAX) == LTTNG_NAME_MAX) {
+       if (!is_valid_chunk_name(name)) {
                ERR("Attempted to set an invalid name on a trace chunk: name = %s",
                                name ? : "NULL");
                status = LTTNG_TRACE_CHUNK_STATUS_INVALID_ARGUMENT;
@@ -745,6 +766,8 @@ enum lttng_trace_chunk_status lttng_trace_chunk_open_file(
                        chunk->credentials.value.use_current_user ?
                                        NULL : &chunk->credentials.value.user);
        if (ret < 0) {
+               ERR("Failed to open file relative to trace chunk file_path = \"%s\", flags = %d, mode = %d",
+                               file_path, flags, (int) mode);
                status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
                goto end;
        }
@@ -798,8 +821,6 @@ void lttng_trace_chunk_move_to_completed(struct lttng_trace_chunk *trace_chunk)
        int ret;
        char *directory_to_rename = NULL;
        bool free_directory_to_rename = false;
-       const int session_dirfd =
-                       trace_chunk->session_output_directory.value.dirfd;
        char *archived_chunk_name = NULL;
        const uint64_t chunk_id = LTTNG_OPTIONAL_GET(trace_chunk->id);
        const time_t creation_timestamp =
@@ -843,17 +864,18 @@ void lttng_trace_chunk_move_to_completed(struct lttng_trace_chunk *trace_chunk)
                }
 
                for (i = 0; i < count; i++) {
-                       const int temp_dirfd = temporary_rename_directory.dirfd;
                        const char *top_level_name =
                                        lttng_dynamic_pointer_array_get_pointer(
                                                &trace_chunk->top_level_directories, i);
 
-                       /*
-                        * FIXME replace renamat() use by directory handle
-                        * wrapper for non-POSIX 2008 systems.
-                        */
-                       ret = renameat(session_dirfd, top_level_name,
-                                       temp_dirfd, top_level_name);
+                       ret = lttng_directory_handle_rename_as_user(
+                                       &trace_chunk->session_output_directory.value,
+                                       top_level_name,
+                                       &temporary_rename_directory,
+                                       top_level_name,
+                                       LTTNG_OPTIONAL_GET(trace_chunk->credentials).use_current_user ?
+                                               NULL :
+                                               &trace_chunk->credentials.value.user);
                        if (ret) {
                                PERROR("Failed to move \"%s\" to temporary trace chunk rename directory",
                                                top_level_name);
@@ -904,13 +926,14 @@ void lttng_trace_chunk_move_to_completed(struct lttng_trace_chunk *trace_chunk)
        }
        archived_chunks_directory.is_set = true;
 
-       /*
-        * FIXME replace renamat() use by directory handle
-        * wrapper for non-POSIX 2008 systems.
-        */
-       ret = renameat(session_dirfd, directory_to_rename,
-                       archived_chunks_directory.value.dirfd,
-                       archived_chunk_name);
+       ret = lttng_directory_handle_rename_as_user(
+                       &trace_chunk->session_output_directory.value,
+                       directory_to_rename,
+                       &archived_chunks_directory.value,
+                       archived_chunk_name,
+                       LTTNG_OPTIONAL_GET(trace_chunk->credentials).use_current_user ?
+                               NULL :
+                               &trace_chunk->credentials.value.user);
        if (ret) {
                PERROR("Failed to rename folder \"%s\" to \"%s\"",
                                directory_to_rename, archived_chunk_name);
This page took 0.024713 seconds and 4 git commands to generate.