thread.c 1.3 KB

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