]> granicus.if.org Git - sysstat/commitdiff
Use thread-safe versions of gmtime() and localtime()
authorSebastien GODARD <sysstat@users.noreply.github.com>
Fri, 21 Sep 2018 14:45:01 +0000 (16:45 +0200)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Fri, 21 Sep 2018 14:45:01 +0000 (16:45 +0200)
It is safer to use gmtime_r() and localtime_r() functions (which are
thread-safe) instead of gmtime() and localtime() ones, even though
sysstat's code doesn't use multiple threads.

Signed-off-by: Sebastien GODARD <sysstat@users.noreply.github.com>
common.c
sa_common.c
sadf.c
sadf_misc.c
svg_stats.c

index d418abf1438bf1eb2e51e46a29bff482b854310d..bdcaa50de93f132f97ebe9e939e969b9e1469572 100644 (file)
--- a/common.c
+++ b/common.c
@@ -97,15 +97,11 @@ void print_version(void)
 time_t get_localtime(struct tm *rectime, int d_off)
 {
        time_t timer;
-       struct tm *ltm;
 
        time(&timer);
        timer -= SEC_PER_DAY * d_off;
-       ltm = localtime(&timer);
+       localtime_r(&timer, rectime);
 
-       if (ltm) {
-               *rectime = *ltm;
-       }
        return timer;
 }
 
@@ -126,15 +122,11 @@ time_t get_localtime(struct tm *rectime, int d_off)
 time_t get_gmtime(struct tm *rectime, int d_off)
 {
        time_t timer;
-       struct tm *ltm;
 
        time(&timer);
        timer -= SEC_PER_DAY * d_off;
-       ltm = gmtime(&timer);
+       gmtime_r(&timer, rectime);
 
-       if (ltm) {
-               *rectime = *ltm;
-       }
        return timer;
 }
 
index 20cf4e574f74ac226bcad95f98e73b72caf5351b..e13759318755b4959edfbda13f679cb47389e12f 100644 (file)
@@ -804,8 +804,6 @@ void get_itv_value(struct record_header *record_hdr_curr,
 void get_file_timestamp_struct(unsigned int flags, struct tm *rectime,
                               struct file_header *file_hdr)
 {
-       struct tm *loc_t;
-
        if (PRINT_TRUE_TIME(flags)) {
                /* Get local time. This is just to fill fields with a default value. */
                get_time(rectime, 0);
@@ -821,9 +819,7 @@ void get_file_timestamp_struct(unsigned int flags, struct tm *rectime,
                mktime(rectime);
        }
        else {
-               if ((loc_t = localtime((const time_t *) &file_hdr->sa_ust_time)) != NULL) {
-                       *rectime = *loc_t;
-               }
+               localtime_r((const time_t *) &file_hdr->sa_ust_time, rectime);
        }
 }
 
@@ -2692,8 +2688,10 @@ int sa_get_record_timestamp_struct(unsigned int l_flags, struct record_header *r
 
        /* Fill localtime structure if given */
        if (loctime) {
-               if ((ltm = localtime((const time_t *) &(record_hdr->ust_time))) != NULL) {
-                       *loctime = *ltm;
+               ltm = localtime_r((const time_t *) &(record_hdr->ust_time), loctime);
+               if (ltm) {
+                       /* Done so that we have some default values */
+                       *rectime = *loctime;
                }
                else {
                        rc = 1;
@@ -2703,7 +2701,7 @@ int sa_get_record_timestamp_struct(unsigned int l_flags, struct record_header *r
        /* Fill generic rectime structure */
        if (PRINT_LOCAL_TIME(l_flags) && !ltm) {
                /* Get local time if not already done */
-               ltm = localtime((const time_t *) &(record_hdr->ust_time));
+               ltm = localtime_r((const time_t *) &(record_hdr->ust_time), rectime);
        }
 
        if (!PRINT_LOCAL_TIME(l_flags) && !PRINT_TRUE_TIME(l_flags)) {
@@ -2711,14 +2709,10 @@ int sa_get_record_timestamp_struct(unsigned int l_flags, struct record_header *r
                 * Get time in UTC
                 * (the user doesn't want local time nor time of file's creator).
                 */
-               ltm = gmtime((const time_t *) &(record_hdr->ust_time));
+               ltm = gmtime_r((const time_t *) &(record_hdr->ust_time), rectime);
        }
 
-       if (ltm) {
-               /* Done even in true time mode so that we have some default values */
-               *rectime = *ltm;
-       }
-       else {
+       if (!ltm) {
                rc = 1;
        }
 
diff --git a/sadf.c b/sadf.c
index f36d05239b0ef98ee0240b59365d33747146f230..d10d98b522c0c10d6d7f7ccc21dfe5d985cad5fc 100644 (file)
--- a/sadf.c
+++ b/sadf.c
@@ -380,16 +380,16 @@ void list_fields(unsigned int act_id)
  */
 time_t get_time_ref(void)
 {
-       struct tm *ltm;
+       struct tm ltm;
        time_t t;
 
        if (DISPLAY_ONE_DAY(flags)) {
-               ltm = localtime((time_t *) &(record_hdr[2].ust_time));
+               localtime_r((time_t *) &(record_hdr[2].ust_time), &ltm);
 
                /* Move back to midnight */
-               ltm->tm_sec = ltm->tm_min = ltm->tm_hour = 0;
+               ltm.tm_sec = ltm.tm_min = ltm.tm_hour = 0;
 
-               t = mktime(ltm);
+               t = mktime(&ltm);
                if (t != -1)
                        return t;
        }
index da01ee59d90858093351bf8ac5f78a52d74177e1..bbb4c2ff6560ab4d022e8d099a4af175bc47df95 100644 (file)
@@ -694,7 +694,7 @@ __printf_funct_t print_xml_header(void *parm, int action, char *dfile,
                                  struct activity *act[], unsigned int id_seq[],
                                  struct file_activity *file_actlst)
 {
-       struct tm rectime, *loc_t;
+       struct tm rectime, loc_t;
        char cur_time[TIMESTAMP_LEN];
        int *tab = (int *) parm;
 
@@ -726,8 +726,8 @@ __printf_funct_t print_xml_header(void *parm, int action, char *dfile,
                strftime(cur_time, sizeof(cur_time), "%Y-%m-%d", &rectime);
                xprintf(*tab, "<file-date>%s</file-date>", cur_time);
 
-               if ((loc_t = gmtime((const time_t *) &file_hdr->sa_ust_time)) != NULL) {
-                       strftime(cur_time, sizeof(cur_time), "%T", loc_t);
+               if (gmtime_r((const time_t *) &file_hdr->sa_ust_time, &loc_t) != NULL) {
+                       strftime(cur_time, sizeof(cur_time), "%T", &loc_t);
                        xprintf(*tab, "<file-utc-time>%s</file-utc-time>", cur_time);
                }
 
@@ -762,7 +762,7 @@ __printf_funct_t print_json_header(void *parm, int action, char *dfile,
                                   struct activity *act[], unsigned int id_seq[],
                                   struct file_activity *file_actlst)
 {
-       struct tm rectime, *loc_t;
+       struct tm rectime, loc_t;
        char cur_time[TIMESTAMP_LEN];
        int *tab = (int *) parm;
 
@@ -784,8 +784,8 @@ __printf_funct_t print_json_header(void *parm, int action, char *dfile,
                strftime(cur_time, sizeof(cur_time), "%Y-%m-%d", &rectime);
                xprintf0(*tab, "\"file-date\": \"%s\"", cur_time);
 
-               if ((loc_t = gmtime((const time_t *) &file_hdr->sa_ust_time)) != NULL) {
-                       strftime(cur_time, sizeof(cur_time), "%T", loc_t);
+               if (gmtime_r((const time_t *) &file_hdr->sa_ust_time, &loc_t) != NULL) {
+                       strftime(cur_time, sizeof(cur_time), "%T", &loc_t);
                        printf(",\n");
                        xprintf0(*tab, "\"file-utc-time\": \"%s\"", cur_time);
                }
@@ -821,7 +821,7 @@ __printf_funct_t print_hdr_header(void *parm, int action, char *dfile,
                                  struct file_activity *file_actlst)
 {
        int i, p;
-       struct tm rectime, *loc_t;
+       struct tm rectime, loc_t;
        struct file_activity *fal;
        char cur_time[TIMESTAMP_LEN];
 
@@ -841,7 +841,7 @@ __printf_funct_t print_hdr_header(void *parm, int action, char *dfile,
                       file_magic->upgraded);
 
                printf(_("Host: "));
-               print_gal_header(localtime((const time_t *) &(file_hdr->sa_ust_time)),
+               print_gal_header(localtime_r((const time_t *) &(file_hdr->sa_ust_time), &rectime),
                                 file_hdr->sa_sysname, file_hdr->sa_release,
                                 file_hdr->sa_nodename, file_hdr->sa_machine,
                                 file_hdr->sa_cpu_nr > 1 ? file_hdr->sa_cpu_nr - 1 : 1,
@@ -852,9 +852,9 @@ __printf_funct_t print_hdr_header(void *parm, int action, char *dfile,
                strftime(cur_time, sizeof(cur_time), "%Y-%m-%d", &rectime);
                printf(_("File date: %s\n"), cur_time);
 
-               if ((loc_t = gmtime((const time_t *) &file_hdr->sa_ust_time)) != NULL) {
+               if (gmtime_r((const time_t *) &file_hdr->sa_ust_time, &loc_t) != NULL) {
                        printf(_("File time: "));
-                       strftime(cur_time, sizeof(cur_time), "%T", loc_t);
+                       strftime(cur_time, sizeof(cur_time), "%T", &loc_t);
                        printf("%s UTC\n", cur_time);
                }
 
@@ -919,6 +919,7 @@ __printf_funct_t print_svg_header(void *parm, int action, char *dfile,
                                  struct file_activity *file_actlst)
 {
        struct svg_hdr_parm *hdr_parm = (struct svg_hdr_parm *) parm;
+       struct tm rectime;
        unsigned int height = 0, ht = 0;
        int i, p;
 
@@ -956,7 +957,7 @@ __printf_funct_t print_svg_header(void *parm, int action, char *dfile,
                       " fill=\"black\" stroke=\"gray\" stroke-width=\"1\">\n",
                       SVG_T_XSIZE * (hdr_parm->views_per_row), height);
                printf("<text x=\"0\" y=\"30\" text-anchor=\"start\" stroke=\"brown\">");
-               print_gal_header(localtime((const time_t *) &(file_hdr->sa_ust_time)),
+               print_gal_header(localtime_r((const time_t *) &(file_hdr->sa_ust_time), &rectime),
                                 file_hdr->sa_sysname, file_hdr->sa_release,
                                 file_hdr->sa_nodename, file_hdr->sa_machine,
                                 file_hdr->sa_cpu_nr > 1 ? file_hdr->sa_cpu_nr - 1 : 1,
index eadff1ecefaa2e1fa92cc2ef5007317d93b45acb..ecb74862fb727182f96008795f352dfef6b214ce 100644 (file)
@@ -832,6 +832,7 @@ int draw_activity_graphs(int g_nr, int g_type[], char *title[], char *g_title[],
        long int xpos;
        double lmax, xfactor, yfactor, ypos, gmin, gmax;
        char val[32], cur_date[TIMESTAMP_LEN];
+       struct tm rectime;
 
        /* For each view which is part of current activity */
        for (i = 0; i < g_nr; i++) {
@@ -931,7 +932,7 @@ int draw_activity_graphs(int g_nr, int g_type[], char *title[], char *g_title[],
                               svg_p->file_hdr->sa_nodename);
 
                        /* Get report date */
-                       set_report_date(localtime((const time_t *) &(svg_p->file_hdr->sa_ust_time)),
+                       set_report_date(localtime_r((const time_t *) &(svg_p->file_hdr->sa_ust_time), &rectime),
                                        cur_date, sizeof(cur_date));
                        printf("<tspan x=\"%d\" y=\"%d\" "
                               "style=\"fill: yellow; text-anchor: end; stroke: none; font-size: 14px\">"