util.c 3.0 KB

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