]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/generic-main.c
Add UI option --dry-run
[petitboot] / ui / ncurses / generic-main.c
1 /*
2  * Petitboot generic ncurses bootloader UI
3  *
4  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
5  *  Copyright 2009 Sony Corp.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; version 2 of the License.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #if defined(HAVE_CONFIG_H)
22 #include "config.h"
23 #endif
24
25 #define _GNU_SOURCE
26 #include <errno.h>
27 #include <getopt.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/time.h>
32
33 #include "log/log.h"
34 #include "talloc/talloc.h"
35 #include "waiter/waiter.h"
36 #include "ui/common/discover-client.h"
37 #include "nc-cui.h"
38
39 static void print_version(void)
40 {
41         printf("petitboot-nc (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
42 }
43
44 static void print_usage(void)
45 {
46         print_version();
47         printf(
48 "Usage: petitboot-nc [-d, --dry-run] [-h, --help] [-l, --log log-file]\n"
49 "                    [-s, --start-daemon] [-V, --version]\n");
50 }
51
52 /**
53  * enum opt_value - Tri-state options variables.
54  */
55
56 enum opt_value {opt_undef = 0, opt_yes, opt_no};
57
58 /**
59  * struct opts - Values from command line options.
60  */
61
62 struct opts {
63         enum opt_value dry_run;
64         enum opt_value show_help;
65         const char *log_file;
66         enum opt_value start_daemon;
67         enum opt_value show_version;
68 };
69
70 /**
71  * opts_parse - Parse the command line options.
72  */
73
74 static int opts_parse(struct opts *opts, int argc, char *argv[])
75 {
76         static const struct option long_options[] = {
77                 {"dry-run",      no_argument,       NULL, 'd'},
78                 {"help",         no_argument,       NULL, 'h'},
79                 {"log",          required_argument, NULL, 'l'},
80                 {"start-daemon", no_argument,       NULL, 's'},
81                 {"version",      no_argument,       NULL, 'V'},
82                 { NULL,          0,                 NULL, 0},
83         };
84         static const char short_options[] = "dhl:sV";
85         static const struct opts default_values = {
86                 .log_file = "/var/log/petitboot/petitboot-nc.log",
87         };
88
89         *opts = default_values;
90
91         while (1) {
92                 int c = getopt_long(argc, argv, short_options, long_options,
93                         NULL);
94
95                 if (c == EOF)
96                         break;
97
98                 switch (c) {
99                 case 'd':
100                         opts->dry_run = opt_yes;
101                         break;
102                 case 'h':
103                         opts->show_help = opt_yes;
104                         break;
105                 case 'l':
106                         opts->log_file = optarg;
107                         break;
108                 case 's':
109                         opts->start_daemon = opt_yes;
110                         break;
111                 case 'V':
112                         opts->show_version = opt_yes;
113                         break;
114                 default:
115                         opts->show_help = opt_yes;
116                         return -1;
117                 }
118         }
119
120         return 0;
121 }
122
123 /**
124  * struct pb_cui - Main cui program instance.
125  * @mm: Main menu.
126  * @svm: Set video mode menu.
127  */
128
129 struct pb_cui {
130         struct pmenu *mm;
131         struct cui *cui;
132         struct opts opts;
133 };
134
135 static struct pb_cui *pb_from_cui(struct cui *cui)
136 {
137         struct pb_cui *pb;
138
139         assert(cui->c_sig == pb_cui_sig);
140         pb = cui->platform_info;
141         assert(pb->cui->c_sig == pb_cui_sig);
142         return pb;
143 }
144
145 /**
146  * pb_kexec_cb - The kexec callback.
147  */
148
149 static int pb_kexec_cb(struct cui *cui, struct cui_opt_data *cod)
150 {
151         struct pb_cui *pb = pb_from_cui(cui);
152
153         pb_log("%s: %s\n", __func__, cod->name);
154
155         assert(pb->cui->current == &pb->cui->main->scr);
156
157         return pb_run_kexec(cod->kd, pb->opts.dry_run);
158 }
159
160 /**
161  * pb_mm_init - Setup the main menu instance.
162  */
163
164 static struct pmenu *pb_mm_init(struct pb_cui *pb_cui)
165 {
166         int result;
167         struct pmenu *m;
168         struct pmenu_item *i;
169
170         m = pmenu_init(pb_cui->cui, 1, cui_on_exit);
171
172         if (!m) {
173                 pb_log("%s: failed\n", __func__);
174                 return NULL;
175         }
176
177         m->on_open = cui_on_open;
178
179         m->scr.frame.title = talloc_strdup(m, "Petitboot");
180         m->scr.frame.help = talloc_strdup(m,
181                 "ESC=exit, Enter=accept, e=edit, o=open");
182         m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
183
184         i = pmenu_item_init(m, 0, "Exit to Shell");
185         i->on_execute = pmenu_exit_cb;
186
187         result = pmenu_setup(m);
188
189         if (result) {
190                 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
191                         strerror(errno));
192                 goto fail_setup;
193         }
194
195         menu_opts_off(m->ncm, O_SHOWDESC);
196         set_menu_mark(m->ncm, " *");
197         set_current_item(m->ncm, i->nci);
198
199         return m;
200
201 fail_setup:
202         talloc_free(m);
203         return NULL;
204 }
205
206 static struct pb_cui pb;
207
208 static void sig_handler(int signum)
209 {
210         DBGS("%d\n", signum);
211
212         switch (signum) {
213         case SIGALRM:
214                 if (pb.cui)
215                         ui_timer_sigalrm(&pb.cui->timer);
216                 break;
217         case SIGWINCH:
218                 if (pb.cui)
219                         cui_resize(pb.cui);
220                 break;
221         default:
222                 assert(0 && "unknown sig");
223                 /* fall through */
224         case SIGINT:
225         case SIGHUP:
226         case SIGTERM:
227                 if (pb.cui)
228                         cui_abort(pb.cui);
229                 break;
230         }
231 }
232
233 /**
234  * main - cui bootloader main routine.
235  */
236
237 int main(int argc, char *argv[])
238 {
239         static struct sigaction sa;
240         int result;
241         int cui_result;
242
243         result = opts_parse(&pb.opts, argc, argv);
244
245         if (result) {
246                 print_usage();
247                 return EXIT_FAILURE;
248         }
249
250         if (pb.opts.show_help == opt_yes) {
251                 print_usage();
252                 return EXIT_SUCCESS;
253         }
254
255         if (pb.opts.show_version == opt_yes) {
256                 print_version();
257                 return EXIT_SUCCESS;
258         }
259
260         if (strcmp(pb.opts.log_file, "-")) {
261                 FILE *log = fopen(pb.opts.log_file, "a");
262
263                 assert(log);
264                 pb_log_set_stream(log);
265         } else
266                 pb_log_set_stream(stderr);
267
268 #if defined(DEBUG)
269         pb_log_always_flush(1);
270 #endif
271
272         pb_log("--- petitboot-nc ---\n");
273
274         sa.sa_handler = sig_handler;
275         result = sigaction(SIGALRM, &sa, NULL);
276         result += sigaction(SIGHUP, &sa, NULL);
277         result += sigaction(SIGINT, &sa, NULL);
278         result += sigaction(SIGTERM, &sa, NULL);
279         result += sigaction(SIGWINCH, &sa, NULL);
280
281         if (result) {
282                 pb_log("%s sigaction failed.\n", __func__);
283                 return EXIT_FAILURE;
284         }
285
286         pb.cui = cui_init(&pb, pb_kexec_cb, NULL, pb.opts.start_daemon);
287
288         if (!pb.cui)
289                 return EXIT_FAILURE;
290
291         pb.mm = pb_mm_init(&pb);
292         ui_timer_disable(&pb.cui->timer);
293
294         cui_result = cui_run(pb.cui, pb.mm, 0);
295
296         pmenu_delete(pb.mm);
297
298         talloc_free(pb.cui);
299
300         pb_log("--- end ---\n");
301
302         return cui_result ? EXIT_FAILURE : EXIT_SUCCESS;
303 }