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