blob: 652e30b3e4a2352bbcccbd8339359b8ece91182e [file] [log] [blame]
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001From 792693bb90768cfde4898e8dd31ee1b5de803d2f Mon Sep 17 00:00:00 2001
Brad Bishopd7bf8c12018-02-25 22:55:05 -05002From: Alexander Kanavin <alex.kanavin@gmail.com>
3Date: Thu, 8 Jun 2017 17:08:09 +0300
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08004Subject: [PATCH] build/pack.c: remove static local variables from buildHost()
5 and getBuildTime()
Brad Bishopd7bf8c12018-02-25 22:55:05 -05006
7Their use is causing difficult to diagnoze data races when building multiple
8packages in parallel, and is a bad idea in general, as it also makes it more
9difficult to reason about code.
10
11Upstream-Status: Submitted [https://github.com/rpm-software-management/rpm/pull/226]
12Signed-off-by: Alexander Kanavin <alex.kanavin@gmail.com>
13
Brad Bishopd7bf8c12018-02-25 22:55:05 -050014Signed-off-by: Alexander Kanavin <alex.kanavin@gmail.com>
Brad Bishop316dfdd2018-06-25 12:45:53 -040015
Brad Bishopd7bf8c12018-02-25 22:55:05 -050016---
17 build/build.c | 54 ++++++++++++++++++++++++++++--
18 build/pack.c | 84 +++++++++--------------------------------------
19 build/rpmbuild_internal.h | 8 +++--
20 3 files changed, 74 insertions(+), 72 deletions(-)
21
22diff --git a/build/build.c b/build/build.c
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080023index 13c3df2..b154f08 100644
Brad Bishopd7bf8c12018-02-25 22:55:05 -050024--- a/build/build.c
25+++ b/build/build.c
26@@ -6,6 +6,8 @@
27 #include "system.h"
28
29 #include <errno.h>
30+#include <netdb.h>
31+#include <time.h>
32 #include <sys/wait.h>
33
34 #include <rpm/rpmlog.h>
35@@ -16,6 +18,50 @@
36
37 #include "debug.h"
38
39+static rpm_time_t getBuildTime(void)
40+{
41+ rpm_time_t buildTime = 0;
42+ char *srcdate;
43+ time_t epoch;
44+ char *endptr;
45+
46+ srcdate = getenv("SOURCE_DATE_EPOCH");
47+ if (srcdate) {
48+ errno = 0;
49+ epoch = strtol(srcdate, &endptr, 10);
50+ if (srcdate == endptr || *endptr || errno != 0)
51+ rpmlog(RPMLOG_ERR, _("unable to parse SOURCE_DATE_EPOCH\n"));
52+ else
53+ buildTime = (int32_t) epoch;
54+ } else
55+ buildTime = (int32_t) time(NULL);
56+
57+ return buildTime;
58+}
59+
60+static char * buildHost(void)
61+{
62+ char* hostname;
63+ struct hostent *hbn;
64+ char *bhMacro;
65+
66+ bhMacro = rpmExpand("%{?_buildhost}", NULL);
67+ if (strcmp(bhMacro, "") != 0) {
68+ rasprintf(&hostname, "%s", bhMacro);
69+ } else {
70+ hostname = rcalloc(1024, sizeof(*hostname));
71+ (void) gethostname(hostname, 1024);
72+ hbn = gethostbyname(hostname);
73+ if (hbn)
74+ strcpy(hostname, hbn->h_name);
75+ else
76+ rpmlog(RPMLOG_WARNING,
77+ _("Could not canonicalize hostname: %s\n"), hostname);
78+ }
79+ free(bhMacro);
80+ return(hostname);
81+}
82+
83 /**
84 */
85 static rpmRC doRmSource(rpmSpec spec)
Brad Bishop316dfdd2018-06-25 12:45:53 -040086@@ -201,6 +247,9 @@ static rpmRC buildSpec(BTA_t buildArgs, rpmSpec spec, int what)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050087 rpmRC rc = RPMRC_OK;
88 int test = (what & RPMBUILD_NOBUILD);
89 char *cookie = buildArgs->cookie ? xstrdup(buildArgs->cookie) : NULL;
90+ const char* host = buildHost();
91+ rpm_time_t buildTime = getBuildTime();
92+
93
94 if (rpmExpandNumeric("%{?source_date_epoch_from_changelog}") &&
95 getenv("SOURCE_DATE_EPOCH") == NULL) {
Brad Bishop316dfdd2018-06-25 12:45:53 -040096@@ -269,11 +318,11 @@ static rpmRC buildSpec(BTA_t buildArgs, rpmSpec spec, int what)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050097 goto exit;
98
99 if (((what & RPMBUILD_PACKAGESOURCE) && !test) &&
100- (rc = packageSources(spec, &cookie)))
101+ (rc = packageSources(spec, &cookie, buildTime, host)))
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800102 goto exit;
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500103
104 if (((what & RPMBUILD_PACKAGEBINARY) && !test) &&
105- (rc = packageBinaries(spec, cookie, (didBuild == 0))))
106+ (rc = packageBinaries(spec, cookie, (didBuild == 0), buildTime, host)))
107 goto exit;
108
109 if ((what & RPMBUILD_CLEAN) &&
Brad Bishop316dfdd2018-06-25 12:45:53 -0400110@@ -293,6 +342,7 @@ static rpmRC buildSpec(BTA_t buildArgs, rpmSpec spec, int what)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500111 (void) unlink(spec->specFile);
112
113 exit:
114+ free(host);
115 free(cookie);
116 spec->rootDir = NULL;
117 if (rc != RPMRC_OK && rpmlogGetNrecs() > 0) {
118diff --git a/build/pack.c b/build/pack.c
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800119index df15876..17a4b09 100644
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500120--- a/build/pack.c
121+++ b/build/pack.c
122@@ -6,8 +6,6 @@
123 #include "system.h"
124
125 #include <errno.h>
126-#include <netdb.h>
127-#include <time.h>
128 #include <sys/wait.h>
129
130 #include <rpm/rpmlib.h> /* RPMSIGTAG*, rpmReadPackageFile */
Brad Bishop316dfdd2018-06-25 12:45:53 -0400131@@ -152,57 +150,6 @@ exit:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500132 return rc;
133 }
134
135-static rpm_time_t * getBuildTime(void)
136-{
137- static rpm_time_t buildTime[1];
138- char *srcdate;
139- time_t epoch;
140- char *endptr;
141-
142- if (buildTime[0] == 0) {
143- srcdate = getenv("SOURCE_DATE_EPOCH");
144- if (srcdate) {
145- errno = 0;
146- epoch = strtol(srcdate, &endptr, 10);
147- if (srcdate == endptr || *endptr || errno != 0)
148- rpmlog(RPMLOG_ERR, _("unable to parse SOURCE_DATE_EPOCH\n"));
149- else
150- buildTime[0] = (int32_t) epoch;
151- } else
152- buildTime[0] = (int32_t) time(NULL);
153- }
154-
155- return buildTime;
156-}
157-
158-static const char * buildHost(void)
159-{
160- static char hostname[1024];
161- static int oneshot = 0;
162- struct hostent *hbn;
163- char *bhMacro;
164-
165- if (! oneshot) {
166- bhMacro = rpmExpand("%{?_buildhost}", NULL);
167- if (strcmp(bhMacro, "") != 0 && strlen(bhMacro) < 1024) {
168- strcpy(hostname, bhMacro);
169- } else {
170- if (strcmp(bhMacro, "") != 0)
171- rpmlog(RPMLOG_WARNING, _("The _buildhost macro is too long\n"));
172- (void) gethostname(hostname, sizeof(hostname));
173- hbn = gethostbyname(hostname);
174- if (hbn)
175- strcpy(hostname, hbn->h_name);
176- else
177- rpmlog(RPMLOG_WARNING,
178- _("Could not canonicalize hostname: %s\n"), hostname);
179- }
180- free(bhMacro);
181- oneshot = 1;
182- }
183- return(hostname);
184-}
185-
186 static rpmRC processScriptFiles(rpmSpec spec, Package pkg)
187 {
188 struct TriggerFileEntry *p;
Brad Bishop316dfdd2018-06-25 12:45:53 -0400189@@ -476,7 +423,8 @@ exit:
190 * order to how the RPM format is laid on disk.
191 */
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500192 static rpmRC writeRPM(Package pkg, unsigned char ** pkgidp,
193- const char *fileName, char **cookie)
194+ const char *fileName, char **cookie,
195+ rpm_time_t buildTime, const char* buildHost)
196 {
197 FD_t fd = NULL;
198 char * rpmio_flags = NULL;
Brad Bishop316dfdd2018-06-25 12:45:53 -0400199@@ -500,7 +448,7 @@ static rpmRC writeRPM(Package pkg, unsigned char ** pkgidp,
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500200
201 /* Create and add the cookie */
202 if (cookie) {
203- rasprintf(cookie, "%s %d", buildHost(), (int) (*getBuildTime()));
204+ rasprintf(cookie, "%s %d", buildHost, buildTime);
205 headerPutString(pkg->header, RPMTAG_COOKIE, *cookie);
206 }
Brad Bishop316dfdd2018-06-25 12:45:53 -0400207
208@@ -641,7 +589,7 @@ static rpmRC checkPackages(char *pkgcheck)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500209 return RPMRC_OK;
210 }
211
212-static rpmRC packageBinary(rpmSpec spec, Package pkg, const char *cookie, int cheating, char** filename)
213+static rpmRC packageBinary(rpmSpec spec, Package pkg, const char *cookie, int cheating, char** filename, rpm_time_t buildTime, const char* buildHost)
214 {
215 const char *errorString;
216 rpmRC rc = RPMRC_OK;
Brad Bishop316dfdd2018-06-25 12:45:53 -0400217@@ -660,8 +608,8 @@ static rpmRC packageBinary(rpmSpec spec, Package pkg, const char *cookie, int ch
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500218 headerCopyTags(spec->packages->header, pkg->header, copyTags);
219
220 headerPutString(pkg->header, RPMTAG_RPMVERSION, VERSION);
221- headerPutString(pkg->header, RPMTAG_BUILDHOST, buildHost());
222- headerPutUint32(pkg->header, RPMTAG_BUILDTIME, getBuildTime(), 1);
223+ headerPutString(pkg->header, RPMTAG_BUILDHOST, buildHost);
224+ headerPutUint32(pkg->header, RPMTAG_BUILDTIME, &buildTime, 1);
225
226 if (spec->sourcePkgId != NULL) {
227 headerPutBin(pkg->header, RPMTAG_SOURCEPKGID, spec->sourcePkgId,16);
Brad Bishop316dfdd2018-06-25 12:45:53 -0400228@@ -699,7 +647,7 @@ static rpmRC packageBinary(rpmSpec spec, Package pkg, const char *cookie, int ch
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500229 free(binRpm);
230 }
231
232- rc = writeRPM(pkg, NULL, *filename, NULL);
233+ rc = writeRPM(pkg, NULL, *filename, NULL, buildTime, buildHost);
234 if (rc == RPMRC_OK) {
235 /* Do check each written package if enabled */
236 char *pkgcheck = rpmExpand("%{?_build_pkgcheck} ", *filename, NULL);
Brad Bishop316dfdd2018-06-25 12:45:53 -0400237@@ -719,7 +667,7 @@ struct binaryPackageTaskData
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500238 struct binaryPackageTaskData *next;
239 };
240
241-static struct binaryPackageTaskData* runBinaryPackageTasks(rpmSpec spec, const char *cookie, int cheating)
242+static struct binaryPackageTaskData* runBinaryPackageTasks(rpmSpec spec, const char *cookie, int cheating, rpm_time_t buildTime, char* buildHost)
243 {
244 struct binaryPackageTaskData *tasks = NULL;
245 struct binaryPackageTaskData *task = NULL;
Brad Bishop316dfdd2018-06-25 12:45:53 -0400246@@ -731,7 +679,7 @@ static struct binaryPackageTaskData* runBinaryPackageTasks(rpmSpec spec, const c
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500247 if (pkg == spec->packages) {
248 // the first package needs to be processed ahead of others, as they copy
249 // changelog data from it, and so otherwise data races would happen
250- task->result = packageBinary(spec, pkg, cookie, cheating, &(task->filename));
251+ task->result = packageBinary(spec, pkg, cookie, cheating, &(task->filename), buildTime, buildHost);
252 rpmlog(RPMLOG_NOTICE, _("Finished binary package job, result %d, filename %s\n"), task->result, task->filename);
253 tasks = task;
254 }
Brad Bishop316dfdd2018-06-25 12:45:53 -0400255@@ -748,7 +696,7 @@ static struct binaryPackageTaskData* runBinaryPackageTasks(rpmSpec spec, const c
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500256 if (task != tasks)
257 #pragma omp task
258 {
259- task->result = packageBinary(spec, task->pkg, cookie, cheating, &(task->filename));
260+ task->result = packageBinary(spec, task->pkg, cookie, cheating, &(task->filename), buildTime, buildHost);
261 rpmlog(RPMLOG_NOTICE, _("Finished binary package job, result %d, filename %s\n"), task->result, task->filename);
262 }
263 }
Brad Bishop316dfdd2018-06-25 12:45:53 -0400264@@ -766,11 +714,11 @@ static void freeBinaryPackageTasks(struct binaryPackageTaskData* tasks)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500265 }
266 }
267
268-rpmRC packageBinaries(rpmSpec spec, const char *cookie, int cheating)
269+rpmRC packageBinaries(rpmSpec spec, const char *cookie, int cheating, rpm_time_t buildTime, char* buildHost)
270 {
271 char *pkglist = NULL;
272
273- struct binaryPackageTaskData *tasks = runBinaryPackageTasks(spec, cookie, cheating);
274+ struct binaryPackageTaskData *tasks = runBinaryPackageTasks(spec, cookie, cheating, buildTime, buildHost);
275
276 for (struct binaryPackageTaskData *task = tasks; task != NULL; task = task->next) {
277 if (task->result == RPMRC_OK) {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400278@@ -797,7 +745,7 @@ rpmRC packageBinaries(rpmSpec spec, const char *cookie, int cheating)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500279 return RPMRC_OK;
280 }
281
282-rpmRC packageSources(rpmSpec spec, char **cookie)
283+rpmRC packageSources(rpmSpec spec, char **cookie, rpm_time_t buildTime, char* buildHost)
284 {
285 Package sourcePkg = spec->sourcePackage;
286 rpmRC rc;
Brad Bishop316dfdd2018-06-25 12:45:53 -0400287@@ -805,8 +753,8 @@ rpmRC packageSources(rpmSpec spec, char **cookie)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500288
289 /* Add some cruft */
290 headerPutString(sourcePkg->header, RPMTAG_RPMVERSION, VERSION);
291- headerPutString(sourcePkg->header, RPMTAG_BUILDHOST, buildHost());
292- headerPutUint32(sourcePkg->header, RPMTAG_BUILDTIME, getBuildTime(), 1);
293+ headerPutString(sourcePkg->header, RPMTAG_BUILDHOST, buildHost);
294+ headerPutUint32(sourcePkg->header, RPMTAG_BUILDTIME, &buildTime, 1);
Brad Bishop316dfdd2018-06-25 12:45:53 -0400295 headerPutUint32(sourcePkg->header, RPMTAG_SOURCEPACKAGE, &one, 1);
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500296
297 /* XXX this should be %_srpmdir */
Brad Bishop316dfdd2018-06-25 12:45:53 -0400298@@ -814,7 +762,7 @@ rpmRC packageSources(rpmSpec spec, char **cookie)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500299 char *pkgcheck = rpmExpand("%{?_build_pkgcheck_srpm} ", fn, NULL);
300
301 spec->sourcePkgId = NULL;
302- rc = writeRPM(sourcePkg, &spec->sourcePkgId, fn, cookie);
303+ rc = writeRPM(sourcePkg, &spec->sourcePkgId, fn, cookie, buildTime, buildHost);
304
305 /* Do check SRPM package if enabled */
306 if (rc == RPMRC_OK && pkgcheck[0] != ' ') {
307diff --git a/build/rpmbuild_internal.h b/build/rpmbuild_internal.h
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800308index 439b7d3..07e8338 100644
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500309--- a/build/rpmbuild_internal.h
310+++ b/build/rpmbuild_internal.h
Brad Bishop316dfdd2018-06-25 12:45:53 -0400311@@ -427,19 +427,23 @@ rpmRC processSourceFiles(rpmSpec spec, rpmBuildPkgFlags pkgFlags);
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500312 * @param spec spec file control structure
313 * @param cookie build identifier "cookie" or NULL
314 * @param cheating was build shortcircuited?
315+ * @param buildTime the build timestamp that goes into packages
316+ * @param buildHost the hostname where the build is happening
317 * @return RPMRC_OK on success
318 */
319 RPM_GNUC_INTERNAL
320-rpmRC packageBinaries(rpmSpec spec, const char *cookie, int cheating);
321+rpmRC packageBinaries(rpmSpec spec, const char *cookie, int cheating, rpm_time_t buildTime, char* buildHost);
322
323 /** \ingroup rpmbuild
324 * Generate source package.
325 * @param spec spec file control structure
326 * @retval cookie build identifier "cookie" or NULL
327+ * @param buildTime the build timestamp that goes into packages
328+ * @param buildHost the hostname where the build is happening
329 * @return RPMRC_OK on success
330 */
331 RPM_GNUC_INTERNAL
332-rpmRC packageSources(rpmSpec spec, char **cookie);
333+rpmRC packageSources(rpmSpec spec, char **cookie, rpm_time_t buildTime, char* buildHost);
334
335 RPM_GNUC_INTERNAL
336 int addLangTag(rpmSpec spec, Header h, rpmTagVal tag,