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