/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <signal.h>
#include <limits.h>
#include <unistd.h>
#include <errno.h>
#include "globals.h"
#include "daemon.h"
#include "opendd.h"
#include "./include/wrapper.h"
#include "./include/config_option.h"
void termination_handler(int signum) {
/* -----------------------------------------------------------------------------
* Signal handler
*
* @param int signum
* @return int
* -------------------------------------------------------------------------- */
char *config_file;
switch(signum) {
case SIGTERM:
close_lock_pidfile();
empty_lock_pidfile();
drop_all_option();
logmsg(LOG_INFO, "received a SIGTERM signal");
logmsg(LOG_INFO, "exiting");
set_config_file(NULL);
exit(-1);
break;
case SIGHUP:
logmsg(LOG_INFO, "received a SIGHUP signal");
config_file = get_config_file();
if (config_file == NULL)
config_file = CONFIG_FILE;
if (check_config_file_mode(config_file)) {
/* Reload the config file only if its file attributes are valid */
drop_all_option();
if (parse_config_file(config_file))
logmsg(LOG_INFO, "new configuration reloaded from %s", config_file);
else {
logmsg(LOG_ERR, "error in reloading configuration : %s", strerror(errno));
logmsg(LOG_INFO, "exiting");
set_config_file(NULL);
close_lock_pidfile();
exit(-1);
}
/* Set the OpenDD service online */
if (!is_service_online()) {
logmsg(LOG_INFO, "restoring the OpenDD service");
set_service_online(1);
}
}
break;
}
}
void sig_handler(int signum) {
/* -----------------------------------------------------------------------------
* Signal handler
*
* @param int signum
* @return int
* -------------------------------------------------------------------------- */
switch(signum) {
case SIGUSR1:
/* Custom signal to force DD update */
/* This signal should awake my_sleep() because it triggers a EINTR error in select() */
set_force_update_status(1);
break;
}
}
int setdaemon() {
/* -----------------------------------------------------------------------------
* Become a daemon process
*
* @return int
* -------------------------------------------------------------------------- */
int i = 0;
switch (fork()) {
case -1: /* Erreur fork */
logmsg(LOG_ERR, "setdaemon() : %s", strerror(errno));
return 0;
break;
case 0: /* Child process */
chdir("/");
setsid();
for (i = 0; i < _POSIX_OPEN_MAX; i++)
close(i);
break;
default: /* Parent process */
return 0;
}
signal(SIGTERM, termination_handler);
signal(SIGHUP, termination_handler);
signal(SIGUSR1, sig_handler);
return 1;
}
void my_sleep(int second) {
/* -----------------------------------------------------------------------------
* Sleep, implemented with select() system call
*
* @param int second
* -------------------------------------------------------------------------- */
struct timeval tm;
tm.tv_sec = second;
tm.tv_usec = 0;
select(0, NULL, NULL, NULL, &tm);
}
syntax highlighted by Code2HTML, v. 0.9.1