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