2 * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 int util_create_path(struct udev *udev, const char *path)
34 char p[UTIL_PATH_SIZE];
39 util_strlcpy(p, path, sizeof(p));
40 pos = strrchr(p, '/');
41 if (pos == p || pos == NULL)
44 while (pos[-1] == '/')
48 dbg(udev, "stat '%s'\n", p);
49 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
52 if (util_create_path(udev, p) != 0)
55 dbg(udev, "mkdir '%s'\n", p);
56 udev_selinux_setfscreatecon(udev, p, S_IFDIR|0755);
58 udev_selinux_resetfscreatecon(udev);
63 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
68 int util_delete_path(struct udev *udev, const char *path)
70 char p[UTIL_PATH_SIZE];
75 pos = strrchr(p, '/');
76 if (pos == p || pos == NULL)
81 pos = strrchr(p, '/');
83 /* don't remove the last one */
84 if ((pos == p) || (pos == NULL))
92 if (errno == ENOTEMPTY)
94 err(udev, "rmdir(%s) failed: %m\n", p);
97 dbg(udev, "removed '%s'\n", p);
102 /* Reset permissions on the device node, before unlinking it to make sure,
103 * that permisions of possible hard links will be removed too.
105 int util_unlink_secure(struct udev *udev, const char *filename)
109 retval = chown(filename, 0, 0);
111 err(udev, "chown(%s, 0, 0) failed: %m\n", filename);
113 retval = chmod(filename, 0000);
115 err(udev, "chmod(%s, 0000) failed: %m\n", filename);
117 retval = unlink(filename);
122 err(udev, "unlink(%s) failed: %m\n", filename);
127 uid_t util_lookup_user(struct udev *udev, const char *user)
132 if (strcmp(user, "root") == 0)
137 if (errno == 0 || errno == ENOENT || errno == ESRCH)
138 err(udev, "specified user '%s' unknown\n", user);
140 err(udev, "error resolving user '%s': %m\n", user);
146 extern gid_t util_lookup_group(struct udev *udev, const char *group)
151 if (strcmp(group, "root") == 0)
154 gr = getgrnam(group);
156 if (errno == 0 || errno == ENOENT || errno == ESRCH)
157 err(udev, "specified group '%s' unknown\n", group);
159 err(udev, "error resolving group '%s': %m\n", group);
165 int util_run_program(struct udev *udev, const char *command, char **envp,
166 char *result, size_t ressize, size_t *reslen)
169 int outpipe[2] = {-1, -1};
170 int errpipe[2] = {-1, -1};
172 char arg[UTIL_PATH_SIZE];
173 char program[UTIL_PATH_SIZE];
174 char *argv[(sizeof(arg) / 2) + 1];
179 /* build argv from command */
180 util_strlcpy(arg, command, sizeof(arg));
182 if (strchr(arg, ' ') != NULL) {
185 while (pos != NULL && pos[0] != '\0') {
186 if (pos[0] == '\'') {
187 /* do not separate quotes */
189 argv[i] = strsep(&pos, "\'");
190 while (pos != NULL && pos[0] == ' ')
193 argv[i] = strsep(&pos, " ");
195 dbg(udev, "arg[%i] '%s'\n", i, argv[i]);
203 info(udev, "'%s'\n", command);
205 /* prepare pipes from child to parent */
206 if (result != NULL || udev_get_log_priority(udev) >= LOG_INFO) {
207 if (pipe(outpipe) != 0) {
208 err(udev, "pipe failed: %m\n");
212 if (udev_get_log_priority(udev) >= LOG_INFO) {
213 if (pipe(errpipe) != 0) {
214 err(udev, "pipe failed: %m\n");
219 /* allow programs in /lib/udev/ to be called without the path */
220 if (strchr(argv[0], '/') == NULL) {
221 util_strlcpy(program, UDEV_PREFIX "/lib/udev/", sizeof(program));
222 util_strlcat(program, argv[0], sizeof(program));
229 /* child closes parent ends of pipes */
230 if (outpipe[READ_END] > 0)
231 close(outpipe[READ_END]);
232 if (errpipe[READ_END] > 0)
233 close(errpipe[READ_END]);
235 /* discard child output or connect to pipe */
236 devnull = open("/dev/null", O_RDWR);
238 dup2(devnull, STDIN_FILENO);
239 if (outpipe[WRITE_END] < 0)
240 dup2(devnull, STDOUT_FILENO);
241 if (errpipe[WRITE_END] < 0)
242 dup2(devnull, STDERR_FILENO);
245 err(udev, "open /dev/null failed: %m\n");
246 if (outpipe[WRITE_END] > 0) {
247 dup2(outpipe[WRITE_END], STDOUT_FILENO);
248 close(outpipe[WRITE_END]);
250 if (errpipe[WRITE_END] > 0) {
251 dup2(errpipe[WRITE_END], STDERR_FILENO);
252 close(errpipe[WRITE_END]);
254 execve(argv[0], argv, envp);
255 if (errno == ENOENT || errno == ENOTDIR) {
256 /* may be on a filesytem which is not mounted right now */
257 info(udev, "program '%s' not found\n", argv[0]);
260 err(udev, "exec of program '%s' failed\n", argv[0]);
264 err(udev, "fork of '%s' failed: %m\n", argv[0]);
267 /* read from child if requested */
268 if (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
272 /* parent closes child ends of pipes */
273 if (outpipe[WRITE_END] > 0)
274 close(outpipe[WRITE_END]);
275 if (errpipe[WRITE_END] > 0)
276 close(errpipe[WRITE_END]);
278 /* read child output */
279 while (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
284 if (outpipe[READ_END] > 0)
285 FD_SET(outpipe[READ_END], &readfds);
286 if (errpipe[READ_END] > 0)
287 FD_SET(errpipe[READ_END], &readfds);
288 fdcount = select(UDEV_MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL);
297 if (outpipe[READ_END] > 0 && FD_ISSET(outpipe[READ_END], &readfds)) {
302 count = read(outpipe[READ_END], inbuf, sizeof(inbuf)-1);
304 close(outpipe[READ_END]);
305 outpipe[READ_END] = -1;
307 err(udev, "stdin read failed: %m\n");
314 /* store result for rule processing */
316 if (respos + count < ressize) {
317 memcpy(&result[respos], inbuf, count);
320 err(udev, "ressize %ld too short\n", (long)ressize);
325 while ((line = strsep(&pos, "\n")))
326 if (pos || line[0] != '\0')
327 info(udev, "'%s' (stdout) '%s'\n", argv[0], line);
331 if (errpipe[READ_END] > 0 && FD_ISSET(errpipe[READ_END], &readfds)) {
336 count = read(errpipe[READ_END], errbuf, sizeof(errbuf)-1);
338 close(errpipe[READ_END]);
339 errpipe[READ_END] = -1;
341 err(udev, "stderr read failed: %m\n");
344 errbuf[count] = '\0';
346 while ((line = strsep(&pos, "\n")))
347 if (pos || line[0] != '\0')
348 info(udev, "'%s' (stderr) '%s'\n", argv[0], line);
351 if (outpipe[READ_END] > 0)
352 close(outpipe[READ_END]);
353 if (errpipe[READ_END] > 0)
354 close(errpipe[READ_END]);
356 /* return the childs stdout string */
358 result[respos] = '\0';
359 dbg(udev, "result='%s'\n", result);
364 waitpid(pid, &status, 0);
365 if (WIFEXITED(status)) {
366 info(udev, "'%s' returned with status %i\n", argv[0], WEXITSTATUS(status));
367 if (WEXITSTATUS(status) != 0)
370 err(udev, "'%s' abnormal exit\n", argv[0]);