util.c 3.4 KB

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