process.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (C) 2002 Jeff Dike (jdike@addtoit.com)
  3. * Licensed under the GPL
  4. */
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <errno.h>
  8. #include <signal.h>
  9. #include <linux/unistd.h>
  10. #include <sys/mman.h>
  11. #include <sys/wait.h>
  12. #include "ptrace_user.h"
  13. #include "os.h"
  14. #include "user.h"
  15. #include "user_util.h"
  16. #define ARBITRARY_ADDR -1
  17. #define FAILURE_PID -1
  18. #define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
  19. #define COMM_SCANF "%*[^)])"
  20. unsigned long os_process_pc(int pid)
  21. {
  22. char proc_stat[STAT_PATH_LEN], buf[256];
  23. unsigned long pc;
  24. int fd, err;
  25. sprintf(proc_stat, "/proc/%d/stat", pid);
  26. fd = os_open_file(proc_stat, of_read(OPENFLAGS()), 0);
  27. if(fd < 0){
  28. printk("os_process_pc - couldn't open '%s', err = %d\n",
  29. proc_stat, -fd);
  30. return(ARBITRARY_ADDR);
  31. }
  32. err = os_read_file(fd, buf, sizeof(buf));
  33. if(err < 0){
  34. printk("os_process_pc - couldn't read '%s', err = %d\n",
  35. proc_stat, -err);
  36. os_close_file(fd);
  37. return(ARBITRARY_ADDR);
  38. }
  39. os_close_file(fd);
  40. pc = ARBITRARY_ADDR;
  41. if(sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d "
  42. "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
  43. "%*d %*d %*d %*d %*d %lu", &pc) != 1){
  44. printk("os_process_pc - couldn't find pc in '%s'\n", buf);
  45. }
  46. return(pc);
  47. }
  48. int os_process_parent(int pid)
  49. {
  50. char stat[STAT_PATH_LEN];
  51. char data[256];
  52. int parent, n, fd;
  53. if(pid == -1) return(-1);
  54. snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
  55. fd = os_open_file(stat, of_read(OPENFLAGS()), 0);
  56. if(fd < 0){
  57. printk("Couldn't open '%s', err = %d\n", stat, -fd);
  58. return(FAILURE_PID);
  59. }
  60. n = os_read_file(fd, data, sizeof(data));
  61. os_close_file(fd);
  62. if(n < 0){
  63. printk("Couldn't read '%s', err = %d\n", stat, -n);
  64. return(FAILURE_PID);
  65. }
  66. parent = FAILURE_PID;
  67. n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
  68. if(n != 1)
  69. printk("Failed to scan '%s'\n", data);
  70. return(parent);
  71. }
  72. void os_stop_process(int pid)
  73. {
  74. kill(pid, SIGSTOP);
  75. }
  76. void os_kill_process(int pid, int reap_child)
  77. {
  78. kill(pid, SIGKILL);
  79. if(reap_child)
  80. CATCH_EINTR(waitpid(pid, NULL, 0));
  81. }
  82. /* Kill off a ptraced child by all means available. kill it normally first,
  83. * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
  84. * which it can't exit directly.
  85. */
  86. void os_kill_ptraced_process(int pid, int reap_child)
  87. {
  88. kill(pid, SIGKILL);
  89. ptrace(PTRACE_KILL, pid);
  90. ptrace(PTRACE_CONT, pid);
  91. if(reap_child)
  92. CATCH_EINTR(waitpid(pid, NULL, 0));
  93. }
  94. void os_usr1_process(int pid)
  95. {
  96. kill(pid, SIGUSR1);
  97. }
  98. /*Don't use the glibc version, which caches the result in TLS. It misses some
  99. * syscalls, and also breaks with clone(), which does not unshare the TLS.*/
  100. inline _syscall0(pid_t, getpid)
  101. int os_getpid(void)
  102. {
  103. return(getpid());
  104. }
  105. int os_getpgrp(void)
  106. {
  107. return getpgrp();
  108. }
  109. int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
  110. int r, int w, int x)
  111. {
  112. void *loc;
  113. int prot;
  114. prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
  115. (x ? PROT_EXEC : 0);
  116. loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
  117. fd, off);
  118. if(loc == MAP_FAILED)
  119. return(-errno);
  120. return(0);
  121. }
  122. int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
  123. {
  124. int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
  125. (x ? PROT_EXEC : 0));
  126. if(mprotect(addr, len, prot) < 0)
  127. return(-errno);
  128. return(0);
  129. }
  130. int os_unmap_memory(void *addr, int len)
  131. {
  132. int err;
  133. err = munmap(addr, len);
  134. if(err < 0)
  135. return(-errno);
  136. return(0);
  137. }
  138. /*
  139. * Overrides for Emacs so that we follow Linus's tabbing style.
  140. * Emacs will notice this stuff at the end of the file and automatically
  141. * adjust the settings for this buffer only. This must remain at the end
  142. * of the file.
  143. * ---------------------------------------------------------------------------
  144. * Local variables:
  145. * c-file-style: "linux"
  146. * End:
  147. */