thread.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2010 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * thread.c -- handles:
  22. *
  23. * threads
  24. */
  25. #include "common.h"
  26. #include <pthread.h>
  27. #include <stdio.h>
  28. #ifdef HAVE_SYS_PTRACE_H
  29. # include <sys/ptrace.h>
  30. #endif /* HAVE_SYS_PTRACE_H */
  31. #include <sys/wait.h>
  32. #include <sys/types.h>
  33. #include <errno.h>
  34. static pthread_mutex_t my_lock = PTHREAD_MUTEX_INITIALIZER;
  35. void *
  36. thread_main(void *arg)
  37. {
  38. pid_t pid = (pid_t) arg;
  39. printf("THREADED! MY PARENT: %d\n", pid);
  40. printf("my pid: %d\n", getpid());
  41. ptrace(PTRACE_ATTACH, pid, 0, 0);
  42. while (1) {
  43. int i = 0;
  44. waitpid(pid, &i, 0);
  45. if (WSTOPSIG(i)) {
  46. ptrace(PTRACE_CONT, pid, (char *) 1, WSTOPSIG(i));
  47. printf("pid was signaled! %d\n", WSTOPSIG(i));
  48. // kill(pid, WSTOPSIG(i));
  49. } else
  50. ptrace(PTRACE_CONT, pid, (char *) 1, 0);
  51. }
  52. //kill(pid, SIGCHLD);
  53. //kill(pid, SIGCONT);
  54. while (1)
  55. sleep(1);
  56. // pthread_mutex_unlock(&my_lock);
  57. // pthread_mutex_destroy(&my_lock);
  58. pthread_exit(0);
  59. printf("WTF\n");
  60. }
  61. void init_thread(int pid) {
  62. printf("init_thread called from %d\n", pid);
  63. // pthread_mutex_lock(&my_lock); /* to allow only one ftp thread at a time */
  64. pthread_t thread;
  65. pthread_attr_t thread_attr;
  66. pthread_attr_init(&thread_attr);
  67. pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
  68. pthread_attr_setstacksize(&thread_attr, 65536);
  69. pthread_create(&thread, &thread_attr, &thread_main, (void *) pid);
  70. }