1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59#include "libbb.h"
60#include <syslog.h>
61#include <linux/input.h>
62
63#ifndef EV_SW
64# define EV_SW 0x05
65#endif
66#ifndef EV_KEY
67# define EV_KEY 0x01
68#endif
69#ifndef SW_LID
70# define SW_LID 0x00
71#endif
72#ifndef SW_RFKILL_ALL
73# define SW_RFKILL_ALL 0x03
74#endif
75#ifndef KEY_POWER
76# define KEY_POWER 116
77#endif
78#ifndef KEY_SLEEP
79# define KEY_SLEEP 142
80#endif
81
82enum {
83 OPT_c = (1 << 0),
84 OPT_d = (1 << 1),
85 OPT_e = (1 << 2),
86 OPT_f = (1 << 3),
87 OPT_l = (1 << 4),
88 OPT_a = (1 << 5),
89 OPT_M = (1 << 6),
90 OPT_p = (1 << 7) * ENABLE_FEATURE_PIDFILE,
91};
92
93struct acpi_event {
94 const char *s_type;
95 uint16_t n_type;
96 const char *s_code;
97 uint16_t n_code;
98 uint32_t value;
99 const char *desc;
100};
101
102static const struct acpi_event f_evt_tab[] ALIGN_PTR = {
103 { "EV_KEY", 0x01, "KEY_POWER", 116, 1, "button/power PWRF 00000080" },
104 { "EV_KEY", 0x01, "KEY_POWER", 116, 1, "button/power PWRB 00000080" },
105 { "EV_SW", 0x05, "SW_LID", 0x00, 1, "button/lid LID0 00000080" },
106};
107
108struct acpi_action {
109 const char *key;
110 const char *action;
111};
112
113static const struct acpi_action f_act_tab[] ALIGN_PTR = {
114 { "PWRF", "PWRF/00000080" },
115 { "LID0", "LID/00000080" },
116};
117
118struct globals {
119 struct acpi_action *act_tab;
120 int n_act;
121 struct acpi_event *evt_tab;
122 int n_evt;
123} FIX_ALIASING;
124#define G (*ptr_to_globals)
125#define act_tab (G.act_tab)
126#define n_act (G.n_act )
127#define evt_tab (G.evt_tab)
128#define n_evt (G.n_evt )
129#define INIT_G() do { \
130 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
131} while (0)
132
133
134
135
136
137
138
139
140
141
142
143
144
145static void process_event(const char *event)
146{
147 struct stat st;
148 char *handler = xasprintf("./%s", event);
149 const char *args[] = { "run-parts", handler, NULL };
150
151
152 bb_simple_error_msg(event);
153
154
155
156
157
158 if (0 == stat(event, &st))
159 spawn((char **)args + (0==(st.st_mode & S_IFDIR)));
160 else
161 bb_simple_perror_msg(event);
162
163 free(handler);
164}
165
166static const char *find_action(struct input_event *ev, const char *buf)
167{
168 const char *action = NULL;
169 int i;
170
171
172 for (i = 0; i < n_evt; i++) {
173 if (ev) {
174 if (ev->type == evt_tab[i].n_type && ev->code == evt_tab[i].n_code && ev->value == evt_tab[i].value) {
175 action = evt_tab[i].desc;
176 break;
177 }
178 }
179
180 if (buf) {
181 if (is_prefixed_with(evt_tab[i].desc, buf)) {
182 action = evt_tab[i].desc;
183 break;
184 }
185 }
186 }
187
188
189 if (action) {
190 for (i = 0; i < n_act; i++) {
191 if (strstr(action, act_tab[i].key)) {
192 action = act_tab[i].action;
193 break;
194 }
195 }
196 }
197
198 return action;
199}
200
201static void parse_conf_file(const char *filename)
202{
203 parser_t *parser;
204 char *tokens[2];
205
206 parser = config_open2(filename, fopen_for_read);
207
208 if (parser) {
209 while (config_read(parser, tokens, 2, 2, "# \t", PARSE_NORMAL)) {
210 act_tab = xrealloc_vector(act_tab, 1, n_act);
211 act_tab[n_act].key = xstrdup(tokens[0]);
212 act_tab[n_act].action = xstrdup(tokens[1]);
213 n_act++;
214 }
215 config_close(parser);
216 } else {
217 act_tab = (void*)f_act_tab;
218 n_act = ARRAY_SIZE(f_act_tab);
219 }
220}
221
222static void parse_map_file(const char *filename)
223{
224 parser_t *parser;
225 char *tokens[6];
226
227 parser = config_open2(filename, fopen_for_read);
228
229 if (parser) {
230 while (config_read(parser, tokens, 6, 6, "# \t", PARSE_NORMAL)) {
231 evt_tab = xrealloc_vector(evt_tab, 1, n_evt);
232 evt_tab[n_evt].s_type = xstrdup(tokens[0]);
233 evt_tab[n_evt].n_type = xstrtou(tokens[1], 16);
234 evt_tab[n_evt].s_code = xstrdup(tokens[2]);
235 evt_tab[n_evt].n_code = xatou16(tokens[3]);
236 evt_tab[n_evt].value = xatoi_positive(tokens[4]);
237 evt_tab[n_evt].desc = xstrdup(tokens[5]);
238 n_evt++;
239 }
240 config_close(parser);
241 } else {
242 evt_tab = (void*)f_evt_tab;
243 n_evt = ARRAY_SIZE(f_evt_tab);
244 }
245}
246
247
248
249
250
251int acpid_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
252int acpid_main(int argc UNUSED_PARAM, char **argv)
253{
254 int nfd;
255 int opts;
256 struct pollfd *pfd;
257 const char *opt_dir = "/etc/acpi";
258 const char *opt_input = "/dev/input/event";
259 const char *opt_logfile = "/var/log/acpid.log";
260 const char *opt_action = "/etc/acpid.conf";
261 const char *opt_map = "/etc/acpi.map";
262#if ENABLE_FEATURE_PIDFILE
263 const char *opt_pidfile = CONFIG_PID_FILE_PATH "/acpid.pid";
264#endif
265
266 INIT_G();
267
268 opts = getopt32(argv, "^"
269 "c:de:fl:a:M:"
270 IF_FEATURE_PIDFILE("p:")
271 IF_FEATURE_ACPID_COMPAT("g:m:s:S:v")
272 "\0"
273 "df:e--e",
274 &opt_dir, &opt_input, &opt_logfile, &opt_action, &opt_map
275 IF_FEATURE_PIDFILE(, &opt_pidfile)
276 IF_FEATURE_ACPID_COMPAT(, NULL, NULL, NULL, NULL)
277 );
278
279 if (!(opts & OPT_f)) {
280
281 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
282 }
283
284 if (!(opts & OPT_d)) {
285
286
287
288 xmove_fd(xopen(opt_logfile, O_WRONLY | O_CREAT | O_APPEND), STDOUT_FILENO);
289 xdup2(STDOUT_FILENO, STDERR_FILENO);
290
291 openlog(applet_name, LOG_PID, LOG_DAEMON);
292 logmode = LOGMODE_SYSLOG | LOGMODE_STDIO;
293 }
294
295
296 parse_conf_file(opt_action);
297 parse_map_file(opt_map);
298
299 xchdir(opt_dir);
300
301
302 bb_signals((1 << SIGCHLD), SIG_IGN);
303
304
305
306
307
308 pfd = NULL;
309 nfd = 0;
310 while (1) {
311 int fd;
312 char *dev_event;
313
314 dev_event = xasprintf((opts & OPT_e) ? "%s" : "%s%u", opt_input, nfd);
315 fd = open(dev_event, O_RDONLY | O_NONBLOCK);
316 if (fd < 0) {
317 if (nfd == 0)
318 bb_simple_perror_msg_and_die(dev_event);
319 break;
320 }
321 free(dev_event);
322 pfd = xrealloc_vector(pfd, 1, nfd);
323 pfd[nfd].fd = fd;
324 pfd[nfd].events = POLLIN;
325 nfd++;
326 }
327
328 write_pidfile(opt_pidfile);
329
330 while (safe_poll(pfd, nfd, -1) > 0) {
331 int i;
332 for (i = 0; i < nfd; i++) {
333 const char *event;
334
335 if (!(pfd[i].revents & POLLIN)) {
336 if (pfd[i].revents == 0)
337 continue;
338
339
340
341
342 close(pfd[i].fd);
343 nfd--;
344 for (; i < nfd; i++)
345 pfd[i].fd = pfd[i + 1].fd;
346 break;
347 }
348
349 event = NULL;
350 if (option_mask32 & OPT_e) {
351 char *buf;
352 int len;
353
354 buf = xmalloc_reads(pfd[i].fd, NULL);
355
356 len = strlen(buf) - 9;
357 if (len >= 0)
358 buf[len] = '\0';
359 event = find_action(NULL, buf);
360 free(buf);
361 } else {
362 struct input_event ev;
363
364 if (sizeof(ev) != full_read(pfd[i].fd, &ev, sizeof(ev)))
365 continue;
366
367 if (ev.value != 1 && ev.value != 0)
368 continue;
369
370 event = find_action(&ev, NULL);
371 }
372 if (!event)
373 continue;
374
375 process_event(event);
376 }
377 }
378
379 if (ENABLE_FEATURE_CLEAN_UP) {
380 while (nfd--)
381 close(pfd[nfd].fd);
382 free(pfd);
383 }
384 remove_pidfile(opt_pidfile);
385
386 return EXIT_SUCCESS;
387}
388