|
| 1 | +/** |
| 2 | + * This file is part of the EGIL SCIM client. |
| 3 | + * |
| 4 | + * Copyright (C) 2017-2025 Föreningen Sambruk |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as |
| 8 | + * published by the Free Software Foundation, either version 3 of the |
| 9 | + * License, or (at your option) any later version. |
| 10 | +
|
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | +
|
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +#ifndef EGILSCIM_ADVISORY_FILE_LOCK_HPP |
| 21 | +#define EGILSCIM_ADVISORY_FILE_LOCK_HPP |
| 22 | + |
| 23 | +#include <memory> |
| 24 | +#include <string> |
| 25 | +#include <boost/interprocess/sync/named_mutex.hpp> |
| 26 | + |
| 27 | +/** This is an advisory file lock for the cache file. |
| 28 | + * Internally this uses boost's named_mutex to coordinate |
| 29 | + * access to the cache file, but in a pragmatic way to |
| 30 | + * suit our needs for the cache file. |
| 31 | + * |
| 32 | + * Motivation: |
| 33 | + * |
| 34 | + * 1. We really want to avoid being blocked from writing the new cache file. |
| 35 | + * 2. Under normal circumstances there should be little risk of having to |
| 36 | + * wait long for access to the file. |
| 37 | + * 3. There could be other processes accessing the file (such as a backup script) |
| 38 | + * which won't respect our advisory locking anyway, so the writing to the |
| 39 | + * cache file will anyway need to implement a retry in case the filesystem |
| 40 | + * blocks the rename when we're done writing. |
| 41 | + * 4. boost's named_mutex can throw an interprocess_error, if the interprocess |
| 42 | + * implementation isn't working for some reason we don't want that to be |
| 43 | + * enough of a reason not to write the cache file. |
| 44 | + * 5. If we fail to get the lock within a reasonable time, the most likely explanation |
| 45 | + * is not that others are accessing it, but that the named_mutex hasn't been properly |
| 46 | + * released (for instance it the process is hard killed while holding the mutex). |
| 47 | + * |
| 48 | + * All this put together leads to an implementation which tries to take the |
| 49 | + * named mutex but with a timeout. If we fail to take the mutex (either by timeout or |
| 50 | + * a problem with the interprocess system), we'll carry on as if we have the mutex |
| 51 | + * (and remove it from the system so we won't need to wait next time). |
| 52 | + * |
| 53 | + * In other words, under normal circumstances this will block multiple instances of |
| 54 | + * EgilSCIMClient from accessing the file at once, but it's not a 100% guarantee. |
| 55 | + * We're ok with that since we're anyway retrying the writes. |
| 56 | + */ |
| 57 | +class AdvisoryFileLock { |
| 58 | +public: |
| 59 | + /* |
| 60 | + * path is the file to lock. |
| 61 | + * timeout is given in seconds. |
| 62 | + */ |
| 63 | + AdvisoryFileLock(const std::string& path, int timeout); |
| 64 | + virtual ~AdvisoryFileLock(); |
| 65 | + |
| 66 | +private: |
| 67 | + std::unique_ptr<boost::interprocess::named_mutex> m_pmtx; |
| 68 | + int m_timeout; |
| 69 | +}; |
| 70 | + |
| 71 | +#endif // EGILSCIM_ADVISORY_FILE_LOCK_HPP |
0 commit comments