I want to run programs in linux by a button click an therefore I wrote a function execute
:
void execute(const char* program_call, const char* param )
{
pid_t child = vfork();
if(child == 0) // child process
{
int child_pid = getpid();
char *args[2]; // arguments for exec
args[0] = (char*)program_call; // first argument is program_call
args[1] = (char*)param;
// close all opened file descriptors:
const char* prefix = "/proc/";
const char* suffix = "/fd/";
char child_proc_dir[16];
sprintf(child_proc_dir,"%s%d%s",prefix,child_pid, suffix);
DIR *dir;
struct dirent *ent;
if ((dir = opendir (child_proc_dir)) != NULL) {
// get files and directories within directory
while ((ent = readdir (dir)) != NULL) {
// convert file name to int
char* end;
int fd = strtol(ent->d_name, &end, 32);
if (!*end) // valid file descriptor
{
close(fd); // close file descriptor
// or set the flag FD_CLOEXEC
//fcntl( fd, F_SETFD, FD_CLOEXEC );
}
}
closedir (dir);
}
else
{
cerr<< "can not open directory: " << child_proc_dir <<endl;
}
// replace the child process with exec*-function
execv(program_call,args);
_exit(2);
}
else if (child == -1) // fork error
{
if (errno == EAGAIN)
{
cerr<<“To much processes"<<endl;
}
else if (errno == ENOMEM)
{
cerr<<“Not enough space available."<<endl;
}
}
else // parent process
{
usleep(50); // give some time
if ( errno == EACCES)
{
cerr<<“Permission denied or process file not executable."<<endl;
}
else if ( errno == ENOENT)
{
cerr<<"\n Invalid path or file."<<endl;
}
int child_status;
if ( waitpid(child, &child_status, WNOHANG | WUNTRACED) < 0) // waitpid failed
{
cerr<<"Error - Execution failed"<<endl;
}
else if ( WIFEXITED( child_status ) && WEXITSTATUS( child_status ) != 0)
{
cerr<<“Child process error - Execution failed"<<endl;
}
}
}
There are two problems:
Closing the file descriptors causes some problems, for example Thunderbird crashes or VLC runs without sound. More exactly: closing of
stdout(1)
andstderr(2)
causes these problems. As I understand, closing file descriptor before exec only prevents them from been duplicated (there is no need to send informations from child process to parent process). Why does this affect the child process? Replacingclose()
by setting the flagFD_CLOEXEC
doesn't change anything. Also setting theFD_CLOEXEC
flag before fork doesn't solve the problem. Is there a better way to prevent inheritance of file descriptors?The return value of waitpid is often 0, even if the program call fails, I think because there are two (asynchrone) processes.
usleep(50)
solves this problem for my needs, but I hope there are better solutions for this problem.
I'm using vfork, but the same problems occur by using fork.