]> git.ozlabs.org Git - petitboot/blob - discover/pb-discover.c
Fix sftp loader
[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 opts opts;
105         struct udev *udev;
106         struct user_event *uev;
107
108         if (opts_parse(&opts, argc, argv)) {
109                 print_usage();
110                 return EXIT_FAILURE;
111         }
112
113         if (opts.show_help == opt_yes) {
114                 print_usage();
115                 return EXIT_SUCCESS;
116         }
117
118         if (opts.show_version == opt_yes) {
119                 print_version();
120                 return EXIT_SUCCESS;
121         }
122
123         if (strcmp(opts.log_file, "-")) {
124                 FILE *log = fopen(opts.log_file, "a");
125
126                 assert(log);
127                 pb_log_set_stream(log);
128         } else
129                 pb_log_set_stream(stderr);
130
131 #if defined(DEBUG)
132         pb_log_always_flush(1);
133 #endif
134         pb_log("--- pb-discover ---\n");
135
136         /* we look for closed sockets when we write, so ignore SIGPIPE */
137         signal(SIGPIPE, SIG_IGN);
138
139         signal(SIGINT, sigint_handler);
140
141         server = discover_server_init();
142         if (!server)
143                 return EXIT_FAILURE;
144
145         handler = device_handler_init(server);
146         if (!handler)
147                 return EXIT_FAILURE;
148
149         discover_server_set_device_source(server, handler);
150
151         udev = udev_init(handler);
152         if (!udev)
153                 return EXIT_FAILURE;
154
155         uev = user_event_init(handler);
156         if (!uev)
157                 return EXIT_FAILURE;
158
159         udev_trigger(udev);
160         user_event_trigger(uev);
161
162         for (running = 1; running;) {
163                 if (waiter_poll())
164                         break;
165         }
166
167         device_handler_destroy(handler);
168
169         pb_log("--- end ---\n");
170
171         return EXIT_SUCCESS;
172 }