0

I'm trying to get my C++ program to reload/recreate part of an instance or just call a function while the program is deamonized without killing / restarting the entire thing.

How does Nginx approach the -s reload | stop even when demaon process is running in the background.

where it starts the program in deamon (default) and runs in the background. the re-call nginx -s reload to reload conf file changes

my main.cpp (code is basically pseudo since the program is somewhat large already)

int main(int argc, char *argv[])
{
    
    // daemonize function works fine
    daemonize();
    
    // this should not be stopped 
    initCore();

    // this function sets some vars based on CLI args
    getArg(argc,argv);

    // run var is set from above function from CLI ARGs. 
    // sometimes we only want to get the program version or help etc

    if(run == 1){
    // this function should be recreated 
       longfunction();
    }
    return 0;
}

Deamon Function that should get called on a reload

im looking for a way to reference this from a different instance and kill it to restart it.

void longfunction()
{
    syslog (LOG_NOTICE, "daemon started");

    // this function will read data from a file and set some vars
    loadConfig();

    // this funtion is a never ending that uses data from the loadConfig();
    startOp();

    // use a singal handler ??
}

According to the NGINX source, it uses a sort of cycle ()

  if (ngx_process == NGX_PROCESS_SINGLE) {
        ngx_single_process_cycle(cycle);

    } else {
        ngx_master_process_cycle(cycle);
    }

so far i've come up with this logic

  • start by main -d
  1. create main instance in deamon (-d)
  2. run longfunction while in deamon
  3. detach
  • reload by main -r
  1. check for previous instance (-r)
  2. Destroy that PID, recreate

supervisord, init.d and systemctl reload?

i need to use a test function before the actual reload to prevent reload on fail tests.

Why do i need this?

the initcore() function handles TCP incoming data, which should be fed continuously. that is the reason i am looking to reload the config without restarting it. solution does not need to be exactly like nginx's.

maybe communicating with a unix file/tcp ip?

There's Really not much info i could find to this particular usage.

deamonize function is overrides the usual deamon method. for win32 compatibility. so the solution needs to be win32 approachable too.

janithcooray
  • 188
  • 2
  • 15

0 Answers0