8ab284974d3863bf101d9e90b5ba1e03c79cc64d
[petitboot] / discover / pb-discover.c
1
2 #if defined(HAVE_CONFIG_H)
3 #include "config.h"
4 #endif
5
6 #include <assert.h>
7 #include <getopt.h>
8 #include <stdlib.h>
9 #include <signal.h>
10 #include <string.h>
11
12 #include <waiter/waiter.h>
13 #include <log/log.h>
14 #include <pb-config/pb-config.h>
15
16 #include "udev.h"
17 #include "user-event.h"
18 #include "discover-server.h"
19 #include "device-handler.h"
20 #include "network.h"
21
22 static void print_version(void)
23 {
24         printf("pb-discover (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
25 }
26
27 static void print_usage(void)
28 {
29         print_version();
30         printf(
31 "Usage: pb-discover [-a, --no-autoboot] [-h, --help] [-l, --log log-file]\n"
32 "                   [-n, --dry-run] [-V, --version]\n");
33 }
34
35 /**
36  * enum opt_value - Tri-state options variables.
37  */
38
39 enum opt_value {opt_undef = 0, opt_yes, opt_no};
40
41 /**
42  * struct opts - Values from command line options.
43  */
44
45 struct opts {
46         enum opt_value no_autoboot;
47         enum opt_value show_help;
48         const char *log_file;
49         enum opt_value dry_run;
50         enum opt_value show_version;
51 };
52
53 /**
54  * opts_parse - Parse the command line options.
55  */
56
57 static int opts_parse(struct opts *opts, int argc, char *argv[])
58 {
59         static const struct option long_options[] = {
60                 {"no-autoboot",    no_argument,       NULL, 'a'},
61                 {"help",           no_argument,       NULL, 'h'},
62                 {"log",            required_argument, NULL, 'l'},
63                 {"dry-run",        no_argument,       NULL, 'n'},
64                 {"version",        no_argument,       NULL, 'V'},
65                 { NULL, 0, NULL, 0},
66         };
67         static const char short_options[] = "ahl:nV";
68         static const struct opts default_values = {
69                 .no_autoboot = opt_no,
70                 .log_file = "/var/log/petitboot/pb-discover.log",
71                 .dry_run = opt_no,
72         };
73
74         *opts = default_values;
75
76         while (1) {
77                 int c = getopt_long(argc, argv, short_options, long_options,
78                         NULL);
79
80                 if (c == EOF)
81                         break;
82
83                 switch (c) {
84                 case 'a':
85                         opts->no_autoboot = opt_yes;
86                         break;
87                 case 'h':
88                         opts->show_help = opt_yes;
89                         break;
90                 case 'l':
91                         opts->log_file = optarg;
92                         break;
93                 case 'n':
94                         opts->dry_run = opt_yes;
95                         break;
96                 case 'V':
97                         opts->show_version = opt_yes;
98                         break;
99                 default:
100                         opts->show_help = opt_yes;
101                         return -1;
102                 }
103         }
104
105         return optind != argc;
106 }
107
108 static int running;
109
110 static void sigint_handler(int __attribute__((unused)) signum)
111 {
112         running = 0;
113 }
114
115 int main(int argc, char *argv[])
116 {
117         struct device_handler *handler;
118         struct discover_server *server;
119         struct network *network;
120         struct waitset *waitset;
121         struct opts opts;
122         struct pb_udev *udev;
123         struct user_event *uev;
124
125         if (opts_parse(&opts, argc, argv)) {
126                 print_usage();
127                 return EXIT_FAILURE;
128         }
129
130         if (opts.show_help == opt_yes) {
131                 print_usage();
132                 return EXIT_SUCCESS;
133         }
134
135         if (opts.show_version == opt_yes) {
136                 print_version();
137                 return EXIT_SUCCESS;
138         }
139
140         if (strcmp(opts.log_file, "-")) {
141                 FILE *log = fopen(opts.log_file, "a");
142
143                 assert(log);
144                 pb_log_set_stream(log);
145         } else
146                 pb_log_set_stream(stderr);
147
148 #if defined(DEBUG)
149         pb_log_always_flush(1);
150 #endif
151         pb_log("--- pb-discover ---\n");
152
153         /* we look for closed sockets when we write, so ignore SIGPIPE */
154         signal(SIGPIPE, SIG_IGN);
155
156         signal(SIGINT, sigint_handler);
157
158         config_init(NULL);
159
160         if (opts.no_autoboot == opt_yes)
161                 config_set_autoboot(false);
162
163         waitset = waitset_create(NULL);
164
165         server = discover_server_init(waitset);
166         if (!server)
167                 return EXIT_FAILURE;
168
169         network = network_init(server, waitset, opts.dry_run == opt_yes);
170         if (!network)
171                 return EXIT_FAILURE;
172
173         handler = device_handler_init(server, waitset, opts.dry_run == opt_yes);
174         if (!handler)
175                 return EXIT_FAILURE;
176
177         discover_server_set_device_source(server, handler);
178
179         udev = udev_init(waitset, handler);
180         if (!udev)
181                 return EXIT_FAILURE;
182
183         uev = user_event_init(waitset, handler);
184         if (!uev)
185                 return EXIT_FAILURE;
186
187         user_event_trigger(uev);
188
189         for (running = 1; running;) {
190                 if (waiter_poll(waitset))
191                         break;
192         }
193
194         device_handler_destroy(handler);
195         waitset_destroy(waitset);
196         udev_destroy(udev);
197         config_fini();
198
199         pb_log("--- end ---\n");
200
201         return EXIT_SUCCESS;
202 }