本文整理汇总了Golang中syscall.Getppid函数的典型用法代码示例。如果您正苦于以下问题:Golang Getppid函数的具体用法?Golang Getppid怎么用?Golang Getppid使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Getppid函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GetEnvs
// Convert and validate the GOAGAIN_FD, GOAGAIN_NAME, and GOAGAIN_PPID
// environment variables. If all three are present and in order, this
// is a child process that may pick up where the parent left off.
func GetEnvs() (l *net.TCPListener, ppid int, err error) {
var fd uintptr
_, err = fmt.Sscan(os.Getenv("GOAGAIN_FD"), &fd)
if nil != err {
return
}
var i net.Listener
i, err = net.FileListener(os.NewFile(fd, os.Getenv("GOAGAIN_NAME")))
if nil != err {
return
}
l = i.(*net.TCPListener)
if err = syscall.Close(int(fd)); nil != err {
return
}
_, err = fmt.Sscan(os.Getenv("GOAGAIN_PPID"), &ppid)
if nil != err {
return
}
if syscall.Getppid() != ppid {
err = errors.New(fmt.Sprintf(
"GOAGAIN_PPID is %d but parent is %d\n",
ppid,
syscall.Getppid(),
))
return
}
return
}
示例2: GetEnvs
// Convert and validate the GOAGAIN_FD and GOAGAIN_PPID environment
// variables. If both are present and in order, this is a child process
// that may pick up where the parent left off.
func GetEnvs() (*net.TCPListener, int, error) {
envFd := os.Getenv("GOAGAIN_FD")
if "" == envFd {
return nil, 0, errors.New("GOAGAIN_FD not set")
}
var fd uintptr
_, err := fmt.Sscan(envFd, &fd)
if nil != err {
return nil, 0, err
}
tmp, err := net.FileListener(os.NewFile(fd, "listener"))
if nil != err {
return nil, 0, err
}
l := tmp.(*net.TCPListener)
envPpid := os.Getenv("GOAGAIN_PPID")
if "" == envPpid {
return l, 0, errors.New("GOAGAIN_PPID not set")
}
var ppid int
_, err = fmt.Sscan(envPpid, &ppid)
if nil != err {
return l, 0, err
}
if syscall.Getppid() != ppid {
return l, ppid, errors.New(fmt.Sprintf(
"GOAGAIN_PPID is %d but parent is %d\n", ppid, syscall.Getppid()))
}
return l, ppid, nil
}
示例3: Run
func (srv *FoolServer) Run() { /*{{{*/
logger = NewLog(srv.config.AccessLog, srv.config.ErrorLog, srv.config.RunLog)
//解析模板
CompileTpl(srv.config.ViewPath)
//信号处理函数
go srv.signalHandle()
serverStat = STATE_RUNNING
//kill父进程
if isChild == true {
parent := syscall.Getppid()
if _, err := os.FindProcess(parent); err != nil {
return
}
logger.RunLog(fmt.Sprintf("[Notice] Killing parent pid: %v", parent))
syscall.Kill(parent, syscall.SIGQUIT)
}
srv.createPid(syscall.Getpid())
logger.RunLog("[Notice] Server start.")
//listen loop
srv.Serve(srv.listener)
logger.RunLog("[Notice] Waiting for connections to finish...")
connWg.Wait()
serverStat = STATE_TERMINATE
logger.RunLog("[Notice] Server shuttdown.")
return
} /*}}}*/
示例4: TestCreateToolhelp32Snapshot
func TestCreateToolhelp32Snapshot(t *testing.T) {
handle, err := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
if err != nil {
t.Fatal(err)
}
defer syscall.CloseHandle(syscall.Handle(handle))
// Iterate over the snapshots until our PID is found.
pid := uint32(syscall.Getpid())
for {
process, err := Process32Next(handle)
if errors.Cause(err) == syscall.ERROR_NO_MORE_FILES {
break
}
if err != nil {
t.Fatal(err)
}
t.Logf("CreateToolhelp32Snapshot: ProcessEntry32=%v", process)
if process.ProcessID == pid {
assert.EqualValues(t, syscall.Getppid(), process.ParentProcessID)
return
}
}
assert.Fail(t, "Snapshot not found for PID=%v", pid)
}
示例5: daemon
func daemon(nochdir, noclose int) int {
var ret, ret2 uintptr
var err syscall.Errno
darwin := runtime.GOOS == "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 != nil {
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(int(fd), int(os.Stdin.Fd()))
syscall.Dup2(int(fd), int(os.Stdout.Fd()))
syscall.Dup2(int(fd), int(os.Stderr.Fd()))
}
}
return 0
}
示例6: start
func (t *trTransceiver) start(gracefulProtos string) error {
t.startWorkers()
t.startFlushers()
t.startStatWorker()
t.serviceMgr = newServiceManager(t)
if err := t.serviceMgr.run(gracefulProtos); err != nil {
return err
}
// Wait for workers/flushers to start correctly
t.startWg.Wait()
log.Printf("All workers running, good to go!")
if gracefulProtos != "" {
parent := syscall.Getppid()
log.Printf("start(): Killing parent pid: %v", parent)
syscall.Kill(parent, syscall.SIGTERM)
log.Printf("start(): Waiting for the parent to signal that flush is complete...")
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGUSR1)
s := <-ch
log.Printf("start(): Received %v, proceeding to load the data", s)
}
t.dss.reload() // *finally* load the data (because graceful restart)
go t.dispatcher() // now start dispatcher
return nil
}
示例7: RestoreParentDeathSignal
// RestoreParentDeathSignal sets the parent death signal to old.
func RestoreParentDeathSignal(old int) error {
if old == 0 {
return nil
}
current, err := system.GetParentDeathSignal()
if err != nil {
return fmt.Errorf("get parent death signal %s", err)
}
if old == current {
return nil
}
if err := system.ParentDeathSignal(uintptr(old)); err != nil {
return fmt.Errorf("set parent death signal %s", err)
}
// Signal self if parent is already dead. Does nothing if running in a new
// PID namespace, as Getppid will always return 0.
if syscall.Getppid() == 1 {
return syscall.Kill(syscall.Getpid(), syscall.SIGKILL)
}
return nil
}
示例8: SetDaemonMode
func (this torUtil) SetDaemonMode(nochdir, noclose int) int {
if syscall.Getppid() == 1 {
return 0
}
ret, ret2, err := syscall.RawSyscall(syscall.SYS_FORK, 0, 0, 0)
if err != 0 {
return -1
}
if ret2 < 0 {
os.Exit(-1)
}
if ret > 0 {
os.Exit(0)
}
syscall.Umask(0)
s_ret, s_errno := syscall.Setsid()
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(int(fd), int(os.Stdin.Fd()))
syscall.Dup2(int(fd), int(os.Stdout.Fd()))
syscall.Dup2(int(fd), int(os.Stderr.Fd()))
}
}
return 0
}
示例9: childReady
// blocks on the server ready and when ready, it sends
// a signal to the parent so that it knows it cna now exit
func childReady(srv *falcore.Server) {
pid := syscall.Getpid()
// wait for the ready signal
<-srv.AcceptReady
// grab the parent and send a signal that the child is ready
parent := syscall.Getppid()
fmt.Printf("%v Kill parent %v with SIGUSR1\n", pid, parent)
syscall.Kill(parent, syscall.SIGUSR1)
}
示例10: start
func start(c *cli.Context) {
if pidfile != "" {
pid, err := fileOrPid(pidfile)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
if pid != 0 && pid != syscall.Getppid() {
fmt.Fprintf(os.Stderr, "There is already a pidfile at '%s' that appears to belong to another apiplexy instance.")
os.Exit(1)
}
}
if configPath == "" {
configPath = c.String("config")
}
ap, config, err := initApiplex(configPath)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
server := &http.Server{
Addr: "0.0.0.0:" + strconv.Itoa(config.Serve.Port),
Handler: ap,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 16,
}
fmt.Printf("Launching apiplexy on port %d.\n", config.Serve.Port)
l, err := net.Listen("tcp", server.Addr)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't start server on %s: %s\n", server.Addr, err.Error())
os.Exit(1)
}
// write pidfile and wait for restart signal
if pidfile != "" {
ioutil.WriteFile(pidfile, []byte(strconv.Itoa(syscall.Getpid())), 0600)
}
defer func() {
// server shuts down, delete pidfile if it's still our PID in there
if pidfile != "" {
rp, _ := ioutil.ReadFile(pidfile)
p, _ := strconv.Atoi(string(rp))
if p == syscall.Getpid() {
os.Remove(pidfile)
}
}
}()
server.Serve(l)
}
示例11: NewDefaultFormatter
func NewDefaultFormatter(out io.Writer) Formatter {
if syscall.Getppid() == 1 {
// We're running under init, which may be systemd.
f, err := NewJournaldFormatter()
if err == nil {
return f
}
}
return NewPrettyFormatter(out, false)
}
示例12: _not_work_Fork
func _not_work_Fork(closeno bool) (pid int, err error) {
// don't run this
// not work with go threads
// see: http://code.google.com/p/go/issues/detail?id=227
darwin := runtime.GOOS == "darwin"
// already a daemon
if syscall.Getppid() == 1 {
return 0, nil
}
// fork off the parent process
ret, ret2, errno := syscall.RawSyscall(syscall.SYS_FORK, 0, 0, 0)
if errno != 0 {
return -1, fmt.Errorf("Fork failure: %s", errno)
}
// failure
if ret2 < 0 {
return -1, fmt.Errorf("Fork failure")
}
// 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 {
return 0, nil
}
// create a new SID for the child process
s_ret, s_errno := syscall.Setsid()
if s_errno != nil {
return -1, fmt.Errorf("Error: syscall.Setsid: %s", s_errno)
}
if s_ret < 0 {
return -1, fmt.Errorf("Error: syscall.Setsid: %s", s_errno)
}
if closeno {
f, e := os.OpenFile("/dev/null", os.O_RDWR, 0)
if e == nil {
fd := int(f.Fd())
syscall.Dup2(fd, int(os.Stdin.Fd()))
syscall.Dup2(fd, int(os.Stdout.Fd()))
syscall.Dup2(fd, int(os.Stderr.Fd()))
}
}
return os.Getpid(), nil
}
示例13: main
func main() {
conf := flag.String("conf", "./conf/pmon.json", "config file")
var gracefulChild bool
flag.BoolVar(&gracefulChild, "graceful", false, "listen on fd open 3 (internal use only)")
flag.Parse()
defer glog.Flush()
var err error
confPath, err = filepath.Abs(*conf)
if nil != err {
glog.Errorf("%v", err)
return
}
// sc := make(chan os.Signal, 1)
// signal.Notify(sc, os.Interrupt, os.Kill)
// go func() {
// _ = <-sc
// killAll(&LogWriter{})
// glog.Flush()
// os.Exit(1)
// }()
watchConfFile()
//start admin server
var l net.Listener
if gracefulChild {
f := os.NewFile(3, "")
l, err = net.FileListener(f)
} else {
l, err = net.Listen("tcp", Cfg.Listen)
}
if nil != err {
glog.Errorf("Bind socket failed:%v", err)
return
}
tl := l.(*net.TCPListener)
listenFile, _ = tl.File()
if gracefulChild {
parent := syscall.Getppid()
glog.Infof("main: Killing parent pid: %v", parent)
if proc, _ := os.FindProcess(parent); nil != proc {
proc.Signal(syscall.SIGTERM)
}
//syscall.Kill(parent, syscall.SIGTERM)
}
for {
c, _ := l.Accept()
if nil != c {
processAdminConn(c) //only ONE admin connection allowd
}
}
}
示例14: GetEnvs
// Convert and validate the GOAGAIN_FD, GOAGAIN_NAME, and GOAGAIN_PPID
// environment variables. If all three are present and in order, this
// is a child process that may pick up where the parent left off.
func GetEnvs() (l net.Listener, ppid int, err error) {
var fd uintptr
_, err = fmt.Sscan(os.Getenv("GOAGAIN_FD"), &fd)
if nil != err {
return
}
var i net.Listener
i, err = net.FileListener(os.NewFile(fd, os.Getenv("GOAGAIN_NAME")))
if nil != err {
return
}
switch i.(type) {
case *net.TCPListener:
l = i.(*net.TCPListener)
case *net.UnixListener:
l = i.(*net.UnixListener)
default:
err = errors.New(fmt.Sprintf(
"file descriptor is %T not *net.TCPListener or *net.UnixListener",
i,
))
return
}
if err = syscall.Close(int(fd)); nil != err {
return
}
_, err = fmt.Sscan(os.Getenv("GOAGAIN_PPID"), &ppid)
if nil != err {
return
}
if syscall.Getppid() != ppid {
err = errors.New(fmt.Sprintf(
"GOAGAIN_PPID is %d but parent is %d",
ppid,
syscall.Getppid(),
))
return
}
return
}
示例15: daemon
func daemon() error {
isDarwin := runtime.GOOS == "darwin"
// already a daemon
if syscall.Getppid() == 1 {
return nil
}
// fork off the parent process
ret, ret2, errno := syscall.RawSyscall(syscall.SYS_FORK, 0, 0, 0)
if errno != 0 {
return errors.New(fmt.Sprintf("fork error! [errno=%d]", errno))
}
// failure
if ret2 < 0 {
os.Exit(1)
}
// handle exception for darwin
if isDarwin && ret2 == 1 {
ret = 0
}
// if we got a good PID, then we call exit the parent process.
if int(ret) > 0 {
os.Exit(0)
}
syscall.Umask(0)
// create a new SID for the child process
_, err := syscall.Setsid()
if err != nil {
return err
}
//os.Chdir("/")
f, err := os.OpenFile("/dev/null", os.O_RDWR, 0)
if err == nil {
fd := f.Fd()
syscall.Dup2(int(fd), int(os.Stdin.Fd()))
syscall.Dup2(int(fd), int(os.Stdout.Fd()))
syscall.Dup2(int(fd), int(os.Stderr.Fd()))
}
return nil
}