I am writing a Linux daemon to execute my code. My code makes a call to a third party library. If I execute my code from the parent then everything runs fine, but if I execute my code directly from a child the call to the third party library never returns. And if I create a second executable that executes my code and I have the daemon run the executable then everything runs fine.
Why can't I call my code from the child process?
int main(void)
{
// Our process ID and Session ID
pid_t pid, sid;
fflush(stdout);
// Fork off the parent process
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);
// If we got a good PID, then we can exit the parent process.
if (pid > 0)
exit(EXIT_SUCCESS);
// Change the file mode mask
umask(0);
// Open any logs here
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
if (open("/dev/null",O_RDONLY) == -1)
exit(EXIT_FAILURE);
if (open("/dev/null",O_WRONLY) == -1)
exit(EXIT_FAILURE);
if (open("/dev/null",O_WRONLY) == -1)
exit(EXIT_FAILURE);
// Create a new SID for the child process
sid = setsid();
if (sid < 0)
exit(EXIT_FAILURE);
// Change the current working directory
if ((chdir("/")) < 0)
exit(EXIT_FAILURE);
// doesn't work
MyObject ob;
ob.start();
// works
//execlp("/home/root/NextGenAutoGuidance", "NextGenAutoGuidance", (char*)NULL);
while(1)
{
sleep(60);
}
exit(EXIT_SUCCESS);
}
I have tried putting the object declaration of my object as a global and static global, I have also tried doing a new/delete of my object.
The only way the call to the third party library will return is if my object is started from the parent process.
How can I create the daemon so that I don't have to call an external binary to run correctly?
Edit
I need to add that I have also tried to not kill the parent and I have the same problem.