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