github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/libkb/flock.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package libkb
     5  
     6  import (
     7  	"os"
     8  )
     9  
    10  type LockPIDFile struct {
    11  	Contextified
    12  	name string
    13  	file *os.File
    14  }
    15  
    16  func NewLockPIDFile(g *GlobalContext, s string) *LockPIDFile {
    17  	return &LockPIDFile{Contextified: NewContextified(g), name: s}
    18  }
    19  
    20  func (f *LockPIDFile) Close() (err error) {
    21  	if f.file != nil {
    22  		if e1 := f.file.Close(); e1 != nil {
    23  			f.G().Log.Warning("Error closing pid file: %s\n", e1)
    24  		}
    25  		f.G().Log.Debug("Cleaning up pidfile %s", f.name)
    26  		if err = os.Remove(f.name); err != nil {
    27  			f.G().Log.Warning("Error removing pidfile: %s\n", err)
    28  		}
    29  	}
    30  	return
    31  }