From 0ee566dc669b0b40db7092344f9156602489445c Mon Sep 17 00:00:00 2001 From: Frank Ueberschar Date: Fri, 3 Jan 2020 09:46:33 +0100 Subject: [PATCH] Merge pull request #385 from WaryWolf/fix-job-creation-racecond (manual merge) copy value of seq to the stack to avoid race condition when generating Job names --- AUTHORS | 1 + core/src/dird/job.cc | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/AUTHORS b/AUTHORS index 1ecd31d74b1..f671d7b005f 100644 --- a/AUTHORS +++ b/AUTHORS @@ -19,6 +19,7 @@ Andreas Piesk Andreas Rogge Andreas Unterkircher Andrew Ford +Anthony Vaccaro Arno Lehmann Aron Schüler Attila Fülöp diff --git a/core/src/dird/job.cc b/core/src/dird/job.cc index baa687a7274..a6b7656dcb2 100644 --- a/core/src/dird/job.cc +++ b/core/src/dird/job.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2010 Free Software Foundation Europe e.V. Copyright (C) 2011-2016 Planets Communications B.V. - Copyright (C) 2013-2018 Bareos GmbH & Co. KG + Copyright (C) 2013-2020 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -1478,6 +1478,7 @@ void CreateUniqueJobName(JobControlRecord *jcr, const char *base_name) static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static time_t last_start_time = 0; static int seq = 0; + int lseq = 0; time_t now = time(NULL); char dt[MAX_TIME_LENGTH]; char name[MAX_NAME_LENGTH]; @@ -1496,6 +1497,7 @@ void CreateUniqueJobName(JobControlRecord *jcr, const char *base_name) now = time(NULL); } } + lseq = seq; last_start_time = now; V(mutex); /* allow creation of jobs */ jcr->start_time = now; @@ -1509,7 +1511,7 @@ void CreateUniqueJobName(JobControlRecord *jcr, const char *base_name) len = strlen(dt) + 5; /* dt + .%02d EOS */ bstrncpy(name, base_name, sizeof(name)); name[sizeof(name)-len] = 0; /* truncate if too long */ - Bsnprintf(jcr->Job, sizeof(jcr->Job), "%s.%s_%02d", name, dt, seq); /* add date & time */ + Bsnprintf(jcr->Job, sizeof(jcr->Job), "%s.%s_%02d", name, dt, lseq); /* add date & time */ /* Convert spaces into underscores */ for (p=jcr->Job; *p; p++) { if (*p == ' ') {