0

I have a function that is calling a process called driverclear. Seems like the process starts but it never returns because I never get the output of the process and I never get the "Process Complete" message. Is there something I am doing wrong?

void cleanDriver
{
    pid_t pid;
    if(chmod("./helpers/driverclear", S_IXUSR) == 0)
    {

        int status = 0;
        pid = fork();

        if(pid == 0)
        {
            if(!execl("./helpers/driverclear", "driverclear", (char*) NULL))
            {
                perror("execl failed.\n");
            }
        }

        else
        {
            printf("Process Starting...");
            waitpid(pid, &status, 0);
            printf("Process Complete\n");
        }
    }
}
adamk
  • 1
  • 1
  • It works fine for me, but you should check for errors in `fork()` too. `if(pid < 0) { }` - and you don't need to check the return value from `execl`. If it returns, it failed, so you can write the `perror()` directly after it. – Ted Lyngmo Jan 29 '20 at 18:21
  • Thank you for your response @TedLyngmo. I tried the check for errors in fork() and it is not returning any errors. – adamk Jan 29 '20 at 18:31
  • Is the `driverclear` executeable perhaps not exiting? Check the processes you have running while this program is hanging. Does [this](https://godbolt.org/z/jdpMDa) version of your program also hang? – Ted Lyngmo Jan 29 '20 at 18:47
  • If I add `execl("/bin/echo", "/bin/echo", "Hello World", nullptr);` it prints Hello World. Does this mean there is an issue with the executable? If I run the process driverclear on its own, the process seems to complete successfully. – adamk Jan 29 '20 at 18:50
  • "_Does this mean there is an issue with the executable?_" - Probably. It could be doing different things if started this way compared to how it's executed from the command line. Is it a program you've written yourself or have the source code for? Just to be clear, it prints `Hello World` and the `waitpid()` returns I guess? – Ted Lyngmo Jan 29 '20 at 18:53
  • I dont have access to the source code. But I did try with a simple print.sh that just contains ```echo "Hello World"``` and that behaves the exact same as driverclear(Does not work). – adamk Jan 29 '20 at 19:14
  • Does "_Does not work_" mean that it hangs? – Ted Lyngmo Jan 29 '20 at 19:15
  • I'm not sure "hangs" is the correct term now that I look at it. I am not convinced the script is being called at all. The program continues after the waitpid() but crashes because the script was never executed. – adamk Jan 29 '20 at 19:19
  • If the `waitpid` returns, the `fork()`ed process is dead. If you didn't get `execl failed`, it most probably was executed. Just make the script do something traceable. `date >> logfile` or something similar. But do add the error checking on all places, like I did in my version of your program. – Ted Lyngmo Jan 29 '20 at 19:23

1 Answers1

0

Instead of using execl I just switched to using system("sh root/helpers/driverclear"); which fixes my problems.

adamk
  • 1
  • 1