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