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