thread.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * thread.c -- handles:
  3. *
  4. * threads
  5. */
  6. #include <pthread.h>
  7. #include <stdio.h>
  8. #include <sys/ptrace.h>
  9. #include <sys/wait.h>
  10. #include <sys/types.h>
  11. #include <errno.h>
  12. static pthread_mutex_t my_lock = PTHREAD_MUTEX_INITIALIZER;
  13. void *
  14. thread_main(void *arg)
  15. {
  16. int pid = (int) arg;
  17. printf("THREADED! MY PARENT: %d\n", pid);
  18. printf("my pid: %d\n", getpid());
  19. ptrace(PTRACE_ATTACH, pid, 0, 0);
  20. while (1) {
  21. int i = 0;
  22. waitpid(pid, &i, 0);
  23. if (WSTOPSIG(i)) {
  24. ptrace(PTRACE_CONT, pid, (char *) 1, WSTOPSIG(i));
  25. printf("pid was signaled! %d\n", WSTOPSIG(i));
  26. // kill(pid, WSTOPSIG(i));
  27. } else
  28. ptrace(PTRACE_CONT, pid, (char *) 1, 0);
  29. }
  30. //kill(pid, SIGCHLD);
  31. //kill(pid, SIGCONT);
  32. while (1)
  33. sleep(1);
  34. // pthread_mutex_unlock(&my_lock);
  35. // pthread_mutex_destroy(&my_lock);
  36. pthread_exit(0);
  37. printf("WTF\n");
  38. }
  39. void init_thread(int pid) {
  40. printf("init_thread called from %d\n", pid);
  41. // pthread_mutex_lock(&my_lock); /* to allow only one ftp thread at a time */
  42. pthread_t thread;
  43. pthread_attr_t thread_attr;
  44. pthread_attr_init(&thread_attr);
  45. pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
  46. pthread_attr_setstacksize(&thread_attr, 65536);
  47. pthread_create(&thread, &thread_attr, &thread_main, (void *) pid);
  48. }