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