1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
|
/*
* userui_core.c - Core of all userspace user interfaces.
*
* Copyright (C) 2005, Bernard Blackham <bernard@blackham.com.au>
*
* This file is subject to the terms and conditions of the GNU General Public
* License v2. See the file COPYING in the main directory of this archive for
* more details.
*
*/
#define _GNU_SOURCE
#include <asm/types.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <linux/kd.h>
#include <linux/netlink.h>
#include <linux/vt.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include "userui.h"
#define PAGE_SIZE 0x1000
#define bail_err(x) do { fprintf(stderr, x": %s\n", strerror(errno)); fflush(stderr); abort(); } while (0)
#define bail(x...) do { fprintf(stderr, ## x); fflush(stderr); abort(); } while (0)
static struct termios termios_backup;
static int have_termios_backup = 0;
static int raw_keypresses = 0;
static int nlsock = -1;
static int test_run = 0;
static int running = 0;
static int need_cleanup = 0;
static int safe_to_exit = 1;
static volatile int need_loglevel_change = 0;
static int debugging_enabled = 0;
char software_suspend_version[32];
static FILE *printk_f = NULL;
static int saved_console_loglevel = -1;
volatile int console_loglevel = 1;
volatile int suspend_action = 0;
volatile int suspend_debug = 0;
extern struct userui_ops *userui_ops;
static int netlink_socket_num = 0;
static char* descriptions[] = {
"General",
"Freezer",
"Memory eating",
"Pageset",
"I/O",
"Bmap",
"Image header",
"Image writer",
"Memory",
"Extent",
"Spinlock",
"Memory pool",
"Range paranoia",
"Nosave",
"Integrity"
};
static void open_misc() {
if ((printk_f = fopen("/proc/sys/kernel/printk", "r+")) == NULL)
perror("open(/proc/sys/kernel/printk)");
else
setvbuf(printk_f, (char*)NULL, _IONBF, 0);
}
void set_console_loglevel(int exiting) {
if (!printk_f)
return;
fseek(printk_f, 0, SEEK_SET);
fprintf(printk_f, "%d\n", console_loglevel);
if (!exiting)
userui_ops->log_level_change();
}
void get_console_loglevel() {
if (!printk_f)
return;
fseek(printk_f, 0, SEEK_SET);
fscanf(printk_f, "%d", &console_loglevel);
}
int send_message(int type, void* buf, int len) {
struct nlmsghdr nl;
struct iovec iovec[2];
if (nlsock < 0)
return 0;
nl.nlmsg_len = NLMSG_LENGTH(len);
nl.nlmsg_type = type;
nl.nlmsg_flags = NLM_F_REQUEST;
nl.nlmsg_pid = getpid();
iovec[0].iov_base = &nl; iovec[0].iov_len = sizeof(nl);
iovec[1].iov_base = buf; iovec[1].iov_len = len;
if (writev(nlsock, iovec, (buf && len > 0)?2:1) == -1)
return 0;
return 1;
}
static void request_abort_suspend() {
if (test_run) {
exit(0);
}
send_message(USERUI_MSG_ABORT, NULL, 0);
}
static void toggle_reboot() {
suspend_action ^= (1 << SUSPEND_REBOOT);
send_message(USERUI_MSG_SET_STATE, (int*)&suspend_action, sizeof(suspend_action));
userui_ops->message(0, SUSPEND_UI_MSG, 1,
(suspend_action & (1 << SUSPEND_REBOOT) ?
"Rebooting enabled." :
"Rebooting disabled."));
}
static void toggle_debug_state(key) {
int bit = 0;
char message[80];
if (key >= 59 && key <= 68)
bit = key - 59;
if (key == 87 || key == 88)
bit = key - 77;
suspend_debug ^= (1 << bit);
send_message(USERUI_MSG_SET_DEBUG_STATE, (int*)&suspend_debug,
sizeof(suspend_debug));
sprintf(message, "%s messages %s", descriptions[bit],
(suspend_debug & (1 << bit)) ?
"enabled" : "disabled");
userui_ops->message(0, SUSPEND_UI_MSG, 1, message);
}
static void toggle_pause() {
if (!debugging_enabled)
return;
suspend_action ^= (1 << SUSPEND_PAUSE);
send_message(USERUI_MSG_SET_STATE, (int*)&suspend_action, sizeof(suspend_action));
userui_ops->message(0, SUSPEND_UI_MSG, 1,
(suspend_action & (1 << SUSPEND_PAUSE) ?
"Pause between steps enabled." :
"Pause between steps disabled."));
}
static void toggle_singlestep() {
if (!debugging_enabled)
return;
suspend_action ^= (1 << SUSPEND_SINGLESTEP);
send_message(USERUI_MSG_SET_STATE, (int*)&suspend_action, sizeof(suspend_action));
userui_ops->message(0, SUSPEND_UI_MSG, 1,
(suspend_action & (1 << SUSPEND_SINGLESTEP) ?
"Single stepping enabled." :
"Single stepping disabled."));
}
static void toggle_log_all() {
int temp = suspend_action | (1 << SUSPEND_LOGALL);
if (!debugging_enabled)
return;
/* Log this message always */
send_message(USERUI_MSG_SET_STATE, (int*)&temp, sizeof(temp));
suspend_action ^= (1 << SUSPEND_LOGALL);
userui_ops->message(0, SUSPEND_UI_MSG, 1,
(suspend_action & (1 << SUSPEND_LOGALL) ?
"Logging everything enabled." :
"Logging everything disabled."));
send_message(USERUI_MSG_SET_STATE, (int*)&suspend_action, sizeof(suspend_action));
}
static void notify_space_pressed() {
send_message(USERUI_MSG_SPACE, NULL, 0);
}
int common_keypress_handler(int key) {
switch (key) {
case 0x01: /* Escape */
request_abort_suspend();
break;
case 0x02: /* 1 */
case 0x03: /* 2 */
case 0x04: /* 3 */
case 0x05: /* 4 */
case 0x06: /* 5 */
case 0x07: /* 6 */
case 0x08: /* 7 */
case 0x09: /* 8 */
case 0x0a: /* 9 */
case 0x0b: /* 0 */
console_loglevel = (key - 1)%10;
set_console_loglevel(0);
break;
case 0x13: /* R */
toggle_reboot();
break;
case 0x19: /* P */
toggle_pause();
break;
case 0x1F: /* S */
toggle_singlestep();
break;
case 0x26: /* L */
toggle_log_all();
break;
case 0x39: /* Spacebar */
notify_space_pressed();
break;
case 0x3B: /* F1 */
case 0x3C: /* F2 */
case 0x3D: /* F3 */
case 0x3E: /* F4 */
case 0x3F: /* F5 */
case 0x40: /* F6 */
case 0x41: /* F7 */
case 0x42: /* F8 */
case 0x43: /* F9 */
case 0x44: /* F10 */
case 0x57: /* F11 */
case 0x58: /* F12 */
toggle_debug_state(key);
break;
default:
return 0;
}
return 1;
}
static void handle_params(int argc, char **argv) {
static char global_optstring[] = "htc:";
static struct option global_longopts[] = {
{"help", 0, 0, 'h'},
{"test", 0, 0, 't'},
{"channel", 1, 0, 'c'},
{NULL, 0, 0, 0},
};
int len;
char *optstring, *s1, *s2;
struct option *longopts, *o1, *o2;
int c;
/* Create optstring */
len = sizeof(global_optstring)-1;
if (userui_ops->optstring)
len += strlen(userui_ops->optstring);
len++; /* NULL terminator */
s1 = optstring = (char*)malloc(len);
for (s2 = global_optstring; (*s1++ = *s2++); );
if (userui_ops->optstring)
for (s1--, s2 = userui_ops->optstring; (*s1++ = *s2++););
/* Create longopts */
len = sizeof(global_longopts)/sizeof(global_longopts[0])-1;
if (userui_ops->longopts) {
o1 = userui_ops->longopts;
while ((o1++)->name) len++;
}
len++; /* NULL terminator */
o1 = longopts = (struct option*)malloc(len * sizeof(struct option));
for (o2 = global_longopts; o2->name; o1++, o2++)
memcpy(o1, o2, sizeof(*o1));
if (userui_ops->longopts)
for (o2 = userui_ops->longopts; o2->name; o1++, o2++)
memcpy(o1, o2, sizeof(*o1));
memset(o1, 0, sizeof(*o1));
while (1) {
int optindex;
c = getopt_long(argc, argv, optstring, longopts, &optindex);
if (c == -1)
break;
switch (c) {
case 'c':
sscanf(optarg, "%d", &netlink_socket_num);
break;
case 't':
test_run++;
break;
case 'h':
fprintf(stderr,
"Usage: %s [options]\n"
"\n"
" -t, --test\n"
" Specifying -t once will give an demo of this module.\n"
" Specifying -t twice will make the demo run as fast as it can.\n"
" (useful for performance testing).\n"
"%s"
"\n"
"Suspend2 UserUI version %s (%s module)\n",
argv[0],
(userui_ops->cmdline_options)?userui_ops->cmdline_options():"",
USERUI_VERSION, userui_ops->name);
exit(1);
default:
if (userui_ops->option_handler) {
userui_ops->option_handler(c);
}
}
}
if (optind < argc) {
fprintf(stderr, "Invalid parameters: ");
while (optind < argc)
fprintf(stderr, "%s ", argv[optind++]);
fprintf(stderr, "\n");
/* Don't actually exit. Just complain. */
}
free(optstring);
free(longopts);
}
static void lock_memory() {
/* Make sure we don't get swapped out */
if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
bail_err("mlockall");
}
static void get_info() {
FILE *f = fopen("/proc/suspend2/version", "r");
if (f) {
fgets(software_suspend_version, sizeof(software_suspend_version), f);
fclose(f);
software_suspend_version[sizeof(software_suspend_version)-1] = '\0';
software_suspend_version[strlen(software_suspend_version)-1] = '\0';
}
if (!send_message(USERUI_MSG_GET_STATE, NULL, 0)) {
bail_err("send_message");
}
if (!send_message(USERUI_MSG_GET_DEBUG_STATE, NULL, 0)) {
bail_err("send_message");
}
if (!send_message(USERUI_MSG_GET_DEBUGGING, NULL, 0)) {
bail_err("send_message");
}
/* We'll get the replies in our message loop */
get_console_loglevel();
saved_console_loglevel = console_loglevel;
}
static long my_vm_size() {
FILE *f;
long ret;
ret = -1;
if (!(f = fopen("/proc/self/statm", "r")))
goto out;
if (fscanf(f, "%ld", &ret) == 1)
ret *= PAGE_SIZE;
out:
if (f)
fclose(f);
return ret;
}
static void reserve_memory(unsigned long bytes) {
struct rlimit r;
long vm_size;
/* round up to the next page */
bytes = (bytes + PAGE_SIZE - 1) & ~PAGE_SIZE;
/* find out our existing VM usage */
if ((vm_size = my_vm_size()) == -1)
return;
/* Give an upper limit on our maximum address space so that we don't starve
* Software Suspend of memory.
* FIXME - this never actually gets reported to software suspend! Perhaps
* suspend_userui should look at our RLIMIT_AS ?
*/
r.rlim_cur = r.rlim_max = vm_size + bytes;
setrlimit(RLIMIT_AS, &r);
}
static void enforce_lifesavers() {
struct rlimit r;
r.rlim_cur = r.rlim_max = 0;
/* Set RLIMIT_NOFILE to prevent files being opened. */
setrlimit(RLIMIT_NOFILE, &r);
/* Set RLIMIT_NPROC to prevent process forks. */
setrlimit(RLIMIT_NPROC, &r);
/* Never core dump - that's bad too. */
setrlimit(RLIMIT_CORE, &r);
}
static void restore_console() {
ioctl(STDIN_FILENO, KDSKBMODE, K_XLATE);
ioctl(STDOUT_FILENO, KDSETMODE, KD_TEXT);
write(1, "\033[?25h\033[?0c", 11);
if (saved_console_loglevel >= 0) {
console_loglevel = saved_console_loglevel;
set_console_loglevel(1);
}
if (need_cleanup) {
userui_ops->cleanup();
need_cleanup = 0;
}
if (have_termios_backup)
tcsetattr(STDIN_FILENO, TCSANOW, &termios_backup);
}
/* A generic signal handler to ensure we don't quit in times of desperation,
* risking corrupting the image. */
static void sig_hand(int sig) {
printf("userui: Ack! SIG %d\n", sig);
restore_console();
if (test_run)
exit(1);
if (!safe_to_exit)
sleep(60*60*1); /* 1 hour */
_exit(1);
}
static char ascii_to_raw(char a) {
/* Table from drivers/char/keyboard.c */
const unsigned char raw_to_ascii[] =
"\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
"qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
"dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
"bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
"\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
"230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
"\r\000/"; /* 0x60 - 0x6f */
static unsigned char ascii_to_raw[sizeof(raw_to_ascii)];
static int ascii_to_raw_built = 0;
if (!ascii_to_raw_built) {
int i;
memset(ascii_to_raw, 0, sizeof(ascii_to_raw));
for (i = 0; i < sizeof(raw_to_ascii); i++)
if (raw_to_ascii[i])
ascii_to_raw[tolower(raw_to_ascii[i])] = i;
ascii_to_raw_built = 1;
}
return ascii_to_raw[(int)a];
}
static void keypress_signal_handler(int sig) {
static char next_is_escaped = 0;
unsigned char a, b;
if (!running) /* We're not running yet */
return;
while (1) {
if (next_is_escaped) {
if (read(STDIN_FILENO, &b, 1) <= 0)
break;
else {
if (!raw_keypresses)
a = ascii_to_raw(a);
next_is_escaped = 0;
userui_ops->keypress((a << 8)|b);
}
} else {
if (read(STDIN_FILENO, &a, 1) <= 0)
break;
else {
if (!raw_keypresses)
a = ascii_to_raw(a);
if (a == 0xe0) {
next_is_escaped = 1;
continue;
}
userui_ops->keypress(a);
}
}
}
}
static sighandler_t install_sighand(int signum, sighandler_t handler) {
struct sigaction act, oact;
act.sa_handler = handler;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, signum);
act.sa_flags = SA_RESTART;
sigaction(signum, &act, &oact);
return oact.sa_handler;
}
static void setup_signal_handlers() {
/* In a vague numerical order ... */
install_sighand(SIGHUP, sig_hand);
install_sighand(SIGINT, sig_hand);
install_sighand(SIGQUIT, sig_hand);
/* Pass on SIGILL... that does signal impending doom */
install_sighand(SIGABRT, sig_hand);
install_sighand(SIGFPE, sig_hand);
/* Can't do much about SIGKILL */
install_sighand(SIGBUS, sig_hand);
install_sighand(SIGSEGV, sig_hand);
install_sighand(SIGPIPE, sig_hand);
install_sighand(SIGALRM, sig_hand);
install_sighand(SIGUSR1, sig_hand);
install_sighand(SIGUSR2, sig_hand);
}
static void open_console() {
int fd;
if ((fd = open("/dev/console", O_RDWR)) == -1)
bail_err("open(\"/dev/console\", O_RDWR)");
/* We're about to replace FDs 0-2, so make sure this isn't one of them. */
if (fd <= 2)
fd = dup2(fd, 42);
if (dup2(fd, STDIN_FILENO) == -1)
bail_err("dup2(fd_r, STDIN_FILENO)");
if (dup2(fd, STDOUT_FILENO) == -1)
bail_err("dup2(fd, STDOUT_FILENO)");
if (dup2(fd, STDERR_FILENO) == -1)
bail_err("dup2(fd, STDERR_FILENO)");
close(fd);
}
static void prepare_console() {
int flags;
struct termios t;
/* Backup and set our favourite termios settings */
if (tcgetattr(STDIN_FILENO, &t) == -1) {
perror("tcgetattr");
} else {
memcpy(&termios_backup, &t, sizeof(t));
have_termios_backup = 1;
/* t.c_lflag &= ~(ICANON|ECHO|IXOFF|IGNBRK|BRKINT|); */
t.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
t.c_lflag &= ~(ISIG|ICANON|ECHO);
t.c_cc[VTIME] = 0;
t.c_cc[VMIN] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &t);
}
/* Make sure we clean up properly */
atexit(restore_console);
/* Want to receive raw keypresses */
if (ioctl(STDIN_FILENO, KDSKBMODE, K_MEDIUMRAW) == 0)
raw_keypresses = 1;
/* And be notified about them asynchronously */
install_sighand(SIGIO, keypress_signal_handler);
if (fcntl(STDIN_FILENO, F_SETOWN, getpid()) == -1)
bail_err("fcntl(STDIN_FILENO, F_SETOWN)");
flags = O_RDONLY | O_ASYNC | O_NONBLOCK;
if (fcntl(STDIN_FILENO, F_SETFL, flags) == -1)
bail_err("fcntl(STDIN_FILENO, F_SETFL)");
/* Set outputs to non-buffered */
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
}
static void open_netlink() {
struct sockaddr_nl sanl;
nlsock = socket(PF_NETLINK, SOCK_DGRAM, netlink_socket_num);
if (nlsock < 0)
bail_err("socket");
memset(&sanl, 0, sizeof(sanl));
sanl.nl_family = AF_NETLINK;
if (bind(nlsock, (struct sockaddr *)&sanl, sizeof(sanl)) == -1)
bail_err("bind");
}
static int send_ready() {
int version = SUSPEND_USERUI_INTERFACE_VERSION;
safe_to_exit = 0;
return send_message(USERUI_MSG_READY, &version, sizeof(version));
}
static struct nlmsghdr *fetch_message(void* buf, int buf_size, int non_block) {
static char local_buf[4096];
long flags = 0;
int n;
if (!buf) {
buf = (void*)local_buf;
buf_size = sizeof(local_buf);
}
if (non_block) {
if ((flags = fcntl(nlsock, F_GETFL)) == -1)
return NULL;
flags |= O_NONBLOCK;
if (fcntl(nlsock, F_SETFL, flags) == -1)
return NULL;
}
if ((n = recv(nlsock, buf, buf_size, 0)) == -1) {
if (!non_block && errno != EAGAIN && errno != EINTR)
bail_err("recv");
}
if (non_block) {
flags &= ~O_NONBLOCK;
fcntl(nlsock, F_SETFL, flags);
}
/* Check if the socket was closed on us, or no data was read from a
* non-blocking fd. */
if (n <= 0)
return NULL;
return (struct nlmsghdr *)buf;
}
static void report_nl_error(struct nlmsghdr *nlh) {
struct nlmsgerr *err;
err = NLMSG_DATA(nlh);
if (err->error == 0)
return; /* just a netlink ack */
fprintf(stderr, "userui: Received netlink error: %s\n",
strerror(-err->error));
fprintf(stderr, "userui: This was in response to a type %d message.\n",
err->msg.nlmsg_type);
}
static void get_nofreeze() {
struct nlmsghdr *nlh;
struct userui_msg_params *msg;
if (!send_message(USERUI_MSG_NOFREEZE_ME, NULL, 0))
bail_err("send_message");
while (1) {
if (!(nlh = fetch_message(NULL, 0, 0)))
bail_err("fetch_message() EOF");
msg = NLMSG_DATA(nlh);
switch (nlh->nlmsg_type) {
case USERUI_MSG_NOFREEZE_ACK:
return;
case NLMSG_ERROR:
report_nl_error(nlh);
break;
}
}
}
static void unblank_screen() {
int i = 4 /* TIOCL_UNBLANKSCREEN - see console_ioctl(4) */;
ioctl(1, TIOCLINUX, &i);
}
static void message_loop() {
static char buf1[4096], buf2[4096];
struct nlmsghdr *nlh, *nlh2;
while (1) {
struct userui_msg_params *msg;
int missed_progress_events = 0;
if (!(nlh = fetch_message(buf1, sizeof(buf1), 0)))
return; /* EOF */
while (nlh->nlmsg_type == USERUI_MSG_PROGRESS ||
nlh->nlmsg_type == USERUI_MSG_MESSAGE) {
nlh2 = fetch_message(buf2, sizeof(buf2), 1);
if (nlh2 == NULL)
break;
if (nlh2->nlmsg_type == USERUI_MSG_PROGRESS) {
if (nlh->nlmsg_type == USERUI_MSG_MESSAGE) {
/* Simply ignore the progress message */
continue;
}
/* Otherwise, ignore the message in nlh and keep on reading */
memcpy(buf1, buf2, sizeof(buf2));
missed_progress_events++;
if (missed_progress_events == 20)
break;
}
}
msg = NLMSG_DATA(nlh);
switch (nlh->nlmsg_type) {
case USERUI_MSG_MESSAGE:
userui_ops->message(msg->a, msg->b, msg->c, msg->text);
break;
case USERUI_MSG_PROGRESS:
userui_ops->update_progress(msg->a, msg->b, msg->text);
break;
case USERUI_MSG_GET_STATE:
suspend_action = *(int*)NLMSG_DATA(nlh);
break;
case USERUI_MSG_GET_DEBUG_STATE:
suspend_debug = *(int*)NLMSG_DATA(nlh);
break;
case USERUI_MSG_IS_DEBUGGING:
debugging_enabled = *(int *)NLMSG_DATA(nlh);
break;
case USERUI_MSG_CLEANUP:
userui_ops->cleanup();
close(nlsock);
exit(0);
case USERUI_MSG_REDRAW:
userui_ops->redraw();
unblank_screen();
break;
case NLMSG_ERROR:
report_nl_error(nlh);
break;
case NLMSG_DONE:
break;
default:
printf("userui: Received unknown message %d\n", nlh->nlmsg_type);
break;
}
if (need_loglevel_change) {
need_loglevel_change = 0;
userui_ops->log_level_change();
}
}
}
static void do_test_run() {
int i;
int max = 1024;
/* If test_run == 1, just give them an example display.
* If test_run >= 2, go as fast as we can (for performance timings).
*/
userui_ops->log_level_change();
userui_ops->message(0, 0, 1, "Writing caches ...");
for (i = 0; i <= max; i+=2) {
char buf[128];
snprintf(buf, 128, "%d/%d MB", i, max);
userui_ops->update_progress(i, max, buf);
if (i == 2*max/3) {
userui_ops->message(0, 0, 0, "Doing atomic copy");
if (test_run == 1)
usleep(800*1000);
}
if (test_run == 1)
usleep(10*1000);
if (need_loglevel_change) {
need_loglevel_change = 0;
userui_ops->log_level_change();
}
}
if (test_run == 1)
usleep(400*1000);
userui_ops->cleanup();
}
int main(int argc, char **argv) {
handle_params(argc, argv);
setup_signal_handlers();
open_console();
open_misc();
if (!test_run) {
open_netlink();
get_nofreeze();
get_info();
}
lock_memory();
prepare_console();
userui_ops->prepare();
need_cleanup = 1;
running = 1;
nice(1);
if (userui_ops->memory_required)
reserve_memory(userui_ops->memory_required());
else
reserve_memory(4*1024*1024); /* say 4MB */
enforce_lifesavers();
if (test_run) {
do_test_run();
return 0;
}
if (send_ready())
message_loop();
/* The only point we ever reach here is if message_loop crashed out.
* If this is the case, we should spin for a few hours before exiting to
* ensure that we don't corrupt stuff on disk (if we're past the atomic
* copy).
*/
sleep(60*60*1); /* 1 hours */
_exit(1);
}
|