chiark / gitweb /
trivial fixes for *_id programs
[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         const char *val;
69         int *intval;
70         int retval = 1;
71
72         env = getenv("UDEV_LOG");
73         if (env)
74                 log = log_priority(env);
75
76         logging_init("udevcontrol");
77         dbg("version %s", UDEV_VERSION);
78
79         if (argc != 2) {
80                 err("error finding comand");
81                 goto exit;
82         }
83
84         memset(&usend_msg, 0x00, sizeof(struct udevd_msg));
85         strcpy(usend_msg.magic, UDEV_MAGIC);
86
87         if (!strcmp(argv[1], "stop_exec_queue"))
88                 usend_msg.type = UDEVD_STOP_EXEC_QUEUE;
89         else if (!strcmp(argv[1], "start_exec_queue"))
90                 usend_msg.type = UDEVD_START_EXEC_QUEUE;
91         else if (!strncmp(argv[1], "log_priority=", strlen("log_priority="))) {
92                 intval = (int *) usend_msg.envbuf;
93                 val = &argv[1][strlen("log_priority=")];
94                 usend_msg.type = UDEVD_SET_LOG_LEVEL;
95                 *intval = log_priority(val);
96                 info("send log_priority=%i", *intval);
97         } else if (!strncmp(argv[1], "max_childs=", strlen("max_childs="))) {
98                 intval = (int *) usend_msg.envbuf;
99                 val = &argv[1][strlen("max_childs=")];
100                 usend_msg.type = UDEVD_SET_MAX_CHILDS;
101                 *intval = atoi(val);
102                 info("send max_childs=%i", *intval);
103         } else {
104                 err("error parsing command\n");
105                 goto exit;
106         }
107
108         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
109         if (sock == -1) {
110                 err("error getting socket");
111                 goto exit;
112         }
113
114         memset(&saddr, 0x00, sizeof(struct sockaddr_un));
115         saddr.sun_family = AF_LOCAL;
116         /* use abstract namespace for socket path */
117         strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
118         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
119
120
121         retval = sendto(sock, &usend_msg, sizeof(usend_msg), 0, (struct sockaddr *)&saddr, addrlen);
122         if (retval == -1) {
123                 info("error sending message (%s)", strerror(errno));
124                 retval = 1;
125         } else {
126                 dbg("sent message type=0x%02x, %u bytes sent", usend_msg.type, retval);
127                 retval = 0;
128         }
129
130         close(sock);
131
132 exit:
133         logging_close();
134
135         return retval;
136 }