chiark / gitweb /
fix udevstart event ordering, we want /dev/null very early
[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_libc_wrapper.h"
35 #include "udev.h"
36 #include "logging.h"
37 #include "udev_utils.h"
38 #include "list.h"
39
40
41 int pass_env_to_socket(const char *sockname, const char *devpath, const char *action)
42 {
43         int sock;
44         struct sockaddr_un saddr;
45         socklen_t addrlen;
46         char buf[2048];
47         size_t bufpos = 0;
48         int i;
49         int retval;
50
51         dbg("pass environment to socket '%s'", sockname);
52         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
53         memset(&saddr, 0x00, sizeof(struct sockaddr_un));
54         saddr.sun_family = AF_LOCAL;
55         /* only abstract namespace is supported */
56         strcpy(&saddr.sun_path[1], sockname);
57         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
58
59         bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
60         bufpos++;
61         for (i = 0; environ[i] != NULL && bufpos < sizeof(buf); i++) {
62                 bufpos += strlcpy(&buf[bufpos], environ[i], sizeof(buf) - bufpos-1);
63                 bufpos++;
64         }
65
66         retval = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, addrlen);
67         if (retval != -1)
68                 retval = 0;
69
70         close(sock);
71         return retval;
72 }
73
74 int run_program(const char *command, const char *subsystem,
75                 char *result, size_t ressize, size_t *reslen, int dbg)
76 {
77         int retval = 0;
78         int status;
79         int outpipe[2] = {-1, -1};
80         int errpipe[2] = {-1, -1};
81         pid_t pid;
82         char *pos;
83         char arg[PATH_SIZE];
84         char *argv[(sizeof(arg) / 2) + 1];
85         int devnull;
86         int i;
87
88         strlcpy(arg, command, sizeof(arg));
89         i = 0;
90         if (strchr(arg, ' ')) {
91                 pos = arg;
92                 while (pos != NULL) {
93                         if (pos[0] == '\'') {
94                                 /* don't separate if in apostrophes */
95                                 pos++;
96                                 argv[i] = strsep(&pos, "\'");
97                                 while (pos && pos[0] == ' ')
98                                         pos++;
99                         } else {
100                                 argv[i] = strsep(&pos, " ");
101                         }
102                         dbg("arg[%i] '%s'", i, argv[i]);
103                         i++;
104                 }
105                 argv[i] = NULL;
106                 dbg("execute '%s' with parsed arguments", arg);
107         } else {
108                 argv[0] = arg;
109                 argv[1] = (char *) subsystem;
110                 argv[2] = NULL;
111                 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
112         }
113
114         /* prepare pipes from child to parent */
115         if (result || dbg) {
116                 if (pipe(outpipe) != 0) {
117                         err("pipe failed");
118                         return -1;
119                 }
120         }
121         if (dbg) {
122                 if (pipe(errpipe) != 0) {
123                         err("pipe failed");
124                         return -1;
125                 }
126         }
127
128         pid = fork();
129         switch(pid) {
130         case 0:
131                 /* child closes parent ends of pipes */
132                 if (outpipe[0] > 0)
133                         close(outpipe[0]);
134                 if (errpipe[0] > 0)
135                         close(errpipe[0]);
136
137                 /* discard child output or connect to pipe */
138                 devnull = open("/dev/null", O_RDWR);
139                 if (devnull < 0) {
140                         err("open /dev/null failed");
141                         exit(1);
142                 }
143                 dup2(devnull, STDIN_FILENO);
144
145                 if (outpipe[1] > 0)
146                         dup2(outpipe[1], STDOUT_FILENO);
147                 else
148                         dup2(devnull, STDOUT_FILENO);
149
150                 if (errpipe[1] > 0)
151                         dup2(errpipe[1], STDERR_FILENO);
152                 else
153                         dup2(devnull, STDERR_FILENO);
154
155                 close(devnull);
156                 execv(arg, argv);
157
158                 /* we should never reach this */
159                 err("exec of program failed");
160                 _exit(1);
161         case -1:
162                 err("fork of '%s' failed", arg);
163                 return -1;
164         default:
165                 /* read from child if requested */
166                 if (outpipe[0] > 0 || errpipe[0] > 0) {
167                         ssize_t count;
168                         size_t respos = 0;
169
170                         /* parent closes child ends of pipes */
171                         if (outpipe[1] > 0)
172                                 close(outpipe[1]);
173                         if (errpipe[1] > 0)
174                                 close(errpipe[1]);
175
176                         /* read child output */
177                         while (outpipe[0] > 0 || errpipe[0] > 0) {
178                                 int fdcount;
179                                 fd_set readfds;
180
181                                 FD_ZERO(&readfds);
182                                 if (outpipe[0] > 0)
183                                         FD_SET(outpipe[0], &readfds);
184                                 if (errpipe[0] > 0)
185                                         FD_SET(errpipe[0], &readfds);
186                                 fdcount = select(UDEV_MAX(outpipe[0], errpipe[0])+1, &readfds, NULL, NULL, NULL);
187                                 if (fdcount < 0) {
188                                         if (errno == EINTR)
189                                                 continue;
190                                         retval = -1;
191                                         break;
192                                 }
193
194                                 /* get stdout */
195                                 if (outpipe[0] > 0 && FD_ISSET(outpipe[0], &readfds)) {
196                                         char inbuf[1024];
197
198                                         count = read(outpipe[0], inbuf, sizeof(inbuf)-1);
199                                         if (count <= 0) {
200                                                 close(outpipe[0]);
201                                                 outpipe[0] = -1;
202                                                 if (count < 0) {
203                                                         err("stdin read failed with '%s'", strerror(errno));
204                                                         retval = -1;
205                                                 }
206                                                 continue;
207                                         }
208                                         inbuf[count] = '\0';
209                                         dbg("stdout: '%s'", inbuf);
210
211                                         if (result) {
212                                                 if (respos + count >= ressize) {
213                                                         err("ressize %ld too short", (long)ressize);
214                                                         retval = -1;
215                                                         continue;
216                                                 }
217                                                 memcpy(&result[respos], inbuf, count);
218                                                 respos += count;
219                                         }
220                                 }
221
222                                 /* get stderr */
223                                 if (errpipe[0] > 0 && FD_ISSET(errpipe[0], &readfds)) {
224                                         char errbuf[1024];
225
226                                         count = read(errpipe[0], errbuf, sizeof(errbuf)-1);
227                                         if (count <= 0) {
228                                                 close(errpipe[0]);
229                                                 errpipe[0] = -1;
230                                                 if (count < 0)
231                                                         err("stderr read failed with '%s'", strerror(errno));
232                                                 continue;
233                                         }
234                                         errbuf[count] = '\0';
235                                         dbg("stderr: '%s'", errbuf);
236                                 }
237                         }
238                         if (outpipe[0] > 0)
239                                 close(outpipe[0]);
240                         if (errpipe[0] > 0)
241                                 close(errpipe[0]);
242
243                         /* return the childs stdout string */
244                         if (result) {
245                                 result[respos] = '\0';
246                                 dbg("result='%s'", result);
247                                 if (reslen)
248                                         *reslen = respos;
249                         }
250                 }
251                 waitpid(pid, &status, 0);
252                 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
253                         dbg("exec program status 0x%x", status);
254                         retval = -1;
255                 }
256         }
257
258         return retval;
259 }