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