chiark / gitweb /
fix offsetof() build issue with recent glibc
[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 <time.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stddef.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/wait.h>
31 #include <sys/un.h>
32
33 #include "udev.h"
34 #include "udevd.h"
35
36 /* global variables */
37 static int sock = -1;
38 static int udev_log = 0;
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)
46                 return;
47
48         va_start(args, format);
49         vsyslog(priority, format, args);
50         va_end(args);
51 }
52 #endif
53
54
55 int main(int argc, char *argv[], char *envp[])
56 {
57         static struct udevd_msg usend_msg;
58         struct sockaddr_un saddr;
59         socklen_t addrlen;
60         const char *env;
61         const char *val;
62         int *intval;
63         int i;
64         int retval = 1;
65
66         env = getenv("UDEV_LOG");
67         if (env)
68                 udev_log = log_priority(env);
69
70         logging_init("udevcontrol");
71         dbg("version %s", UDEV_VERSION);
72
73         if (argc < 2) {
74                 fprintf(stderr, "missing command\n\n");
75                 goto exit;
76         }
77
78         memset(&usend_msg, 0x00, sizeof(struct udevd_msg));
79         strcpy(usend_msg.magic, UDEV_MAGIC);
80
81         for (i = 1 ; i < argc; i++) {
82                 char *arg = argv[i];
83
84                 if (!strcmp(arg, "stop_exec_queue"))
85                         usend_msg.type = UDEVD_STOP_EXEC_QUEUE;
86                 else if (!strcmp(arg, "start_exec_queue"))
87                         usend_msg.type = UDEVD_START_EXEC_QUEUE;
88                 else if (!strcmp(arg, "reload_rules"))
89                         usend_msg.type = UDEVD_RELOAD_RULES;
90                 else if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
91                         intval = (int *) usend_msg.envbuf;
92                         val = &arg[strlen("log_priority=")];
93                         usend_msg.type = UDEVD_SET_LOG_LEVEL;
94                         *intval = log_priority(val);
95                         info("send log_priority=%i", *intval);
96                 } else if (!strncmp(arg, "max_childs=", strlen("max_childs="))) {
97                         intval = (int *) usend_msg.envbuf;
98                         val = &arg[strlen("max_childs=")];
99                         usend_msg.type = UDEVD_SET_MAX_CHILDS;
100                         *intval = atoi(val);
101                         info("send max_childs=%i", *intval);
102                 } else if (strcmp(arg, "help") == 0  || strcmp(arg, "--help") == 0  || strcmp(arg, "-h") == 0) {
103                         printf("Usage: udevcontrol COMMAND\n"
104                                 "  log_priority=<level> set the udev log level for the daemon\n"
105                                 "  stop_exec_queue      keep udevd from executing events, queue only\n"
106                                 "  start_exec_queue     execute events, flush queue\n"
107                                 "  reload_rules         reloads the rules files\n"
108                                 "  max_childs=<N>       maximum number of childs running at the same time\n"
109                                 "  --help               print this help text\n\n");
110                         exit(0);
111                 } else {
112                         fprintf(stderr, "unknown option\n\n");
113                         exit(1);
114                 }
115         }
116
117         if (getuid() != 0) {
118                 fprintf(stderr, "need to be root, exit\n\n");
119                 exit(1);
120         }
121
122         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
123         if (sock == -1) {
124                 err("error getting socket: %s", strerror(errno));
125                 goto exit;
126         }
127
128         memset(&saddr, 0x00, sizeof(struct sockaddr_un));
129         saddr.sun_family = AF_LOCAL;
130         /* use abstract namespace for socket path */
131         strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
132         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
133
134
135         retval = sendto(sock, &usend_msg, sizeof(usend_msg), 0, (struct sockaddr *)&saddr, addrlen);
136         if (retval == -1) {
137                 err("error sending message: %s", strerror(errno));
138                 retval = 1;
139         } else {
140                 dbg("sent message type=0x%02x, %u bytes sent", usend_msg.type, retval);
141                 retval = 0;
142         }
143
144         close(sock);
145
146 exit:
147         logging_close();
148
149         return retval;
150 }