bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2008-2018 Dovecot authors, see the included COPYING file */
5ae1765a2cad99de34d3e42b98b087a39b606184Timo Sirainenstatic struct dotlock_settings dotlock_settings = {
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen .stale_timeout = MAILDIR_UIDLIST_LOCK_STALE_TIMEOUT,
1c7fa51b35231f375998f66d5756f214519218f8Timo Sirainenstatic void sig_die(const siginfo_t *si ATTR_UNUSED, void *context ATTR_UNUSED)
5ae1765a2cad99de34d3e42b98b087a39b606184Timo Sirainenstatic int maildir_lock(const char *path, unsigned int timeout,
5ae1765a2cad99de34d3e42b98b087a39b606184Timo Sirainen dotlock_settings.use_excl_lock = getenv("DOTLOCK_USE_EXCL") != NULL;
5ae1765a2cad99de34d3e42b98b087a39b606184Timo Sirainen dotlock_settings.nfs_flush = getenv("MAIL_NFS_STORAGE") != NULL;
4e3e50374f74af24b35524a994af85a080c29722Timo Sirainen path = t_strconcat(path, "/" MAILDIR_UIDLIST_NAME, NULL);
5ae1765a2cad99de34d3e42b98b087a39b606184Timo Sirainen return file_dotlock_create(&dotlock_settings, path, 0, dotlock_r);
4e3e50374f74af24b35524a994af85a080c29722Timo Sirainen fprintf(stderr, "Usage: maildirlock <path> <timeout>\n"
4e3e50374f74af24b35524a994af85a080c29722Timo Sirainen " - SIGTERM will release the lock.\n");
f29fea1ed4ff62e438b982bd38ae46de12973857Timo Sirainen fprintf(stderr, "pipe() failed: %s", strerror(errno));
f29fea1ed4ff62e438b982bd38ae46de12973857Timo Sirainen fprintf(stderr, "fork() failed: %s", strerror(errno));
4e3e50374f74af24b35524a994af85a080c29722Timo Sirainen /* call lib_init() only after fork so that PID gets set correctly */
5512d420d826a2f9d4e7cb4e4919e1864fe688b0Timo Sirainen lib_signals_set_handler(SIGINT, LIBSIG_FLAG_DELAYED, sig_die, NULL);
5512d420d826a2f9d4e7cb4e4919e1864fe688b0Timo Sirainen lib_signals_set_handler(SIGTERM, LIBSIG_FLAG_DELAYED, sig_die, NULL);
9cd58c1fb9501bf7d3af7f2bb91b5bcbdcd1d8f2Timo Sirainen /* locking timed out */
4e3e50374f74af24b35524a994af85a080c29722Timo Sirainen /* child process - stdout has to be closed so that caller knows when
4e3e50374f74af24b35524a994af85a080c29722Timo Sirainen to stop reading it. */
e48f289d2e5b2546a2c5dcc90f7ab624cc58cca2Stephan Bosch i_fatal("Invalid timeout value: %s", argv[2]);
4e3e50374f74af24b35524a994af85a080c29722Timo Sirainen if (maildir_lock(argv[1], timeout, &dotlock) <= 0)
9cd58c1fb9501bf7d3af7f2bb91b5bcbdcd1d8f2Timo Sirainen /* locked - send a byte */