rotation-api: introduce rotation schedule descriptors
[lttng-tools.git] / src / common / config / session-config.c
index a600a5b47fe72b1dcec9f807a73f2ef899ace7df..ffe7b14ed59cb0cca2dd23fe4075e5bc48a637ae 100644 (file)
@@ -32,6 +32,7 @@
 #include <common/error.h>
 #include <common/macros.h>
 #include <common/utils.h>
+#include <common/dynamic-buffer.h>
 #include <common/compat/getenv.h>
 #include <lttng/lttng-error.h>
 #include <libxml/parser.h>
@@ -184,6 +185,8 @@ LTTNG_HIDDEN const char * const config_event_context_interruptible = "INTERRUPTI
 LTTNG_HIDDEN const char * const config_event_context_preemptible = "PREEMPTIBLE";
 LTTNG_HIDDEN const char * const config_event_context_need_reschedule = "NEED_RESCHEDULE";
 LTTNG_HIDDEN const char * const config_event_context_migratable = "MIGRATABLE";
+LTTNG_HIDDEN const char * const config_event_context_callstack_user= "CALLSTACK_USER";
+LTTNG_HIDDEN const char * const config_event_context_callstack_kernel = "CALLSTACK_KERNEL";
 
 /* Deprecated symbols */
 const char * const config_element_perf;
@@ -1017,6 +1020,12 @@ int get_context_type(xmlChar *context_type)
        } else if (!strcmp((char *) context_type,
                config_event_context_migratable)) {
                ret = LTTNG_EVENT_CONTEXT_MIGRATABLE;
+       } else if (!strcmp((char *) context_type,
+               config_event_context_callstack_user)) {
+               ret = LTTNG_EVENT_CONTEXT_CALLSTACK_USER;
+       } else if (!strcmp((char *) context_type,
+               config_event_context_callstack_kernel)) {
+               ret = LTTNG_EVENT_CONTEXT_CALLSTACK_KERNEL;
        } else {
                goto error;
        }
@@ -2545,6 +2554,82 @@ end:
        return ret;
 }
 
+static
+int add_periodic_rotation(const char *name, uint64_t time_us)
+{
+       int ret;
+       enum lttng_rotation_status status;
+       struct lttng_rotation_schedule *periodic =
+                       lttng_rotation_schedule_periodic_create();
+
+       if (!periodic) {
+               ret = -LTTNG_ERR_NOMEM;
+               goto error;
+       }
+
+       status = lttng_rotation_schedule_periodic_set_period(periodic,
+                       time_us);
+       if (status != LTTNG_ROTATION_STATUS_OK) {
+               ret = -LTTNG_ERR_INVALID;
+               goto error;
+       }
+
+       status = lttng_session_add_rotation_schedule(name, periodic);
+       switch (status) {
+       case LTTNG_ROTATION_STATUS_OK:
+               ret = LTTNG_OK;
+               break;
+       case LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET:
+       case LTTNG_ROTATION_STATUS_INVALID:
+               ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
+               break;
+       default:
+               ret = -LTTNG_ERR_UNK;
+               break;
+       }
+error:
+       lttng_rotation_schedule_destroy(periodic);
+       return ret;
+}
+
+static
+int add_size_rotation(const char *name, uint64_t size_bytes)
+{
+       int ret;
+       enum lttng_rotation_status status;
+       struct lttng_rotation_schedule *size =
+                       lttng_rotation_schedule_size_threshold_create();
+
+       if (!size) {
+               ret = -LTTNG_ERR_NOMEM;
+               goto error;
+       }
+
+       status = lttng_rotation_schedule_size_threshold_set_threshold(size,
+                       size_bytes);
+       if (status != LTTNG_ROTATION_STATUS_OK) {
+               ret = -LTTNG_ERR_INVALID;
+               goto error;
+       }
+
+       status = lttng_session_add_rotation_schedule(name, size);
+       switch (status) {
+       case LTTNG_ROTATION_STATUS_OK:
+               ret = LTTNG_OK;
+               break;
+       case LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET:
+       case LTTNG_ROTATION_STATUS_INVALID:
+               ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
+               break;
+       default:
+               ret = -LTTNG_ERR_UNK;
+               break;
+       }
+error:
+       lttng_rotation_schedule_destroy(size);
+       return ret;
+}
+
 static
 int process_session_node(xmlNodePtr session_node, const char *session_name,
                int overwrite,
@@ -2821,23 +2906,17 @@ domain_init_error:
                }
        }
 
-       if (rotation_timer_interval || rotation_size) {
-               struct lttng_rotation_schedule_attr *rotation_attr = lttng_rotation_schedule_attr_create();
-
-               if (!rotation_attr) {
-                       goto error;
-               }
-               ret = lttng_rotation_schedule_attr_set_session_name(rotation_attr, (const char *) name);
-               if (ret) {
-                       lttng_rotation_schedule_attr_destroy(rotation_attr);
+       if (rotation_timer_interval) {
+               ret = add_periodic_rotation((const char *) name,
+                               rotation_timer_interval);
+               if (ret < 0) {
                        goto error;
                }
-               lttng_rotation_schedule_attr_set_timer_period(rotation_attr,
-                               rotation_timer_interval);
-               lttng_rotation_schedule_attr_set_size(rotation_attr, rotation_size);
-               ret = lttng_rotation_set_schedule(rotation_attr);
-               lttng_rotation_schedule_attr_destroy(rotation_attr);
-               if (ret) {
+       }
+       if (rotation_size) {
+               ret = add_size_rotation((const char *) name,
+                               rotation_size);
+               if (ret < 0) {
                        goto error;
                }
        }
@@ -2952,23 +3031,6 @@ end:
        return ret;
 }
 
-/* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
-static
-struct dirent *alloc_dirent(const char *path)
-{
-       size_t len;
-       long name_max;
-       struct dirent *entry;
-
-       name_max = pathconf(path, _PC_NAME_MAX);
-       if (name_max == -1) {
-               name_max = PATH_MAX;
-       }
-       len = offsetof(struct dirent, d_name) + name_max + 1;
-       entry = zmalloc(len);
-       return entry;
-}
-
 static
 int load_session_from_path(const char *path, const char *session_name,
        struct session_config_validation_ctx *validation_ctx, int overwrite,
@@ -2976,9 +3038,19 @@ int load_session_from_path(const char *path, const char *session_name,
 {
        int ret, session_found = !session_name;
        DIR *directory = NULL;
+       struct lttng_dynamic_buffer file_path;
+       size_t path_len;
 
        assert(path);
        assert(validation_ctx);
+       path_len = strlen(path);
+       lttng_dynamic_buffer_init(&file_path);
+       if (path_len >= LTTNG_PATH_MAX) {
+               ERR("Session configuration load path \"%s\" length (%zu) exceeds the maximal length allowed (%d)",
+                               path, path_len, LTTNG_PATH_MAX);
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
 
        directory = opendir(path);
        if (!directory) {
@@ -2995,67 +3067,107 @@ int load_session_from_path(const char *path, const char *session_name,
                }
        }
        if (directory) {
-               struct dirent *entry;
-               struct dirent *result;
-               char *file_path = NULL;
-               size_t path_len = strlen(path);
+               size_t file_path_root_len;
 
-               if (path_len >= PATH_MAX) {
-                       ret = -LTTNG_ERR_INVALID;
-                       goto end;
-               }
-
-               entry = alloc_dirent(path);
-               if (!entry) {
+               ret = lttng_dynamic_buffer_set_capacity(&file_path,
+                               LTTNG_PATH_MAX);
+               if (ret) {
                        ret = -LTTNG_ERR_NOMEM;
                        goto end;
                }
 
-               file_path = zmalloc(PATH_MAX);
-               if (!file_path) {
+               ret = lttng_dynamic_buffer_append(&file_path, path, path_len);
+               if (ret) {
                        ret = -LTTNG_ERR_NOMEM;
-                       free(entry);
                        goto end;
                }
 
-               strncpy(file_path, path, path_len);
-               if (file_path[path_len - 1] != '/') {
-                       file_path[path_len++] = '/';
+               if (file_path.data[file_path.size - 1] != '/') {
+                       ret = lttng_dynamic_buffer_append(&file_path, "/", 1);
+                       if (ret) {
+                               ret = -LTTNG_ERR_NOMEM;
+                               goto end;
+                       }
                }
+               file_path_root_len = file_path.size;
 
-               ret = 0;
                /* Search for *.lttng files */
-               while (!readdir_r(directory, entry, &result) && result) {
-                       size_t file_name_len = strlen(result->d_name);
+               for (;;) {
+                       size_t file_name_len;
+                       struct dirent *result;
+
+                       /*
+                        * When the end of the directory stream is reached, NULL
+                        * is returned and errno is kept unchanged. When an
+                        * error occurs, NULL is returned and errno is set
+                        * accordingly. To distinguish between the two, set
+                        * errno to zero before calling readdir().
+                        *
+                        * On success, readdir() returns a pointer to a dirent
+                        * structure. This structure may be statically
+                        * allocated, do not attempt to free(3) it.
+                        */
+                       errno = 0;
+                       result = readdir(directory);
+
+                       /* Reached end of dir stream or error out. */
+                       if (!result) {
+                               if (errno) {
+                                       PERROR("Failed to enumerate the contents of path \"%s\" while loading session, readdir returned", path);
+                                       ret = -LTTNG_ERR_LOAD_IO_FAIL;
+                                       goto end;
+                               }
+                               break;
+                       }
+
+                       file_name_len = strlen(result->d_name);
 
                        if (file_name_len <=
                                sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION)) {
                                continue;
                        }
 
-                       if (path_len + file_name_len >= PATH_MAX) {
+                       if (file_path.size + file_name_len >= LTTNG_PATH_MAX) {
+                               WARN("Ignoring file \"%s\" since the path's length (%zu) would exceed the maximal permitted size (%d)",
+                                               result->d_name,
+                                               /* +1 to account for NULL terminator. */
+                                               file_path.size + file_name_len + 1,
+                                               LTTNG_PATH_MAX);
                                continue;
                        }
 
+                       /* Does the file end with .lttng? */
                        if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION,
-                               result->d_name + file_name_len - sizeof(
-                               DEFAULT_SESSION_CONFIG_FILE_EXTENSION) + 1)) {
+                                       result->d_name + file_name_len - sizeof(
+                                       DEFAULT_SESSION_CONFIG_FILE_EXTENSION) + 1)) {
                                continue;
                        }
 
-                       strncpy(file_path + path_len, result->d_name, file_name_len);
-                       file_path[path_len + file_name_len] = '\0';
+                       ret = lttng_dynamic_buffer_append(&file_path, result->d_name,
+                                       file_name_len + 1);
+                       if (ret) {
+                               ret = -LTTNG_ERR_NOMEM;
+                               goto end;
+                       }
 
-                       ret = load_session_from_file(file_path, session_name,
+                       ret = load_session_from_file(file_path.data, session_name,
                                validation_ctx, overwrite, overrides);
                        if (session_name && !ret) {
                                session_found = 1;
                                break;
                        }
+                       /*
+                        * Reset the buffer's size to the location of the
+                        * path's trailing '/'.
+                        */
+                       ret = lttng_dynamic_buffer_set_size(&file_path,
+                                       file_path_root_len);
+                       if (ret) {
+                               ret = -LTTNG_ERR_UNK;
+                               goto end;
+                       }
                }
 
-               free(entry);
-               free(file_path);
        } else {
                ret = load_session_from_file(path, session_name,
                        validation_ctx, overwrite, overrides);
@@ -3072,11 +3184,10 @@ end:
                        PERROR("closedir");
                }
        }
-
        if (session_found && !ret) {
                ret = 0;
        }
-
+       lttng_dynamic_buffer_reset(&file_path);
        return ret;
 }
 
This page took 0.02774 seconds and 5 git commands to generate.