util.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <errno.h>
  8. #include <signal.h>
  9. #include <string.h>
  10. #include <termios.h>
  11. #include <wait.h>
  12. #include <sys/mman.h>
  13. #include <sys/utsname.h>
  14. #include "kern_constants.h"
  15. #include "os.h"
  16. #include "user.h"
  17. void stack_protections(unsigned long address)
  18. {
  19. if (mprotect((void *) address, UM_THREAD_SIZE,
  20. PROT_READ | PROT_WRITE | PROT_EXEC) < 0)
  21. panic("protecting stack failed, errno = %d", errno);
  22. }
  23. int raw(int fd)
  24. {
  25. struct termios tt;
  26. int err;
  27. CATCH_EINTR(err = tcgetattr(fd, &tt));
  28. if (err < 0)
  29. return -errno;
  30. cfmakeraw(&tt);
  31. CATCH_EINTR(err = tcsetattr(fd, TCSADRAIN, &tt));
  32. if (err < 0)
  33. return -errno;
  34. /*
  35. * XXX tcsetattr could have applied only some changes
  36. * (and cfmakeraw() is a set of changes)
  37. */
  38. return 0;
  39. }
  40. void setup_machinename(char *machine_out)
  41. {
  42. struct utsname host;
  43. uname(&host);
  44. #ifdef UML_CONFIG_UML_X86
  45. # ifndef UML_CONFIG_64BIT
  46. if (!strcmp(host.machine, "x86_64")) {
  47. strcpy(machine_out, "i686");
  48. return;
  49. }
  50. # else
  51. if (!strcmp(host.machine, "i686")) {
  52. strcpy(machine_out, "x86_64");
  53. return;
  54. }
  55. # endif
  56. #endif
  57. strcpy(machine_out, host.machine);
  58. }
  59. void setup_hostinfo(char *buf, int len)
  60. {
  61. struct utsname host;
  62. uname(&host);
  63. snprintf(buf, len, "%s %s %s %s %s", host.sysname, host.nodename,
  64. host.release, host.version, host.machine);
  65. }
  66. void os_dump_core(void)
  67. {
  68. int pid;
  69. signal(SIGSEGV, SIG_DFL);
  70. /*
  71. * We are about to SIGTERM this entire process group to ensure that
  72. * nothing is around to run after the kernel exits. The
  73. * kernel wants to abort, not die through SIGTERM, so we
  74. * ignore it here.
  75. */
  76. signal(SIGTERM, SIG_IGN);
  77. kill(0, SIGTERM);
  78. /*
  79. * Most of the other processes associated with this UML are
  80. * likely sTopped, so give them a SIGCONT so they see the
  81. * SIGTERM.
  82. */
  83. kill(0, SIGCONT);
  84. /*
  85. * Now, having sent signals to everyone but us, make sure they
  86. * die by ptrace. Processes can survive what's been done to
  87. * them so far - the mechanism I understand is receiving a
  88. * SIGSEGV and segfaulting immediately upon return. There is
  89. * always a SIGSEGV pending, and (I'm guessing) signals are
  90. * processed in numeric order so the SIGTERM (signal 15 vs
  91. * SIGSEGV being signal 11) is never handled.
  92. *
  93. * Run a waitpid loop until we get some kind of error.
  94. * Hopefully, it's ECHILD, but there's not a lot we can do if
  95. * it's something else. Tell os_kill_ptraced_process not to
  96. * wait for the child to report its death because there's
  97. * nothing reasonable to do if that fails.
  98. */
  99. while ((pid = waitpid(-1, NULL, WNOHANG | __WALL)) > 0)
  100. os_kill_ptraced_process(pid, 0);
  101. abort();
  102. }