#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"
*/
#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>
{
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");
- }
- }
-}
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 */
#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;
}
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;
#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
*
#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 */
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
--- /dev/null
+/*
+ * 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");
+ }
+ }
+}
--- /dev/null
+/*
+ * 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 */