pax_global_header00006660000000000000000000000064137754343570014534gustar00rootroot0000000000000052 comment=ab128e95c492bc7d5a4c8ee11c0e80bff375e37b go-singleinstance-1.0.0/000077500000000000000000000000001377543435700151435ustar00rootroot00000000000000go-singleinstance-1.0.0/.gitignore000066400000000000000000000004121377543435700171300ustar00rootroot00000000000000# Compiled Object files, Static and Dynamic libs (Shared Objects) *.o *.a *.so # Folders _obj _test # Architecture specific extensions/prefixes *.[568vq] [568vq].out *.cgo1.go *.cgo2.c _cgo_defun.c _cgo_gotypes.go _cgo_export.* _testmain.go *.exe *.test *.prof go-singleinstance-1.0.0/LICENSE000066400000000000000000000020671377543435700161550ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015 Allan Simon Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. go-singleinstance-1.0.0/README.md000066400000000000000000000017331377543435700164260ustar00rootroot00000000000000# go-singleinstance Cross plateform library to have only one instance of a software (based on python's [tendo](https://github.com/pycontribs/tendo/blob/master/tendo/singleton.py)). ## Usage ```go package main import ( "fmt" "time" "github.com/allan-simon/go-singleinstance" ) func main() { lockFile, err := singleinstance.CreateLockFile("plop.lock") if err != nil { fmt.Println("An instance already exists") return } defer lockFile.Close() fmt.Println("Sleeping...") time.Sleep(10 * time.Second) fmt.Println("Done") } ``` If you try to launch it twice, the second instance will fail. ## Thanks For the python library trendo, from which I've shamelessly adapted the code. ## Contribution Don't be afraid if it says "last commit 2 years ago", this library is made to be small and simple so it's unlikely it changes after some times, however I'm pretty reactive on github overall, so feel free to use issues to ask question, propose patch etc. :) ## License MIT go-singleinstance-1.0.0/go.mod000066400000000000000000000000421377543435700162450ustar00rootroot00000000000000module go-singleinstance go 1.15 go-singleinstance-1.0.0/lock.go000066400000000000000000000005001377543435700164150ustar00rootroot00000000000000package singleinstance import ( "io/ioutil" "strconv" ) // If filename is a lock file, returns the PID of the process locking it func GetLockFilePid(filename string) (pid int, err error) { contents, err := ioutil.ReadFile(filename) if err != nil { return } pid, err = strconv.Atoi(string(contents)) return } go-singleinstance-1.0.0/lock_example.go000066400000000000000000000010001377543435700201240ustar00rootroot00000000000000// +build ignore package main import ( "fmt" "time" "github.com/allan-simon/go-singleinstance" ) func main() { filename := "plop.lock" _, err := singleinstance.CreateLockFile(filename) if err != nil { fmt.Println("An instance already exists") pid, err := singleinstance.GetLockFilePid(filename) if err != nil { fmt.Println("Cannot get PID:", err) return } fmt.Println("Locking PID:", pid) return } fmt.Println("Sleeping...") time.Sleep(30 * time.Second) fmt.Println("Done") } go-singleinstance-1.0.0/lock_posix.go000066400000000000000000000014251377543435700176460ustar00rootroot00000000000000// +build !windows package singleinstance import ( "os" "strconv" "syscall" ) // CreateLockFile tries to create a file with given name and acquire an // exclusive lock on it. If the file already exists AND is still locked, it will // fail. func CreateLockFile(filename string) (*os.File, error) { file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0600) if err != nil { return nil, err } err = syscall.Flock(int(file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB) if err != nil { file.Close() return nil, err } // Write PID to lock file contents := strconv.Itoa(os.Getpid()) if err := file.Truncate(0); err != nil { file.Close() return nil, err } if _, err := file.WriteString(contents); err != nil { file.Close() return nil, err } return file, nil } go-singleinstance-1.0.0/lock_test.go000066400000000000000000000021031377543435700174550ustar00rootroot00000000000000package singleinstance import ( "io/ioutil" "os" "testing" ) func createTestLock(t *testing.T) *os.File { tmpfile, err := ioutil.TempFile("", "singleinstance") if err != nil { t.Fatal("Cannot create temporary file:", err) } tmpfile.Close() f, err := CreateLockFile(tmpfile.Name()) if err != nil { t.Fatal("Expected no error while creating lock, got:", err) } return f } func TestCreateLockFile(t *testing.T) { f := createTestLock(t) defer os.Remove(f.Name()) _, err := CreateLockFile(f.Name()) if err == nil { t.Fatal("Expected an error while trying to lock, got:", err) } f.Close() // Remove the lock f, err = CreateLockFile(f.Name()) if err != nil { t.Fatal("Expected no error while trying to lock, got:", err) } f.Close() } func TestGetLockFilePid(t *testing.T) { f := createTestLock(t) defer os.Remove(f.Name()) defer f.Close() pid, err := GetLockFilePid(f.Name()) if err != nil { t.Fatal("Expected no error while getting PID, got:", err) } if pid != os.Getpid() { t.Errorf("Invalid PID: expected %v but got %v", os.Getpid(), pid) } } go-singleinstance-1.0.0/lock_windows.go000066400000000000000000000014001377543435700201670ustar00rootroot00000000000000// +build windows package singleinstance import ( "os" "strconv" ) // CreateLockFile tries to create a file with given name and acquire an // exclusive lock on it. If the file already exists AND is still locked, it will // fail. func CreateLockFile(filename string) (*os.File, error) { if _, err := os.Stat(filename); err == nil { // If the files exists, we first try to remove it if err = os.Remove(filename); err != nil { return nil, err } } else if !os.IsNotExist(err) { return nil, err } file, err := os.OpenFile(filename, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600) if err != nil { return nil, err } // Write PID to lock file _, err = file.WriteString(strconv.Itoa(os.Getpid())) if err != nil { return nil, err } return file, nil }