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

📄 syslogd.c

📁 linux下记录系统日志代码以及记录内核日志代码
💻 C
📖 第 1 页 / 共 5 页
字号:
				/* open the terminal */				ttyf = open(p, O_WRONLY|O_NOCTTY);				if (ttyf >= 0) {					struct stat statb;					if (fstat(ttyf, &statb) == 0 &&					    (statb.st_mode & S_IWRITE))						(void) writev(ttyf, iov, 6);					close(ttyf);					ttyf = -1;				}			}			(void) alarm(0);		}		exit(0);	}	/* close the user login file */	endutent();	reenter = 0;}void reapchild(){	int saved_errno = errno;#if defined(SYSV) && !defined(linux)	(void) signal(SIGCHLD, reapchild);	/* reset signal handler -ASP */	wait ((int *)0);#else	union wait status;	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)		;#endif#ifdef linux	(void) signal(SIGCHLD, reapchild);	/* reset signal handler -ASP */#endif	errno = saved_errno;}/* * Return a printable representation of a host address. */const char *cvthname(f)	struct sockaddr_in *f;{	struct hostent *hp;	register char *p;	int count;	if (f->sin_family != AF_INET) {		dprintf("Malformed from address.\n");		return ("???");	}	hp = gethostbyaddr((char *) &f->sin_addr, sizeof(struct in_addr), \			   f->sin_family);	if (hp == 0) {		dprintf("Host name for your address (%s) unknown.\n",			inet_ntoa(f->sin_addr));		return (inet_ntoa(f->sin_addr));	}	/*	 * Convert to lower case, just like LocalDomain above	 */	for (p = (char *)hp->h_name; *p ; p++)		if (isupper(*p))			*p = tolower(*p);	/*	 * Notice that the string still contains the fqdn, but your	 * hostname and domain are separated by a '\0'.	 */	if ((p = strchr(hp->h_name, '.'))) {		if (strcmp(p + 1, LocalDomain) == 0) {			*p = '\0';			return (hp->h_name);		} else {			if (StripDomains) {				count=0;				while (StripDomains[count]) {					if (strcmp(p + 1, StripDomains[count]) == 0) {						*p = '\0';						return (hp->h_name);					}					count++;				}			}			if (LocalHosts) {				count=0;				while (LocalHosts[count]) {					if (!strcmp(hp->h_name, LocalHosts[count])) {						*p = '\0';						return (hp->h_name);					}					count++;				}			}		}	}	return (hp->h_name);}void domark(){	register struct filed *f;#ifdef SYSV	int lognum;#endif	if (MarkInterval > 0) {	now = time(0);	MarkSeq += TIMERINTVL;	if (MarkSeq >= MarkInterval) {		logmsg(LOG_INFO, "-- MARK --", LocalHostName, ADDDATE|MARK);		MarkSeq = 0;	}#ifdef SYSV	for (lognum = 0; lognum <= nlogs; lognum++) {		f = &Files[lognum];#else	for (f = Files; f; f = f->f_next) {#endif		if (f->f_prevcount && now >= REPEATTIME(f)) {			dprintf("flush %s: repeated %d times, %d sec.\n",			    TypeNames[f->f_type], f->f_prevcount,			    repeatinterval[f->f_repeatcount]);			fprintlog(f, LocalHostName, 0, (char *)NULL);			BACKOFF(f);		}	}	}	(void) signal(SIGALRM, domark);	(void) alarm(TIMERINTVL);}void debug_switch(){	dprintf("Switching debugging_on to %s\n", (debugging_on == 0) ? "true" : "false");	debugging_on = (debugging_on == 0) ? 1 : 0;	signal(SIGUSR1, debug_switch);}/* * Print syslogd errors some place. */void logerror(type)	char *type;{	char buf[100];	dprintf("Called logerr, msg: %s\n", type);	if (errno == 0)		(void) snprintf(buf, sizeof(buf), "syslogd: %s", type);	else		(void) snprintf(buf, sizeof(buf), "syslogd: %s: %s", type, strerror(errno));	errno = 0;	logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE);	return;}void die(sig)	int sig;	{	register struct filed *f;	char buf[100];	int lognum;	int i;	int was_initialized = Initialized;	Initialized = 0;	/* Don't log SIGCHLDs in case we				   receive one during exiting */	for (lognum = 0; lognum <= nlogs; lognum++) {		f = &Files[lognum];		/* flush any pending output */		if (f->f_prevcount)			fprintlog(f, LocalHostName, 0, (char *)NULL);	}	Initialized = was_initialized;	if (sig) {		dprintf("syslogd: exiting on signal %d\n", sig);		(void) snprintf(buf, sizeof(buf), "exiting on signal %d", sig);		errno = 0;		logmsg(LOG_SYSLOG|LOG_INFO, buf, LocalHostName, ADDDATE);	}	/* Close the UNIX sockets. */        for (i = 0; i < nfunix; i++)		if (funix[i] != -1)			close(funix[i]);	/* Close the inet socket. */	if (InetInuse) close(inetm);	/* Clean-up files. */        for (i = 0; i < nfunix; i++)		if (funixn[i] && funix[i] != -1)			(void)unlink(funixn[i]);#ifndef TESTING	(void) remove_pid(PidFile);#endif	exit(0);}/* * Signal handler to terminate the parent process. */#ifndef TESTINGvoid doexit(sig)	int sig;{	exit (0);}#endif/* *  INIT -- Initialize syslogd from configuration table */void init(){	register int i, lognum;	register FILE *cf;	register struct filed *f;#ifndef TESTING#ifndef SYSV	register struct filed **nextp = (struct filed **) 0;#endif#endif	register char *p;	register unsigned int Forwarding = 0;#ifdef CONT_LINE	char cbuf[BUFSIZ];	char *cline;#else	char cline[BUFSIZ];#endif	struct servent *sp;	sp = getservbyname("syslog", "udp");	if (sp == NULL) {		errno = 0;		logerror("network logging disabled (syslog/udp service unknown).");		logerror("see syslogd(8) for details of whether and how to enable it.");		return;	}	LogPort = sp->s_port;	/*	 *  Close all open log files and free log descriptor array.	 */	dprintf("Called init.\n");	Initialized = 0;	if ( nlogs > -1 )	{		dprintf("Initializing log structures.\n");		for (lognum = 0; lognum <= nlogs; lognum++ ) {			f = &Files[lognum];			/* flush any pending output */			if (f->f_prevcount)				fprintlog(f, LocalHostName, 0, (char *)NULL);			switch (f->f_type) {				case F_FILE:				case F_PIPE:				case F_TTY:				case F_CONSOLE:					(void) close(f->f_file);				break;			}		}		/*		 * This is needed especially when HUPing syslogd as the		 * structure would grow infinitively.  -Joey		 */		nlogs = -1;		free((void *) Files);		Files = (struct filed *) 0;	}	#ifdef SYSV	lognum = 0;#else	f = NULL;#endif	/* open the configuration file */	if ((cf = fopen(ConfFile, "r")) == NULL) {		dprintf("cannot open %s.\n", ConfFile);#ifdef SYSV		allocate_log();		f = &Files[lognum++];#ifndef TESTING		cfline("*.err\t" _PATH_CONSOLE, f);#else		snprintf(cbuf,sizeof(cbuf), "*.*\t%s", ttyname(0));		cfline(cbuf, f);#endif#else		*nextp = (struct filed *)calloc(1, sizeof(*f));		cfline("*.ERR\t" _PATH_CONSOLE, *nextp);		(*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f))	/* ASP */		cfline("*.PANIC\t*", (*nextp)->f_next);#endif		Initialized = 1;		return;	}	/*	 *  Foreach line in the conf table, open that file.	 */#if CONT_LINE	cline = cbuf;	while (fgets(cline, sizeof(cbuf) - (cline - cbuf), cf) != NULL) {#else	while (fgets(cline, sizeof(cline), cf) != NULL) {#endif		/*		 * check for end-of-section, comments, strip off trailing		 * spaces and newline character.		 */		for (p = cline; isspace(*p); ++p);		if (*p == '\0' || *p == '#')			continue;#if CONT_LINE		strcpy(cline, p);#endif		for (p = strchr(cline, '\0'); isspace(*--p););#if CONT_LINE		if (*p == '\\') {			if ((p - cbuf) > BUFSIZ - 30) {				/* Oops the buffer is full - what now? */				cline = cbuf;			} else {				*p = 0;				cline = p;				continue;			}		}  else			cline = cbuf;#endif		*++p = '\0';#ifndef SYSV		f = (struct filed *)calloc(1, sizeof(*f));		*nextp = f;		nextp = &f->f_next;#endif		allocate_log();		f = &Files[lognum++];#if CONT_LINE		cfline(cbuf, f);#else		cfline(cline, f);#endif		if (f->f_type == F_FORW || f->f_type == F_FORW_SUSP || f->f_type == F_FORW_UNKN) {			Forwarding++;		}	}	/* close the configuration file */	(void) fclose(cf);#ifdef SYSLOG_UNIXAF	for (i = 0; i < nfunix; i++) {		if (funix[i] != -1)			/* Don't close the socket, preserve it instead			close(funix[i]);			*/			continue;		if ((funix[i] = create_unix_socket(funixn[i])) != -1)			dprintf("Opened UNIX socket `%s'.\n", funixn[i]);	}#endif#ifdef SYSLOG_INET	if (Forwarding || AcceptRemote) {		if (finet < 0) {			finet = create_inet_socket();			if (finet >= 0) {				InetInuse = 1;				dprintf("Opened syslog UDP port.\n");			}		}	}	else {		if (finet >= 0)			close(finet);		finet = -1;		InetInuse = 0;	}	inetm = finet;#endif	Initialized = 1;	if ( Debug ) {#ifdef SYSV		for (lognum = 0; lognum <= nlogs; lognum++) {			f = &Files[lognum];			if (f->f_type != F_UNUSED) {				printf ("%2d: ", lognum);#else		for (f = Files; f; f = f->f_next) {			if (f->f_type != F_UNUSED) {#endif				for (i = 0; i <= LOG_NFACILITIES; i++)					if (f->f_pmask[i] == TABLE_NOPRI)						printf(" X ");					else						printf("%2X ", f->f_pmask[i]);				printf("%s: ", TypeNames[f->f_type]);				switch (f->f_type) {				case F_FILE:				case F_PIPE:				case F_TTY:				case F_CONSOLE:					printf("%s", f->f_un.f_fname);					if (f->f_file == -1)						printf(" (unused)");					break;				case F_FORW:				case F_FORW_SUSP:				case F_FORW_UNKN:					printf("%s", f->f_un.f_forw.f_hname);					break;				case F_USERS:					for (i = 0; i < MAXUNAMES && *f->f_un.f_uname[i]; i++)						printf("%s, ", f->f_un.f_uname[i]);					break;				}				printf("\n");			}		}	}	if ( AcceptRemote )#ifdef DEBRELEASE		logmsg(LOG_SYSLOG|LOG_INFO, "syslogd " VERSION "." PATCHLEVEL "#" DEBRELEASE \		       ": restart (remote reception)." , LocalHostName, \		       	ADDDATE);#else		logmsg(LOG_SYSLOG|LOG_INFO, "syslogd " VERSION "." PATCHLEVEL \		       ": restart (remote reception)." , LocalHostName, \		       	ADDDATE);#endif	else#ifdef DEBRELEASE		logmsg(LOG_SYSLOG|LOG_INFO, "syslogd " VERSION "." PATCHLEVEL "#" DEBRELEASE \		       ": restart." , LocalHostName, ADDDATE);#else		logmsg(LOG_SYSLOG|LOG_INFO, "syslogd " VERSION "." PATCHLEVEL \		       ": restart." , LocalHostName, ADDDATE);#endif	(void) signal(SIGHUP, sighup_handler);	dprintf("syslogd: restarted.\n");}#if FALSE	}}} /* balance parentheses for emacs */#endif/* * Crack a configuration file line */void cfline(line, f)	char *line;	register struct filed *f;{	register char *p;	register char *q;	register int i, i2;	char *bp;	int pri;	int singlpri = 0;

⌨️ 快捷键说明

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