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;
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;
59 bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
61 for (i = 0; environ[i] != NULL && bufpos < sizeof(buf); i++) {
62 bufpos += strlcpy(&buf[bufpos], environ[i], sizeof(buf) - bufpos-1);
66 retval = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, addrlen);
74 int run_program(const char *command, const char *subsystem,
75 char *result, size_t ressize, size_t *reslen, int dbg)
79 int outpipe[2] = {-1, -1};
80 int errpipe[2] = {-1, -1};
84 char *argv[(sizeof(arg) / 2) + 1];
88 strlcpy(arg, command, sizeof(arg));
90 if (strchr(arg, ' ')) {
94 /* don't separate if in apostrophes */
96 argv[i] = strsep(&pos, "\'");
97 while (pos && pos[0] == ' ')
100 argv[i] = strsep(&pos, " ");
102 dbg("arg[%i] '%s'", i, argv[i]);
106 dbg("execute '%s' with parsed arguments", arg);
109 argv[1] = (char *) subsystem;
111 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
114 /* prepare pipes from child to parent */
116 if (pipe(outpipe) != 0) {
122 if (pipe(errpipe) != 0) {
131 /* child closes parent ends of pipes */
137 /* discard child output or connect to pipe */
138 devnull = open("/dev/null", O_RDWR);
140 err("open /dev/null failed");
143 dup2(devnull, STDIN_FILENO);
146 dup2(outpipe[1], STDOUT_FILENO);
148 dup2(devnull, STDOUT_FILENO);
151 dup2(errpipe[1], STDERR_FILENO);
153 dup2(devnull, STDERR_FILENO);
158 /* we should never reach this */
159 err("exec of program failed");
162 err("fork of '%s' failed", arg);
165 /* read from child if requested */
166 if (outpipe[0] > 0 || errpipe[0] > 0) {
170 /* parent closes child ends of pipes */
176 /* read child output */
177 while (outpipe[0] > 0 || errpipe[0] > 0) {
183 FD_SET(outpipe[0], &readfds);
185 FD_SET(errpipe[0], &readfds);
186 fdcount = select(UDEV_MAX(outpipe[0], errpipe[0])+1, &readfds, NULL, NULL, NULL);
195 if (outpipe[0] > 0 && FD_ISSET(outpipe[0], &readfds)) {
198 count = read(outpipe[0], inbuf, sizeof(inbuf)-1);
203 err("stdin read failed with '%s'", strerror(errno));
209 dbg("stdout: '%s'", inbuf);
212 if (respos + count >= ressize) {
213 err("ressize %ld too short", (long)ressize);
217 memcpy(&result[respos], inbuf, count);
223 if (errpipe[0] > 0 && FD_ISSET(errpipe[0], &readfds)) {
226 count = read(errpipe[0], errbuf, sizeof(errbuf)-1);
231 err("stderr read failed with '%s'", strerror(errno));
234 errbuf[count] = '\0';
235 dbg("stderr: '%s'", errbuf);
243 /* return the childs stdout string */
245 result[respos] = '\0';
246 dbg("result='%s'", result);
251 waitpid(pid, &status, 0);
252 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
253 dbg("exec program status 0x%x", status);