chiark / gitweb /
[PATCH] 038 release
[elogind.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 <time.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stddef.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <linux/stddef.h>
37
38 #include "udev.h"
39 #include "udev_lib.h"
40 #include "udev_version.h"
41 #include "udevd.h"
42 #include "logging.h"
43
44 #ifdef LOG
45 unsigned char logname[LOGNAME_SIZE];
46 void log_message (int level, const char *format, ...)
47 {
48         va_list args;
49
50         va_start(args, format);
51         vsyslog(level, format, args);
52         va_end(args);
53 }
54 #endif
55
56 static int start_daemon(void)
57 {
58         pid_t pid;
59         pid_t child_pid;
60
61         pid = fork();
62         switch (pid) {
63         case 0:
64                 /* helper child */
65                 child_pid = fork();
66                 switch (child_pid) {
67                 case 0:
68                         /* daemon */
69                         setsid();
70                         chdir("/");
71                         execl(UDEVD_BIN, "udevd", NULL);
72                         dbg("exec of daemon failed");
73                         exit(1);
74                 case -1:
75                         dbg("fork of daemon failed");
76                         return -1;
77                 default:
78                         exit(0);
79                 }
80                 break;
81         case -1:
82                 dbg("fork of helper failed");
83                 return -1;
84         default:
85                 wait(NULL);
86         }
87         return 0;
88 }
89
90 static void run_udev(const char *subsystem)
91 {
92         pid_t pid;
93
94         pid = fork();
95         switch (pid) {
96         case 0:
97                 /* child */
98                 execl(UDEV_BIN, "udev", subsystem, NULL);
99                 dbg("exec of child failed");
100                 exit(1);
101                 break;
102         case -1:
103                 dbg("fork of child failed");
104                 break;
105         default:
106                 wait(NULL);
107         }
108 }
109
110 int main(int argc, char* argv[])
111 {
112         struct hotplug_msg msg;
113         char *action;
114         char *devpath;
115         char *subsystem;
116         char *seqnum;
117         unsigned long long seq;
118         int retval = 1;
119         int loop;
120         struct timespec tspec;
121         int sock = -1;
122         struct sockaddr_un saddr;
123         socklen_t addrlen;
124         int started_daemon = 0;
125
126         init_logging("udevsend");
127         dbg("version %s", UDEV_VERSION);
128
129         subsystem = get_subsystem(argv[1]);
130         if (subsystem == NULL) {
131                 dbg("no subsystem");
132                 goto exit;
133         }
134         dbg("subsystem = '%s'", subsystem);
135
136         devpath = get_devpath();
137         if (devpath == NULL) {
138                 dbg("no devpath");
139                 goto exit;
140         }
141         dbg("DEVPATH = '%s'", devpath);
142
143         action = get_action();
144         if (action == NULL) {
145                 dbg("no action");
146                 goto exit;
147         }
148         dbg("ACTION = '%s'", action);
149
150         seqnum = get_seqnum();
151         if (seqnum == NULL)
152                 seq = 0;
153         else
154                 seq = strtoull(seqnum, NULL, 10);
155         dbg("SEQNUM = '%llu'", seq);
156
157         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
158         if (sock == -1) {
159                 dbg("error getting socket");
160                 goto fallback;
161         }
162
163         set_cloexec_flag(sock, 1);
164
165         memset(&saddr, 0x00, sizeof(struct sockaddr_un));
166         saddr.sun_family = AF_LOCAL;
167         /* use abstract namespace for socket path */
168         strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
169         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
170
171         memset(&msg, 0x00, sizeof(struct hotplug_msg));
172         strcpy(msg.magic, UDEV_MAGIC);
173         msg.seqnum = seq;
174         strfieldcpy(msg.action, action);
175         strfieldcpy(msg.devpath, devpath);
176         strfieldcpy(msg.subsystem, subsystem);
177
178         /* If we can't send, try to start daemon and resend message */
179         loop = UDEVSEND_CONNECT_RETRY;
180         while (loop--) {
181                 retval = sendto(sock, &msg, sizeof(struct hotplug_msg), 0,
182                                 (struct sockaddr *)&saddr, addrlen);
183                 if (retval != -1) {
184                         retval = 0;
185                         goto exit;
186                 }
187
188                 if (errno != ECONNREFUSED) {
189                         dbg("error sending message");
190                         goto fallback;
191                 }
192
193                 if (!started_daemon) {
194                         info("starting udevd daemon");
195                         retval = start_daemon();
196                         if (retval) {
197                                 info("error starting daemon");
198                                 goto fallback;
199                         }
200                         dbg("daemon started");
201                         started_daemon = 1;
202                 } else {
203                         dbg("retry to connect %d", UDEVSEND_CONNECT_RETRY - loop);
204                         tspec.tv_sec = 0;
205                         tspec.tv_nsec = 100000000;  /* 100 millisec */
206                         nanosleep(&tspec, NULL);
207                 }
208         }
209
210 fallback:
211         info("unable to connect to event daemon, try to call udev directly");
212         run_udev(subsystem);
213
214 exit:
215         if (sock != -1)
216                 close(sock);
217
218         return retval;
219 }