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