Create utils.c/.h in libcommon
authorDavid Goulet <dgoulet@efficios.com>
Fri, 20 Jul 2012 15:37:24 +0000 (11:37 -0400)
committerDavid Goulet <dgoulet@efficios.com>
Fri, 20 Jul 2012 15:37:24 +0000 (11:37 -0400)
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 <dgoulet@efficios.com>
src/bin/lttng-sessiond/main.c
src/bin/lttng-sessiond/utils.c
src/bin/lttng-sessiond/utils.h
src/bin/lttng/commands/create.c
src/bin/lttng/utils.c
src/bin/lttng/utils.h
src/common/Makefile.am
src/common/utils.c [new file with mode: 0644]
src/common/utils.h [new file with mode: 0644]

index bf5adc50130c6609bb9239542d3153941def70fa..3c16d50a057fefc4496a42e40f660ab32fe63e1c 100644 (file)
@@ -44,6 +44,7 @@
 #include <common/kernel-consumer/kernel-consumer.h>
 #include <common/futex.h>
 #include <common/relayd/relayd.h>
+#include <common/utils.h>
 
 #include "lttng-sessiond.h"
 #include "channel.h"
index d0c8dd16f62571618db8118759d7a8ee4e650a76..07f29adedcebc0570c81bc153fe07769991b4e69 100644 (file)
  */
 
 #define _GNU_SOURCE
-#include <errno.h>
-#include <fcntl.h>
-#include <limits.h>
-#include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
 #include <unistd.h>
 
 #include <common/error.h>
@@ -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");
-               }
-       }
-}
index a63d0a3dc5458f538cdb75a18987760bc578400c..d4bd8c286f9df4b0f577029677d2a92d150a5f13 100644 (file)
@@ -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 */
index f89e2778c78d8a3dd0bc97a5e95210389d68c2c5..354a68dcfbc461ab09ff86cb64f672b3bc8f0ae8 100644 (file)
@@ -31,6 +31,7 @@
 #include <common/defaults.h>
 #include <common/sessiond-comm/sessiond-comm.h>
 #include <common/uri.h>
+#include <common/utils.h>
 
 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;
index 8ae984836595d1271b0a38a8809e2e5f5e0d88b9..b7f5170aad76bf66518e2f8aefb54b622e31441a 100644 (file)
 #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
  *
index a430b8bdf3e9577ebf5bbcb1e176f519cc9c50fc..9f7bfcca1053544e5ebad5d0b52b36f3943ff97a 100644 (file)
 
 #include <popt.h>
 
-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 */
index 5c694c1c55148ca8d244dc055949d474819eca03..131fda8e5925ee51579828d30659a6664b0d0e16 100644 (file)
@@ -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 (file)
index 0000000..bc9b2db
--- /dev/null
@@ -0,0 +1,156 @@
+/*
+ * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
+ *
+ * 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 <ctype.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <common/common.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 *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 (file)
index 0000000..87d0902
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
+ *
+ * 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 */
This page took 0.034172 seconds and 5 git commands to generate.