system.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #define wait4 __wait4
  2. #define execl __execl
  3. #define signal __signal
  4. #include <stdio.h>
  5. #include <stddef.h>
  6. #include <signal.h>
  7. #include <unistd.h>
  8. #include <sys/wait.h>
  9. /* uClinux-2.0 has vfork, but Linux 2.0 doesn't */
  10. #include <sys/syscall.h>
  11. #if ! defined __NR_vfork
  12. #define vfork fork
  13. #endif
  14. int __libc_system(char *command)
  15. {
  16. int wait_val, pid;
  17. __sighandler_t save_quit, save_int, save_chld;
  18. if (command == 0)
  19. return 1;
  20. save_quit = signal(SIGQUIT, SIG_IGN);
  21. save_int = signal(SIGINT, SIG_IGN);
  22. save_chld = signal(SIGCHLD, SIG_DFL);
  23. if ((pid = vfork()) < 0) {
  24. signal(SIGQUIT, save_quit);
  25. signal(SIGINT, save_int);
  26. signal(SIGCHLD, save_chld);
  27. return -1;
  28. }
  29. if (pid == 0) {
  30. signal(SIGQUIT, SIG_DFL);
  31. signal(SIGINT, SIG_DFL);
  32. signal(SIGCHLD, SIG_DFL);
  33. execl("/bin/sh", "sh", "-c", command, (char *) 0);
  34. _exit(127);
  35. }
  36. /* Signals are not absolutly guarenteed with vfork */
  37. signal(SIGQUIT, SIG_IGN);
  38. signal(SIGINT, SIG_IGN);
  39. #if 0
  40. __printf("Waiting for child %d\n", pid);
  41. #endif
  42. if (wait4(pid, &wait_val, 0, 0) == -1)
  43. wait_val = -1;
  44. signal(SIGQUIT, save_quit);
  45. signal(SIGINT, save_int);
  46. signal(SIGCHLD, save_chld);
  47. return wait_val;
  48. }
  49. weak_alias(__libc_system, system)