how can I log the output of the execve call?

2.7k views Asked by At

I wrote this simple program. I want the output of it to be logged to the test.log as opened below. Is it possible for me to do this?

int main(int argc, char **argv)
{
      int fd = open("test.log", O_CREAT|O_WRONLY);
      char *path[2];
      path[0] = "/bin/ls";
      path[1] = NULL;

      execve((char *)&path[0], &path, NULL);

      close(fd);
      return 0;
}
1

There are 1 answers

0
CaseyJones On

Working solution, as guided by Rici.

int main(int argc, char **argv)
{
        int fd = open("test.log", O_CREAT|O_WRONLY, 0600);
        char *path[2];
        path[0] = "./tes";
        path[1] = NULL;

        dup2(fd, 1);
        dup2(fd, 2);
        close(fd);

        execve(path[0], (char **)&path, NULL);

        return EXIT_FAILURE;
}