Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/mobile: os.Hostname() fails on Android 0 (possibly earlier version as well). #24701

halseth opened this issue Apr 5, 2018 · 10 comments


Copy link

@halseth halseth commented Apr 5, 2018

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.10.1 darwin/amd64

Does this issue reproduce with the latest release?


What operating system and processor architecture are you using (go env)?

GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/kq/3436m_v11sg0l7zqtmv2r1gw0000gn/T/go-build866177725=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Building a golang library for Android using gomobile bind:

"$GOPATH/bin/gomobile" bind -target=android -tags="android" -v -o "$android_dest" "$package"

When the library is, calling os.Hostname(), the call fails with error open /proc/sys/kernel/hostname: permission denied run on Android O (8.1.0). Also tested on Marshmallow (6.0) where the problem didn't occur.

EDIT: Exact messsage is

04-05 15:43:10.178 5585-5585/? W/Thread-4: type=1400 audit(0.0:4585): avc: denied { read } for name="hostname" dev="proc" ino=3489882 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:proc:s0 tclass=file permissive=0

Looks like access to /proc has been restricted?

What did you expect to see?

Call not failing.

What did you see instead?

App stopping with error

04-05 15:52:53.295 6990-6990/? W/Thread-4: type=1400 audit(0.0:4592): avc: denied { read } for name="hostname" dev="proc" ino=3515836 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:proc:s0 tclass=file permissive=0
04-05 15:52:53.313 1130-1570/? W/InputDispatcher: channel 'ee0d04c com.example.johan.lndtestapp2/com.example.johan.lndtestapp2.MainActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
04-05 15:52:53.313 1130-1570/? E/InputDispatcher: channel 'ee0d04c com.example.johan.lndtestapp2/com.example.johan.lndtestapp2.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
04-05 15:52:53.313 1130-2774/? I/ActivityManager: Process com.example.johan.lndtestapp2 (pid 6990) has died: fore TOP 
04-05 15:52:53.313 1130-1170/? W/zygote64: kill(-6990, 9) failed: No such process
04-05 15:52:53.313 1130-1170/? I/zygote64: Successfully killed process cgroup uid 10179 pid 6990 in 0ms
04-05 15:52:53.314 1130-2774/? W/ActivityManager: Force removing ActivityRecord{2ba77ef u0 com.example.johan.lndtestapp2/.MainActivity t1949}: app died, no saved state

EDIT: my library wasn't handling the error case properly, so it is not actually crashing, just that the call to os.Hostname() is failing.

@gopherbot gopherbot added this to the Unreleased milestone Apr 5, 2018
@gopherbot gopherbot added the mobile label Apr 5, 2018
Copy link

@bradfitz bradfitz commented Apr 5, 2018

It looks like 04e6ae6 was included in go1.10.1: 67b6956 and 8d90bb4 and 1188eb9

Maybe you need to recompile something?

/cc @eliasnaur @tklauser @zx2c4

Copy link

@eliasnaur eliasnaur commented Apr 6, 2018

On my Android O device and emulator, os.Hostname returns an error: open /proc/sys/kernel/hostname: permission denied. That's not a crash. Perhaps some code in your program depends on os.Hostname not to fail.

If you don't see a crash from the standard library either, please change this issue title to be about making os.Hostname not fail on Android O and later. I don't know how to do that, though.

@halseth halseth changed the title x/mobile: os.Hostname() calls crashes on Android 0 (possibly earlier version as well). x/mobile: os.Hostname() fails on Android 0 (possibly earlier version as well). Apr 6, 2018
Copy link

@halseth halseth commented Apr 6, 2018

@bradfitz Just tried reinstalling go from (go1.10.1 darwin/amd64), and compiling gomobile and gobind at commit golang/mobile@f161431 (newest commit that would actually build my project). Same issue.

Copy link

@halseth halseth commented Apr 6, 2018

@eliasnaur You are absolutely right! App was crashing because the go library was not handling the error properly. Updated title and description to reflect that.

Copy link

@eliasnaur eliasnaur commented Apr 6, 2018

Perhaps the __system_property_get function as mentioned in #20455 (comment) could be used as a replacement on GOOS=android.

Copy link

@bradfitz bradfitz commented Apr 6, 2018


Copy link

@gopherbot gopherbot commented Apr 30, 2018

Change mentions this issue: os: return fake hostname on Android to avoid being killed

@gopherbot gopherbot closed this in 3c7456c Apr 30, 2018
Copy link

@mdcnz mdcnz commented Apr 30, 2018

Apologies for commenting on a closed issue, but in the commit 3c7456c should there be a break after name = string(buf[:i]) ?

+	var buf [512]byte // Enough for a DNS name.
+	for i, b := range un.Nodename[:] {
+		buf[i] = uint8(b)
+		if b == 0 {
+			name = string(buf[:i])
+		}
+	}
Copy link

@gopherbot gopherbot commented Apr 30, 2018

Change mentions this issue: os: fix missing break bug in earlier CL 110295's use of Uname

Copy link

@bradfitz bradfitz commented Apr 30, 2018

@mdcnz, whoops. Fixed. Thanks!

gopherbot pushed a commit that referenced this issue May 1, 2018
The Uname name was never being used because it always generated a
too-long string.

The new test looking for zero bytes wouldn't have caught it (I thought
it would've), but is still nice to have.

Updates #24701

Change-Id: I2648074452609e4ad1b9736973e1b3a95eac658d
Reviewed-by: Ian Lance Taylor <>
@golang golang locked and limited conversation to collaborators Apr 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.