chiark / gitweb /
080 release
[elogind.git] / udev_utils_run.c
1 /*
2  * udev_utils_run.c - execute programs from udev and read its output
3  *
4  * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  * 
10  *      This program is distributed in the hope that it will be useful, but
11  *      WITHOUT ANY WARRANTY; without even the implied warranty of
12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *      General Public License for more details.
14  * 
15  *      You should have received a copy of the GNU General Public License along
16  *      with this program; if not, write to the Free Software Foundation, Inc.,
17  *      675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  */
20
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <sys/socket.h>
30 #include <sys/un.h>
31 #include <sys/wait.h>
32 #include <sys/select.h>
33
34 #include "udev.h"
35
36
37 int pass_env_to_socket(const char *sockname, const char *devpath, const char *action)
38 {
39         int sock;
40         struct sockaddr_un saddr;
41         socklen_t addrlen;
42         char buf[2048];
43         size_t bufpos = 0;
44         int i;
45         ssize_t count;
46         int retval = 0;
47
48         dbg("pass environment to socket '%s'", sockname);
49         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
50         memset(&saddr, 0x00, sizeof(struct sockaddr_un));
51         saddr.sun_family = AF_LOCAL;
52         /* only abstract namespace is supported */
53         strcpy(&saddr.sun_path[1], sockname);
54         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
55
56         bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
57         bufpos++;
58         for (i = 0; environ[i] != NULL && bufpos < sizeof(buf); i++) {
59                 bufpos += strlcpy(&buf[bufpos], environ[i], sizeof(buf) - bufpos-1);
60                 bufpos++;
61         }
62
63         count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, addrlen);
64         if (count < 0)
65                 retval = -1;
66         info("passed %zi bytes to socket '%s', ", count, sockname);
67
68         close(sock);
69         return retval;
70 }
71
72 int run_program(const char *command, const char *subsystem,
73                 char *result, size_t ressize, size_t *reslen, int log)
74 {
75         int retval = 0;
76         int status;
77         int outpipe[2] = {-1, -1};
78         int errpipe[2] = {-1, -1};
79         pid_t pid;
80         char arg[PATH_SIZE];
81         char program[PATH_SIZE];
82         char *argv[(sizeof(arg) / 2) + 1];
83         int devnull;
84         int i;
85
86         strlcpy(arg, command, sizeof(arg));
87         i = 0;
88         if (strchr(arg, ' ')) {
89                 char *pos = arg;
90                 while (pos != NULL) {
91                         if (pos[0] == '\'') {
92                                 /* don't separate if in apostrophes */
93                                 pos++;
94                                 argv[i] = strsep(&pos, "\'");
95                                 while (pos && pos[0] == ' ')
96                                         pos++;
97                         } else {
98                                 argv[i] = strsep(&pos, " ");
99                         }
100                         dbg("arg[%i] '%s'", i, argv[i]);
101                         i++;
102                 }
103                 argv[i] = NULL;
104                 info("'%s'", command);
105         } else {
106                 argv[0] = arg;
107                 argv[1] = (char *) subsystem;
108                 argv[2] = NULL;
109                 info("'%s' '%s'", arg, argv[1]);
110         }
111
112         /* prepare pipes from child to parent */
113         if (result || log) {
114                 if (pipe(outpipe) != 0) {
115                         err("pipe failed: %s", strerror(errno));
116                         return -1;
117                 }
118         }
119         if (log) {
120                 if (pipe(errpipe) != 0) {
121                         err("pipe failed: %s", strerror(errno));
122                         return -1;
123                 }
124         }
125
126         /* allow programs in /lib/udev called without the path */
127         if (strchr(argv[0], '/') == NULL) {
128                 strlcpy(program, "/lib/udev/", sizeof(program));
129                 strlcat(program, argv[0], sizeof(program));
130                 argv[0] = program;
131         }
132
133         pid = fork();
134         switch(pid) {
135         case 0:
136                 /* child closes parent ends of pipes */
137                 if (outpipe[READ_END] > 0)
138                         close(outpipe[READ_END]);
139                 if (errpipe[READ_END] > 0)
140                         close(errpipe[READ_END]);
141
142                 /* discard child output or connect to pipe */
143                 devnull = open("/dev/null", O_RDWR);
144                 if (devnull > 0) {
145                         dup2(devnull, STDIN_FILENO);
146                         if (outpipe[WRITE_END] < 0)
147                                 dup2(devnull, STDOUT_FILENO);
148                         if (errpipe[WRITE_END] < 0)
149                                 dup2(devnull, STDERR_FILENO);
150                         close(devnull);
151                 } else
152                         err("open /dev/null failed: %s", strerror(errno));
153                 if (outpipe[WRITE_END] > 0)
154                         dup2(outpipe[WRITE_END], STDOUT_FILENO);
155                 if (errpipe[WRITE_END] > 0)
156                         dup2(errpipe[WRITE_END], STDERR_FILENO);
157                 execv(argv[0], argv);
158
159                 /* we should never reach this */
160                 err("exec of program '%s' failed", argv[0]);
161                 _exit(1);
162         case -1:
163                 err("fork of '%s' failed: %s", argv[0], strerror(errno));
164                 return -1;
165         default:
166                 /* read from child if requested */
167                 if (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
168                         ssize_t count;
169                         size_t respos = 0;
170
171                         /* parent closes child ends of pipes */
172                         if (outpipe[WRITE_END] > 0)
173                                 close(outpipe[WRITE_END]);
174                         if (errpipe[WRITE_END] > 0)
175                                 close(errpipe[WRITE_END]);
176
177                         /* read child output */
178                         while (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
179                                 int fdcount;
180                                 fd_set readfds;
181
182                                 FD_ZERO(&readfds);
183                                 if (outpipe[READ_END] > 0)
184                                         FD_SET(outpipe[READ_END], &readfds);
185                                 if (errpipe[READ_END] > 0)
186                                         FD_SET(errpipe[READ_END], &readfds);
187                                 fdcount = select(UDEV_MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL);
188                                 if (fdcount < 0) {
189                                         if (errno == EINTR)
190                                                 continue;
191                                         retval = -1;
192                                         break;
193                                 }
194
195                                 /* get stdout */
196                                 if (outpipe[READ_END] > 0 && FD_ISSET(outpipe[READ_END], &readfds)) {
197                                         char inbuf[1024];
198                                         char *pos;
199                                         char *line;
200
201                                         count = read(outpipe[READ_END], inbuf, sizeof(inbuf)-1);
202                                         if (count <= 0) {
203                                                 close(outpipe[READ_END]);
204                                                 outpipe[READ_END] = -1;
205                                                 if (count < 0) {
206                                                         err("stdin read failed: %s", strerror(errno));
207                                                         retval = -1;
208                                                 }
209                                                 continue;
210                                         }
211                                         inbuf[count] = '\0';
212
213                                         /* store result for rule processing */
214                                         if (result) {
215                                                 if (respos + count < ressize) {
216                                                         memcpy(&result[respos], inbuf, count);
217                                                         respos += count;
218                                                 } else {
219                                                         err("ressize %ld too short", (long)ressize);
220                                                         retval = -1;
221                                                 }
222                                         }
223                                         pos = inbuf;
224                                         while ((line = strsep(&pos, "\n")))
225                                                 if (pos || line[0] != '\0')
226                                                         info("'%s' (stdout) '%s'", argv[0], line);
227                                 }
228
229                                 /* get stderr */
230                                 if (errpipe[READ_END] > 0 && FD_ISSET(errpipe[READ_END], &readfds)) {
231                                         char errbuf[1024];
232                                         char *pos;
233                                         char *line;
234
235                                         count = read(errpipe[READ_END], errbuf, sizeof(errbuf)-1);
236                                         if (count <= 0) {
237                                                 close(errpipe[READ_END]);
238                                                 errpipe[READ_END] = -1;
239                                                 if (count < 0)
240                                                         err("stderr read failed: %s", strerror(errno));
241                                                 continue;
242                                         }
243                                         errbuf[count] = '\0';
244                                         pos = errbuf;
245                                         while ((line = strsep(&pos, "\n")))
246                                                 if (pos || line[0] != '\0')
247                                                         info("'%s' (stderr) '%s'", argv[0], line);
248                                 }
249                         }
250                         if (outpipe[READ_END] > 0)
251                                 close(outpipe[READ_END]);
252                         if (errpipe[READ_END] > 0)
253                                 close(errpipe[READ_END]);
254
255                         /* return the childs stdout string */
256                         if (result) {
257                                 result[respos] = '\0';
258                                 dbg("result='%s'", result);
259                                 if (reslen)
260                                         *reslen = respos;
261                         }
262                 }
263                 waitpid(pid, &status, 0);
264                 if (WIFEXITED(status)) {
265                         info("'%s' returned with status %i", argv[0], WEXITSTATUS(status));
266                         if (WEXITSTATUS(status) != 0)
267                                 retval = -1;
268                 } else {
269                         err("'%s' abnormal exit", argv[0]);
270                         retval = -1;
271                 }
272         }
273
274         return retval;
275 }