Make slave-stats updates atomic

This commit is contained in:
Viktor Dukhovni
2017-12-13 17:44:42 -05:00
committed by Nico Williams
parent bac9c1fe94
commit 49d56916e1

View File

@@ -38,9 +38,10 @@ static krb5_log_facility *log_facility;
static int verbose; static int verbose;
const char *slave_stats_file; static const char *slave_stats_file;
const char *slave_time_missing = "2 min"; static const char *slave_stats_temp_file;
const char *slave_time_gone = "5 min"; static const char *slave_time_missing = "2 min";
static const char *slave_time_gone = "5 min";
static int time_before_missing; static int time_before_missing;
static int time_before_gone; static int time_before_gone;
@@ -946,32 +947,26 @@ process_msg (kadm5_server_context *server_context, slave *s, int log_fd,
#define SLAVE_STATUS "Status" #define SLAVE_STATUS "Status"
#define SLAVE_SEEN "Last Seen" #define SLAVE_SEEN "Last Seen"
static FILE * static void
open_stats(krb5_context context) init_stats_names(krb5_context context)
{ {
char *statfile = NULL;
const char *fn = NULL; const char *fn = NULL;
FILE *out = NULL; char *buf = NULL;
/*
* krb5_config_get_string_default() returs default value as-is,
* delay free() of "statfile" until we're done with "fn".
*/
if (slave_stats_file) if (slave_stats_file)
fn = slave_stats_file; fn = slave_stats_file;
else if (asprintf(&statfile, "%s/slaves-stats", hdb_db_dir(context)) != -1 else if ((fn = krb5_config_get_string(context, NULL, "kdc",
&& statfile != NULL) "iprop-stats", NULL)) == NULL) {
fn = krb5_config_get_string_default(context, if (asprintf(&buf, "%s/slaves-stats", hdb_db_dir(context)) != -1
NULL, && buf != NULL)
statfile, fn = buf;
"kdc", buf = NULL;
"iprop-stats", }
NULL); if (fn != NULL) {
if (fn != NULL) slave_stats_file = fn;
out = fopen(fn, "w"); if (asprintf(&buf, "%s.tmp", fn) != -1 && buf != NULL)
if (statfile != NULL) slave_stats_temp_file = buf;
free(statfile); }
return out;
} }
static void static void
@@ -979,15 +974,17 @@ write_master_down(krb5_context context)
{ {
char str[100]; char str[100];
time_t t = time(NULL); time_t t = time(NULL);
FILE *fp; FILE *fp = NULL;
fp = open_stats(context); if (slave_stats_temp_file != NULL)
fp = fopen(slave_stats_temp_file, "w");
if (fp == NULL) if (fp == NULL)
return; return;
krb5_format_time(context, t, str, sizeof(str), TRUE); krb5_format_time(context, t, str, sizeof(str), TRUE);
fprintf(fp, "master down at %s\n", str); fprintf(fp, "master down at %s\n", str);
fclose(fp); if (fclose(fp) != EOF)
rk_rename(slave_stats_temp_file, slave_stats_file);
} }
static void static void
@@ -996,9 +993,10 @@ write_stats(krb5_context context, slave *slaves, uint32_t current_version)
char str[100]; char str[100];
rtbl_t tbl; rtbl_t tbl;
time_t t = time(NULL); time_t t = time(NULL);
FILE *fp; FILE *fp = NULL;
fp = open_stats(context); if (slave_stats_temp_file != NULL)
fp = fopen(slave_stats_temp_file, "w");
if (fp == NULL) if (fp == NULL)
return; return;
@@ -1052,7 +1050,8 @@ write_stats(krb5_context context, slave *slaves, uint32_t current_version)
rtbl_format(tbl, fp); rtbl_format(tbl, fp);
rtbl_destroy(tbl); rtbl_destroy(tbl);
fclose(fp); if (fclose(fp) != EOF)
rk_rename(slave_stats_temp_file, slave_stats_file);
} }
@@ -1151,6 +1150,8 @@ main(int argc, char **argv)
if (ret) if (ret)
krb5_err(context, 1, ret, "reading configuration files"); krb5_err(context, 1, ret, "reading configuration files");
init_stats_names(context);
time_before_gone = parse_time (slave_time_gone, "s"); time_before_gone = parse_time (slave_time_gone, "s");
if (time_before_gone < 0) if (time_before_gone < 0)
krb5_errx (context, 1, "couldn't parse time: %s", slave_time_gone); krb5_errx (context, 1, "couldn't parse time: %s", slave_time_gone);