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_strscpy(p, sizeof(p), path);
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)
130 int buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
136 if (strcmp(user, "root") == 0)
138 uid = strtoul(user, &endptr, 10);
139 if (endptr[0] == '\0')
143 getpwnam_r(user, &pwbuf, buf, buflen, &pw);
146 if (errno == 0 || errno == ENOENT || errno == ESRCH)
147 err(udev, "specified user '%s' unknown\n", user);
149 err(udev, "error resolving user '%s': %m\n", user);
153 gid_t util_lookup_group(struct udev *udev, const char *group)
156 int buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
162 if (strcmp(group, "root") == 0)
164 gid = strtoul(group, &endptr, 10);
165 if (endptr[0] == '\0')
169 getgrnam_r(group, &grbuf, buf, buflen, &gr);
172 if (errno == 0 || errno == ENOENT || errno == ESRCH)
173 err(udev, "specified group '%s' unknown\n", group);
175 err(udev, "error resolving group '%s': %m\n", group);
179 /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
180 int util_resolve_subsys_kernel(struct udev *udev, const char *string,
181 char *result, size_t maxsize, int read_value)
183 char temp[UTIL_PATH_SIZE];
186 struct udev_device *dev;
189 if (string[0] != '[')
192 util_strscpy(temp, sizeof(temp), string);
196 sysname = strchr(subsys, '/');
200 sysname = &sysname[1];
202 attr = strchr(sysname, ']');
212 if (read_value && attr == NULL)
215 dev = udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
222 val = udev_device_get_sysattr_value(dev, attr);
224 util_strscpy(result, maxsize, val);
227 info(udev, "value '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result);
233 l = util_strpcpyl(&s, maxsize, udev_device_get_syspath(dev), NULL);
235 util_strpcpyl(&s, l, "/", attr, NULL);
236 info(udev, "path '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result);
238 udev_device_unref(dev);
242 int util_run_program(struct udev *udev, const char *command, char **envp,
243 char *result, size_t ressize, size_t *reslen)
246 int outpipe[2] = {-1, -1};
247 int errpipe[2] = {-1, -1};
249 char arg[UTIL_PATH_SIZE];
250 char program[UTIL_PATH_SIZE];
251 char *argv[(sizeof(arg) / 2) + 1];
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]);
280 info(udev, "'%s'\n", command);
282 /* prepare pipes from child to parent */
283 if (result != NULL || udev_get_log_priority(udev) >= LOG_INFO) {
284 if (pipe(outpipe) != 0) {
285 err(udev, "pipe failed: %m\n");
289 if (udev_get_log_priority(udev) >= LOG_INFO) {
290 if (pipe(errpipe) != 0) {
291 err(udev, "pipe failed: %m\n");
296 /* allow programs in /lib/udev/ to be called without the path */
297 if (argv[0][0] != '/') {
298 util_strscpyl(program, sizeof(program), UDEV_PREFIX "/lib/udev/", argv[0], NULL);
305 /* child closes parent ends of pipes */
306 if (outpipe[READ_END] > 0)
307 close(outpipe[READ_END]);
308 if (errpipe[READ_END] > 0)
309 close(errpipe[READ_END]);
311 /* discard child output or connect to pipe */
312 devnull = open("/dev/null", O_RDWR);
314 dup2(devnull, STDIN_FILENO);
315 if (outpipe[WRITE_END] < 0)
316 dup2(devnull, STDOUT_FILENO);
317 if (errpipe[WRITE_END] < 0)
318 dup2(devnull, STDERR_FILENO);
321 err(udev, "open /dev/null failed: %m\n");
322 if (outpipe[WRITE_END] > 0) {
323 dup2(outpipe[WRITE_END], STDOUT_FILENO);
324 close(outpipe[WRITE_END]);
326 if (errpipe[WRITE_END] > 0) {
327 dup2(errpipe[WRITE_END], STDERR_FILENO);
328 close(errpipe[WRITE_END]);
330 execve(argv[0], argv, envp);
331 if (errno == ENOENT || errno == ENOTDIR) {
332 /* may be on a filesytem which is not mounted right now */
333 info(udev, "program '%s' not found\n", argv[0]);
336 err(udev, "exec of program '%s' failed\n", argv[0]);
340 err(udev, "fork of '%s' failed: %m\n", argv[0]);
343 /* read from child if requested */
344 if (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
348 /* parent closes child ends of pipes */
349 if (outpipe[WRITE_END] > 0)
350 close(outpipe[WRITE_END]);
351 if (errpipe[WRITE_END] > 0)
352 close(errpipe[WRITE_END]);
354 /* read child output */
355 while (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
360 if (outpipe[READ_END] > 0)
361 FD_SET(outpipe[READ_END], &readfds);
362 if (errpipe[READ_END] > 0)
363 FD_SET(errpipe[READ_END], &readfds);
364 fdcount = select(UDEV_MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL);
373 if (outpipe[READ_END] > 0 && FD_ISSET(outpipe[READ_END], &readfds)) {
378 count = read(outpipe[READ_END], inbuf, sizeof(inbuf)-1);
380 close(outpipe[READ_END]);
381 outpipe[READ_END] = -1;
383 err(udev, "stdin read failed: %m\n");
390 /* store result for rule processing */
392 if (respos + count < ressize) {
393 memcpy(&result[respos], inbuf, count);
396 err(udev, "ressize %ld too short\n", (long)ressize);
401 while ((line = strsep(&pos, "\n")))
402 if (pos || line[0] != '\0')
403 info(udev, "'%s' (stdout) '%s'\n", argv[0], line);
407 if (errpipe[READ_END] > 0 && FD_ISSET(errpipe[READ_END], &readfds)) {
412 count = read(errpipe[READ_END], errbuf, sizeof(errbuf)-1);
414 close(errpipe[READ_END]);
415 errpipe[READ_END] = -1;
417 err(udev, "stderr read failed: %m\n");
420 errbuf[count] = '\0';
422 while ((line = strsep(&pos, "\n")))
423 if (pos || line[0] != '\0')
424 info(udev, "'%s' (stderr) '%s'\n", argv[0], line);
427 if (outpipe[READ_END] > 0)
428 close(outpipe[READ_END]);
429 if (errpipe[READ_END] > 0)
430 close(errpipe[READ_END]);
432 /* return the childs stdout string */
434 result[respos] = '\0';
435 dbg(udev, "result='%s'\n", result);
440 waitpid(pid, &status, 0);
441 if (WIFEXITED(status)) {
442 info(udev, "'%s' returned with status %i\n", argv[0], WEXITSTATUS(status));
443 if (WEXITSTATUS(status) != 0)
446 err(udev, "'%s' abnormal exit\n", command);