LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   execve fails with "No such file or directory" (https://www.linuxquestions.org/questions/programming-9/execve-fails-with-no-such-file-or-directory-466452/)

TTux 07-22-2006 01:02 AM

execve fails with "No such file or directory"
 
Hello,

I'm trying to do the following:

1. parent application is a simple QT app.
2. from the QT app, I fork a child
3. In the child, I call execve("mplayer", "/home/TTux/test.mpg", 0);

But the call to execve() fails. I can't figure out why.
Below is the code snippet:

void mparent::open_file()
{
char *args[1];
int launch_error;
QString s = QFileDialog::getOpenFileName("~",
"All Files (*.*)",
this,
"open file dialog",
"Open a file to play"
);

// save the child's pid in parent's context
this->mplayer = fork();

if(this->mplayer)
{
// in parent process
fprintf(stdout, "Child started successfully");
}
else
{
// in child process
args[0] = (char*) s.ascii();
launch_error = execve("mplayer", args, 0);
fprintf(stdout, "mplayer launch failed: error code %d\n", launch_error);
perror("error is");
}
}

The output is:

mplayer launch failed: error code -1
error is: No such file or directory
Xlib: unexpected async reply (sequence 0x30c)!

After this the parent's QT window (QMainWindow instance) does remain there, but after a few moment the whole programme hangs and then crashes. I don't know what's wrong with this code, and why is the Xlib printing out a message. The message "Child started successfully" never gets printed.

Please help.
Thanks!
Kapil

jlliagre 07-22-2006 01:27 AM

The arguments you pass to execve are wrong, no surprise it fails.

The first one should be the path to the command, you just give the name, which trigger the "no such file or directory".

The next argument should be an array of arguments, you only give one, and not the first that should be passed (arg[0]).

Try instead:

Code:

execl("/usr/bin/mplayer", "mplayer", "/home/TTux/test.mpg", 0);

TTux 07-22-2006 02:17 AM

Solved!
 
Hey thanks a lot!
Now my problem is solved. Here's what I have done:

{
char *args;
int launch_error;
QString s = QFileDialog::getOpenFileName("~",
"All Files (*.*)",
this,
"open file dialog",
"Open a file to play"
);

this->mplayer = fork();

// in parent process
if(this->mplayer)
{
fprintf(stdout, "Child started successfully\n");
}

// in child process
else
{
args = (char*) s.ascii();
launch_error = execlp("mplayer",
"mplayer",
args,
(char*)NULL);
fprintf(stdout, "mplayer launch failed: error code %d\n", launch_error);
perror("error is");
}
}

This works well :-)

Thanks again!


All times are GMT -5. The time now is 12:34 PM.