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
|
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif
#include <assert.h>
#include <getopt.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <waiter/waiter.h>
#include <log/log.h>
#include "udev.h"
#include "user-event.h"
#include "discover-server.h"
#include "device-handler.h"
static void print_version(void)
{
printf("pb-discover (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
}
static void print_usage(void)
{
print_version();
printf(
"Usage: pb-discover [-h, --help] [-l, --log log-file] [-V, --version]\n");
}
/**
* enum opt_value - Tri-state options variables.
*/
enum opt_value {opt_undef = 0, opt_yes, opt_no};
/**
* struct opts - Values from command line options.
*/
struct opts {
enum opt_value show_help;
const char *log_file;
enum opt_value show_version;
enum opt_value dry_run;
};
/**
* opts_parse - Parse the command line options.
*/
static int opts_parse(struct opts *opts, int argc, char *argv[])
{
static const struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"log", required_argument, NULL, 'l'},
{"dry-run", no_argument, NULL, 'n'},
{"version", no_argument, NULL, 'V'},
{ NULL, 0, NULL, 0},
};
static const char short_options[] = "hl:nV";
static const struct opts default_values = {
.log_file = "/var/log/petitboot/pb-discover.log",
.dry_run = opt_no,
};
*opts = default_values;
while (1) {
int c = getopt_long(argc, argv, short_options, long_options,
NULL);
if (c == EOF)
break;
switch (c) {
case 'h':
opts->show_help = opt_yes;
break;
case 'l':
opts->log_file = optarg;
break;
case 'n':
opts->dry_run = opt_yes;
break;
case 'V':
opts->show_version = opt_yes;
break;
default:
opts->show_help = opt_yes;
return -1;
}
}
return optind != argc;
}
static int running;
static void sigint_handler(int __attribute__((unused)) signum)
{
running = 0;
}
int main(int argc, char *argv[])
{
struct device_handler *handler;
struct discover_server *server;
struct waitset *waitset;
struct opts opts;
struct pb_udev *udev;
struct user_event *uev;
if (opts_parse(&opts, argc, argv)) {
print_usage();
return EXIT_FAILURE;
}
if (opts.show_help == opt_yes) {
print_usage();
return EXIT_SUCCESS;
}
if (opts.show_version == opt_yes) {
print_version();
return EXIT_SUCCESS;
}
if (strcmp(opts.log_file, "-")) {
FILE *log = fopen(opts.log_file, "a");
assert(log);
pb_log_set_stream(log);
} else
pb_log_set_stream(stderr);
#if defined(DEBUG)
pb_log_always_flush(1);
#endif
pb_log("--- pb-discover ---\n");
/* we look for closed sockets when we write, so ignore SIGPIPE */
signal(SIGPIPE, SIG_IGN);
signal(SIGINT, sigint_handler);
waitset = waitset_create(NULL);
server = discover_server_init(waitset);
if (!server)
return EXIT_FAILURE;
handler = device_handler_init(server, opts.dry_run == opt_yes);
if (!handler)
return EXIT_FAILURE;
discover_server_set_device_source(server, handler);
udev = udev_init(waitset, handler);
if (!udev)
return EXIT_FAILURE;
uev = user_event_init(waitset, handler);
if (!uev)
return EXIT_FAILURE;
user_event_trigger(uev);
for (running = 1; running;) {
if (waiter_poll(waitset))
break;
}
device_handler_destroy(handler);
waitset_destroy(waitset);
udev_destroy(udev);
pb_log("--- end ---\n");
return EXIT_SUCCESS;
}
|