I have a C++ program that runs as a linux service. Some of the program's command line options simply set values in its configuration files and then exit, which then require the service to restart to pick up the new config. In order to allow the service to continue running uninterrupted, it works as follows:
- background service starts on system boot
- background service creates a 'config watchdog' thread monitoring config files
- user runs 'progname options' from command line
- config files modified
- command line instance of program exits
- background service config watchdog thread detects changes to config, triggers restart
When the program restarts after reading the new config, I am calling execv so that it will remain in the same process space as the original instance, so that it can continue to be managed as a service. The problem is that execv is not behaving as expected, and it is instead terminating the existing process and restarting in a new one. Because the PID no longer matches, if I attempt to run 'service progname stop/restart' after this point, it will not work properly, 'stop' will leave the service running, and 'restart' will spawn a duplicate instance of the program.
I have confirmed that argv[0] being passed to execv is the full path to the executable, so it should not be searching for the executable in the PATH via the shell (which should also be prevented by the fact that I'm using execv instead of execvp) which I have read about causing similar problems in other applications.