From: David Goulet Date: Mon, 27 Jun 2011 19:21:45 +0000 (-0400) Subject: Rename and add missing header file X-Git-Tag: v2.0-pre1~77 X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=commitdiff_plain;h=beb8c75afd91bc32f2e7a9c124c9a21ecffd1486 Rename and add missing header file Rename config.c/.h to conf.* to avoid confusion with the autogenerated config.h from autoconf. Signed-off-by: David Goulet --- diff --git a/lttng/Makefile.am b/lttng/Makefile.am index de2f22cdc..fcea4668a 100644 --- a/lttng/Makefile.am +++ b/lttng/Makefile.am @@ -2,7 +2,7 @@ AM_CPPFLAGS = -I$(top_srcdir)/include bin_PROGRAMS = lttng -lttng_SOURCES = config.c commands/start.c commands/add_channel.c \ +lttng_SOURCES = conf.c commands/start.c commands/add_channel.c \ commands/list.c commands/create.c commands/destroy.c \ commands/stop.c commands/enable_events.c \ utils.c lttng.c diff --git a/lttng/commands/add_channel.c b/lttng/commands/add_channel.c index 6b41e875a..ea7db820e 100644 --- a/lttng/commands/add_channel.c +++ b/lttng/commands/add_channel.c @@ -26,7 +26,7 @@ #include #include "cmd.h" -#include "config.h" +#include "conf.h" #include "utils.h" static char *opt_channel_name; diff --git a/lttng/commands/create.c b/lttng/commands/create.c index 522ebc9c3..941f72355 100644 --- a/lttng/commands/create.c +++ b/lttng/commands/create.c @@ -27,7 +27,7 @@ #include #include "cmd.h" -#include "config.h" +#include "conf.h" static char *opt_output_path; static char *opt_session_name; diff --git a/lttng/commands/destroy.c b/lttng/commands/destroy.c index 9519403ad..f08a99fd7 100644 --- a/lttng/commands/destroy.c +++ b/lttng/commands/destroy.c @@ -26,7 +26,7 @@ #include #include "cmd.h" -#include "config.h" +#include "conf.h" #include "utils.h" static char *opt_session_name; diff --git a/lttng/commands/enable_events.c b/lttng/commands/enable_events.c index f9b51b14d..cde0cc48c 100644 --- a/lttng/commands/enable_events.c +++ b/lttng/commands/enable_events.c @@ -26,7 +26,7 @@ #include #include "cmd.h" -#include "config.h" +#include "conf.h" #include "utils.h" static char *opt_event_list; diff --git a/lttng/commands/start.c b/lttng/commands/start.c index 3a4febe4f..1a3e568ed 100644 --- a/lttng/commands/start.c +++ b/lttng/commands/start.c @@ -26,7 +26,7 @@ #include #include "cmd.h" -#include "config.h" +#include "conf.h" #include "utils.h" static char *opt_session_name; diff --git a/lttng/commands/stop.c b/lttng/commands/stop.c index 0245296a4..56edc2d6d 100644 --- a/lttng/commands/stop.c +++ b/lttng/commands/stop.c @@ -26,7 +26,7 @@ #include #include "cmd.h" -#include "config.h" +#include "conf.h" #include "utils.h" static char *opt_session_name; diff --git a/lttng/conf.c b/lttng/conf.c new file mode 100644 index 000000000..9e3a626ba --- /dev/null +++ b/lttng/conf.c @@ -0,0 +1,301 @@ +/* + * Copyright (c) 2011 David Goulet + * + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#include "conf.h" +#include "lttngerr.h" + +/* + * get_config_file_path + * + * Return the path with '/CONFIG_FILENAME' added to it. + */ +static char *get_config_file_path(char *path) +{ + int ret; + char *file_path; + + ret = asprintf(&file_path, "%s/%s", path, CONFIG_FILENAME); + if (ret < 0) { + ERR("Fail allocating config file path"); + } + + return file_path; +} + +/* + * open_config + * + * Return an open FILE pointer to the config file. + */ +static FILE *open_config(char *path, const char *mode) +{ + FILE *fp = NULL; + char *file_path; + + file_path = get_config_file_path(path); + if (file_path == NULL) { + goto error; + } + + fp = fopen(file_path, mode); + if (fp == NULL) { + perror("config file"); + goto error; + } + +error: + if (file_path) { + free(file_path); + } + return fp; +} + +/* + * create_config_file + * + * Create the empty config file a the path. + */ +static int create_config_file(char *path) +{ + int ret; + FILE *fp; + + fp = open_config(path, "w+"); + if (fp == NULL) { + ERR("Unable to create config file"); + ret = -1; + goto error; + } + + ret = fclose(fp); + +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) +{ + FILE *fp; + + fp = open_config(file_path, "a"); + if (fp == NULL) { + goto error; + } + + /* Write session name into config file */ + fwrite(data, size, 1, fp); + fclose(fp); + +error: + return; +} + +/* + * config_get_default_path + * + * Return the default path to config directory which is the current working + * directory. User must free() the returned allocated string. + */ +char *config_get_default_path(void) +{ + char *alloc_path; + + alloc_path = getcwd(NULL, 0); + if (alloc_path == NULL) { + perror("getcwd"); + } + + return alloc_path; +} + +/* + * config_destroy + * + * Destroy directory config and file config. + */ +void config_destroy(char *path) +{ + int ret; + char *config_path; + + config_path = get_config_file_path(path); + + ret = remove(config_path); + if (ret < 0) { + perror("remove config file"); + } + + ret = rmdir(path); + if (ret < 0) { + perror("rmdir config dir"); + } + + free(config_path); +} + +/* + * config_read_session_name + * + * Return sesson name from the config file. + */ +char *config_read_session_name(char *path) +{ + int ret; + FILE *fp; + char var[NAME_MAX], *session_name; + + fp = open_config(path, "r"); + if (fp == NULL) { + ERR("Can't find valid lttng config in %s", path); + goto error; + } + + session_name = malloc(NAME_MAX); + while (!feof(fp)) { + if ((ret = fscanf(fp, "%[^'=']=%s\n", var, session_name)) != 2) { + if (ret == -1) { + ERR("Missing session=NAME in config file."); + goto error; + } + continue; + } + + if (strcmp(var, "session") == 0) { + goto found; + } + } + + fclose(fp); + +error: + return NULL; + +found: + fclose(fp); + return session_name; + +} + +/* + * config_add_session_name + * + * Write session name option to the config file. + */ +int config_add_session_name(char *path, char *name) +{ + int ret; + char session_name[NAME_MAX]; + + ret = snprintf(session_name, NAME_MAX, "session=%s\n", name); + if (ret < 0) { + goto error; + } + + write_config(path, ret, session_name); + ret = 0; + +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 ret; + + /* Create config directory (.lttng) */ + ret = create_config_dir(path); + if (ret < 0) { + goto error; + } + + /* Create default config file */ + ret = create_config_file(path); + if (ret < 0) { + goto error; + } + + DBG("Init config session in %s", path); + +error: + return ret; +} diff --git a/lttng/conf.h b/lttng/conf.h new file mode 100644 index 000000000..b221d5a92 --- /dev/null +++ b/lttng/conf.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2011 - David Goulet + * + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef _LTTNG_CONFIG_H +#define _LTTNG_CONFIG_H + +#define CONFIG_FILENAME "config" +#define CONFIG_DIRNAME ".lttng" + +void config_destroy(char *path); +int config_init(char *path); +int config_add_session_name(char *path, char *name); + +/* Must free() the return pointer */ +char *config_generate_dir_path(char *path); +char *config_read_session_name(char *path); +char *config_get_default_path(void); + +#endif /* _LTTNG_CONFIG_H */ diff --git a/lttng/config.c b/lttng/config.c deleted file mode 100644 index 224bd1a1b..000000000 --- a/lttng/config.c +++ /dev/null @@ -1,301 +0,0 @@ -/* - * Copyright (c) 2011 David Goulet - * - * 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. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" -#include "lttngerr.h" - -/* - * get_config_file_path - * - * Return the path with '/CONFIG_FILENAME' added to it. - */ -static char *get_config_file_path(char *path) -{ - int ret; - char *file_path; - - ret = asprintf(&file_path, "%s/%s", path, CONFIG_FILENAME); - if (ret < 0) { - ERR("Fail allocating config file path"); - } - - return file_path; -} - -/* - * open_config - * - * Return an open FILE pointer to the config file. - */ -static FILE *open_config(char *path, const char *mode) -{ - FILE *fp = NULL; - char *file_path; - - file_path = get_config_file_path(path); - if (file_path == NULL) { - goto error; - } - - fp = fopen(file_path, mode); - if (fp == NULL) { - perror("config file"); - goto error; - } - -error: - if (file_path) { - free(file_path); - } - return fp; -} - -/* - * create_config_file - * - * Create the empty config file a the path. - */ -static int create_config_file(char *path) -{ - int ret; - FILE *fp; - - fp = open_config(path, "w+"); - if (fp == NULL) { - ERR("Unable to create config file"); - ret = -1; - goto error; - } - - ret = fclose(fp); - -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) -{ - FILE *fp; - - fp = open_config(file_path, "a"); - if (fp == NULL) { - goto error; - } - - /* Write session name into config file */ - fwrite(data, size, 1, fp); - fclose(fp); - -error: - return; -} - -/* - * config_get_default_path - * - * Return the default path to config directory which is the current working - * directory. User must free() the returned allocated string. - */ -char *config_get_default_path(void) -{ - char *alloc_path; - - alloc_path = getcwd(NULL, 0); - if (alloc_path == NULL) { - perror("getcwd"); - } - - return alloc_path; -} - -/* - * config_destroy - * - * Destroy directory config and file config. - */ -void config_destroy(char *path) -{ - int ret; - char *config_path; - - config_path = get_config_file_path(path); - - ret = remove(config_path); - if (ret < 0) { - perror("remove config file"); - } - - ret = rmdir(path); - if (ret < 0) { - perror("rmdir config dir"); - } - - free(config_path); -} - -/* - * config_read_session_name - * - * Return sesson name from the config file. - */ -char *config_read_session_name(char *path) -{ - int ret; - FILE *fp; - char var[NAME_MAX], *session_name; - - fp = open_config(path, "r"); - if (fp == NULL) { - ERR("Can't find valid lttng config in %s", path); - goto error; - } - - session_name = malloc(NAME_MAX); - while (!feof(fp)) { - if ((ret = fscanf(fp, "%[^'=']=%s\n", var, session_name)) != 2) { - if (ret == -1) { - ERR("Missing session=NAME in config file."); - goto error; - } - continue; - } - - if (strcmp(var, "session") == 0) { - goto found; - } - } - - fclose(fp); - -error: - return NULL; - -found: - fclose(fp); - return session_name; - -} - -/* - * config_add_session_name - * - * Write session name option to the config file. - */ -int config_add_session_name(char *path, char *name) -{ - int ret; - char session_name[NAME_MAX]; - - ret = snprintf(session_name, NAME_MAX, "session=%s\n", name); - if (ret < 0) { - goto error; - } - - write_config(path, ret, session_name); - ret = 0; - -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 ret; - - /* Create config directory (.lttng) */ - ret = create_config_dir(path); - if (ret < 0) { - goto error; - } - - /* Create default config file */ - ret = create_config_file(path); - if (ret < 0) { - goto error; - } - - DBG("Init config session in %s", path); - -error: - return ret; -} diff --git a/lttng/lttng.c b/lttng/lttng.c index 09e336803..5f0699119 100644 --- a/lttng/lttng.c +++ b/lttng/lttng.c @@ -27,7 +27,7 @@ #include #include "cmd.h" -#include "config.h" +#include "conf.h" #include "lttngerr.h" /* Variables */ diff --git a/lttng/utils.c b/lttng/utils.c index b05fd2283..f9edb173b 100644 --- a/lttng/utils.c +++ b/lttng/utils.c @@ -20,7 +20,7 @@ #include -#include "config.h" +#include "conf.h" /* * get_config_file_path