⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util_funcs.c

📁 ucd-snmp源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * util_funcs.c */#include <config.h>#if HAVE_IO_H#include <io.h>#endif#include <stdio.h>#if HAVE_STDLIB_H#include <stdlib.h>#endif#if HAVE_MALLOC_H#include <malloc.h>#endif#include <sys/types.h>#ifdef __alpha#ifndef _BSD#define _BSD#define _myBSD#endif#endif#if HAVE_SYS_WAIT_H# include <sys/wait.h>#endif#ifdef __alpha#ifdef _myBSD#undef _BSD#undef _myBSD#endif#endif#ifndef WEXITSTATUS# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)#endif#ifndef WIFEXITED# define WIFEXITED(stat_val) (((stat_val) & 255) == 0)#endif#if TIME_WITH_SYS_TIME# ifdef WIN32#  include <sys/timeb.h># else#  include <sys/time.h># endif# include <time.h>#else# if HAVE_SYS_TIME_H#  include <sys/time.h># else#  include <time.h># endif#endif#if HAVE_UNISTD_H#include <unistd.h>#endif#if HAVE_FCNTL_H#include <fcntl.h>#endif#include <errno.h>#include <signal.h>#if HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif#include <ctype.h>#if HAVE_WINSOCK_H#include <winsock.h>#endif#if HAVE_NETINET_IN_H#include <netinet/in.h>#endif#if HAVE_BASETSD_H#include <basetsd.h>#define ssize_t SSIZE_T#endif#if HAVE_RAISE#define alarm raise#endif#include "mibincl.h"#include "struct.h"#include "util_funcs.h"#include "system.h"#if HAVE_LIMITS_H#include "limits.h"#endif#ifdef USING_UCD_SNMP_ERRORMIB_MODULE#include "ucd-snmp/errormib.h"#else#define setPerrorstatus(x) snmp_log_perror(x)#endif#include "read_config.h"#ifdef EXCACHETIMEstatic long cachetime;#endifextern int numprocs, numextens;voidExit(int var){  snmp_log(LOG_ERR, "Server Exiting with code %d\n",var);  exit(var);}static const char *make_tempfile(void){  static char	name[32];  int		fd = -1;  strcpy(name, "/tmp/snmpdXXXXXX");#ifdef HAVE_MKSTEMP  fd = mkstemp(name);#else  if (mktemp(name))    fd = open(name, O_CREAT|O_EXCL|O_WRONLY);#endif  if (fd >= 0) {    close(fd);    return name;  }  return NULL;}int shell_command(struct extensible *ex){#if HAVE_SYSTEM  const char *ofname;  char shellline[STRMAX];  FILE *shellout;    ofname = make_tempfile();  if (ofname == NULL) {    ex->output[0] = 0;    ex->result = 127;    return ex->result;  }  sprintf(shellline,"%s > %s",ex->command, ofname);  ex->result = system(shellline);  ex->result = WEXITSTATUS(ex->result);  shellout = fopen(ofname,"r");  if(shellout != NULL) {    if (fgets(ex->output,sizeof(ex->output),shellout) == NULL) {      ex->output[0] = 0;    }    fclose(shellout);  }  unlink(ofname);#else  ex->output[0] = 0;  ex->result = 0;#endif  return(ex->result);}#define MAXOUTPUT 300int exec_command(struct extensible *ex){#if HAVE_EXECV  int fd;  FILE *file;    if ((fd = get_exec_output(ex))) {    file = fdopen(fd,"r");    if (fgets(ex->output,sizeof(ex->output),file) == NULL) {      ex->output[0] = 0;    }    fclose(file);    wait_on_exec(ex);  } else#endif  {    ex->output[0] = 0;    ex->result = 0;  }  return(ex->result);}void wait_on_exec(struct extensible *ex){#ifndef EXCACHETIME    if (ex->pid && waitpid(ex->pid,&ex->result,0) < 0) {      setPerrorstatus("waitpid");    }    ex->pid = 0;#endif}#define MAXARGS 30int get_exec_output(struct extensible *ex){#if HAVE_EXECV  int fd[2],i, cnt;  char ctmp[STRMAX], *cptr1, *cptr2, argvs[STRMAX], **argv, **aptr;#ifdef EXCACHETIME  char cachefile[STRMAX];  char cache[MAXCACHESIZE];  ssize_t cachebytes;  long curtime;  static char lastcmd[STRMAX];  int cfd;  static int lastresult;  int readcount;#endif#ifdef EXCACHETIME  sprintf(cachefile, "%s/%s", PERSISTENT_DIRECTORY,CACHEFILE);  curtime = time(NULL);  if (curtime > (cachetime + EXCACHETIME) ||      strcmp(ex->command, lastcmd) != 0) {    strcpy(lastcmd,ex->command);    cachetime = curtime;#endif    if (pipe(fd))       {        setPerrorstatus("pipe");#ifdef EXCACHETIME        cachetime = 0;#endif        return 0;      }    if ((ex->pid = fork()) == 0)       {        close(1);        if (dup(fd[1]) != 1)          {            setPerrorstatus("dup");            return 0;          }        /* write standard output and standard error to pipe. */        /* close all other file descriptors. */        for (cnt=getdtablesize()-1; cnt>=2; --cnt)                (void) close(cnt);        (void) dup(1);                 /* stderr */        /* set standard input to /dev/null */        close(0);        (void) open("/dev/null", O_RDWR);        for(cnt=1,cptr1 = ex->command, cptr2 = argvs; cptr1 && *cptr1 != 0;            cptr2++, cptr1++) {          *cptr2 = *cptr1;          if (*cptr1 == ' ') {            *(cptr2++) = 0;            if ((cptr1 = skip_white(cptr1)) == NULL)                break;            if (cptr1) {                *cptr2 = *cptr1;                if (*cptr1 != 0) cnt++;            }          }        }        *cptr2 = 0;        *(cptr2+1) = 0;        argv = (char **) malloc((cnt+2) * sizeof(char *));        if (argv == NULL)          return 0; /* memory alloc error */        aptr = argv;        *(aptr++) = argvs;        for (cptr2 = argvs, i=1; i != cnt; cptr2++)          if (*cptr2 == 0) {            *(aptr++) = cptr2 + 1;            i++;          }        while (*cptr2 != 0) cptr2++;        *(aptr++) = NULL;        copy_word(ex->command,ctmp);        execv(ctmp,argv);        perror(ctmp);        exit(1);      }    else      {        close(fd[1]);        if (ex->pid < 0) {          close(fd[0]);          setPerrorstatus("fork");#ifdef EXCACHETIME          cachetime = 0;#endif          return 0;        }#ifdef EXCACHETIME        unlink(cachefile);	/* XXX  Use SNMP_FILEMODE_CLOSED instead of 644? */        if ((cfd = open(cachefile,O_WRONLY|O_TRUNC|O_CREAT,0644)) < 0) {          setPerrorstatus(cachefile);          cachetime = 0;          return 0;        }        fcntl(fd[0],F_SETFL,O_NONBLOCK);  /* don't block on reads */#ifdef HAVE_USLEEP        for (readcount = 0; readcount <= MAXREADCOUNT*100 &&               (cachebytes = read(fd[0],(void *)cache,MAXCACHESIZE));             readcount++) {#else        for (readcount = 0; readcount <= MAXREADCOUNT &&               (cachebytes = read(fd[0],(void *)cache,MAXCACHESIZE));             readcount++) {#endif          if (cachebytes > 0)            write(cfd,(void *) cache, cachebytes);          else if (cachebytes == -1 && errno != EAGAIN) {            setPerrorstatus("read");            break;          }          else#ifdef HAVE_USLEEP            usleep (10000);	/* sleeps for 0.01 sec */#else	    sleep (1);#endif        }        close(cfd);        close(fd[0]);        /* wait for the child to finish */        if (ex->pid > 0 && waitpid(ex->pid,&ex->result,0) < 0) {          setPerrorstatus("waitpid()");          cachetime = 0;          return 0;        }        ex->pid = 0;        ex->result = WEXITSTATUS(ex->result);        lastresult = ex->result;#else /* !EXCACHETIME */        return(fd[0]);#endif      }#ifdef EXCACHETIME  }  else {      ex->result = lastresult;  }  if ((cfd = open(cachefile,O_RDONLY)) < 0) {    setPerrorstatus(cachefile);    return 0;  }  return(cfd);#endif#else /* !HAVE_EXECV */  return 0;#endif}int get_exec_pipes(char *cmd,		   int *fdIn, 		   int *fdOut, 		   int *pid){#if HAVE_EXECV  int fd[2][2],i, cnt;  char ctmp[STRMAX], *cptr1, *cptr2, argvs[STRMAX], **argv, **aptr;  /* Setup our pipes */  if (pipe(fd[0]) || pipe(fd[1]))    {      setPerrorstatus("pipe");      return 0;    }  if ((*pid = fork()) == 0)   /* First handle for the child */    {      close(0);      if (dup(fd[0][0]) != 0)        {          setPerrorstatus("dup 0");          return 0;        }      close(1);      if (dup(fd[1][1]) != 1)        {          setPerrorstatus("dup 1");          return 0;        }        /* write standard output and standard error to pipe. */        /* close all non-standard open file descriptors */        for (cnt=getdtablesize()-1; cnt>=2; --cnt)                (void) close(cnt);        (void) dup(1);                 /* stderr */      for(cnt=1,cptr1 = cmd, cptr2 = argvs; *cptr1 != 0;          cptr2++, cptr1++) {        *cptr2 = *cptr1;        if (*cptr1 == ' ') {          *(cptr2++) = 0;          if ((cptr1 = skip_white(cptr1)) == NULL)              break;          *cptr2 = *cptr1;          if (*cptr1 != 0) cnt++;        }      }      *cptr2 = 0;      *(cptr2+1) = 0;      argv = (char **) malloc((cnt+2) * sizeof(char *));      if (argv == NULL)        return 0; /* memory alloc error */      aptr = argv;      *(aptr++) = argvs;      for (cptr2 = argvs, i=1; i != cnt; cptr2++)        if (*cptr2 == 0) {          *(aptr++) = cptr2 + 1;          i++;        }      while (*cptr2 != 0) cptr2++;      *(aptr++) = NULL;      copy_word(cmd,ctmp);      execv(ctmp,argv);      perror(ctmp);      exit(1);    }  else    {      close(fd[0][0]);      close(fd[1][1]);      if (*pid < 0) {        close(fd[0][1]);        close(fd[1][0]);        setPerrorstatus("fork");        return 0;      }      *fdIn = fd[1][0];      *fdOut = fd[0][1];      return(1); /* We are returning 0 for error... */    }#endif /* !HAVE_EXECV */  return 0;}int clear_cache(int action,		u_char *var_val,		u_char var_val_type,		size_t var_val_len,		u_char *statP,		oid *name,		size_t name_len){    long tmp=0;  if (var_val_type != ASN_INTEGER) {    snmp_log(LOG_NOTICE, "Wrong type != int\n");    return SNMP_ERR_WRONGTYPE;  }  tmp = *((long *) var_val);  if (tmp == 1 && action == COMMIT) {#ifdef EXCACHETIME    cachetime = 0;                      /* reset the cache next read */#endif   }   return SNMP_ERR_NOERROR;}char **argvrestartp, *argvrestartname, *argvrestart;RETSIGTYPE restart_doit(int a){    snmp_shutdown("snmpd");  /* do the exec */#if HAVE_EXECV  execv(argvrestartname,argvrestartp);  setPerrorstatus(argvrestartname);#endif}intrestart_hook(int action,	     u_char *var_val,	     u_char var_val_type,	     size_t var_val_len,	     u_char *statP,	     oid *name,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -