-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
267 lines (238 loc) · 4.98 KB
/
main.c
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* This file is part of jacklistener - jack state monitor
* Copyright (C) 2012 Maxim A. Mikityanskiy - <maxtram95@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/input.h>
#include <getopt.h>
#include <pwd.h>
#include <string.h>
#include <errno.h>
#ifdef ENABLE_UDEV
#include <libudev.h>
#endif
#include "main.h"
#include "event.h"
#include "dbus.h"
#include "watchdevs.h"
static FILE *pidfile;
int is_quiet = 0;
static int is_udev = 0;
static int has_udev =
#ifdef ENABLE_UDEV
1
#else
0
#endif
;
fd_set evdevfds;
struct hash_element *fdhash = NULL;
static void usage(const char *argv0);
static void open_pidfile(char *path);
static void parse_events(void);
#ifdef ENABLE_UDEV
struct udev *udev = NULL;
struct udev_monitor *mon = NULL;
#endif
enum {
ARG_HAS_UDEV = 1000,
};
int main(int argc, char *argv[])
{
// Parse options
int is_daemon = 0;
int is_pidfile = 0;
int is_query_has_udev = 0;
const struct option options[] = {
{ "quiet", no_argument, NULL, 'q' },
{ "daemon", no_argument, NULL, 'd' },
{ "pidfile", required_argument, NULL, 'p' },
#ifdef ENABLE_UDEV
{ "udev", no_argument, NULL, 'u'},
#endif
{ "has-udev", no_argument, NULL, ARG_HAS_UDEV },
{ NULL, 0, NULL, 0 }
};
int optparse;
const char *optchars = "qdp:"
#ifdef ENABLE_UDEV
"u"
#endif
;
//opterr = 0;
while ((optparse = getopt_long(argc, argv, optchars, options, NULL)) != -1) {
switch (optparse) {
case 'q':
is_quiet = 1;
break;
case 'd':
is_daemon = 1;
break;
case 'p':
is_pidfile = 1;
open_pidfile(optarg);
break;
#ifdef ENABLE_UDEV
case 'u':
is_udev = 1;
break;
#endif
case ARG_HAS_UDEV:
is_query_has_udev = 1;
break;
case ':':
case '?':
default:
fprintf(stderr, "Invalid options\n");
usage(argv[0]);
break;
}
}
if (is_query_has_udev) {
exit(!has_udev);
}
if (!is_udev && optind >= argc) {
usage(argv[0]);
}
if (is_udev && optind != argc) {
usage(argv[0]);
}
if (!mp_dbus_init()) {
fprintf(stderr, "Unable to initialize D-Bus\n");
exit(13);
}
FD_ZERO(&evdevfds);
#ifdef ENABLE_UDEV
if (is_udev) {
udev = udev_new();
if (!udev) {
fprintf(stderr, "%s: Cannot create udev context\n", __PRETTY_FUNCTION__);
terminate(1);
}
}
#endif
if (!is_udev) {
init_devices_list(argc - optind, argv + optind);
}
if (is_daemon) {
switch (fork()) {
case -1:
terminate(63);
case 0:
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
if (chdir("/")) ;
if (setsid() == -1) {
terminate(63);
}
break;
default:
if (!is_quiet) {
fprintf(stderr, "Daemonized successfully\n");
}
exit(0);
}
}
if (is_pidfile) {
fprintf(pidfile, "%d", getpid());
fclose(pidfile);
}
#ifdef ENABLE_UDEV
if (is_udev) {
mon = init_monitor();
init_devices_udev();
}
#endif
if (!is_udev) {
setuid(getpwnam("daemon")->pw_uid);
}
parse_events();
terminate(0);
return 0;
}
static void parse_events(void)
{
#ifdef ENABLE_UDEV
int mon_fd = -1;
if (is_udev) {
mon_fd = udev_monitor_get_fd(mon);
}
#endif
while (1) {
fd_set activefds = evdevfds;
struct hash_element *elem, *tmp;
#ifdef ENABLE_UDEV
if (is_udev) {
FD_SET(mon_fd, &activefds);
}
#endif
if (select(FD_SETSIZE, &activefds, NULL, NULL, NULL) == -1) {
fprintf(stderr, "Error when polling events in select(): %s\n", strerror(errno));
terminate(3);
}
HASH_ITER(hh, fdhash, elem, tmp) {
if (FD_ISSET(elem->fd, &activefds)) {
if (!handle_device_event(elem->fd)) {
unlisten_device_by_hash_element(elem);
}
}
}
#ifdef ENABLE_UDEV
if (is_udev && FD_ISSET(mon_fd, &activefds)) {
handle_monitor_event();
}
#endif
}
}
static void usage(const char *argv0)
{
const char *udev_args_desc = ""
#ifdef ENABLE_UDEV
"[-u|--udev]|"
#endif
;
fprintf(stderr, "Usage: %s --has-udev | ([-q|--quiet] [-d|--daemon] [-p|--pidfile <pidfile>] %s<event device files>)\n", argv0, udev_args_desc);
exit(254);
}
void terminate(int exitcode)
{
mp_dbus_fini();
#ifdef ENABLE_UDEV
if (is_udev) {
unlisten_all_devices();
if (mon) {
udev_monitor_unref(mon);
}
if (udev) {
udev_unref(udev);
}
}
#endif
exit(exitcode);
}
static void open_pidfile(char *path)
{
if ((pidfile = fopen(path, "w")) == NULL) {
perror("fopen");
fprintf(stderr, "Cannot open pidfile \"%s\"\n", path);
exit(31);
}
}