47 lines
1021 B
C
47 lines
1021 B
C
#define _GNU_SOURCE
|
|
#include <stdlib.h>
|
|
#include <sys/file.h>
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <syslog.h>
|
|
#include <signal.h>
|
|
#include <fcntl.h>
|
|
#include "log.h"
|
|
|
|
int pid_file;
|
|
|
|
void exitprogram(int status){
|
|
flock(pid_file, LOCK_UN);
|
|
unlink("/var/run/acsreader.pid");
|
|
close(pid_file);
|
|
|
|
writesyslog(LOG_INFO, "Goodbye and thanks for all the fish");
|
|
#ifdef TEST
|
|
printf("bye....\n");
|
|
#endif
|
|
exit(status);
|
|
}
|
|
|
|
void createpidfile(const char *location){
|
|
pid_file = open("/var/run/acsreader.pid", O_CREAT | O_RDWR, 0666);
|
|
int fl = flock(pid_file, LOCK_EX | LOCK_NB);
|
|
if(fl) {
|
|
if(EWOULDBLOCK == errno){
|
|
writesyslog(LOG_ERR, "Attempting to run duplicate instance of acsreader");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
}
|
|
|
|
void sighandler(int signo){
|
|
exitprogram(EXIT_SUCCESS);
|
|
}
|
|
|
|
void sighandlerinit(void){
|
|
if (signal(SIGINT | SIGKILL | SIGTERM | SIGQUIT, sighandler) == SIG_ERR){
|
|
writesyslog(LOG_ERR, "can't catch signals\n");
|
|
exitprogram(EXIT_FAILURE);
|
|
}
|
|
}
|