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, '/');
37 if (pos == p || pos == NULL)
40 while (pos[-1] == '/')
44 dbg(udev, "stat '%s'\n", p);
45 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
48 if (util_create_path(udev, p) != 0)
51 dbg(udev, "mkdir '%s'\n", p);
52 udev_selinux_setfscreatecon(udev, p, S_IFDIR|0755);
54 if (err != 0 && errno == EEXIST)
55 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
57 udev_selinux_resetfscreatecon(udev);
61 int util_delete_path(struct udev *udev, const char *path)
63 char p[UTIL_PATH_SIZE];
67 util_strscpy(p, sizeof(p), path);
68 pos = strrchr(p, '/');
69 if (pos == p || pos == NULL)
74 pos = strrchr(p, '/');
76 /* don't remove the last one */
77 if ((pos == p) || (pos == NULL))
85 if (errno == ENOTEMPTY)
87 err(udev, "rmdir(%s) failed: %m\n", p);
90 dbg(udev, "removed '%s'\n", p);
95 /* Reset permissions on the device node, before unlinking it to make sure,
96 * that permissions of possible hard links will be removed too.
98 int util_unlink_secure(struct udev *udev, const char *filename)
102 chmod(filename, 0000);
103 chown(filename, 0, 0);
104 err = unlink(filename);
108 err(udev, "unlink(%s) failed: %m\n", filename);
112 uid_t util_lookup_user(struct udev *udev, const char *user)
115 int buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
121 if (strcmp(user, "root") == 0)
123 uid = strtoul(user, &endptr, 10);
124 if (endptr[0] == '\0')
127 errno = getpwnam_r(user, &pwbuf, buf, buflen, &pw);
130 if (errno == 0 || errno == ENOENT || errno == ESRCH)
131 err(udev, "specified user '%s' unknown\n", user);
133 err(udev, "error resolving user '%s': %m\n", user);
137 gid_t util_lookup_group(struct udev *udev, const char *group)
140 int buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
146 if (strcmp(group, "root") == 0)
148 gid = strtoul(group, &endptr, 10);
149 if (endptr[0] == '\0')
156 newbuf = realloc(buf, buflen);
160 errno = getgrnam_r(group, &grbuf, buf, buflen, &gr);
163 } else if (errno == ERANGE) {
166 } else if (errno == 0 || errno == ENOENT || errno == ESRCH) {
167 err(udev, "specified group '%s' unknown\n", group);
169 err(udev, "error resolving group '%s': %m\n", group);
177 /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
178 int util_resolve_subsys_kernel(struct udev *udev, const char *string,
179 char *result, size_t maxsize, int read_value)
181 char temp[UTIL_PATH_SIZE];
184 struct udev_device *dev;
187 if (string[0] != '[')
190 util_strscpy(temp, sizeof(temp), string);
194 sysname = strchr(subsys, '/');
198 sysname = &sysname[1];
200 attr = strchr(sysname, ']');
210 if (read_value && attr == NULL)
213 dev = udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
220 val = udev_device_get_sysattr_value(dev, attr);
222 util_strscpy(result, maxsize, val);
225 info(udev, "value '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result);
231 l = util_strpcpyl(&s, maxsize, udev_device_get_syspath(dev), NULL);
233 util_strpcpyl(&s, l, "/", attr, NULL);
234 info(udev, "path '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result);
236 udev_device_unref(dev);
240 int util_run_program(struct udev *udev, const char *command, char **envp,
241 char *result, size_t ressize, size_t *reslen)
244 int outpipe[2] = {-1, -1};
245 int errpipe[2] = {-1, -1};
247 char arg[UTIL_PATH_SIZE];
248 char program[UTIL_PATH_SIZE];
249 char *argv[(sizeof(arg) / 2) + 1];
254 info(udev, "'%s' started\n", command);
256 /* build argv from command */
257 util_strscpy(arg, sizeof(arg), command);
259 if (strchr(arg, ' ') != NULL) {
262 while (pos != NULL && pos[0] != '\0') {
263 if (pos[0] == '\'') {
264 /* do not separate quotes */
266 argv[i] = strsep(&pos, "\'");
267 while (pos != NULL && pos[0] == ' ')
270 argv[i] = strsep(&pos, " ");
272 dbg(udev, "arg[%i] '%s'\n", i, argv[i]);
281 /* prepare pipes from child to parent */
282 if (result != NULL || udev_get_log_priority(udev) >= LOG_INFO) {
283 if (pipe(outpipe) != 0) {
284 err(udev, "pipe failed: %m\n");
288 if (udev_get_log_priority(udev) >= LOG_INFO) {
289 if (pipe(errpipe) != 0) {
290 err(udev, "pipe failed: %m\n");
295 /* allow programs in /lib/udev/ to be called without the path */
296 if (argv[0][0] != '/') {
297 util_strscpyl(program, sizeof(program), LIBEXECDIR "/", argv[0], NULL);
304 /* child closes parent ends of pipes */
305 if (outpipe[READ_END] > 0)
306 close(outpipe[READ_END]);
307 if (errpipe[READ_END] > 0)
308 close(errpipe[READ_END]);
310 /* discard child output or connect to pipe */
311 devnull = open("/dev/null", O_RDWR);
313 dup2(devnull, STDIN_FILENO);
314 if (outpipe[WRITE_END] < 0)
315 dup2(devnull, STDOUT_FILENO);
316 if (errpipe[WRITE_END] < 0)
317 dup2(devnull, STDERR_FILENO);
320 err(udev, "open /dev/null failed: %m\n");
321 if (outpipe[WRITE_END] > 0) {
322 dup2(outpipe[WRITE_END], STDOUT_FILENO);
323 close(outpipe[WRITE_END]);
325 if (errpipe[WRITE_END] > 0) {
326 dup2(errpipe[WRITE_END], STDERR_FILENO);
327 close(errpipe[WRITE_END]);
329 execve(argv[0], argv, envp);
330 if (errno == ENOENT || errno == ENOTDIR) {
331 /* may be on a filesystem which is not mounted right now */
332 info(udev, "program '%s' not found\n", argv[0]);
335 err(udev, "exec of program '%s' failed\n", argv[0]);
339 err(udev, "fork of '%s' failed: %m\n", argv[0]);
342 /* read from child if requested */
343 if (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
347 /* parent closes child ends of pipes */
348 if (outpipe[WRITE_END] > 0)
349 close(outpipe[WRITE_END]);
350 if (errpipe[WRITE_END] > 0)
351 close(errpipe[WRITE_END]);
353 /* read child output */
354 while (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
359 if (outpipe[READ_END] > 0)
360 FD_SET(outpipe[READ_END], &readfds);
361 if (errpipe[READ_END] > 0)
362 FD_SET(errpipe[READ_END], &readfds);
363 fdcount = select(MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL);
372 if (outpipe[READ_END] > 0 && FD_ISSET(outpipe[READ_END], &readfds)) {
377 count = read(outpipe[READ_END], inbuf, sizeof(inbuf)-1);
379 close(outpipe[READ_END]);
380 outpipe[READ_END] = -1;
382 err(udev, "stdin read failed: %m\n");
389 /* store result for rule processing */
391 if (respos + count < ressize) {
392 memcpy(&result[respos], inbuf, count);
395 err(udev, "ressize %ld too short\n", (long)ressize);
400 while ((line = strsep(&pos, "\n")))
401 if (pos || line[0] != '\0')
402 info(udev, "'%s' (stdout) '%s'\n", argv[0], line);
406 if (errpipe[READ_END] > 0 && FD_ISSET(errpipe[READ_END], &readfds)) {
411 count = read(errpipe[READ_END], errbuf, sizeof(errbuf)-1);
413 close(errpipe[READ_END]);
414 errpipe[READ_END] = -1;
416 err(udev, "stderr read failed: %m\n");
419 errbuf[count] = '\0';
421 while ((line = strsep(&pos, "\n")))
422 if (pos || line[0] != '\0')
423 info(udev, "'%s' (stderr) '%s'\n", argv[0], line);
426 if (outpipe[READ_END] > 0)
427 close(outpipe[READ_END]);
428 if (errpipe[READ_END] > 0)
429 close(errpipe[READ_END]);
431 /* return the childs stdout string */
433 result[respos] = '\0';
434 dbg(udev, "result='%s'\n", result);
439 waitpid(pid, &status, 0);
440 if (WIFEXITED(status)) {
441 info(udev, "'%s' returned with exitcode %i\n", command, WEXITSTATUS(status));
442 if (WEXITSTATUS(status) != 0)
445 err(udev, "'%s' unexpected exit with status 0x%04x\n", command, status);