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