+ if (flags & FLAG_DAEMON) {
+ FILE *pidfile = NULL;
+
+ if (flags & FLAG_PID_FILE) {
+ mode_t old_umask;
+
+ old_umask = umask(022);
+ pidfile = fopen(PID_FILE, "w");
+ umask(old_umask);
+
+ if (!pidfile) {
+ perror("Can't create PID file " PID_FILE);
+ exit(EXIT_FAILURE);
+ }
+
+ memset(&sact, 0, sizeof(sact));
+ sact.sa_handler = sigterm_handler;
+
+ if (sigaction(SIGTERM, &sact, NULL) == -1) {
+ perror("sigaction(SIGTERM)");
+ exit(EXIT_FAILURE);
+ }
+ }
+