| 1 |
/* |
| 2 |
* util_funcs.c |
| 3 |
*/ |
| 4 |
/* |
| 5 |
* Portions of this file are copyrighted by: |
| 6 |
* Copyright Copyright 2003 Sun Microsystems, Inc. All rights reserved. |
| 7 |
* Use is subject to license terms specified in the COPYING file |
| 8 |
* distributed with the Net-SNMP package. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <net-snmp/net-snmp-config.h> |
| 12 |
|
| 13 |
#ifdef HAVE_UNISTD_H |
| 14 |
#include <unistd.h> |
| 15 |
#endif |
| 16 |
#include <signal.h> |
| 17 |
|
| 18 |
#include <net-snmp/net-snmp-includes.h> |
| 19 |
#include <net-snmp/library/snmp_logging.h> |
| 20 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 21 |
#include "restart.h" |
| 22 |
|
| 23 |
#ifdef USING_UCD_SNMP_ERRORMIB_MODULE |
| 24 |
#include "ucd-snmp/errormib.h" |
| 25 |
#else |
| 26 |
#define setPerrorstatus(x) snmp_log_perror(x) |
| 27 |
#endif |
| 28 |
|
| 29 |
char **argvrestartp, *argvrestartname, *argvrestart; |
| 30 |
|
| 31 |
#ifdef SIGALRM |
| 32 |
static RETSIGTYPE |
| 33 |
restart_doit(int a) |
| 34 |
{ |
| 35 |
char * name = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, |
| 36 |
NETSNMP_DS_LIB_APPTYPE); |
| 37 |
snmp_shutdown(name); |
| 38 |
|
| 39 |
/* This signal handler may run with SIGALARM blocked. |
| 40 |
* Since the signal mask is preserved accross execv(), we must |
| 41 |
* make sure that SIGALARM is unblocked prior of execv'ing. |
| 42 |
* Otherwise SIGALARM will be ignored in the next incarnation |
| 43 |
* of snmpd, because the signal is blocked. And thus, the |
| 44 |
* restart doesn't work anymore. |
| 45 |
* |
| 46 |
* A quote from the sigprocmask() man page: |
| 47 |
* The use of sigprocmask() is unspecified in a multithreaded process; see |
| 48 |
* pthread_sigmask(3). |
| 49 |
*/ |
| 50 |
#ifdef HAVE_SIGPROCMASK |
| 51 |
{ |
| 52 |
sigset_t empty_set; |
| 53 |
|
| 54 |
sigemptyset(&empty_set); |
| 55 |
sigprocmask(SIG_SETMASK, &empty_set, NULL); |
| 56 |
} |
| 57 |
#elif defined(HAVE_SIGBLOCK) |
| 58 |
sigsetmask(0); |
| 59 |
#endif |
| 60 |
|
| 61 |
/* |
| 62 |
* do the exec |
| 63 |
*/ |
| 64 |
#ifdef HAVE_EXECV |
| 65 |
execv(argvrestartname, argvrestartp); |
| 66 |
setPerrorstatus(argvrestartname); |
| 67 |
#endif |
| 68 |
} |
| 69 |
#endif |
| 70 |
|
| 71 |
int |
| 72 |
restart_hook(int action, |
| 73 |
u_char * var_val, |
| 74 |
u_char var_val_type, |
| 75 |
size_t var_val_len, |
| 76 |
u_char * statP, oid * name, size_t name_len) |
| 77 |
{ |
| 78 |
|
| 79 |
long tmp = 0; |
| 80 |
|
| 81 |
if (var_val_type != ASN_INTEGER) { |
| 82 |
snmp_log(LOG_NOTICE, "Wrong type != int\n"); |
| 83 |
return SNMP_ERR_WRONGTYPE; |
| 84 |
} |
| 85 |
tmp = *((long *) var_val); |
| 86 |
if (tmp == 1 && action == COMMIT) { |
| 87 |
#ifdef SIGALRM |
| 88 |
signal(SIGALRM, restart_doit); |
| 89 |
alarm(NETSNMP_RESTARTSLEEP); |
| 90 |
#endif |
| 91 |
} |
| 92 |
return SNMP_ERR_NOERROR; |
| 93 |
} |