The code looks like this:
Copy Code code as follows:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <syslog.h>
#include <stdarg.h>
Char **bak_argv;
int flag = 0;
void restart (void);
void Sighup_handler (int sig);
Argv[0] Use absolute paths as much as possible, because ChDir () may be used during program execution.
int main (int argc, char *argv[])
{
Openlog ("Sig_hup", Log_pid, 0);
Syslog (Log_err, "%s", argv[0]);
Closelog ();
BAK_ARGV = argv;
Signal (Sighup, Sighup_handler);
while (1)
{
if (flag)
Restart ();
Sleep (10);
}
return 0;
}
void Sighup_handler (int sig)
{
flag = 1;
}
void restart (void)
{
Switch (fork ())
{
Case-1:
Openlog ("Sig_hup", Log_pid, 0);
Syslog (Log_err, "Fork failed:%s", Strerror (errno));
Closelog ();
Exit (Exit_failure);
Break
Case 0:
Break
Default
Exit (exit_success);
}
EXECV (Bak_argv[0], bak_argv);
Openlog ("Sig_hup", Log_pid, 0);
Syslog (Log_err, "Execv" (%s, ...) Failed:%s\n ", Bak_argv[0], strerror (errno));
Closelog ();
Exit (Exit_failure);
}