2 * libudev - interface to udev device information
4 * Copyright (C) 2003-2009 Kay Sievers <kay.sievers@vrfy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
23 #include <sys/param.h>
26 #include "libudev-private.h"
28 int util_create_path(struct udev *udev, const char *path)
30 char p[UTIL_PATH_SIZE];
35 util_strscpy(p, sizeof(p), path);
36 pos = strrchr(p, '/');
39 while (pos != p && pos[-1] == '/')
45 dbg(udev, "stat '%s'\n", p);
46 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
49 if (util_create_path(udev, p) != 0)
52 dbg(udev, "mkdir '%s'\n", p);
53 udev_selinux_setfscreatecon(udev, p, S_IFDIR|0755);
55 if (err != 0 && errno == EEXIST)
56 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
58 udev_selinux_resetfscreatecon(udev);
62 int util_delete_path(struct udev *udev, const char *path)
64 char p[UTIL_PATH_SIZE];
71 util_strscpy(p, sizeof(p), path);
72 pos = strrchr(p, '/');
73 if (pos == p || pos == NULL)
78 pos = strrchr(p, '/');
80 /* don't remove the last one */
81 if ((pos == p) || (pos == NULL))
89 if (errno == ENOTEMPTY)
91 err(udev, "rmdir(%s) failed: %m\n", p);
94 dbg(udev, "removed '%s'\n", p);
99 /* Reset permissions on the device node, before unlinking it to make sure,
100 * that permissions of possible hard links will be removed too.
102 int util_unlink_secure(struct udev *udev, const char *filename)
106 chmod(filename, 0000);
107 chown(filename, 0, 0);
108 err = unlink(filename);
112 err(udev, "unlink(%s) failed: %m\n", filename);
116 uid_t util_lookup_user(struct udev *udev, const char *user)
119 int buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
125 if (strcmp(user, "root") == 0)
127 uid = strtoul(user, &endptr, 10);
128 if (endptr[0] == '\0')
131 errno = getpwnam_r(user, &pwbuf, buf, buflen, &pw);
134 if (errno == 0 || errno == ENOENT || errno == ESRCH)
135 err(udev, "specified user '%s' unknown\n", user);
137 err(udev, "error resolving user '%s': %m\n", user);
141 gid_t util_lookup_group(struct udev *udev, const char *group)
144 int buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
150 if (strcmp(group, "root") == 0)
152 gid = strtoul(group, &endptr, 10);
153 if (endptr[0] == '\0')
160 newbuf = realloc(buf, buflen);
164 errno = getgrnam_r(group, &grbuf, buf, buflen, &gr);
167 } else if (errno == ERANGE) {
170 } else if (errno == 0 || errno == ENOENT || errno == ESRCH) {
171 err(udev, "specified group '%s' unknown\n", group);
173 err(udev, "error resolving group '%s': %m\n", group);
181 /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
182 int util_resolve_subsys_kernel(struct udev *udev, const char *string,
183 char *result, size_t maxsize, int read_value)
185 char temp[UTIL_PATH_SIZE];
188 struct udev_device *dev;
191 if (string[0] != '[')
194 util_strscpy(temp, sizeof(temp), string);
198 sysname = strchr(subsys, '/');
202 sysname = &sysname[1];
204 attr = strchr(sysname, ']');
214 if (read_value && attr == NULL)
217 dev = udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
224 val = udev_device_get_sysattr_value(dev, attr);
226 util_strscpy(result, maxsize, val);
229 info(udev, "value '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result);
235 l = util_strpcpyl(&s, maxsize, udev_device_get_syspath(dev), NULL);
237 util_strpcpyl(&s, l, "/", attr, NULL);
238 info(udev, "path '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result);
240 udev_device_unref(dev);
244 int util_run_program(struct udev *udev, const char *command, char **envp,
245 char *result, size_t ressize, size_t *reslen)
248 int outpipe[2] = {-1, -1};
249 int errpipe[2] = {-1, -1};
251 char arg[UTIL_PATH_SIZE];
252 char program[UTIL_PATH_SIZE];
253 char *argv[(sizeof(arg) / 2) + 1];
258 info(udev, "'%s' started\n", command);
260 /* build argv from command */
261 util_strscpy(arg, sizeof(arg), command);
263 if (strchr(arg, ' ') != NULL) {
266 while (pos != NULL && pos[0] != '\0') {
267 if (pos[0] == '\'') {
268 /* do not separate quotes */
270 argv[i] = strsep(&pos, "\'");
271 while (pos != NULL && pos[0] == ' ')
274 argv[i] = strsep(&pos, " ");
276 dbg(udev, "arg[%i] '%s'\n", i, argv[i]);
285 /* prepare pipes from child to parent */
286 if (result != NULL || udev_get_log_priority(udev) >= LOG_INFO) {
287 if (pipe(outpipe) != 0) {
288 err(udev, "pipe failed: %m\n");
292 if (udev_get_log_priority(udev) >= LOG_INFO) {
293 if (pipe(errpipe) != 0) {
294 err(udev, "pipe failed: %m\n");
299 /* allow programs in /lib/udev/ to be called without the path */
300 if (argv[0][0] != '/') {
301 util_strscpyl(program, sizeof(program), LIBEXECDIR "/", argv[0], NULL);
308 /* child closes parent ends of pipes */
309 if (outpipe[READ_END] > 0)
310 close(outpipe[READ_END]);
311 if (errpipe[READ_END] > 0)
312 close(errpipe[READ_END]);
314 /* discard child output or connect to pipe */
315 devnull = open("/dev/null", O_RDWR);
317 dup2(devnull, STDIN_FILENO);
318 if (outpipe[WRITE_END] < 0)
319 dup2(devnull, STDOUT_FILENO);
320 if (errpipe[WRITE_END] < 0)
321 dup2(devnull, STDERR_FILENO);
324 err(udev, "open /dev/null failed: %m\n");
325 if (outpipe[WRITE_END] > 0) {
326 dup2(outpipe[WRITE_END], STDOUT_FILENO);
327 close(outpipe[WRITE_END]);
329 if (errpipe[WRITE_END] > 0) {
330 dup2(errpipe[WRITE_END], STDERR_FILENO);
331 close(errpipe[WRITE_END]);
333 execve(argv[0], argv, envp);
334 if (errno == ENOENT || errno == ENOTDIR) {
335 /* may be on a filesystem which is not mounted right now */
336 info(udev, "program '%s' not found\n", argv[0]);
339 err(udev, "exec of program '%s' failed\n", argv[0]);
343 err(udev, "fork of '%s' failed: %m\n", argv[0]);
346 /* read from child if requested */
347 if (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
351 /* parent closes child ends of pipes */
352 if (outpipe[WRITE_END] > 0)
353 close(outpipe[WRITE_END]);
354 if (errpipe[WRITE_END] > 0)
355 close(errpipe[WRITE_END]);
357 /* read child output */
358 while (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
363 if (outpipe[READ_END] > 0)
364 FD_SET(outpipe[READ_END], &readfds);
365 if (errpipe[READ_END] > 0)
366 FD_SET(errpipe[READ_END], &readfds);
367 fdcount = select(MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL);
376 if (outpipe[READ_END] > 0 && FD_ISSET(outpipe[READ_END], &readfds)) {
381 count = read(outpipe[READ_END], inbuf, sizeof(inbuf)-1);
383 close(outpipe[READ_END]);
384 outpipe[READ_END] = -1;
386 err(udev, "stdin read failed: %m\n");
393 /* store result for rule processing */
395 if (respos + count < ressize) {
396 memcpy(&result[respos], inbuf, count);
399 err(udev, "ressize %ld too short\n", (long)ressize);
404 while ((line = strsep(&pos, "\n")))
405 if (pos || line[0] != '\0')
406 info(udev, "'%s' (stdout) '%s'\n", argv[0], line);
410 if (errpipe[READ_END] > 0 && FD_ISSET(errpipe[READ_END], &readfds)) {
415 count = read(errpipe[READ_END], errbuf, sizeof(errbuf)-1);
417 close(errpipe[READ_END]);
418 errpipe[READ_END] = -1;
420 err(udev, "stderr read failed: %m\n");
423 errbuf[count] = '\0';
425 while ((line = strsep(&pos, "\n")))
426 if (pos || line[0] != '\0')
427 info(udev, "'%s' (stderr) '%s'\n", argv[0], line);
430 if (outpipe[READ_END] > 0)
431 close(outpipe[READ_END]);
432 if (errpipe[READ_END] > 0)
433 close(errpipe[READ_END]);
435 /* return the childs stdout string */
437 result[respos] = '\0';
438 dbg(udev, "result='%s'\n", result);
443 waitpid(pid, &status, 0);
444 if (WIFEXITED(status)) {
445 info(udev, "'%s' returned with exitcode %i\n", command, WEXITSTATUS(status));
446 if (WEXITSTATUS(status) != 0)
449 err(udev, "'%s' unexpected exit with status 0x%04x\n", command, status);