Add TODO file
[lttng-tools.git] / ltt-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
DG
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
82a3637f
DG
7 * as published by the Free Software Foundation; only version 2
8 * of the License.
8e68d1c8
DG
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
54d01ffb
DG
32/*
33 * Write to writable pipe used to notify a thread.
34 */
35int 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
b082db07 47/*
050349bb 48 * Return pointer to home directory path using the env variable HOME.
b082db07 49 *
050349bb 50 * No home, NULL is returned.
b082db07
DG
51 */
52const char *get_home_dir(void)
53{
54 return ((const char *) getenv("HOME"));
55}
56
57/*
050349bb 58 * Create recursively directory using the FULL path.
b082db07 59 */
996b65c8 60int mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
8e68d1c8 61{
49a1d1ed
MD
62 int ret;
63 char *p, tmp[PATH_MAX];
64 size_t len;
65 mode_t old_umask;
8e68d1c8 66
49a1d1ed 67 ret = snprintf(tmp, sizeof(tmp), "%s", path);
8e68d1c8
DG
68 if (ret < 0) {
69 perror("snprintf mkdir");
70 goto error;
71 }
72
49a1d1ed
MD
73 len = ret;
74 if (tmp[len - 1] == '/') {
75 tmp[len - 1] = 0;
76 }
8e68d1c8 77
49a1d1ed
MD
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)) {
8e68d1c8 85 perror("mkdir recursive");
996b65c8
MD
86 ret = -errno;
87 goto umask_error;
88 }
89 } else if (ret == 0) {
90 /*
050349bb
DG
91 * We created the directory. Set its ownership to the
92 * user/group specified.
996b65c8
MD
93 */
94 ret = chown(tmp, uid, gid);
95 if (ret < 0) {
96 perror("chown in mkdir recursive");
97 ret = -errno;
49a1d1ed
MD
98 goto umask_error;
99 }
100 }
101 *p = '/';
102 }
103 }
8e68d1c8 104
49a1d1ed 105 ret = mkdir(tmp, mode);
8e68d1c8 106 if (ret < 0) {
996b65c8
MD
107 ret = -errno;
108 } else if (ret == 0) {
109 /*
050349bb
DG
110 * We created the directory. Set its ownership to the user/group
111 * specified.
996b65c8
MD
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 }
8e68d1c8
DG
119 }
120
121umask_error:
122 umask(old_umask);
123error:
49a1d1ed 124 return ret;
8e68d1c8 125}
This page took 0.027828 seconds and 4 git commands to generate.