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