fork_exec.c 856 B

1234567891011121314151617181920212223242526272829303132333435
  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 <unistd.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <errno.h>
  7. #include <sys/wait.h>
  8. int main (int argc, char * const * argv, const char * const * envp)
  9. {
  10. int newfd = dup(1), outfd = dup(1);
  11. char fd_argv[4];
  12. snprintf(fd_argv, 4, "%d", newfd);
  13. char * const new_argv[] = { "./exec_victim", fd_argv, NULL };
  14. setenv("IN_EXECVE", "1", 1);
  15. int pid = fork();
  16. if (pid == 0) {
  17. close(outfd);
  18. execv(new_argv[0], new_argv);
  19. }
  20. wait(NULL);
  21. FILE * out = fdopen(outfd, "a");
  22. if (!out) {
  23. printf("cannot open file descriptor\n");
  24. return -1;
  25. }
  26. fprintf(out, "Goodbye world!\n");
  27. return 0;
  28. }