On 10/18/2022 8:08 AM, Maciek Machnikowski wrote:
> To enable handling lstab in the same way by different pps sources, move
> update_leapsecond_table function from the nmea_pps_source to the generic lstab
> file. This also required moving leapfile filename and its modification time to
> the struct lstab.
> 
> Signed-off-by: Maciek Machnikowski <mac...@machnikowski.net>

Makes sense.

Reviewed-by: Jacob Keller <jacob.e.kel...@intel.com>

Thanks,
Jake

> ---
>  lstab.c                  | 55 ++++++++++++++++++++++++++++++++++++++++
>  lstab.h                  |  8 ++++++
>  ts2phc_nmea_pps_source.c | 51 +++----------------------------------
>  3 files changed, 67 insertions(+), 47 deletions(-)
> 
> diff --git a/lstab.c b/lstab.c
> index a44aead..019e2d1 100644
> --- a/lstab.c
> +++ b/lstab.c
> @@ -3,6 +3,7 @@
>   * @note Copyright (C) 2012 Richard Cochran <richardcoch...@gmail.com>
>   * @note SPDX-License-Identifier: GPL-2.0+
>   */
> +#include <sys/stat.h>
>  #include <inttypes.h>
>  #include <stdio.h>
>  #include <stdlib.h>
> @@ -45,6 +46,8 @@ struct epoch_marker {
>  struct lstab {
>       struct epoch_marker lstab[N_LEAPS];
>       uint64_t expiration_utc;
> +     const char *leapfile;
> +     time_t lsfile_mtime;
>       int length;
>  };
>  
> @@ -157,6 +160,8 @@ static int lstab_read(struct lstab *lstab, const char 
> *name)
>  struct lstab *lstab_create(const char *filename)
>  {
>       struct lstab *lstab = calloc(1, sizeof(*lstab));
> +     struct stat statbuf;
> +     int err;
>  
>       if (!lstab) {
>               return NULL;
> @@ -166,12 +171,57 @@ struct lstab *lstab_create(const char *filename)
>                       free(lstab);
>                       return NULL;
>               }
> +             lstab->leapfile = filename;
> +
> +             err = stat(lstab->leapfile, &statbuf);
> +             if (err) {
> +                     fprintf(stderr, "file status failed on %s: %m",
> +                             lstab->leapfile);
> +                     return NULL;
> +             }
> +
> +             lstab->lsfile_mtime = statbuf.st_mtim.tv_sec;
> +
>       } else {
>               lstab_init(lstab);
>       }
>       return lstab;
>  }
>  
> +int update_leapsecond_table(struct lstab *lstab)
> +{
> +     const char* leapfile;
> +     struct stat statbuf;
> +     int err;
> +
> +     if (!lstab) {
> +             return -1;
> +     }
> +
> +     if (!lstab->leapfile) {
> +             return 0;
> +     }
> +     err = stat(lstab->leapfile, &statbuf);
> +     if (err) {
> +             fprintf(stderr, "file status failed on %s: %m",
> +                     lstab->leapfile);
> +             return -1;
> +     }
> +     if (lstab->lsfile_mtime == statbuf.st_mtim.tv_sec) {
> +             return 0;
> +     }
> +     printf("updating leap seconds file\n");
> +     leapfile = lstab->leapfile;
> +     lstab_destroy(lstab);
> +
> +     lstab = lstab_create(leapfile);
> +     if (!lstab) {
> +             return -1;
> +     }
> +
> +     return 0;
> +}
> +
>  void lstab_destroy(struct lstab *lstab)
>  {
>       free(lstab);
> @@ -182,6 +232,11 @@ enum lstab_result lstab_utc2tai(struct lstab *lstab, 
> uint64_t utctime,
>  {
>       int epoch = -1, index, next;
>  
> +     if (update_leapsecond_table(lstab)) {
> +             fprintf(stderr, "Failed to update leap seconds table");
> +             return LSTAB_UNKNOWN;
> +     }
> +
>       for (index = lstab->length - 1; index > -1; index--) {
>               if (utctime >= lstab->lstab[index].utc) {
>                       epoch = index;
> diff --git a/lstab.h b/lstab.h
> index 3811aed..43a5ec0 100644
> --- a/lstab.h
> +++ b/lstab.h
> @@ -19,6 +19,14 @@ struct lstab;
>   */
>  struct lstab *lstab_create(const char *filename);
>  
> +/**
> + * Updates an instance of a leap second table from the associated file.
> + * @param lstab  Pointer to lstab to be updated.
> + * @return 0 if lstab is up to date, don't use a file or was successfully 
> updated.
> + *         non-zero - on error
> + */
> +int update_leapsecond_table(struct lstab *lstab);
> +
>  /**
>   * Destroys a leap second table instance.
>   * @param lstab  A pointer obtained via lstab_create().
> diff --git a/ts2phc_nmea_pps_source.c b/ts2phc_nmea_pps_source.c
> index 8ea26bf..3a4267d 100644
> --- a/ts2phc_nmea_pps_source.c
> +++ b/ts2phc_nmea_pps_source.c
> @@ -29,8 +29,6 @@
>  struct ts2phc_nmea_pps_source {
>       struct ts2phc_pps_source pps_source;
>       struct config *config;
> -     const char *leapfile;
> -     time_t lsfile_mtime;
>       struct lstab *lstab;
>       pthread_t worker;
>       /* Protects anonymous struct fields, below, from concurrent access. */
> @@ -144,34 +142,6 @@ static void *monitor_nmea_status(void *arg)
>       return NULL;
>  }
>  
> -static int update_leapsecond_table(struct ts2phc_nmea_pps_source *s)
> -{
> -     struct stat statbuf;
> -     int err;
> -
> -     if (!s->leapfile) {
> -             return 0;
> -     }
> -     err = stat(s->leapfile, &statbuf);
> -     if (err) {
> -             pr_err("nmea: file status failed on %s: %m", s->leapfile);
> -             return -1;
> -     }
> -     if (s->lsfile_mtime == statbuf.st_mtim.tv_sec) {
> -             return 0;
> -     }
> -     pr_info("nmea: updating leap seconds file");
> -     if (s->lstab) {
> -             lstab_destroy(s->lstab);
> -     }
> -     s->lstab = lstab_create(s->leapfile);
> -     if (!s->lstab) {
> -             return -1;
> -     }
> -     s->lsfile_mtime = statbuf.st_mtim.tv_sec;
> -     return 0;
> -}
> -
>  static void ts2phc_nmea_pps_source_destroy(struct ts2phc_pps_source *src)
>  {
>       struct ts2phc_nmea_pps_source *s =
> @@ -225,11 +195,6 @@ static int ts2phc_nmea_pps_source_getppstime(struct 
> ts2phc_pps_source *src,
>       utc_time /= (int64_t) 1000000000;
>       *ts = tmv_to_timespec(rmc);
>  
> -     if (update_leapsecond_table(m)) {
> -             pr_err("nmea: failed to update leap seconds table");
> -             return -1;
> -     }
> -
>       result = lstab_utc2tai(m->lstab, utc_time, &tai_offset);
>       switch (result) {
>       case LSTAB_OK:
> @@ -254,28 +219,20 @@ struct ts2phc_pps_source 
> *ts2phc_nmea_pps_source_create(struct ts2phc_private *p
>                                                       const char *dev)
>  {
>       struct ts2phc_nmea_pps_source *s;
> -     struct stat statbuf;
> +     const char* leapfile;
>       int err;
>  
>       s = calloc(1, sizeof(*s));
>       if (!s) {
>               return NULL;
>       }
> -     s->leapfile = config_get_string(priv->cfg, NULL, "leapfile");
> -     s->lstab = lstab_create(s->leapfile);
> +     leapfile = config_get_string(priv->cfg, NULL, "leapfile");
> +     s->lstab = lstab_create(leapfile);
>       if (!s->lstab) {
>               free(s);
>               return NULL;
>       }
> -     if (s->leapfile) {
> -             err = stat(s->leapfile, &statbuf);
> -             if (err) {
> -                     lstab_destroy(s->lstab);
> -                     free(s);
> -                     return NULL;
> -             }
> -             s->lsfile_mtime = statbuf.st_mtim.tv_sec;
> -     }
> +
>       s->pps_source.destroy = ts2phc_nmea_pps_source_destroy;
>       s->pps_source.getppstime = ts2phc_nmea_pps_source_getppstime;
>       s->config = priv->cfg;


_______________________________________________
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel

Reply via email to