2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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
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.
26 #include <sys/types.h>
35 * Write to writable pipe used to notify a thread.
37 int notify_thread_pipe(int wpipe
)
41 ret
= write(wpipe
, "!", 1);
43 PERROR("write poll pipe");
50 * Return pointer to home directory path using the env variable HOME.
52 * No home, NULL is returned.
54 const char *get_home_dir(void)
56 return ((const char *) getenv("HOME"));
60 * Create recursively directory using the FULL path.
63 int _mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
66 char *p
, tmp
[PATH_MAX
];
70 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
72 PERROR("snprintf mkdir");
77 if (tmp
[len
- 1] == '/') {
82 for (p
= tmp
+ 1; *p
; p
++) {
85 ret
= mkdir(tmp
, mode
);
87 if (!(errno
== EEXIST
)) {
88 PERROR("mkdir recursive");
97 ret
= mkdir(tmp
, mode
);
99 if (!(errno
== EEXIST
)) {
100 PERROR("mkdir recursive last piece");
114 int run_as(int (*cmd
)(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
),
115 const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
121 * If we are non-root, we can only deal with our own uid.
123 if (geteuid() != 0) {
124 if (uid
!= geteuid()) {
125 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
129 return (*cmd
)(path
, mode
, uid
, gid
);
137 * Parent: wait for child to return, in which case the
138 * shared memory map will have been created.
141 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
146 } else if (pid
== 0) {
158 ret
= (*cmd
)(path
, mode
, uid
, gid
);
170 int mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
172 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
173 path
, mode
, uid
, gid
);
174 return run_as(_mkdir_recursive
, path
, mode
, uid
, gid
);
This page took 0.032169 seconds and 4 git commands to generate.