0

I have been writing a linux daemon which listens on TCP/IP for a request and launches an application on receiving that request. My problem is when I run this daemon from command prompt or IDE (eclipse 3.7) everything works fine and my executable launches. But when i use
sudo service <myservicename> start It will receive request on socket but its not launching that executable.

here is the standard code I am using for daemonizing the process /// Linux Daemon related stuff

/// Create the lock file as the current user 
int lfp = open( "/var/lock/subsys/LauncherService", O_RDWR|O_CREAT, 0640);
if ( lfp < 0 ) 
{
    LOG_ERROR ("Unable to open lockfile");
    LOG_ERROR ( strerror(errno) );
}

/// All
/// Our process ID and Session ID
pid_t pid, sid;

/// 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);

/// Create a new SID for the child process
sid = setsid();
if (sid < 0)
{
    LOG_ERROR ("Error Setting sid");
    exit(EXIT_FAILURE);
}
LOG_INFO ("sid set");

/// Change the current working directory
if ( (chdir("/usr/local/<mylocaldir>/bin")) < 0 )
{
       LOG_ERROR ("Error changing Directory");
       exit(EXIT_FAILURE);
}
LOG_INFO ("chdir successful");

/* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);

here is function in which i am launching my binary which needs to be launched. This function is called in forked child process.

std::string configFile = "/usr/local/<mydir>/config/Settings.config";
std::string binary = "<binaryname>";
std::string path ="/usr/local/<mydir>/bin/<binaryname>";

//char *argv[] = { "<binaryname>", "/usr/local/<mydir>/config/Settings.config", (char *)0 };

LOG_INFO("Calling Process" );
if ( execlp( path.c_str(), binary.c_str(), configFile.c_str(), (char *)0 ) == -1 )
//if ( execv("/usr/local/<mydir>/bin/<binaryname>", argv) == -1 )
//if ( execvp("/usr/local/<mydir>/bin/<binaryname>", argv) == -1 )
{
    LOG_ERROR("System call failed !!")
    std::string errString = strerror(errno);
    LOG_ERROR (errString );
}
else
{        
    LOG_INFO("System call successful");
}
Abhishek Bansal
  • 5,197
  • 4
  • 40
  • 69
  • What error is it logging? – Casey Jun 07 '13 at 18:05
  • its not logging anything. I think its just blocking or its because current process image is getting replaced I am not able to get any logs. – Abhishek Bansal Jun 07 '13 at 18:41
  • If it's not logging an error, then the `execlp` is succeeding. You should be trying to figure out why the *other* program isn't working correctly. – Casey Jun 07 '13 at 18:50
  • ok when I run this service from init.d script I set certain environment variables like `LD_LIBRARY_PATH` would they be still available to this child process ? My concern is this code works perfectly fine with IDE or command line but it only does not work with system service. If it succeeds then atleast it should log success message that also I am not getiing. I am getting Logs till "Calling Process" – Abhishek Bansal Jun 07 '13 at 18:56
  • 1
    If an `exec()` succeeds, it never returns: the new program is loaded into the process and starts executing. The `else` branch in your calling program is dead code. – Casey Jun 07 '13 at 19:10
  • Ok Thanks ! this means at-least exec is succeeding then it might be the issue with environment variables only. Do you know if setting environment variables in init.d script is sufficient for new process also ? – Abhishek Bansal Jun 07 '13 at 19:24

1 Answers1

0

So after discussion with Casey I investigated more into my called program and I found that my program indeed is getting called. Also I found out that environment variables are not the issue child process is taking environment from parent itself. I am creating QApplication (qt gui application) in my main program. Its some issue with that and linux system daemon. I will try to figure that out and will ask separate question if needed.

Edit: Final Solution It was a qt GUI application which was not able to connect to XServer. I had to changes suggested by Casey and given in this post Cannot connect to X server :0.0 with a Qt application

after that it started launching.

Community
  • 1
  • 1
Abhishek Bansal
  • 5,197
  • 4
  • 40
  • 69