2 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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.
27 #include <sys/socket.h>
30 #include <sys/select.h>
34 extern char **environ;
36 int pass_env_to_socket(const char *sockname, const char *devpath, const char *action)
39 struct sockaddr_un saddr;
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;
55 bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
57 for (i = 0; environ[i] != NULL && bufpos < sizeof(buf); i++) {
58 bufpos += strlcpy(&buf[bufpos], environ[i], sizeof(buf) - bufpos-1);
62 count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, addrlen);
65 info("passed %zi bytes to socket '%s', ", count, sockname);
71 int run_program(const char *command, const char *subsystem,
72 char *result, size_t ressize, size_t *reslen, int log)
76 int outpipe[2] = {-1, -1};
77 int errpipe[2] = {-1, -1};
80 char program[PATH_SIZE];
81 char *argv[(sizeof(arg) / 2) + 1];
85 /* build argv from comand */
86 strlcpy(arg, command, sizeof(arg));
88 if (strchr(arg, ' ') != NULL) {
93 /* don't separate if in apostrophes */
95 argv[i] = strsep(&pos, "\'");
96 while (pos != NULL && pos[0] == ' ')
99 argv[i] = strsep(&pos, " ");
101 dbg("arg[%i] '%s'", i, argv[i]);
109 info("'%s'", command);
111 /* prepare pipes from child to parent */
113 if (pipe(outpipe) != 0) {
114 err("pipe failed: %s", strerror(errno));
119 if (pipe(errpipe) != 0) {
120 err("pipe failed: %s", strerror(errno));
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));
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]);
141 /* discard child output or connect to pipe */
142 devnull = open("/dev/null", O_RDWR);
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);
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]);
156 if (errpipe[WRITE_END] > 0) {
157 dup2(errpipe[WRITE_END], STDERR_FILENO);
158 close(errpipe[WRITE_END]);
160 execv(argv[0], argv);
161 if ((errno == ENOENT) || (errno = ENOTDIR)) {
162 /* may be on a filesytem which is not mounted right now */
163 info("program '%s' not found", argv[0]);
166 err("exec of program '%s' failed", argv[0]);
170 err("fork of '%s' failed: %s", argv[0], strerror(errno));
173 /* read from child if requested */
174 if (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
178 /* parent closes child ends of pipes */
179 if (outpipe[WRITE_END] > 0)
180 close(outpipe[WRITE_END]);
181 if (errpipe[WRITE_END] > 0)
182 close(errpipe[WRITE_END]);
184 /* read child output */
185 while (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
190 if (outpipe[READ_END] > 0)
191 FD_SET(outpipe[READ_END], &readfds);
192 if (errpipe[READ_END] > 0)
193 FD_SET(errpipe[READ_END], &readfds);
194 fdcount = select(UDEV_MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL);
203 if (outpipe[READ_END] > 0 && FD_ISSET(outpipe[READ_END], &readfds)) {
208 count = read(outpipe[READ_END], inbuf, sizeof(inbuf)-1);
210 close(outpipe[READ_END]);
211 outpipe[READ_END] = -1;
213 err("stdin read failed: %s", strerror(errno));
220 /* store result for rule processing */
222 if (respos + count < ressize) {
223 memcpy(&result[respos], inbuf, count);
226 err("ressize %ld too short", (long)ressize);
231 while ((line = strsep(&pos, "\n")))
232 if (pos || line[0] != '\0')
233 info("'%s' (stdout) '%s'", argv[0], line);
237 if (errpipe[READ_END] > 0 && FD_ISSET(errpipe[READ_END], &readfds)) {
242 count = read(errpipe[READ_END], errbuf, sizeof(errbuf)-1);
244 close(errpipe[READ_END]);
245 errpipe[READ_END] = -1;
247 err("stderr read failed: %s", strerror(errno));
250 errbuf[count] = '\0';
252 while ((line = strsep(&pos, "\n")))
253 if (pos || line[0] != '\0')
254 info("'%s' (stderr) '%s'", argv[0], line);
257 if (outpipe[READ_END] > 0)
258 close(outpipe[READ_END]);
259 if (errpipe[READ_END] > 0)
260 close(errpipe[READ_END]);
262 /* return the childs stdout string */
264 result[respos] = '\0';
265 dbg("result='%s'", result);
270 waitpid(pid, &status, 0);
271 if (WIFEXITED(status)) {
272 info("'%s' returned with status %i", argv[0], WEXITSTATUS(status));
273 if (WEXITSTATUS(status) != 0)
276 err("'%s' abnormal exit", argv[0]);