util.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <limits.h>
  9. #include <sys/mman.h>
  10. #include <sys/stat.h>
  11. #include <sys/utsname.h>
  12. #include <sys/param.h>
  13. #include <sys/time.h>
  14. #include "asm/types.h"
  15. #include <ctype.h>
  16. #include <signal.h>
  17. #include <wait.h>
  18. #include <errno.h>
  19. #include <stdarg.h>
  20. #include <sched.h>
  21. #include <termios.h>
  22. #include <string.h>
  23. #include "kern_util.h"
  24. #include "user.h"
  25. #include "mem_user.h"
  26. #include "init.h"
  27. #include "ptrace_user.h"
  28. #include "uml-config.h"
  29. #include "os.h"
  30. #include "longjmp.h"
  31. #include "kern_constants.h"
  32. void stack_protections(unsigned long address)
  33. {
  34. if(mprotect((void *) address, UM_THREAD_SIZE,
  35. PROT_READ | PROT_WRITE | PROT_EXEC) < 0)
  36. panic("protecting stack failed, errno = %d", errno);
  37. }
  38. int raw(int fd)
  39. {
  40. struct termios tt;
  41. int err;
  42. CATCH_EINTR(err = tcgetattr(fd, &tt));
  43. if(err < 0)
  44. return -errno;
  45. cfmakeraw(&tt);
  46. CATCH_EINTR(err = tcsetattr(fd, TCSADRAIN, &tt));
  47. if(err < 0)
  48. return -errno;
  49. /* XXX tcsetattr could have applied only some changes
  50. * (and cfmakeraw() is a set of changes) */
  51. return 0;
  52. }
  53. void setup_machinename(char *machine_out)
  54. {
  55. struct utsname host;
  56. uname(&host);
  57. #ifdef UML_CONFIG_UML_X86
  58. # ifndef UML_CONFIG_64BIT
  59. if (!strcmp(host.machine, "x86_64")) {
  60. strcpy(machine_out, "i686");
  61. return;
  62. }
  63. # else
  64. if (!strcmp(host.machine, "i686")) {
  65. strcpy(machine_out, "x86_64");
  66. return;
  67. }
  68. # endif
  69. #endif
  70. strcpy(machine_out, host.machine);
  71. }
  72. void setup_hostinfo(char *buf, int len)
  73. {
  74. struct utsname host;
  75. uname(&host);
  76. snprintf(buf, len, "%s %s %s %s %s", host.sysname, host.nodename,
  77. host.release, host.version, host.machine);
  78. }
  79. int setjmp_wrapper(void (*proc)(void *, void *), ...)
  80. {
  81. va_list args;
  82. jmp_buf buf;
  83. int n;
  84. n = UML_SETJMP(&buf);
  85. if(n == 0){
  86. va_start(args, proc);
  87. (*proc)(&buf, &args);
  88. }
  89. va_end(args);
  90. return n;
  91. }
  92. void os_dump_core(void)
  93. {
  94. int pid;
  95. signal(SIGSEGV, SIG_DFL);
  96. /*
  97. * We are about to SIGTERM this entire process group to ensure that
  98. * nothing is around to run after the kernel exits. The
  99. * kernel wants to abort, not die through SIGTERM, so we
  100. * ignore it here.
  101. */
  102. signal(SIGTERM, SIG_IGN);
  103. kill(0, SIGTERM);
  104. /*
  105. * Most of the other processes associated with this UML are
  106. * likely sTopped, so give them a SIGCONT so they see the
  107. * SIGTERM.
  108. */
  109. kill(0, SIGCONT);
  110. /*
  111. * Now, having sent signals to everyone but us, make sure they
  112. * die by ptrace. Processes can survive what's been done to
  113. * them so far - the mechanism I understand is receiving a
  114. * SIGSEGV and segfaulting immediately upon return. There is
  115. * always a SIGSEGV pending, and (I'm guessing) signals are
  116. * processed in numeric order so the SIGTERM (signal 15 vs
  117. * SIGSEGV being signal 11) is never handled.
  118. *
  119. * Run a waitpid loop until we get some kind of error.
  120. * Hopefully, it's ECHILD, but there's not a lot we can do if
  121. * it's something else. Tell os_kill_ptraced_process not to
  122. * wait for the child to report its death because there's
  123. * nothing reasonable to do if that fails.
  124. */
  125. while ((pid = waitpid(-1, NULL, WNOHANG)) > 0)
  126. os_kill_ptraced_process(pid, 0);
  127. abort();
  128. }