chiark / gitweb /
rules: Fedora update
[elogind.git] / udev_utils_run.c
1 /*
2  * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
3  *
4  *      This program is free software; you can redistribute it and/or modify it
5  *      under the terms of the GNU General Public License as published by the
6  *      Free Software Foundation version 2 of the License.
7  * 
8  *      This program is distributed in the hope that it will be useful, but
9  *      WITHOUT ANY WARRANTY; without even the implied warranty of
10  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  *      General Public License for more details.
12  * 
13  *      You should have received a copy of the GNU General Public License along
14  *      with this program; if not, write to the Free Software Foundation, Inc.,
15  *      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16  *
17  */
18
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <stddef.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <errno.h>
26 #include <ctype.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include <sys/wait.h>
30 #include <sys/select.h>
31
32 #include "udev.h"
33
34 extern char **environ;
35
36 int pass_env_to_socket(const char *sockname, const char *devpath, const char *action)
37 {
38         int sock;
39         struct sockaddr_un saddr;
40         socklen_t addrlen;
41         char buf[2048];
42         size_t bufpos = 0;
43         int i;
44         ssize_t count;
45         int retval = 0;
46
47         dbg("pass environment to socket '%s'", sockname);
48         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
49         memset(&saddr, 0x00, sizeof(struct sockaddr_un));
50         saddr.sun_family = AF_LOCAL;
51         /* abstract namespace only */
52         strcpy(&saddr.sun_path[1], sockname);
53         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
54
55         bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
56         bufpos++;
57         for (i = 0; environ[i] != NULL && bufpos < sizeof(buf); i++) {
58                 bufpos += strlcpy(&buf[bufpos], environ[i], sizeof(buf) - bufpos-1);
59                 bufpos++;
60         }
61
62         count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, addrlen);
63         if (count < 0)
64                 retval = -1;
65         info("passed %zi bytes to socket '%s', ", count, sockname);
66
67         close(sock);
68         return retval;
69 }
70
71 int run_program(const char *command, const char *subsystem,
72                 char *result, size_t ressize, size_t *reslen, int log)
73 {
74         int retval = 0;
75         int status;
76         int outpipe[2] = {-1, -1};
77         int errpipe[2] = {-1, -1};
78         pid_t pid;
79         char arg[PATH_SIZE];
80         char program[PATH_SIZE];
81         char *argv[(sizeof(arg) / 2) + 1];
82         int devnull;
83         int i;
84
85         /* build argv from comand */
86         strlcpy(arg, command, sizeof(arg));
87         i = 0;
88         if (strchr(arg, ' ') != NULL) {
89                 char *pos = arg;
90
91                 while (pos != NULL) {
92                         if (pos[0] == '\'') {
93                                 /* don't separate if in apostrophes */
94                                 pos++;
95                                 argv[i] = strsep(&pos, "\'");
96                                 while (pos != NULL && pos[0] == ' ')
97                                         pos++;
98                         } else {
99                                 argv[i] = strsep(&pos, " ");
100                         }
101                         dbg("arg[%i] '%s'", i, argv[i]);
102                         i++;
103                 }
104                 argv[i] = NULL;
105         } else {
106                 argv[0] = arg;
107                 argv[1] = NULL;
108         }
109         info("'%s'", command);
110
111         /* prepare pipes from child to parent */
112         if (result || log) {
113                 if (pipe(outpipe) != 0) {
114                         err("pipe failed: %s", strerror(errno));
115                         return -1;
116                 }
117         }
118         if (log) {
119                 if (pipe(errpipe) != 0) {
120                         err("pipe failed: %s", strerror(errno));
121                         return -1;
122                 }
123         }
124
125         /* allow programs in /lib/udev called without the path */
126         if (strchr(argv[0], '/') == NULL) {
127                 strlcpy(program, "/lib/udev/", sizeof(program));
128                 strlcat(program, argv[0], sizeof(program));
129                 argv[0] = program;
130         }
131
132         pid = fork();
133         switch(pid) {
134         case 0:
135                 /* child closes parent ends of pipes */
136                 if (outpipe[READ_END] > 0)
137                         close(outpipe[READ_END]);
138                 if (errpipe[READ_END] > 0)
139                         close(errpipe[READ_END]);
140
141                 /* discard child output or connect to pipe */
142                 devnull = open("/dev/null", O_RDWR);
143                 if (devnull > 0) {
144                         dup2(devnull, STDIN_FILENO);
145                         if (outpipe[WRITE_END] < 0)
146                                 dup2(devnull, STDOUT_FILENO);
147                         if (errpipe[WRITE_END] < 0)
148                                 dup2(devnull, STDERR_FILENO);
149                         close(devnull);
150                 } else
151                         err("open /dev/null failed: %s", strerror(errno));
152                 if (outpipe[WRITE_END] > 0) {
153                         dup2(outpipe[WRITE_END], STDOUT_FILENO);
154                         close(outpipe[WRITE_END]);
155                 }
156                 if (errpipe[WRITE_END] > 0) {
157                         dup2(errpipe[WRITE_END], STDERR_FILENO);
158                         close(errpipe[WRITE_END]);
159                 }
160                 execv(argv[0], argv);
161
162                 /* we should never reach this */
163                 err("exec of program '%s' failed", argv[0]);
164                 _exit(1);
165         case -1:
166                 err("fork of '%s' failed: %s", argv[0], strerror(errno));
167                 return -1;
168         default:
169                 /* read from child if requested */
170                 if (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
171                         ssize_t count;
172                         size_t respos = 0;
173
174                         /* parent closes child ends of pipes */
175                         if (outpipe[WRITE_END] > 0)
176                                 close(outpipe[WRITE_END]);
177                         if (errpipe[WRITE_END] > 0)
178                                 close(errpipe[WRITE_END]);
179
180                         /* read child output */
181                         while (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
182                                 int fdcount;
183                                 fd_set readfds;
184
185                                 FD_ZERO(&readfds);
186                                 if (outpipe[READ_END] > 0)
187                                         FD_SET(outpipe[READ_END], &readfds);
188                                 if (errpipe[READ_END] > 0)
189                                         FD_SET(errpipe[READ_END], &readfds);
190                                 fdcount = select(UDEV_MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL);
191                                 if (fdcount < 0) {
192                                         if (errno == EINTR)
193                                                 continue;
194                                         retval = -1;
195                                         break;
196                                 }
197
198                                 /* get stdout */
199                                 if (outpipe[READ_END] > 0 && FD_ISSET(outpipe[READ_END], &readfds)) {
200                                         char inbuf[1024];
201                                         char *pos;
202                                         char *line;
203
204                                         count = read(outpipe[READ_END], inbuf, sizeof(inbuf)-1);
205                                         if (count <= 0) {
206                                                 close(outpipe[READ_END]);
207                                                 outpipe[READ_END] = -1;
208                                                 if (count < 0) {
209                                                         err("stdin read failed: %s", strerror(errno));
210                                                         retval = -1;
211                                                 }
212                                                 continue;
213                                         }
214                                         inbuf[count] = '\0';
215
216                                         /* store result for rule processing */
217                                         if (result) {
218                                                 if (respos + count < ressize) {
219                                                         memcpy(&result[respos], inbuf, count);
220                                                         respos += count;
221                                                 } else {
222                                                         err("ressize %ld too short", (long)ressize);
223                                                         retval = -1;
224                                                 }
225                                         }
226                                         pos = inbuf;
227                                         while ((line = strsep(&pos, "\n")))
228                                                 if (pos || line[0] != '\0')
229                                                         info("'%s' (stdout) '%s'", argv[0], line);
230                                 }
231
232                                 /* get stderr */
233                                 if (errpipe[READ_END] > 0 && FD_ISSET(errpipe[READ_END], &readfds)) {
234                                         char errbuf[1024];
235                                         char *pos;
236                                         char *line;
237
238                                         count = read(errpipe[READ_END], errbuf, sizeof(errbuf)-1);
239                                         if (count <= 0) {
240                                                 close(errpipe[READ_END]);
241                                                 errpipe[READ_END] = -1;
242                                                 if (count < 0)
243                                                         err("stderr read failed: %s", strerror(errno));
244                                                 continue;
245                                         }
246                                         errbuf[count] = '\0';
247                                         pos = errbuf;
248                                         while ((line = strsep(&pos, "\n")))
249                                                 if (pos || line[0] != '\0')
250                                                         info("'%s' (stderr) '%s'", argv[0], line);
251                                 }
252                         }
253                         if (outpipe[READ_END] > 0)
254                                 close(outpipe[READ_END]);
255                         if (errpipe[READ_END] > 0)
256                                 close(errpipe[READ_END]);
257
258                         /* return the childs stdout string */
259                         if (result) {
260                                 result[respos] = '\0';
261                                 dbg("result='%s'", result);
262                                 if (reslen)
263                                         *reslen = respos;
264                         }
265                 }
266                 waitpid(pid, &status, 0);
267                 if (WIFEXITED(status)) {
268                         info("'%s' returned with status %i", argv[0], WEXITSTATUS(status));
269                         if (WEXITSTATUS(status) != 0)
270                                 retval = -1;
271                 } else {
272                         err("'%s' abnormal exit", argv[0]);
273                         retval = -1;
274                 }
275         }
276
277         return retval;
278 }