util.c 4.5 KB

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