struct pidfile* pidfile_create(const char *path, enum pidfile_mode mode, int dbglev) { int filepid; filepid = pidfile_check(path); if(filepid == 0){ return pidfile_create_(path, mode, dbglev); } else if (filepid < 0){ return NULL; } fprintf(stderr, "Application is allready running (pid: %d)\n", filepid); switch(mode){ case PMODE_HUP: kill (filepid, SIGHUP); /* continues */ case PMODE_RETURN: return NULL; case PMODE_KILL: kill(filepid, SIGKILL); return pidfile_create_(path, mode, dbglev); case PMODE_EXIT: exit(0); } return NULL; }
/* returns 0 on success, 1 if file already exists and process is running and -1 on error */ int pidfile_create (char *pidfile, pid_t pid) { int fh, rc; FILE *fp; while (TRUE) { /* we try to create pidfile but fail if it already exists */ fh = open (pidfile, O_RDWR|O_CREAT|O_EXCL, 0666); if (fh >= 0) break; if (errno != EEXIST) return -1; /* if process is already active return 1 */ rc = pidfile_check (pidfile); switch (rc) { case 1: return 1; case -1: return -1; } } fp = fdopen (fh, "w"); if (fp == NULL) { close (fh); return -1; } fprintf (fp, "%d\n", (int)pid); if (fclose (fp) < 0) return -1; return 0; }
/** * main() - entry point * * Declare VRRP instance, init daemon * and launch state machine */ int main(int argc, char *argv[]) { signal_setup(); /* Current VRRP instance */ struct vrrp vrrp; struct vrrp_net vnet; /* Init VRRP instance */ vrrp_init(&vrrp); vrrp_net_init(&vnet); /* cmdline options */ if (! !vrrp_options(&vrrp, &vnet, argc, argv)) exit(EXIT_FAILURE); /* pidfile init && check */ if (pidfile_init(vrrp.vrid) != 0) exit(EXIT_FAILURE); pidfile_check(vrrp.vrid); /* logs */ log_open("uvrrpd", (char const *) loglevel); /* open sockets */ if ((vrrp_net_socket(&vnet) != 0) || (vrrp_net_socket_xmit(&vnet) != 0)) exit(EXIT_FAILURE); /* hook script */ if (vrrp_exec_init(&vrrp) != 0) exit(EXIT_FAILURE); /* advertisement pkt */ if (vrrp_adv_init(&vnet, &vrrp) != 0) exit(EXIT_FAILURE); /* net topology */ if (vnet.family == AF_INET) { if (vrrp_arp_init(&vnet) != 0) exit(EXIT_FAILURE); } else if (vnet.family == AF_INET6) { if (vrrp_na_init(&vnet) != 0) exit(EXIT_FAILURE); } /* daemonize */ if (background) { daemon(0, (log_trigger(NULL) > LOG_INFO)); } else chdir("/"); /* pidfile */ pidfile(vrrp.vrid); /* process */ set_bit(KEEP_GOING, ®); while (test_bit(KEEP_GOING, ®) && !vrrp_process(&vrrp, &vnet)); /* shutdown */ vrrp_adv_cleanup(&vnet); if (vnet.family == AF_INET) vrrp_arp_cleanup(&vnet); else /* AF_INET6 */ vrrp_na_cleanup(&vnet); vrrp_cleanup(&vrrp); vrrp_exec_cleanup(&vrrp); vrrp_net_cleanup(&vnet); log_close(); free(loglevel); pidfile_unlink(); free(pidfile_name); return EXIT_SUCCESS; }
int main(int argc, char **argv) { if (1 == argc) { utlog(LOG_ERR, "No configuration file was specified\n\n"); print_help(EXIT_FAILURE); } int c; int foreground = 0; char *config_file = ""; while (1) { static struct option long_options[] = { {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'v'}, {"config", required_argument, 0, 'c'}, {"foreground", no_argument, 0, 'f'}, {0, 0, 0, 0} }; // getopt_long stores the option index here. int option_index = 0; c = getopt_long(argc, argv, "hvfc:", long_options, &option_index); // Detect the end of the options. if (c == -1) { break; } switch (c) { case 'h': print_help(EXIT_SUCCESS); break; case 'v': print_version(EXIT_SUCCESS); break; case 'c': config_file = optarg; break; case 'f': foreground = 1; break; case '?': // getopt_long already printed an error message. utlog(LOG_INFO, "\n"); print_help(EXIT_FAILURE); break; default: break; } } pid_t pid; if (0 != (pid = pidfile_check(pidfile))) { utlog(LOG_ERR, "A instance of avm-motion-triggerd (%d) is already running\n", pid); exit(EXIT_FAILURE); } conf = get_config(config_file); validate_config(&conf); // Run as daemon if we should if (0 == foreground) { daemonize(); utlog_mode(LOG_BACKGROUND); utlog_pri_mode(LOG_PRI_DISABLE); } else { utlog_mode(LOG_FOREGROUND); utlog_pri_mode(LOG_PRI_ENABLE); } // Write a pidfile for the current daemon process if (0 == pidfile_write(pidfile)) { exit(EXIT_FAILURE); } // Bind the signal handler signal(SIGINT, handle_signal); signal(SIGTERM, handle_signal); // Initialize the sensors init_sensors(&conf); // Call the business logic loop detect_motions(&conf); return EXIT_SUCCESS; }