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