Create utils.c/.h in libcommon
[lttng-tools.git] / src / common / utils.c
CommitLineData
81b86775
DG
1/*
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18#define _GNU_SOURCE
19#include <ctype.h>
20#include <fcntl.h>
21#include <limits.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include <common/common.h>
26
27#include "utils.h"
28
29/*
30 * Return the realpath(3) of the path even if the last directory token does not
31 * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
32 * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
33 * fails if the end point directory does not exist.
34 */
35char *utils_expand_path(const char *path)
36{
37 const char *end_path = path;
38 char *next, *cut_path = NULL, *expanded_path = NULL;
39
40 /* Safety net */
41 if (path == NULL) {
42 goto error;
43 }
44
45 /* Find last token delimited by '/' */
46 while ((next = strpbrk(end_path + 1, "/"))) {
47 end_path = next;
48 }
49
50 /* Cut last token from original path */
51 cut_path = strndup(path, end_path - path);
52
53 expanded_path = zmalloc(PATH_MAX);
54 if (expanded_path == NULL) {
55 PERROR("zmalloc expand path");
56 goto error;
57 }
58
59 expanded_path = realpath((char *)cut_path, expanded_path);
60 if (expanded_path == NULL) {
61 switch (errno) {
62 case ENOENT:
63 ERR("%s: No such file or directory", cut_path);
64 break;
65 default:
66 PERROR("realpath utils expand path");
67 break;
68 }
69 goto error;
70 }
71
72 /* Add end part to expanded path */
73 strncat(expanded_path, end_path, PATH_MAX);
74
75 free(cut_path);
76 return expanded_path;
77
78error:
79 free(expanded_path);
80 free(cut_path);
81 return NULL;
82}
83
84/*
85 * Create a pipe in dst.
86 */
87int utils_create_pipe(int *dst)
88{
89 int ret;
90
91 if (dst == NULL) {
92 return -1;
93 }
94
95 ret = pipe(dst);
96 if (ret < 0) {
97 PERROR("create pipe");
98 }
99
100 return ret;
101}
102
103/*
104 * Create pipe and set CLOEXEC flag to both fd.
105 *
106 * Make sure the pipe opened by this function are closed at some point. Use
107 * utils_close_pipe().
108 */
109int utils_create_pipe_cloexec(int *dst)
110{
111 int ret, i;
112
113 if (dst == NULL) {
114 return -1;
115 }
116
117 ret = utils_create_pipe(dst);
118 if (ret < 0) {
119 goto error;
120 }
121
122 for (i = 0; i < 2; i++) {
123 ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC);
124 if (ret < 0) {
125 PERROR("fcntl pipe cloexec");
126 goto error;
127 }
128 }
129
130error:
131 return ret;
132}
133
134/*
135 * Close both read and write side of the pipe.
136 */
137void utils_close_pipe(int *src)
138{
139 int i, ret;
140
141 if (src == NULL) {
142 return;
143 }
144
145 for (i = 0; i < 2; i++) {
146 /* Safety check */
147 if (src[i] < 0) {
148 continue;
149 }
150
151 ret = close(src[i]);
152 if (ret) {
153 PERROR("close pipe");
154 }
155 }
156}
This page took 0.039625 seconds and 4 git commands to generate.