util_funcs.c

来自「eCos操作系统源码」· C语言 代码 · 共 791 行 · 第 1/2 页

C
791
字号
#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");          return 0;        }      close(1);      if (dup(fd[1][1]) != 1)        {          setPerrorstatus("dup");          return 0;        }      close(fd[0][0]);      close(fd[0][1]);      close(fd[1][0]);      close(fd[1][1]);      for(cnt=1,cptr1 = cmd, cptr2 = argvs; *cptr1 != 0;          cptr2++, cptr1++) {        *cptr2 = *cptr1;        if (*cptr1 == ' ') {          *(cptr2++) = 0;          cptr1 = skip_white(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(cmd,ctmp);      execv(ctmp,argv);      snmp_log_perror("execv");      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){  int i;    /* close everything open */  for (i=0; i<= 2; i++)    close(i);  /* do the exec */#if HAVE_EXECV  execv(argvrestartname,argvrestartp);  setPerrorstatus("execv");#endif}intrestart_hook(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 SIGALRM    signal(SIGALRM,restart_doit);#endif#ifndef __ECOS    alarm(RESTARTSLEEP);#endif  }   return SNMP_ERR_NOERROR;}voidprint_mib_oid(oid name[],	      size_t len){  char *buffer;  buffer=malloc(11*len); /* maximum digit lengths for int32 + a '.' */  if (!buffer) {    snmp_log(LOG_ERR, "Malloc failed - out of memory?");    return;  }  sprint_mib_oid(buffer, name, len);  snmp_log(LOG_NOTICE, "Mib: %s\n", buffer);  free(buffer);}voidsprint_mib_oid(char *buf,	       oid name[],	       size_t len){  int i;  for(i=0; i < (int)len; i++) {    sprintf(buf,".%d",(int) name[i]);    while (*buf != 0)      buf++;  }}/*******************************************************************-o-****** * header_simple_table * * Parameters: *	  *vp		 Variable data. *	  *name		 Fully instantiated OID name. *	  *length	 Length of name. *	   exact	 TRUE if an exact match is desired. *	  *var_len	 Hook for size of returned data type. *	(**write_method) Hook for write method (UNUSED). *	   max *       * Returns: *	0	If name matches vp->name (accounting for 'exact') and is *			not greater in length than 'max'. *	1	Otherwise. * * * Compare 'name' to vp->name for the best match or an exact match (if *	requested).  Also check that 'name' is not longer than 'max' if *	max is greater-than/equal 0. * Store a successful match in 'name', and increment the OID instance if *	the match was not exact.   * * 'name' and 'length' are undefined upon failure. * */int header_simple_table(struct variable *vp, oid *name, size_t *length,                        int exact, size_t *var_len,                        WriteMethod **write_method, int max){  int	i,	rtest;	/* Set to:	-1	If name < vp->name,	 	 *		1	If name > vp->name,		 *		0	Otherwise.		 */  oid newname[MAX_OID_LEN];  for(i=0,rtest=0; i < (int) vp->namelen && i < (int)(*length) && !rtest; i++) {    if (name[i] != vp->name[i]) {      if (name[i] < vp->name[i])         rtest = -1;      else        rtest = 1;    }  }  if (rtest > 0 ||      (rtest == 0 && !exact && (int)(vp->namelen+1) < (int) *length) ||    (exact == 1 && (rtest || (int)*length != (int)(vp->namelen+1)))) {    if (var_len)	*var_len = 0;    return MATCH_FAILED;  }  memset(newname, 0, sizeof(newname));  if (((int) *length) <= (int) vp->namelen || rtest == -1) {    memmove(newname, vp->name, (int)vp->namelen * sizeof (oid));    newname[vp->namelen] = 1;    *length = vp->namelen+1;  }  else {    *length = vp->namelen+1;    memmove(newname, name, (*length) * sizeof(oid));    if (!exact)      newname[*length-1] = name[*length-1] + 1;    else      newname[*length-1] = name[*length-1];  }    if (max >= 0 && ((int)newname[*length-1] > max)) {    if(var_len)      *var_len = 0;    return MATCH_FAILED;  }  memmove(name, newname, (*length) * sizeof(oid));   if (write_method)    *write_method = 0;  if (var_len)    *var_len = sizeof(long);   /* default */  return(MATCH_SUCCEEDED);}/*  header_generic(...  Arguments:  vp	  IN      - pointer to variable entry that points here  name    IN/OUT  - IN/name requested, OUT/name found  length  IN/OUT  - length of IN/OUT oid's   exact   IN      - TRUE if an exact match was requested  var_len OUT     - length of variable or 0 if function returned  write_method  *//*******************************************************************-o-****** * generic_header * * Parameters: *	  *vp	   (I)     Pointer to variable entry that points here. *	  *name	   (I/O)   Input name requested, output name found. *	  *length  (I/O)   Length of input and output oid's. *	   exact   (I)     TRUE if an exact match was requested. *	  *var_len (O)     Length of variable or 0 if function returned. *	(**write_method)   Hook to name a write method (UNUSED). *       * Returns: *	MATCH_SUCCEEDED	If vp->name matches name (accounting for exact bit). *	MATCH_FAILED	Otherwise, * * * Check whether variable (vp) matches name. */intheader_generic(struct variable *vp,	       oid *name,	       size_t *length,	       int exact,	       size_t *var_len,	       WriteMethod **write_method){    oid newname[MAX_OID_LEN];    int result;    DEBUGMSGTL(("util_funcs", "header_generic: "));    DEBUGMSGOID(("util_funcs", name, *length));    DEBUGMSG(("util_funcs"," exact=%d\n", exact));    memcpy((char *)newname, (char *)vp->name, (int)vp->namelen * sizeof(oid));    newname[vp->namelen] = 0;    result = snmp_oid_compare(name, *length, newname, vp->namelen + 1);    DEBUGMSGTL(("util_funcs", "  result: %d\n", result));    if ((exact && (result != 0)) || (!exact && (result >= 0)))        return(MATCH_FAILED);    memcpy( (char *)name,(char *)newname, ((int)vp->namelen + 1) * sizeof(oid));    *length = vp->namelen + 1;    *write_method = 0;    *var_len = sizeof(long);	/* default to 'long' results */    return(MATCH_SUCCEEDED);}/* checkmib(): provided for backwards compatibility, do not use: */int checkmib(struct variable *vp, oid *name, size_t *length,             int exact, size_t *var_len,             WriteMethod **write_method, int max) {  /* checkmib used to be header_simple_table, with reveresed boolean     return output.  header_simple_table() was created to match     header_generic(). */  return (!header_simple_table(vp, name, length, exact, var_len,                              write_method, max));}char *find_field(char *ptr,		 int field){  int i;  char *init=ptr;    if (field == LASTFIELD) {    /* skip to end */    while (*ptr++);    ptr = ptr - 2;    /* rewind a field length */    while (*ptr != 0 && isspace(*ptr) && init <= ptr) ptr--;    while (*ptr != 0 && !isspace(*ptr) && init <= ptr) ptr--;    if (isspace(*ptr)) ptr++;  /* past space */    if (ptr < init) ptr = init;    if (!isspace(*ptr) && *ptr != 0) return(ptr);  } else {    if ((ptr = skip_white(ptr)) == NULL) return(NULL);    for (i=1; *ptr != 0 && i != field; i++)       {        if ((ptr = skip_not_white(ptr)) == NULL) return (NULL);        if ((ptr = skip_white(ptr)) == NULL) return (NULL);      }    if (*ptr != 0 && i == field) return(ptr);    return (NULL);  }  return(NULL);}int parse_miboid(const char *buf,		 oid *oidout){  int i;    if (!buf)    return 0;  if (*buf == '.') buf++;  for(i=0;isdigit(*buf);i++) {    oidout[i] = atoi(buf);    while(isdigit(*buf++));    if (*buf == '.') buf++;  }  /* oidout[i] = -1; hmmm */  return i;}voidstring_append_int (char *s, 		   int val){    char textVal[16];    if (val < 10) {	*s++ = '0' + val;	*s = '\0';	return;    }    sprintf (textVal, "%d", val);    strcpy(s, textVal);    return;}

⌨️ 快捷键说明

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