BACK to addon.html#config_edit

#include <unistd.h>
#include <signal.h>
#include <stdio.h>

#include "daemon-opts.h"

int reset = 0;

static void
handle_sighup(int sig)
{
    static char const * fake_av[2] = { "daemon", NULL };
    optionRestore(&daemonOptions);

    /*
     * We use a fake arg vector because we've already saved the initial
     * options into our config file.  Someone has changed that config file,
     * so we do not want to reprocess command line options.
     */
    (void)optionProcess(&daemonOptions, 1, (char **)fake_av);
    reset = 1;
}

int
main(int argc, char ** argv)
{
    optionSaveState(&daemonOptions);
    (void)optionProcess(&daemonOptions, argc, argv);

    /*
     *  Save the resulting state.  The command line options are augment
     *  the current config and are saved for future reference.
     */
    if (argc > 1)
        optionSaveFile(&daemonOptions);

    (void)signal(SIGHUP, handle_sighup);

    {
        FILE * fp = fopen("daemon.pid", "w");
        if (fp != NULL) {
            fprintf(fp, "%d\n", getpid());
            fclose(fp);
        }
    }

    for (;;) {
        printf("text: %-70s\r", OPT_ARG(TEXT));
        fflush(stdout);
        sleep(10);
        if (reset) {
            fputc('\n', stdout);
            reset = 0;
        }
    }
}