2 * $Id: timelord.c,v 1.5 2001-06-29 14:14:46 rufustfirefly Exp $
4 * Copyright (c) 1990,1992 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
7 * The "timelord protocol" was reverse engineered from Timelord,
8 * distributed with CAP, Copyright (c) 1990, The University of
9 * Melbourne. The following copyright, supplied by The University
10 * of Melbourne, may apply to this code:
12 * This version of timelord.c is based on code distributed
13 * by the University of Melbourne as part of the CAP package.
15 * The tardis/Timelord package for Macintosh/CAP is
16 * Copyright (c) 1990, The University of Melbourne.
21 #endif /* HAVE_CONFIG_H */
23 #include <sys/param.h>
24 #include <sys/types.h>
28 #include <netatalk/at.h>
29 #include <netatalk/endian.h>
30 #include <atalk/atp.h>
35 #endif /* HAVE_SGTTY_H */
42 #endif /* HAVE_NETDB_H */
46 #endif /* HAVE_FCNTL_H */
47 #ifdef HAVE_SYS_FCNTL_H
48 #include <sys/fcntl.h>
49 #endif /* HAVE_SYS_FCNTL_H */
53 #endif /* HAVE_TERMIOS_H */
54 #ifdef HAVE_SYS_TERMIOS_H
55 #include <sys/termios.h>
56 #endif /* HAVE_SYS_TERMIOS_H */
61 #define EPOCH 0x7C25B080 /* 00:00:00 GMT Jan 1, 1970 for Mac */
64 char *bad = "Bad request!";
73 if (( s = rindex( p, '/' )) == NULL ) {
78 fprintf( stderr, "Usage:\t%s -d -n nbpname\n", s );
83 * Unregister ourself on signal.
88 if ( nbp_unrgstr( server, "TimeLord", "*" ) < 0 ) {
89 syslog( LOG_ERR, "Can't unregister %s", server );
92 syslog( LOG_INFO, "going down" );
101 struct sockaddr_at sat;
102 struct atp_block atpb;
107 char hostname[ MAXHOSTNAMELEN ];
110 long req, mtime, resp;
114 if ( gethostname( hostname, sizeof( hostname )) < 0 ) {
115 perror( "gethostname" );
118 if (( server = index( hostname, '.' )) != 0 ) {
123 while (( c = getopt( ac, av, "dn:" )) != EOF ) {
132 fprintf( stderr, "Unknown option -- '%c'\n", c );
138 * Disassociate from controlling tty.
145 dt = getdtablesize();
146 for ( i = 0; i < dt; i++ ) {
149 if (( i = open( "/dev/tty", O_RDWR )) >= 0 ) {
150 (void)ioctl( i, TIOCNOTTY, 0 );
151 setpgrp( 0, getpid());
163 if (( p = rindex( *av, '/' )) == NULL ) {
170 openlog( p, LOG_PID );
172 openlog( p, LOG_NDELAY|LOG_PID, LOG_DAEMON );
175 /* allocate memory */
176 memset (&sat.sat_addr, 0, sizeof (sat.sat_addr));
178 if (( atp = atp_open( ATADDR_ANYPORT, &sat.sat_addr )) == NULL ) {
179 syslog( LOG_ERR, "main: atp_open: %m" );
183 if ( nbp_rgstr( atp_sockaddr( atp ), server, "TimeLord", "*" ) < 0 ) {
184 syslog( LOG_ERR, "Can't register %s", server );
187 syslog( LOG_INFO, "%s:TimeLord started", server );
189 signal(SIGHUP, goaway);
190 signal(SIGTERM, goaway);
194 * Something seriously wrong with atp, since these assigns must
197 atpb.atp_saddr = &sat;
198 atpb.atp_rreqdata = buf;
199 bzero( &sat, sizeof( struct sockaddr_at ));
200 atpb.atp_rreqdlen = sizeof( buf );
201 if ( atp_rreq( atp, &atpb ) < 0 ) {
202 syslog( LOG_ERR, "main: atp_rreq: %m" );
207 bcopy( p, &req, sizeof( long ));
213 if ( atpb.atp_rreqdlen > 5 ) {
214 bcopy( p + 1, &mtime, sizeof( long ));
215 mtime = ntohl( mtime );
216 syslog( LOG_INFO, "gettime from %s %s was %u",
217 (*( p + 5 ) == '\0' ) ? "<unknown>" : p + 5,
218 ( *p == 0 ) ? "at boot" : "in chooser",
221 syslog( LOG_INFO, "gettime" );
224 if ( gettimeofday( &tv, &tz ) < 0 ) {
225 syslog( LOG_ERR, "main: gettimeofday: %m" );
228 if (( tm = gmtime( &tv.tv_sec )) == 0 ) {
229 perror( "localtime" );
233 mtime = tv.tv_sec + EPOCH;
234 mtime = htonl( mtime );
237 bcopy( &resp, buf, sizeof( long ));
238 bcopy( &mtime, buf + sizeof( long ), sizeof( long ));
239 iov.iov_len = sizeof( long ) + sizeof( long );
243 syslog( LOG_ERR, bad );
246 bcopy( &resp, buf, sizeof( long ));
247 *( buf + 4 ) = (unsigned char)strlen( bad );
248 strcpy( buf + 5, bad );
249 iov.iov_len = sizeof( long ) + 2 + strlen( bad );
254 atpb.atp_sresiov = &iov;
255 atpb.atp_sresiovcnt = 1;
256 if ( atp_sresp( atp, &atpb ) < 0 ) {
257 syslog( LOG_ERR, "main: atp_sresp: %m" );