Unit tests: don't rebuild units under test
[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
35f90c40 19#include <assert.h>
81b86775
DG
20#include <ctype.h>
21#include <fcntl.h>
22#include <limits.h>
23#include <stdlib.h>
24#include <string.h>
2d851108
DG
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <unistd.h>
fe4477ee 28#include <inttypes.h>
81b86775
DG
29
30#include <common/common.h>
fe4477ee 31#include <common/runas.h>
81b86775
DG
32
33#include "utils.h"
34
35/*
36 * Return the realpath(3) of the path even if the last directory token does not
37 * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
38 * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
39 * fails if the end point directory does not exist.
40 */
90e535ef 41LTTNG_HIDDEN
81b86775
DG
42char *utils_expand_path(const char *path)
43{
44 const char *end_path = path;
45 char *next, *cut_path = NULL, *expanded_path = NULL;
46
47 /* Safety net */
48 if (path == NULL) {
49 goto error;
50 }
51
52 /* Find last token delimited by '/' */
53 while ((next = strpbrk(end_path + 1, "/"))) {
54 end_path = next;
55 }
56
57 /* Cut last token from original path */
58 cut_path = strndup(path, end_path - path);
59
60 expanded_path = zmalloc(PATH_MAX);
61 if (expanded_path == NULL) {
62 PERROR("zmalloc expand path");
63 goto error;
64 }
65
66 expanded_path = realpath((char *)cut_path, expanded_path);
67 if (expanded_path == NULL) {
68 switch (errno) {
69 case ENOENT:
70 ERR("%s: No such file or directory", cut_path);
71 break;
72 default:
73 PERROR("realpath utils expand path");
74 break;
75 }
76 goto error;
77 }
78
79 /* Add end part to expanded path */
c30ce0b3 80 strncat(expanded_path, end_path, PATH_MAX - strlen(expanded_path) - 1);
81b86775
DG
81
82 free(cut_path);
83 return expanded_path;
84
85error:
86 free(expanded_path);
87 free(cut_path);
88 return NULL;
89}
90
91/*
92 * Create a pipe in dst.
93 */
90e535ef 94LTTNG_HIDDEN
81b86775
DG
95int utils_create_pipe(int *dst)
96{
97 int ret;
98
99 if (dst == NULL) {
100 return -1;
101 }
102
103 ret = pipe(dst);
104 if (ret < 0) {
105 PERROR("create pipe");
106 }
107
108 return ret;
109}
110
111/*
112 * Create pipe and set CLOEXEC flag to both fd.
113 *
114 * Make sure the pipe opened by this function are closed at some point. Use
115 * utils_close_pipe().
116 */
90e535ef 117LTTNG_HIDDEN
81b86775
DG
118int utils_create_pipe_cloexec(int *dst)
119{
120 int ret, i;
121
122 if (dst == NULL) {
123 return -1;
124 }
125
126 ret = utils_create_pipe(dst);
127 if (ret < 0) {
128 goto error;
129 }
130
131 for (i = 0; i < 2; i++) {
132 ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC);
133 if (ret < 0) {
134 PERROR("fcntl pipe cloexec");
135 goto error;
136 }
137 }
138
139error:
140 return ret;
141}
142
143/*
144 * Close both read and write side of the pipe.
145 */
90e535ef 146LTTNG_HIDDEN
81b86775
DG
147void utils_close_pipe(int *src)
148{
149 int i, ret;
150
151 if (src == NULL) {
152 return;
153 }
154
155 for (i = 0; i < 2; i++) {
156 /* Safety check */
157 if (src[i] < 0) {
158 continue;
159 }
160
161 ret = close(src[i]);
162 if (ret) {
163 PERROR("close pipe");
164 }
165 }
166}
a4b92340
DG
167
168/*
169 * Create a new string using two strings range.
170 */
90e535ef 171LTTNG_HIDDEN
a4b92340
DG
172char *utils_strdupdelim(const char *begin, const char *end)
173{
174 char *str;
175
176 str = zmalloc(end - begin + 1);
177 if (str == NULL) {
178 PERROR("zmalloc strdupdelim");
179 goto error;
180 }
181
182 memcpy(str, begin, end - begin);
183 str[end - begin] = '\0';
184
185error:
186 return str;
187}
b662582b
DG
188
189/*
190 * Set CLOEXEC flag to the give file descriptor.
191 */
90e535ef 192LTTNG_HIDDEN
b662582b
DG
193int utils_set_fd_cloexec(int fd)
194{
195 int ret;
196
197 if (fd < 0) {
198 ret = -EINVAL;
199 goto end;
200 }
201
202 ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
203 if (ret < 0) {
204 PERROR("fcntl cloexec");
205 ret = -errno;
206 }
207
208end:
209 return ret;
210}
35f90c40
DG
211
212/*
213 * Create pid file to the given path and filename.
214 */
90e535ef 215LTTNG_HIDDEN
35f90c40
DG
216int utils_create_pid_file(pid_t pid, const char *filepath)
217{
218 int ret;
219 FILE *fp;
220
221 assert(filepath);
222
223 fp = fopen(filepath, "w");
224 if (fp == NULL) {
225 PERROR("open pid file %s", filepath);
226 ret = -1;
227 goto error;
228 }
229
230 ret = fprintf(fp, "%d\n", pid);
231 if (ret < 0) {
232 PERROR("fprintf pid file");
233 }
234
235 fclose(fp);
236 DBG("Pid %d written in file %s", pid, filepath);
237error:
238 return ret;
239}
2d851108
DG
240
241/*
242 * Recursively create directory using the given path and mode.
243 *
244 * On success, return 0 else a negative error code.
245 */
90e535ef 246LTTNG_HIDDEN
2d851108
DG
247int utils_mkdir_recursive(const char *path, mode_t mode)
248{
249 char *p, tmp[PATH_MAX];
250 struct stat statbuf;
251 size_t len;
252 int ret;
253
254 assert(path);
255
256 ret = snprintf(tmp, sizeof(tmp), "%s", path);
257 if (ret < 0) {
258 PERROR("snprintf mkdir");
259 goto error;
260 }
261
262 len = ret;
263 if (tmp[len - 1] == '/') {
264 tmp[len - 1] = 0;
265 }
266
267 for (p = tmp + 1; *p; p++) {
268 if (*p == '/') {
269 *p = 0;
270 if (tmp[strlen(tmp) - 1] == '.' &&
271 tmp[strlen(tmp) - 2] == '.' &&
272 tmp[strlen(tmp) - 3] == '/') {
273 ERR("Using '/../' is not permitted in the trace path (%s)",
274 tmp);
275 ret = -1;
276 goto error;
277 }
278 ret = stat(tmp, &statbuf);
279 if (ret < 0) {
280 ret = mkdir(tmp, mode);
281 if (ret < 0) {
282 if (errno != EEXIST) {
283 PERROR("mkdir recursive");
284 ret = -errno;
285 goto error;
286 }
287 }
288 }
289 *p = '/';
290 }
291 }
292
293 ret = mkdir(tmp, mode);
294 if (ret < 0) {
295 if (errno != EEXIST) {
296 PERROR("mkdir recursive last piece");
297 ret = -errno;
298 } else {
299 ret = 0;
300 }
301 }
302
303error:
304 return ret;
305}
fe4477ee
JD
306
307/*
308 * Create the stream tracefile on disk.
309 *
310 * Return 0 on success or else a negative value.
311 */
bc182241 312LTTNG_HIDDEN
fe4477ee
JD
313int utils_create_stream_file(char *path_name, char *file_name, uint64_t size,
314 uint64_t count, int uid, int gid)
315{
be96a7d1 316 int ret, out_fd, flags, mode;
fe4477ee
JD
317 char full_path[PATH_MAX], *path_name_id = NULL, *path;
318
319 assert(path_name);
320 assert(file_name);
321
322 ret = snprintf(full_path, sizeof(full_path), "%s/%s",
323 path_name, file_name);
324 if (ret < 0) {
325 PERROR("snprintf create output file");
326 goto error;
327 }
328
329 /*
330 * If we split the trace in multiple files, we have to add the count at the
331 * end of the tracefile name
332 */
333 if (size > 0) {
334 ret = asprintf(&path_name_id, "%s_%" PRIu64, full_path, count);
335 if (ret < 0) {
336 PERROR("Allocating path name ID");
337 goto error;
338 }
339 path = path_name_id;
340 } else {
341 path = full_path;
342 }
343
be96a7d1 344 flags = O_WRONLY | O_CREAT | O_TRUNC;
0f907de1 345 /* Open with 660 mode */
be96a7d1
DG
346 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
347
348 if (uid < 0 || gid < 0) {
349 out_fd = open(path, flags, mode);
350 } else {
351 out_fd = run_as_open(path, flags, mode, uid, gid);
352 }
fe4477ee
JD
353 if (out_fd < 0) {
354 PERROR("open stream path %s", path);
355 goto error_open;
356 }
357 ret = out_fd;
358
359error_open:
360 free(path_name_id);
361error:
362 return ret;
363}
364
365/*
366 * Change the output tracefile according to the given size and count The
367 * new_count pointer is set during this operation.
368 *
369 * From the consumer, the stream lock MUST be held before calling this function
370 * because we are modifying the stream status.
371 *
372 * Return 0 on success or else a negative value.
373 */
bc182241 374LTTNG_HIDDEN
fe4477ee
JD
375int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size,
376 uint64_t count, int uid, int gid, int out_fd, uint64_t *new_count)
377{
378 int ret;
379
380 ret = close(out_fd);
381 if (ret < 0) {
382 PERROR("Closing tracefile");
383 goto error;
384 }
385
386 if (count > 0) {
387 *new_count = (*new_count + 1) % count;
388 } else {
389 (*new_count)++;
390 }
391
392 return utils_create_stream_file(path_name, file_name, size, *new_count,
393 uid, gid);
394error:
395 return ret;
396}
This page took 0.063832 seconds and 4 git commands to generate.