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