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>
45 * Create recursively directory using the FULL path.
48 int _mkdir_recursive(void *_data
)
50 struct mkdir_data
*data
= _data
;
52 char *p
, tmp
[PATH_MAX
];
61 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
63 PERROR("snprintf mkdir");
68 if (tmp
[len
- 1] == '/') {
72 for (p
= tmp
+ 1; *p
; p
++) {
75 ret
= stat(tmp
, &statbuf
);
77 ret
= mkdir(tmp
, mode
);
79 if (!(errno
== EEXIST
)) {
80 PERROR("mkdir recursive");
90 ret
= mkdir(tmp
, mode
);
92 if (!(errno
== EEXIST
)) {
93 PERROR("mkdir recursive last piece");
105 int _mkdir(void *_data
)
107 struct mkdir_data
*data
= _data
;
108 return mkdir(data
->path
, data
->mode
);
112 int _open(void *_data
)
114 struct open_data
*data
= _data
;
115 return open(data
->path
, data
->flags
, data
->mode
);
119 int run_as(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
125 * If we are non-root, we can only deal with our own uid.
127 if (geteuid() != 0) {
128 if (uid
!= geteuid()) {
129 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
141 * Parent: wait for child to return, in which case the
142 * shared memory map will have been created.
145 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
150 } else if (pid
== 0) {
175 int mkdir_recursive_run_as(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
177 struct mkdir_data data
;
179 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
180 path
, mode
, uid
, gid
);
183 return run_as(_mkdir_recursive
, &data
, uid
, gid
);
186 int mkdir_run_as(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
188 struct mkdir_data data
;
190 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
191 path
, mode
, uid
, gid
);
194 return run_as(_mkdir
, &data
, uid
, gid
);
198 * Note: open_run_as is currently not working. We'd need to pass the fd
199 * opened in the child to the parent.
201 int open_run_as(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
203 //struct open_data data;
206 DBG3("open() %s with flags %d mode %d for uid %d and gid %d",
207 path
, flags
, mode
, uid
, gid
);
208 fd
= open(path
, flags
, mode
);
213 ret
= fchown(fd
, uid
, gid
);
224 return run_as(_open
, &data
, uid
, gid
);
This page took 0.035269 seconds and 5 git commands to generate.