Add missing rcu read lock
[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
49a1d1ed 68 ret = snprintf(tmp, sizeof(tmp), "%s", path);
8e68d1c8 69 if (ret < 0) {
7272acf5 70 PERROR("snprintf mkdir");
8e68d1c8
DG
71 goto error;
72 }
73
49a1d1ed
MD
74 len = ret;
75 if (tmp[len - 1] == '/') {
76 tmp[len - 1] = 0;
77 }
8e68d1c8 78
49a1d1ed
MD
79 old_umask = umask(0);
80 for (p = tmp + 1; *p; p++) {
81 if (*p == '/') {
82 *p = 0;
83 ret = mkdir(tmp, mode);
84 if (ret < 0) {
85 if (!(errno == EEXIST)) {
7272acf5 86 PERROR("mkdir recursive");
996b65c8
MD
87 ret = -errno;
88 goto umask_error;
89 }
90 } else if (ret == 0) {
91 /*
050349bb
DG
92 * We created the directory. Set its ownership to the
93 * user/group specified.
996b65c8
MD
94 */
95 ret = chown(tmp, uid, gid);
96 if (ret < 0) {
7272acf5 97 PERROR("chown in mkdir recursive");
996b65c8 98 ret = -errno;
49a1d1ed
MD
99 goto umask_error;
100 }
101 }
102 *p = '/';
103 }
104 }
8e68d1c8 105
49a1d1ed 106 ret = mkdir(tmp, mode);
8e68d1c8 107 if (ret < 0) {
7272acf5
YB
108 if (!(errno == EEXIST)) {
109 PERROR("mkdir recursive last piece");
110 ret = -errno;
111 } else {
112 ret = 0;
113 }
996b65c8
MD
114 } else if (ret == 0) {
115 /*
050349bb
DG
116 * We created the directory. Set its ownership to the user/group
117 * specified.
996b65c8
MD
118 */
119 ret = chown(tmp, uid, gid);
120 if (ret < 0) {
7272acf5 121 PERROR("chown in mkdir recursive");
996b65c8
MD
122 ret = -errno;
123 goto umask_error;
124 }
8e68d1c8
DG
125 }
126
127umask_error:
128 umask(old_umask);
129error:
49a1d1ed 130 return ret;
8e68d1c8 131}
This page took 0.030006 seconds and 4 git commands to generate.