util.c 3.3 KB

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