|
| 1 | +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | +/* vim:set ts=2 sw=2 sts=2 et cindent: */ |
| 3 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 | + |
| 7 | +#include "MultiInstanceLock.h" |
| 8 | + |
| 9 | +#include "commonupdatedir.h" // for GetInstallHash |
| 10 | +#include "mozilla/UniquePtr.h" |
| 11 | +#include "nsPrintfCString.h" |
| 12 | +#include "nsPromiseFlatString.h" |
| 13 | +#include "updatedefines.h" // for NS_t* definitions |
| 14 | + |
| 15 | +#ifndef XP_WIN |
| 16 | +# include <fcntl.h> |
| 17 | +# include <sys/stat.h> |
| 18 | +# include <sys/types.h> |
| 19 | +#endif |
| 20 | + |
| 21 | +namespace mozilla { |
| 22 | + |
| 23 | +static bool GetLockFileName(const char* nameToken, const char16_t* installPath, |
| 24 | + nsCString& filePath) { |
| 25 | + mozilla::UniquePtr<NS_tchar[]> pathHash; |
| 26 | + if (!GetInstallHash(installPath, MOZ_APP_VENDOR, pathHash)) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + |
| 30 | +#ifdef XP_WIN |
| 31 | + // On Windows, the lock file is placed at the path |
| 32 | + // ProgramData\[vendor]\[nameToken]-[pathHash], so first we need to get the |
| 33 | + // ProgramData path and then append our directory and the file name. |
| 34 | + PWSTR programDataPath; |
| 35 | + HRESULT hr = SHGetKnownFolderPath(FOLDERID_ProgramData, KF_FLAG_CREATE, |
| 36 | + nullptr, &programDataPath); |
| 37 | + if (FAILED(hr)) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + mozilla::UniquePtr<wchar_t, CoTaskMemFreeDeleter> programDataPathUnique( |
| 41 | + programDataPath); |
| 42 | + |
| 43 | + filePath = nsPrintfCString("%S\\%s\\%s-%S", programDataPath, MOZ_APP_VENDOR, |
| 44 | + nameToken, pathHash.get()); |
| 45 | + |
| 46 | +#else |
| 47 | + // On POSIX platforms the base path is /tmp/[vendor][nameToken]-[pathHash]. |
| 48 | + filePath = nsPrintfCString("/tmp/%s%s-%s", MOZ_APP_VENDOR, nameToken, |
| 49 | + pathHash.get()); |
| 50 | + |
| 51 | +#endif |
| 52 | + |
| 53 | + return true; |
| 54 | +} |
| 55 | + |
| 56 | +MultiInstLockHandle OpenMultiInstanceLock(const char* nameToken, |
| 57 | + const char16_t* installPath) { |
| 58 | + nsCString filePath; |
| 59 | + GetLockFileName(nameToken, installPath, filePath); |
| 60 | + |
| 61 | + // Open a file handle with full privileges and sharing, and then attempt to |
| 62 | + // take a shared (nonexclusive, read-only) lock on it. |
| 63 | +#ifdef XP_WIN |
| 64 | + HANDLE h = |
| 65 | + ::CreateFileW(PromiseFlatString(NS_ConvertUTF8toUTF16(filePath)).get(), |
| 66 | + GENERIC_READ | GENERIC_WRITE, |
| 67 | + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 68 | + nullptr, OPEN_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, nullptr); |
| 69 | + if (h != INVALID_HANDLE_VALUE) { |
| 70 | + // The LockFileEx functions always require an OVERLAPPED structure even |
| 71 | + // though we did not open the lock file for overlapped I/O. |
| 72 | + OVERLAPPED o = {0}; |
| 73 | + if (!::LockFileEx(h, LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &o)) { |
| 74 | + CloseHandle(h); |
| 75 | + h = INVALID_HANDLE_VALUE; |
| 76 | + } |
| 77 | + } |
| 78 | + return h; |
| 79 | + |
| 80 | +#else |
| 81 | + int fd = ::open(PromiseFlatCString(filePath).get(), |
| 82 | + O_CLOEXEC | O_CREAT | O_NOFOLLOW, |
| 83 | + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
| 84 | + if (fd != -1) { |
| 85 | + // We would like to ensure that the lock file is deleted when we are done |
| 86 | + // with it. The normal way to do that would be to call unlink on it right |
| 87 | + // now, but that would immediately delete the name from the file system, and |
| 88 | + // we need other instances to be able to open that name and get the same |
| 89 | + // inode, so we can't unlink the file before we're done with it. This means |
| 90 | + // we accept some unreliability in getting the file deleted, but it's a zero |
| 91 | + // byte file in the tmp directory, so having it stay around isn't the worst. |
| 92 | + struct flock l = {0}; |
| 93 | + l.l_start = 0; |
| 94 | + l.l_len = 0; |
| 95 | + l.l_type = F_RDLCK; |
| 96 | + if (::fcntl(fd, F_SETLK, &l)) { |
| 97 | + ::close(fd); |
| 98 | + fd = -1; |
| 99 | + } |
| 100 | + } |
| 101 | + return fd; |
| 102 | + |
| 103 | +#endif |
| 104 | +} |
| 105 | + |
| 106 | +void ReleaseMultiInstanceLock(MultiInstLockHandle lock) { |
| 107 | + if (lock != MULTI_INSTANCE_LOCK_HANDLE_ERROR) { |
| 108 | +#ifdef XP_WIN |
| 109 | + OVERLAPPED o = {0}; |
| 110 | + ::UnlockFileEx(lock, 0, 1, 0, &o); |
| 111 | + // We've used FILE_FLAG_DELETE_ON_CLOSE, so if we are the last instance |
| 112 | + // with a handle on the lock file, closing it here will delete it. |
| 113 | + ::CloseHandle(lock); |
| 114 | + |
| 115 | +#else |
| 116 | + // If we're the last instance, then unlink the lock file. There is a race |
| 117 | + // condition here that may cause an instance to fail to open the same inode |
| 118 | + // as another even though they use the same path, but there's no reasonable |
| 119 | + // way to avoid that without skipping deleting the file at all, so we accept |
| 120 | + // that risk. |
| 121 | + bool otherInstance = true; |
| 122 | + if (IsOtherInstanceRunning(lock, &otherInstance) && !otherInstance) { |
| 123 | + // Recover the file's path so we can unlink it. |
| 124 | + // There's no error checking in here because we're content to let the file |
| 125 | + // hang around if any of this fails (which can happen if for example we're |
| 126 | + // on a system where /proc/self/fd does not exist); this is a zero-byte |
| 127 | + // file in the tmp directory after all. |
| 128 | + UniquePtr<NS_tchar[]> linkPath = MakeUnique<NS_tchar[]>(MAXPATHLEN + 1); |
| 129 | + NS_tsnprintf(linkPath.get(), MAXPATHLEN + 1, "/proc/self/fd/%d", lock); |
| 130 | + UniquePtr<NS_tchar[]> lockFilePath = |
| 131 | + MakeUnique<NS_tchar[]>(MAXPATHLEN + 1); |
| 132 | + if (::readlink(linkPath.get(), lockFilePath.get(), MAXPATHLEN + 1) != |
| 133 | + -1) { |
| 134 | + ::unlink(lockFilePath.get()); |
| 135 | + } |
| 136 | + } |
| 137 | + // Now close the lock file, which will release the lock. |
| 138 | + ::close(lock); |
| 139 | +#endif |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +bool IsOtherInstanceRunning(MultiInstLockHandle lock, bool* aResult) { |
| 144 | + // Every running instance has opened a readonly lock, and read locks prevent |
| 145 | + // write locks from being opened, so to see if we are the only instance, we |
| 146 | + // attempt to take a write lock, and if it succeeds then that must mean there |
| 147 | + // are no other read locks open and therefore no other instances. |
| 148 | + if (lock == MULTI_INSTANCE_LOCK_HANDLE_ERROR) { |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | +#ifdef XP_WIN |
| 153 | + // We need to release the lock we're holding before we would be allowed to |
| 154 | + // take an exclusive lock, and if that succeeds we need to release it too |
| 155 | + // in order to get our shared lock back. This procedure is not atomic, so we |
| 156 | + // accept the risk of the scheduler deciding to ruin our day between these |
| 157 | + // operations; we'd get a false negative in a different instance's check. |
| 158 | + OVERLAPPED o = {0}; |
| 159 | + // Release our current shared lock. |
| 160 | + if (!::UnlockFileEx(lock, 0, 1, 0, &o)) { |
| 161 | + return false; |
| 162 | + } |
| 163 | + // Attempt to take an exclusive lock. |
| 164 | + bool rv = false; |
| 165 | + if (::LockFileEx(lock, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, |
| 166 | + 1, 0, &o)) { |
| 167 | + // We got the exclusive lock, so now release it. |
| 168 | + ::UnlockFileEx(lock, 0, 1, 0, &o); |
| 169 | + *aResult = false; |
| 170 | + rv = true; |
| 171 | + } else if (::GetLastError() == ERROR_LOCK_VIOLATION) { |
| 172 | + // We didn't get the exclusive lock because of outstanding shared locks. |
| 173 | + *aResult = true; |
| 174 | + rv = true; |
| 175 | + } |
| 176 | + // Attempt to reclaim the shared lock we released at the beginning. |
| 177 | + if (!::LockFileEx(lock, LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &o)) { |
| 178 | + rv = false; |
| 179 | + } |
| 180 | + return rv; |
| 181 | + |
| 182 | +#else |
| 183 | + // See if we would be allowed to set a write lock (no need to actually do so). |
| 184 | + struct flock l = {0}; |
| 185 | + l.l_start = 0; |
| 186 | + l.l_len = 0; |
| 187 | + l.l_type = F_WRLCK; |
| 188 | + if (::fcntl(lock, F_GETLK, &l)) { |
| 189 | + return false; |
| 190 | + } |
| 191 | + *aResult = l.l_type != F_UNLCK; |
| 192 | + return true; |
| 193 | + |
| 194 | +#endif |
| 195 | +} |
| 196 | + |
| 197 | +}; // namespace mozilla |
0 commit comments