Add debug statement
[lttng-tools.git] / lttng-sessiond / utils.c
CommitLineData
8e68d1c8 1/*
996b65c8 2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
1e307fab 3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8e68d1c8 4 *
7272acf5
YB
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.
8e68d1c8 8 *
7272acf5
YB
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.
8e68d1c8 13 *
7272acf5
YB
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.
8e68d1c8
DG
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
7272acf5
YB
29#include <lttngerr.h>
30
8e68d1c8
DG
31#include "utils.h"
32
54d01ffb
DG
33/*
34 * Write to writable pipe used to notify a thread.
35 */
36int notify_thread_pipe(int wpipe)
37{
38 int ret;
39
40 ret = write(wpipe, "!", 1);
41 if (ret < 0) {
7272acf5 42 PERROR("write poll pipe");
54d01ffb
DG
43 }
44
45 return ret;
46}
47
b082db07 48/*
050349bb 49 * Return pointer to home directory path using the env variable HOME.
b082db07 50 *
050349bb 51 * No home, NULL is returned.
b082db07
DG
52 */
53const char *get_home_dir(void)
54{
55 return ((const char *) getenv("HOME"));
56}
57
58/*
050349bb 59 * Create recursively directory using the FULL path.
b082db07 60 */
996b65c8 61int mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
8e68d1c8 62{
49a1d1ed
MD
63 int ret;
64 char *p, tmp[PATH_MAX];
65 size_t len;
66 mode_t old_umask;
8e68d1c8 67
0a962c07
DG
68 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d", path, mode,
69 uid, gid);
70
49a1d1ed 71 ret = snprintf(tmp, sizeof(tmp), "%s", path);
8e68d1c8 72 if (ret < 0) {
7272acf5 73 PERROR("snprintf mkdir");
8e68d1c8
DG
74 goto error;
75 }
76
49a1d1ed
MD
77 len = ret;
78 if (tmp[len - 1] == '/') {
79 tmp[len - 1] = 0;
80 }
8e68d1c8 81
49a1d1ed
MD
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)) {
7272acf5 89 PERROR("mkdir recursive");
996b65c8
MD
90 ret = -errno;
91 goto umask_error;
92 }
93 } else if (ret == 0) {
94 /*
050349bb
DG
95 * We created the directory. Set its ownership to the
96 * user/group specified.
996b65c8
MD
97 */
98 ret = chown(tmp, uid, gid);
99 if (ret < 0) {
7272acf5 100 PERROR("chown in mkdir recursive");
996b65c8 101 ret = -errno;
49a1d1ed
MD
102 goto umask_error;
103 }
104 }
105 *p = '/';
106 }
107 }
8e68d1c8 108
49a1d1ed 109 ret = mkdir(tmp, mode);
8e68d1c8 110 if (ret < 0) {
7272acf5
YB
111 if (!(errno == EEXIST)) {
112 PERROR("mkdir recursive last piece");
113 ret = -errno;
114 } else {
115 ret = 0;
116 }
996b65c8
MD
117 } else if (ret == 0) {
118 /*
050349bb
DG
119 * We created the directory. Set its ownership to the user/group
120 * specified.
996b65c8
MD
121 */
122 ret = chown(tmp, uid, gid);
123 if (ret < 0) {
7272acf5 124 PERROR("chown in mkdir recursive");
996b65c8
MD
125 ret = -errno;
126 goto umask_error;
127 }
8e68d1c8
DG
128 }
129
130umask_error:
131 umask(old_umask);
132error:
49a1d1ed 133 return ret;
8e68d1c8 134}
This page took 0.043272 seconds and 4 git commands to generate.