429d25b3b1a2ffa243d40f2d2cd32abbd1a89f27
[lttng-tools.git] / ltt-sessiond / utils.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; only version 2
8 * of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #define _GNU_SOURCE
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29
30 #include "utils.h"
31
32 /*
33 * Write to writable pipe used to notify a thread.
34 */
35 int notify_thread_pipe(int wpipe)
36 {
37 int ret;
38
39 ret = write(wpipe, "!", 1);
40 if (ret < 0) {
41 perror("write poll pipe");
42 }
43
44 return ret;
45 }
46
47 /*
48 * Return pointer to home directory path using the env variable HOME.
49 *
50 * No home, NULL is returned.
51 */
52 const char *get_home_dir(void)
53 {
54 return ((const char *) getenv("HOME"));
55 }
56
57 /*
58 * Create recursively directory using the FULL path.
59 */
60 int mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
61 {
62 int ret;
63 char *p, tmp[PATH_MAX];
64 size_t len;
65 mode_t old_umask;
66
67 ret = snprintf(tmp, sizeof(tmp), "%s", path);
68 if (ret < 0) {
69 perror("snprintf mkdir");
70 goto error;
71 }
72
73 len = ret;
74 if (tmp[len - 1] == '/') {
75 tmp[len - 1] = 0;
76 }
77
78 old_umask = umask(0);
79 for (p = tmp + 1; *p; p++) {
80 if (*p == '/') {
81 *p = 0;
82 ret = mkdir(tmp, mode);
83 if (ret < 0) {
84 if (!(errno == EEXIST)) {
85 perror("mkdir recursive");
86 ret = -errno;
87 goto umask_error;
88 }
89 } else if (ret == 0) {
90 /*
91 * We created the directory. Set its ownership to the
92 * user/group specified.
93 */
94 ret = chown(tmp, uid, gid);
95 if (ret < 0) {
96 perror("chown in mkdir recursive");
97 ret = -errno;
98 goto umask_error;
99 }
100 }
101 *p = '/';
102 }
103 }
104
105 ret = mkdir(tmp, mode);
106 if (ret < 0) {
107 ret = -errno;
108 } else if (ret == 0) {
109 /*
110 * We created the directory. Set its ownership to the user/group
111 * specified.
112 */
113 ret = chown(tmp, uid, gid);
114 if (ret < 0) {
115 perror("chown in mkdir recursive");
116 ret = -errno;
117 goto umask_error;
118 }
119 }
120
121 umask_error:
122 umask(old_umask);
123 error:
124 return ret;
125 }
This page took 0.047021 seconds and 3 git commands to generate.