| 1 | /* |
| 2 | Copyright (c) 2012, Broadcom Europe Ltd |
| 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 met: |
| 7 | * Redistributions of source code must retain the above copyright |
| 8 | notice, this list of conditions and the following disclaimer. |
| 9 | * Redistributions in binary form must reproduce the above copyright |
| 10 | notice, this list of conditions and the following disclaimer in the |
| 11 | documentation and/or other materials provided with the distribution. |
| 12 | * Neither the name of the copyright holder nor the |
| 13 | names of its contributors may be used to endorse or promote products |
| 14 | derived from this software without specific prior written permission. |
| 15 | |
| 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "vcfiled_check.h" |
| 29 | #include <errno.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <unistd.h> |
| 34 | #include <syslog.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <fcntl.h> |
| 37 | |
| 38 | int vcfiled_lock(const char *lockfile, VCFILED_LOGMSG_T logmsg) |
| 39 | { |
| 40 | int rc, fd; |
| 41 | char pidbuf[32]; |
| 42 | char *lockdir = strdup(lockfile); |
| 43 | char *sep = strrchr(lockdir, '/'); |
| 44 | int ret = -1; |
| 45 | if (!sep) |
| 46 | { |
| 47 | free(lockdir); |
| 48 | return -1; |
| 49 | } |
| 50 | *sep = '\0'; |
| 51 | |
| 52 | if (mkdir(lockdir, S_IRWXU | S_IRGRP|S_IXGRP) < 0) |
| 53 | { |
| 54 | if (errno != EEXIST) |
| 55 | { |
| 56 | logmsg(LOG_CRIT, "could not create %s:%s\n" , lockdir,strerror(errno)); |
| 57 | goto finish; |
| 58 | } |
| 59 | } |
| 60 | fd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0640); |
| 61 | if (fd<0) |
| 62 | { |
| 63 | if (errno != EEXIST) |
| 64 | { |
| 65 | logmsg(LOG_CRIT, "could not create lockfile %s:%s\n" , lockfile, strerror(errno)); |
| 66 | goto finish; |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | // it already exists - reopen it and try to lock it |
| 71 | fd = open(lockfile, O_RDWR); |
| 72 | if (fd<0) |
| 73 | { |
| 74 | logmsg(LOG_CRIT, "could not re-open lockfile %s:%s\n" , lockfile, strerror(errno)); |
| 75 | goto finish; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | // at this point, we have opened the file, and can use discretionary locking, |
| 80 | // which should work even over NFS |
| 81 | |
| 82 | struct flock flock; |
| 83 | memset(&flock, 0, sizeof(flock)); |
| 84 | flock.l_type = F_WRLCK; |
| 85 | flock.l_whence = SEEK_SET; |
| 86 | flock.l_start = 0; |
| 87 | flock.l_len = 1; |
| 88 | if (fcntl(fd, F_SETLK, &flock) < 0) |
| 89 | { |
| 90 | // if we failed to lock, then it might mean we're already running, or |
| 91 | // something else bad. |
| 92 | if (errno == EACCES || errno == EAGAIN) |
| 93 | { |
| 94 | // already running |
| 95 | int pid = 0, rc = read(fd, pidbuf, sizeof(pidbuf)); |
| 96 | if (rc) |
| 97 | pid = atoi(pidbuf); |
| 98 | logmsg(LOG_CRIT, "already running at pid %d\n" , pid); |
| 99 | close(fd); |
| 100 | goto finish; |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | logmsg(LOG_CRIT, "could not lock %s:%s\n" , lockfile, strerror(errno)); |
| 105 | close(fd); |
| 106 | goto finish; |
| 107 | } |
| 108 | } |
| 109 | snprintf(pidbuf,sizeof(pidbuf),"%d" , getpid()); |
| 110 | rc = write(fd, pidbuf, strlen(pidbuf)+1); |
| 111 | if (rc<0) |
| 112 | { |
| 113 | logmsg(LOG_CRIT, "could not write pid:%s\n" , strerror(errno)); |
| 114 | goto finish; |
| 115 | } |
| 116 | /* do not close the file, as that will release the lock - it will |
| 117 | * will close automatically when the program exits. |
| 118 | */ |
| 119 | ret = 0; |
| 120 | finish: |
| 121 | free(lockdir); |
| 122 | /* coverity[leaked_handle] - fd left open on purpose */ |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | int vcfiled_is_running(const char *filename) |
| 127 | { |
| 128 | int ret; |
| 129 | |
| 130 | int fd = open(filename, O_RDONLY); |
| 131 | if (fd < 0) |
| 132 | { |
| 133 | // file not there, so filed not running |
| 134 | ret = 0; |
| 135 | } |
| 136 | |
| 137 | else |
| 138 | { |
| 139 | struct flock flock; |
| 140 | memset(&flock, 0, sizeof(flock)); |
| 141 | flock.l_type = F_WRLCK; |
| 142 | flock.l_whence = SEEK_SET; |
| 143 | flock.l_start = 0; |
| 144 | flock.l_len = 1; |
| 145 | int rc = fcntl(fd, F_GETLK, &flock); |
| 146 | if (rc != 0) |
| 147 | { |
| 148 | /* could not access lock info */ |
| 149 | printf("%s: Could not access lockfile %s: %s\n" , |
| 150 | "vmcs_main" , filename, strerror(errno)); |
| 151 | ret = 0; |
| 152 | } |
| 153 | else if (flock.l_pid == 0) |
| 154 | { |
| 155 | /* file is unlocked, so filed not running */ |
| 156 | ret = 0; |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | /* file is locked, so filed is running */ |
| 161 | ret = 1; |
| 162 | } |
| 163 | } |
| 164 | /* coverity[leaked_handle] - fd left open on purpose */ |
| 165 | return ret; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | |
| 170 | |