2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
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.
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
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.
26 #include <common/common.h>
31 * Return the realpath(3) of the path even if the last directory token does not
32 * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
33 * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
34 * fails if the end point directory does not exist.
36 __attribute__((visibility("hidden")))
37 char *utils_expand_path(const char *path
)
39 const char *end_path
= path
;
40 char *next
, *cut_path
= NULL
, *expanded_path
= NULL
;
47 /* Find last token delimited by '/' */
48 while ((next
= strpbrk(end_path
+ 1, "/"))) {
52 /* Cut last token from original path */
53 cut_path
= strndup(path
, end_path
- path
);
55 expanded_path
= zmalloc(PATH_MAX
);
56 if (expanded_path
== NULL
) {
57 PERROR("zmalloc expand path");
61 expanded_path
= realpath((char *)cut_path
, expanded_path
);
62 if (expanded_path
== NULL
) {
65 ERR("%s: No such file or directory", cut_path
);
68 PERROR("realpath utils expand path");
74 /* Add end part to expanded path */
75 strncat(expanded_path
, end_path
, PATH_MAX
- strlen(expanded_path
) - 1);
87 * Create a pipe in dst.
89 __attribute__((visibility("hidden")))
90 int utils_create_pipe(int *dst
)
100 PERROR("create pipe");
107 * Create pipe and set CLOEXEC flag to both fd.
109 * Make sure the pipe opened by this function are closed at some point. Use
110 * utils_close_pipe().
112 __attribute__((visibility("hidden")))
113 int utils_create_pipe_cloexec(int *dst
)
121 ret
= utils_create_pipe(dst
);
126 for (i
= 0; i
< 2; i
++) {
127 ret
= fcntl(dst
[i
], F_SETFD
, FD_CLOEXEC
);
129 PERROR("fcntl pipe cloexec");
139 * Close both read and write side of the pipe.
141 __attribute__((visibility("hidden")))
142 void utils_close_pipe(int *src
)
150 for (i
= 0; i
< 2; i
++) {
158 PERROR("close pipe");
164 * Create a new string using two strings range.
166 __attribute__((visibility("hidden")))
167 char *utils_strdupdelim(const char *begin
, const char *end
)
171 str
= zmalloc(end
- begin
+ 1);
173 PERROR("zmalloc strdupdelim");
177 memcpy(str
, begin
, end
- begin
);
178 str
[end
- begin
] = '\0';
185 * Set CLOEXEC flag to the give file descriptor.
187 __attribute__((visibility("hidden")))
188 int utils_set_fd_cloexec(int fd
)
197 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
199 PERROR("fcntl cloexec");
208 * Create pid file to the given path and filename.
210 __attribute__((visibility("hidden")))
211 int utils_create_pid_file(pid_t pid
, const char *filepath
)
218 fp
= fopen(filepath
, "w");
220 PERROR("open pid file %s", filepath
);
225 ret
= fprintf(fp
, "%d\n", pid
);
227 PERROR("fprintf pid file");
231 DBG("Pid %d written in file %s", pid
, filepath
);
This page took 0.034085 seconds and 4 git commands to generate.