system.c 1.2 KB

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