| 1 |
/* |
|---|
| 2 |
* Copyright (c) 2010, OmniTI Computer Consulting, Inc. |
|---|
| 3 |
* All rights reserved. |
|---|
| 4 |
* |
|---|
| 5 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 6 |
* modification, are permitted provided that the following conditions are |
|---|
| 7 |
* met: |
|---|
| 8 |
* |
|---|
| 9 |
* * Redistributions of source code must retain the above copyright |
|---|
| 10 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 11 |
* * Redistributions in binary form must reproduce the above |
|---|
| 12 |
* copyright notice, this list of conditions and the following |
|---|
| 13 |
* disclaimer in the documentation and/or other materials provided |
|---|
| 14 |
* with the distribution. |
|---|
| 15 |
* * Neither the name OmniTI Computer Consulting, Inc. nor the names |
|---|
| 16 |
* of its contributors may be used to endorse or promote products |
|---|
| 17 |
* derived from this software without specific prior written |
|---|
| 18 |
* permission. |
|---|
| 19 |
* |
|---|
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 24 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 31 |
*/ |
|---|
| 32 |
|
|---|
| 33 |
#include "noit_config.h" |
|---|
| 34 |
#include "utils/noit_lockfile.h" |
|---|
| 35 |
#include <unistd.h> |
|---|
| 36 |
#include <fcntl.h> |
|---|
| 37 |
#include <errno.h> |
|---|
| 38 |
|
|---|
| 39 |
noit_lockfile_t |
|---|
| 40 |
noit_lockfile_acquire(const char *fp) { |
|---|
| 41 |
noit_lockfile_t fd; |
|---|
| 42 |
struct flock fl; |
|---|
| 43 |
int frv; |
|---|
| 44 |
|
|---|
| 45 |
fd = open(fp, O_RDWR|O_CREAT, 0600); |
|---|
| 46 |
if(fd < 0) return -1; |
|---|
| 47 |
|
|---|
| 48 |
memset(&fl, 0, sizeof(fl)); |
|---|
| 49 |
fl.l_type = F_WRLCK; |
|---|
| 50 |
fl.l_whence = SEEK_SET; |
|---|
| 51 |
fl.l_start = 0; |
|---|
| 52 |
fl.l_len = 0; |
|---|
| 53 |
|
|---|
| 54 |
while ((frv = fcntl(fd, F_SETLK, &fl)) == -1 && errno == EINTR); |
|---|
| 55 |
if(frv < 0) { |
|---|
| 56 |
close(fd); |
|---|
| 57 |
return -1; |
|---|
| 58 |
} |
|---|
| 59 |
return fd; |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
int |
|---|
| 63 |
noit_lockfile_release(noit_lockfile_t fd) { |
|---|
| 64 |
int frv; |
|---|
| 65 |
struct flock fl; |
|---|
| 66 |
if(fd < 0) return -1; |
|---|
| 67 |
|
|---|
| 68 |
memset(&fl, 0, sizeof(fl)); |
|---|
| 69 |
fl.l_type = F_UNLCK; |
|---|
| 70 |
fl.l_whence = SEEK_SET; |
|---|
| 71 |
fl.l_start = 0; |
|---|
| 72 |
fl.l_len = 0; |
|---|
| 73 |
|
|---|
| 74 |
while ((frv = fcntl(fd, F_SETLK, &fl)) == -1 && errno == EINTR); |
|---|
| 75 |
close(fd); |
|---|
| 76 |
if(frv < 0) return -1; |
|---|
| 77 |
return 0; |
|---|
| 78 |
} |
|---|