[comp.sys.hp] Time server for HP 9000/3xx??

jsd@esl.ESL.COM (Jeff Dalton) (01/04/91)

I've gotten off the net a version xntp for the Sun and it
seems to be a lot more than I need.  All I want to do is
sync time on about 15 nodes.  The time doesn't have to be
very accurate, all the nodes just need to be within a
second or so.

Is there a time server (such as ntp) which will allow me
to run the server on a node I specify and then have up to
15 clients?  Is it available for the HP9000/3xx running 
HP-UX 7.0?

Any help is appreciated...


-- 
Jeff Dalton, ESL Inc.                    Real programmers can write 
jsd@esl.com                                 Fortran in any language.

paul@eye.com (Paul B. Booth) (01/05/91)

In article <370@esl.ESL.COM> jsd@bambam.UUCP (Jeff Dalton) writes:
>All I want to do is
>sync time on about 15 nodes.  The time doesn't have to be
>very accurate, all the nodes just need to be within a
>second or so.

The simplest time server I've seen is udptime, which was given to us by a
couple of HP engineers from Germany a while back.  It's totally homegrown
(no docs, comments, warranty, etc.), but has worked very well for our herd of
25 300's and 800s.  I kick off udptime -r from /etc/rc on all nodes in the net
and then run udptime -s from cron on one node once a day.  Seems to do it.

enjoy

Paul B. Booth  (paul@eye.com) (...!hplabs!hpfcla!eye!paul)
-------------------------------------------------------------------------------
3D/EYE, Inc., 2359 N. Triphammer Rd., Ithaca, NY  14850    voice: (607)257-1381
                                                             fax: (607)257-7335

							    
/*

  udptime - send/receive time information

  usage:

    udptime -r   to receive time information; runs as a daemon.
    udptime -s   to send time information.  Sends time on local
		 node to all nodes running udptime -r.

    s300: cc -O -s -N -Wc,-Nd2000 -Wc,-Ns2000 udptime.c -o udptime
    s800: cc -O -s udptime.c -o udptime

*/

#include <sys/types.h>

#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <time.h>

#define PORT    873
#define PREFIX  "udptime 1.0"
#define POSTFIX "trailer"

extern unsigned long  sleep();
extern void exit();
extern void perror();

struct timebuf {
  char  prefix[16];
  struct timeval tv;
  struct timezone tz;
  char  postfix[16];
};

static char  *myname;

/*---------------------------------------------------------------------------*/

static void giveup()
{
  perror(myname);
  exit(1);
}

/*---------------------------------------------------------------------------*/

static void receive_time()
{

  int  i;
  int  s;
  struct sockaddr_in myaddr;
  struct timebuf timebuf;

  if (fork()) return;
  if (chdir("/")) exit(1);
  for (i = 0; i < _NFILE; i++) close(i);
  setpgrp();
  if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) exit(1);
  myaddr.sin_family = AF_INET;
  myaddr.sin_addr.s_addr = INADDR_ANY;
  myaddr.sin_port = PORT;
  if (bind(s, &myaddr, sizeof(myaddr)) < 0) exit(1);
  for (; ; )
    if (read(s, &timebuf, sizeof(timebuf)) == sizeof(timebuf) && !strcmp(timebuf.prefix, PREFIX) && !strcmp(timebuf.postfix, POSTFIX))
      settimeofday(&timebuf.tv, &timebuf.tz);
}

/*---------------------------------------------------------------------------*/

static void send_time()
{

  int  i;
  int  s;
  struct sockaddr_in myaddr;
  struct sockaddr_in toaddr;
  struct timebuf timebuf;

  if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) giveup();
  myaddr.sin_family = AF_INET;
  myaddr.sin_addr.s_addr = INADDR_ANY;
  myaddr.sin_port = 0;
  if (bind(s, &myaddr, sizeof(myaddr)) < 0) giveup();
  toaddr.sin_family = AF_INET;
  toaddr.sin_addr.s_addr = INADDR_BROADCAST;
  toaddr.sin_port = PORT;
  strcpy(timebuf.prefix, PREFIX);
  strcpy(timebuf.postfix, POSTFIX);
  for (i = 0; i < 10; i++) {
    if (gettimeofday(&timebuf.tv, &timebuf.tz) < 0) giveup();
    if (sendto(s, &timebuf, sizeof(timebuf), 0, &toaddr, sizeof(toaddr)) != sizeof(timebuf)) giveup();
    sleep(1);
  }
}

/*---------------------------------------------------------------------------*/

main(argc, argv)
int  argc;
char  **argv;
{
  myname = argv[0];
  if (argc != 2 || strcmp(argv[1], "-r") && strcmp(argv[1], "-s")) {
    printf("usage: %s -r\n", myname);
    printf("       %s -s\n", myname);
    exit(1);
  }
  if (!strcmp(argv[1], "-r"))
    receive_time();
  else
    send_time();
  return 0;
}