util.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. void os_dump_core(void)
  82. {
  83. int pid;
  84. signal(SIGSEGV, SIG_DFL);
  85. /*
  86. * We are about to SIGTERM this entire process group to ensure that
  87. * nothing is around to run after the kernel exits. The
  88. * kernel wants to abort, not die through SIGTERM, so we
  89. * ignore it here.
  90. */
  91. signal(SIGTERM, SIG_IGN);
  92. kill(0, SIGTERM);
  93. /*
  94. * Most of the other processes associated with this UML are
  95. * likely sTopped, so give them a SIGCONT so they see the
  96. * SIGTERM.
  97. */
  98. kill(0, SIGCONT);
  99. /*
  100. * Now, having sent signals to everyone but us, make sure they
  101. * die by ptrace. Processes can survive what's been done to
  102. * them so far - the mechanism I understand is receiving a
  103. * SIGSEGV and segfaulting immediately upon return. There is
  104. * always a SIGSEGV pending, and (I'm guessing) signals are
  105. * processed in numeric order so the SIGTERM (signal 15 vs
  106. * SIGSEGV being signal 11) is never handled.
  107. *
  108. * Run a waitpid loop until we get some kind of error.
  109. * Hopefully, it's ECHILD, but there's not a lot we can do if
  110. * it's something else. Tell os_kill_ptraced_process not to
  111. * wait for the child to report its death because there's
  112. * nothing reasonable to do if that fails.
  113. */
  114. while ((pid = waitpid(-1, NULL, WNOHANG | __WALL)) > 0)
  115. os_kill_ptraced_process(pid, 0);
  116. uml_abort();
  117. }
  118. void um_early_printk(const char *s, unsigned int n)
  119. {
  120. printf("%.*s", n, s);
  121. }