chiark / gitweb /
[PATCH] added URL to spec file.
[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
36 #include "udev.h"
37 #include "udevd.h"
38 #include "logging.h"
39
40 static inline char *get_action(void)
41 {
42         char *action;
43
44         action = getenv("ACTION");
45         return action;
46 }
47
48 static inline char *get_devpath(void)
49 {
50         char *devpath;
51
52         devpath = getenv("DEVPATH");
53         return devpath;
54 }
55
56 static inline char *get_seqnum(void)
57 {
58         char *seqnum;
59
60         seqnum = getenv("SEQNUM");
61         return seqnum;
62 }
63
64 static int build_hotplugmsg(struct hotplug_msg *msg, char *action,
65                             char *devpath, char *subsystem, int seqnum)
66 {
67         memset(msg, 0x00, sizeof(msg));
68         msg->mtype = HOTPLUGMSGTYPE;
69         msg->seqnum = seqnum;
70         strncpy(msg->action, action, 8);
71         strncpy(msg->devpath, devpath, 128);
72         strncpy(msg->subsystem, subsystem, 16);
73         return sizeof(struct hotplug_msg);
74 }
75
76 static int start_daemon(void)
77 {
78         pid_t pid;
79         pid_t child_pid;
80
81         pid = fork();
82         switch (pid) {
83         case 0:
84                 /* helper child */
85                 child_pid = fork();
86                 switch (child_pid) {
87                 case 0:
88                         /* daemon */
89                         setsid();
90                         execl(UDEVD_EXEC, "udevd", NULL);
91                         dbg("exec of daemon failed");
92                         exit(1);
93                 case -1:
94                         dbg("fork of daemon failed");
95                         return -1;
96                 default:
97                         exit(0);
98                 }
99                 break;
100         case -1:
101                 dbg("fork of helper failed");
102                 return -1;
103         default:
104                 wait(NULL);
105         }
106         return 0;
107 }
108
109
110 int main(int argc, char* argv[])
111 {
112         int msgid;
113         key_t key;
114         struct msqid_ds msg_queue;
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
126         subsystem = argv[1];
127         if (subsystem == NULL) {
128                 dbg("no subsystem");
129                 goto exit;
130         }
131
132         devpath = get_devpath();
133         if (devpath == NULL) {
134                 dbg("no devpath");
135                 goto exit;
136         }
137
138         action = get_action();
139         if (action == NULL) {
140                 dbg("no action");
141                 goto exit;
142         }
143
144         seqnum = get_seqnum();
145         if (seqnum == NULL) {
146                 dbg("no seqnum");
147                 goto exit;
148         }
149         seq = atoi(seqnum);
150
151         /* create ipc message queue or get id of our existing one */
152         key = ftok(UDEVD_EXEC, IPC_KEY_ID);
153         size =  build_hotplugmsg(&message, action, devpath, subsystem, seq);
154         msgid = msgget(key, IPC_CREAT);
155         if (msgid == -1) {
156                 dbg("error open ipc queue");
157                 goto exit;
158         }
159
160         /* send ipc message to the daemon */
161         retval = msgsnd(msgid, &message, size, 0);
162         if (retval == -1) {
163                 dbg("error sending ipc message");
164                 goto exit;
165         }
166
167         /* get state of ipc queue */
168         tspec.tv_sec = 0;
169         tspec.tv_nsec = 10000000;  /* 10 millisec */
170         loop = 30;
171         while (loop--) {
172                 retval = msgctl(msgid, IPC_STAT, &msg_queue);
173                 if (retval == -1) {
174                         dbg("error getting info on ipc queue");
175                         goto exit;
176                 }
177                 if (msg_queue.msg_qnum == 0)
178                         goto exit;
179                 nanosleep(&tspec, NULL);
180         }
181
182         info("message is still in the ipc queue, starting daemon...");
183         retval = start_daemon();
184
185 exit:
186         return retval;
187 }