-1

Here is my pipe to pipe function:

  • int *pip is the pipe that I want to read from
  • char **cmd is the command that I want to execute
  • char **envp points to the environment variables

I want to take the pipe (pip) as STDIN with dup2 and take the new pipe (fd) as STDOUT also with dup2

The problem is that when it comes to execve the command is executed but the program is stuck in an infinite loop

int *pipe_to_pipe(int *pip, char **cmd, char **envp) {

    int     *fd;
    int     pid;

    fd = (int *)malloc(sizeof(int) * 2);
    if (!fd || pipe(fd) == -1)
            error(1);
    pid = fork();
    if (pid == -1)
            error(1);
    if (pid == 0)
    {
            dup2(pip[0], STDIN_FILENO);
            dup2(fd[1], STDOUT_FILENO);
            close(fd[0]);
            close(pip[1]);
            execve((command_path), cmd, envp);
            exit(0);
    }
    wait(NULL);
    free(pip);
    return (fd);
}

As an example, if I have "hello world" in my int *pip as a char **cmd I have cmd[0] = "cat", cmd[1] = "-e". When I make execve(path, cmd, envp); the program cat "hello world" and infinite loop here.

How can I fix this?

Maxou
  • 37
  • 5
  • 1
    I don't know what `d[i]` is - you have omitted the code declaring it, but the loop condition `while (d[i])` is almost definitely wrong and will go outside the array boundaries. – Eugene Sh. Feb 15 '22 at 17:21
  • I use d[i] to free the split that I make I will always have a NULL in my split so it wont go outside of the arrays here is my split function edit I can't show you my split function ;( – Maxou Feb 15 '22 at 17:25
  • 2
    You need to show [mcve]. This code is missing pieces. – Eugene Sh. Feb 15 '22 at 17:27
  • I know that the loop just start after the excution of the execve in the exec_ve function that I use in the pipe_to_pipe function I also know that the error don't come from ft_split and free_split – Maxou Feb 15 '22 at 17:35
  • In that case you should reduce your code (see "minimal") to not include any irrelevant stuff. – Eugene Sh. Feb 15 '22 at 17:36
  • Ok ok give me a sec – Maxou Feb 15 '22 at 17:39
  • 1
    `execve()` doesn't return if it's successful. – Barmar Feb 15 '22 at 17:42
  • Is this better Eugene ? Barmar then why after my execve the programm get stuck in an infinite loop when I try to print in STDERR somethin just after the execve it don't show when I execute the prog to me this mean that my prog get stuck in the execve am i right ?! – Maxou Feb 15 '22 at 17:51
  • You can't go changing your question once you get answers that point out a problem in a previous version of the question. Get your code and question right before you ask! It's simply not fair to those who would like to help you! And your change is drastic, even though it involves one character added! – Jonathan Leffler Feb 15 '22 at 18:36
  • I told you that I didn't explain it well (and I'm really sorry for that) and I know that I used it correctly with the good args so the problem don't come from the args of execve thats why I correct my questions so the people don't think that it comes from the args I know anyways that's my bad sry – Maxou Feb 15 '22 at 19:01
  • You aren't closing enough file descriptors in the child. **Rule of thumb**: If you [`dup2()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/dup2.html) one end of a pipe to standard input or standard output, close both of the original file descriptors from `pipe()` as soon as possible. In particular, that means before using any of the [`exec*()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/execvp.html) family of functions. The rule also applies with either `dup()` or [`fcntl()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html) with `F_DUPFD`. – Jonathan Leffler Feb 15 '22 at 19:04
  • Not closing the file descriptors in the child does not, however, explain the infinite loop part of the problem. – Jonathan Leffler Feb 15 '22 at 19:05

1 Answers1

-1

The problem comes from the fact that I didn't close the pipe "pip" in the parent process adding those lines that would be executed by the parent process fix everything

if (pid != 0){
close(pip[0]);
close(pip[1]);
wait(NULL);
}

Otherwise it fixes my problem I don't understand why if someone can explain to me I will be gratefull Thanks to everyone that answered me <3

Maxou
  • 37
  • 5