2 * udev_utils_run.c - execute programs from udev and read its output
4 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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.
29 #include <sys/socket.h>
32 #include <sys/select.h>
34 #include "udev_libc_wrapper.h"
37 #include "udev_utils.h"
41 int pass_env_to_socket(const char *sockname, const char *devpath, const char *action)
44 struct sockaddr_un saddr;
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;
60 bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
62 for (i = 0; environ[i] != NULL && bufpos < sizeof(buf); i++) {
63 bufpos += strlcpy(&buf[bufpos], environ[i], sizeof(buf) - bufpos-1);
67 count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, addrlen);
70 info("passed %zi bytes to socket '%s', ", count, sockname);
76 int run_program(const char *command, const char *subsystem,
77 char *result, size_t ressize, size_t *reslen, int log)
81 int outpipe[2] = {-1, -1};
82 int errpipe[2] = {-1, -1};
85 char *argv[(sizeof(arg) / 2) + 1];
89 strlcpy(arg, command, sizeof(arg));
91 if (strchr(arg, ' ')) {
95 /* don't separate if in apostrophes */
97 argv[i] = strsep(&pos, "\'");
98 while (pos && pos[0] == ' ')
101 argv[i] = strsep(&pos, " ");
103 dbg("arg[%i] '%s'", i, argv[i]);
107 info("'%s'", command);
110 argv[1] = (char *) subsystem;
112 info("'%s' '%s'", arg, argv[1]);
115 /* prepare pipes from child to parent */
117 if (pipe(outpipe) != 0) {
118 err("pipe failed: %s", strerror(errno));
123 if (pipe(errpipe) != 0) {
124 err("pipe failed: %s", strerror(errno));
132 /* child closes parent ends of pipes */
133 if (outpipe[READ_END] > 0)
134 close(outpipe[READ_END]);
135 if (errpipe[READ_END] > 0)
136 close(errpipe[READ_END]);
138 /* discard child output or connect to pipe */
139 devnull = open("/dev/null", O_RDWR);
141 dup2(devnull, STDIN_FILENO);
142 if (outpipe[WRITE_END] < 0)
143 dup2(devnull, STDOUT_FILENO);
144 if (errpipe[WRITE_END] < 0)
145 dup2(devnull, STDERR_FILENO);
148 err("open /dev/null failed: %s", strerror(errno));
149 if (outpipe[WRITE_END] > 0)
150 dup2(outpipe[WRITE_END], STDOUT_FILENO);
151 if (errpipe[WRITE_END] > 0)
152 dup2(errpipe[WRITE_END], STDERR_FILENO);
153 execv(argv[0], argv);
155 /* we should never reach this */
156 err("exec of program '%s' failed", argv[0]);
159 err("fork of '%s' failed: %s", argv[0], strerror(errno));
162 /* read from child if requested */
163 if (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
167 /* parent closes child ends of pipes */
168 if (outpipe[WRITE_END] > 0)
169 close(outpipe[WRITE_END]);
170 if (errpipe[WRITE_END] > 0)
171 close(errpipe[WRITE_END]);
173 /* read child output */
174 while (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
179 if (outpipe[READ_END] > 0)
180 FD_SET(outpipe[READ_END], &readfds);
181 if (errpipe[READ_END] > 0)
182 FD_SET(errpipe[READ_END], &readfds);
183 fdcount = select(UDEV_MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL);
192 if (outpipe[READ_END] > 0 && FD_ISSET(outpipe[READ_END], &readfds)) {
197 count = read(outpipe[READ_END], inbuf, sizeof(inbuf)-1);
199 close(outpipe[READ_END]);
200 outpipe[READ_END] = -1;
202 err("stdin read failed: %s", strerror(errno));
209 /* store result for rule processing */
211 if (respos + count < ressize) {
212 memcpy(&result[respos], inbuf, count);
215 err("ressize %ld too short", (long)ressize);
220 while ((line = strsep(&pos, "\n")))
221 if (pos || line[0] != '\0')
222 info("'%s' (stdout) '%s'", argv[0], line);
226 if (errpipe[READ_END] > 0 && FD_ISSET(errpipe[READ_END], &readfds)) {
231 count = read(errpipe[READ_END], errbuf, sizeof(errbuf)-1);
233 close(errpipe[READ_END]);
234 errpipe[READ_END] = -1;
236 err("stderr read failed: %s", strerror(errno));
239 errbuf[count] = '\0';
241 while ((line = strsep(&pos, "\n")))
242 if (pos || line[0] != '\0')
243 info("'%s' (stderr) '%s'", argv[0], line);
246 if (outpipe[READ_END] > 0)
247 close(outpipe[READ_END]);
248 if (errpipe[READ_END] > 0)
249 close(errpipe[READ_END]);
251 /* return the childs stdout string */
253 result[respos] = '\0';
254 dbg("result='%s'", result);
259 waitpid(pid, &status, 0);
260 if (WIFEXITED(status)) {
261 info("'%s' returned with status %i", argv[0], WEXITSTATUS(status));
262 if (WEXITSTATUS(status) != 0)
265 err("'%s' abnormal exit", argv[0]);