vfork_exec.c 892 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <sys/types.h>
  8. #include <sys/wait.h>
  9. #include <unistd.h>
  10. int main (int argc, const char ** argv, const char ** envp)
  11. {
  12. int newfd = dup(1), outfd = dup(1);
  13. char fd_argv[4];
  14. snprintf(fd_argv, 4, "%d", newfd);
  15. char * const new_argv[] = { "./exec_victim", fd_argv, NULL };
  16. setenv("IN_EXECVE", "1", 1);
  17. int pid = vfork();
  18. if (pid == 0) {
  19. close(outfd);
  20. execv(new_argv[0], new_argv);
  21. }
  22. wait(NULL);
  23. FILE * out = fdopen(outfd, "a");
  24. if (!out) {
  25. printf("cannot open file descriptor\n");
  26. return -1;
  27. }
  28. fprintf(out, "Goodbye world!\n");
  29. return 0;
  30. }