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