1
0
mirror of https://github.com/golang/go synced 2024-11-25 06:17:58 -07:00
This commit is contained in:
Adam Benhassen 2024-09-07 09:55:02 +02:00
parent cbc1cd81d8
commit 02fa1b3b25
7 changed files with 63 additions and 21 deletions

View File

@ -180,13 +180,3 @@ pthread_key_destructor(void* g) {
x_crosscall2_ptr(NULL, g, 0, 0);
}
}
// x_cgo_is_musl reports whether the C library is musl.
int
x_cgo_is_musl() {
#if defined(__GLIBC__) || defined(__UCLIBC__)
return 0;
#else
return 1;
#endif
}

View File

@ -65,3 +65,13 @@ threadentry(void *v)
crosscall1(ts.fn, setg_gcc, ts.g);
return nil;
}
// x_cgo_is_musl reports whether the C library is musl.
int
x_cgo_is_musl() {
#if defined(__GLIBC__) || defined(__UCLIBC__)
return 0;
#else
return 1;
#endif
}

View File

@ -90,3 +90,13 @@ threadentry(void *v)
crosscall1(ts.fn, setg_gcc, (void*)ts.g);
return nil;
}
// x_cgo_is_musl reports whether the C library is musl.
int
x_cgo_is_musl() {
#if defined(__GLIBC__) || defined(__UCLIBC__)
return 0;
#else
return 1;
#endif
}

View File

@ -86,3 +86,13 @@ x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
x_cgo_inittls(tlsg, tlsbase);
}
}
// x_cgo_is_musl reports whether the C library is musl.
int
x_cgo_is_musl() {
#if defined(__GLIBC__) || defined(__UCLIBC__)
return 0;
#else
return 1;
#endif
}

View File

@ -234,7 +234,7 @@ func sysargs(argc int32, argv **byte) {
n := argc + 1
// auxv on argv is not available on musl library/archive. avoid for musl.
if !((islibrary || isarchive) && isMusl()) {
if !((islibrary || isarchive) && GOOS == "linux" && isMusl()) {
// skip over argv, envp to get to auxv
for argv_index(argv, n) != nil {
n++

View File

@ -764,12 +764,18 @@ func getGodebugEarly() string {
// Similar to goenv_unix but extracts the environment value for
// GODEBUG directly.
// TODO(moehrmann): remove when general goenvs() can be called before cpuinit()
if (isarchive || islibrary) && isMusl() {
for _, value := range fetch_from_fd(procEnviron) {
if hasPrefix(value, prefix) {
// If the binary is an archive or a library, the operating system is Linux,
// and the system uses Musl, then read the environment variables from the
// /proc/self/environ file. Iterate over each null-terminated string read
// from the file. If any string has the specified prefix, return that string.
if (isarchive || islibrary) && GOOS == "linux" && isMusl() {
for _, value := range readNullTerminatedStringsFromFile(procEnviron) {
if stringslite.HasPrefix(value, prefix) {
return value
}
}
return env
}
n := int32(0)

View File

@ -82,8 +82,9 @@ func goargs() {
return
}
if (isarchive || islibrary) && isMusl() {
argslice = fetch_from_fd(procCmdline)
// musl-linux library: Read argv from /proc/self/cmdline instead
if (isarchive || islibrary) && GOOS == "linux" && isMusl() {
argslice = readNullTerminatedStringsFromFile(procCmdline)
return
}
@ -94,8 +95,9 @@ func goargs() {
}
func goenvs_unix() {
if (isarchive || islibrary) && isMusl() {
envs = fetch_from_fd(procEnviron)
// musl-linux library: Read envs from /proc/self/environ instead
if (isarchive || islibrary) && GOOS == "linux" && isMusl() {
envs = readNullTerminatedStringsFromFile(procEnviron)
return
}
@ -113,15 +115,29 @@ func goenvs_unix() {
}
}
func fetch_from_fd(path []byte) []string {
// readNullTerminatedStringsFromFile reads a file specified by the given path
// and returns a slice of strings. Each string in the slice is null-terminated
// in the file.
//
// Parameters:
// - path: A null-terminated byte slice representing the file path.
//
// Returns:
// - A slice of strings read from the file, where each string is null-terminated.
//
// It opens the file, reads its contents in chunks,
// and parses the data into a slice of strings based on null-termination.
//
// Note: This function will return nil if the file cannot be opened.
func readNullTerminatedStringsFromFile(path []byte) []string {
fd := open(&path[0], 0 /* O_RDONLY */, 0)
if fd <= 0 {
return nil
}
// Read the file in 16-byte chunks.
// Read the file.
var data []byte
var buf [16]byte
var buf [1024]byte
for {
n := read(fd, noescape(unsafe.Pointer(&buf[0])), int32(unsafe.Sizeof(buf)))
if n <= 0 { // EOF