github.com/feiyang21687/docker@v1.5.0/utils/daemon.go (about)

     1  package utils
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"log"
     7  	"os"
     8  	"strconv"
     9  )
    10  
    11  func CreatePidFile(pidfile string) error {
    12  	if pidString, err := ioutil.ReadFile(pidfile); err == nil {
    13  		pid, err := strconv.Atoi(string(pidString))
    14  		if err == nil {
    15  			if _, err := os.Stat(fmt.Sprintf("/proc/%d/", pid)); err == nil {
    16  				return fmt.Errorf("pid file found, ensure docker is not running or delete %s", pidfile)
    17  			}
    18  		}
    19  	}
    20  
    21  	file, err := os.Create(pidfile)
    22  	if err != nil {
    23  		return err
    24  	}
    25  
    26  	defer file.Close()
    27  
    28  	_, err = fmt.Fprintf(file, "%d", os.Getpid())
    29  	return err
    30  }
    31  
    32  func RemovePidFile(pidfile string) {
    33  	if err := os.Remove(pidfile); err != nil {
    34  		log.Printf("Error removing %s: %s", pidfile, err)
    35  	}
    36  }