From: David Goulet Date: Fri, 20 Jul 2012 15:37:24 +0000 (-0400) Subject: Create utils.c/.h in libcommon X-Git-Tag: v2.1.0-rc1~71 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=81b8677518a0a8836d0b17e5c2a7fb43382a44c1;p=lttng-tools.git Create utils.c/.h in libcommon Move function from lttng/utils.h and lttng-session/utils.h in libcommon so they can be used at large. Simply link with libcommon and include common/utils.h. Signed-off-by: David Goulet --- diff --git a/src/bin/lttng-sessiond/main.c b/src/bin/lttng-sessiond/main.c index bf5adc501..3c16d50a0 100644 --- a/src/bin/lttng-sessiond/main.c +++ b/src/bin/lttng-sessiond/main.c @@ -44,6 +44,7 @@ #include #include #include +#include #include "lttng-sessiond.h" #include "channel.h" diff --git a/src/bin/lttng-sessiond/utils.c b/src/bin/lttng-sessiond/utils.c index d0c8dd16f..07f29aded 100644 --- a/src/bin/lttng-sessiond/utils.c +++ b/src/bin/lttng-sessiond/utils.c @@ -17,12 +17,7 @@ */ #define _GNU_SOURCE -#include -#include -#include -#include #include -#include #include #include @@ -53,77 +48,3 @@ const char *get_home_dir(void) { return ((const char *) getenv("HOME")); } - -/* - * Create a pipe in dst. - */ -int utils_create_pipe(int *dst) -{ - int ret; - - if (dst == NULL) { - return -1; - } - - ret = pipe(dst); - if (ret < 0) { - PERROR("create pipe"); - } - - return ret; -} - -/* - * Create pipe and set CLOEXEC flag to both fd. - * - * Make sure the pipe opened by this function are closed at some point. Use - * utils_close_pipe(). - */ -int utils_create_pipe_cloexec(int *dst) -{ - int ret, i; - - if (dst == NULL) { - return -1; - } - - ret = utils_create_pipe(dst); - if (ret < 0) { - goto error; - } - - for (i = 0; i < 2; i++) { - ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC); - if (ret < 0) { - PERROR("fcntl pipe cloexec"); - goto error; - } - } - -error: - return ret; -} - -/* - * Close both read and write side of the pipe. - */ -void utils_close_pipe(int *src) -{ - int i, ret; - - if (src == NULL) { - return; - } - - for (i = 0; i < 2; i++) { - /* Safety check */ - if (src[i] < 0) { - continue; - } - - ret = close(src[i]); - if (ret) { - PERROR("close pipe"); - } - } -} diff --git a/src/bin/lttng-sessiond/utils.h b/src/bin/lttng-sessiond/utils.h index a63d0a3dc..d4bd8c286 100644 --- a/src/bin/lttng-sessiond/utils.h +++ b/src/bin/lttng-sessiond/utils.h @@ -20,8 +20,5 @@ const char *get_home_dir(void); int notify_thread_pipe(int wpipe); -int utils_create_pipe_cloexec(int *dst); -int utils_create_pipe(int *dst); -void utils_close_pipe(int *src); #endif /* _LTT_UTILS_H */ diff --git a/src/bin/lttng/commands/create.c b/src/bin/lttng/commands/create.c index f89e2778c..354a68dcf 100644 --- a/src/bin/lttng/commands/create.c +++ b/src/bin/lttng/commands/create.c @@ -31,6 +31,7 @@ #include #include #include +#include static char *opt_output_path; static char *opt_session_name; @@ -177,7 +178,7 @@ static int create_session() } if (opt_output_path != NULL) { - traces_path = expand_full_path(opt_output_path); + traces_path = utils_expand_path(opt_output_path); if (traces_path == NULL) { ret = CMD_ERROR; goto error; diff --git a/src/bin/lttng/utils.c b/src/bin/lttng/utils.c index 8ae984836..b7f5170aa 100644 --- a/src/bin/lttng/utils.c +++ b/src/bin/lttng/utils.c @@ -25,54 +25,6 @@ #include "conf.h" #include "utils.h" -/* - * Return the realpath(3) of the path even if the last directory token does not - * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the - * /tmp/test1 does, the real path is returned. In normal time, realpath(3) - * fails if the end point directory does not exist. - */ -char *expand_full_path(const char *path) -{ - const char *end_path = path; - char *next, *cut_path, *expanded_path; - - /* Find last token delimited by '/' */ - while ((next = strpbrk(end_path + 1, "/"))) { - end_path = next; - } - - /* Cut last token from original path */ - cut_path = strndup(path, end_path - path); - - expanded_path = malloc(PATH_MAX); - if (expanded_path == NULL) { - goto error; - } - - expanded_path = realpath((char *)cut_path, expanded_path); - if (expanded_path == NULL) { - switch (errno) { - case ENOENT: - ERR("%s: No such file or directory", cut_path); - break; - default: - perror("realpath"); - break; - } - goto error; - } - - /* Add end part to expanded path */ - strcat(expanded_path, end_path); - - free(cut_path); - return expanded_path; - -error: - free(cut_path); - return NULL; -} - /* * get_session_name * diff --git a/src/bin/lttng/utils.h b/src/bin/lttng/utils.h index a430b8bdf..9f7bfcca1 100644 --- a/src/bin/lttng/utils.h +++ b/src/bin/lttng/utils.h @@ -20,10 +20,7 @@ #include -char *expand_full_path(const char *path); -char *get_config_file_path(void); char *get_session_name(void); -int set_session_name(char *name); void list_cmd_options(FILE *ofp, struct poptOption *options); #endif /* _LTTNG_UTILS_H */ diff --git a/src/common/Makefile.am b/src/common/Makefile.am index 5c694c1c5..131fda8e5 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -4,12 +4,13 @@ SUBDIRS = compat hashtable kernel-ctl sessiond-comm relayd kernel-consumer ust-c AM_CFLAGS = -fno-strict-aliasing -noinst_HEADERS = lttng-kernel.h defaults.h macros.h error.h futex.h uri.h +noinst_HEADERS = lttng-kernel.h defaults.h macros.h error.h futex.h uri.h utils.h # Common library noinst_LTLIBRARIES = libcommon.la -libcommon_la_SOURCES = runas.c runas.h common.h futex.c futex.h uri.c uri.h +libcommon_la_SOURCES = runas.c runas.h common.h futex.c futex.h uri.c uri.h \ + utils.c utils.h # Consumer library noinst_LTLIBRARIES += libconsumer.la diff --git a/src/common/utils.c b/src/common/utils.c new file mode 100644 index 000000000..bc9b2db3f --- /dev/null +++ b/src/common/utils.c @@ -0,0 +1,156 @@ +/* + * Copyright (C) 2012 - David Goulet + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * 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 "utils.h" + +/* + * Return the realpath(3) of the path even if the last directory token does not + * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the + * /tmp/test1 does, the real path is returned. In normal time, realpath(3) + * fails if the end point directory does not exist. + */ +char *utils_expand_path(const char *path) +{ + const char *end_path = path; + char *next, *cut_path = NULL, *expanded_path = NULL; + + /* Safety net */ + if (path == NULL) { + goto error; + } + + /* Find last token delimited by '/' */ + while ((next = strpbrk(end_path + 1, "/"))) { + end_path = next; + } + + /* Cut last token from original path */ + cut_path = strndup(path, end_path - path); + + expanded_path = zmalloc(PATH_MAX); + if (expanded_path == NULL) { + PERROR("zmalloc expand path"); + goto error; + } + + expanded_path = realpath((char *)cut_path, expanded_path); + if (expanded_path == NULL) { + switch (errno) { + case ENOENT: + ERR("%s: No such file or directory", cut_path); + break; + default: + PERROR("realpath utils expand path"); + break; + } + goto error; + } + + /* Add end part to expanded path */ + strncat(expanded_path, end_path, PATH_MAX); + + free(cut_path); + return expanded_path; + +error: + free(expanded_path); + free(cut_path); + return NULL; +} + +/* + * Create a pipe in dst. + */ +int utils_create_pipe(int *dst) +{ + int ret; + + if (dst == NULL) { + return -1; + } + + ret = pipe(dst); + if (ret < 0) { + PERROR("create pipe"); + } + + return ret; +} + +/* + * Create pipe and set CLOEXEC flag to both fd. + * + * Make sure the pipe opened by this function are closed at some point. Use + * utils_close_pipe(). + */ +int utils_create_pipe_cloexec(int *dst) +{ + int ret, i; + + if (dst == NULL) { + return -1; + } + + ret = utils_create_pipe(dst); + if (ret < 0) { + goto error; + } + + for (i = 0; i < 2; i++) { + ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC); + if (ret < 0) { + PERROR("fcntl pipe cloexec"); + goto error; + } + } + +error: + return ret; +} + +/* + * Close both read and write side of the pipe. + */ +void utils_close_pipe(int *src) +{ + int i, ret; + + if (src == NULL) { + return; + } + + for (i = 0; i < 2; i++) { + /* Safety check */ + if (src[i] < 0) { + continue; + } + + ret = close(src[i]); + if (ret) { + PERROR("close pipe"); + } + } +} diff --git a/src/common/utils.h b/src/common/utils.h new file mode 100644 index 000000000..87d090245 --- /dev/null +++ b/src/common/utils.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2012 - David Goulet + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * 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. + */ + +#ifndef _COMMON_UTILS_H +#define _COMMON_UTILS_H + +char *utils_expand_path(const char *path); +int utils_create_pipe(int *dst); +int utils_create_pipe_cloexec(int *dst); +void utils_close_pipe(int *src); + +#endif /* _COMMON_UTILS_H */