typo fixes
[oweals/busybox.git] / util-linux / acpid.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * simple ACPI events listener
4  *
5  * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
6  *
7  * Licensed under GPLv2, see file LICENSE in this source tree.
8  */
9
10 //usage:#define acpid_trivial_usage
11 //usage:       "[-d] [-c CONFDIR] [-l LOGFILE] [-a ACTIONFILE] [-M MAPFILE] [-e PROC_EVENT_FILE] [-p PIDFILE]"
12 //usage:#define acpid_full_usage "\n\n"
13 //usage:       "Listen to ACPI events and spawn specific helpers on event arrival\n"
14 //usage:     "\n        -c DIR  Config directory [/etc/acpi]"
15 //usage:     "\n        -d      Don't daemonize, (implies -f)"
16 //usage:     "\n        -e FILE /proc event file [/proc/acpi/event]"
17 //usage:     "\n        -f      Run in foreground"
18 //usage:     "\n        -l FILE Log file [/var/log/acpid.log]"
19 //usage:     "\n        -p FILE Pid file [/var/run/acpid.pid]"
20 //usage:     "\n        -a FILE Action file [/etc/acpid.conf]"
21 //usage:     "\n        -M FILE Map file [/etc/acpi.map]"
22 //usage:        IF_FEATURE_ACPID_COMPAT(
23 //usage:     "\n\nAccept and ignore compatibility options -g -m -s -S -v"
24 //usage:        )
25 //usage:
26 //usage:#define acpid_example_usage
27 //usage:       "Without -e option, acpid uses all /dev/input/event* files\n"
28 //usage:       "# acpid\n"
29 //usage:       "# acpid -l /var/log/my-acpi-log\n"
30 //usage:       "# acpid -e /proc/acpi/event\n"
31
32 #include "libbb.h"
33 #include <syslog.h>
34 #include <linux/input.h>
35
36 enum {
37         OPT_c = (1 << 0),
38         OPT_d = (1 << 1),
39         OPT_e = (1 << 2),
40         OPT_f = (1 << 3),
41         OPT_l = (1 << 4),
42         OPT_a = (1 << 5),
43         OPT_M = (1 << 6),
44         OPT_p = (1 << 7) * ENABLE_FEATURE_PIDFILE,
45 };
46
47 struct acpi_event {
48         const char *s_type;
49         uint16_t n_type;
50         const char *s_code;
51         uint16_t n_code;
52         uint32_t value;
53         const char *desc;
54 };
55
56 static const struct acpi_event f_evt_tab[] = {
57         { "EV_KEY", 0x01, "KEY_POWER", 116, 1, "button/power PWRF 00000080" },
58         { "EV_KEY", 0x01, "KEY_POWER", 116, 1, "button/power PWRB 00000080" },
59 };
60
61 struct acpi_action {
62         const char *key;
63         const char *action;
64 };
65
66 static const struct acpi_action f_act_tab[] = {
67         { "PWRF", "PWRF/00000080" },
68         { "LID0", "LID/00000080" },
69 };
70
71 struct globals {
72         struct acpi_action *act_tab;
73         int n_act;
74         struct acpi_event *evt_tab;
75         int n_evt;
76 } FIX_ALIASING;
77 #define G (*ptr_to_globals)
78 #define act_tab         (G.act_tab)
79 #define n_act           (G.n_act  )
80 #define evt_tab         (G.evt_tab)
81 #define n_evt           (G.n_evt  )
82 #define INIT_G() do { \
83         SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
84 } while (0)
85
86 /*
87  * acpid listens to ACPI events coming either in textual form
88  * from /proc/acpi/event (though it is marked deprecated,
89  * it is still widely used and _is_ a standard) or in binary form
90  * from specified evdevs (just use /dev/input/event*).
91  * It parses the event to retrieve ACTION and a possible PARAMETER.
92  * It then spawns /etc/acpi/<ACTION>[/<PARAMETER>] either via run-parts
93  * (if the resulting path is a directory) or directly.
94  * If the resulting path does not exist it logs it via perror
95  * and continues listening.
96  */
97
98 static void process_event(const char *event)
99 {
100         struct stat st;
101         char *handler = xasprintf("./%s", event);
102         const char *args[] = { "run-parts", handler, NULL };
103
104         // debug info
105         if (option_mask32 & OPT_d) {
106                 bb_error_msg("%s", event);
107         }
108
109         // spawn handler
110         // N.B. run-parts would require scripts to have #!/bin/sh
111         // handler is directory? -> use run-parts
112         // handler is file? -> run it directly
113         if (0 == stat(event, &st))
114                 spawn((char **)args + (0==(st.st_mode & S_IFDIR)));
115         else
116                 bb_simple_perror_msg(event);
117
118         free(handler);
119 }
120
121 static const char *find_action(struct input_event *ev, const char *buf)
122 {
123         const char *action = NULL;
124         int i;
125
126         // map event
127         for (i = 0; i < n_evt; i++) {
128                 if (ev) {
129                         if (ev->type == evt_tab[i].n_type && ev->code == evt_tab[i].n_code && ev->value == evt_tab[i].value) {
130                                 action = evt_tab[i].desc;
131                                 break;
132                         }
133                 }
134
135                 if (buf) {
136                         if (strncmp(buf, evt_tab[i].desc, strlen(buf)) == 0) {
137                                 action = evt_tab[i].desc;
138                                 break;
139                         }
140                 }
141         }
142
143         // get action
144         if (action) {
145                 for (i = 0; i < n_act; i++) {
146                         if (strstr(action, act_tab[i].key)) {
147                                 action = act_tab[i].action;
148                                 break;
149                         }
150                 }
151         }
152
153         return action;
154 }
155
156 static void parse_conf_file(const char *filename)
157 {
158         parser_t *parser;
159         char *tokens[2];
160
161         parser = config_open2(filename, fopen_for_read);
162
163         if (parser) {
164                 while (config_read(parser, tokens, 2, 2, "# \t", PARSE_NORMAL)) {
165                         act_tab = xrealloc_vector(act_tab, 1, n_act);
166                         act_tab[n_act].key = xstrdup(tokens[0]);
167                         act_tab[n_act].action = xstrdup(tokens[1]);
168                         n_act++;
169                 }
170                 config_close(parser);
171         } else {
172                 act_tab = (void*)f_act_tab;
173                 n_act = ARRAY_SIZE(f_act_tab);
174         }
175 }
176
177 static void parse_map_file(const char *filename)
178 {
179         parser_t *parser;
180         char *tokens[6];
181
182         parser = config_open2(filename, fopen_for_read);
183
184         if (parser) {
185                 while (config_read(parser, tokens, 6, 6, "# \t", PARSE_NORMAL)) {
186                         evt_tab = xrealloc_vector(evt_tab, 1, n_evt);
187                         evt_tab[n_evt].s_type = xstrdup(tokens[0]);
188                         evt_tab[n_evt].n_type = xstrtou(tokens[1], 16);
189                         evt_tab[n_evt].s_code = xstrdup(tokens[2]);
190                         evt_tab[n_evt].n_code = xatou16(tokens[3]);
191                         evt_tab[n_evt].value = xatoi_positive(tokens[4]);
192                         evt_tab[n_evt].desc = xstrdup(tokens[5]);
193                         n_evt++;
194                 }
195                 config_close(parser);
196         } else {
197                 evt_tab = (void*)f_evt_tab;
198                 n_evt = ARRAY_SIZE(f_evt_tab);
199         }
200 }
201
202 /*
203  * acpid [-c conf_dir] [-r conf_file ] [-a map_file ] [-l log_file] [-e proc_event_file]
204  */
205
206 int acpid_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
207 int acpid_main(int argc UNUSED_PARAM, char **argv)
208 {
209         struct input_event ev;
210         int nfd;
211         int opts;
212         struct pollfd *pfd;
213         const char *opt_dir = "/etc/acpi";
214         const char *opt_input = "/dev/input/event";
215         const char *opt_logfile = "/var/log/acpid.log";
216         const char *opt_action = "/etc/acpid.conf";
217         const char *opt_map = "/etc/acpi.map";
218 #if ENABLE_FEATURE_PIDFILE
219         const char *opt_pidfile = "/var/run/acpid.pid";
220 #endif
221
222         INIT_G();
223
224         opt_complementary = "df:e--e";
225         opts = getopt32(argv, "c:de:fl:a:M:" IF_FEATURE_PIDFILE("p:") IF_FEATURE_ACPID_COMPAT("g:m:s:S:v"),
226                 &opt_dir, &opt_input, &opt_logfile, &opt_action, &opt_map
227                 IF_FEATURE_PIDFILE(, &opt_pidfile)
228                 IF_FEATURE_ACPID_COMPAT(, NULL, NULL, NULL, NULL)
229         );
230
231         if (!(opts & OPT_f)) {
232                 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
233         }
234
235         if (!(opts & OPT_d)) {
236                 openlog(applet_name, LOG_PID, LOG_DAEMON);
237                 logmode = LOGMODE_SYSLOG | LOGMODE_STDIO;
238         } else {
239                 xmove_fd(xopen(opt_logfile, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
240         }
241
242         parse_conf_file(opt_action);
243         parse_map_file(opt_map);
244
245         xchdir(opt_dir);
246
247         bb_signals((1 << SIGCHLD), SIG_IGN);
248         bb_signals(BB_FATAL_SIGS, record_signo);
249
250         pfd = NULL;
251         nfd = 0;
252         while (1) {
253                 int fd;
254                 char *dev_event;
255
256                 dev_event = xasprintf((option_mask32 & OPT_e) ? "%s" : "%s%u", opt_input, nfd);
257                 fd = open(dev_event, O_RDONLY | O_NONBLOCK);
258                 if (fd < 0) {
259                         if (nfd == 0)
260                                 bb_simple_perror_msg_and_die(dev_event);
261                         break;
262                 }
263                 pfd = xrealloc_vector(pfd, 1, nfd);
264                 pfd[nfd].fd = fd;
265                 pfd[nfd].events = POLLIN;
266                 nfd++;
267         }
268
269         write_pidfile(opt_pidfile);
270
271         while (poll(pfd, nfd, -1) > 0) {
272                 int i;
273                 for (i = 0; i < nfd; i++) {
274                         const char *event = NULL;
275
276                         memset(&ev, 0, sizeof(ev));
277
278                         if (!(pfd[i].revents & POLLIN))
279                                 continue;
280
281                         if (option_mask32 & OPT_e) {
282                                 char *buf;
283                                 int len;
284
285                                 buf = xmalloc_reads(pfd[i].fd, NULL);
286                                 /* buf = "button/power PWRB 00000080 00000000" */
287                                 len = strlen(buf) - 9;
288                                 if (len >= 0)
289                                         buf[len] = '\0';
290                                 event = find_action(NULL, buf);
291                         } else {
292                                 if (sizeof(ev) != full_read(pfd[i].fd, &ev, sizeof(ev)))
293                                         continue;
294
295                                 if (ev.value != 1 && ev.value != 0)
296                                         continue;
297
298                                 event = find_action(&ev, NULL);
299                         }
300                         if (!event)
301                                 continue;
302                         // spawn event handler
303                         process_event(event);
304                 }
305         }
306
307         if (ENABLE_FEATURE_CLEAN_UP) {
308                 while (nfd--) {
309                         if (pfd[nfd].fd) {
310                                 close(pfd[nfd].fd);
311                         }
312                 }
313                 free(pfd);
314         }
315         remove_pidfile(opt_pidfile);
316
317         return EXIT_SUCCESS;
318 }