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