aboutsummaryrefslogtreecommitdiffstats
path: root/meta/recipes-devtools/rpm/files/0004-build-pack.c-remove-static-local-variables-from-buil.patch
blob: 9d43c3767b1731f628c293edbe684509fa2b08c4 (plain)
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
From 869d60f6cf68c2c83cc799f051c064e762ae9814 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 3/3] 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>

---
 build/pack.c | 92 ++++++++++++++++++++++++++++++------------------------------
 1 file changed, 46 insertions(+), 46 deletions(-)

diff --git a/build/pack.c b/build/pack.c
index f85d8dc90..12b1ceb76 100644
--- a/build/pack.c
+++ b/build/pack.c
@@ -151,54 +151,47 @@ exit:
     return rc;
 }
 
-static rpm_time_t * getBuildTime(void)
+static rpm_time_t getBuildTime(void)
 {
-    static rpm_time_t buildTime[1];
+    rpm_time_t buildTime = 0;
     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);
-    }
+    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 const char * buildHost(void)
+static char * buildHost(void)
 {
-    static char hostname[1024];
-    static int oneshot = 0;
+    char* hostname;
     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;
-    }
+    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);
 }
 
@@ -308,7 +301,8 @@ static int haveRichDep(Package pkg)
 }
 
 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;
@@ -397,7 +391,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);
     }
     
@@ -546,7 +540,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;
@@ -565,8 +559,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);
@@ -604,7 +598,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);
@@ -629,6 +623,8 @@ static struct binaryPackageTaskData* runBinaryPackageTasks(rpmSpec spec, const c
     struct binaryPackageTaskData *tasks = NULL;
     struct binaryPackageTaskData *task = NULL;
     struct binaryPackageTaskData *prev = NULL;
+    rpm_time_t buildTime = getBuildTime();
+    char *host = buildHost();
 
     for (Package pkg = spec->packages; pkg != NULL; pkg = pkg->next) {
         task = rcalloc(1, sizeof(*task));
@@ -636,7 +632,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;
         }
@@ -652,11 +648,12 @@ 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);
         }
     }
 
+    free(host);
     return tasks;
 }
 
@@ -707,16 +704,18 @@ rpmRC packageSources(rpmSpec spec, char **cookie)
     rpmRC rc;
 
     /* Add some cruft */
+    rpm_time_t buildTime = getBuildTime();
+    char* host = buildHost();
     headerPutString(sourcePkg->header, RPMTAG_RPMVERSION, VERSION);
-    headerPutString(sourcePkg->header, RPMTAG_BUILDHOST, buildHost());
-    headerPutUint32(sourcePkg->header, RPMTAG_BUILDTIME, getBuildTime(), 1);
+    headerPutString(sourcePkg->header, RPMTAG_BUILDHOST, (const char*)buildHost);
+    headerPutUint32(sourcePkg->header, RPMTAG_BUILDTIME, &buildTime, 1);
 
     /* XXX this should be %_srpmdir */
     {	char *fn = rpmGetPath("%{_srcrpmdir}/", spec->sourceRpmName,NULL);
 	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, host);
 
 	/* Do check SRPM package if enabled */
 	if (rc == RPMRC_OK && pkgcheck[0] != ' ') {
@@ -726,5 +725,6 @@ rpmRC packageSources(rpmSpec spec, char **cookie)
 	free(pkgcheck);
 	free(fn);
     }
+    free(host);
     return rc;
 }
-- 
2.11.0