1e08de77d4ca2854611c6a7372a769a9c7bdeb5e
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>
47 * Write to writable pipe used to notify a thread.
49 int notify_thread_pipe(int wpipe
)
53 ret
= write(wpipe
, "!", 1);
55 PERROR("write poll pipe");
62 * Return pointer to home directory path using the env variable HOME.
64 * No home, NULL is returned.
66 const char *get_home_dir(void)
68 return ((const char *) getenv("HOME"));
72 * Create recursively directory using the FULL path.
75 int _mkdir_recursive(void *_data
)
77 struct mkdir_data
*data
= _data
;
79 char *p
, tmp
[PATH_MAX
];
88 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
90 PERROR("snprintf mkdir");
95 if (tmp
[len
- 1] == '/') {
99 for (p
= tmp
+ 1; *p
; p
++) {
102 ret
= stat(tmp
, &statbuf
);
104 ret
= mkdir(tmp
, mode
);
106 if (!(errno
== EEXIST
)) {
107 PERROR("mkdir recursive");
117 ret
= mkdir(tmp
, mode
);
119 if (!(errno
== EEXIST
)) {
120 PERROR("mkdir recursive last piece");
132 int _mkdir(void *_data
)
134 struct mkdir_data
*data
= _data
;
135 return mkdir(data
->path
, data
->mode
);
139 int _open(void *_data
)
141 struct open_data
*data
= _data
;
142 return open(data
->path
, data
->flags
, data
->mode
);
146 int run_as(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
152 * If we are non-root, we can only deal with our own uid.
154 if (geteuid() != 0) {
155 if (uid
!= geteuid()) {
156 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
168 * Parent: wait for child to return, in which case the
169 * shared memory map will have been created.
172 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
177 } else if (pid
== 0) {
202 int mkdir_recursive_run_as(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
204 struct mkdir_data data
;
206 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
207 path
, mode
, uid
, gid
);
210 return run_as(_mkdir_recursive
, &data
, uid
, gid
);
213 int mkdir_run_as(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
215 struct mkdir_data data
;
217 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
218 path
, mode
, uid
, gid
);
221 return run_as(_mkdir
, &data
, uid
, gid
);
224 int open_run_as(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
226 struct open_data data
;
228 DBG3("open() %s with flags %d mode %d for uid %d and gid %d",
229 path
, flags
, mode
, uid
, gid
);
233 return run_as(_open
, &data
, uid
, gid
);
This page took 0.034352 seconds and 4 git commands to generate.