Permalink
Cannot retrieve contributors at this time
Fetching contributors…
| // Copyright 2016 Canonical Ltd. | |
| // Licensed under the LGPLv3, see LICENCE file for details. | |
| // +build !windows,!linux | |
| package mutex | |
| import ( | |
| "os" | |
| "syscall" | |
| "github.com/juju/errors" | |
| ) | |
| type impl struct { | |
| fd int | |
| } | |
| func acquire(name string) (Releaser, error) { | |
| fd, err := syscall.Open(flockName(name), syscall.O_CREAT|syscall.O_RDONLY, 0600) | |
| if err != nil { | |
| return nil, errors.Trace(err) | |
| } | |
| err = syscall.Flock(fd, syscall.LOCK_EX|syscall.LOCK_NB) | |
| if err != nil { | |
| syscall.Close(fd) | |
| if err == syscall.EWOULDBLOCK { | |
| return nil, errors.Trace(ErrLocked) | |
| } | |
| return nil, errors.Trace(err) | |
| } | |
| return &impl{fd: fd}, nil | |
| } | |
| // Release implements Releaser. | |
| func (i *impl) Release() error { | |
| return syscall.Close(i.fd) | |
| } | |
| func flockName(name string) string { | |
| currentUser := os.Getenv("USER") | |
| if currentUser == "" { | |
| currentUser = "client" | |
| } | |
| return "/tmp/juju-" + currentUser + "-" + name | |
| } |