X-Git-Url: http://git.efficios.com/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Futils.c;h=da4c036b9630a3ecddebd9aa74d419609705379d;hp=b345100ea0fa5ad6517d28c72cbc53a5bd87e391;hb=6c71277b0dc97ce8a4ac6b8d359b4b349c04b658;hpb=094f381cd5b1fa8588098b840bcc26bc974ea3d9 diff --git a/src/common/utils.c b/src/common/utils.c index b345100ea..da4c036b9 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -351,10 +352,11 @@ error: */ LTTNG_HIDDEN int utils_create_stream_file(const char *path_name, char *file_name, uint64_t size, - uint64_t count, int uid, int gid) + uint64_t count, int uid, int gid, char *suffix) { int ret, out_fd, flags, mode; - char full_path[PATH_MAX], *path_name_id = NULL, *path; + char full_path[PATH_MAX], *path_name_suffix = NULL, *path; + char *extra = NULL; assert(path_name); assert(file_name); @@ -366,17 +368,30 @@ int utils_create_stream_file(const char *path_name, char *file_name, uint64_t si goto error; } + /* Setup extra string if suffix or/and a count is needed. */ + if (size > 0 && suffix) { + ret = asprintf(&extra, "_%" PRIu64 "%s", count, suffix); + } else if (size > 0) { + ret = asprintf(&extra, "_%" PRIu64, count); + } else if (suffix) { + ret = asprintf(&extra, "%s", suffix); + } + if (ret < 0) { + PERROR("Allocating extra string to name"); + goto error; + } + /* * If we split the trace in multiple files, we have to add the count at the * end of the tracefile name */ - if (size > 0) { - ret = asprintf(&path_name_id, "%s_%" PRIu64, full_path, count); + if (extra) { + ret = asprintf(&path_name_suffix, "%s%s", full_path, extra); if (ret < 0) { - PERROR("Allocating path name ID"); - goto error; + PERROR("Allocating path name with extra string"); + goto error_free_suffix; } - path = path_name_id; + path = path_name_suffix; } else { path = full_path; } @@ -397,7 +412,9 @@ int utils_create_stream_file(const char *path_name, char *file_name, uint64_t si ret = out_fd; error_open: - free(path_name_id); + free(path_name_suffix); +error_free_suffix: + free(extra); error: return ret; } @@ -413,10 +430,14 @@ error: */ LTTNG_HIDDEN int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size, - uint64_t count, int uid, int gid, int out_fd, uint64_t *new_count) + uint64_t count, int uid, int gid, int out_fd, uint64_t *new_count, + int *stream_fd) { int ret; + assert(new_count); + assert(stream_fd); + ret = close(out_fd); if (ret < 0) { PERROR("Closing tracefile"); @@ -429,8 +450,16 @@ int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size, (*new_count)++; } - return utils_create_stream_file(path_name, file_name, size, *new_count, - uid, gid); + ret = utils_create_stream_file(path_name, file_name, size, *new_count, + uid, gid, 0); + if (ret < 0) { + goto error; + } + *stream_fd = ret; + + /* Success. */ + ret = 0; + error: return ret; } @@ -666,3 +695,24 @@ size_t utils_get_current_time_str(const char *format, char *dst, size_t len) return ret; } + +/* + * Return the group ID matching name, else 0 if it cannot be found. + */ +LTTNG_HIDDEN +gid_t utils_get_group_id(const char *name) +{ + struct group *grp; + + grp = getgrnam(name); + if (!grp) { + static volatile int warn_once; + + if (!warn_once) { + WARN("No tracing group detected"); + warn_once = 1; + } + return 0; + } + return grp->gr_gid; +}