From 9b1dffc1569bee8473e7b402e103ea605e30d534 Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Fri, 21 Sep 2018 16:45:01 +0200 Subject: [PATCH] Use thread-safe versions of gmtime() and localtime() 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 --- common.c | 12 ++---------- sa_common.c | 22 ++++++++-------------- sadf.c | 8 ++++---- sadf_misc.c | 23 ++++++++++++----------- svg_stats.c | 3 ++- 5 files changed, 28 insertions(+), 40 deletions(-) diff --git a/common.c b/common.c index d418abf..bdcaa50 100644 --- 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; } diff --git a/sa_common.c b/sa_common.c index 20cf4e5..e137593 100644 --- a/sa_common.c +++ b/sa_common.c @@ -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 f36d052..d10d98b 100644 --- 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), <m); /* 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(<m); if (t != -1) return t; } diff --git a/sadf_misc.c b/sadf_misc.c index da01ee5..bbb4c2f 100644 --- a/sadf_misc.c +++ b/sadf_misc.c @@ -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, "%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); xprintf(*tab, "%s", 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(""); - 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, diff --git a/svg_stats.c b/svg_stats.c index eadff1e..ecb7486 100644 --- a/svg_stats.c +++ b/svg_stats.c @@ -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("" -- 2.40.0