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.
28 #include <sys/socket.h>
31 #include <sys/select.h>
35 extern char **environ;
37 int pass_env_to_socket(const char *sockname, const char *devpath, const char *action)
40 struct sockaddr_un saddr;
48 dbg("pass environment to socket '%s'", sockname);
49 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
50 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
51 saddr.sun_family = AF_LOCAL;
52 /* abstract namespace only */
53 strcpy(&saddr.sun_path[1], sockname);
54 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
56 bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
58 for (i = 0; environ[i] != NULL && bufpos < sizeof(buf); i++) {
59 bufpos += strlcpy(&buf[bufpos], environ[i], sizeof(buf) - bufpos-1);
63 count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, addrlen);
66 info("passed %zi bytes to socket '%s', ", count, sockname);
72 int run_program(const char *command, const char *subsystem,
73 char *result, size_t ressize, size_t *reslen)
76 int outpipe[2] = {-1, -1};
77 int errpipe[2] = {-1, -1};
80 char program[PATH_SIZE];
81 char *argv[(sizeof(arg) / 2) + 1];
86 /* build argv from comand */
87 strlcpy(arg, command, sizeof(arg));
89 if (strchr(arg, ' ') != NULL) {
94 /* don't separate if in apostrophes */
96 argv[i] = strsep(&pos, "\'");
97 while (pos != NULL && pos[0] == ' ')
100 argv[i] = strsep(&pos, " ");
102 dbg("arg[%i] '%s'", i, argv[i]);
110 info("'%s'", command);
112 /* prepare pipes from child to parent */
113 if (result != NULL || udev_log_priority >= LOG_INFO) {
114 if (pipe(outpipe) != 0) {
115 err("pipe failed: %s", strerror(errno));
119 if (udev_log_priority >= LOG_INFO) {
120 if (pipe(errpipe) != 0) {
121 err("pipe failed: %s", strerror(errno));
126 /* allow programs in /lib/udev called without the path */
127 if (strchr(argv[0], '/') == NULL) {
128 strlcpy(program, "/lib/udev/", sizeof(program));
129 strlcat(program, argv[0], sizeof(program));
136 /* child closes parent ends of pipes */
137 if (outpipe[READ_END] > 0)
138 close(outpipe[READ_END]);
139 if (errpipe[READ_END] > 0)
140 close(errpipe[READ_END]);
142 /* discard child output or connect to pipe */
143 devnull = open("/dev/null", O_RDWR);
145 dup2(devnull, STDIN_FILENO);
146 if (outpipe[WRITE_END] < 0)
147 dup2(devnull, STDOUT_FILENO);
148 if (errpipe[WRITE_END] < 0)
149 dup2(devnull, STDERR_FILENO);
152 err("open /dev/null failed: %s", strerror(errno));
153 if (outpipe[WRITE_END] > 0) {
154 dup2(outpipe[WRITE_END], STDOUT_FILENO);
155 close(outpipe[WRITE_END]);
157 if (errpipe[WRITE_END] > 0) {
158 dup2(errpipe[WRITE_END], STDERR_FILENO);
159 close(errpipe[WRITE_END]);
161 execv(argv[0], argv);
162 if (errno == ENOENT || errno == ENOTDIR) {
163 /* may be on a filesytem which is not mounted right now */
164 info("program '%s' not found", argv[0]);
167 err("exec of program '%s' failed", argv[0]);
171 err("fork of '%s' failed: %s", argv[0], strerror(errno));
174 /* read from child if requested */
175 if (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
179 /* parent closes child ends of pipes */
180 if (outpipe[WRITE_END] > 0)
181 close(outpipe[WRITE_END]);
182 if (errpipe[WRITE_END] > 0)
183 close(errpipe[WRITE_END]);
185 /* read child output */
186 while (outpipe[READ_END] > 0 || errpipe[READ_END] > 0) {
191 if (outpipe[READ_END] > 0)
192 FD_SET(outpipe[READ_END], &readfds);
193 if (errpipe[READ_END] > 0)
194 FD_SET(errpipe[READ_END], &readfds);
195 fdcount = select(UDEV_MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL);
204 if (outpipe[READ_END] > 0 && FD_ISSET(outpipe[READ_END], &readfds)) {
209 count = read(outpipe[READ_END], inbuf, sizeof(inbuf)-1);
211 close(outpipe[READ_END]);
212 outpipe[READ_END] = -1;
214 err("stdin read failed: %s", strerror(errno));
221 /* store result for rule processing */
223 if (respos + count < ressize) {
224 memcpy(&result[respos], inbuf, count);
227 err("ressize %ld too short", (long)ressize);
232 while ((line = strsep(&pos, "\n")))
233 if (pos || line[0] != '\0')
234 info("'%s' (stdout) '%s'", argv[0], line);
238 if (errpipe[READ_END] > 0 && FD_ISSET(errpipe[READ_END], &readfds)) {
243 count = read(errpipe[READ_END], errbuf, sizeof(errbuf)-1);
245 close(errpipe[READ_END]);
246 errpipe[READ_END] = -1;
248 err("stderr read failed: %s", strerror(errno));
251 errbuf[count] = '\0';
253 while ((line = strsep(&pos, "\n")))
254 if (pos || line[0] != '\0')
255 info("'%s' (stderr) '%s'", argv[0], line);
258 if (outpipe[READ_END] > 0)
259 close(outpipe[READ_END]);
260 if (errpipe[READ_END] > 0)
261 close(errpipe[READ_END]);
263 /* return the childs stdout string */
265 result[respos] = '\0';
266 dbg("result='%s'", result);
271 waitpid(pid, &status, 0);
272 if (WIFEXITED(status)) {
273 info("'%s' returned with status %i", argv[0], WEXITSTATUS(status));
274 if (WEXITSTATUS(status) != 0)
277 err("'%s' abnormal exit", argv[0]);