chiark / gitweb /
98776ccefd84f635b0b209934eac8504d096fe25
[elogind.git] / udevcontrol.c
1 /*
2  * udevcontrol.c
3  *
4  * Userspace devfs
5  *
6  * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
7  *
8  *
9  *      This program is free software; you can redistribute it and/or modify it
10  *      under the terms of the GNU General Public License as published by the
11  *      Free Software Foundation version 2 of the License.
12  * 
13  *      This program is distributed in the hope that it will be useful, but
14  *      WITHOUT ANY WARRANTY; without even the implied warranty of
15  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *      General Public License for more details.
17  * 
18  *      You should have received a copy of the GNU General Public License along
19  *      with this program; if not, write to the Free Software Foundation, Inc.,
20  *      675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <sys/wait.h>
27 #include <sys/un.h>
28 #include <time.h>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stddef.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <linux/stddef.h>
36
37 #include "udev.h"
38 #include "udev_version.h"
39 #include "udevd.h"
40 #include "udev_utils.h"
41 #include "logging.h"
42
43 /* global variables */
44 static int sock = -1;
45 static int log = 0;
46
47 #ifdef USE_LOG
48 void log_message (int priority, const char *format, ...)
49 {
50         va_list args;
51
52         if (priority > log)
53                 return;
54
55         va_start(args, format);
56         vsyslog(priority, format, args);
57         va_end(args);
58 }
59 #endif
60
61
62 int main(int argc, char *argv[], char *envp[])
63 {
64         static struct udevd_msg usend_msg;
65         struct sockaddr_un saddr;
66         socklen_t addrlen;
67         const char *env;
68         int retval = 1;
69
70         env = getenv("UDEV_LOG");
71         if (env)
72                 log = log_priority(env);
73
74         logging_init("udevcontrol");
75         dbg("version %s", UDEV_VERSION);
76
77         if (argc != 2) {
78                 info("usage: udevcontrol <cmd>");
79                 goto exit;
80         }
81
82         memset(&usend_msg, 0x00, sizeof(struct udevd_msg));
83         strcpy(usend_msg.magic, UDEV_MAGIC);
84
85         if (!strcmp(argv[1], "stop_exec_queue"))
86                 usend_msg.type = UDEVD_STOP_EXEC_QUEUE;
87         else if (!strcmp(argv[1], "start_exec_queue"))
88                 usend_msg.type = UDEVD_START_EXEC_QUEUE;
89         else if (!strncmp(argv[1], "log_priority=", strlen("log_priority="))) {
90                 int *level = (int *) usend_msg.envbuf;
91                 char *prio = &argv[1][strlen("log_priority=")];
92
93                 usend_msg.type = UDEVD_SET_LOG_LEVEL;
94                 *level = log_priority(prio);
95                 dbg("send log_priority=%i", *level);
96         } else {
97                 err("unknown command\n");
98                 goto exit;
99         }
100
101         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
102         if (sock == -1) {
103                 info("error getting socket");
104                 goto exit;
105         }
106
107         memset(&saddr, 0x00, sizeof(struct sockaddr_un));
108         saddr.sun_family = AF_LOCAL;
109         /* use abstract namespace for socket path */
110         strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
111         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
112
113
114         retval = sendto(sock, &usend_msg, sizeof(usend_msg), 0, (struct sockaddr *)&saddr, addrlen);
115         if (retval == -1) {
116                 info("error sending message (%s)", strerror(errno));
117                 retval = 1;
118         } else {
119                 dbg("sent message '%x' (%u bytes sent)", usend_msg.type, retval);
120                 retval = 0;
121         }
122
123         close(sock);
124
125 exit:
126         logging_close();
127
128         return retval;
129 }