]> git.ipfire.org Git - thirdparty/systemd.git/blob - udevsend.c
[PATCH] udev - safer string handling all over the place
[thirdparty/systemd.git] / udevsend.c
1 /*
2 * udevsend.c
3 *
4 * Userspace devfs
5 *
6 * Copyright (C) 2004 Ling, Xiaofeng <xiaofeng.ling@intel.com>
7 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
8 *
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation version 2 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/wait.h>
28 #include <sys/un.h>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stddef.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <time.h>
36 #include <linux/stddef.h>
37
38 #include "udev.h"
39 #include "udev_version.h"
40 #include "udevd.h"
41 #include "logging.h"
42
43 #ifdef LOG
44 unsigned char logname[42];
45 void log_message (int level, const char *format, ...)
46 {
47 va_list args;
48
49 va_start(args, format);
50 vsyslog(level, format, args);
51 va_end(args);
52 }
53 #endif
54
55 static inline char *get_action(void)
56 {
57 char *action;
58
59 action = getenv("ACTION");
60 return action;
61 }
62
63 static inline char *get_devpath(void)
64 {
65 char *devpath;
66
67 devpath = getenv("DEVPATH");
68 return devpath;
69 }
70
71 static inline char *get_seqnum(void)
72 {
73 char *seqnum;
74
75 seqnum = getenv("SEQNUM");
76 return seqnum;
77 }
78
79 static int build_hotplugmsg(struct hotplug_msg *msg, char *action,
80 char *devpath, char *subsystem, int seqnum)
81 {
82 memset(msg, 0x00, sizeof(*msg));
83 strfieldcpy(msg->magic, UDEV_MAGIC);
84 msg->seqnum = seqnum;
85 strfieldcpy(msg->action, action);
86 strfieldcpy(msg->devpath, devpath);
87 strfieldcpy(msg->subsystem, subsystem);
88 return sizeof(struct hotplug_msg);
89 }
90
91 static int start_daemon(void)
92 {
93 pid_t pid;
94 pid_t child_pid;
95
96 pid = fork();
97 switch (pid) {
98 case 0:
99 /* helper child */
100 child_pid = fork();
101 switch (child_pid) {
102 case 0:
103 /* daemon */
104 setsid();
105 chdir("/");
106 execl(UDEVD_BIN, "udevd", NULL);
107 dbg("exec of daemon failed");
108 exit(1);
109 case -1:
110 dbg("fork of daemon failed");
111 return -1;
112 default:
113 exit(0);
114 }
115 break;
116 case -1:
117 dbg("fork of helper failed");
118 return -1;
119 default:
120 wait(NULL);
121 }
122 return 0;
123 }
124
125 int main(int argc, char* argv[])
126 {
127 struct hotplug_msg msg;
128 char *action;
129 char *devpath;
130 char *subsystem;
131 char *seqnum;
132 int seq;
133 int retval = 1;
134 int size;
135 int loop;
136 struct timespec tspec;
137 int sock;
138 struct sockaddr_un saddr;
139 socklen_t addrlen;
140 int started_daemon = 0;
141
142 #ifdef DEBUG
143 init_logging("udevsend");
144 #endif
145 dbg("version %s", UDEV_VERSION);
146
147 subsystem = argv[1];
148 if (subsystem == NULL) {
149 dbg("no subsystem");
150 goto exit;
151 }
152 dbg("subsystem = '%s'", subsystem);
153
154 devpath = get_devpath();
155 if (devpath == NULL) {
156 dbg("no devpath");
157 goto exit;
158 }
159 dbg("DEVPATH = '%s'", devpath);
160
161 action = get_action();
162 if (action == NULL) {
163 dbg("no action");
164 goto exit;
165 }
166 dbg("ACTION = '%s'", action);
167
168 seqnum = get_seqnum();
169 if (seqnum == NULL)
170 seq = -1;
171 else
172 seq = atoi(seqnum);
173 dbg("SEQNUM = '%d'", seq);
174
175 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
176 if (sock == -1) {
177 dbg("error getting socket");
178 goto exit;
179 }
180
181 memset(&saddr, 0x00, sizeof(saddr));
182 saddr.sun_family = AF_LOCAL;
183 /* use abstract namespace for socket path */
184 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
185 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
186
187 size = build_hotplugmsg(&msg, action, devpath, subsystem, seq);
188
189 /* If we can't send, try to start daemon and resend message */
190 loop = UDEVSEND_CONNECT_RETRY;
191 while (loop--) {
192 retval = sendto(sock, &msg, size, 0, (struct sockaddr *)&saddr, addrlen);
193 if (retval != -1) {
194 retval = 0;
195 goto close_and_exit;
196 }
197
198 if (errno != ECONNREFUSED) {
199 dbg("error sending message");
200 goto close_and_exit;
201 }
202
203 if (!started_daemon) {
204 dbg("connect failed, try starting daemon...");
205 retval = start_daemon();
206 if (retval) {
207 dbg("error starting daemon");
208 goto exit;
209 }
210
211 dbg("daemon started");
212 started_daemon = 1;
213 } else {
214 dbg("retry to connect %d", UDEVSEND_CONNECT_RETRY - loop);
215 tspec.tv_sec = 0;
216 tspec.tv_nsec = 100000000; /* 100 millisec */
217 nanosleep(&tspec, NULL);
218 }
219 }
220
221 close_and_exit:
222 close(sock);
223 exit:
224 return retval;
225 }