📄 conf.c
字号:
*/#ifdef _AUX_SOURCE# include <compat.h>#endifinit_md(argc, argv) int argc; char **argv;{#ifdef _AUX_SOURCE setcompat(getcompat() | COMPAT_BSDPROT);#endif}/*** GETLA -- get the current load average**** This code stolen from la.c.**** Parameters:** none.**** Returns:** The current load average as an integer.**** Side Effects:** none.*//* try to guess what style of load average we have */#define LA_ZERO 1 /* always return load average as zero */#define LA_INT 2 /* read kmem for avenrun; interpret as long */#define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */#define LA_SUBR 4 /* call getloadavg */#define LA_MACH 5 /* MACH load averages (as on NeXT boxes) */#define LA_SHORT 6 /* read kmem for avenrun; interpret as short */#define LA_PROCSTR 7 /* read string ("1.17") from /proc/loadavg *//* do guesses based on general OS type */#ifndef LA_TYPE# define LA_TYPE LA_ZERO#endif#if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)#include <nlist.h>#ifndef LA_AVENRUN# ifdef SYSTEM5# define LA_AVENRUN "avenrun"# else# define LA_AVENRUN "_avenrun"# endif#endif/* _PATH_UNIX should be defined in <paths.h> */#ifndef _PATH_UNIX# if defined(SYSTEM5)# define _PATH_UNIX "/unix"# else# define _PATH_UNIX "/vmunix"# endif#endifstruct nlist Nl[] ={ { LA_AVENRUN },#define X_AVENRUN 0 { 0 },};#ifndef FSHIFT# if defined(unixpc)# define FSHIFT 5# endif# if defined(__alpha) || defined(IRIX)# define FSHIFT 10# endif#endif#ifndef FSHIFT# define FSHIFT 8#endif#ifndef FSCALE# define FSCALE (1 << FSHIFT)#endifgetla(){ static int kmem = -1;#if LA_TYPE == LA_INT long avenrun[3];#else# if LA_TYPE == LA_SHORT short avenrun[3];# else double avenrun[3];# endif#endif extern off_t lseek(); extern int errno; if (kmem < 0) { kmem = open("/dev/kmem", 0, 0); if (kmem < 0) { if (tTd(3, 1)) printf("getla: open(/dev/kmem): %s\n", errstring(errno)); return (-1); } (void) fcntl(kmem, F_SETFD, 1); if (nlist(_PATH_UNIX, Nl) < 0) { if (tTd(3, 1)) printf("getla: nlist(%s): %s\n", _PATH_UNIX, errstring(errno)); return (-1); } if (Nl[X_AVENRUN].n_value == 0) { if (tTd(3, 1)) printf("getla: nlist(%s, %s) ==> 0\n", _PATH_UNIX, LA_AVENRUN); return (-1); }#ifdef IRIX Nl[X_AVENRUN].n_value &= 0x7fffffff;#endif } if (tTd(3, 20)) printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 || read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) { /* thank you Ian */ if (tTd(3, 1)) printf("getla: lseek or read: %s\n", errstring(errno)); return (-1); }#if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) if (tTd(3, 5)) { printf("getla: avenrun = %d", avenrun[0]); if (tTd(3, 15)) printf(", %d, %d", avenrun[1], avenrun[2]); printf("\n"); } if (tTd(3, 1)) printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);#else if (tTd(3, 5)) { printf("getla: avenrun = %g", avenrun[0]); if (tTd(3, 15)) printf(", %g, %g", avenrun[1], avenrun[2]); printf("\n"); } if (tTd(3, 1)) printf("getla: %d\n", (int) (avenrun[0] +0.5)); return ((int) (avenrun[0] + 0.5));#endif}#else#if LA_TYPE == LA_SUBR#ifdef DGUX#include <sys/dg_sys_info.h>int getla(){ struct dg_sys_info_load_info load_info; dg_sys_info((long *)&load_info, DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0); return((int) (load_info.one_minute + 0.5));}#elsegetla(){ double avenrun[3]; if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) { if (tTd(3, 1)) perror("getla: getloadavg failed:"); return (-1); } if (tTd(3, 1)) printf("getla: %d\n", (int) (avenrun[0] +0.5)); return ((int) (avenrun[0] + 0.5));}#endif /* DGUX */#else#if LA_TYPE == LA_MACH/*** This has been tested on NEXTSTEP release 2.1/3.X.*/#if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0# include <mach/mach.h>#else# include <mach.h>#endifgetla(){ processor_set_t default_set; kern_return_t error; unsigned int info_count; struct processor_set_basic_info info; host_t host; error = processor_set_default(host_self(), &default_set); if (error != KERN_SUCCESS) return -1; info_count = PROCESSOR_SET_BASIC_INFO_COUNT; if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, &host, (processor_set_info_t)&info, &info_count) != KERN_SUCCESS) { return -1; } return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;}#else#if LA_TYPE == LA_PROCSTR/*** Read /proc/loadavg for the load average. This is assumed to be** in a format like "0.15 0.12 0.06".**** Initially intended for Linux. This has been in the kernel** since at least 0.99.15.*/# ifndef _PATH_LOADAVG# define _PATH_LOADAVG "/proc/loadavg"# endifintgetla(){ double avenrun; register int result; FILE *fp; fp = fopen(_PATH_LOADAVG, "r"); if (fp == NULL) { if (tTd(3, 1)) printf("getla: fopen(%s): %s\n", _PATH_LOADAVG, errstring(errno)); return -1; } result = fscanf(fp, "%lf", &avenrun); fclose(fp); if (result != 1) { if (tTd(3, 1)) printf("getla: fscanf() = %d: %s\n", result, errstring(errno)); return -1; } if (tTd(3, 1)) printf("getla(): %.2f\n", avenrun); return ((int) (avenrun + 0.5));}#elsegetla(){ if (tTd(3, 1)) printf("getla: ZERO\n"); return (0);}#endif#endif#endif#endif/* * Copyright 1989 Massachusetts Institute of Technology * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that * the above copyright notice appear in all copies and that both that * copyright notice and this permission notice appear in supporting * documentation, and that the name of M.I.T. not be used in advertising or * publicity pertaining to distribution of the software without specific, * written prior permission. M.I.T. makes no representations about the * suitability of this software for any purpose. It is provided "as is" * without express or implied warranty. * * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T. * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * Authors: Many and varied... *//* Non Apollo stuff removed by Don Lewis 11/15/93 */#ifndef lintstatic char rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $";#endif /* !lint */#ifdef apollo# undef volatile# include <apollo/base.h>/* ARGSUSED */int getloadavg( call_data ) caddr_t call_data; /* pointer to (double) return value */{ double *avenrun = (double *) call_data; int i; status_$t st; long loadav[3]; proc1_$get_loadav(loadav, &st); *avenrun = loadav[0] / (double) (1 << 16); return(0);}# endif /* apollo *//*** SHOULDQUEUE -- should this message be queued or sent?**** Compares the message cost to the load average to decide.**** Parameters:** pri -- the priority of the message in question.** ctime -- the message creation time.**** Returns:** TRUE -- if this message should be queued up for the** time being.** FALSE -- if the load is low enough to send this message.**** Side Effects:** none.*/boolshouldqueue(pri, ctime) long pri; time_t ctime;{ if (CurrentLA < QueueLA) return (FALSE); if (CurrentLA >= RefuseLA) return (TRUE); return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));}/*** REFUSECONNECTIONS -- decide if connections should be refused**** Parameters:** none.**** Returns:** TRUE if incoming SMTP connections should be refused** (for now).** FALSE if we should accept new work.**** Side Effects:** none.*/boolrefuseconnections(){#ifdef XLA if (!xla_smtp_ok()) return TRUE;#endif /* this is probably too simplistic */ return (CurrentLA >= RefuseLA);}/*** SETPROCTITLE -- set process title for ps**** Parameters:** fmt -- a printf style format string.** a, b, c -- possible parameters to fmt.**** Returns:** none.**** Side Effects:** Clobbers argv of our main procedure so ps(1) will** display the title.*/#ifdef SETPROCTITLE# ifdef HASSETPROCTITLE *** ERROR *** Cannot have both SETPROCTITLE and HASSETPROCTITLE defined# endif# ifdef __hpux# include <sys/pstat.h># endif# ifdef BSD4_4# include <machine/vmparam.h># include <sys/exec.h># ifdef __bsdi__# undef PS_STRINGS /* BSDI 1.0 doesn't do PS_STRINGS as we expect */# define PROCTITLEPAD '\0'# endif# ifdef PS_STRINGS# define SETPROC_STATIC static# endif# endif# ifndef SETPROC_STATIC# define SETPROC_STATIC# endif#endif#ifndef PROCTITLEPAD# define PROCTITLEPAD ' '#endif#ifndef HASSETPROCTITLE/*VARARGS1*/#ifdef __STDC__setproctitle(char *fmt, ...)#elsesetproctitle(fmt, va_alist) char *fmt; va_dcl#endif{# ifdef SETPROCTITLE register char *p; register int i; SETPROC_STATIC char buf[MAXLINE]; VA_LOCAL_DECL# ifdef __hpux union pstun pst;# endif extern char **Argv; extern char *LastArgv; p = buf; /* print sendmail: heading for grep */ (void) strcpy(p, "sendmail: "); p += strlen(p); /* print the argument string */ VA_START(fmt); (void) vsprintf(p, fmt, ap); VA_END; i = strlen(buf);# ifdef __hpux pst.pst_command = buf; pstat(PSTAT_SETCMD, pst, i, 0, 0);# else# ifdef PS_STRINGS PS_STRINGS->ps_nargvstr = 1; PS_STRINGS->ps_argvstr = buf;# else if (i > LastArgv - Argv[0] - 2) { i = LastArgv - Argv[0] - 2; buf[i] = '\0'; } (void) strcpy(Argv[0], buf); p = &Argv[0][i]; while (p < LastArgv) *p++ = PROCTITLEPAD;# endif# endif# endif /* SETPROCTITLE */}#endif/*** REAPCHILD -- pick up the body of my child, lest it become a zombie**** Parameters:** none.**** Returns:** none.**** Side Effects:** Picks up extant zombies.*/voidreapchild(){ int olderrno = errno;# ifdef HASWAITPID auto int status; int count; int pid; count = 0; while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { if (count++ > 1000) {#ifdef LOG syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", pid, status);#endif break; } }# else# ifdef WNOHANG union wait status; while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) continue;# else /* WNOHANG */ auto int status; while (wait(&status) > 0) continue;# endif /* WNOHANG */# endif# ifdef SYS5SIGNALS (void) setsignal(SIGCHLD, reapchild);# endif errno = olderrno;}/*** UNSETENV -- remove a variable from the environment**** Not needed on newer systems.**** Parameters:** name -- the string name of the environment variable to be** deleted from the current environment.**** Returns:** none.**** Globals:** environ -- a pointer to the current environment.**** Side Effects:** Modifies environ.*/#ifndef HASUNSETENVvoidunsetenv(name) char *name;{ extern char **environ; register char **pp; int len = strlen(name); for (pp = environ; *pp != NULL; pp++) { if (strncmp(name, *pp, len) == 0 && ((*pp)[len] == '=' || (*pp)[len] == '\0')) break; } for (; *pp != NULL; pp++) *pp = pp[1];}#endif/*** GETDTABLESIZE -- return number of file descriptors**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -