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>
33 #include "udev_libc_wrapper.h"
36 #include "udev_utils.h"
40 int pass_env_to_socket(const char *sockname, const char *devpath, const char *action)
43 struct sockaddr_un saddr;
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;
58 bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
60 for (i = 0; environ[i] != NULL && bufpos < sizeof(buf); i++) {
61 bufpos += strlcpy(&buf[bufpos], environ[i], sizeof(buf) - bufpos-1);
65 retval = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, addrlen);
73 int execute_program(const char *command, const char *subsystem,
74 char *result, size_t ressize, size_t *reslen)
83 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]);
115 if (pipe(pipefds) != 0) {
124 /* child dup2 write side of pipe to STDOUT */
125 devnull = open("/dev/null", O_RDWR);
127 dup2(devnull, STDIN_FILENO);
129 dup2(devnull, STDOUT_FILENO);
130 dup2(devnull, STDERR_FILENO);
134 dup2(pipefds[1], STDOUT_FILENO);
136 err("exec of program failed");
139 err("fork of '%s' failed", arg);
142 /* parent reads from pipefds[0] */
147 count = read(pipefds[0], result + len, ressize - len-1);
149 err("read failed with '%s'", strerror(errno));
158 if (len >= ressize-1) {
159 err("ressize %ld too short", (long)ressize);
169 waitpid(pid, &status, 0);
171 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
172 dbg("exec program status 0x%x", status);