chiark / gitweb /
replace missing get_attr_value() -> get_sysattr_value()
[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         errno = 0;
133         pw = getpwnam(user);
134         if (pw == NULL) {
135                 if (errno == 0 || errno == ENOENT || errno == ESRCH)
136                         err(udev, "specified user '%s' unknown\n", user);
137                 else
138                         err(udev, "error resolving user '%s': %m\n", user);
139         } else
140                 uid = pw->pw_uid;
141
142         return uid;
143 }
144
145 extern gid_t util_lookup_group(struct udev *udev, const char *group)
146 {
147         struct group *gr;
148         gid_t gid = 0;
149
150         errno = 0;
151         gr = getgrnam(group);
152         if (gr == NULL) {
153                 if (errno == 0 || errno == ENOENT || errno == ESRCH)
154                         err(udev, "specified group '%s' unknown\n", group);
155                 else
156                         err(udev, "error resolving group '%s': %m\n", group);
157         } else
158                 gid = gr->gr_gid;
159
160         return gid;
161 }
162
163 int util_run_program(struct udev *udev, const char *command, char **envp,
164                      char *result, size_t ressize, size_t *reslen)
165 {
166         int status;
167         int outpipe[2] = {-1, -1};
168         int errpipe[2] = {-1, -1};
169         pid_t pid;
170         char arg[UTIL_PATH_SIZE];
171         char program[UTIL_PATH_SIZE];
172         char *argv[(sizeof(arg) / 2) + 1];
173         int devnull;
174         int i;
175         int err = 0;
176
177         /* build argv from command */
178         util_strlcpy(arg, command, sizeof(arg));
179         i = 0;
180         if (strchr(arg, ' ') != NULL) {
181                 char *pos = arg;
182
183                 while (pos != NULL && pos[0] != '\0') {
184                         if (pos[0] == '\'') {
185                                 /* do not separate quotes */
186                                 pos++;
187                                 argv[i] = strsep(&pos, "\'");
188                                 while (pos != NULL && pos[0] == ' ')
189                                         pos++;
190                         } else {
191                                 argv[i] = strsep(&pos, " ");
192                         }
193                         dbg(udev, "arg[%i] '%s'\n", i, argv[i]);
194                         i++;
195                 }
196                 argv[i] = NULL;
197         } else {
198                 argv[0] = arg;
199                 argv[1] = NULL;
200         }
201         info(udev, "'%s'\n", command);
202
203         /* prepare pipes from child to parent */
204         if (result != NULL || udev_get_log_priority(udev) >= LOG_INFO) {
205                 if (pipe(outpipe) != 0) {
206                         err(udev, "pipe failed: %m\n");
207                         return -1;
208                 }
209         }
210         if (udev_get_log_priority(udev) >= LOG_INFO) {
211                 if (pipe(errpipe) != 0) {
212                         err(udev, "pipe failed: %m\n");
213                         return -1;
214                 }
215         }
216
217         /* allow programs in /lib/udev/ to be called without the path */
218         if (strchr(argv[0], '/') == NULL) {
219                 util_strlcpy(program, UDEV_PREFIX "/lib/udev/", sizeof(program));
220                 util_strlcat(program, argv[0], sizeof(program));
221                 argv[0] = program;
222         }
223
224         pid = fork();
225         switch(pid) {
226         case 0:
227                 /* child closes parent ends of pipes */
228                 if (outpipe[READ_END] > 0)
229                         close(outpipe[READ_END]);
230                 if (errpipe[READ_END] > 0)
231                         close(errpipe[READ_END]);
232
233                 /* discard child output or connect to pipe */
234                 devnull = open("/dev/null", O_RDWR);
235                 if (devnull > 0) {
236                         dup2(devnull, STDIN_FILENO);
237                         if (outpipe[WRITE_END] < 0)
238                                 dup2(devnull, STDOUT_FILENO);
239                         if (errpipe[WRITE_END] < 0)
240                                 dup2(devnull, STDERR_FILENO);
241                         close(devnull);
242                 } else
243                         err(udev, "open /dev/null failed: %m\n");
244                 if (outpipe[WRITE_END] > 0) {
245                         dup2(outpipe[WRITE_END], STDOUT_FILENO);
246                         close(outpipe[WRITE_END]);
247                 }
248                 if (errpipe[WRITE_END] > 0) {
249                         dup2(errpipe[WRITE_END], STDERR_FILENO);
250                         close(errpipe[WRITE_END]);
251                 }
252                 execve(argv[0], argv, envp);
253                 if (errno == ENOENT || errno == ENOTDIR) {
254                         /* may be on a filesytem which is not mounted right now */
255                         info(udev, "program '%s' not found\n", argv[0]);
256                 } else {
257                         /* other problems */
258                         err(udev, "exec of program '%s' failed\n", argv[0]);
259                 }
260                 _exit(1);
261         case -1:
262                 err(udev, "fork of '%s' failed: %m\n", argv[0]);
263                 return -1;
264         default:
265                 /* read from child if requested */
266                 if (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
267                         ssize_t count;
268                         size_t respos = 0;
269
270                         /* parent closes child ends of pipes */
271                         if (outpipe[WRITE_END] > 0)
272                                 close(outpipe[WRITE_END]);
273                         if (errpipe[WRITE_END] > 0)
274                                 close(errpipe[WRITE_END]);
275
276                         /* read child output */
277                         while (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
278                                 int fdcount;
279                                 fd_set readfds;
280
281                                 FD_ZERO(&readfds);
282                                 if (outpipe[READ_END] > 0)
283                                         FD_SET(outpipe[READ_END], &readfds);
284                                 if (errpipe[READ_END] > 0)
285                                         FD_SET(errpipe[READ_END], &readfds);
286                                 fdcount = select(UDEV_MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL);
287                                 if (fdcount < 0) {
288                                         if (errno == EINTR)
289                                                 continue;
290                                         err = -1;
291                                         break;
292                                 }
293
294                                 /* get stdout */
295                                 if (outpipe[READ_END] > 0 && FD_ISSET(outpipe[READ_END], &readfds)) {
296                                         char inbuf[1024];
297                                         char *pos;
298                                         char *line;
299
300                                         count = read(outpipe[READ_END], inbuf, sizeof(inbuf)-1);
301                                         if (count <= 0) {
302                                                 close(outpipe[READ_END]);
303                                                 outpipe[READ_END] = -1;
304                                                 if (count < 0) {
305                                                         err(udev, "stdin read failed: %m\n");
306                                                         err = -1;
307                                                 }
308                                                 continue;
309                                         }
310                                         inbuf[count] = '\0';
311
312                                         /* store result for rule processing */
313                                         if (result) {
314                                                 if (respos + count < ressize) {
315                                                         memcpy(&result[respos], inbuf, count);
316                                                         respos += count;
317                                                 } else {
318                                                         err(udev, "ressize %ld too short\n", (long)ressize);
319                                                         err = -1;
320                                                 }
321                                         }
322                                         pos = inbuf;
323                                         while ((line = strsep(&pos, "\n")))
324                                                 if (pos || line[0] != '\0')
325                                                         info(udev, "'%s' (stdout) '%s'\n", argv[0], line);
326                                 }
327
328                                 /* get stderr */
329                                 if (errpipe[READ_END] > 0 && FD_ISSET(errpipe[READ_END], &readfds)) {
330                                         char errbuf[1024];
331                                         char *pos;
332                                         char *line;
333
334                                         count = read(errpipe[READ_END], errbuf, sizeof(errbuf)-1);
335                                         if (count <= 0) {
336                                                 close(errpipe[READ_END]);
337                                                 errpipe[READ_END] = -1;
338                                                 if (count < 0)
339                                                         err(udev, "stderr read failed: %m\n");
340                                                 continue;
341                                         }
342                                         errbuf[count] = '\0';
343                                         pos = errbuf;
344                                         while ((line = strsep(&pos, "\n")))
345                                                 if (pos || line[0] != '\0')
346                                                         info(udev, "'%s' (stderr) '%s'\n", argv[0], line);
347                                 }
348                         }
349                         if (outpipe[READ_END] > 0)
350                                 close(outpipe[READ_END]);
351                         if (errpipe[READ_END] > 0)
352                                 close(errpipe[READ_END]);
353
354                         /* return the childs stdout string */
355                         if (result) {
356                                 result[respos] = '\0';
357                                 dbg(udev, "result='%s'\n", result);
358                                 if (reslen)
359                                         *reslen = respos;
360                         }
361                 }
362                 waitpid(pid, &status, 0);
363                 if (WIFEXITED(status)) {
364                         info(udev, "'%s' returned with status %i\n", argv[0], WEXITSTATUS(status));
365                         if (WEXITSTATUS(status) != 0)
366                                 err = -1;
367                 } else {
368                         err(udev, "'%s' abnormal exit\n", argv[0]);
369                         err = -1;
370                 }
371         }
372         return err;
373 }