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