1
2
3
4
5
6
7
8
9#include "libbb.h"
10
11#include <linux/input.h>
12#ifndef SW_RFKILL_ALL
13# define SW_RFKILL_ALL 3
14#endif
15
16
17
18
19
20
21
22
23
24
25
26
27
28static void process_event(const char *event)
29{
30 struct stat st;
31 char *handler = xasprintf("./%s", event);
32 const char *args[] = { "run-parts", handler, NULL };
33
34
35 if (option_mask32 & 8) {
36 bb_error_msg("%s", event);
37 }
38
39
40
41
42
43 if (0 == stat(event, &st))
44 spawn((char **)args + (0==(st.st_mode & S_IFDIR)));
45 else
46 bb_simple_perror_msg(event);
47 free(handler);
48}
49
50
51
52
53
54int acpid_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
55int acpid_main(int argc, char **argv)
56{
57 struct pollfd *pfd;
58 int i, nfd;
59 const char *opt_conf = "/etc/acpi";
60 const char *opt_input = "/proc/acpi/event";
61 const char *opt_logfile = "/var/log/acpid.log";
62
63 getopt32(argv, "c:e:l:d"
64 USE_FEATURE_ACPID_COMPAT("g:m:s:S:v"),
65 &opt_conf, &opt_input, &opt_logfile
66 USE_FEATURE_ACPID_COMPAT(, NULL, NULL, NULL, NULL, NULL)
67 );
68
69
70 if (!(option_mask32 & 8)) {
71 bb_daemonize_or_rexec(0, argv);
72 close(2);
73 xopen(opt_logfile, O_WRONLY | O_CREAT | O_TRUNC);
74 }
75
76 argv += optind;
77
78
79 xchdir(opt_conf);
80
81
82
83
84
85 if (!*argv) {
86
87 char *token[4];
88 parser_t *parser = config_open(opt_input);
89
90
91 while (config_read(parser, token, 4, 4, "\0 ", PARSE_NORMAL)) {
92 char *event = xasprintf("%s/%s", token[1], token[2]);
93 process_event(event);
94 free(event);
95 }
96
97 if (ENABLE_FEATURE_CLEAN_UP)
98 config_close(parser);
99 return EXIT_SUCCESS;
100 }
101
102
103
104
105 pfd = xzalloc(sizeof(*pfd) * (argc - optind));
106 nfd = 0;
107 while (*argv) {
108 pfd[nfd].fd = open_or_warn(*argv++, O_RDONLY | O_NONBLOCK);
109 if (pfd[nfd].fd >= 0)
110 pfd[nfd++].events = POLLIN;
111 }
112
113
114 while ( poll(pfd, nfd, -1) > 0) {
115 for (i = 0; i < nfd; i++) {
116 const char *event;
117 struct input_event ev;
118
119 if (!(pfd[i].revents & POLLIN))
120 continue;
121
122 if (sizeof(ev) != full_read(pfd[i].fd, &ev, sizeof(ev)))
123 continue;
124
125
126
127 if (ev.value != 1)
128 continue;
129
130 event = NULL;
131
132
133
134
135
136
137
138 if (EV_KEY == ev.type) {
139 if (KEY_POWER == ev.code)
140 event = "PWRF/00000080";
141 else if (KEY_SLEEP == ev.code)
142 event = "SLPB/00000080";
143 }
144
145 else if (EV_SW == ev.type) {
146 if (SW_LID == ev.code)
147 event = "LID/00000080";
148 else if (SW_RFKILL_ALL == ev.code)
149 event = "RFKILL";
150 }
151
152 if (!event)
153 continue;
154
155
156 process_event(event);
157 }
158 }
159
160 if (ENABLE_FEATURE_CLEAN_UP) {
161 for (i = 0; i < nfd; i++)
162 close(pfd[i].fd);
163 free(pfd);
164 }
165
166 return EXIT_SUCCESS;
167}
168