2 * $Id: timelord.c,v 1.6 2001-08-30 13:14:53 srittau 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>
31 #include <netatalk/at.h>
32 #include <netatalk/endian.h>
33 #include <atalk/atp.h>
34 #include <atalk/nbp.h>
39 #endif /* HAVE_SGTTY_H */
47 #endif /* HAVE_NETDB_H */
51 #endif /* HAVE_FCNTL_H */
52 #ifdef HAVE_SYS_FCNTL_H
53 #include <sys/fcntl.h>
54 #endif /* HAVE_SYS_FCNTL_H */
58 #endif /* HAVE_TERMIOS_H */
59 #ifdef HAVE_SYS_TERMIOS_H
60 #include <sys/termios.h>
61 #endif /* HAVE_SYS_TERMIOS_H */
66 #define EPOCH 0x7C25B080 /* 00:00:00 GMT Jan 1, 1970 for Mac */
69 char *bad = "Bad request!";
77 if (( s = rindex( p, '/' )) == NULL ) {
82 fprintf( stderr, "Usage:\t%s -d -n nbpname\n", s );
87 * Unregister ourself on signal.
92 if ( nbp_unrgstr( server, "TimeLord", "*", NULL ) < 0 ) {
93 syslog( LOG_ERR, "Can't unregister %s", server );
96 syslog( LOG_INFO, "going down" );
100 int main( int ac, char **av )
103 struct sockaddr_at sat;
104 struct atp_block atpb;
109 char hostname[ MAXHOSTNAMELEN ];
112 long req, mtime, resp;
116 if ( gethostname( hostname, sizeof( hostname )) < 0 ) {
117 perror( "gethostname" );
120 if (( server = index( hostname, '.' )) != 0 ) {
125 while (( c = getopt( ac, av, "dn:" )) != EOF ) {
134 fprintf( stderr, "Unknown option -- '%c'\n", c );
140 * Disassociate from controlling tty.
147 dt = getdtablesize();
148 for ( i = 0; i < dt; i++ ) {
151 if (( i = open( "/dev/tty", O_RDWR )) >= 0 ) {
152 (void)ioctl( i, TIOCNOTTY, 0 );
153 setpgid( 0, getpid());
165 if (( p = rindex( *av, '/' )) == NULL ) {
172 openlog( p, LOG_PID );
174 openlog( p, LOG_NDELAY|LOG_PID, LOG_DAEMON );
177 /* allocate memory */
178 memset (&sat.sat_addr, 0, sizeof (sat.sat_addr));
180 if (( atp = atp_open( ATADDR_ANYPORT, &sat.sat_addr )) == NULL ) {
181 syslog( LOG_ERR, "main: atp_open: %s", strerror( errno ) );
185 if ( nbp_rgstr( atp_sockaddr( atp ), server, "TimeLord", "*" ) < 0 ) {
186 syslog( LOG_ERR, "Can't register %s", server );
189 syslog( LOG_INFO, "%s:TimeLord started", server );
191 signal(SIGHUP, goaway);
192 signal(SIGTERM, goaway);
196 * Something seriously wrong with atp, since these assigns must
199 atpb.atp_saddr = &sat;
200 atpb.atp_rreqdata = buf;
201 bzero( &sat, sizeof( struct sockaddr_at ));
202 atpb.atp_rreqdlen = sizeof( buf );
203 if ( atp_rreq( atp, &atpb ) < 0 ) {
204 syslog( LOG_ERR, "main: atp_rreq: %s", strerror( errno ) );
209 bcopy( p, &req, sizeof( long ));
215 if ( atpb.atp_rreqdlen > 5 ) {
216 bcopy( p + 1, &mtime, sizeof( long ));
217 mtime = ntohl( mtime );
218 syslog( LOG_INFO, "gettime from %s %s was %lu",
219 (*( p + 5 ) == '\0' ) ? "<unknown>" : p + 5,
220 ( *p == 0 ) ? "at boot" : "in chooser",
223 syslog( LOG_INFO, "gettime" );
226 if ( gettimeofday( &tv, &tz ) < 0 ) {
227 syslog( LOG_ERR, "main: gettimeofday: %s", strerror( errno ) );
230 if (( tm = gmtime( &tv.tv_sec )) == 0 ) {
231 perror( "localtime" );
235 mtime = tv.tv_sec + EPOCH;
236 mtime = htonl( mtime );
239 bcopy( &resp, buf, sizeof( long ));
240 bcopy( &mtime, buf + sizeof( long ), sizeof( long ));
241 iov.iov_len = sizeof( long ) + sizeof( long );
245 syslog( LOG_ERR, bad );
248 bcopy( &resp, buf, sizeof( long ));
249 *( buf + 4 ) = (unsigned char)strlen( bad );
250 strcpy( buf + 5, bad );
251 iov.iov_len = sizeof( long ) + 2 + strlen( bad );
256 atpb.atp_sresiov = &iov;
257 atpb.atp_sresiovcnt = 1;
258 if ( atp_sresp( atp, &atpb ) < 0 ) {
259 syslog( LOG_ERR, "main: atp_sresp: %s", strerror( errno ) );