Guest User

Untitled

a guest
Jun 8th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.64 KB | None | 0 0
  1. /* C standard headers */
  2. #include <errno.h>
  3. #include <inttypes.h>
  4. #include <setjmp.h>
  5. #include <stdbool.h>
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. /* Linux headers */
  13. #include <dirent.h>
  14. #include <fcntl.h>
  15. #include <sys/mman.h>
  16. #include <sys/ptrace.h>
  17. #include <sys/types.h>
  18. #include <sys/wait.h>
  19. #include <unistd.h>
  20. #include <sys/user.h>
  21. /* Architecture-specific headers */
  22. #include <asm/ptrace.h>
  23. #include <asm/unistd.h>
  24.  
  25. #include <signal.h>
  26. #include <assert.h>
  27.  
  28. typedef void fun_moved_from_context();
  29. // using namespace std;
  30. void attachTo(pid_t pid, char* id) {
  31.   long ret = ptrace(PTRACE_ATTACH, pid, NULL, NULL);
  32.   printf("\t%s\tattachTo: %ld\n", id, ret);
  33.   if (ret == -1) perror("err: ");
  34. }
  35. void seizeTo(pid_t pid, char* id) {
  36.   long ret = ptrace(PTRACE_SEIZE, pid, NULL, NULL);
  37.   assert(ret > 0);
  38.   printf("\t%s\tseizeTo: %ld\n", id, ret);
  39. }
  40. void detachFrom(pid_t pid, char* id) {
  41.   long ret = ptrace(PTRACE_DETACH, pid, NULL, NULL);
  42.   printf("\t%s\tdetachFrom: %ld\n", id, ret);
  43. }
  44. void setOptions(pid_t pid, char* id) {
  45.   long ret = ptrace(
  46.       PTRACE_SETOPTIONS, pid, NULL,
  47.       (void*)(PTRACE_O_TRACECLONE | PTRACE_O_TRACEEXEC | PTRACE_O_TRACEEXIT |
  48.               PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK));
  49.   printf("\t%s\tsetOptions: %ld\n", id, ret);
  50. }
  51. void setVarData(pid_t pid, volatile bool* can_run, void* data, char* id) {
  52.   long ret = ptrace(PTRACE_POKEDATA, pid, (void*)can_run, (void*)data);
  53.   printf("\t%s\tsetVarData: %ld\n", id, ret);
  54. }
  55. void cont(pid_t pid, char* id) {
  56.   long ret = ptrace(PTRACE_CONT, pid, NULL, NULL);
  57.   printf("\t%s\tcont: %ld\n", id, ret);
  58. }
  59. void interrupt(pid_t pid, char* id) {
  60.   long ret = ptrace(PTRACE_INTERRUPT, pid, NULL, NULL);
  61.   printf("\t%s\tinterrupt: %ld\n", id, ret);
  62. }
  63.  
  64. int main() {
  65.   volatile bool can_runA = false, can_runB = false;
  66.   pid_t procA = getpid();
  67.   volatile pid_t procB = 0;
  68.  
  69.   if (fork() > 0) {  // process A
  70.     while (!can_runA) {
  71.       printf("\tA\twaiting to continue...\n");
  72.       sleep(1);
  73.     }
  74.     attachTo(procB, "A");
  75.     waitpid(procB, NULL, __WALL);
  76.     setOptions(procB, "A");
  77.     setVarData(procB, &can_runB, 1, "A");
  78.     cont(procB, "A");
  79.     printf("\tA\tfinished\n");
  80.   } else {  // process B
  81.     procB = getpid();
  82.     attachTo(procA, "B");
  83.     waitpid(procA, NULL, __WALL);
  84.     setOptions(procA, "B");
  85.     setVarData(procA, &can_runA, 1, "B");
  86.     setVarData(procA, &procB, procB, "B");
  87.     cont(procA, "B");
  88.     while (!can_runB) {
  89.       printf("\tB\twaiting to continue...\n");
  90.       sleep(1);
  91.     }
  92.     printf("\tB\tfinished\n");
  93.   }  
  94.   return 0;
  95. }
Add Comment
Please, Sign In to add comment