]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-cui.c
ui: pb_kexec_data -> pb_boot_data
[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 static struct cui_opt_data *cod_from_item(struct pmenu_item *item)
34 {
35         return item->data;
36 }
37
38 /**
39  * cui_abort - Signal the main cui program loop to exit.
40  *
41  * Sets cui.abort, which causes the cui_run() routine to return.
42  */
43
44 void cui_abort(struct cui *cui)
45 {
46         pb_log("%s: exiting\n", __func__);
47         cui->abort = 1;
48 }
49
50 /**
51  * cui_resize - Signal the main cui program loop to resize
52  *
53  * Called at SIGWINCH.
54  */
55
56 void cui_resize(struct cui *cui)
57 {
58         pb_log("%s: resizing\n", __func__);
59         cui->resize = 1;
60 }
61
62 /**
63  * cui_make_item_name - Format the menu item name srting.
64  *
65  * Returns a talloc string.
66  */
67
68 static char *cui_make_item_name(struct pmenu_item *i, struct cui_opt_data *cod)
69 {
70         char *name;
71
72         assert(cod->name);
73         assert(cod->bd);
74
75         name = talloc_asprintf(i, "%s:", cod->name);
76
77         if (cod->bd->image)
78                 name = talloc_asprintf_append(name, " %s", cod->bd->image);
79
80         if (cod->bd->initrd)
81                 name = talloc_asprintf_append(name, " initrd=%s",
82                         cod->bd->initrd);
83
84         if (cod->bd->args)
85                 name = talloc_asprintf_append(name, " %s", cod->bd->args);
86
87         DBGS("@%s@\n", name);
88         return name;
89 }
90
91 /**
92  * cui_on_exit - A generic main menu ESC callback.
93  */
94
95 void cui_on_exit(struct pmenu *menu)
96 {
97         cui_abort(cui_from_pmenu(menu));
98 }
99
100 /**
101  * cui_run_cmd - A generic cb to run the supplied command.
102  */
103
104 int cui_run_cmd(struct pmenu_item *item)
105 {
106         int result;
107         struct cui *cui = cui_from_item(item);
108         const char *const *cmd_argv = item->data;
109
110         nc_scr_status_printf(cui->current, "Running %s...", cmd_argv[0]);
111
112         def_prog_mode();
113
114         result = pb_run_cmd(cmd_argv, 1, 0);
115
116         reset_prog_mode();
117         redrawwin(cui->current->main_ncw);
118
119         if (result) {
120                 pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
121                 nc_scr_status_printf(cui->current, "Failed: %s", cmd_argv[0]);
122         }
123
124         return result;
125 }
126
127 /**
128  * cui_run_kexec - A generic cb to run kexec.
129  */
130
131 static int cui_run_kexec(struct pmenu_item *item)
132 {
133         int result;
134         struct cui *cui = cui_from_item(item);
135         struct cui_opt_data *cod = cod_from_item(item);
136
137         assert(cui->current == &cui->main->scr);
138         assert(cui->on_kexec);
139
140         pb_log("%s: %s\n", __func__, cod->name);
141         nc_scr_status_printf(cui->current, "Booting %s...", cod->name);
142
143         def_prog_mode();
144
145         result = cui->on_kexec(cui, cod);
146
147         reset_prog_mode();
148         redrawwin(cui->current->main_ncw);
149
150         if (!result) {
151                 clear();
152                 mvaddstr(1, 0, "system is going down now...");
153                 refresh();
154                 sleep(cui->dry_run ? 1 : 60);
155         }
156
157         pb_log("%s: failed: %s\n", __func__, cod->bd->image);
158         nc_scr_status_printf(cui->current, "Failed: kexec %s", cod->bd->image);
159
160         return 0;
161 }
162
163 /**
164  * cui_boot_editor_on_exit - The boot_editor on_exit callback.
165  */
166
167 static void cui_boot_editor_on_exit(struct boot_editor *boot_editor, enum boot_editor_result boot_editor_result,
168         struct pb_boot_data *bd)
169 {
170         struct cui *cui = cui_from_arg(boot_editor->scr.ui_ctx);
171
172         if (boot_editor_result == boot_editor_update) {
173                 struct pmenu_item *i = pmenu_find_selected(cui->main);
174                 struct cui_opt_data *cod = cod_from_item(i);
175                 char *name;
176
177                 assert(bd);
178
179                 talloc_steal(i, bd);
180                 talloc_free(cod->bd);
181                 cod->bd = bd;
182
183                 name = cui_make_item_name(i, cod);
184                 pmenu_item_replace(i, name);
185
186                 /* FIXME: need to make item visible somehow */
187                 set_current_item(cui->main->ncm, i->nci);
188
189                 pb_log("%s: updating opt '%s'\n", __func__, cod->name);
190                 pb_log(" image  '%s'\n", cod->bd->image);
191                 pb_log(" initrd '%s'\n", cod->bd->initrd);
192                 pb_log(" args   '%s'\n", cod->bd->args);
193         }
194
195         cui_set_current(cui, &cui->main->scr);
196
197         talloc_free(boot_editor);
198 }
199
200 int cui_boot_editor_run(struct pmenu_item *item)
201 {
202         struct cui *cui = cui_from_item(item);
203         struct cui_opt_data *cod = cod_from_item(item);
204         struct boot_editor *boot_editor;
205
206         boot_editor = boot_editor_init(cui, cod->bd, cui_boot_editor_on_exit);
207         cui_set_current(cui, &boot_editor->scr);
208
209         return 0;
210 }
211
212 /**
213  * cui_set_current - Set the currently active screen and redraw it.
214  */
215
216 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
217 {
218         struct nc_scr *old;
219
220         DBGS("%p -> %p\n", cui->current, scr);
221
222         assert(cui->current != scr);
223
224         old = cui->current;
225         old->unpost(old);
226
227         cui->current = scr;
228         cui->current->post(cui->current);
229
230         return old;
231 }
232
233 /**
234  * cui_process_key - Process input on stdin.
235  */
236
237 static int cui_process_key(void *arg)
238 {
239         struct cui *cui = cui_from_arg(arg);
240
241         assert(cui->current);
242
243         ui_timer_disable(&cui->timer);
244         cui->current->process_key(cui->current);
245
246         return 0;
247 }
248
249 /**
250  * cui_process_js - Process joystick events.
251  */
252
253 static int cui_process_js(void *arg)
254 {
255         struct cui *cui = cui_from_arg(arg);
256         int c;
257
258         c = pjs_process_event(cui->pjs);
259
260         if (c) {
261                 ungetch(c);
262                 cui_process_key(arg);
263         }
264
265         return 0;
266 }
267 /**
268  * cui_client_process_socket - Process a socket event from the discover server.
269  */
270
271 static int cui_client_process_socket(void *arg)
272 {
273         struct discover_client *client = arg;
274
275         discover_client_process(client);
276         return 0;
277 }
278
279 /**
280  * cui_handle_timeout - Handle the timeout.
281  */
282
283 static void cui_handle_timeout(struct ui_timer *timer)
284 {
285         struct cui *cui = cui_from_timer(timer);
286         struct pmenu_item *i = pmenu_find_selected(cui->main);
287
288 #if defined(DEBUG)
289         {
290                 struct cui_opt_data *cod = cod_from_item(i);
291                 assert(cod && (cod->opt_hash == cui->default_item));
292         }
293 #endif
294         i->on_execute(i);
295 }
296
297 /**
298  * cui_handle_resize - Handle the term resize.
299  */
300
301 static void cui_handle_resize(struct cui *cui)
302 {
303         struct winsize ws;
304
305         if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
306                 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
307                 return;
308         }
309
310         pb_log("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
311
312         wclear(cui->current->main_ncw);
313         resize_term(ws.ws_row, ws.ws_col);
314         cui->current->resize(cui->current);
315
316         /* For some reason this makes ncurses redraw the screen */
317         getch();
318         redrawwin(cui->current->main_ncw);
319         wrefresh(cui->current->main_ncw);
320 }
321
322 /**
323  * cui_on_open - Open new item callback.
324  */
325
326 void cui_on_open(struct pmenu *menu)
327 {
328         unsigned int insert_pt;
329         struct pmenu_item *i;
330         struct cui_opt_data *cod;
331
332         menu->scr.unpost(&menu->scr);
333
334         /* This disconnects items array from menu. */
335
336         set_menu_items(menu->ncm, NULL);
337
338         /* Insert new items at insert_pt. */
339
340         insert_pt = pmenu_grow(menu, 1);
341         i = pmenu_item_alloc(menu);
342
343         i->on_edit = cui_boot_editor_run;
344         i->on_execute = cui_run_kexec;
345         i->data = cod = talloc_zero(i, struct cui_opt_data);
346
347         cod->name = talloc_asprintf(i, "User item %u:", insert_pt);
348         cod->bd = talloc_zero(i, struct pb_boot_data);
349
350         pmenu_item_setup(menu, i, insert_pt, talloc_strdup(i, cod->name));
351
352         /* Re-attach the items array. */
353
354         set_menu_items(menu->ncm, menu->items);
355
356         menu->scr.post(&menu->scr);
357         set_current_item(menu->ncm, i->nci);
358
359         i->on_edit(i);
360 }
361
362 /**
363  * cui_device_add - Client device_add callback.
364  *
365  * Creates menu_items for all the device boot_options and inserts those
366  * menu_items into the main menu.  Redraws the main menu if it is active.
367  */
368
369 static int cui_device_add(struct device *dev, void *arg)
370 {
371         struct cui *cui = cui_from_arg(arg);
372         int result;
373         struct boot_option *opt;
374         unsigned int o_count; /* device opts */
375         unsigned int insert_pt;
376         ITEM *selected;
377
378         pb_log("%s: %p %s\n", __func__, dev, dev->id);
379
380         selected = current_item(cui->main->ncm);
381
382         if (cui->current == &cui->main->scr)
383                 cui->current->unpost(cui->current);
384
385         /* This disconnects items array from menu. */
386
387         result = set_menu_items(cui->main->ncm, NULL);
388
389         if (result)
390                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
391
392         o_count = 0;
393         list_for_each_entry(&dev->boot_options, opt, list)
394                 o_count++;
395
396         /* Insert new items at insert_pt. */
397
398         insert_pt = pmenu_grow(cui->main, o_count);
399
400         list_for_each_entry(&dev->boot_options, opt, list) {
401                 struct pmenu_item *i;
402                 struct cui_opt_data *cod;
403                 char *name;
404
405                 /* Save the item in opt->ui_info for cui_device_remove() */
406
407                 opt->ui_info = i = pmenu_item_alloc(cui->main);
408
409                 i->on_edit = cui_boot_editor_run;
410                 i->on_execute = cui_run_kexec;
411                 i->data = cod = talloc(i, struct cui_opt_data);
412
413                 cod->dev = dev;
414                 cod->opt = opt;
415                 cod->opt_hash = pb_opt_hash(dev, opt);
416                 cod->name = opt->name;
417                 cod->bd = talloc(i, struct pb_boot_data);
418
419                 cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
420                 cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
421                 cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
422
423                 name = cui_make_item_name(i, cod);
424                 pmenu_item_setup(cui->main, i, insert_pt, name);
425
426                 insert_pt++;
427
428                 pb_log("%s: adding opt '%s'\n", __func__, cod->name);
429                 pb_log("   image  '%s'\n", cod->bd->image);
430                 pb_log("   initrd '%s'\n", cod->bd->initrd);
431                 pb_log("   args   '%s'\n", cod->bd->args);
432
433                 /* If this is the default_item select it and start timer. */
434
435                 if (cod->opt_hash == cui->default_item) {
436                         selected = i->nci;
437                         ui_timer_kick(&cui->timer);
438                 }
439         }
440
441         /* Re-attach the items array. */
442
443         result = set_menu_items(cui->main->ncm, cui->main->items);
444
445         if (result)
446                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
447
448         if (0) {
449                 pb_log("%s\n", __func__);
450                 pmenu_dump_items(cui->main->items,
451                         item_count(cui->main->ncm) + 1);
452         }
453
454         /* FIXME: need to make item visible somehow */
455         menu_driver(cui->main->ncm, REQ_SCR_UPAGE);
456         menu_driver(cui->main->ncm, REQ_SCR_DPAGE);
457         set_current_item(cui->main->ncm, selected);
458
459         if (cui->current == &cui->main->scr)
460                 cui->current->post(cui->current);
461
462         return 0;
463 }
464
465 /**
466  * cui_device_remove - Client device remove callback.
467  *
468  * Removes all the menu_items for the device from the main menu and redraws the
469  * main menu if it is active.
470  */
471
472 static void cui_device_remove(struct device *dev, void *arg)
473 {
474         struct cui *cui = cui_from_arg(arg);
475         int result;
476         struct boot_option *opt;
477
478         pb_log("%s: %p %s\n", __func__, dev, dev->id);
479
480         if (cui->current == &cui->main->scr)
481                 cui->current->unpost(cui->current);
482
483         /* This disconnects items array from menu. */
484
485         result = set_menu_items(cui->main->ncm, NULL);
486
487         if (result)
488                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
489
490         list_for_each_entry(&dev->boot_options, opt, list) {
491                 struct pmenu_item *i = pmenu_item_from_arg(opt->ui_info);
492                 struct cui_opt_data *cod = cod_from_item(i);
493
494                 assert(pb_protocol_device_cmp(dev, cod->dev));
495                 pmenu_remove(cui->main, i);
496
497                 /* If this is the default_item disable timer. */
498
499                 if (cod->opt_hash == cui->default_item)
500                         ui_timer_disable(&cui->timer);
501         }
502
503         /* Re-attach the items array. */
504
505         result = set_menu_items(cui->main->ncm, cui->main->items);
506
507         if (result)
508                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
509
510         if (0) {
511                 pb_log("%s\n", __func__);
512                 pmenu_dump_items(cui->main->items,
513                         item_count(cui->main->ncm) + 1);
514         }
515
516         if (cui->current == &cui->main->scr)
517                 cui->current->post(cui->current);
518 }
519
520 static struct discover_client_ops cui_client_ops = {
521         .device_add = cui_device_add,
522         .device_remove = cui_device_remove,
523 };
524
525 /**
526  * cui_init - Setup the cui instance.
527  * @platform_info: A value for the struct cui platform_info member.
528  *
529  * Returns a pointer to a struct cui on success, or NULL on error.
530  *
531  * Allocates the cui instance, sets up the client and stdin waiters, and
532  * sets up the ncurses menu screen.
533  */
534
535 struct cui *cui_init(void* platform_info,
536         int (*on_kexec)(struct cui *, struct cui_opt_data *),
537         int (*js_map)(const struct js_event *e), int start_deamon, int dry_run)
538 {
539         struct cui *cui;
540         struct discover_client *client;
541         unsigned int i;
542
543         cui = talloc_zero(NULL, struct cui);
544
545         if (!cui) {
546                 pb_log("%s: alloc cui failed.\n", __func__);
547                 fprintf(stderr, "%s: alloc cui failed.\n", __func__);
548                 goto fail_alloc;
549         }
550
551         cui->c_sig = pb_cui_sig;
552         cui->platform_info = platform_info;
553         cui->on_kexec = on_kexec;
554         cui->timer.handle_timeout = cui_handle_timeout;
555         cui->dry_run = dry_run;
556         cui->waitset = waitset_create(cui);
557
558         /* Loop here for scripts that just started the server. */
559
560 retry_start:
561         for (i = start_deamon ? 2 : 10; i; i--) {
562                 client = discover_client_init(&cui_client_ops, cui);
563                 if (client || !i)
564                         break;
565                 pb_log("%s: waiting for server %d\n", __func__, i);
566                 sleep(1);
567         }
568
569         if (!client && start_deamon) {
570                 int result;
571
572                 start_deamon = 0;
573
574                 result = pb_start_daemon();
575
576                 if (!result)
577                         goto retry_start;
578
579                 pb_log("%s: discover_client_init failed.\n", __func__);
580                 fprintf(stderr, "%s: error: discover_client_init failed.\n",
581                         __func__);
582                 fprintf(stderr, "could not start pb-discover, the petitboot "
583                         "daemon.\n");
584                 goto fail_client_init;
585         }
586
587         if (!client) {
588                 pb_log("%s: discover_client_init failed.\n", __func__);
589                 fprintf(stderr, "%s: error: discover_client_init failed.\n",
590                         __func__);
591                 fprintf(stderr, "check that pb-discover, "
592                         "the petitboot daemon is running.\n");
593                 goto fail_client_init;
594         }
595
596         atexit(nc_atexit);
597         nc_start();
598
599         waiter_register(cui->waitset, discover_client_get_fd(client), WAIT_IN,
600                         cui_client_process_socket, client);
601
602         waiter_register(cui->waitset, STDIN_FILENO, WAIT_IN,
603                         cui_process_key, cui);
604
605         if (js_map) {
606
607                 cui->pjs = pjs_init(cui, js_map);
608
609                 if (cui->pjs)
610                         waiter_register(cui->waitset, pjs_get_fd(cui->pjs),
611                                         WAIT_IN, cui_process_js, cui);
612         }
613
614         return cui;
615
616 fail_client_init:
617         talloc_free(cui);
618 fail_alloc:
619         return NULL;
620 }
621
622 /**
623  * cui_run - The main cui program loop.
624  * @cui: The cui instance.
625  * @main: The menu to use as the main menu.
626  *
627  * Runs the cui engine.  Does not return until indicated to do so by some
628  * user action, or an error occurs.  Frees the cui object on return.
629  * Returns 0 on success (return to shell), -1 on error (should restart).
630  */
631
632 int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
633 {
634         assert(main);
635
636         cui->main = main;
637         cui->current = &cui->main->scr;
638         cui->default_item = default_item;
639
640         cui->current->post(cui->current);
641
642         while (1) {
643                 int result = waiter_poll(cui->waitset);
644
645                 if (result < 0 && errno != EINTR) {
646                         pb_log("%s: poll: %s\n", __func__, strerror(errno));
647                         break;
648                 }
649
650                 if (cui->abort)
651                         break;
652
653                 ui_timer_process_sig(&cui->timer);
654
655                 while (cui->resize) {
656                         cui->resize = 0;
657                         cui_handle_resize(cui);
658                 }
659         }
660
661         nc_atexit();
662
663         return cui->abort ? 0 : -1;
664 }