]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/generic-main.c
ui/ncurses: Use tty name in default log filename
[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 #include <assert.h>
26 #include <errno.h>
27 #include <getopt.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <sys/time.h>
33
34 #include "log/log.h"
35 #include "talloc/talloc.h"
36 #include "waiter/waiter.h"
37 #include "ui/common/discover-client.h"
38 #include "nc-cui.h"
39
40 extern const char *main_menu_help_text;
41
42 static void print_version(void)
43 {
44         printf("petitboot-nc (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
45 }
46
47 static void print_usage(void)
48 {
49         print_version();
50         printf(
51 "Usage: petitboot-nc [-h, --help] [-l, --log log-file]\n"
52 "                    [-s, --start-daemon] [-V, --version]\n");
53 }
54
55 /**
56  * enum opt_value - Tri-state options variables.
57  */
58
59 enum opt_value {opt_undef = 0, opt_yes, opt_no};
60
61 /**
62  * struct opts - Values from command line options.
63  */
64
65 struct opts {
66         enum opt_value show_help;
67         const char *log_file;
68         enum opt_value start_daemon;
69         enum opt_value show_version;
70 };
71
72 /**
73  * opts_parse - Parse the command line options.
74  */
75
76 static int opts_parse(struct opts *opts, int argc, char *argv[])
77 {
78         static const struct option long_options[] = {
79                 {"help",         no_argument,       NULL, 'h'},
80                 {"log",          required_argument, NULL, 'l'},
81                 {"start-daemon", no_argument,       NULL, 's'},
82                 {"version",      no_argument,       NULL, 'V'},
83                 { NULL,          0,                 NULL, 0},
84         };
85         static const char short_options[] = "dhl:sV";
86         static const struct opts default_values = { 0 };
87
88         *opts = default_values;
89
90         while (1) {
91                 int c = getopt_long(argc, argv, short_options, long_options,
92                         NULL);
93
94                 if (c == EOF)
95                         break;
96
97                 switch (c) {
98                 case 'h':
99                         opts->show_help = opt_yes;
100                         break;
101                 case 'l':
102                         opts->log_file = optarg;
103                         break;
104                 case 's':
105                         opts->start_daemon = opt_yes;
106                         break;
107                 case 'V':
108                         opts->show_version = opt_yes;
109                         break;
110                 default:
111                         opts->show_help = opt_yes;
112                         return -1;
113                 }
114         }
115
116         return 0;
117 }
118
119 static char *default_log_filename(void)
120 {
121         const char *base = "/var/log/petitboot/petitboot-nc";
122         static char name[PATH_MAX];
123         char *tty;
124         int i;
125
126         tty = ttyname(STDIN_FILENO);
127
128         /* strip /dev/ */
129         if (tty && !strncmp(tty, "/dev/", 5))
130                 tty += 5;
131
132         /* change slashes to hyphens */
133         for (i = 0; tty && tty[i]; i++)
134                 if (tty[i] == '/')
135                         tty[i] = '-';
136
137         if (!tty || !*tty)
138                 tty = "unknown";
139
140         snprintf(name, sizeof(name), "%s.%s.log", base, tty);
141
142         return name;
143 }
144 /**
145  * struct pb_cui - Main cui program instance.
146  * @mm: Main menu.
147  * @svm: Set video mode menu.
148  */
149
150 struct pb_cui {
151         struct pmenu *mm;
152         struct cui *cui;
153 };
154
155 static int pmenu_sysinfo(struct pmenu_item *item)
156 {
157         cui_show_sysinfo(cui_from_item(item));
158         return 0;
159 }
160
161 static int pmenu_config(struct pmenu_item *item)
162 {
163         cui_show_config(cui_from_item(item));
164         return 0;
165 }
166
167 static int pmenu_reinit(struct pmenu_item *item)
168 {
169         cui_send_reinit(cui_from_item(item));
170         return 0;
171 }
172
173 /**
174  * pb_mm_init - Setup the main menu instance.
175  */
176
177 static struct pmenu *pb_mm_init(struct pb_cui *pb_cui)
178 {
179         int result;
180         struct pmenu *m;
181         struct pmenu_item *i;
182
183         m = pmenu_init(pb_cui->cui, 5, cui_on_exit);
184
185         if (!m) {
186                 pb_log("%s: failed\n", __func__);
187                 return NULL;
188         }
189
190         m->on_new = cui_item_new;
191
192         m->scr.frame.ltitle = talloc_asprintf(m,
193                 "Petitboot (" PACKAGE_VERSION ")");
194         m->scr.frame.rtitle = NULL;
195         m->scr.frame.help = talloc_strdup(m,
196                 "Enter=accept, e=edit, n=new, x=exit, h=help");
197         m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
198
199         i = pmenu_item_init(m, 0, " ");
200         item_opts_off(i->nci, O_SELECTABLE);
201         i = pmenu_item_init(m, 1, "System information");
202         i->on_execute = pmenu_sysinfo;
203         i = pmenu_item_init(m, 2, "System configuration");
204         i->on_execute = pmenu_config;
205         i = pmenu_item_init(m, 3, "Rescan devices");
206         i->on_execute = pmenu_reinit;
207         i = pmenu_item_init(m, 4, "Exit to shell");
208         i->on_execute = pmenu_exit_cb;
209
210         result = pmenu_setup(m);
211
212         if (result) {
213                 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
214                         strerror(errno));
215                 goto fail_setup;
216         }
217
218         m->help_title = "main menu";
219         m->help_text = main_menu_help_text;
220
221         menu_opts_off(m->ncm, O_SHOWDESC);
222         set_menu_mark(m->ncm, " *");
223         set_current_item(m->ncm, i->nci);
224
225         return m;
226
227 fail_setup:
228         talloc_free(m);
229         return NULL;
230 }
231
232 static struct pb_cui pb;
233
234 static void sig_handler(int signum)
235 {
236         DBGS("%d\n", signum);
237
238         switch (signum) {
239         case SIGWINCH:
240                 if (pb.cui)
241                         cui_resize(pb.cui);
242                 break;
243         default:
244                 assert(0 && "unknown sig");
245                 /* fall through */
246         case SIGINT:
247         case SIGHUP:
248         case SIGTERM:
249                 if (pb.cui)
250                         cui_abort(pb.cui);
251                 break;
252         }
253 }
254
255 /**
256  * main - cui bootloader main routine.
257  */
258
259 int main(int argc, char *argv[])
260 {
261         static struct sigaction sa;
262         const char *log_filename;
263         int result;
264         int cui_result;
265         struct opts opts;
266         FILE *log;
267
268         result = opts_parse(&opts, argc, argv);
269
270         if (result) {
271                 print_usage();
272                 return EXIT_FAILURE;
273         }
274
275         if (opts.show_help == opt_yes) {
276                 print_usage();
277                 return EXIT_SUCCESS;
278         }
279
280         if (opts.show_version == opt_yes) {
281                 print_version();
282                 return EXIT_SUCCESS;
283         }
284
285         if (opts.log_file)
286                 log_filename = opts.log_file;
287         else
288                 log_filename = default_log_filename();
289
290         log = stderr;
291         if (strcmp(log_filename, "-")) {
292                 log = fopen(log_filename, "a");
293
294                 if (!log)
295                         log = fopen("/dev/null", "a");
296         }
297
298         pb_log_init(log);
299
300         pb_log("--- petitboot-nc ---\n");
301
302         sa.sa_handler = sig_handler;
303         result = sigaction(SIGALRM, &sa, NULL);
304         result += sigaction(SIGHUP, &sa, NULL);
305         result += sigaction(SIGINT, &sa, NULL);
306         result += sigaction(SIGTERM, &sa, NULL);
307         result += sigaction(SIGWINCH, &sa, NULL);
308
309         if (result) {
310                 pb_log("%s sigaction failed.\n", __func__);
311                 return EXIT_FAILURE;
312         }
313
314         pb.cui = cui_init(&pb, NULL, opts.start_daemon);
315
316         if (!pb.cui)
317                 return EXIT_FAILURE;
318
319         pb.mm = pb_mm_init(&pb);
320
321         cui_result = cui_run(pb.cui, pb.mm, 0);
322
323         pmenu_delete(pb.mm);
324
325         talloc_free(pb.cui);
326
327         pb_log("--- end ---\n");
328
329         return cui_result ? EXIT_FAILURE : EXIT_SUCCESS;
330 }