util.c 4.1 KB

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