user_util.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <setjmp.h>
  10. #include <sys/mman.h>
  11. #include <sys/stat.h>
  12. #include <sys/utsname.h>
  13. #include <sys/param.h>
  14. #include <sys/time.h>
  15. #include "asm/types.h"
  16. #include <ctype.h>
  17. #include <signal.h>
  18. #include <wait.h>
  19. #include <errno.h>
  20. #include <stdarg.h>
  21. #include <sched.h>
  22. #include <termios.h>
  23. #include <string.h>
  24. #include "user_util.h"
  25. #include "kern_util.h"
  26. #include "user.h"
  27. #include "mem_user.h"
  28. #include "init.h"
  29. #include "ptrace_user.h"
  30. #include "uml-config.h"
  31. void stop(void)
  32. {
  33. while(1) sleep(1000000);
  34. }
  35. void stack_protections(unsigned long address)
  36. {
  37. int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
  38. if(mprotect((void *) address, page_size(), prot) < 0)
  39. panic("protecting stack failed, errno = %d", errno);
  40. }
  41. void task_protections(unsigned long address)
  42. {
  43. unsigned long guard = address + page_size();
  44. unsigned long stack = guard + page_size();
  45. int prot = 0, pages;
  46. #ifdef notdef
  47. if(mprotect((void *) stack, page_size(), prot) < 0)
  48. panic("protecting guard page failed, errno = %d", errno);
  49. #endif
  50. pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER) - 2;
  51. prot = PROT_READ | PROT_WRITE | PROT_EXEC;
  52. if(mprotect((void *) stack, pages * page_size(), prot) < 0)
  53. panic("protecting stack failed, errno = %d", errno);
  54. }
  55. int wait_for_stop(int pid, int sig, int cont_type, void *relay)
  56. {
  57. sigset_t *relay_signals = relay;
  58. int status, ret;
  59. while(1){
  60. CATCH_EINTR(ret = waitpid(pid, &status, WUNTRACED));
  61. if((ret < 0) ||
  62. !WIFSTOPPED(status) || (WSTOPSIG(status) != sig)){
  63. if(ret < 0){
  64. printk("wait failed, errno = %d\n",
  65. errno);
  66. }
  67. else if(WIFEXITED(status))
  68. printk("process %d exited with status %d\n",
  69. pid, WEXITSTATUS(status));
  70. else if(WIFSIGNALED(status))
  71. printk("process %d exited with signal %d\n",
  72. pid, WTERMSIG(status));
  73. else if((WSTOPSIG(status) == SIGVTALRM) ||
  74. (WSTOPSIG(status) == SIGALRM) ||
  75. (WSTOPSIG(status) == SIGIO) ||
  76. (WSTOPSIG(status) == SIGPROF) ||
  77. (WSTOPSIG(status) == SIGCHLD) ||
  78. (WSTOPSIG(status) == SIGWINCH) ||
  79. (WSTOPSIG(status) == SIGINT)){
  80. ptrace(cont_type, pid, 0, WSTOPSIG(status));
  81. continue;
  82. }
  83. else if((relay_signals != NULL) &&
  84. sigismember(relay_signals, WSTOPSIG(status))){
  85. ptrace(cont_type, pid, 0, WSTOPSIG(status));
  86. continue;
  87. }
  88. else printk("process %d stopped with signal %d\n",
  89. pid, WSTOPSIG(status));
  90. panic("wait_for_stop failed to wait for %d to stop "
  91. "with %d\n", pid, sig);
  92. }
  93. return(status);
  94. }
  95. }
  96. int raw(int fd)
  97. {
  98. struct termios tt;
  99. int err;
  100. CATCH_EINTR(err = tcgetattr(fd, &tt));
  101. if(err < 0)
  102. return -errno;
  103. cfmakeraw(&tt);
  104. CATCH_EINTR(err = tcsetattr(fd, TCSADRAIN, &tt));
  105. if(err < 0)
  106. return -errno;
  107. /* XXX tcsetattr could have applied only some changes
  108. * (and cfmakeraw() is a set of changes) */
  109. return(0);
  110. }
  111. void setup_machinename(char *machine_out)
  112. {
  113. struct utsname host;
  114. uname(&host);
  115. #if defined(UML_CONFIG_UML_X86) && !defined(UML_CONFIG_64BIT)
  116. if (!strcmp(host.machine, "x86_64")) {
  117. strcpy(machine_out, "i686");
  118. return;
  119. }
  120. #endif
  121. strcpy(machine_out, host.machine);
  122. }
  123. char host_info[(_UTSNAME_LENGTH + 1) * 4 + _UTSNAME_NODENAME_LENGTH + 1];
  124. void setup_hostinfo(void)
  125. {
  126. struct utsname host;
  127. uname(&host);
  128. sprintf(host_info, "%s %s %s %s %s", host.sysname, host.nodename,
  129. host.release, host.version, host.machine);
  130. }
  131. int setjmp_wrapper(void (*proc)(void *, void *), ...)
  132. {
  133. va_list args;
  134. sigjmp_buf buf;
  135. int n;
  136. n = sigsetjmp(buf, 1);
  137. if(n == 0){
  138. va_start(args, proc);
  139. (*proc)(&buf, &args);
  140. }
  141. va_end(args);
  142. return(n);
  143. }
  144. /*
  145. * Overrides for Emacs so that we follow Linus's tabbing style.
  146. * Emacs will notice this stuff at the end of the file and automatically
  147. * adjust the settings for this buffer only. This must remain at the end
  148. * of the file.
  149. * ---------------------------------------------------------------------------
  150. * Local variables:
  151. * c-file-style: "linux"
  152. * End:
  153. */