Zum Hauptinhalt springen

4. Programmiermodell und Schnittstellen

Dieser Abschnitt beschreibt das Programmiermodell für den Synchronisierungs-Daemon und Benutzeranwendungsprogramme. Die Ideen basieren auf Vorschlägen von Jeff Mogul und Philip Gladstone sowie einer vom letzteren entworfenen ähnlichen Schnittstelle. Es ist wichtig darauf hinzuweisen, dass die Funktionalität des ursprünglichen Unix-Systemaufrufs adjtime() erhalten bleibt, sodass der geänderte Kernel wie der unveränderte arbeitet, wenn die neuen Funktionen nicht verwendet werden. In diesem Fall kann der Systemaufruf ntp_adjtime() dennoch verwendet werden, um Kernel-Variablen zu lesen und zu schreiben, die beispielsweise von einem anderen Synchronisierungs-Daemon als NTP genutzt werden könnten.

4.1. Der Systemaufruf ntp_gettime()​

Syntax und Semantik des Aufrufs ntp_gettime() sind im folgenden Fragment der Header-Datei timex.h angegeben. Diese Datei ist in den Kernel-Distributionen für SunOS, Ultrix und OSF/1 identisch, abgesehen von der Definition SHIFT_HZ. (Die Definition SHIFT_HZ stellt den Logarithmus zur Basis 2 der Uhr-Oszillatorfrequenz dar, die für jeden Systemtyp spezifisch ist.) Beachten Sie, dass die Datei timex.h die System-Header-Datei syscall.h aufruft, die geändert werden muss, um den für jeden Systemtyp spezifischen Systemaufruf SYS_ntp_gettime zu definieren. Die Kernel-Distributionen enthalten Anweisungen dazu, wie dies zu tun ist.

      /*
       * This header file defines the Network Time Protocol (NTP)
       * interfaces for user and daemon application programs. These are
       * implemented using private system calls and data structures and
       * require specific kernel support.
       *
       * NAME
       *   ntp_gettime - NTP user application interface
       *
       * SYNOPSIS
       *   #include <sys/timex.h>
       *
       *   int system call(SYS_ntp_gettime, tptr)
       *
       *   int SYS_ntp_gettime     defined in syscall.h header file
       *   struct ntptimeval *tptr pointer to ntptimeval structure
       *
       * NTP user interface - used to read kernel clock values
       * Note: maximum error = NTP synch distance = dispersion + delay /
       * 2
       * estimated error = NTP dispersion.
       */
      struct ntptimeval {
           struct timeval time;    /* current time */
           long maxerror;          /* maximum error (us) */
           long esterror;          /* estimated error (us) */
      };

      The ntp_gettime() system call returns three values in the
      ntptimeval structure: the current time in unix timeval format plus
      the maximum and estimated errors in microseconds. While the 32-bit
      long data type limits the error quantities to something more than
      an hour, in practice this is not significant, since the protocol
      itself will declare an unsynchronized condition well below that
      limit. In the NTP Version 3 specification, if the protocol
      computes either of these values in excess of 16 seconds, they are
      clamped to that value and the system clock declared
      unsynchronized.

      Following is a detailed description of the ntptimeval structure
      members.

      struct timeval time;    /* current time */

         This member returns the current system time, expressed as a
         Unix timeval structure. The timeval structure consists of two
         32-bit words; the first returns the number of seconds past 1
         January 1970, while the second returns the number of
         microseconds.

      long maxerror;          /* maximum error (us) */

         This member returns the time_maxerror kernel variable in
         microseconds. See the entry for this variable in section 5 for
         additional information.

      long esterror;          /* estimated error (us) */

         This member returns the time_esterror kernel variable in
         microseconds. See the entry for this variable in section 5 for
         additional information.

4.2. Der Systemaufruf ntp_adjtime()​

Syntax und Semantik des Aufrufs ntp_adjtime() sind im folgenden Fragment der Header-Datei timex.h angegeben. Beachten Sie, dass wie beim Systemaufruf ntp_gettime() die System-Header-Datei syscall.h geändert werden muss, um den für jeden Systemtyp spezifischen Systemaufruf SYS_ntp_adjtime zu definieren.

      /*
       * NAME
       *   ntp_adjtime - NTP daemon application interface
       *
       * SYNOPSIS
       *   #include <sys/timex.h>
       *
       *   int system call(SYS_ntp_adjtime, mode, tptr)
       *
       *   int SYS_ntp_adjtime     defined in syscall.h header file
       *   struct timex *tptr      pointer to timex structure
       *
       * NTP daemon interface - used to discipline kernel clock
       * oscillator
       */
      struct timex {
          int mode;                /* mode selector */
          long offset;             /* time offset (us) */
          long frequency;          /* frequency offset (scaled ppm) */
          long maxerror;           /* maximum error (us) */
          long esterror;           /* estimated error (us) */
          int status;              /* clock command/status */
          long time_constant;      /* pll time constant */
          long precision;          /* clock precision (us) (read only)
                                    */
          long tolerance;          /* clock frequency tolerance (scaled
                                    * ppm) (read only) */
          /*
           * The following read-only structure members are implemented
           * only if the PPS signal discipline is configured in the
           * kernel.
           */
          long ybar;               /* frequency estimate (scaled ppm) */
          long disp;               /* dispersion estimate (scaled ppm)
                                    */
          int shift;               /* interval duration (s) (shift) */
          long calcnt;             /* calibration intervals */
          long jitcnt;             /* jitter limit exceeded */
          long discnt;             /* dispersion limit exceeded */
      };

      The ntp_adjtime() system call is used to read and write certain
      time-related kernel variables summarized in this and subsequent
      sections. Writing these variables can only be done in superuser
      mode. To write a variable, the mode structure member is set with
      one or more bits, one of which is assigned each of the following
      variables in turn. The current values for all variables are
      returned in any case; therefore, a mode argument of zero means to
      return these values without changing anything.

      Following is a description of the timex structure members.

      int mode;               /* mode selector */

         This is a bit-coded variable selecting one or more structure
         members, with one bit assigned each member. If a bit is set,
         the value of the associated member variable is copied to the
         corresponding kernel variable; if not, the member is ignored.
         The bits are assigned as given in the following fragment of the
         timex.h header file. Note that the precision and tolerance are
         determined by the kernel and cannot be changed by
         ntp_adjtime().

         /*
          * Mode codes (timex.mode)
          */
         #define ADJ_OFFSET       0x0001    /* time offset */
         #define ADJ_FREQUENCY    0x0002    /* frequency offset */
         #define ADJ_MAXERROR     0x0004    /* maximum time error */
         #define ADJ_ESTERROR     0x0008    /* estimated time error */
         #define ADJ_STATUS       0x0010    /* clock status */
         #define ADJ_TIMECONST    0x0020    /* pll time constant */

      long offset;            /* time offset (us) */

         If selected, this member replaces the value of the time_offset
         kernel variable in microseconds. The absolute value must be
         less than MAXPHASE microseconds defined in the timex.h header
         file. See the entry for this variable in section 5 for
         additional information.

         If within range and the PPS signal and/or external oscillator
         are configured and operating properly, the clock status is
         automatically set to TIME_OK.

      long time_constant;     /* pll time constant */

         If selected, this member replaces the value of the
         time_constant kernel variable. The value must be between zero
         and MAXTC defined in the timex.h header file. See the entry for
         this variable in section 5 for additional information.

      long frequency;         /* frequency offset (scaled ppm) */

         If selected, this member replaces the value of the
         time_frequency kernel variable. The value is in ppm, with the
         integer part in the high order 16 bits and fraction in the low
         order 16 bits. The absolute value must be in the range less
         than MAXFREQ ppm defined in the timex.h header file. See the
         entry for this variable in section 5 for additional
         information.

      long maxerror;          /* maximum error (us) */

         If selected, this member replaces the value of the
         time_maxerror kernel variable in microseconds. See the entry
         for this variable in section 5 for additional information.

      long esterror;          /* estimated error (us) */

         If selected, this member replaces the value of the
         time_esterror kernel variable in microseconds. See the entry
         for this variable in section 5 for additional information.

      int status;             /* clock command/status */

         If selected, this member replaces the value of the time_status
         kernel variable. See the entry for this variable in section 5
         for additional information.

         In order to set this variable by ntp_adjtime(), either (a) the
         current clock status must be TIME_OK or (b) the member value is
         TIME_BAD; that is, the ntp_adjtime() call can always set the
         clock to the unsynchronized state or, if the clock is running
         correctly, can set it to any state. In any case, the
         ntp_adjtime() call always returns the current state in this
         member, so the caller can determine whether or not the request
         succeeded.

      long time_constant;     /* pll time constant */

         If selected, this member replaces the value of the
         time_constant kernel variable. The value must be between zero
         and MAXTC defined in the timex.h header file. See the entry for
         this variable in section 5 for additional information.

      long precision;         /* clock precision (us) (read only) */

         This member returns the time_precision kernel variable in
         microseconds. The variable can be written only by the kernel.
         See the entry for this variable in section 5 for additional
         information.

      long tolerance;         /* clock frequency tolerance (scaled ppm)
                               */

         This member returns the time_tolerance kernel variable in
         microseconds. The variable can be written only by the kernel.
         See the entry for this variable in section 5 for additional
         information.

      long ybar;              /* frequency estimate (scaled ppm) */

         This member returns the pps_ybar kernel variable in
         microseconds. The variable can be written only by the kernel.
         See the entry for this variable in section 5 for additional
         information.

      long disp;              /* dispersion estimate (scaled ppm) */

         This member returns the pps_disp kernel variable in
         microseconds. The variable can be written only by the kernel.
         See the entry for this variable in section 5 for additional
         information.

      int shift;              /* interval duration (s) (shift) */

         This member returns the pps_shift kernel variable in
         microseconds. The variable can be written only by the kernel.
         See the entry for this variable in section 5 for additional
         information.

      long calcnt;            /* calibration intervals */

         This member returns the pps_calcnt kernel variable in
         microseconds. The variable can be written only by the kernel.
         See the entry for this variable in section 5 for additional
         information.

      long jitcnt;            /* jitter limit exceeded */

         This member returns the pps_jittcnt kernel variable in
         microseconds. The variable can be written only by the kernel.
         See the entry for this variable in section 5 for additional
         information.

      long discnt;            /* dispersion limit exceeded */

         This member returns the pps_discnt kernel variable in
         microseconds. The variable can be written only by the kernel.
         See the entry for this variable in section 5 for additional
         information.

4.3. Befehls-/Statuscodes​

Die Kernel-Routinen verwenden die Systemuhr-Statusvariable time_status, die aufzeichnet, ob die Uhr synchronisiert ist, auf eine Schaltsekunde wartet usw. Der Wert dieser Variablen wird als Ergebniscode sowohl vom Systemaufruf ntp_gettime() als auch von ntp_adjtime() zurückgegeben. Darüber hinaus kann sie explizit mit dem Systemaufruf ntp_adjtime() gelesen und geschrieben werden, darf aber nur im Superuser-Modus geschrieben werden. Die derzeit in der Header-Datei timex.h definierten Werte sind wie folgt:

      /*
* Clock command/status codes (timex.status)
*/
#define TIME_OK 0 /* clock synchronized */
#define TIME_INS 1 /* insert leap second */
#define TIME_DEL 2 /* delete leap second */
#define TIME_OOP 3 /* leap second in progress */
#define TIME_BAD 4 /* kernel clock not synchronized */
#define TIME_ERR 5 /* external oscillator not
synchronized */

A detailed description of these codes as used by the leap-second
state machine is given later in this memorandum. In case of a
negative result code, the kernel has intercepted an invalid
address or (in case of the ntp_adjtime() system call), a superuser
violation.