Skip to content

Commit

Permalink
Merge pull request #385 from WaryWolf/fix-job-creation-racecond
Browse files Browse the repository at this point in the history
(manual merge)
copy value of seq to the stack to avoid race condition when generating Job names
  • Loading branch information
franku committed Jan 3, 2020
1 parent 68c99eb commit 0ee566d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -19,6 +19,7 @@ Andreas Piesk
Andreas Rogge
Andreas Unterkircher
Andrew Ford
Anthony Vaccaro
Arno Lehmann
Aron Schüler
Attila Fülöp
Expand Down
6 changes: 4 additions & 2 deletions core/src/dird/job.cc
Expand Up @@ -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
Expand Down Expand Up @@ -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];
Expand All @@ -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;
Expand All @@ -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 == ' ') {
Expand Down

0 comments on commit 0ee566d

Please sign in to comment.