chiark / gitweb /
[PATCH] convert udevsend/udevd to DGRAM and single-threaded
[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 <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 unsigned char logname[42];
44
45 static inline char *get_action(void)
46 {
47         char *action;
48
49         action = getenv("ACTION");
50         return action;
51 }
52
53 static inline char *get_devpath(void)
54 {
55         char *devpath;
56
57         devpath = getenv("DEVPATH");
58         return devpath;
59 }
60
61 static inline char *get_seqnum(void)
62 {
63         char *seqnum;
64
65         seqnum = getenv("SEQNUM");
66         return seqnum;
67 }
68
69 static int build_hotplugmsg(struct hotplug_msg *msg, char *action,
70                             char *devpath, char *subsystem, int seqnum)
71 {
72         memset(msg, 0x00, sizeof(*msg));
73         strfieldcpy(msg->magic, UDEV_MAGIC);
74         msg->seqnum = seqnum;
75         strncpy(msg->action, action, 8);
76         strncpy(msg->devpath, devpath, 128);
77         strncpy(msg->subsystem, subsystem, 16);
78         return sizeof(struct hotplug_msg);
79 }
80
81 static int start_daemon(void)
82 {
83         pid_t pid;
84         pid_t child_pid;
85
86         pid = fork();
87         switch (pid) {
88         case 0:
89                 /* helper child */
90                 child_pid = fork();
91                 switch (child_pid) {
92                 case 0:
93                         /* daemon */
94                         setsid();
95                         chdir("/");
96                         execl(UDEVD_BIN, "udevd", NULL);
97                         dbg("exec of daemon failed");
98                         exit(1);
99                 case -1:
100                         dbg("fork of daemon failed");
101                         return -1;
102                 default:
103                         exit(0);
104                 }
105                 break;
106         case -1:
107                 dbg("fork of helper failed");
108                 return -1;
109         default:
110                 wait(NULL);
111         }
112         return 0;
113 }
114
115 int main(int argc, char* argv[])
116 {
117         struct hotplug_msg message;
118         char *action;
119         char *devpath;
120         char *subsystem;
121         char *seqnum;
122         int seq;
123         int retval = 1;
124         int size;
125         int loop;
126         struct timespec tspec;
127         int sock;
128         struct sockaddr_un saddr;
129         socklen_t addrlen;
130         int started_daemon = 0;
131
132 #ifdef DEBUG
133         init_logging("udevsend");
134 #endif
135
136         subsystem = argv[1];
137         if (subsystem == NULL) {
138                 dbg("no subsystem");
139                 goto exit;
140         }
141
142         devpath = get_devpath();
143         if (devpath == NULL) {
144                 dbg("no devpath");
145                 goto exit;
146         }
147
148         action = get_action();
149         if (action == NULL) {
150                 dbg("no action");
151                 goto exit;
152         }
153
154         seqnum = get_seqnum();
155         if (seqnum == NULL)
156                 seq = -1;
157         else
158                 seq = atoi(seqnum);
159
160         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
161         if (sock == -1) {
162                 dbg("error getting socket");
163                 goto exit;
164         }
165
166         memset(&saddr, 0x00, sizeof(saddr));
167         saddr.sun_family = AF_LOCAL;
168         /* use abstract namespace for socket path */
169         strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
170         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
171
172         size = build_hotplugmsg(&message, action, devpath, subsystem, seq);
173         
174         /* If we can't send, try to start daemon and resend message */
175         loop = UDEVSEND_CONNECT_RETRY;
176         while (loop--) {
177                 retval = sendto(sock, &message, size, 0, (struct sockaddr*)&saddr, addrlen);
178                 if (retval != -1) {
179                         retval = 0;
180                         goto close_and_exit;
181                 }
182                 
183                 if (errno != ECONNREFUSED) {
184                         dbg("error sending message");
185                         goto close_and_exit;
186                 }
187                 
188                 if (!started_daemon) {
189                         dbg("connect failed, try starting daemon...");
190                         retval = start_daemon();
191                         if (retval) {
192                                 dbg("error starting daemon");
193                                 goto exit;
194                         }
195                         
196                         dbg("daemon started");
197                         started_daemon = 1;
198                 } else {
199                         dbg("retry to connect %d", UDEVSEND_CONNECT_RETRY - loop);
200                         tspec.tv_sec = 0;
201                         tspec.tv_nsec = 100000000;  /* 100 millisec */
202                         nanosleep(&tspec, NULL);
203                 }
204         }
205         
206 close_and_exit:
207         close(sock);
208 exit:
209         return retval;
210 }