util_funcs.c
来自「eCos操作系统源码」· C语言 代码 · 共 791 行 · 第 1/2 页
C
791 行
//==========================================================================//// ./agent/current/src/mibgroup/util_funcs.c//////==========================================================================//####ECOSGPLCOPYRIGHTBEGIN####// -------------------------------------------// This file is part of eCos, the Embedded Configurable Operating System.// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.//// eCos 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 or (at your option) any later version.//// eCos 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 General Public License// for more details.//// You should have received a copy of the GNU General Public License along// with eCos; if not, write to the Free Software Foundation, Inc.,// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.//// As a special exception, if other files instantiate templates or use macros// or inline functions from this file, or you compile this file and link it// with other works to produce a work based on this file, this file does not// by itself cause the resulting work to be covered by the GNU General Public// License. However the source code for this file must still be made available// in accordance with section (3) of the GNU General Public License.//// This exception does not invalidate any other reasons why a work based on// this file might be covered by the GNU General Public License.//// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.// at http://sources.redhat.com/ecos/ecos-license/// -------------------------------------------//####ECOSGPLCOPYRIGHTEND####//####UCDSNMPCOPYRIGHTBEGIN####//// -------------------------------------------//// Portions of this software may have been derived from the UCD-SNMP// project, <http://ucd-snmp.ucdavis.edu/> from the University of// California at Davis, which was originally based on the Carnegie Mellon// University SNMP implementation. Portions of this software are therefore// covered by the appropriate copyright disclaimers included herein.//// The release used was version 4.1.2 of May 2000. "ucd-snmp-4.1.2"// -------------------------------------------////####UCDSNMPCOPYRIGHTEND####//==========================================================================//#####DESCRIPTIONBEGIN####//// Author(s): hmt// Contributors: hmt// Date: 2000-05-30// Purpose: Port of UCD-SNMP distribution to eCos.// Description: // ////####DESCRIPTIONEND####////==========================================================================/******************************************************************** Copyright 1989, 1991, 1992 by Carnegie Mellon University Derivative Work -Copyright 1996, 1998, 1999, 2000 The Regents of the University of California All Rights ReservedPermission to use, copy, modify and distribute this software and itsdocumentation for any purpose and without fee is hereby granted,provided that the above copyright notice appears in all copies andthat both that copyright notice and this permission notice appear insupporting documentation, and that the name of CMU and The Regents ofthe University of California not be used in advertising or publicitypertaining to distribution of the software without specific writtenpermission.CMU AND THE REGENTS OF THE UNIVERSITY OF CALIFORNIA DISCLAIM ALLWARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIEDWARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL CMU ORTHE REGENTS OF THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR ANY SPECIAL,INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTINGFROM THE LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OFCONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR INCONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.*********************************************************************//* * 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_BASETSD_H#include <basetsd.h>#define ssize_t SSIZE_T#endif#if HAVE_RAISE#define alarm raise#endif#include "mibincl.h"#include "mibgroup/struct.h"#include "mibgroup/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);}int shell_command(struct extensible *ex){#if HAVE_SYSTEM char shellline[STRMAX]; FILE *shellout; sprintf(shellline,"%s > /tmp/shoutput",ex->command); ex->result = system(shellline); ex->result = WEXITSTATUS(ex->result); shellout = fopen("/tmp/shoutput","r"); if((shellout = fopen("/tmp/shoutput","r")) != NULL) { if (fgets(ex->output,STRMAX,shellout) == NULL) { ex->output[0] = 0; } fclose(shellout); } unlink("/tmp/shoutput");#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,STRMAX,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 __ECOS#ifndef EXCACHETIME if (ex->pid && waitpid(ex->pid,&ex->result,0) < 0) { setPerrorstatus("waitpid"); } ex->pid = 0;#endif#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 cache[MAXCACHESIZE]; ssize_t cachebytes; long curtime; static char lastcmd[STRMAX]; int cfd; static int lastresult; int readcount;#endif#ifdef EXCACHETIME 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; } close(fd[1]); close(fd[0]); for(cnt=1,cptr1 = ex->command, 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(ex->command,ctmp); execv(ctmp,argv); snmp_log_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("open"); 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("open"); return 0; } return(cfd);#endif#else /* !HAVE_EXECV */ return 0;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?