chiark / gitweb /
8cabe9039bcf061a708260511dd9bd8e4b18c424
[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 #include <linux/stddef.h>
34
35 #include "udev.h"
36 #include "udev_version.h"
37 #include "udevd.h"
38 #include "logging.h"
39
40 /* global variables */
41 static int sock = -1;
42
43 #ifdef USE_LOG
44 void log_message (int priority, const char *format, ...)
45 {
46         va_list args;
47
48         if (priority > udev_log_priority)
49                 return;
50
51         va_start(args, format);
52         vsyslog(priority, format, args);
53         va_end(args);
54 }
55 #endif
56
57 int main(int argc, char *argv[], char *envp[])
58 {
59         static struct udevd_msg usend_msg;
60         int usend_msg_len;
61         int i;
62         struct sockaddr_un saddr;
63         socklen_t addrlen;
64         int bufpos = 0;
65         int retval = 0;
66         const char *subsystem = NULL;
67
68         logging_init("udevsend");
69 #ifdef USE_LOG
70         udev_init_config();
71 #endif
72         dbg("version %s", UDEV_VERSION);
73
74         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
75         if (sock < 0) {
76                 err("error getting socket: %s", strerror(errno));
77                 retval = 1;
78                 goto exit;
79         }
80
81         memset(&saddr, 0x00, sizeof(struct sockaddr_un));
82         saddr.sun_family = AF_LOCAL;
83         /* use abstract namespace for socket path */
84         strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
85         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
86
87         memset(&usend_msg, 0x00, sizeof(struct udevd_msg));
88         strcpy(usend_msg.magic, UDEV_MAGIC);
89         usend_msg.type = UDEVD_UEVENT_UDEVSEND;
90
91         /* copy all keys to send buffer */
92         for (i = 0; envp[i]; i++) {
93                 const char *key;
94                 int keylen;
95
96                 key = envp[i];
97                 keylen = strlen(key);
98
99                 /* prevent loops in the scripts we execute */
100                 if (strncmp(key, "UDEVD_EVENT=", 12) == 0) {
101                         err("event loop, already passed through the daemon, exit");
102                         retval = 2;
103                         goto exit;
104                 }
105
106                 if (bufpos + keylen >= UEVENT_BUFFER_SIZE-1) {
107                         err("environment buffer too small, probably not called by the kernel");
108                         continue;
109                 }
110
111                 /* remember the SUBSYSTEM */
112                 if (strncmp(key, "SUBSYSTEM=", 10) == 0)
113                         subsystem = &key[10];
114
115                 dbg("add '%s' to env[%i] buffer", key, i);
116                 strcpy(&usend_msg.envbuf[bufpos], key);
117                 bufpos += keylen + 1;
118         }
119
120         usend_msg_len = offsetof(struct udevd_msg, envbuf) + bufpos;
121         dbg("usend_msg_len=%i", usend_msg_len);
122
123         if (sendto(sock, &usend_msg, usend_msg_len, 0, (struct sockaddr *)&saddr, addrlen) < 0) {
124                 retval = 3;
125                 err("error sending message: %s", strerror(errno));
126         }
127
128 exit:
129         if (sock != -1)
130                 close(sock);
131
132         logging_close();
133         return retval;
134 }