2 * libudev - interface to udev device information
4 * Copyright (C) 2004-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 udev_selinux_resetfscreatecon(udev);
59 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
64 int util_delete_path(struct udev *udev, const char *path)
66 char p[UTIL_PATH_SIZE];
71 pos = strrchr(p, '/');
72 if (pos == p || pos == NULL)
77 pos = strrchr(p, '/');
79 /* don't remove the last one */
80 if ((pos == p) || (pos == NULL))
88 if (errno == ENOTEMPTY)
90 err(udev, "rmdir(%s) failed: %m\n", p);
93 dbg(udev, "removed '%s'\n", p);
98 /* Reset permissions on the device node, before unlinking it to make sure,
99 * that permisions of possible hard links will be removed too.
101 int util_unlink_secure(struct udev *udev, const char *filename)
105 retval = chown(filename, 0, 0);
107 err(udev, "chown(%s, 0, 0) failed: %m\n", filename);
109 retval = chmod(filename, 0000);
111 err(udev, "chmod(%s, 0000) failed: %m\n", filename);
113 retval = unlink(filename);
118 err(udev, "unlink(%s) failed: %m\n", filename);
123 uid_t util_lookup_user(struct udev *udev, const char *user)
126 int buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
132 if (strcmp(user, "root") == 0)
134 uid = strtoul(user, &endptr, 10);
135 if (endptr[0] == '\0')
139 getpwnam_r(user, &pwbuf, buf, buflen, &pw);
142 if (errno == 0 || errno == ENOENT || errno == ESRCH)
143 err(udev, "specified user '%s' unknown\n", user);
145 err(udev, "error resolving user '%s': %m\n", user);
149 gid_t util_lookup_group(struct udev *udev, const char *group)
152 int buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
158 if (strcmp(group, "root") == 0)
160 gid = strtoul(group, &endptr, 10);
161 if (endptr[0] == '\0')
165 getgrnam_r(group, &grbuf, buf, buflen, &gr);
168 if (errno == 0 || errno == ENOENT || errno == ESRCH)
169 err(udev, "specified group '%s' unknown\n", group);
171 err(udev, "error resolving group '%s': %m\n", group);
175 /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
176 int util_resolve_subsys_kernel(struct udev *udev, const char *string,
177 char *result, size_t maxsize, int read_value)
179 char temp[UTIL_PATH_SIZE];
182 struct udev_device *dev;
185 if (string[0] != '[')
188 util_strscpy(temp, sizeof(temp), string);
192 sysname = strchr(subsys, '/');
196 sysname = &sysname[1];
198 attr = strchr(sysname, ']');
208 if (read_value && attr == NULL)
211 dev = udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
218 val = udev_device_get_sysattr_value(dev, attr);
220 util_strscpy(result, maxsize, val);
223 info(udev, "value '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result);
229 l = util_strpcpyl(&s, maxsize, udev_device_get_syspath(dev), NULL);
231 util_strpcpyl(&s, l, "/", attr, NULL);
232 info(udev, "path '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result);
234 udev_device_unref(dev);
238 int util_run_program(struct udev *udev, const char *command, char **envp,
239 char *result, size_t ressize, size_t *reslen)
242 int outpipe[2] = {-1, -1};
243 int errpipe[2] = {-1, -1};
245 char arg[UTIL_PATH_SIZE];
246 char program[UTIL_PATH_SIZE];
247 char *argv[(sizeof(arg) / 2) + 1];
252 /* build argv from command */
253 util_strscpy(arg, sizeof(arg), command);
255 if (strchr(arg, ' ') != NULL) {
258 while (pos != NULL && pos[0] != '\0') {
259 if (pos[0] == '\'') {
260 /* do not separate quotes */
262 argv[i] = strsep(&pos, "\'");
263 while (pos != NULL && pos[0] == ' ')
266 argv[i] = strsep(&pos, " ");
268 dbg(udev, "arg[%i] '%s'\n", i, argv[i]);
276 info(udev, "'%s'\n", command);
278 /* prepare pipes from child to parent */
279 if (result != NULL || udev_get_log_priority(udev) >= LOG_INFO) {
280 if (pipe(outpipe) != 0) {
281 err(udev, "pipe failed: %m\n");
285 if (udev_get_log_priority(udev) >= LOG_INFO) {
286 if (pipe(errpipe) != 0) {
287 err(udev, "pipe failed: %m\n");
292 /* allow programs in /lib/udev/ to be called without the path */
293 if (argv[0][0] != '/') {
294 util_strscpyl(program, sizeof(program), LIBEXECDIR "/", argv[0], NULL);
301 /* child closes parent ends of pipes */
302 if (outpipe[READ_END] > 0)
303 close(outpipe[READ_END]);
304 if (errpipe[READ_END] > 0)
305 close(errpipe[READ_END]);
307 /* discard child output or connect to pipe */
308 devnull = open("/dev/null", O_RDWR);
310 dup2(devnull, STDIN_FILENO);
311 if (outpipe[WRITE_END] < 0)
312 dup2(devnull, STDOUT_FILENO);
313 if (errpipe[WRITE_END] < 0)
314 dup2(devnull, STDERR_FILENO);
317 err(udev, "open /dev/null failed: %m\n");
318 if (outpipe[WRITE_END] > 0) {
319 dup2(outpipe[WRITE_END], STDOUT_FILENO);
320 close(outpipe[WRITE_END]);
322 if (errpipe[WRITE_END] > 0) {
323 dup2(errpipe[WRITE_END], STDERR_FILENO);
324 close(errpipe[WRITE_END]);
326 execve(argv[0], argv, envp);
327 if (errno == ENOENT || errno == ENOTDIR) {
328 /* may be on a filesytem which is not mounted right now */
329 info(udev, "program '%s' not found\n", argv[0]);
332 err(udev, "exec of program '%s' failed\n", argv[0]);
336 err(udev, "fork of '%s' failed: %m\n", argv[0]);
339 /* read from child if requested */
340 if (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
344 /* parent closes child ends of pipes */
345 if (outpipe[WRITE_END] > 0)
346 close(outpipe[WRITE_END]);
347 if (errpipe[WRITE_END] > 0)
348 close(errpipe[WRITE_END]);
350 /* read child output */
351 while (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
356 if (outpipe[READ_END] > 0)
357 FD_SET(outpipe[READ_END], &readfds);
358 if (errpipe[READ_END] > 0)
359 FD_SET(errpipe[READ_END], &readfds);
360 fdcount = select(MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL);
369 if (outpipe[READ_END] > 0 && FD_ISSET(outpipe[READ_END], &readfds)) {
374 count = read(outpipe[READ_END], inbuf, sizeof(inbuf)-1);
376 close(outpipe[READ_END]);
377 outpipe[READ_END] = -1;
379 err(udev, "stdin read failed: %m\n");
386 /* store result for rule processing */
388 if (respos + count < ressize) {
389 memcpy(&result[respos], inbuf, count);
392 err(udev, "ressize %ld too short\n", (long)ressize);
397 while ((line = strsep(&pos, "\n")))
398 if (pos || line[0] != '\0')
399 info(udev, "'%s' (stdout) '%s'\n", argv[0], line);
403 if (errpipe[READ_END] > 0 && FD_ISSET(errpipe[READ_END], &readfds)) {
408 count = read(errpipe[READ_END], errbuf, sizeof(errbuf)-1);
410 close(errpipe[READ_END]);
411 errpipe[READ_END] = -1;
413 err(udev, "stderr read failed: %m\n");
416 errbuf[count] = '\0';
418 while ((line = strsep(&pos, "\n")))
419 if (pos || line[0] != '\0')
420 info(udev, "'%s' (stderr) '%s'\n", argv[0], line);
423 if (outpipe[READ_END] > 0)
424 close(outpipe[READ_END]);
425 if (errpipe[READ_END] > 0)
426 close(errpipe[READ_END]);
428 /* return the childs stdout string */
430 result[respos] = '\0';
431 dbg(udev, "result='%s'\n", result);
436 waitpid(pid, &status, 0);
437 if (WIFEXITED(status)) {
438 info(udev, "'%s' returned with exitcode %i\n", command, WEXITSTATUS(status));
439 if (WEXITSTATUS(status) != 0)
442 err(udev, "'%s' unexpected exit with status 0x%04x\n", command, status);