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