github.com/m3db/m3@v1.5.0/src/dbnode/server/lockfile.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package server
    22  
    23  import (
    24  	"os"
    25  	paths "path"
    26  
    27  	"github.com/pkg/errors"
    28  	"golang.org/x/sys/unix"
    29  )
    30  
    31  // lockfile represents an acquired lockfile.
    32  type lockfile struct {
    33  	file os.File
    34  }
    35  
    36  // acquireLockfile creates the given file path if it doesn't exist and
    37  // obtains an exclusive lock on it. An error is returned if the lock
    38  // has been obtained by another process.
    39  func acquireLockfile(path string) (*lockfile, error) {
    40  	file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0666)
    41  	if err != nil {
    42  		return nil, errors.Wrap(err, "failed opening lock path")
    43  	}
    44  
    45  	ft := &unix.Flock_t{
    46  		Pid:  int32(os.Getpid()),
    47  		Type: unix.F_WRLCK,
    48  	}
    49  
    50  	if err = unix.FcntlFlock(file.Fd(), unix.F_SETLK, ft); err != nil {
    51  		return nil, errors.Wrap(err, "failed obtaining lock")
    52  	}
    53  
    54  	lf := lockfile{*file}
    55  
    56  	return &lf, nil
    57  }
    58  
    59  // createAndAcquireLockfile creates any non-existing directories needed to
    60  // create the lock file, then acquires a lock on it.
    61  func createAndAcquireLockfile(path string, newDirMode os.FileMode) (*lockfile, error) {
    62  	if err := os.MkdirAll(paths.Dir(path), newDirMode); err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	return acquireLockfile(path)
    67  }
    68  
    69  // releaseLockfile releases the lock on the file and removes the file.
    70  func (lf lockfile) releaseLockfile() error {
    71  	ft := &unix.Flock_t{
    72  		Pid:  int32(os.Getpid()),
    73  		Type: unix.F_UNLCK,
    74  	}
    75  
    76  	if err := unix.FcntlFlock(lf.file.Fd(), unix.F_SETLK, ft); err != nil {
    77  		return errors.Wrap(err, "failed releasing lock")
    78  	}
    79  
    80  	if err := lf.file.Close(); err != nil {
    81  		return errors.Wrap(err, "failed closing lock file descriptor")
    82  	}
    83  
    84  	if err := os.Remove(lf.file.Name()); err != nil {
    85  		return errors.Wrap(err, "failed removing lock file")
    86  	}
    87  
    88  	return nil
    89  }