system.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #include <stdio.h>
  7. #include <stddef.h>
  8. #include <signal.h>
  9. #include <unistd.h>
  10. #include <sys/wait.h>
  11. #include <stdlib.h>
  12. /* uClinux-2.0 has vfork, but Linux 2.0 doesn't */
  13. #include <sys/syscall.h>
  14. #ifndef __NR_vfork
  15. # define vfork fork
  16. #endif
  17. extern __typeof(system) __libc_system;
  18. int __libc_system(const char *command)
  19. {
  20. int wait_val, pid;
  21. __sighandler_t save_quit, save_int, save_chld;
  22. if (command == 0)
  23. return 1;
  24. save_quit = signal(SIGQUIT, SIG_IGN);
  25. save_int = signal(SIGINT, SIG_IGN);
  26. save_chld = signal(SIGCHLD, SIG_DFL);
  27. if ((pid = vfork()) < 0) {
  28. signal(SIGQUIT, save_quit);
  29. signal(SIGINT, save_int);
  30. signal(SIGCHLD, save_chld);
  31. return -1;
  32. }
  33. if (pid == 0) {
  34. signal(SIGQUIT, SIG_DFL);
  35. signal(SIGINT, SIG_DFL);
  36. signal(SIGCHLD, SIG_DFL);
  37. execl("/bin/sh", "sh", "-c", command, (char *) 0);
  38. _exit(127);
  39. }
  40. /* Signals are not absolutly guarenteed with vfork */
  41. signal(SIGQUIT, SIG_IGN);
  42. signal(SIGINT, SIG_IGN);
  43. #if 0
  44. __printf("Waiting for child %d\n", pid);
  45. #endif
  46. if (wait4(pid, &wait_val, 0, 0) == -1)
  47. wait_val = -1;
  48. signal(SIGQUIT, save_quit);
  49. signal(SIGINT, save_int);
  50. signal(SIGCHLD, save_chld);
  51. return wait_val;
  52. }
  53. weak_alias(__libc_system,system)