#include <sys/wait.h>
#include <err.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <db.h>
#include <rpc/rpc.h>
#include "statd.h"
struct sigaction sa;
int debug = 0;
int _rpcsvcdirty = 0;
static DB *db;
Header status_info;
static char undefdata[] = "\0\1\2\3\4\5\6\7";
static DBT undefkey = {
undefdata,
sizeof(undefdata)
};
extern char *__progname;
static int walk_one(int (*fun )(DBT *, HostInfo *, void *), DBT *, DBT *, void *);
static int walk_db(int (*fun )(DBT *, HostInfo *, void *), void *);
static int reset_host(DBT *, HostInfo *, void *);
static int check_work(DBT *, HostInfo *, void *);
static int unmon_host(DBT *, HostInfo *, void *);
static int notify_one(DBT *, HostInfo *, void *);
static void init_file(char *);
static int notify_one_host(char *);
static __dead void die(int);
int main(int, char **);
int
main(int argc, char **argv)
{
SVCXPRT *transp;
int ch;
struct sigaction nsa;
while ((ch = getopt(argc, argv, "d")) != (-1)) {
switch (ch) {
case 'd':
debug = 1;
break;
default:
fprintf(stderr, "usage: %s [-d]\n", __progname);
exit(1);
}
}
pmap_unset(SM_PROG, SM_VERS);
transp = svcudp_create(RPC_ANYSOCK);
if (transp == NULL) {
errx(1, "cannot create udp service.");
}
if (!svc_register(transp, SM_PROG, SM_VERS, sm_prog_1, IPPROTO_UDP)) {
errx(1, "unable to register (SM_PROG, SM_VERS, udp).");
}
transp = svctcp_create(RPC_ANYSOCK, 0, 0);
if (transp == NULL) {
errx(1, "cannot create tcp service.");
}
if (!svc_register(transp, SM_PROG, SM_VERS, sm_prog_1, IPPROTO_TCP)) {
errx(1, "unable to register (SM_PROG, SM_VERS, tcp).");
}
init_file("/var/db/statd.status");
daemon(0, 0);
sigemptyset(&nsa.sa_mask);
nsa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT;
nsa.sa_handler = SIG_IGN;
sigaction(SIGCHLD, &nsa, NULL);
openlog("rpc.statd", 0, LOG_DAEMON);
if (debug)
syslog(LOG_INFO, "Starting - debug enabled");
else
syslog(LOG_INFO, "Starting");
sa.sa_handler = die;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sa.sa_handler = SIG_IGN;
sa.sa_flags = SA_RESTART;
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask, SIGALRM);
notify_handler(0);
while (1)
svc_run();
die(0);
}
void
notify_handler(int sig)
{
time_t now;
NO_ALARM;
sa.sa_handler = SIG_IGN;
sigaction(SIGALRM, &sa, NULL);
now = time(NULL);
walk_db(notify_one, &now);
if (walk_db(check_work, &now) == 0) {
CLR_ALARM;
return;
}
sync_file();
ALARM;
alarm(5);
}
void
sync_file()
{
DBT data;
data.data = &status_info;
data.size = sizeof(status_info);
switch ((*db->put)(db, &undefkey, &data, 0)) {
case 0:
return;
case -1:
goto bad;
default:
abort();
}
if ((*db->sync)(db, 0) == -1) {
bad:
syslog(LOG_ERR, "database corrupted %m");
die(1);
}
}
void
change_host(char *hostnamep, HostInfo *hp)
{
DBT key, data;
char *ptr;
char hostname[HOST_NAME_MAX+1 + 1];
HostInfo h;
strlcpy(hostname, hostnamep, sizeof(hostname));
h = *hp;
for (ptr = hostname; *ptr; ptr++)
if (isupper((unsigned char) *ptr))
*ptr = tolower((unsigned char) *ptr);
key.data = hostname;
key.size = ptr - hostname + 1;
data.data = &h;
data.size = sizeof(h);
switch ((*db->put)(db, &key, &data, 0)) {
case -1:
syslog(LOG_ERR, "database corrupted %m");
die(1);
case 0:
return;
default:
abort();
}
}
HostInfo *
find_host(char *hostname, HostInfo *hp)
{
DBT key, data;
char *ptr;
for (ptr = hostname; *ptr; ptr++)
if (isupper((unsigned char) *ptr))
*ptr = tolower((unsigned char) *ptr);
key.data = hostname;
key.size = ptr - hostname + 1;
switch ((*db->get)(db, &key, &data, 0)) {
case 0:
if (data.size != sizeof(*hp))
goto bad;
return memcpy(hp, data.data, sizeof(*hp));
case 1:
return NULL;
case -1:
goto bad;
default:
abort();
}
bad:
syslog(LOG_ERR, "Database corrupted %m");
return NULL;
}
static int
walk_one(int (*fun)(DBT *, HostInfo *, void *), DBT *key, DBT *data, void *ptr)
{
HostInfo h;
if (key->size == undefkey.size &&
memcmp(key->data, undefkey.data, key->size) == 0)
return 0;
if (data->size != sizeof(HostInfo)) {
syslog(LOG_ERR, "Bad data in database");
die(1);
}
memcpy(&h, data->data, sizeof(h));
return (*fun)(key, &h, ptr);
}
static int
walk_db(int (*fun)(DBT *, HostInfo *, void *), void *ptr)
{
DBT key, data;
switch ((*db->seq)(db, &key, &data, R_FIRST)) {
case -1:
goto bad;
case 1:
abort();
case 0:
if (walk_one(fun, &key, &data, ptr) == -1)
return -1;
break;
default:
abort();
}
for (;;)
switch ((*db->seq)(db, &key, &data, R_NEXT)) {
case -1:
goto bad;
case 0:
if (walk_one(fun, &key, &data, ptr) == -1)
return -1;
break;
case 1:
return 0;
default:
abort();
}
bad:
syslog(LOG_ERR, "Corrupted database %m");
die(1);
}
static int
reset_host(DBT *key, HostInfo *hi, void *ptr)
{
if (hi->monList) {
hi->notifyReqd = *(time_t *) ptr;
hi->attempts = 0;
hi->monList = NULL;
change_host((char *)key->data, hi);
}
return 0;
}
static int
check_work(DBT *key, HostInfo *hi, void *ptr)
{
return hi->notifyReqd ? -1 : 0;
}
static int
unmon_host(DBT *key, HostInfo *hi, void *ptr)
{
char *name = key->data;
if (do_unmon(name, hi, ptr))
change_host(name, hi);
return 0;
}
static int
notify_one(DBT *key, HostInfo *hi, void *ptr)
{
time_t now = *(time_t *) ptr;
char *name = key->data;
int error;
if (hi->notifyReqd == 0 || hi->notifyReqd > now)
return 0;
if (notify_one_host(name) || hi->attempts++ >= 44) {
error = 0;
hi->notifyReqd = 0;
hi->attempts = 0;
} else {
error = -1;
if (hi->attempts < 10)
hi->notifyReqd += 5;
else if (hi->attempts < 20)
hi->notifyReqd += 60;
else
hi->notifyReqd += 60 * 60;
}
change_host(name, hi);
return error;
}
static void
init_file(char *filename)
{
DBT data;
db = dbopen(filename, O_RDWR|O_CREAT|O_NDELAY|O_EXLOCK, 0644, DB_HASH,
NULL);
if (db == NULL)
err(1, "Cannot open `%s'", filename);
switch ((*db->get)(db, &undefkey, &data, 0)) {
case 1:
memset(&status_info, 0, sizeof(status_info));
sync_file();
return;
case -1:
err(1, "error accessing database (%m)");
case 0:
if (data.size != sizeof(status_info))
errx(1, "database corrupted %lu != %lu",
(u_long)data.size, (u_long)sizeof(status_info));
memcpy(&status_info, data.data, data.size);
break;
default:
abort();
}
reset_database();
return;
}
void
reset_database(void)
{
time_t now = time(NULL);
walk_db(reset_host, &now);
status_info.ourState =
(status_info.ourState + 2) & 0xfffffffe;
status_info.ourState++;
sync_file();
}
void
unmon_hosts(void)
{
time_t now = time(NULL);
walk_db(unmon_host, &now);
sync_file();
}
static int
notify_one_host(char *hostname)
{
struct timeval timeout = {20, 0};
CLIENT *cli;
char dummy;
stat_chge arg;
char our_hostname[HOST_NAME_MAX+1 + 1];
gethostname(our_hostname, sizeof(our_hostname));
our_hostname[sizeof(our_hostname) - 1] = '\0';
arg.mon_name = our_hostname;
arg.state = status_info.ourState;
if (debug)
syslog(LOG_DEBUG, "Sending SM_NOTIFY to host %s from %s",
hostname, our_hostname);
cli = clnt_create(hostname, SM_PROG, SM_VERS, "udp");
if (!cli) {
syslog(LOG_ERR, "Failed to contact host %s%s", hostname,
clnt_spcreateerror(""));
return (FALSE);
}
if (clnt_call(cli, SM_NOTIFY, xdr_stat_chge, &arg, xdr_void,
&dummy, timeout) != RPC_SUCCESS) {
syslog(LOG_ERR, "Failed to contact rpc.statd at host %s",
hostname);
clnt_destroy(cli);
return (FALSE);
}
clnt_destroy(cli);
return (TRUE);
}
static __dead void
die(int n)
{
(*db->close)(db);
exit(n);
}