Go应用程序在使用另一个用户的凭据启动子进程时出现挂起的情况。

huangapple go评论76阅读模式
英文:

Go application hangs when starting subprocess with another user's credentials

问题

我过去大约6个月一直在使用一个简单的游戏服务器管理应用程序,运行在Ubuntu 14.04上。最近一次服务器更新和重启后,当尝试启动子进程时,该应用程序会挂起。经过一些调试,我发现只要我尝试使用其他用户的凭据(我以root身份运行),任何命令都会挂起。

以下是一个简单的应用程序,演示了导致挂起的原因:

package main
import (
	"os/exec"
	"syscall"
	"fmt"
)

func main() {
	proc := exec.Command("ls")

	proc.SysProcAttr = &syscall.SysProcAttr{}
	proc.SysProcAttr.Credential = &syscall.Credential{Uid: 1022, Gid: 1023}
	err := proc.Run()
	if err != nil {
		fmt.Printf("err: %v", err)
	}
}

如果移除 syscall.Credential 部分,该应用程序将可以正常运行。

我的问题是:是否有某些特定的平台/更新原因导致了这种行为?现在是否不再是以另一个用户身份运行子进程的正确方式?

编辑:
以下是 strace -f 的最后5行输出:

[pid 3994] futex(0xc21000a888, FUTEX_WAKE, 1 <unfinished ...>
[pid 3995] <... futex resumed> ) = 0
[pid 3994] <... futex resumed> ) = 1
[pid 3995] futex(0xc21000a888, FUTEX_WAIT, 0, NULL <unfinished ...>
[pid 3994] select(0, NULL, NULL, NULL, {0, 20}) = 0 (Timeout)
[pid 3994] futex(0x7f615c51a4f8, FUTEX_WAIT, 0, NULL

所以,如果我理解正确的话,它在 futex_wait 处阻塞了。

英文:

I've been using a simple game server management application on Ubuntu 14.04 for the last 6 months or so. After a recent server update & reboot the application would hang on when trying to start a subprocess. After some debugging it seems that whenever I try to start a subprocess with another user's credentials (I'm running as a root) any command will hang.

Here's a simple application to demonstrate what causes the hang:

package main
import (
	&quot;os/exec&quot;
	&quot;syscall&quot;
	&quot;fmt&quot;
)

func main() {
	proc := exec.Command(&quot;ls&quot;)

	proc.SysProcAttr = &amp;syscall.SysProcAttr{}
	proc.SysProcAttr.Credential = &amp;syscall.Credential{Uid: 1022, Gid: 1023}
	err := proc.Run()
	if err != nil {
		fmt.Printf(&quot;err: %v&quot;, err)
	}
}

By removing the syscall.Credential part, the application will run without any issues.

My question is: is there some platform/update specific reason that causes this behaviour? Is this no longer a correct way to run a subprocess as another user?

EDIT:
Here's the last 5 lines of strace -f

[pid  3994] futex(0xc21000a888, FUTEX_WAKE, 1 &lt;unfinished ...&gt;
[pid  3995] &lt;... futex resumed&gt; )       = 0
[pid  3994] &lt;... futex resumed&gt; )       = 1
[pid  3995] futex(0xc21000a888, FUTEX_WAIT, 0, NULL &lt;unfinished ...&gt;
[pid  3994] select(0, NULL, NULL, NULL, {0, 20}) = 0 (Timeout)
[pid  3994] futex(0x7f615c51a4f8, FUTEX_WAIT, 0, NULL

So apparently if I'm interpreting this right it's blocking at futex_wait.

答案1

得分: 1

你应该使用strace来执行你的应用程序。所以运行strace myapp,看看它在哪里卡住了。可能是因为在你的应用程序执行之前,有其他的东西在分叉,导致它挂起。

英文:

You should execute your application with strace. So strace myapp and see where it locks up. It could be you have something else that's forking before your application executes, which is causing it to hang.

huangapple
  • 本文由 发表于 2016年2月2日 04:57:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/35140177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定