📄 conf.c
字号:
** Only on non-BSD systems**** Parameters:** none**** Returns:** size of file descriptor table**** Side Effects:** none*/#ifdef SOLARIS# include <sys/resource.h>#endifintgetdtsize(){#ifdef RLIMIT_NOFILE struct rlimit rl; if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) return rl.rlim_cur;#endif# ifdef HASGETDTABLESIZE return getdtablesize();# else# ifdef _SC_OPEN_MAX return sysconf(_SC_OPEN_MAX);# else return NOFILE;# endif# endif}/*** UNAME -- get the UUCP name of this system.*/#ifndef HASUNAMEintuname(name) struct utsname *name;{ FILE *file; char *n; name->nodename[0] = '\0'; /* try /etc/whoami -- one line with the node name */ if ((file = fopen("/etc/whoami", "r")) != NULL) { (void) fgets(name->nodename, NODE_LENGTH + 1, file); (void) fclose(file); n = strchr(name->nodename, '\n'); if (n != NULL) *n = '\0'; if (name->nodename[0] != '\0') return (0); } /* try /usr/include/whoami.h -- has a #define somewhere */ if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) { char buf[MAXLINE]; while (fgets(buf, MAXLINE, file) != NULL) if (sscanf(buf, "#define sysname \"%*[^\"]\"", NODE_LENGTH, name->nodename) > 0) break; (void) fclose(file); if (name->nodename[0] != '\0') return (0); }#ifdef TRUST_POPEN /* ** Popen is known to have security holes. */ /* try uuname -l to return local name */ if ((file = popen("uuname -l", "r")) != NULL) { (void) fgets(name, NODE_LENGTH + 1, file); (void) pclose(file); n = strchr(name, '\n'); if (n != NULL) *n = '\0'; if (name->nodename[0] != '\0') return (0); }#endif return (-1);}#endif /* HASUNAME *//*** INITGROUPS -- initialize groups**** Stub implementation for System V style systems*/#ifndef HASINITGROUPSinitgroups(name, basegid) char *name; int basegid;{ return 0;}#endif/*** SETSID -- set session id (for non-POSIX systems)*/#ifndef HASSETSIDpid_tsetsid __P ((void)){#ifdef TIOCNOTTY int fd; fd = open("/dev/tty", O_RDWR, 0); if (fd >= 0) { (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); (void) close(fd); }#endif /* TIOCNOTTY */# ifdef SYS5SETPGRP return setpgrp();# else return setpgid(0, getpid());# endif}#endif/*** FSYNC -- dummy fsync*/#ifdef NEEDFSYNCfsync(fd) int fd;{# ifdef O_SYNC return fcntl(fd, F_SETFL, O_SYNC);# else /* nothing we can do */ return 0;# endif}#endif/*** DGUX_INET_ADDR -- inet_addr for DG/UX**** Data General DG/UX version of inet_addr returns a struct in_addr** instead of a long. This patches things.*/#ifdef DGUX#undef inet_addrlongdgux_inet_addr(host) char *host;{ struct in_addr haddr; haddr = inet_addr(host); return haddr.s_addr;}#endif/*** GETOPT -- for old systems or systems with bogus implementations*/#ifdef NEEDGETOPT/* * Copyright (c) 1985 Regents of the University of California. * All rights reserved. The Berkeley software License Agreement * specifies the terms and conditions for redistribution. *//*** this version hacked to add `atend' flag to allow state machine** to reset if invoked by the program to scan args for a 2nd time*/#if defined(LIBC_SCCS) && !defined(lint)static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86";#endif /* LIBC_SCCS and not lint */#include <stdio.h>/* * get option letter from argument vector */#ifdef _CONVEX_SOURCEextern int optind, opterr;#elseint opterr = 1; /* if error message should be printed */int optind = 1; /* index into parent argv vector */#endifint optopt; /* character checked for validity */char *optarg; /* argument associated with option */#define BADCH (int)'?'#define EMSG ""#define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}getopt(nargc,nargv,ostr) int nargc; char *const *nargv; const char *ostr;{ static char *place = EMSG; /* option letter processing */ static char atend = 0; register char *oli; /* option letter list index */ if (atend) { atend = 0; place = EMSG; } if(!*place) { /* update scanning pointer */ if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { atend++; return(EOF); } if (*place == '-') { /* found "--" */ ++optind; atend++; return(EOF); } } /* option letter okay? */ if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { if (!*place) ++optind; tell(": illegal option -- "); } if (*++oli != ':') { /* don't need argument */ optarg = NULL; if (!*place) ++optind; } else { /* need an argument */ if (*place) optarg = place; /* no white space */ else if (nargc <= ++optind) { /* no arg */ place = EMSG; tell(": option requires an argument -- "); } else optarg = nargv[optind]; /* white space */ place = EMSG; ++optind; } return(optopt); /* dump back option letter */}#endif/*** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version*/#ifdef NEEDVPRINTF#define MAXARG 16vfprintf(fp, fmt, ap) FILE * fp; char * fmt; char ** ap;{ char * bp[MAXARG]; int i = 0; while (*ap && i < MAXARG) bp[i++] = *ap++; fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], bp[4], bp[5], bp[6], bp[7], bp[8], bp[9], bp[10], bp[11], bp[12], bp[13], bp[14], bp[15]);}vsprintf(s, fmt, ap) char * s; char * fmt; char ** ap;{ char * bp[MAXARG]; int i = 0; while (*ap && i < MAXARG) bp[i++] = *ap++; sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], bp[4], bp[5], bp[6], bp[7], bp[8], bp[9], bp[10], bp[11], bp[12], bp[13], bp[14], bp[15]);}#endif/*** USERSHELLOK -- tell if a user's shell is ok for unrestricted use**** Parameters:** shell -- the user's shell from /etc/passwd**** Returns:** TRUE -- if it is ok to use this for unrestricted access.** FALSE -- if the shell is restricted.*/#if !HASGETUSERSHELL# ifndef _PATH_SHELLS# define _PATH_SHELLS "/etc/shells"# endifchar *DefaultUserShells[] ={ "/bin/sh", "/usr/bin/sh", "/bin/csh", "/usr/bin/csh",#ifdef __hpux "/bin/rsh", "/bin/ksh", "/bin/rksh", "/bin/pam", "/usr/bin/keysh", "/bin/posix/sh",#endif NULL};#endif#define WILDCARD_SHELL "/SENDMAIL/ANY/SHELL/"boolusershellok(shell) char *shell;{#if HASGETUSERSHELL register char *p; extern char *getusershell(); setusershell(); while ((p = getusershell()) != NULL) if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0) break; endusershell(); return p != NULL;#else register FILE *shellf; char buf[MAXLINE]; shellf = fopen(_PATH_SHELLS, "r"); if (shellf == NULL) { /* no /etc/shells; see if it is one of the std shells */ char **d; for (d = DefaultUserShells; *d != NULL; d++) { if (strcmp(shell, *d) == 0) return TRUE; } return FALSE; } while (fgets(buf, sizeof buf, shellf) != NULL) { register char *p, *q; p = buf; while (*p != '\0' && *p != '#' && *p != '/') p++; if (*p == '#' || *p == '\0') continue; q = p; while (*p != '\0' && *p != '#' && !isspace(*p)) p++; *p = '\0'; if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0) { fclose(shellf); return TRUE; } } fclose(shellf); return FALSE;#endif}/*** FREESPACE -- see how much free space is on the queue filesystem**** Only implemented if you have statfs.**** Parameters:** dir -- the directory in question.** bsize -- a variable into which the filesystem** block size is stored.**** Returns:** The number of bytes free on the queue filesystem.** -1 if the statfs call fails.**** Side effects:** Puts the filesystem block size into bsize.*//* statfs types */#define SFS_NONE 0 /* no statfs implementation */#define SFS_USTAT 1 /* use ustat */#define SFS_4ARGS 2 /* use four-argument statfs call */#define SFS_VFS 3 /* use <sys/vfs.h> implementation */#define SFS_MOUNT 4 /* use <sys/mount.h> implementation */#define SFS_STATFS 5 /* use <sys/statfs.h> implementation */#define SFS_STATVFS 6 /* use <sys/statvfs.h> implementation */#ifndef SFS_TYPE# define SFS_TYPE SFS_NONE#endif#if SFS_TYPE == SFS_USTAT# include <ustat.h>#endif#if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS# include <sys/statfs.h>#endif#if SFS_TYPE == SFS_VFS# include <sys/vfs.h>#endif#if SFS_TYPE == SFS_MOUNT# include <sys/mount.h>#endif#if SFS_TYPE == SFS_STATVFS# include <sys/statvfs.h>#endiflongfreespace(dir, bsize) char *dir; long *bsize;{#if SFS_TYPE != SFS_NONE# if SFS_TYPE == SFS_USTAT struct ustat fs; struct stat statbuf;# define FSBLOCKSIZE DEV_BSIZE# define f_bavail f_tfree# else# if defined(ultrix) struct fs_data fs;# define f_bavail fd_bfreen# define FSBLOCKSIZE fs.fd_bsize# else# if SFS_TYPE == SFS_STATVFS struct statvfs fs;# define FSBLOCKSIZE fs.f_bsize# else struct statfs fs;# define FSBLOCKSIZE fs.f_bsize# if defined(_SCO_unix_) || defined(IRIX) || defined(apollo)# define f_bavail f_bfree# endif# endif# endif# endif extern int errno;# if SFS_TYPE == SFS_USTAT if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)# else# if SFS_TYPE == SFS_4ARGS if (statfs(dir, &fs, sizeof fs, 0) == 0)# else# if defined(ultrix) if (statfs(dir, &fs) > 0)# else if (statfs(dir, &fs) == 0)# endif# endif# endif { if (bsize != NULL) *bsize = FSBLOCKSIZE; return (fs.f_bavail); }#endif return (-1);}/*** ENOUGHSPACE -- check to see if there is enough free space on the queue fs**** Only implemented if you have statfs.**** Parameters:** msize -- the size to check against. If zero, we don't yet** know how big the message will be, so just check for** a "reasonable" amount.**** Returns:** TRUE if there is enough space.** FALSE otherwise.*/boolenoughspace(msize) long msize;{ long bfree, bsize; if (MinBlocksFree <= 0 && msize <= 0) { if (tTd(4, 80)) printf("enoughspace: no threshold\n"); return TRUE; } if ((bfree = freespace(QueueDir, &bsize)) >= 0) { if (tTd(4, 80)) printf("enoughspace: bavail=%ld, need=%ld\n", bfree, msize); /* convert msize to block count */ msize = msize / bsize + 1; if (MinBlocksFree >= 0) msize += MinBlocksFree; if (bfree < msize) {#ifdef LOG if (LogLevel > 0) syslog(LOG_ALERT, "%s: low on space (have %ld, %s needs %ld in %s)", CurEnv->e_id, bfree, CurHostName, msize, QueueDir);#endif return FALSE; } } else if (tTd(4, 80)) printf("enoughspace failure: min=%ld, need=%ld: %s\n", MinBlocksFree, msize, errstring(errno)); return TRUE;}/*** TRANSIENTERROR -- tell if an error code indicates a transient failure**** This looks at an errno value and tells if this is likely to** go away if retried later.**** Parameters:** err -- the errno code to classify.**** Returns:** TRUE if this is probably transient.** FALSE otherwise.*/booltransienterror(err) int err;{ switch (err) { case EIO: /* I/O error */ case ENXIO: /* Device not configured */ case EAGAIN: /* Resource temporarily unavailable */ case ENOMEM: /* Cannot allocate memory */ case ENODEV: /* Operation not supported by device */ case ENFILE: /* Too many open files in system */ case EMFILE: /* Too many open files */ case ENOSPC: /* No space left on device */#ifdef ETIMEDOUT case ETIMEDOUT: /* Connection timed out */#endif#ifdef ESTALE case ESTALE: /* Stale NFS file handle */#endif#ifdef ENETDOWN case ENETDOWN: /* Network is down */#endif#ifdef ENETUNREACH
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -