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];
70 util_strscpy(p, sizeof(p), path);
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 permissions of possible hard links will be removed too.
101 int util_unlink_secure(struct udev *udev, const char *filename)
105 chmod(filename, 0000);
106 chown(filename, 0, 0);
107 err = unlink(filename);
111 err(udev, "unlink(%s) failed: %m\n", filename);
115 uid_t util_lookup_user(struct udev *udev, const char *user)
118 int buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
124 if (strcmp(user, "root") == 0)
126 uid = strtoul(user, &endptr, 10);
127 if (endptr[0] == '\0')
130 errno = getpwnam_r(user, &pwbuf, buf, buflen, &pw);
133 if (errno == 0 || errno == ENOENT || errno == ESRCH)
134 err(udev, "specified user '%s' unknown\n", user);
136 err(udev, "error resolving user '%s': %m\n", user);
140 gid_t util_lookup_group(struct udev *udev, const char *group)
143 int buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
149 if (strcmp(group, "root") == 0)
151 gid = strtoul(group, &endptr, 10);
152 if (endptr[0] == '\0')
159 newbuf = realloc(buf, buflen);
163 errno = getgrnam_r(group, &grbuf, buf, buflen, &gr);
166 } else if (errno == ERANGE) {
169 } else if (errno == 0 || errno == ENOENT || errno == ESRCH) {
170 err(udev, "specified group '%s' unknown\n", group);
172 err(udev, "error resolving group '%s': %m\n", group);
180 /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
181 int util_resolve_subsys_kernel(struct udev *udev, const char *string,
182 char *result, size_t maxsize, int read_value)
184 char temp[UTIL_PATH_SIZE];
187 struct udev_device *dev;
190 if (string[0] != '[')
193 util_strscpy(temp, sizeof(temp), string);
197 sysname = strchr(subsys, '/');
201 sysname = &sysname[1];
203 attr = strchr(sysname, ']');
213 if (read_value && attr == NULL)
216 dev = udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
223 val = udev_device_get_sysattr_value(dev, attr);
225 util_strscpy(result, maxsize, val);
228 info(udev, "value '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result);
234 l = util_strpcpyl(&s, maxsize, udev_device_get_syspath(dev), NULL);
236 util_strpcpyl(&s, l, "/", attr, NULL);
237 info(udev, "path '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result);
239 udev_device_unref(dev);
243 int util_run_program(struct udev *udev, const char *command, char **envp,
244 char *result, size_t ressize, size_t *reslen)
247 int outpipe[2] = {-1, -1};
248 int errpipe[2] = {-1, -1};
250 char arg[UTIL_PATH_SIZE];
251 char program[UTIL_PATH_SIZE];
252 char *argv[(sizeof(arg) / 2) + 1];
257 info(udev, "'%s' started\n", command);
259 /* build argv from command */
260 util_strscpy(arg, sizeof(arg), command);
262 if (strchr(arg, ' ') != NULL) {
265 while (pos != NULL && pos[0] != '\0') {
266 if (pos[0] == '\'') {
267 /* do not separate quotes */
269 argv[i] = strsep(&pos, "\'");
270 while (pos != NULL && pos[0] == ' ')
273 argv[i] = strsep(&pos, " ");
275 dbg(udev, "arg[%i] '%s'\n", i, argv[i]);
284 /* prepare pipes from child to parent */
285 if (result != NULL || udev_get_log_priority(udev) >= LOG_INFO) {
286 if (pipe(outpipe) != 0) {
287 err(udev, "pipe failed: %m\n");
291 if (udev_get_log_priority(udev) >= LOG_INFO) {
292 if (pipe(errpipe) != 0) {
293 err(udev, "pipe failed: %m\n");
298 /* allow programs in /lib/udev/ to be called without the path */
299 if (argv[0][0] != '/') {
300 util_strscpyl(program, sizeof(program), LIBEXECDIR "/", argv[0], NULL);
307 /* child closes parent ends of pipes */
308 if (outpipe[READ_END] > 0)
309 close(outpipe[READ_END]);
310 if (errpipe[READ_END] > 0)
311 close(errpipe[READ_END]);
313 /* discard child output or connect to pipe */
314 devnull = open("/dev/null", O_RDWR);
316 dup2(devnull, STDIN_FILENO);
317 if (outpipe[WRITE_END] < 0)
318 dup2(devnull, STDOUT_FILENO);
319 if (errpipe[WRITE_END] < 0)
320 dup2(devnull, STDERR_FILENO);
323 err(udev, "open /dev/null failed: %m\n");
324 if (outpipe[WRITE_END] > 0) {
325 dup2(outpipe[WRITE_END], STDOUT_FILENO);
326 close(outpipe[WRITE_END]);
328 if (errpipe[WRITE_END] > 0) {
329 dup2(errpipe[WRITE_END], STDERR_FILENO);
330 close(errpipe[WRITE_END]);
332 execve(argv[0], argv, envp);
333 if (errno == ENOENT || errno == ENOTDIR) {
334 /* may be on a filesystem which is not mounted right now */
335 info(udev, "program '%s' not found\n", argv[0]);
338 err(udev, "exec of program '%s' failed\n", argv[0]);
342 err(udev, "fork of '%s' failed: %m\n", argv[0]);
345 /* read from child if requested */
346 if (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
350 /* parent closes child ends of pipes */
351 if (outpipe[WRITE_END] > 0)
352 close(outpipe[WRITE_END]);
353 if (errpipe[WRITE_END] > 0)
354 close(errpipe[WRITE_END]);
356 /* read child output */
357 while (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
362 if (outpipe[READ_END] > 0)
363 FD_SET(outpipe[READ_END], &readfds);
364 if (errpipe[READ_END] > 0)
365 FD_SET(errpipe[READ_END], &readfds);
366 fdcount = select(MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL);
375 if (outpipe[READ_END] > 0 && FD_ISSET(outpipe[READ_END], &readfds)) {
380 count = read(outpipe[READ_END], inbuf, sizeof(inbuf)-1);
382 close(outpipe[READ_END]);
383 outpipe[READ_END] = -1;
385 err(udev, "stdin read failed: %m\n");
392 /* store result for rule processing */
394 if (respos + count < ressize) {
395 memcpy(&result[respos], inbuf, count);
398 err(udev, "ressize %ld too short\n", (long)ressize);
403 while ((line = strsep(&pos, "\n")))
404 if (pos || line[0] != '\0')
405 info(udev, "'%s' (stdout) '%s'\n", argv[0], line);
409 if (errpipe[READ_END] > 0 && FD_ISSET(errpipe[READ_END], &readfds)) {
414 count = read(errpipe[READ_END], errbuf, sizeof(errbuf)-1);
416 close(errpipe[READ_END]);
417 errpipe[READ_END] = -1;
419 err(udev, "stderr read failed: %m\n");
422 errbuf[count] = '\0';
424 while ((line = strsep(&pos, "\n")))
425 if (pos || line[0] != '\0')
426 info(udev, "'%s' (stderr) '%s'\n", argv[0], line);
429 if (outpipe[READ_END] > 0)
430 close(outpipe[READ_END]);
431 if (errpipe[READ_END] > 0)
432 close(errpipe[READ_END]);
434 /* return the childs stdout string */
436 result[respos] = '\0';
437 dbg(udev, "result='%s'\n", result);
442 waitpid(pid, &status, 0);
443 if (WIFEXITED(status)) {
444 info(udev, "'%s' returned with exitcode %i\n", command, WEXITSTATUS(status));
445 if (WEXITSTATUS(status) != 0)
448 err(udev, "'%s' unexpected exit with status 0x%04x\n", command, status);