]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/generic-main.c
Add BMC interface MAC to system info output
[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 #include <libintl.h>
34 #include <locale.h>
35
36 #include "log/log.h"
37 #include "talloc/talloc.h"
38 #include "waiter/waiter.h"
39 #include "i18n/i18n.h"
40 #include "ui/common/discover-client.h"
41 #include "nc-cui.h"
42
43 static void print_version(void)
44 {
45         printf("petitboot-nc (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
46 }
47
48 static void print_usage(void)
49 {
50         print_version();
51         printf(
52 "%s: petitboot-nc [-h, --help] [-l, --log log-file]\n"
53 "                    [-s, --start-daemon] [-v, --verbose] [-V, --version]\n",
54                         _("Usage"));
55 }
56
57 /**
58  * enum opt_value - Tri-state options variables.
59  */
60
61 enum opt_value {opt_undef = 0, opt_yes, opt_no};
62
63 /**
64  * struct opts - Values from command line options.
65  */
66
67 struct opts {
68         enum opt_value show_help;
69         const char *log_file;
70         enum opt_value start_daemon;
71         enum opt_value verbose;
72         enum opt_value show_version;
73 };
74
75 /**
76  * opts_parse - Parse the command line options.
77  */
78
79 static int opts_parse(struct opts *opts, int argc, char *argv[])
80 {
81         static const struct option long_options[] = {
82                 {"help",         no_argument,       NULL, 'h'},
83                 {"log",          required_argument, NULL, 'l'},
84                 {"start-daemon", no_argument,       NULL, 's'},
85                 {"verbose",      no_argument,       NULL, 'v'},
86                 {"version",      no_argument,       NULL, 'V'},
87                 { NULL,          0,                 NULL, 0},
88         };
89         static const char short_options[] = "dhl:svV";
90         static const struct opts default_values = { 0 };
91
92         *opts = default_values;
93
94         while (1) {
95                 int c = getopt_long(argc, argv, short_options, long_options,
96                         NULL);
97
98                 if (c == EOF)
99                         break;
100
101                 switch (c) {
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->verbose = opt_yes;
113                         break;
114                 case 'V':
115                         opts->show_version = opt_yes;
116                         break;
117                 default:
118                         opts->show_help = opt_yes;
119                         return -1;
120                 }
121         }
122
123         return 0;
124 }
125
126 static char *default_log_filename(void)
127 {
128         const char *base = "/var/log/petitboot/petitboot-nc";
129         static char name[PATH_MAX];
130         char *tty;
131         int i;
132
133         tty = ttyname(STDIN_FILENO);
134
135         /* strip /dev/ */
136         if (tty && !strncmp(tty, "/dev/", 5))
137                 tty += 5;
138
139         /* change slashes to hyphens */
140         for (i = 0; tty && tty[i]; i++)
141                 if (tty[i] == '/')
142                         tty[i] = '-';
143
144         if (!tty || !*tty)
145                 tty = "unknown";
146
147         snprintf(name, sizeof(name), "%s.%s.log", base, tty);
148
149         return name;
150 }
151
152 struct cui *cui;
153
154 /*
155  * struct pb_cui - Main cui program instance.
156  * @mm: Main menu.
157  * @svm: Set video mode menu.
158  */
159
160 static void sig_handler(int signum)
161 {
162         DBGS("%d\n", signum);
163
164         switch (signum) {
165         case SIGWINCH:
166                 if (cui)
167                         cui_resize(cui);
168                 break;
169         default:
170                 assert(0 && "unknown sig");
171                 /* fall through */
172         case SIGINT:
173         case SIGHUP:
174         case SIGTERM:
175                 if (cui)
176                         cui_abort(cui);
177                 break;
178         }
179 }
180
181 /**
182  * main - cui bootloader main routine.
183  */
184
185 int main(int argc, char *argv[])
186 {
187         static struct sigaction sa;
188         const char *log_filename;
189         int result;
190         int cui_result;
191         struct opts opts;
192         FILE *log;
193
194         result = opts_parse(&opts, argc, argv);
195
196         setlocale(LC_ALL, "");
197         bindtextdomain(PACKAGE, LOCALEDIR);
198         textdomain(PACKAGE);
199
200         if (result) {
201                 print_usage();
202                 return EXIT_FAILURE;
203         }
204
205         if (opts.show_help == opt_yes) {
206                 print_usage();
207                 return EXIT_SUCCESS;
208         }
209
210         if (opts.show_version == opt_yes) {
211                 print_version();
212                 return EXIT_SUCCESS;
213         }
214
215         if (opts.log_file)
216                 log_filename = opts.log_file;
217         else
218                 log_filename = default_log_filename();
219
220         log = stderr;
221         if (strcmp(log_filename, "-")) {
222                 log = fopen(log_filename, "a");
223
224                 if (!log)
225                         log = fopen("/dev/null", "a");
226         }
227
228         pb_log_init(log);
229
230         if (opts.verbose == opt_yes)
231                 pb_log_set_debug(true);
232
233         pb_log("--- petitboot-nc ---\n");
234
235         sa.sa_handler = sig_handler;
236         result = sigaction(SIGALRM, &sa, NULL);
237         result += sigaction(SIGHUP, &sa, NULL);
238         result += sigaction(SIGINT, &sa, NULL);
239         result += sigaction(SIGTERM, &sa, NULL);
240         result += sigaction(SIGWINCH, &sa, NULL);
241
242         if (result) {
243                 pb_log("%s sigaction failed.\n", __func__);
244                 return EXIT_FAILURE;
245         }
246
247         cui = cui_init(NULL, NULL, opts.start_daemon);
248         if (!cui)
249                 return EXIT_FAILURE;
250
251         cui_result = cui_run(cui);
252
253         talloc_free(cui);
254
255         pb_log("--- end ---\n");
256
257         return cui_result ? EXIT_FAILURE : EXIT_SUCCESS;
258 }