util.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #include <signal.h>
  10. #include <string.h>
  11. #include <termios.h>
  12. #include <wait.h>
  13. #include <sys/mman.h>
  14. #include <sys/utsname.h>
  15. #include <init.h>
  16. #include <os.h>
  17. void stack_protections(unsigned long address)
  18. {
  19. if (mprotect((void *) address, UM_THREAD_SIZE,
  20. PROT_READ | PROT_WRITE | PROT_EXEC) < 0)
  21. panic("protecting stack failed, errno = %d", errno);
  22. }
  23. int raw(int fd)
  24. {
  25. struct termios tt;
  26. int err;
  27. CATCH_EINTR(err = tcgetattr(fd, &tt));
  28. if (err < 0)
  29. return -errno;
  30. cfmakeraw(&tt);
  31. CATCH_EINTR(err = tcsetattr(fd, TCSADRAIN, &tt));
  32. if (err < 0)
  33. return -errno;
  34. /*
  35. * XXX tcsetattr could have applied only some changes
  36. * (and cfmakeraw() is a set of changes)
  37. */
  38. return 0;
  39. }
  40. void setup_machinename(char *machine_out)
  41. {
  42. struct utsname host;
  43. uname(&host);
  44. #ifdef UML_CONFIG_UML_X86
  45. # ifndef UML_CONFIG_64BIT
  46. if (!strcmp(host.machine, "x86_64")) {
  47. strcpy(machine_out, "i686");
  48. return;
  49. }
  50. # else
  51. if (!strcmp(host.machine, "i686")) {
  52. strcpy(machine_out, "x86_64");
  53. return;
  54. }
  55. # endif
  56. #endif
  57. strcpy(machine_out, host.machine);
  58. }
  59. void setup_hostinfo(char *buf, int len)
  60. {
  61. struct utsname host;
  62. uname(&host);
  63. snprintf(buf, len, "%s %s %s %s %s", host.sysname, host.nodename,
  64. host.release, host.version, host.machine);
  65. }
  66. /*
  67. * We cannot use glibc's abort(). It makes use of tgkill() which
  68. * has no effect within UML's kernel threads.
  69. * After that glibc would execute an invalid instruction to kill
  70. * the calling process and UML crashes with SIGSEGV.
  71. */
  72. static inline void __attribute__ ((noreturn)) uml_abort(void)
  73. {
  74. sigset_t sig;
  75. fflush(NULL);
  76. if (!sigemptyset(&sig) && !sigaddset(&sig, SIGABRT))
  77. sigprocmask(SIG_UNBLOCK, &sig, 0);
  78. for (;;)
  79. if (kill(getpid(), SIGABRT) < 0)
  80. exit(127);
  81. }
  82. /*
  83. * UML helper threads must not handle SIGWINCH/INT/TERM
  84. */
  85. void os_fix_helper_signals(void)
  86. {
  87. signal(SIGWINCH, SIG_IGN);
  88. signal(SIGINT, SIG_DFL);
  89. signal(SIGTERM, SIG_DFL);
  90. }
  91. void os_dump_core(void)
  92. {
  93. int pid;
  94. signal(SIGSEGV, SIG_DFL);
  95. /*
  96. * We are about to SIGTERM this entire process group to ensure that
  97. * nothing is around to run after the kernel exits. The
  98. * kernel wants to abort, not die through SIGTERM, so we
  99. * ignore it here.
  100. */
  101. signal(SIGTERM, SIG_IGN);
  102. kill(0, SIGTERM);
  103. /*
  104. * Most of the other processes associated with this UML are
  105. * likely sTopped, so give them a SIGCONT so they see the
  106. * SIGTERM.
  107. */
  108. kill(0, SIGCONT);
  109. /*
  110. * Now, having sent signals to everyone but us, make sure they
  111. * die by ptrace. Processes can survive what's been done to
  112. * them so far - the mechanism I understand is receiving a
  113. * SIGSEGV and segfaulting immediately upon return. There is
  114. * always a SIGSEGV pending, and (I'm guessing) signals are
  115. * processed in numeric order so the SIGTERM (signal 15 vs
  116. * SIGSEGV being signal 11) is never handled.
  117. *
  118. * Run a waitpid loop until we get some kind of error.
  119. * Hopefully, it's ECHILD, but there's not a lot we can do if
  120. * it's something else. Tell os_kill_ptraced_process not to
  121. * wait for the child to report its death because there's
  122. * nothing reasonable to do if that fails.
  123. */
  124. while ((pid = waitpid(-1, NULL, WNOHANG | __WALL)) > 0)
  125. os_kill_ptraced_process(pid, 0);
  126. uml_abort();
  127. }
  128. void um_early_printk(const char *s, unsigned int n)
  129. {
  130. printf("%.*s", n, s);
  131. }
  132. static int quiet_info;
  133. static int __init quiet_cmd_param(char *str, int *add)
  134. {
  135. quiet_info = 1;
  136. return 0;
  137. }
  138. __uml_setup("quiet", quiet_cmd_param,
  139. "quiet\n"
  140. " Turns off information messages during boot.\n\n");
  141. void os_info(const char *fmt, ...)
  142. {
  143. va_list list;
  144. if (quiet_info)
  145. return;
  146. va_start(list, fmt);
  147. vfprintf(stderr, fmt, list);
  148. va_end(list);
  149. }
  150. void os_warn(const char *fmt, ...)
  151. {
  152. va_list list;
  153. va_start(list, fmt);
  154. vfprintf(stderr, fmt, list);
  155. va_end(list);
  156. }