6def0900bc73cddf56397c67641825899e8abde1
[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         FILE *log;
127
128         if (opts_parse(&opts, argc, argv)) {
129                 print_usage();
130                 return EXIT_FAILURE;
131         }
132
133         if (opts.show_help == opt_yes) {
134                 print_usage();
135                 return EXIT_SUCCESS;
136         }
137
138         if (opts.show_version == opt_yes) {
139                 print_version();
140                 return EXIT_SUCCESS;
141         }
142
143         log = stderr;
144         if (strcmp(opts.log_file, "-")) {
145                 log = fopen(opts.log_file, "a");
146                 if (!log) {
147                         fprintf(stderr, "can't open log file %s, logging to "
148                                         "stderr\n", opts.log_file);
149                         log = stderr;
150                 }
151         }
152         pb_log_init(log);
153
154         pb_log("--- pb-discover ---\n");
155
156         /* we look for closed sockets when we write, so ignore SIGPIPE */
157         signal(SIGPIPE, SIG_IGN);
158
159         signal(SIGINT, sigint_handler);
160
161         if (opts.no_autoboot == opt_yes)
162                 config_set_autoboot(false);
163
164         waitset = waitset_create(NULL);
165
166         server = discover_server_init(waitset);
167         if (!server)
168                 return EXIT_FAILURE;
169
170         procset = process_init(server, waitset, opts.dry_run == opt_yes);
171         if (!procset)
172                 return EXIT_FAILURE;
173
174         config_init(NULL);
175
176         handler = device_handler_init(server, waitset, opts.dry_run == opt_yes);
177         if (!handler)
178                 return EXIT_FAILURE;
179
180         discover_server_set_device_source(server, handler);
181
182         /* init our device sources: udev, network and user events */
183         udev = udev_init(waitset, handler);
184         if (!udev)
185                 return EXIT_FAILURE;
186
187         network = network_init(handler, waitset, opts.dry_run == opt_yes);
188         if (!network)
189                 return EXIT_FAILURE;
190
191         uev = user_event_init(waitset, handler);
192         if (!uev)
193                 return EXIT_FAILURE;
194
195         for (running = 1; running;) {
196                 if (waiter_poll(waitset))
197                         break;
198         }
199
200         device_handler_destroy(handler);
201         udev_destroy(udev);
202         config_fini();
203
204         pb_log("--- end ---\n");
205
206         return EXIT_SUCCESS;
207 }