X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=lttng%2Fconf.c;h=5479a3cd016e740100d14198e210d45769da013d;hp=57aaf7b2ddd5d726b70cf1b27a60f6f2eebcbe13;hb=2f2215907a630529cfa2f85d3d143c889e4fc021;hpb=2e0ac66c6c13c670bca92e7a333358554769f638 diff --git a/lttng/conf.c b/lttng/conf.c index 57aaf7b2d..5479a3cd0 100644 --- a/lttng/conf.c +++ b/lttng/conf.c @@ -3,8 +3,8 @@ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. + * as published by the Free Software Foundation; only version 2 + * of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -29,11 +29,11 @@ #include "lttngerr.h" /* - * get_config_file_path + * config_get_file_path * * Return the path with '/CONFIG_FILENAME' added to it. */ -static char *get_config_file_path(char *path) +char *config_get_file_path(char *path) { int ret; char *file_path; @@ -56,7 +56,7 @@ static FILE *open_config(char *path, const char *mode) FILE *fp = NULL; char *file_path; - file_path = get_config_file_path(path); + file_path = config_get_file_path(path); if (file_path == NULL) { goto error; } @@ -97,63 +97,41 @@ error: return ret; } -/* - * create_config_dir - * - * Create the empty config dir. - */ -static int create_config_dir(char *path) -{ - int ret; - - /* Create session directory .lttng */ - ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP); - if (ret < 0) { - if (errno == EEXIST) { - ERR("Session already exist at %s", path); - } else { - perror("mkdir config"); - ERR("Couldn't init config directory at %s", path); - } - ret = -errno; - goto error; - } - -error: - return ret; -} - /* * write_config * * Append data to the config file in file_path */ -static void write_config(char *file_path, size_t size, char *data) +static int write_config(char *file_path, size_t size, char *data) { FILE *fp; + size_t len; + int ret = 0; fp = open_config(file_path, "a"); if (fp == NULL) { - goto error; + ret = -1; + goto end; } /* Write session name into config file */ - fwrite(data, size, 1, fp); + len = fwrite(data, size, 1, fp); + if (len < 1) { + ret = -1; + } fclose(fp); - -error: - return; +end: + return ret; } /* * config_get_default_path * - * Return the HOME directory path. The output is dup so the user MUST - * free(3) the returned string. + * Return the HOME directory path. Caller MUST NOT free(3) the return pointer. */ char *config_get_default_path(void) { - return strdup(getenv("HOME")); + return getenv("HOME"); } /* @@ -166,7 +144,10 @@ void config_destroy(char *path) int ret; char *config_path; - config_path = get_config_file_path(path); + config_path = config_get_file_path(path); + if (config_path == NULL) { + return; + } ret = remove(config_path); if (ret < 0) { @@ -189,7 +170,7 @@ char *config_read_session_name(char *path) fp = open_config(path, "r"); if (fp == NULL) { - ERR("Can't find valid lttng config in %s", path); + ERR("Can't find valid lttng config %s/.lttngrc", path); goto error; } @@ -233,46 +214,24 @@ int config_add_session_name(char *path, char *name) if (ret < 0) { goto error; } - - write_config(path, ret, session_name); - ret = 0; - + ret = write_config(path, ret, session_name); error: return ret; } -/* - * config_generate_dir_path - * - * Return allocated path string to path/CONFIG_DIRNAME. - */ -char *config_generate_dir_path(char *path) -{ - int ret; - char *new_path; - - ret = asprintf(&new_path, "%s/%s", path, CONFIG_DIRNAME); - if (ret < 0) { - perror("config path problem"); - goto error; - } - -error: - return new_path; -} - /* * config_init * * Init configuration directory and file. */ -int config_init(char *path) +int config_init(char *session_name) { int ret; + char *path; - /* Create config directory (.lttng) */ - ret = create_config_dir(path); - if (ret < 0) { + path = config_get_default_path(); + if (path == NULL) { + ret = -1; goto error; } @@ -282,6 +241,11 @@ int config_init(char *path) goto error; } + ret = config_add_session_name(path, session_name); + if (ret < 0) { + goto error; + } + DBG("Init config session in %s", path); error: