initial work on daemon
This commit is contained in:
parent
ff3f30d3a6
commit
08aca40316
@ -6,13 +6,13 @@ AC_LANG([C])
|
|||||||
AM_INIT_AUTOMAKE
|
AM_INIT_AUTOMAKE
|
||||||
|
|
||||||
# Checks for programs.
|
# Checks for programs.
|
||||||
AC_PROG_CC
|
AC_PROG_CC_C99
|
||||||
|
|
||||||
# Checks for libraries.
|
# Checks for libraries.
|
||||||
PKG_CHECK_MODULES([gammu], [gammu >= 1.0])
|
PKG_CHECK_MODULES([gammu], [gammu >= 1.0])
|
||||||
|
|
||||||
# Checks for header files.
|
# Checks for header files.
|
||||||
AC_CHECK_HEADERS([fcntl.h stdlib.h string.h syslog.h unistd.h])
|
AC_CHECK_HEADERS([fcntl.h signal.h stdlib.h string.h syslog.h unistd.h])
|
||||||
|
|
||||||
# Checks for typedefs, structures, and compiler characteristics.
|
# Checks for typedefs, structures, and compiler characteristics.
|
||||||
|
|
||||||
|
184
yasnd.c
184
yasnd.c
@ -1,63 +1,149 @@
|
|||||||
// #include <gammu/gammu.h>
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
|
#include <signal.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
// #include <gammu/gammu.h>
|
||||||
|
|
||||||
|
// Target hosts count
|
||||||
|
#define NUM_HOSTS 3
|
||||||
|
|
||||||
|
// Should we do excessive debug logging or not
|
||||||
|
#define DEBUG
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char* hostname; // address of host
|
||||||
|
pid_t helper_pid; // pid of helper('pinger') child process
|
||||||
|
int fail_count; // how many times in a row host was unreachable
|
||||||
|
} host_decl;
|
||||||
|
|
||||||
|
host_decl hosts[NUM_HOSTS];
|
||||||
|
|
||||||
|
void init()
|
||||||
|
{
|
||||||
|
hosts[0].hostname="10.0.0.1";
|
||||||
|
hosts[0].fail_count=0;
|
||||||
|
hosts[1].hostname="10.0.0.2";
|
||||||
|
hosts[1].fail_count=0;
|
||||||
|
hosts[2].hostname="10.0.0.254";
|
||||||
|
hosts[2].fail_count=0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void log_event(const char *message)
|
||||||
|
{
|
||||||
|
#ifdef DEBUG
|
||||||
|
syslog(LOG_INFO,"%s",message);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void check_host(int num,host_decl host)
|
||||||
|
{
|
||||||
|
pid_t pid = fork();
|
||||||
|
if (pid < 0) {
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
// If pid == 0, then we are in the child
|
||||||
|
if (pid == 0) {
|
||||||
|
char tmp[50];
|
||||||
|
sprintf(tmp, "Pinging host %s", host.hostname);
|
||||||
|
log_event(tmp);
|
||||||
|
execl("/bin/ping","/bin/ping","-c 4","-n",host.hostname,(char*) 0);
|
||||||
|
// STUB: check result of exec call
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
// Save child's pid
|
||||||
|
host.helper_pid=pid;
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop_function()
|
||||||
|
{
|
||||||
|
for (int i=0;i<NUM_HOSTS;i++)
|
||||||
|
{
|
||||||
|
check_host(i,hosts[i]);
|
||||||
|
}
|
||||||
|
for (int i=0;i<NUM_HOSTS;i++)
|
||||||
|
{
|
||||||
|
int pid_status;
|
||||||
|
int result = waitpid(hosts[i].helper_pid, &pid_status, NULL);
|
||||||
|
if (result > 0)
|
||||||
|
{
|
||||||
|
if (WIFEXITED(pid_status))
|
||||||
|
{
|
||||||
|
char tmp[50];
|
||||||
|
sprintf(tmp, "Finished child %d with result %d", result, WEXITSTATUS(pid_status));
|
||||||
|
log_event(tmp);
|
||||||
|
// if host is alive - reset failure counter
|
||||||
|
// otherwise - increment it
|
||||||
|
if (WEXITSTATUS(pid_status) == 0)
|
||||||
|
hosts[i].fail_count=0;
|
||||||
|
else
|
||||||
|
hosts[i].fail_count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void termination_handler(int signum)
|
||||||
|
{
|
||||||
|
closelog();
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
/* Our process ID and Session ID */
|
||||||
/* Our process ID and Session ID */
|
pid_t pid, sid;
|
||||||
pid_t pid, sid;
|
|
||||||
|
|
||||||
/* Fork off the parent process */
|
|
||||||
pid = fork();
|
|
||||||
if (pid < 0) {
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
/* If we got a good PID, then
|
|
||||||
we can exit the parent process. */
|
|
||||||
if (pid > 0) {
|
|
||||||
exit(EXIT_SUCCESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Change the file mode mask */
|
/* Fork off the parent process */
|
||||||
umask(0);
|
pid = fork();
|
||||||
|
if (pid < 0) {
|
||||||
/* Open any logs here */
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
/* Create a new SID for the child process */
|
/* If we got a good PID, then we can exit the parent process. */
|
||||||
sid = setsid();
|
if (pid > 0) {
|
||||||
if (sid < 0) {
|
exit(EXIT_SUCCESS);
|
||||||
/* Log the failure */
|
}
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Change the file mode mask */
|
||||||
/* Change the current working directory */
|
umask(0);
|
||||||
if ((chdir("/")) < 0) {
|
|
||||||
/* Log the failure */
|
/* Open any logs here */
|
||||||
exit(EXIT_FAILURE);
|
openlog("yasnd", LOG_PID|LOG_CONS, LOG_USER);
|
||||||
}
|
|
||||||
|
/* Create a new SID for the child process */
|
||||||
/* Close out the standard file descriptors */
|
sid = setsid();
|
||||||
close(STDIN_FILENO);
|
if (sid < 0) {
|
||||||
close(STDOUT_FILENO);
|
/* Log the failure */
|
||||||
close(STDERR_FILENO);
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
/* Daemon-specific initialization goes here */
|
|
||||||
|
/* Change the current working directory */
|
||||||
/* The Big Loop */
|
if ((chdir("/")) < 0) {
|
||||||
while (1) {
|
/* Log the failure */
|
||||||
/* Do some task here ... */
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
sleep(30); /* wait 30 seconds */
|
|
||||||
}
|
/* Close out the standard file descriptors */
|
||||||
exit(EXIT_SUCCESS);
|
close(STDIN_FILENO);
|
||||||
|
close(STDOUT_FILENO);
|
||||||
|
close(STDERR_FILENO);
|
||||||
|
|
||||||
|
// Set signal handler for SIGTERM
|
||||||
|
signal(SIGTERM, termination_handler);
|
||||||
|
|
||||||
|
// Init apropriate structures
|
||||||
|
init();
|
||||||
|
|
||||||
|
/* The Big Loop */
|
||||||
|
while (1) {
|
||||||
|
loop_function();
|
||||||
|
// sleep(60); /* wait 60 seconds */
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user