chiark / gitweb /
[PATCH] add ability to install udevtest to Makefile
[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 #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         strncpy(msg->action, action, 8);
86         strncpy(msg->devpath, devpath, 128);
87         strncpy(msg->subsystem, subsystem, 16);
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
146         subsystem = argv[1];
147         if (subsystem == NULL) {
148                 dbg("no subsystem");
149                 goto exit;
150         }
151
152         devpath = get_devpath();
153         if (devpath == NULL) {
154                 dbg("no devpath");
155                 goto exit;
156         }
157
158         action = get_action();
159         if (action == NULL) {
160                 dbg("no action");
161                 goto exit;
162         }
163
164         seqnum = get_seqnum();
165         if (seqnum == NULL)
166                 seq = -1;
167         else
168                 seq = atoi(seqnum);
169
170         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
171         if (sock == -1) {
172                 dbg("error getting socket");
173                 goto exit;
174         }
175
176         memset(&saddr, 0x00, sizeof(saddr));
177         saddr.sun_family = AF_LOCAL;
178         /* use abstract namespace for socket path */
179         strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
180         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
181
182         size = build_hotplugmsg(&msg, action, devpath, subsystem, seq);
183
184         /* If we can't send, try to start daemon and resend message */
185         loop = UDEVSEND_CONNECT_RETRY;
186         while (loop--) {
187                 retval = sendto(sock, &msg, size, 0, (struct sockaddr *)&saddr, addrlen);
188                 if (retval != -1) {
189                         retval = 0;
190                         goto close_and_exit;
191                 }
192                 
193                 if (errno != ECONNREFUSED) {
194                         dbg("error sending message");
195                         goto close_and_exit;
196                 }
197                 
198                 if (!started_daemon) {
199                         dbg("connect failed, try starting daemon...");
200                         retval = start_daemon();
201                         if (retval) {
202                                 dbg("error starting daemon");
203                                 goto exit;
204                         }
205                         
206                         dbg("daemon started");
207                         started_daemon = 1;
208                 } else {
209                         dbg("retry to connect %d", UDEVSEND_CONNECT_RETRY - loop);
210                         tspec.tv_sec = 0;
211                         tspec.tv_nsec = 100000000;  /* 100 millisec */
212                         nanosleep(&tspec, NULL);
213                 }
214         }
215         
216 close_and_exit:
217         close(sock);
218 exit:
219         return retval;
220 }