slave.c

来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 584 行 · 第 1/2 页

C
584
字号
#ifndef lintstatic  char    *sccsid = "@(#)slave.c	4.1  (ULTRIX)        7/2/90";#endif lint/************************************************************************ *									* *			Copyright (c) 1988 by				* *		Digital Equipment Corporation, Maynard, MA		* *			All rights reserved.				* *									* *   This software is furnished under a license and may be used and	* *   copied  only  in accordance with the terms of such license and	* *   with the  inclusion  of  the  above  copyright  notice.   This	* *   software  or  any  other copies thereof may not be provided or	* *   otherwise made available to any other person.  No title to and	* *   ownership of the software is hereby transferred.			* *									* *   This software is  derived  from  software  received  from  the	* *   University    of   California,   Berkeley,   and   from   Bell	* *   Laboratories.  Use, duplication, or disclosure is  subject  to	* *   restrictions  under  license  agreements  with  University  of	* *   California and with AT&T.						* *									* *   The information in this software is subject to change  without	* *   notice  and should not be construed as a commitment by Digital	* *   Equipment Corporation.						* *									* *   Digital assumes no responsibility for the use  or  reliability	* *   of its software on equipment which is not supplied by Digital.	* *									* ************************************************************************//* * Copyright (c) 1985 Regents of the University of California. * All rights reserved.  The Berkeley software License Agreement * specifies the terms and conditions for redistribution. *static char sccsid[] = "@(#)slave.c	2.16 (Berkeley) 6/5/86"; */#include "globals.h"#include <protocols/timed.h>#include <setjmp.h>extern jmp_buf jmpenv;extern u_short sequence;slave(){	int length;	int senddateack;	long electiontime, refusetime, looktime;	u_short seq;	char candidate[MAXHOSTNAMELEN];	struct tsp *msg, to, *readmsg();	struct sockaddr_in saveaddr, msaveaddr;	struct timeval wait;	struct timeval time, otime;	struct tsp *answer, *acksend();	int timeout();	char *date();	long casual();	int bytenetorder();	char olddate[32];	struct sockaddr_in server;	register struct netinfo *ntp;	int ind;	struct tsp resp;	extern int Mflag;	extern int justquit;#ifdef MEASURE	extern FILE *fp;#endif	if (slavenet) {		resp.tsp_type = TSP_SLAVEUP;		resp.tsp_vers = TSPVERSION;		(void)strcpy(resp.tsp_name, hostname);		bytenetorder(&resp);		if (sendto(sock, (char *)&resp, sizeof(struct tsp), 0,		    &slavenet->dest_addr, sizeof(struct sockaddr_in)) < 0) {			syslog(LOG_ERR, "sendto: %m");			exit(1);		}	}	if (status & MASTER) {#ifdef MEASURE		if (fp == NULL) {			fp = fopen("/usr/adm/timed.masterlog", "w");			setlinebuf(fp);		}#endif		syslog(LOG_INFO, "THIS MACHINE IS A SUBMASTER");		if (trace) {			fprintf(fd, "THIS MACHINE IS A SUBMASTER\n");		}		for (ntp = nettab; ntp != NULL; ntp = ntp->next)			if (ntp->status == MASTER)				masterup(ntp);	} else {		syslog(LOG_INFO, "THIS MACHINE IS A SLAVE");		if (trace) {			fprintf(fd, "THIS MACHINE IS A SLAVE\n");		}	}	seq = 0;	senddateack = OFF;	refusetime = 0;	(void)gettimeofday(&time, (struct timezone *)0);	electiontime = time.tv_sec + delay2;	if (Mflag)		if (justquit)			looktime = time.tv_sec + delay2;		else 			looktime = 1;	else		looktime = 0;loop:	length = sizeof(struct sockaddr_in);	(void)gettimeofday(&time, (struct timezone *)0);	if (time.tv_sec > electiontime) {		if (trace) 			fprintf(fd, "election timer expired\n");		longjmp(jmpenv, 1);	}	if (looktime && time.tv_sec > looktime) {		if (trace) 			fprintf(fd, "Looking for nets to master and loops\n");				if (nignorednets > 0) {			for (ntp = nettab; ntp != NULL; ntp = ntp->next) {				if (ntp->status == IGNORE) {					lookformaster(ntp);					if (ntp->status == MASTER)						masterup(ntp);					else						ntp->status = IGNORE;				}			}			setstatus();#ifdef MEASURE			/*			 * Check to see if we just became master			 * (file not open)			 */			if (fp == NULL) {				fp = fopen("/usr/adm/timed.masterlog", "w");				setlinebuf(fp);			}#endif		}		for (ntp = nettab; ntp != NULL; ntp = ntp->next) {		    if (ntp->status == MASTER) {			to.tsp_type = TSP_LOOP;			to.tsp_vers = TSPVERSION;			to.tsp_seq = sequence++;			to.tsp_hopcnt = 10;			(void)strcpy(to.tsp_name, hostname);			bytenetorder(&to);			if (sendto(sock, (char *)&to, sizeof(struct tsp), 0,			    &ntp->dest_addr, sizeof(struct sockaddr_in)) < 0) {				syslog(LOG_ERR, "sendto: %m");				exit(1);			}		    }		}		(void)gettimeofday(&time, (struct timezone *)0);		looktime = time.tv_sec + delay2;	}	wait.tv_sec = electiontime - time.tv_sec + 10;	wait.tv_usec = 0;	msg = readmsg(TSP_ANY, (char *)ANYADDR, &wait, (struct netinfo *)NULL);	if (msg != NULL) {		switch (msg->tsp_type) {		case TSP_SETDATE:#ifdef TESTING		case TSP_TEST:#endif		case TSP_MSITE:		case TSP_TRACEOFF:		case TSP_TRACEON:			break;		case TSP_MASTERUP:			if (fromnet == NULL) {				if (trace) {					fprintf(fd, "slave ignored: ");					print(msg, &from);				}				goto loop;			}			break;		default:			if (fromnet == NULL || fromnet->status == IGNORE) {				if (trace) {					fprintf(fd, "slave ignored: ");					print(msg, &from);				}				goto loop;			}			break;		}		switch (msg->tsp_type) {		case TSP_ADJTIME:			if (External_source)                                break;			if (fromnet->status != SLAVE)				break;			(void)gettimeofday(&time, (struct timezone *)0);			electiontime = time.tv_sec + delay2;			if (seq != msg->tsp_seq) {				seq = msg->tsp_seq;				if ((status & SUBMASTER) == SUBMASTER) {					synch((msg->tsp_time.tv_sec * 1000) + 					    (msg->tsp_time.tv_usec / 1000));				} else {					adjclock(&(msg->tsp_time));				}			}			break;		case TSP_SETTIME:			if (External_source)				break;			if (fromnet->status != SLAVE)				break;			if (seq == msg->tsp_seq)				break;			seq = msg->tsp_seq;			(void)strcpy(olddate, date());			(void)gettimeofday(&otime, (struct timezone *)0);			(void)settimeofday(&msg->tsp_time,				(struct timezone *)0);			syslog(LOG_NOTICE, "date changed by %s from: %s",				msg->tsp_name, olddate);			logwtmp(otime, msg->tsp_time);			if ((status & SUBMASTER) == SUBMASTER)				spreadtime();			(void)gettimeofday(&time, (struct timezone *)0);			electiontime = time.tv_sec + delay2;			if (senddateack == ON) {				senddateack = OFF;				msg->tsp_type = TSP_DATEACK;				(void)strcpy(msg->tsp_name, hostname);				bytenetorder(msg);				length = sizeof(struct sockaddr_in);				if (sendto(sock, (char *)msg, 						sizeof(struct tsp), 0,						&saveaddr, length) < 0) {					syslog(LOG_ERR, "sendto: %m");					exit(1);				}			}			break;		case TSP_MASTERUP:			if (slavenet && fromnet != slavenet)				break;			makeslave(fromnet);			setstatus();			msg->tsp_type = TSP_SLAVEUP;			msg->tsp_vers = TSPVERSION;			(void)strcpy(msg->tsp_name, hostname);			bytenetorder(msg);			answerdelay();			length = sizeof(struct sockaddr_in);			if (sendto(sock, (char *)msg, sizeof(struct tsp), 0, 						&from, length) < 0) {				syslog(LOG_ERR, "sendto: %m");				exit(1);			}			backoff = 1;			delay2 = casual((long)MINTOUT, (long)MAXTOUT);			(void)gettimeofday(&time, (struct timezone *)0);			electiontime = time.tv_sec + delay2;			refusetime = 0;			break;		case TSP_MASTERREQ:			if (fromnet->status != SLAVE)				break;			(void)gettimeofday(&time, (struct timezone *)0);			electiontime = time.tv_sec + delay2;			break;		case TSP_SETDATE:			saveaddr = from;

⌨️ 快捷键说明

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