chiark / gitweb /
keymap: move from udev-extras
[elogind.git] / libudev / libudev-util-private.c
1 /*
2  * libudev - interface to udev device information
3  *
4  * Copyright (C) 2004-2009 Kay Sievers <kay.sievers@vrfy.org>
5  *
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.
10  */
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <stddef.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <ctype.h>
20 #include <pwd.h>
21 #include <grp.h>
22 #include <sys/wait.h>
23
24 #include "libudev.h"
25 #include "libudev-private.h"
26
27 int util_create_path(struct udev *udev, const char *path)
28 {
29         char p[UTIL_PATH_SIZE];
30         char *pos;
31         struct stat stats;
32         int ret;
33
34         util_strscpy(p, sizeof(p), path);
35         pos = strrchr(p, '/');
36         if (pos == p || pos == NULL)
37                 return 0;
38
39         while (pos[-1] == '/')
40                 pos--;
41         pos[0] = '\0';
42
43         dbg(udev, "stat '%s'\n", p);
44         if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
45                 return 0;
46
47         if (util_create_path(udev, p) != 0)
48                 return -1;
49
50         dbg(udev, "mkdir '%s'\n", p);
51         udev_selinux_setfscreatecon(udev, p, S_IFDIR|0755);
52         ret = mkdir(p, 0755);
53         udev_selinux_resetfscreatecon(udev);
54         if (ret == 0)
55                 return 0;
56
57         if (errno == EEXIST)
58                 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
59                         return 0;
60         return -1;
61 }
62
63 int util_delete_path(struct udev *udev, const char *path)
64 {
65         char p[UTIL_PATH_SIZE];
66         char *pos;
67         int retval;
68
69         strcpy (p, path);
70         pos = strrchr(p, '/');
71         if (pos == p || pos == NULL)
72                 return 0;
73
74         while (1) {
75                 *pos = '\0';
76                 pos = strrchr(p, '/');
77
78                 /* don't remove the last one */
79                 if ((pos == p) || (pos == NULL))
80                         break;
81
82                 /* remove if empty */
83                 retval = rmdir(p);
84                 if (errno == ENOENT)
85                         retval = 0;
86                 if (retval) {
87                         if (errno == ENOTEMPTY)
88                                 return 0;
89                         err(udev, "rmdir(%s) failed: %m\n", p);
90                         break;
91                 }
92                 dbg(udev, "removed '%s'\n", p);
93         }
94         return 0;
95 }
96
97 /* Reset permissions on the device node, before unlinking it to make sure,
98  * that permisions of possible hard links will be removed too.
99  */
100 int util_unlink_secure(struct udev *udev, const char *filename)
101 {
102         int retval;
103
104         retval = chown(filename, 0, 0);
105         if (retval)
106                 err(udev, "chown(%s, 0, 0) failed: %m\n", filename);
107
108         retval = chmod(filename, 0000);
109         if (retval)
110                 err(udev, "chmod(%s, 0000) failed: %m\n", filename);
111
112         retval = unlink(filename);
113         if (errno == ENOENT)
114                 retval = 0;
115
116         if (retval)
117                 err(udev, "unlink(%s) failed: %m\n", filename);
118
119         return retval;
120 }
121
122 uid_t util_lookup_user(struct udev *udev, const char *user)
123 {
124         char *endptr;
125         int buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
126         char buf[buflen];
127         struct passwd pwbuf;
128         struct passwd *pw;
129         uid_t uid;
130
131         if (strcmp(user, "root") == 0)
132                 return 0;
133         uid = strtoul(user, &endptr, 10);
134         if (endptr[0] == '\0')
135                 return uid;
136
137         errno = 0;
138         getpwnam_r(user, &pwbuf, buf, buflen, &pw);
139         if (pw != NULL)
140                 return pw->pw_uid;
141         if (errno == 0 || errno == ENOENT || errno == ESRCH)
142                 err(udev, "specified user '%s' unknown\n", user);
143         else
144                 err(udev, "error resolving user '%s': %m\n", user);
145         return 0;
146 }
147
148 gid_t util_lookup_group(struct udev *udev, const char *group)
149 {
150         char *endptr;
151         int buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
152         char buf[buflen];
153         struct group grbuf;
154         struct group *gr;
155         gid_t gid = 0;
156
157         if (strcmp(group, "root") == 0)
158                 return 0;
159         gid = strtoul(group, &endptr, 10);
160         if (endptr[0] == '\0')
161                 return gid;
162
163         errno = 0;
164         getgrnam_r(group, &grbuf, buf, buflen, &gr);
165         if (gr != NULL)
166                 return gr->gr_gid;
167         if (errno == 0 || errno == ENOENT || errno == ESRCH)
168                 err(udev, "specified group '%s' unknown\n", group);
169         else
170                 err(udev, "error resolving group '%s': %m\n", group);
171         return 0;
172 }
173
174 /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
175 int util_resolve_subsys_kernel(struct udev *udev, const char *string,
176                                char *result, size_t maxsize, int read_value)
177 {
178         char temp[UTIL_PATH_SIZE];
179         char *subsys;
180         char *sysname;
181         struct udev_device *dev;
182         char *attr;
183
184         if (string[0] != '[')
185                 return -1;
186
187         util_strscpy(temp, sizeof(temp), string);
188
189         subsys = &temp[1];
190
191         sysname = strchr(subsys, '/');
192         if (sysname == NULL)
193                 return -1;
194         sysname[0] = '\0';
195         sysname = &sysname[1];
196
197         attr = strchr(sysname, ']');
198         if (attr == NULL)
199                 return -1;
200         attr[0] = '\0';
201         attr = &attr[1];
202         if (attr[0] == '/')
203                 attr = &attr[1];
204         if (attr[0] == '\0')
205                 attr = NULL;
206
207         if (read_value && attr == NULL)
208                 return -1;
209
210         dev = udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
211         if (dev == NULL)
212                 return -1;
213
214         if (read_value) {
215                 const char *val;
216
217                 val = udev_device_get_sysattr_value(dev, attr);
218                 if (val != NULL)
219                         util_strscpy(result, maxsize, val);
220                 else
221                         result[0] = '\0';
222                 info(udev, "value '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result);
223         } else {
224                 size_t l;
225                 char *s;
226
227                 s = result;
228                 l = util_strpcpyl(&s, maxsize, udev_device_get_syspath(dev), NULL);
229                 if (attr != NULL)
230                         util_strpcpyl(&s, l, "/", attr, NULL);
231                 info(udev, "path '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result);
232         }
233         udev_device_unref(dev);
234         return 0;
235 }
236
237 int util_run_program(struct udev *udev, const char *command, char **envp,
238                      char *result, size_t ressize, size_t *reslen)
239 {
240         int status;
241         int outpipe[2] = {-1, -1};
242         int errpipe[2] = {-1, -1};
243         pid_t pid;
244         char arg[UTIL_PATH_SIZE];
245         char program[UTIL_PATH_SIZE];
246         char *argv[(sizeof(arg) / 2) + 1];
247         int devnull;
248         int i;
249         int err = 0;
250
251         /* build argv from command */
252         util_strscpy(arg, sizeof(arg), command);
253         i = 0;
254         if (strchr(arg, ' ') != NULL) {
255                 char *pos = arg;
256
257                 while (pos != NULL && pos[0] != '\0') {
258                         if (pos[0] == '\'') {
259                                 /* do not separate quotes */
260                                 pos++;
261                                 argv[i] = strsep(&pos, "\'");
262                                 while (pos != NULL && pos[0] == ' ')
263                                         pos++;
264                         } else {
265                                 argv[i] = strsep(&pos, " ");
266                         }
267                         dbg(udev, "arg[%i] '%s'\n", i, argv[i]);
268                         i++;
269                 }
270                 argv[i] = NULL;
271         } else {
272                 argv[0] = arg;
273                 argv[1] = NULL;
274         }
275         info(udev, "'%s'\n", command);
276
277         /* prepare pipes from child to parent */
278         if (result != NULL || udev_get_log_priority(udev) >= LOG_INFO) {
279                 if (pipe(outpipe) != 0) {
280                         err(udev, "pipe failed: %m\n");
281                         return -1;
282                 }
283         }
284         if (udev_get_log_priority(udev) >= LOG_INFO) {
285                 if (pipe(errpipe) != 0) {
286                         err(udev, "pipe failed: %m\n");
287                         return -1;
288                 }
289         }
290
291         /* allow programs in /lib/udev/ to be called without the path */
292         if (argv[0][0] != '/') {
293                 util_strscpyl(program, sizeof(program), LIBEXECDIR "/", argv[0], NULL);
294                 argv[0] = program;
295         }
296
297         pid = fork();
298         switch(pid) {
299         case 0:
300                 /* child closes parent ends of pipes */
301                 if (outpipe[READ_END] > 0)
302                         close(outpipe[READ_END]);
303                 if (errpipe[READ_END] > 0)
304                         close(errpipe[READ_END]);
305
306                 /* discard child output or connect to pipe */
307                 devnull = open("/dev/null", O_RDWR);
308                 if (devnull > 0) {
309                         dup2(devnull, STDIN_FILENO);
310                         if (outpipe[WRITE_END] < 0)
311                                 dup2(devnull, STDOUT_FILENO);
312                         if (errpipe[WRITE_END] < 0)
313                                 dup2(devnull, STDERR_FILENO);
314                         close(devnull);
315                 } else
316                         err(udev, "open /dev/null failed: %m\n");
317                 if (outpipe[WRITE_END] > 0) {
318                         dup2(outpipe[WRITE_END], STDOUT_FILENO);
319                         close(outpipe[WRITE_END]);
320                 }
321                 if (errpipe[WRITE_END] > 0) {
322                         dup2(errpipe[WRITE_END], STDERR_FILENO);
323                         close(errpipe[WRITE_END]);
324                 }
325                 execve(argv[0], argv, envp);
326                 if (errno == ENOENT || errno == ENOTDIR) {
327                         /* may be on a filesytem which is not mounted right now */
328                         info(udev, "program '%s' not found\n", argv[0]);
329                 } else {
330                         /* other problems */
331                         err(udev, "exec of program '%s' failed\n", argv[0]);
332                 }
333                 _exit(1);
334         case -1:
335                 err(udev, "fork of '%s' failed: %m\n", argv[0]);
336                 return -1;
337         default:
338                 /* read from child if requested */
339                 if (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
340                         ssize_t count;
341                         size_t respos = 0;
342
343                         /* parent closes child ends of pipes */
344                         if (outpipe[WRITE_END] > 0)
345                                 close(outpipe[WRITE_END]);
346                         if (errpipe[WRITE_END] > 0)
347                                 close(errpipe[WRITE_END]);
348
349                         /* read child output */
350                         while (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
351                                 int fdcount;
352                                 fd_set readfds;
353
354                                 FD_ZERO(&readfds);
355                                 if (outpipe[READ_END] > 0)
356                                         FD_SET(outpipe[READ_END], &readfds);
357                                 if (errpipe[READ_END] > 0)
358                                         FD_SET(errpipe[READ_END], &readfds);
359                                 fdcount = select(UDEV_MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL);
360                                 if (fdcount < 0) {
361                                         if (errno == EINTR)
362                                                 continue;
363                                         err = -1;
364                                         break;
365                                 }
366
367                                 /* get stdout */
368                                 if (outpipe[READ_END] > 0 && FD_ISSET(outpipe[READ_END], &readfds)) {
369                                         char inbuf[1024];
370                                         char *pos;
371                                         char *line;
372
373                                         count = read(outpipe[READ_END], inbuf, sizeof(inbuf)-1);
374                                         if (count <= 0) {
375                                                 close(outpipe[READ_END]);
376                                                 outpipe[READ_END] = -1;
377                                                 if (count < 0) {
378                                                         err(udev, "stdin read failed: %m\n");
379                                                         err = -1;
380                                                 }
381                                                 continue;
382                                         }
383                                         inbuf[count] = '\0';
384
385                                         /* store result for rule processing */
386                                         if (result) {
387                                                 if (respos + count < ressize) {
388                                                         memcpy(&result[respos], inbuf, count);
389                                                         respos += count;
390                                                 } else {
391                                                         err(udev, "ressize %ld too short\n", (long)ressize);
392                                                         err = -1;
393                                                 }
394                                         }
395                                         pos = inbuf;
396                                         while ((line = strsep(&pos, "\n")))
397                                                 if (pos || line[0] != '\0')
398                                                         info(udev, "'%s' (stdout) '%s'\n", argv[0], line);
399                                 }
400
401                                 /* get stderr */
402                                 if (errpipe[READ_END] > 0 && FD_ISSET(errpipe[READ_END], &readfds)) {
403                                         char errbuf[1024];
404                                         char *pos;
405                                         char *line;
406
407                                         count = read(errpipe[READ_END], errbuf, sizeof(errbuf)-1);
408                                         if (count <= 0) {
409                                                 close(errpipe[READ_END]);
410                                                 errpipe[READ_END] = -1;
411                                                 if (count < 0)
412                                                         err(udev, "stderr read failed: %m\n");
413                                                 continue;
414                                         }
415                                         errbuf[count] = '\0';
416                                         pos = errbuf;
417                                         while ((line = strsep(&pos, "\n")))
418                                                 if (pos || line[0] != '\0')
419                                                         info(udev, "'%s' (stderr) '%s'\n", argv[0], line);
420                                 }
421                         }
422                         if (outpipe[READ_END] > 0)
423                                 close(outpipe[READ_END]);
424                         if (errpipe[READ_END] > 0)
425                                 close(errpipe[READ_END]);
426
427                         /* return the childs stdout string */
428                         if (result) {
429                                 result[respos] = '\0';
430                                 dbg(udev, "result='%s'\n", result);
431                                 if (reslen)
432                                         *reslen = respos;
433                         }
434                 }
435                 waitpid(pid, &status, 0);
436                 if (WIFEXITED(status)) {
437                         info(udev, "'%s' returned with status %i\n", argv[0], WEXITSTATUS(status));
438                         if (WEXITSTATUS(status) != 0)
439                                 err = -1;
440                 } else {
441                         err(udev, "'%s' abnormal exit\n", command);
442                         err = -1;
443                 }
444         }
445         return err;
446 }