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 /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
166 int util_resolve_subsys_kernel(struct udev *udev, const char *string,
167 char *result, size_t maxsize, int read_value)
169 char temp[UTIL_PATH_SIZE];
172 struct udev_device *dev;
177 if (string[0] != '[')
180 util_strlcpy(temp, string, sizeof(temp));
184 sysname = strchr(subsys, '/');
188 sysname = &sysname[1];
190 attr = strchr(sysname, ']');
200 if (read_value && attr == NULL)
203 dev = udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
210 val = udev_device_get_sysattr_value(dev, attr);
212 util_strlcpy(result, val, maxsize);
215 info(udev, "value '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result);
217 util_strlcpy(result, udev_device_get_syspath(dev), maxsize);
219 util_strlcat(result, "/", maxsize);
220 util_strlcat(result, attr, maxsize);
222 info(udev, "path '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result);
224 udev_device_unref(dev);
228 int util_run_program(struct udev *udev, const char *command, char **envp,
229 char *result, size_t ressize, size_t *reslen)
232 int outpipe[2] = {-1, -1};
233 int errpipe[2] = {-1, -1};
235 char arg[UTIL_PATH_SIZE];
236 char program[UTIL_PATH_SIZE];
237 char *argv[(sizeof(arg) / 2) + 1];
242 /* build argv from command */
243 util_strlcpy(arg, command, sizeof(arg));
245 if (strchr(arg, ' ') != NULL) {
248 while (pos != NULL && pos[0] != '\0') {
249 if (pos[0] == '\'') {
250 /* do not separate quotes */
252 argv[i] = strsep(&pos, "\'");
253 while (pos != NULL && pos[0] == ' ')
256 argv[i] = strsep(&pos, " ");
258 dbg(udev, "arg[%i] '%s'\n", i, argv[i]);
266 info(udev, "'%s'\n", command);
268 /* prepare pipes from child to parent */
269 if (result != NULL || udev_get_log_priority(udev) >= LOG_INFO) {
270 if (pipe(outpipe) != 0) {
271 err(udev, "pipe failed: %m\n");
275 if (udev_get_log_priority(udev) >= LOG_INFO) {
276 if (pipe(errpipe) != 0) {
277 err(udev, "pipe failed: %m\n");
282 /* allow programs in /lib/udev/ to be called without the path */
283 if (strchr(argv[0], '/') == NULL) {
284 util_strlcpy(program, UDEV_PREFIX "/lib/udev/", sizeof(program));
285 util_strlcat(program, argv[0], sizeof(program));
292 /* child closes parent ends of pipes */
293 if (outpipe[READ_END] > 0)
294 close(outpipe[READ_END]);
295 if (errpipe[READ_END] > 0)
296 close(errpipe[READ_END]);
298 /* discard child output or connect to pipe */
299 devnull = open("/dev/null", O_RDWR);
301 dup2(devnull, STDIN_FILENO);
302 if (outpipe[WRITE_END] < 0)
303 dup2(devnull, STDOUT_FILENO);
304 if (errpipe[WRITE_END] < 0)
305 dup2(devnull, STDERR_FILENO);
308 err(udev, "open /dev/null failed: %m\n");
309 if (outpipe[WRITE_END] > 0) {
310 dup2(outpipe[WRITE_END], STDOUT_FILENO);
311 close(outpipe[WRITE_END]);
313 if (errpipe[WRITE_END] > 0) {
314 dup2(errpipe[WRITE_END], STDERR_FILENO);
315 close(errpipe[WRITE_END]);
317 execve(argv[0], argv, envp);
318 if (errno == ENOENT || errno == ENOTDIR) {
319 /* may be on a filesytem which is not mounted right now */
320 info(udev, "program '%s' not found\n", argv[0]);
323 err(udev, "exec of program '%s' failed\n", argv[0]);
327 err(udev, "fork of '%s' failed: %m\n", argv[0]);
330 /* read from child if requested */
331 if (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
335 /* parent closes child ends of pipes */
336 if (outpipe[WRITE_END] > 0)
337 close(outpipe[WRITE_END]);
338 if (errpipe[WRITE_END] > 0)
339 close(errpipe[WRITE_END]);
341 /* read child output */
342 while (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
347 if (outpipe[READ_END] > 0)
348 FD_SET(outpipe[READ_END], &readfds);
349 if (errpipe[READ_END] > 0)
350 FD_SET(errpipe[READ_END], &readfds);
351 fdcount = select(UDEV_MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL);
360 if (outpipe[READ_END] > 0 && FD_ISSET(outpipe[READ_END], &readfds)) {
365 count = read(outpipe[READ_END], inbuf, sizeof(inbuf)-1);
367 close(outpipe[READ_END]);
368 outpipe[READ_END] = -1;
370 err(udev, "stdin read failed: %m\n");
377 /* store result for rule processing */
379 if (respos + count < ressize) {
380 memcpy(&result[respos], inbuf, count);
383 err(udev, "ressize %ld too short\n", (long)ressize);
388 while ((line = strsep(&pos, "\n")))
389 if (pos || line[0] != '\0')
390 info(udev, "'%s' (stdout) '%s'\n", argv[0], line);
394 if (errpipe[READ_END] > 0 && FD_ISSET(errpipe[READ_END], &readfds)) {
399 count = read(errpipe[READ_END], errbuf, sizeof(errbuf)-1);
401 close(errpipe[READ_END]);
402 errpipe[READ_END] = -1;
404 err(udev, "stderr read failed: %m\n");
407 errbuf[count] = '\0';
409 while ((line = strsep(&pos, "\n")))
410 if (pos || line[0] != '\0')
411 info(udev, "'%s' (stderr) '%s'\n", argv[0], line);
414 if (outpipe[READ_END] > 0)
415 close(outpipe[READ_END]);
416 if (errpipe[READ_END] > 0)
417 close(errpipe[READ_END]);
419 /* return the childs stdout string */
421 result[respos] = '\0';
422 dbg(udev, "result='%s'\n", result);
427 waitpid(pid, &status, 0);
428 if (WIFEXITED(status)) {
429 info(udev, "'%s' returned with status %i\n", argv[0], WEXITSTATUS(status));
430 if (WEXITSTATUS(status) != 0)
433 err(udev, "'%s' abnormal exit\n", argv[0]);