chiark / gitweb /
6af9df77040a386c39a93ddf1245235efd517eda
[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/ipc.h>
27 #include <sys/msg.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <time.h>
34 #include <wait.h>
35 #include <sys/socket.h>
36 #include <sys/un.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 = -EINVAL;
124         int size;
125         int loop;
126         struct timespec tspec;
127         int sock;
128         struct sockaddr_un saddr;
129
130         init_logging("udevsend");
131
132         subsystem = argv[1];
133         if (subsystem == NULL) {
134                 dbg("no subsystem");
135                 goto exit;
136         }
137
138         devpath = get_devpath();
139         if (devpath == NULL) {
140                 dbg("no devpath");
141                 goto exit;
142         }
143
144         action = get_action();
145         if (action == NULL) {
146                 dbg("no action");
147                 goto exit;
148         }
149
150         seqnum = get_seqnum();
151         if (seqnum == NULL) {
152                 dbg("no seqnum");
153                 goto exit;
154         }
155         seq = atoi(seqnum);
156
157         sock = socket(AF_LOCAL, SOCK_STREAM, 0);
158         if (sock == -1) {
159                 dbg("error getting socket");
160                 goto exit;
161         }
162
163         memset(&saddr, 0x00, sizeof(saddr));
164         saddr.sun_family = AF_LOCAL;
165         strcpy(saddr.sun_path, UDEVD_SOCK);
166
167         /* try to connect, if it fails start daemon */
168         retval = connect(sock, &saddr, sizeof(saddr));
169         if (retval != -1) {
170                 goto send;
171         } else {
172                 dbg("connect failed, try starting daemon...");
173                 retval = start_daemon();
174                 if (retval == 0) {
175                         dbg("daemon started");
176                 } else {
177                         dbg("error starting daemon");
178                         goto exit;
179                 }
180         }
181
182         /* try to connect while daemon to starts */
183         tspec.tv_sec = 0;
184         tspec.tv_nsec = 100000000;  /* 100 millisec */
185         loop = UDEVSEND_CONNECT_RETRY;
186         while (loop--) {
187                 retval = connect(sock, &saddr, sizeof(saddr));
188                 if (retval != -1)
189                         goto send;
190                 else
191                         dbg("retry to connect %d",
192                             UDEVSEND_CONNECT_RETRY - loop);
193                 nanosleep(&tspec, NULL);
194         }
195         dbg("error connecting to daemon, start daemon failed");
196         goto exit;
197
198 send:
199         size = build_hotplugmsg(&message, action, devpath, subsystem, seq);
200         retval = send(sock, &message, size, 0);
201         if (retval == -1) {
202                 dbg("error sending message");
203                 close (sock);
204                 goto exit;
205         }
206         close (sock);
207         return 0;
208
209 exit:
210         return 1;
211 }