chiark / gitweb /
092 release
[elogind.git] / udevsend.c
1 /*
2  * udevsend.c
3  *
4  * Copyright (C) 2004 Ling, Xiaofeng <xiaofeng.ling@intel.com>
5  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
6  *
7  *      This program is free software; you can redistribute it and/or modify it
8  *      under the terms of the GNU General Public License as published by the
9  *      Free Software Foundation version 2 of the License.
10  * 
11  *      This program is distributed in the hope that it will be useful, but
12  *      WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *      General Public License for more details.
15  * 
16  *      You should have received a copy of the GNU General Public License along
17  *      with this program; if not, write to the Free Software Foundation, Inc.,
18  *      675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/wait.h>
32 #include <sys/un.h>
33
34 #include "udev.h"
35 #include "udevd.h"
36
37 /* global variables */
38 static int sock = -1;
39
40 #ifdef USE_LOG
41 void log_message (int priority, const char *format, ...)
42 {
43         va_list args;
44
45         if (priority > udev_log_priority)
46                 return;
47
48         va_start(args, format);
49         vsyslog(priority, format, args);
50         va_end(args);
51 }
52 #endif
53
54 int main(int argc, char *argv[], char *envp[])
55 {
56         static struct udevd_msg usend_msg;
57         int usend_msg_len;
58         int i;
59         struct sockaddr_un saddr;
60         socklen_t addrlen;
61         int bufpos = 0;
62         int retval = 0;
63         const char *subsystem = NULL;
64
65         logging_init("udevsend");
66 #ifdef USE_LOG
67         udev_config_init();
68 #endif
69         dbg("version %s", UDEV_VERSION);
70
71         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
72         if (sock < 0) {
73                 err("error getting socket: %s", strerror(errno));
74                 retval = 1;
75                 goto exit;
76         }
77
78         memset(&saddr, 0x00, sizeof(struct sockaddr_un));
79         saddr.sun_family = AF_LOCAL;
80         /* use abstract namespace for socket path */
81         strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
82         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
83
84         memset(&usend_msg, 0x00, sizeof(struct udevd_msg));
85         strcpy(usend_msg.magic, UDEV_MAGIC);
86         usend_msg.type = UDEVD_UEVENT_UDEVSEND;
87
88         /* copy all keys to send buffer */
89         for (i = 0; envp[i]; i++) {
90                 const char *key;
91                 int keylen;
92
93                 key = envp[i];
94                 keylen = strlen(key);
95
96                 /* ignore events which are already sent on the netlink socket */
97                 if (strncmp(key, "SEQNUM=", 7) == 0) {
98                         dbg("ignoring event with SEQNUM set");
99                         retval = 0;
100                         goto exit;
101                 }
102
103                 /* prevent loops in the scripts we execute */
104                 if (strncmp(key, "UDEVD_EVENT=", 12) == 0) {
105                         err("event loop, already passed through the daemon, exit");
106                         retval = 2;
107                         goto exit;
108                 }
109
110                 if (bufpos + keylen >= UEVENT_BUFFER_SIZE-1) {
111                         err("environment buffer too small, probably not called by the kernel");
112                         continue;
113                 }
114
115                 /* remember the SUBSYSTEM */
116                 if (strncmp(key, "SUBSYSTEM=", 10) == 0)
117                         subsystem = &key[10];
118
119                 dbg("add '%s' to env[%i] buffer", key, i);
120                 strcpy(&usend_msg.envbuf[bufpos], key);
121                 bufpos += keylen + 1;
122         }
123
124         usend_msg_len = offsetof(struct udevd_msg, envbuf) + bufpos;
125         dbg("usend_msg_len=%i", usend_msg_len);
126
127         if (sendto(sock, &usend_msg, usend_msg_len, 0, (struct sockaddr *)&saddr, addrlen) < 0) {
128                 retval = 3;
129                 err("error sending message: %s", strerror(errno));
130         }
131
132 exit:
133         if (sock != -1)
134                 close(sock);
135
136         logging_close();
137         return retval;
138 }