|
| 1 | +/** |
| 2 | + * @file ilockfile.hpp |
| 3 | + * @author itas109 (itas109@qq.com) \n\n |
| 4 | + * Blog : https://blog.csdn.net/itas109 \n |
| 5 | + * Github : https://github.com/itas109 \n |
| 6 | + * Gitee : https://gitee.com/itas109 \n |
| 7 | + * QQ Group : 129518033 |
| 8 | + * @brief lightweight cross-platform lock file library 轻量级跨平台锁文件类 |
| 9 | + */ |
| 10 | +#ifndef __I_LOCKFILE_HPP__ |
| 11 | +#define __I_LOCKFILE_HPP__ |
| 12 | + |
| 13 | +#pragma once |
| 14 | + |
| 15 | +#include <stdio.h> |
| 16 | +#include <string.h> |
| 17 | +#include <errno.h> |
| 18 | +#include <string> // std::atoi |
| 19 | + |
| 20 | +#if defined(_WIN32) |
| 21 | +#define LOCKFILE_UNSUPPORTED |
| 22 | +#else |
| 23 | +#include <unistd.h> |
| 24 | +#include <fcntl.h> |
| 25 | +#include <sys/stat.h> |
| 26 | +#include <signal.h> |
| 27 | +#endif |
| 28 | + |
| 29 | +namespace itas109 |
| 30 | +{ |
| 31 | +class ILockFile |
| 32 | +{ |
| 33 | +public: |
| 34 | + ILockFile() |
| 35 | + : m_fd(-1) |
| 36 | + { |
| 37 | + m_path[0] = '\0'; |
| 38 | + } |
| 39 | + |
| 40 | + ~ILockFile() |
| 41 | + { |
| 42 | + unlock(); |
| 43 | + } |
| 44 | + |
| 45 | + bool lock(const char *deviceName) |
| 46 | + { |
| 47 | +#ifdef LOCKFILE_UNSUPPORTED |
| 48 | + (void)deviceName; |
| 49 | + return false; |
| 50 | +#else |
| 51 | + if (!buildLockPath(deviceName)) |
| 52 | + { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + if (tryCreate()) |
| 57 | + { |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + if (errno == EEXIST) |
| 62 | + { |
| 63 | + if (checkStaleAndRecover()) |
| 64 | + { |
| 65 | + return true; |
| 66 | + } |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + if (errno == EACCES || errno == EPERM) |
| 71 | + { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + return false; |
| 76 | +#endif |
| 77 | + } |
| 78 | + |
| 79 | + void unlock() |
| 80 | + { |
| 81 | +#ifndef LOCKFILE_UNSUPPORTED |
| 82 | + if (m_fd >= 0) |
| 83 | + { |
| 84 | + close(m_fd); |
| 85 | + unlink(m_path); |
| 86 | + m_fd = -1; |
| 87 | + } |
| 88 | +#endif |
| 89 | + } |
| 90 | + |
| 91 | + const char *path() const |
| 92 | + { |
| 93 | + return m_path; |
| 94 | + } |
| 95 | + |
| 96 | +private: |
| 97 | +#ifndef LOCKFILE_UNSUPPORTED |
| 98 | + bool buildLockPath(const char *deviceName) |
| 99 | + { |
| 100 | + const char *base = strrchr(deviceName, '/'); |
| 101 | + base = base ? base + 1 : deviceName; |
| 102 | + |
| 103 | + const char *dirs[] = { |
| 104 | +#if defined(__APPLE__) |
| 105 | + "/var/spool/uucp", |
| 106 | +#elif defined(__linux__) |
| 107 | + "/run/lock", |
| 108 | + "/var/lock", |
| 109 | + "/var/spool/uucp", |
| 110 | +#else |
| 111 | + "/var/spool/lock", |
| 112 | + "/var/spool/uucp", |
| 113 | +#endif |
| 114 | + ".", // current directory |
| 115 | + }; |
| 116 | + |
| 117 | + for (size_t i = 0; i < sizeof(dirs) / sizeof(dirs[0]); ++i) |
| 118 | + { |
| 119 | + if (access(dirs[i], W_OK) == 0) |
| 120 | + { |
| 121 | + snprintf(m_path, sizeof(m_path), "%s/LCK..%s", dirs[i], base); |
| 122 | + return true; |
| 123 | + } |
| 124 | + } |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + bool tryCreate() |
| 129 | + { |
| 130 | + m_fd = open(m_path, O_CREAT | O_EXCL | O_WRONLY, 0644); |
| 131 | + if (m_fd < 0) |
| 132 | + { |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + char buf[32]; |
| 137 | + snprintf(buf, sizeof(buf), "%ld\n", (long)getpid()); |
| 138 | + write(m_fd, buf, strlen(buf)); |
| 139 | + return true; |
| 140 | + } |
| 141 | + |
| 142 | + bool checkStaleAndRecover() |
| 143 | + { |
| 144 | + // get pid from lock file |
| 145 | + int fd = open(m_path, O_RDONLY); |
| 146 | + if (fd < 0) |
| 147 | + { |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + char buf[32] = {0}; |
| 152 | + read(fd, buf, sizeof(buf) - 1); |
| 153 | + close(fd); |
| 154 | + |
| 155 | + pid_t pid = (pid_t)std::atoi(buf); |
| 156 | + if (pid <= 0) |
| 157 | + { |
| 158 | + return false; |
| 159 | + } |
| 160 | + |
| 161 | + // check if process is running |
| 162 | + if (kill(pid, 0) == -1 && errno == ESRCH) |
| 163 | + { |
| 164 | + // stale lock |
| 165 | + unlink(m_path); |
| 166 | + return tryCreate(); |
| 167 | + } |
| 168 | + return false; |
| 169 | + } |
| 170 | +#endif |
| 171 | + |
| 172 | +private: |
| 173 | + char m_path[256]; |
| 174 | + int m_fd; |
| 175 | +}; |
| 176 | + |
| 177 | +} // namespace itas109 |
| 178 | +#endif // __I_LOCKFILE_HPP__ |
0 commit comments