f, e := os.OpenFile("/dev/null", os.O_RDWR, 0)
if e == nil {
fd := f.Fd()
syscall.Dup2(fd, os.Stdin.Fd())
}
--
来自: Golang-China ~ 中文Go语言技术邮件列表
详情: http://groups.google.com/group/golang-china
官网: http://golang-china.org/
IRC: irc.freenode.net #golang-china
@golangchina
f, e := os.OpenFile("/dev/null", os.O_RDWR, 0) if e == nil { fd := f.Fd() syscall.Dup2(fd, os.Stdin.Fd()) }
--
来自: Golang-China ~ 中文Go语言技术邮件列表
详情: http://groups.google.com/group/golang-china
官网: http://golang-china.org/
IRC: irc.freenode.net #golang-china
@golangchina
minux 给个从uintptr->int 的转化方案吧,2.14都可以直接转的
--
来自: Golang-China ~ 中文Go语言技术邮件列表
详情: http://groups.google.com/group/golang-china
官网: http://golang-china.org/
IRC: irc.freenode.net #golang-china
@golangchina
其实你没弄明白,楼主发这贴的目的,他的意思是,int(fd),在2.22号这个版本,是不能通过编译的,我试过r60.3, 2.22-weekly都是可以直接cast的
ret, _, err = syscall.Syscall(syscall.SYS_FORK, 0, 0, 0)
if err != 0 {
return -1
}
switch ret {
case 0:
break
default:
os.Exit(0)
}
pid,r:=syscall.Setsid()
if pid==-1 || r!=nil{
return -1
}
if nochdir == 0 {
os.Chdir("/")
}
if noclose == 0 {
f, e := os.OpenFile("/dev/null", os.O_RDWR, 0)
if e == nil {
fd := f.Fd()
syscall.Dup2(int(fd), int(os.Stdin.Fd()))
}
f, e = os.OpenFile("/var/log/shadaproxy-client", os.O_RDWR, 0)
if e == nil {
fd := f.Fd()
syscall.Dup2(int(fd), int(os.Stdout.Fd()))
syscall.Dup2(int(fd), int(os.Stderr.Fd()))
}
}
return 0
}
现在可以正常编译通过了,守护功能正常,但标准输出和错误输出重定向功能不行,到/var/log下看没有那个文件,noclose传的是0。
2-22可以强制转换,是我搞错了,不好意思。
func daemon(nochdir, noclose int) int {
var ret uintptr
var err syscall.Errno
ret, _, err = syscall.Syscall(syscall.SYS_FORK, 0, 0, 0)
if err != 0 {
return -1
}
switch ret {
case 0:
break
default:
os.Exit(0)
}
pid,r:=syscall.Setsid()
if pid==-1 || r!=nil{
return -1
}
if nochdir == 0 {
os.Chdir("/")
}
if noclose == 0 {
f, e := os.OpenFile("/dev/null", os.O_RDWR, 0)
if e == nil {syscall.Dup2(int(fd), int(os.Stdin.Fd()))
fd := f.Fd()
}
f, e = os.OpenFile("/var/log/shadaproxy-client", os.O_RDWR, 0)
if e == nil {syscall.Dup2(int(fd), int(os.Stdout.Fd()))
fd := f.Fd()
syscall.Dup2(int(fd), int(os.Stderr.Fd()))
}
}
return 0
}
--
把Syscall改成RawSyscall就正常了,但不知道为什么?好像和调度器有关系...