process.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
  3. * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. * Licensed under the GPL
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <signal.h>
  11. #include <fcntl.h>
  12. #include <sys/mman.h>
  13. #include <sys/ptrace.h>
  14. #include <sys/wait.h>
  15. #include <asm/unistd.h>
  16. #include <init.h>
  17. #include <longjmp.h>
  18. #include <os.h>
  19. #define ARBITRARY_ADDR -1
  20. #define FAILURE_PID -1
  21. #define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
  22. #define COMM_SCANF "%*[^)])"
  23. unsigned long os_process_pc(int pid)
  24. {
  25. char proc_stat[STAT_PATH_LEN], buf[256];
  26. unsigned long pc = ARBITRARY_ADDR;
  27. int fd, err;
  28. sprintf(proc_stat, "/proc/%d/stat", pid);
  29. fd = open(proc_stat, O_RDONLY, 0);
  30. if (fd < 0) {
  31. printk(UM_KERN_ERR "os_process_pc - couldn't open '%s', "
  32. "errno = %d\n", proc_stat, errno);
  33. goto out;
  34. }
  35. CATCH_EINTR(err = read(fd, buf, sizeof(buf)));
  36. if (err < 0) {
  37. printk(UM_KERN_ERR "os_process_pc - couldn't read '%s', "
  38. "err = %d\n", proc_stat, errno);
  39. goto out_close;
  40. }
  41. os_close_file(fd);
  42. pc = ARBITRARY_ADDR;
  43. if (sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d "
  44. "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
  45. "%*d %*d %*d %*d %*d %lu", &pc) != 1)
  46. printk(UM_KERN_ERR "os_process_pc - couldn't find pc in '%s'\n",
  47. buf);
  48. out_close:
  49. close(fd);
  50. out:
  51. return pc;
  52. }
  53. int os_process_parent(int pid)
  54. {
  55. char stat[STAT_PATH_LEN];
  56. char data[256];
  57. int parent = FAILURE_PID, n, fd;
  58. if (pid == -1)
  59. return parent;
  60. snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
  61. fd = open(stat, O_RDONLY, 0);
  62. if (fd < 0) {
  63. printk(UM_KERN_ERR "Couldn't open '%s', errno = %d\n", stat,
  64. errno);
  65. return parent;
  66. }
  67. CATCH_EINTR(n = read(fd, data, sizeof(data)));
  68. close(fd);
  69. if (n < 0) {
  70. printk(UM_KERN_ERR "Couldn't read '%s', errno = %d\n", stat,
  71. errno);
  72. return parent;
  73. }
  74. parent = FAILURE_PID;
  75. n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
  76. if (n != 1)
  77. printk(UM_KERN_ERR "Failed to scan '%s'\n", data);
  78. return parent;
  79. }
  80. void os_alarm_process(int pid)
  81. {
  82. kill(pid, SIGALRM);
  83. }
  84. void os_stop_process(int pid)
  85. {
  86. kill(pid, SIGSTOP);
  87. }
  88. void os_kill_process(int pid, int reap_child)
  89. {
  90. kill(pid, SIGKILL);
  91. if (reap_child)
  92. CATCH_EINTR(waitpid(pid, NULL, __WALL));
  93. }
  94. /* Kill off a ptraced child by all means available. kill it normally first,
  95. * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
  96. * which it can't exit directly.
  97. */
  98. void os_kill_ptraced_process(int pid, int reap_child)
  99. {
  100. kill(pid, SIGKILL);
  101. ptrace(PTRACE_KILL, pid);
  102. ptrace(PTRACE_CONT, pid);
  103. if (reap_child)
  104. CATCH_EINTR(waitpid(pid, NULL, __WALL));
  105. }
  106. /* Don't use the glibc version, which caches the result in TLS. It misses some
  107. * syscalls, and also breaks with clone(), which does not unshare the TLS.
  108. */
  109. int os_getpid(void)
  110. {
  111. return syscall(__NR_getpid);
  112. }
  113. int os_getpgrp(void)
  114. {
  115. return getpgrp();
  116. }
  117. int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
  118. int r, int w, int x)
  119. {
  120. void *loc;
  121. int prot;
  122. prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
  123. (x ? PROT_EXEC : 0);
  124. loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
  125. fd, off);
  126. if (loc == MAP_FAILED)
  127. return -errno;
  128. return 0;
  129. }
  130. int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
  131. {
  132. int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
  133. (x ? PROT_EXEC : 0));
  134. if (mprotect(addr, len, prot) < 0)
  135. return -errno;
  136. return 0;
  137. }
  138. int os_unmap_memory(void *addr, int len)
  139. {
  140. int err;
  141. err = munmap(addr, len);
  142. if (err < 0)
  143. return -errno;
  144. return 0;
  145. }
  146. #ifndef MADV_REMOVE
  147. #define MADV_REMOVE KERNEL_MADV_REMOVE
  148. #endif
  149. int os_drop_memory(void *addr, int length)
  150. {
  151. int err;
  152. err = madvise(addr, length, MADV_REMOVE);
  153. if (err < 0)
  154. err = -errno;
  155. return err;
  156. }
  157. int __init can_drop_memory(void)
  158. {
  159. void *addr;
  160. int fd, ok = 0;
  161. printk(UM_KERN_INFO "Checking host MADV_REMOVE support...");
  162. fd = create_mem_file(UM_KERN_PAGE_SIZE);
  163. if (fd < 0) {
  164. printk(UM_KERN_ERR "Creating test memory file failed, "
  165. "err = %d\n", -fd);
  166. goto out;
  167. }
  168. addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
  169. MAP_SHARED, fd, 0);
  170. if (addr == MAP_FAILED) {
  171. printk(UM_KERN_ERR "Mapping test memory file failed, "
  172. "err = %d\n", -errno);
  173. goto out_close;
  174. }
  175. if (madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0) {
  176. printk(UM_KERN_ERR "MADV_REMOVE failed, err = %d\n", -errno);
  177. goto out_unmap;
  178. }
  179. printk(UM_KERN_CONT "OK\n");
  180. ok = 1;
  181. out_unmap:
  182. munmap(addr, UM_KERN_PAGE_SIZE);
  183. out_close:
  184. close(fd);
  185. out:
  186. return ok;
  187. }
  188. static int os_page_mincore(void *addr)
  189. {
  190. char vec[2];
  191. int ret;
  192. ret = mincore(addr, UM_KERN_PAGE_SIZE, vec);
  193. if (ret < 0) {
  194. if (errno == ENOMEM || errno == EINVAL)
  195. return 0;
  196. else
  197. return -errno;
  198. }
  199. return vec[0] & 1;
  200. }
  201. int os_mincore(void *addr, unsigned long len)
  202. {
  203. char *vec;
  204. int ret, i;
  205. if (len <= UM_KERN_PAGE_SIZE)
  206. return os_page_mincore(addr);
  207. vec = calloc(1, (len + UM_KERN_PAGE_SIZE - 1) / UM_KERN_PAGE_SIZE);
  208. if (!vec)
  209. return -ENOMEM;
  210. ret = mincore(addr, UM_KERN_PAGE_SIZE, vec);
  211. if (ret < 0) {
  212. if (errno == ENOMEM || errno == EINVAL)
  213. ret = 0;
  214. else
  215. ret = -errno;
  216. goto out;
  217. }
  218. for (i = 0; i < ((len + UM_KERN_PAGE_SIZE - 1) / UM_KERN_PAGE_SIZE); i++) {
  219. if (!(vec[i] & 1)) {
  220. ret = 0;
  221. goto out;
  222. }
  223. }
  224. ret = 1;
  225. out:
  226. free(vec);
  227. return ret;
  228. }
  229. void init_new_thread_signals(void)
  230. {
  231. set_handler(SIGSEGV);
  232. set_handler(SIGTRAP);
  233. set_handler(SIGFPE);
  234. set_handler(SIGILL);
  235. set_handler(SIGBUS);
  236. signal(SIGHUP, SIG_IGN);
  237. set_handler(SIGIO);
  238. signal(SIGWINCH, SIG_IGN);
  239. }