migrations: make libc check more resilient
Fixes #3661. There are a couple of cases in which ldd won't operate on an executable, for example when the executable is statically linked. This patch makes the call to ldd to ldd more resilient against errors, and looks for the system libc, instead of the libc go-ipfs was linked against. License: MIT Signed-off-by: Lars Gierth <larsg@systemli.org>
Lars Gierth committed
Feb 8, 2017 at 18:56 UTC
150fe9de76a425209399647dcce7ab15a0346163
1 file changed
+9
-7
repo/fsrepo/migrations/migrations.go
+9
-7
@@ -128,7 +128,7 @@ func migrationsVersion(bin string) (int, error) {
128
vs := strings.Trim(string(out), " \n\t")
129
vn, err := strconv.Atoi(vs)
130
if err != nil {
131
- return 0, fmt.Errorf("migrations binary version check did not return a number")
131
+ return 0, fmt.Errorf("migrations binary version check did not return a number: %s", err)
132
}
133
134
return vn, nil
@@ -253,17 +253,19 @@ func osWithVariant() (string, error) {
253
return "", fmt.Errorf("failed to resolve go-ipfs: %s", err)
254
}
255
256
- cmd := exec.Command("ldd", bin)
256
+ // ldd outputs the system's kind of libc
257
+ // - on standard ubuntu: ldd (Ubuntu GLIBC 2.23-0ubuntu5) 2.23
258
+ // - on alpine: musl libc (x86_64)
259
+ cmd := exec.Command("ldd --version", bin)
260
buf := new(bytes.Buffer)
261
cmd.Stdout = buf
259
- err = cmd.Run()
260
- if err != nil {
261
- return "", fmt.Errorf("failed to run ldd: %s", err)
262
- }
262
+ // we throw away the error, this code path must not fail because of
263
+ // a silly issue such as missing/broken ldd. we'll assume glibc in that case.
264
+ _ = cmd.Run()
265
266
scan := bufio.NewScanner(buf)
267
for scan.Scan() {
266
- if strings.Contains(scan.Text(), "libc") && strings.Contains(scan.Text(), "musl") {
268
+ if strings.Contains(scan.Text(), "musl") {
269
return "linux-musl", nil
270
}
271
}