]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-cui.c
2b059eb96b240a146667d686876322ccc18d2b4a
[petitboot] / ui / ncurses / nc-cui.c
1 /*
2  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  *  Copyright 2009 Sony Corp.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; version 2 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #define _GNU_SOURCE
20
21 #include <assert.h>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/ioctl.h>
26
27 #include "log/log.h"
28 #include "talloc/talloc.h"
29 #include "waiter/waiter.h"
30 #include "ui/common/discover-client.h"
31 #include "nc-cui.h"
32
33
34 static struct cui_opt_data *cod_from_item(struct pmenu_item *item)
35 {
36         return item->data;
37 }
38
39 /**
40  * cui_abort - Signal the main cui program loop to exit.
41  *
42  * Sets cui.abort, which causes the cui_run() routine to return.
43  */
44
45 void cui_abort(struct cui *cui)
46 {
47         pb_log("%s: exiting\n", __func__);
48         cui->abort = 1;
49 }
50
51 /**
52  * cui_resize - Signal the main cui program loop to resize
53  *
54  * Called at SIGWINCH.
55  */
56
57 void cui_resize(struct cui *cui)
58 {
59         pb_log("%s: resizing\n", __func__);
60         cui->resize = 1;
61 }
62
63 /**
64  * cui_on_exit - A generic main menu ESC callback.
65  */
66
67 void cui_on_exit(struct pmenu *menu)
68 {
69         cui_abort(cui_from_pmenu(menu));
70 }
71
72 /**
73  * cui_run_cmd - A generic cb to run the supplied command.
74  */
75
76 int cui_run_cmd(struct pmenu_item *item)
77 {
78         int result;
79         struct cui *cui = cui_from_item(item);
80         const char *const *cmd_argv = item->data;
81
82         nc_scr_status_printf(cui->current, "Running %s...", cmd_argv[0]);
83
84         def_prog_mode();
85
86         result = pb_run_cmd(cmd_argv);
87
88         reset_prog_mode();
89         redrawwin(cui->current->main_ncw);
90
91         if (result) {
92                 pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
93                 nc_scr_status_printf(cui->current, "Failed: %s", cmd_argv[0]);
94         }
95
96         return result;
97 }
98
99 /**
100  * cui_run_kexec - A generic cb to run kexec.
101  */
102
103 static int cui_run_kexec(struct pmenu_item *item)
104 {
105         int result;
106         struct cui *cui = cui_from_item(item);
107         struct cui_opt_data *cod = cod_from_item(item);
108
109         assert(cui->current == &cui->main->scr);
110         assert(cui->on_kexec);
111
112         pb_log("%s: %s\n", __func__, cod->dev->name, cod->opt->name);
113         nc_scr_status_printf(cui->current, "Booting %s...", cod->opt->name);
114
115         def_prog_mode();
116
117         result = cui->on_kexec(cui, cod);
118
119         reset_prog_mode();
120         redrawwin(cui->current->main_ncw);
121
122         if (!result) {
123                 clear();
124                 mvaddstr(1, 0, "system is going down now...");
125                 refresh();
126                 sleep(60);
127         }
128
129         pb_log("%s: failed: %s\n", __func__, cod->kd->image);
130         nc_scr_status_printf(cui->current, "Failed: kexec %s", cod->kd->image);
131
132         return 0;
133 }
134
135 /**
136  * cui_ked_on_exit - The ked on_exit callback.
137  */
138
139 static void cui_ked_on_exit(struct ked *ked, enum ked_result ked_result,
140         struct pb_kexec_data *kd)
141 {
142         struct cui *cui = cui_from_arg(ked->scr.ui_ctx);
143
144         if (ked_result == ked_update || ked_result == ked_boot) {
145                 struct pmenu_item *i = pmenu_find_selected(cui->main);
146                 struct cui_opt_data *cod = cod_from_item(i);
147
148                 assert(kd);
149
150                 talloc_steal(i, kd);
151                 talloc_free(cod->kd);
152                 cod->kd = kd;
153
154                 pb_log("%s: updating opt '%s'\n", __func__, cod->opt->name);
155                 pb_log(" image  '%s'\n", cod->kd->image);
156                 pb_log(" initrd '%s'\n", cod->kd->initrd);
157                 pb_log(" args   '%s'\n", cod->kd->args);
158         }
159
160         cui_set_current(cui, &cui->main->scr);
161
162         talloc_free(ked);
163
164         if (ked_result == ked_boot) {
165                 struct pmenu_item *i = pmenu_find_selected(cui->main);
166                 i->on_execute(i);
167         }
168 }
169
170 int cui_ked_run(struct pmenu_item *item)
171 {
172         struct cui *cui = cui_from_item(item);
173         struct ked *ked;
174
175         ked = ked_init(cui, cod_from_item(item)->kd, cui_ked_on_exit);
176
177         cui_set_current(cui, &ked->scr);
178
179         return 0;
180 }
181
182 /**
183  * cui_set_current - Set the currently active screen and redraw it.
184  */
185
186 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
187 {
188         struct nc_scr *old;
189
190         DBGS("%p -> %p\n", cui->current, scr);
191
192         assert(cui->current != scr);
193
194         old = cui->current;
195         old->unpost(old);
196
197         cui->current = scr;
198         cui->current->post(cui->current);
199
200         return old;
201 }
202
203 static int cui_process_key(void *arg)
204 {
205         struct cui *cui = cui_from_arg(arg);
206
207         assert(cui->current);
208         cui->current->process_key(cui->current);
209         return 0;
210 }
211
212 /**
213  * cui_client_process_socket - Process a socket event from the discover server.
214  */
215
216 static int cui_client_process_socket(void *arg)
217 {
218         struct discover_client *client = arg;
219
220         discover_client_process(client);
221         return 0;
222 }
223
224 /**
225  * cui_handle_resize - Handle the term resize.
226  */
227
228 static void cui_handle_resize(struct cui *cui)
229 {
230         struct winsize ws;
231
232         if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
233                 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
234                 return;
235         }
236
237         pb_log("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
238
239         wclear(cui->current->main_ncw);
240         resize_term(ws.ws_row, ws.ws_col);
241         cui->current->resize(cui->current);
242
243         /* For some reason this makes ncurses redraw the screen */
244         getch();
245         redrawwin(cui->current->main_ncw);
246         wrefresh(cui->current->main_ncw);
247 }
248
249 /**
250  * cui_device_add - Client device_add callback.
251  *
252  * Creates menu_items for all the device boot_options and inserts those
253  * menu_items into the main menu.  Redraws the main menu if it is active.
254  */
255
256 static int cui_device_add(struct device *dev, void *arg)
257 {
258         struct cui *cui = cui_from_arg(arg);
259         int result;
260         struct boot_option *opt;
261         unsigned int o_count; /* device opts */
262         unsigned int insert_pt;
263         ITEM *selected;
264
265         pb_log("%s: %p %s\n", __func__, dev, dev->id);
266
267         selected = current_item(cui->main->ncm);
268
269         if (cui->current == &cui->main->scr)
270                 cui->current->unpost(cui->current);
271
272         /* This disconnects items array from menu. */
273
274         result = set_menu_items(cui->main->ncm, NULL);
275
276         if (result)
277                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
278
279         o_count = 0;
280         list_for_each_entry(&dev->boot_options, opt, list)
281                 o_count++;
282
283         /* Insert new items at insert_pt. */
284
285         insert_pt = pmenu_grow(cui->main, o_count);
286
287         list_for_each_entry(&dev->boot_options, opt, list) {
288                 struct pmenu_item *i;
289                 struct cui_opt_data *cod;
290                 char *name;
291                 char *description;
292
293                 /* Save the item in opt->ui_info for cui_device_remove() */
294
295                 opt->ui_info = i = pmenu_item_alloc(cui->main);
296
297                 name = talloc_asprintf(i, "%s: %s", opt->name,
298                         opt->description);
299
300                 description = talloc_asprintf(i,
301                         " kexec: image='%s' initrd='%s' args='%s'",
302                         opt->boot_image_file,
303                         opt->initrd_file,
304                         opt->boot_args);
305
306                 pmenu_item_setup(cui->main, i, insert_pt, name, description);
307
308                 i->on_edit = cui_ked_run;
309                 i->on_execute = cui_run_kexec;
310                 i->data = cod = talloc(i, struct cui_opt_data);
311
312                 cod->dev = dev;
313                 cod->opt = opt;
314                 cod->opt_hash = pb_opt_hash(dev, opt);
315                 cod->kd = talloc(i, struct pb_kexec_data);
316
317                 cod->kd->image = talloc_strdup(cod->kd, opt->boot_image_file);
318                 cod->kd->initrd = talloc_strdup(cod->kd, opt->initrd_file);
319                 cod->kd->args = talloc_strdup(cod->kd, opt->boot_args);
320
321                 insert_pt++;
322
323                 /* If this is the default_item select it. */
324
325                 if (cod->opt_hash == cui->default_item)
326                         selected = i->nci;
327
328                 pb_log("%s: adding opt '%s'\n", __func__, cod->opt->name);
329                 pb_log("   image  '%s'\n", cod->kd->image);
330                 pb_log("   initrd '%s'\n", cod->kd->initrd);
331                 pb_log("   args   '%s'\n", cod->kd->args);
332         }
333
334         /* Re-attach the items array. */
335
336         result = set_menu_items(cui->main->ncm, cui->main->items);
337
338         if (result)
339                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
340
341         if (0) {
342                 pb_log("%s\n", __func__);
343                 pmenu_dump_items(cui->main->items,
344                         item_count(cui->main->ncm) + 1);
345         }
346
347         set_current_item(cui->main->ncm, selected);
348
349         if (cui->current == &cui->main->scr)
350                 cui->current->post(cui->current);
351
352         return 0;
353 }
354
355 /**
356  * cui_device_remove - Client device remove callback.
357  *
358  * Removes all the menu_items for the device from the main menu and redraws the
359  * main menu if it is active.
360  */
361
362 static void cui_device_remove(struct device *dev, void *arg)
363 {
364         struct cui *cui = cui_from_arg(arg);
365         int result;
366         struct boot_option *opt;
367
368         pb_log("%s: %p %s\n", __func__, dev, dev->id);
369
370         if (cui->current == &cui->main->scr)
371                 cui->current->unpost(cui->current);
372
373         /* This disconnects items array from menu. */
374
375         result = set_menu_items(cui->main->ncm, NULL);
376
377         if (result)
378                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
379
380         list_for_each_entry(&dev->boot_options, opt, list) {
381                 struct pmenu_item *i = pmenu_item_from_arg(opt->ui_info);
382
383                 assert(pb_protocol_device_cmp(dev, cod_from_item(i)->dev));
384                 pmenu_remove(cui->main, i);
385         }
386
387         /* Re-attach the items array. */
388
389         result = set_menu_items(cui->main->ncm, cui->main->items);
390
391         if (result)
392                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
393
394         if (0) {
395                 pb_log("%s\n", __func__);
396                 pmenu_dump_items(cui->main->items,
397                         item_count(cui->main->ncm) + 1);
398         }
399
400         if (cui->current == &cui->main->scr)
401                 cui->current->post(cui->current);
402 }
403
404 static struct discover_client_ops cui_client_ops = {
405         .device_add = cui_device_add,
406         .device_remove = cui_device_remove,
407 };
408
409 /**
410  * cui_init - Setup the cui instance.
411  * @platform_info: A value for the struct cui platform_info member.
412  *
413  * Returns a pointer to a struct cui on success, or NULL on error.
414  *
415  * Allocates the cui instance, sets up the client and stdin waiters, and
416  * sets up the ncurses menu screen.
417  */
418
419 struct cui *cui_init(void* platform_info,
420         int (*on_kexec)(struct cui *, struct cui_opt_data *))
421 {
422         struct cui *cui;
423         struct discover_client *client;
424         unsigned int i;
425
426         cui = talloc_zero(NULL, struct cui);
427
428         if (!cui) {
429                 pb_log("%s: alloc cui failed.\n", __func__);
430                 fprintf(stderr, "%s: alloc cui failed.\n", __func__);
431                 goto fail_alloc;
432         }
433
434         cui->c_sig = pb_cui_sig;
435         cui->platform_info = platform_info;
436         cui->on_kexec = on_kexec;
437
438         /* Loop here for scripts that just started the server. */
439
440         for (i = 10; i; i--) {
441                 client = discover_client_init(&cui_client_ops, cui);
442                 if (client)
443                         break;
444                 pb_log("%s: waiting for server %d\n", __func__, i);
445                 sleep(1);
446         }
447
448         if (!client) {
449                 pb_log("%s: discover_client_init failed.\n", __func__);
450                 fprintf(stderr, "%s: error: discover_client_init failed.\n",
451                         __func__);
452                 fprintf(stderr, "check that pb-discover, "
453                         "the petitboot daemon is running.\n");
454                 goto fail_client_init;
455         }
456
457         atexit(nc_atexit);
458         nc_start();
459
460         waiter_register(discover_client_get_fd(client), WAIT_IN,
461                 cui_client_process_socket, client);
462
463         waiter_register(STDIN_FILENO, WAIT_IN, cui_process_key, cui);
464
465         return cui;
466
467 fail_client_init:
468         talloc_free(cui);
469 fail_alloc:
470         return NULL;
471 }
472
473 /**
474  * cui_run - The main cui program loop.
475  * @cui: The cui instance.
476  * @main: The menu to use as the main menu.
477  *
478  * Runs the cui engine.  Does not return until indicated to do so by some
479  * user action, or an error occurs.  Frees the cui object on return.
480  * Returns 0 on success (return to shell), -1 on error (should restart).
481  */
482
483 int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
484 {
485         assert(main);
486
487         cui->main = main;
488         cui->current = &cui->main->scr;
489         cui->default_item = default_item;
490
491         cui->current->post(cui->current);
492
493         while (1) {
494                 int result = waiter_poll();
495
496                 if (result < 0 && errno != EINTR) {
497                         pb_log("%s: poll: %s\n", __func__, strerror(errno));
498                         break;
499                 }
500
501                 if (cui->abort)
502                         break;
503
504                 while (cui->resize) {
505                         cui->resize = 0;
506                         cui_handle_resize(cui);
507                 }
508         }
509
510         nc_atexit();
511
512         return cui->abort ? 0 : -1;
513 }