-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rcbridge: Set cache dir via XDG_CACHE_HOME
Due to how golang calls each packages' init() functions, it's impossible to have a shared library function that runs before any of the init()s. Instead, we'll set the XDG_CACHE_HOME environment variable. This has its own problems because calling setenv() from the Kotlin side only affects libc's global environ variable, which golang does not use (it maintains its own copy of envp). This commit works around that by importing a new envhack package that explicitly copies libc's environ to golang via CGO. Fixes: #11 Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
- Loading branch information
1 parent
2ef6188
commit e5069ea
Showing
4 changed files
with
53 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package envhack | ||
|
||
/* | ||
#include <unistd.h> | ||
*/ | ||
import "C" | ||
import ( | ||
"os" | ||
"strings" | ||
"unsafe" | ||
) | ||
|
||
func init() { | ||
// Golang has its own internal copy of the environment variables. When using | ||
// go as a shared library, the internal map never gets populated from | ||
// _start()'s envp, so no environment variables are accessible. This | ||
// terrible hack allows us to explicitly copy the environment variables from | ||
// libc. | ||
|
||
ptr := C.environ | ||
|
||
for { | ||
if *ptr == nil { | ||
break | ||
} | ||
|
||
key_value := C.GoString(*ptr) | ||
pieces := strings.SplitN(key_value, "=", 2) | ||
if len(pieces) != 2 { | ||
continue | ||
} | ||
|
||
os.Setenv(pieces[0], pieces[1]) | ||
|
||
ptr = (**C.char)(unsafe.Add(unsafe.Pointer(ptr), unsafe.Sizeof(ptr))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters