mbox-lock.c revision 6797b2bb35596ec47d7f140eba79d4c686f370bc
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Copyright (c) 2002-2008 Dovecot authors, see the included COPYING file */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "lib.h"
16f816d3f3c32ae3351834253f52ddd0212bcbf3Timo Sirainen#include "restrict-access.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "nfs-workarounds.h"
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen#include "mail-index-private.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "mbox-storage.h"
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen#include "mbox-file.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "mbox-lock.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen#include <time.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <stdlib.h>
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen#include <unistd.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <fcntl.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <sys/stat.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#ifdef HAVE_FLOCK
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen# include <sys/file.h>
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen#endif
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen/* 0.1 .. 0.2msec */
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen#define LOCK_RANDOM_USLEEP_TIME (100000 + (unsigned int)rand() % 100000)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* lock methods to use in wanted order */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#define DEFAULT_READ_LOCK_METHODS "fcntl"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#define DEFAULT_WRITE_LOCK_METHODS "dotlock fcntl"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* lock timeout */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#define MBOX_DEFAULT_LOCK_TIMEOUT (5*60)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* assume stale dotlock if mbox file hasn't changed for n seconds */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#define DEFAULT_DOTLOCK_CHANGE_TIMEOUT (120)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainenenum mbox_lock_type {
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen MBOX_LOCK_DOTLOCK,
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen MBOX_LOCK_DOTLOCK_TRY,
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen MBOX_LOCK_FCNTL,
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen MBOX_LOCK_FLOCK,
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen MBOX_LOCK_LOCKF,
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen MBOX_LOCK_COUNT
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen};
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenenum mbox_dotlock_op {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen MBOX_DOTLOCK_OP_LOCK,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen MBOX_DOTLOCK_OP_UNLOCK,
41e1c7380edda701719d8ce1fb4d465d2ec4c84dTimo Sirainen MBOX_DOTLOCK_OP_TOUCH
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen};
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstruct mbox_lock_context {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen struct mbox_mailbox *mbox;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen int lock_status[MBOX_LOCK_COUNT];
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen bool checked_file;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen int lock_type;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen bool dotlock_last_stale;
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen bool fcntl_locked;
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen bool using_privileges;
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen};
ee246b46953e4b94b2f22e093373674fa9155500Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstruct mbox_lock_data {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen enum mbox_lock_type type;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen const char *name;
41e1c7380edda701719d8ce1fb4d465d2ec4c84dTimo Sirainen int (*func)(struct mbox_lock_context *ctx, int lock_type,
ee246b46953e4b94b2f22e093373674fa9155500Timo Sirainen time_t max_wait_time);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen};
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainenstatic int mbox_lock_dotlock(struct mbox_lock_context *ctx, int lock_type,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen time_t max_wait_time);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int mbox_lock_dotlock_try(struct mbox_lock_context *ctx, int lock_type,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen time_t max_wait_time);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int mbox_lock_fcntl(struct mbox_lock_context *ctx, int lock_type,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen time_t max_wait_time);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#ifdef HAVE_FLOCK
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainenstatic int mbox_lock_flock(struct mbox_lock_context *ctx, int lock_type,
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen time_t max_wait_time);
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen#else
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen# define mbox_lock_flock NULL
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen#endif
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen#ifdef HAVE_LOCKF
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainenstatic int mbox_lock_lockf(struct mbox_lock_context *ctx, int lock_type,
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen time_t max_wait_time);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen#else
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen# define mbox_lock_lockf NULL
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen#endif
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainenstruct mbox_lock_data lock_data[] = {
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen { MBOX_LOCK_DOTLOCK, "dotlock", mbox_lock_dotlock },
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen { MBOX_LOCK_DOTLOCK_TRY, "dotlock_try", mbox_lock_dotlock_try },
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen { MBOX_LOCK_FCNTL, "fcntl", mbox_lock_fcntl },
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen { MBOX_LOCK_FLOCK, "flock", mbox_lock_flock },
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen { MBOX_LOCK_LOCKF, "lockf", mbox_lock_lockf },
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen { 0, NULL, NULL }
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen};
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainenstatic bool lock_settings_initialized = FALSE;
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainenstatic enum mbox_lock_type read_locks[MBOX_LOCK_COUNT+1];
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainenstatic enum mbox_lock_type write_locks[MBOX_LOCK_COUNT+1];
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainenstatic int lock_timeout, dotlock_change_timeout;
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainenstatic int mbox_lock_list(struct mbox_lock_context *ctx, int lock_type,
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen time_t max_wait_time, int idx);
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainenstatic int mbox_unlock_files(struct mbox_lock_context *ctx);
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainenstatic void mbox_read_lock_methods(const char *str, const char *env,
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen enum mbox_lock_type *locks)
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen{
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen enum mbox_lock_type type;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen const char *const *lock;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen int i, dest;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen for (lock = t_strsplit(str, " "), dest = 0; *lock != NULL; lock++) {
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen for (type = 0; lock_data[type].name != NULL; type++) {
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen if (strcasecmp(*lock, lock_data[type].name) == 0) {
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen type = lock_data[type].type;
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen break;
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen }
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen }
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen if (lock_data[type].name == NULL)
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen i_fatal("%s: Invalid value %s", env, *lock);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen if (lock_data[type].func == NULL) {
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen i_fatal("%s: Support for lock type %s "
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen "not compiled into binary", env, *lock);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen }
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen for (i = 0; i < dest; i++) {
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen if (locks[i] == type)
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen i_fatal("%s: Duplicated value %s", env, *lock);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen }
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen /* @UNSAFE */
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen locks[dest++] = type;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen }
5626ae5e3316eced244adb6485c0927f1c7fdc41Timo Sirainen locks[dest] = (enum mbox_lock_type)-1;
5626ae5e3316eced244adb6485c0927f1c7fdc41Timo Sirainen}
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainenstatic void mbox_init_lock_settings(void)
5626ae5e3316eced244adb6485c0927f1c7fdc41Timo Sirainen{
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen const char *str;
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen int r, w;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen str = getenv("MBOX_READ_LOCKS");
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen if (str == NULL) str = DEFAULT_READ_LOCK_METHODS;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen mbox_read_lock_methods(str, "MBOX_READ_LOCKS", read_locks);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen str = getenv("MBOX_WRITE_LOCKS");
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen if (str == NULL) str = DEFAULT_WRITE_LOCK_METHODS;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen mbox_read_lock_methods(str, "MBOX_WRITE_LOCKS", write_locks);
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen /* check that read/write list orders match. write_locks must contain
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen at least read_locks and possibly more. */
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen for (r = w = 0; write_locks[w] != (enum mbox_lock_type)-1; w++) {
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen if (read_locks[r] == (enum mbox_lock_type)-1)
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen break;
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen if (read_locks[r] == write_locks[w])
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen r++;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen }
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen if (read_locks[r] != (enum mbox_lock_type)-1) {
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen i_fatal("mbox read/write lock list settings are invalid. "
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen "Lock ordering must be the same with both, "
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen "and write locks must contain all read locks "
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen "(and possibly more)");
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen }
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen str = getenv("MBOX_LOCK_TIMEOUT");
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen lock_timeout = str == NULL ? MBOX_DEFAULT_LOCK_TIMEOUT : atoi(str);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen str = getenv("MBOX_DOTLOCK_CHANGE_TIMEOUT");
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen dotlock_change_timeout = str == NULL ?
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen DEFAULT_DOTLOCK_CHANGE_TIMEOUT : atoi(str);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen lock_settings_initialized = TRUE;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen}
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainenstatic int mbox_file_open_latest(struct mbox_lock_context *ctx, int lock_type)
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen{
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen struct mbox_mailbox *mbox = ctx->mbox;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen struct stat st;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen if (ctx->checked_file || lock_type == F_UNLCK)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return 0;
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen if (mbox->mbox_fd != -1) {
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen /* we could flush NFS file handle cache here if we wanted to
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen be sure that the file is latest, but mbox files get rarely
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen deleted and the flushing might cause errors (e.g. EBUSY for
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen trying to flush a /var/mail mountpoint) */
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen if (nfs_safe_stat(mbox->path, &st) < 0) {
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen mbox_set_syscall_error(mbox, "stat()");
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen return -1;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen if (st.st_ino != mbox->mbox_ino ||
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen !CMP_DEV_T(st.st_dev, mbox->mbox_dev))
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen mbox_file_close(mbox);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen if (mbox->mbox_fd == -1) {
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen if (mbox_file_open(mbox) < 0)
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen return -1;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen ctx->checked_file = TRUE;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return 0;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen}
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenstatic bool dotlock_callback(unsigned int secs_left, bool stale, void *context)
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen{
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen struct mbox_lock_context *ctx = context;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen enum mbox_lock_type *lock_types;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen int i;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (ctx->using_privileges)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen restrict_access_drop_priv_gid();
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (stale && !ctx->dotlock_last_stale) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen /* get next index we wish to try locking. it's the one after
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen dotlocking. */
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen lock_types = ctx->lock_type == F_WRLCK ||
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen (ctx->lock_type == F_UNLCK &&
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen ctx->mbox->mbox_lock_type == F_WRLCK) ?
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen write_locks : read_locks;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen for (i = 0; lock_types[i] != (enum mbox_lock_type)-1; i++) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (lock_types[i] == MBOX_LOCK_DOTLOCK)
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen break;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen if (lock_types[i] != (enum mbox_lock_type)-1 &&
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen lock_types[i+1] != (enum mbox_lock_type)-1) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen i++;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (mbox_lock_list(ctx, ctx->lock_type, 0, i) <= 0) {
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen /* we couldn't get fd lock -
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen it's really locked */
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen ctx->dotlock_last_stale = TRUE;
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen return FALSE;
053843989f13d9013b265fb401a4bde7e0e6568eTimo Sirainen }
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen (void)mbox_lock_list(ctx, F_UNLCK, 0, i);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen }
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen }
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen ctx->dotlock_last_stale = stale;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen index_storage_lock_notify(&ctx->mbox->ibox, stale ?
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen MAILBOX_LOCK_NOTIFY_MAILBOX_OVERRIDE :
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen MAILBOX_LOCK_NOTIFY_MAILBOX_ABORT,
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen secs_left);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen if (ctx->using_privileges) {
ab286a8b58306eb8d22fc18342b6c199fd428e1eTimo Sirainen if (restrict_access_use_priv_gid() < 0) {
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen /* shouldn't get here */
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen return FALSE;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen }
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen }
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen return TRUE;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen}
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainenstatic int mbox_dotlock_privileged_op(struct mbox_mailbox *mbox,
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen struct dotlock_settings *set,
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen enum mbox_dotlock_op op)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen{
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen const char *dir, *fname;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen int ret = -1, orig_dir_fd;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen orig_dir_fd = open(".", O_RDONLY);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (orig_dir_fd == -1) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen i_error("open(.) failed: %m");
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return -1;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen }
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen /* allow dotlocks to be created only for files we can read while we're
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen unprivileged. to make sure there are no race conditions we first
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen have to chdir to the mbox file's directory and then use relative
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen paths. unless this is done, users could:
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen - create *.lock files to any directory writable by the
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen privileged group
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen - DoS other users by dotlocking their mailboxes infinitely
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen */
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen fname = strrchr(mbox->path, '/');
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (fname == NULL) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* already relative */
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen fname = mbox->path;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen } else {
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen dir = t_strdup_until(mbox->path, fname);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen if (chdir(dir) < 0) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen i_error("chdir(%s) failed: %m", dir);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen (void)close(orig_dir_fd);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return -1;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen fname++;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (op == MBOX_DOTLOCK_OP_LOCK) {
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen if (access(fname, R_OK) < 0) {
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen i_error("access(%s) failed: %m", mbox->path);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen return -1;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen }
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (restrict_access_use_priv_gid() < 0) {
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen (void)close(orig_dir_fd);
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen return -1;
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen }
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen switch (op) {
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen case MBOX_DOTLOCK_OP_LOCK:
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen /* we're now privileged - avoid doing as much as possible */
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen ret = file_dotlock_create(set, fname, 0, &mbox->mbox_dotlock);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (ret > 0)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen mbox->mbox_used_privileges = TRUE;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen break;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen case MBOX_DOTLOCK_OP_UNLOCK:
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* we're now privileged - avoid doing as much as possible */
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen ret = file_dotlock_delete(&mbox->mbox_dotlock);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen mbox->mbox_used_privileges = FALSE;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen break;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen case MBOX_DOTLOCK_OP_TOUCH:
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen if (!file_dotlock_is_locked(mbox->mbox_dotlock)) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen file_dotlock_delete(&mbox->mbox_dotlock);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen mbox->mbox_used_privileges = TRUE;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen ret = -1;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen } else {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen ret = file_dotlock_touch(mbox->mbox_dotlock);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen }
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen break;
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen }
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen restrict_access_drop_priv_gid();
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen if (fchdir(orig_dir_fd) < 0)
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen i_error("fchdir() failed: %m");
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen (void)close(orig_dir_fd);
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen return ret;
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen}
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainenstatic int
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainenmbox_lock_dotlock_int(struct mbox_lock_context *ctx, int lock_type, bool try)
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen{
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen struct mbox_mailbox *mbox = ctx->mbox;
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen struct dotlock_settings set;
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen int ret;
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen if (lock_type == F_UNLCK) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (!mbox->mbox_dotlocked)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return 1;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (!mbox->mbox_used_privileges)
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen ret = file_dotlock_delete(&mbox->mbox_dotlock);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen else {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen ctx->using_privileges = TRUE;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen ret = mbox_dotlock_privileged_op(mbox, NULL,
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen MBOX_DOTLOCK_OP_UNLOCK);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen ctx->using_privileges = FALSE;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen if (ret <= 0) {
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen mbox_set_syscall_error(mbox, "file_dotlock_delete()");
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen ret = -1;
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen }
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen mbox->mbox_dotlocked = FALSE;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return 1;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (mbox->mbox_dotlocked)
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen return 1;
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen ctx->dotlock_last_stale = -1;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen memset(&set, 0, sizeof(set));
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen set.use_excl_lock = (mbox->storage->storage.flags &
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen MAIL_STORAGE_FLAG_DOTLOCK_USE_EXCL) != 0;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen set.nfs_flush = (mbox->storage->storage.flags &
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen MAIL_STORAGE_FLAG_NFS_FLUSH_STORAGE) != 0;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen set.timeout = lock_timeout;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen set.stale_timeout = dotlock_change_timeout;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen set.callback = dotlock_callback;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen set.context = ctx;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen ret = file_dotlock_create(&set, mbox->path, 0, &mbox->mbox_dotlock);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (ret < 0 && errno == EACCES && restrict_access_have_priv_gid() &&
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen mbox->mbox_privileged_locking) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* try again, this time with extra privileges */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen ret = mbox_dotlock_privileged_op(mbox, &set,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen MBOX_DOTLOCK_OP_LOCK);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (ret < 0) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if ((ENOSPACE(errno) || errno == EACCES) && try)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen return 1;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen mbox_set_syscall_error(mbox, "file_lock_dotlock()");
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return -1;
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen }
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (ret == 0) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen mail_storage_set_error(&mbox->storage->storage,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen MAIL_ERROR_TEMP, MAIL_ERRSTR_LOCK_TIMEOUT);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen return 0;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen mbox->mbox_dotlocked = TRUE;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (mbox_file_open_latest(ctx, lock_type) < 0)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen return -1;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen return 1;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen}
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainenstatic int mbox_lock_dotlock(struct mbox_lock_context *ctx, int lock_type,
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen time_t max_wait_time ATTR_UNUSED)
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen{
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen return mbox_lock_dotlock_int(ctx, lock_type, FALSE);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen}
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenstatic int mbox_lock_dotlock_try(struct mbox_lock_context *ctx, int lock_type,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen time_t max_wait_time ATTR_UNUSED)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen{
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return mbox_lock_dotlock_int(ctx, lock_type, TRUE);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen}
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen#ifdef HAVE_FLOCK
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic int mbox_lock_flock(struct mbox_lock_context *ctx, int lock_type,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen time_t max_wait_time)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen{
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen time_t now, last_notify;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (mbox_file_open_latest(ctx, lock_type) < 0)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return -1;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (lock_type == F_UNLCK && ctx->mbox->mbox_fd == -1)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return 1;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (lock_type == F_WRLCK)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen lock_type = LOCK_EX;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen else if (lock_type == F_RDLCK)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen lock_type = LOCK_SH;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen else
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen lock_type = LOCK_UN;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen last_notify = 0;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen while (flock(ctx->mbox->mbox_fd, lock_type | LOCK_NB) < 0) {
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen if (errno != EWOULDBLOCK) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen mbox_set_syscall_error(ctx->mbox, "flock()");
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen return -1;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen now = time(NULL);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (now >= max_wait_time)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen return 0;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen if (now != last_notify) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen index_storage_lock_notify(&ctx->mbox->ibox,
871c7b8969e8627dc4c8b3e56fd126f948e6bce6Timo Sirainen MAILBOX_LOCK_NOTIFY_MAILBOX_ABORT,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen max_wait_time - now);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen usleep(LOCK_RANDOM_USLEEP_TIME);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return 1;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen}
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen#endif
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen#ifdef HAVE_LOCKF
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainenstatic int mbox_lock_lockf(struct mbox_lock_context *ctx, int lock_type,
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen time_t max_wait_time)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen{
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen time_t now, last_notify;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (mbox_file_open_latest(ctx, lock_type) < 0)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return -1;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (lock_type == F_UNLCK && ctx->mbox->mbox_fd == -1)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return 1;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (lock_type != F_UNLCK)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen lock_type = F_TLOCK;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen else
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen lock_type = F_ULOCK;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen last_notify = 0;
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen while (lockf(ctx->mbox->mbox_fd, lock_type, 0) < 0) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (errno != EAGAIN) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen mbox_set_syscall_error(ctx->mbox, "lockf()");
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return -1;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen now = time(NULL);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (now >= max_wait_time)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen return 0;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (now != last_notify) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen index_storage_lock_notify(&ctx->mbox->ibox,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen MAILBOX_LOCK_NOTIFY_MAILBOX_ABORT,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen max_wait_time - now);
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen }
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen usleep(LOCK_RANDOM_USLEEP_TIME);
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen return 1;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#endif
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int mbox_lock_fcntl(struct mbox_lock_context *ctx, int lock_type,
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen time_t max_wait_time)
811f2e26d9782d9cb99fdf82e18ffa0a77564fe2Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct flock fl;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen time_t now;
811f2e26d9782d9cb99fdf82e18ffa0a77564fe2Timo Sirainen unsigned int next_alarm;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen int wait_type;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mbox_file_open_latest(ctx, lock_type) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (lock_type == F_UNLCK && ctx->mbox->mbox_fd == -1)
811f2e26d9782d9cb99fdf82e18ffa0a77564fe2Timo Sirainen return 1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen memset(&fl, 0, sizeof(fl));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen fl.l_type = lock_type;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen fl.l_whence = SEEK_SET;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen fl.l_start = 0;
8830fab191cab8440281eb641dfdd93974b2933bTimo Sirainen fl.l_len = 0;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (max_wait_time == 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* usually we're waiting here, but if we came from
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mbox_lock_dotlock(), we just want to try locking */
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen wait_type = F_SETLK;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } else {
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen wait_type = F_SETLKW;
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen now = time(NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (now >= max_wait_time)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen alarm(1);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen else
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen alarm(I_MIN(max_wait_time - now, 5));
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen }
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen while (fcntl(ctx->mbox->mbox_fd, wait_type, &fl) < 0) {
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen if (errno != EINTR) {
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen if ((errno == EACCES || errno == EAGAIN) &&
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen wait_type == F_SETLK) {
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen /* non-blocking lock trying failed */
e12648867876aaec17e06ee4caef0bb60363449dTimo Sirainen return 0;
e12648867876aaec17e06ee4caef0bb60363449dTimo Sirainen }
e12648867876aaec17e06ee4caef0bb60363449dTimo Sirainen alarm(0);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (errno != EACCES) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mbox_set_syscall_error(ctx->mbox, "fcntl()");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen }
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen mail_storage_set_critical(&ctx->mbox->storage->storage,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "fcntl() failed with mbox file %s: "
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "File is locked by another process (EACCES)",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->mbox->path);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen now = time(NULL);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen if (now >= max_wait_time) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen alarm(0);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 0;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen }
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* notify locks once every 5 seconds.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen try to use rounded values. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen next_alarm = (max_wait_time - now) % 5;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (next_alarm == 0)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen next_alarm = 5;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen alarm(next_alarm);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen index_storage_lock_notify(&ctx->mbox->ibox,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen MAILBOX_LOCK_NOTIFY_MAILBOX_ABORT,
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen max_wait_time - now);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen }
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen alarm(0);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen ctx->fcntl_locked = TRUE;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen return 1;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen}
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainenstatic int mbox_lock_list(struct mbox_lock_context *ctx, int lock_type,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen time_t max_wait_time, int idx)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen{
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen enum mbox_lock_type *lock_types;
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen enum mbox_lock_type type;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen int i, ret = 0, lock_status;
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen ctx->lock_type = lock_type;
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen lock_types = lock_type == F_WRLCK ||
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen (lock_type == F_UNLCK && ctx->mbox->mbox_lock_type == F_WRLCK) ?
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen write_locks : read_locks;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen for (i = idx; lock_types[i] != (enum mbox_lock_type)-1; i++) {
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen type = lock_types[i];
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen lock_status = lock_type != F_UNLCK;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (ctx->lock_status[type] == lock_status)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen continue;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen ctx->lock_status[type] = lock_status;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = lock_data[type].func(ctx, lock_type, max_wait_time);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ret <= 0)
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen break;
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen }
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen return ret;
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen}
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainenstatic int mbox_update_locking(struct mbox_mailbox *mbox, int lock_type,
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen bool *fcntl_locked_r)
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen{
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen struct mbox_lock_context ctx;
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen time_t max_wait_time;
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen int ret, i;
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen bool drop_locks;
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen *fcntl_locked_r = FALSE;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen index_storage_lock_notify_reset(&mbox->ibox);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (!lock_settings_initialized)
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen mbox_init_lock_settings();
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen if (mbox->mbox_fd == -1 && mbox->mbox_file_stream != NULL) {
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen /* read-only mbox stream. no need to lock. */
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen i_assert(mbox->mbox_readonly);
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen mbox->mbox_lock_type = lock_type;
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen return 1;
4c1827d5d718d6f610df3209a2eb95a6591698afTimo Sirainen }
5a4ab3d6e108a899c8b51bebd0094a37b738d5a1Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen max_wait_time = time(NULL) + lock_timeout;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen memset(&ctx, 0, sizeof(ctx));
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen ctx.mbox = mbox;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mbox->mbox_lock_type == F_WRLCK) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* dropping to shared lock. first drop those that we
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen don't remove completely. */
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen for (i = 0; i < MBOX_LOCK_COUNT; i++)
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen ctx.lock_status[i] = 1;
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen for (i = 0; read_locks[i] != (enum mbox_lock_type)-1; i++)
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen ctx.lock_status[read_locks[i]] = 0;
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen drop_locks = TRUE;
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen } else {
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen drop_locks = FALSE;
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen }
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen mbox->mbox_lock_type = lock_type;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen ret = mbox_lock_list(&ctx, lock_type, max_wait_time, 0);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (ret <= 0) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (!drop_locks)
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen (void)mbox_unlock_files(&ctx);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (ret == 0) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen mail_storage_set_error(&mbox->storage->storage,
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen MAIL_ERROR_TEMP, MAIL_ERRSTR_LOCK_TIMEOUT);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen }
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen return ret;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen }
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (drop_locks) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen /* dropping to shared lock: drop the locks that are only
cd65dfcedc603fabbf644be793efb09096f6c1b5Timo Sirainen in write list */
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen memset(ctx.lock_status, 0, sizeof(ctx.lock_status));
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen for (i = 0; write_locks[i] != (enum mbox_lock_type)-1; i++)
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen ctx.lock_status[write_locks[i]] = 1;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen for (i = 0; read_locks[i] != (enum mbox_lock_type)-1; i++)
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen ctx.lock_status[read_locks[i]] = 0;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen mbox->mbox_lock_type = F_WRLCK;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen (void)mbox_lock_list(&ctx, F_UNLCK, 0, 0);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen mbox->mbox_lock_type = F_RDLCK;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen }
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen *fcntl_locked_r = ctx.fcntl_locked;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen return 1;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen}
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenint mbox_lock(struct mbox_mailbox *mbox, int lock_type,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen unsigned int *lock_id_r)
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen{
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen bool fcntl_locked;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen int ret;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* allow only unlock -> shared/exclusive or exclusive -> shared */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen i_assert(lock_type == F_RDLCK || lock_type == F_WRLCK);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen i_assert(lock_type == F_RDLCK || mbox->mbox_lock_type != F_RDLCK);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* mbox must be locked before index */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen i_assert(mbox->ibox.index->lock_type != F_WRLCK);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (mbox->mbox_lock_type == F_UNLCK) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen ret = mbox_update_locking(mbox, lock_type, &fcntl_locked);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (ret <= 0)
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen return ret;
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if ((mbox->storage->storage.flags &
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen MAIL_STORAGE_FLAG_NFS_FLUSH_STORAGE) != 0) {
28dd1eeb9fdb93051bb5899c80cf300d69ba9f9eTimo Sirainen if (fcntl_locked) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen nfs_flush_attr_cache_fd_locked(mbox->path,
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen mbox->mbox_fd);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen nfs_flush_read_cache_locked(mbox->path,
4c1827d5d718d6f610df3209a2eb95a6591698afTimo Sirainen mbox->mbox_fd);
4c1827d5d718d6f610df3209a2eb95a6591698afTimo Sirainen } else {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen nfs_flush_attr_cache_unlocked(mbox->path);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen nfs_flush_read_cache_unlocked(mbox->path,
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen mbox->mbox_fd);
05751847e5e493fecfd0634d35d21722aad44a0bTimo Sirainen }
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen }
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen mbox->mbox_lock_id += 2;
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen }
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (lock_type == F_RDLCK) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen mbox->mbox_shared_locks++;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen *lock_id_r = mbox->mbox_lock_id;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen } else {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen mbox->mbox_excl_locks++;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen *lock_id_r = mbox->mbox_lock_id + 1;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return 1;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen}
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainenstatic int mbox_unlock_files(struct mbox_lock_context *ctx)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen{
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen int ret = 0;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen if (mbox_lock_list(ctx, F_UNLCK, 0, 0) < 0)
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen ret = -1;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen ctx->mbox->mbox_lock_id += 2;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen ctx->mbox->mbox_lock_type = F_UNLCK;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen return ret;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen}
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainenint mbox_unlock(struct mbox_mailbox *mbox, unsigned int lock_id)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen{
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen struct mbox_lock_context ctx;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen bool fcntl_locked;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen int i;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen i_assert(mbox->mbox_lock_id == (lock_id & ~1));
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (lock_id & 1) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen /* dropping exclusive lock */
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen i_assert(mbox->mbox_excl_locks > 0);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (--mbox->mbox_excl_locks > 0)
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen return 0;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (mbox->mbox_shared_locks > 0) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen /* drop to shared lock */
28dd1eeb9fdb93051bb5899c80cf300d69ba9f9eTimo Sirainen if (mbox_update_locking(mbox, F_RDLCK,
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen &fcntl_locked) < 0)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen return -1;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return 0;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen } else {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* dropping shared lock */
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen i_assert(mbox->mbox_shared_locks > 0);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (--mbox->mbox_shared_locks > 0)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen return 0;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (mbox->mbox_excl_locks > 0)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen return 0;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* all locks gone */
b47c57ee07ccfe76cc23b23bc720e17fe9a46d5cTimo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen memset(&ctx, 0, sizeof(ctx));
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen ctx.mbox = mbox;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen for (i = 0; i < MBOX_LOCK_COUNT; i++)
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen ctx.lock_status[i] = 1;
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return mbox_unlock_files(&ctx);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen}
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenvoid mbox_dotlock_touch(struct mbox_mailbox *mbox)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen{
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (mbox->mbox_dotlock == NULL)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen return;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen if (!mbox->mbox_used_privileges)
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen (void)file_dotlock_touch(mbox->mbox_dotlock);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen else {
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen (void)mbox_dotlock_privileged_op(mbox, NULL,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen MBOX_DOTLOCK_OP_TOUCH);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen}
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen