user_util.c 4.0 KB

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