chiark / gitweb /
don't fail too bad, if /dev/null does not exist
[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                         dup2(devnull, STDIN_FILENO);
141                         if (outpipe[1] < 0)
142                                 dup2(devnull, STDOUT_FILENO);
143                         if (errpipe[1] < 0)
144                                 dup2(devnull, STDERR_FILENO);
145                         close(devnull);
146                 } else
147                         err("open /dev/null failed");
148                 if (outpipe[1] > 0)
149                         dup2(outpipe[1], STDOUT_FILENO);
150                 if (errpipe[1] > 0)
151                         dup2(errpipe[1], STDERR_FILENO);
152                 execv(arg, argv);
153
154                 /* we should never reach this */
155                 err("exec of program failed");
156                 _exit(1);
157         case -1:
158                 err("fork of '%s' failed", arg);
159                 return -1;
160         default:
161                 /* read from child if requested */
162                 if (outpipe[0] > 0 || errpipe[0] > 0) {
163                         ssize_t count;
164                         size_t respos = 0;
165
166                         /* parent closes child ends of pipes */
167                         if (outpipe[1] > 0)
168                                 close(outpipe[1]);
169                         if (errpipe[1] > 0)
170                                 close(errpipe[1]);
171
172                         /* read child output */
173                         while (outpipe[0] > 0 || errpipe[0] > 0) {
174                                 int fdcount;
175                                 fd_set readfds;
176
177                                 FD_ZERO(&readfds);
178                                 if (outpipe[0] > 0)
179                                         FD_SET(outpipe[0], &readfds);
180                                 if (errpipe[0] > 0)
181                                         FD_SET(errpipe[0], &readfds);
182                                 fdcount = select(UDEV_MAX(outpipe[0], errpipe[0])+1, &readfds, NULL, NULL, NULL);
183                                 if (fdcount < 0) {
184                                         if (errno == EINTR)
185                                                 continue;
186                                         retval = -1;
187                                         break;
188                                 }
189
190                                 /* get stdout */
191                                 if (outpipe[0] > 0 && FD_ISSET(outpipe[0], &readfds)) {
192                                         char inbuf[1024];
193
194                                         count = read(outpipe[0], inbuf, sizeof(inbuf)-1);
195                                         if (count <= 0) {
196                                                 close(outpipe[0]);
197                                                 outpipe[0] = -1;
198                                                 if (count < 0) {
199                                                         err("stdin read failed with '%s'", strerror(errno));
200                                                         retval = -1;
201                                                 }
202                                                 continue;
203                                         }
204                                         inbuf[count] = '\0';
205                                         dbg("stdout: '%s'", inbuf);
206
207                                         if (result) {
208                                                 if (respos + count >= ressize) {
209                                                         err("ressize %ld too short", (long)ressize);
210                                                         retval = -1;
211                                                         continue;
212                                                 }
213                                                 memcpy(&result[respos], inbuf, count);
214                                                 respos += count;
215                                         }
216                                 }
217
218                                 /* get stderr */
219                                 if (errpipe[0] > 0 && FD_ISSET(errpipe[0], &readfds)) {
220                                         char errbuf[1024];
221
222                                         count = read(errpipe[0], errbuf, sizeof(errbuf)-1);
223                                         if (count <= 0) {
224                                                 close(errpipe[0]);
225                                                 errpipe[0] = -1;
226                                                 if (count < 0)
227                                                         err("stderr read failed with '%s'", strerror(errno));
228                                                 continue;
229                                         }
230                                         errbuf[count] = '\0';
231                                         dbg("stderr: '%s'", errbuf);
232                                 }
233                         }
234                         if (outpipe[0] > 0)
235                                 close(outpipe[0]);
236                         if (errpipe[0] > 0)
237                                 close(errpipe[0]);
238
239                         /* return the childs stdout string */
240                         if (result) {
241                                 result[respos] = '\0';
242                                 dbg("result='%s'", result);
243                                 if (reslen)
244                                         *reslen = respos;
245                         }
246                 }
247                 waitpid(pid, &status, 0);
248                 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
249                         dbg("exec program status 0x%x", status);
250                         retval = -1;
251                 }
252         }
253
254         return retval;
255 }