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