首页 > Go > daemon function for Go language(golang)(fixed for mac/darwin)

daemon function for Go language(golang)(fixed for mac/darwin)

/* ivan(a.t)mysqlab.net */

package main

import (
        "syscall"
        "os"
        "log"
)

func daemon(nochdir, noclose int) int {
        var ret, ret2 uintptr
        var err uintptr

        darwin := syscall.OS == "darwin"

        // already a daemon
        if syscall.Getppid() == 1 {
                return 0
        }

        // fork off the parent process
        ret, ret2, err = syscall.RawSyscall(syscall.SYS_FORK, 0, 0, 0)
        if err != 0 {
                return -1
        }

        // failure
        if ret2 < 0 {
                os.Exit(-1)
        }

        // handle exception for darwin
        if darwin && ret2 == 1 {
                ret = 0
        }

        // if we got a good PID, then we call exit the parent process.
        if ret > 0 {
                os.Exit(0)
        }

        /* Change the file mode mask */
        _ = syscall.Umask(0)

        // create a new SID for the child process
        s_ret, s_errno := syscall.Setsid()
        if s_errno != 0 {
                log.Printf("Error: syscall.Setsid errno: %d", s_errno)
        }
        if s_ret < 0 {
                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(fd, os.Stdin.Fd())
                        syscall.Dup2(fd, os.Stdout.Fd())
                        syscall.Dup2(fd, os.Stderr.Fd())
                }
        }

        return 0
}
// usage example: daemon(0, 0)
 

参考:http://code.google.com/p/go/issues/detail?id=227

Related posts:

  1. Go(golang) FastCGI Client
  2. Golang 实现的 mysql-proxy
分类: Go 标签: , ,
  1. 2012年1月4日11:29 | #1

    fixed for mac os/dawin
    bcoz syscall.RawSyscall(syscall.SYS_FORK, 0, 0, 0) return the same value for two process.

  2. 2012年3月25日17:15 | #2

    新建了个Go语言群,有兴趣的加
    群号:223643074

  1. 本文目前尚无任何 trackbacks 和 pingbacks.