changeset 421:f37384470855

Changed lockdir to /var/lock/masqmail; Create lockdir and piddir on startup. Moved the lockdir out of the spool dir. (When /var/lock is a ramdisk we do well to have the lock files there.) Added the new configure option --with-lockdir to change that location. Nontheless, if we run_as_user, then lock files are always stored in the spool dir directly. Instead of installing the lockdir and piddir at installation time, we create them on startup time now if they are missing. This is necessary if lockdir or piddir are a tmpfs.
author markus schnalke <meillo@marmaro.de>
date Wed, 30 May 2012 09:38:38 +0200
parents 09da6e72cd30
children bdbedce60247
files INSTALL Makefile.am Makefile.in config.h.in configure configure.ac man/Makefile.in man/masqmail.conf.5 src/Makefile.in src/base64/Makefile.in src/conf.c src/masqmail.c src/md5/Makefile.in
diffstat 13 files changed, 105 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/INSTALL	Tue May 29 22:15:55 2012 +0200
+++ b/INSTALL	Wed May 30 09:38:38 2012 +0200
@@ -66,9 +66,13 @@
 prefer another location than /etc/masqmail/.
 
 --with-piddir=PIDDIR
-sets the default directory for the pid file of the daemon. The usual
+sets the directory for the pid file of the daemon. The default and usual
 location is /var/run, but some GNU/Linux distributions have converted
-to /run.
+to /run. It gets created on program startup if missing.
+
+--with-lockdir=LOCKDIR
+sets the default directory for lock file for spooled messages. Default
+is /var/lock/masqmail. It gets created on program startup if missing.
 
 
 --disable-resolver
@@ -102,10 +106,9 @@
 	drwxr-xr-x 2 mail mail      4096 May 10 12:34 /var/log/masqmail
 	drwxr-xr-x 5 mail mail      4096 May 10 12:34 /var/spool/masqmail
 	drwxr-xr-x 2 mail mail      4096 May 10 12:34 /var/spool/masqmail/input
-	drwxr-xr-x 2 mail mail      4096 May 10 12:34 /var/spool/masqmail/lock
 
 Important are the set-user-id bit for /usr/local/sbin/masqmail and
-the permissions of all files.
+the ownership of the directories.
 
 
 
--- a/Makefile.am	Tue May 29 22:15:55 2012 +0200
+++ b/Makefile.am	Wed May 30 09:38:38 2012 +0200
@@ -2,9 +2,9 @@
 
 SUBDIRS = src man
 
-install-data-local: log_dir spool_dir uid_bit conf_dir tpl_dir pid_dir doc_dir rmail
+install-data-local: log_dir spool_dir uid_bit conf_dir tpl_dir doc_dir rmail
 
-uninstall-local: rm-rmail rm-doc_dir rm-tpl_dir rm-conf_dir rm-pid_dir rm-spool_dir rm-log_dir
+uninstall-local: rm-rmail rm-doc_dir rm-tpl_dir rm-conf_dir rm-spool_dir rm-log_dir
 
 
 conf_dir:
@@ -37,13 +37,6 @@
 	rmdir $(DESTDIR)@docdir@
 
 
-pid_dir:
-	install -d $(DESTDIR)@with_piddir@
-
-rm-pid_dir:
-	rm -rf $(DESTDIR)@with_piddir@
-
-
 log_dir:
 	install -d -o @with_user@ -g @with_group@ $(DESTDIR)@with_logdir@
 
@@ -53,13 +46,11 @@
 
 spool_dir:
 	install -d -o @with_user@ -g @with_group@ $(DESTDIR)@with_spooldir@
-	install -d -o @with_user@ -g @with_group@ $(DESTDIR)@with_spooldir@/lock
 	install -d -o @with_user@ -g @with_group@ $(DESTDIR)@with_spooldir@/input
 
 rm-spool_dir:
 	: # removal fails if the dirs are non-empty
 	: # this prevents losing spooled files
-	rmdir $(DESTDIR)@with_spooldir@/lock
 	rmdir $(DESTDIR)@with_spooldir@/input
 	rmdir $(DESTDIR)@with_spooldir@
 
--- a/Makefile.in	Tue May 29 22:15:55 2012 +0200
+++ b/Makefile.in	Wed May 30 09:38:38 2012 +0200
@@ -199,6 +199,7 @@
 top_srcdir = @top_srcdir@
 with_confdir = @with_confdir@
 with_group = @with_group@
+with_lockdir = @with_lockdir@
 with_logdir = @with_logdir@
 with_piddir = @with_piddir@
 with_spooldir = @with_spooldir@
@@ -690,9 +691,9 @@
 	uninstall uninstall-am uninstall-local
 
 
-install-data-local: log_dir spool_dir uid_bit conf_dir tpl_dir pid_dir doc_dir rmail
+install-data-local: log_dir spool_dir uid_bit conf_dir tpl_dir doc_dir rmail
 
-uninstall-local: rm-rmail rm-doc_dir rm-tpl_dir rm-conf_dir rm-pid_dir rm-spool_dir rm-log_dir
+uninstall-local: rm-rmail rm-doc_dir rm-tpl_dir rm-conf_dir rm-spool_dir rm-log_dir
 
 conf_dir:
 	install -d $(DESTDIR)@with_confdir@
@@ -721,12 +722,6 @@
 	)
 	rmdir $(DESTDIR)@docdir@
 
-pid_dir:
-	install -d $(DESTDIR)@with_piddir@
-
-rm-pid_dir:
-	rm -rf $(DESTDIR)@with_piddir@
-
 log_dir:
 	install -d -o @with_user@ -g @with_group@ $(DESTDIR)@with_logdir@
 
@@ -735,13 +730,11 @@
 
 spool_dir:
 	install -d -o @with_user@ -g @with_group@ $(DESTDIR)@with_spooldir@
-	install -d -o @with_user@ -g @with_group@ $(DESTDIR)@with_spooldir@/lock
 	install -d -o @with_user@ -g @with_group@ $(DESTDIR)@with_spooldir@/input
 
 rm-spool_dir:
 	: # removal fails if the dirs are non-empty
 	: # this prevents losing spooled files
-	rmdir $(DESTDIR)@with_spooldir@/lock
 	rmdir $(DESTDIR)@with_spooldir@/input
 	rmdir $(DESTDIR)@with_spooldir@
 
--- a/config.h.in	Tue May 29 22:15:55 2012 +0200
+++ b/config.h.in	Wed May 30 09:38:38 2012 +0200
@@ -104,6 +104,9 @@
 /* Define to 1 if you have the `vprintf' function. */
 #undef HAVE_VPRINTF
 
+/* The lock file location */
+#undef LOCK_DIR
+
 /* The log directory */
 #undef LOG_DIR
 
--- a/configure	Tue May 29 22:15:55 2012 +0200
+++ b/configure	Wed May 30 09:38:38 2012 +0200
@@ -606,6 +606,7 @@
 am__EXEEXT_TRUE
 LTLIBOBJS
 LIBOBJS
+with_lockdir
 with_piddir
 with_confdir
 with_spooldir
@@ -717,6 +718,7 @@
 with_spooldir
 with_confdir
 with_piddir
+with_lockdir
 '
       ac_precious_vars='build_alias
 host_alias
@@ -1364,6 +1366,7 @@
   --with-spooldir=DIR     set spool directory /var/spool/masqmail
   --with-confdir=DIR      directory for configuration /etc/masqmail
   --with-piddir=DIR       directory for pid files /var/run
+  --with-lockdir=DIR      directory for lock files /var/lock/masqmail
 
 Some influential environment variables:
   CC          C compiler command
@@ -5310,6 +5313,22 @@
 
 
 
+
+# Check whether --with-lockdir was given.
+if test "${with_lockdir+set}" = set; then :
+  withval=$with_lockdir;
+else
+  with_lockdir='/var/lock/masqmail'
+
+fi
+
+
+cat >>confdefs.h <<_ACEOF
+#define LOCK_DIR "${with_lockdir}"
+_ACEOF
+
+
+
 test "x$prefix" = xNONE && prefix="$ac_default_prefix"
 
 
--- a/configure.ac	Tue May 29 22:15:55 2012 +0200
+++ b/configure.ac	Wed May 30 09:38:38 2012 +0200
@@ -197,6 +197,15 @@
 AC_DEFINE_UNQUOTED(PID_DIR, "${with_piddir}", [The pid file location])
 AC_SUBST(with_piddir)
 
+dnl dir for lock files
+AC_ARG_WITH(lockdir,
+	[  --with-lockdir=DIR      directory for lock files [/var/lock/masqmail]],
+	,
+	with_lockdir='/var/lock/masqmail'
+	)
+AC_DEFINE_UNQUOTED(LOCK_DIR, "${with_lockdir}", [The lock file location])
+AC_SUBST(with_lockdir)
+
 test "x$prefix" = xNONE && prefix="$ac_default_prefix"
 
 dnl well, /me/ thought that autoconf should make things _easy_ ... -- oku
--- a/man/Makefile.in	Tue May 29 22:15:55 2012 +0200
+++ b/man/Makefile.in	Wed May 30 09:38:38 2012 +0200
@@ -171,6 +171,7 @@
 top_srcdir = @top_srcdir@
 with_confdir = @with_confdir@
 with_group = @with_group@
+with_lockdir = @with_lockdir@
 with_logdir = @with_logdir@
 with_piddir = @with_piddir@
 with_spooldir = @with_spooldir@
--- a/man/masqmail.conf.5	Tue May 29 22:15:55 2012 +0200
+++ b/man/masqmail.conf.5	Wed May 30 09:38:38 2012 +0200
@@ -92,7 +92,7 @@
 \fBspool_dir = \fIfile\fR
 
 The directory where masqmail stores its spool files
-(and later also other stuff).
+(and lock files if \fIrun_as_user\fP).
 It must have a subdirectory \fIinput\fR.
 Masqmail needs read and write permissions for this directory.
 \fIfile\fR must be an absolute path.
@@ -104,8 +104,12 @@
 
 The directory where masqmail stores its lock files.
 Masqmail needs read and write permissions for this directory.
-By default it is a directory ``lock'' inside of \fIspool_dir\fP.
+The default is \fI/var/lock/masqmail\fR for normal operation.
 \fIfile\fR must be an absolute path.
+The directory is created on startup if yet missing.
+
+If \fIrun_as_user\fP then lock files are stored in the \fIspool_dir\fP
+directly and the \fBlock_dir\fP setting is ignored.
 
 .TP
 \fBhost_name = \fIstring\fR
--- a/src/Makefile.in	Tue May 29 22:15:55 2012 +0200
+++ b/src/Makefile.in	Wed May 30 09:38:38 2012 +0200
@@ -230,6 +230,7 @@
 top_srcdir = @top_srcdir@
 with_confdir = @with_confdir@
 with_group = @with_group@
+with_lockdir = @with_lockdir@
 with_logdir = @with_logdir@
 with_piddir = @with_piddir@
 with_spooldir = @with_spooldir@
--- a/src/base64/Makefile.in	Tue May 29 22:15:55 2012 +0200
+++ b/src/base64/Makefile.in	Wed May 30 09:38:38 2012 +0200
@@ -172,6 +172,7 @@
 top_srcdir = @top_srcdir@
 with_confdir = @with_confdir@
 with_group = @with_group@
+with_lockdir = @with_lockdir@
 with_logdir = @with_logdir@
 with_piddir = @with_piddir@
 with_spooldir = @with_spooldir@
--- a/src/conf.c	Tue May 29 22:15:55 2012 +0200
+++ b/src/conf.c	Wed May 30 09:38:38 2012 +0200
@@ -405,6 +405,7 @@
 	conf.localpartcmp = strcmp;
 	conf.max_defer_time = 86400 * 4;  /* 4 days */
 	conf.max_msg_size = 0; /* no limit on msg size */
+	conf.lock_dir = LOCK_DIR;
 	conf.spool_dir = SPOOL_DIR;
 	conf.mail_dir = "/var/mail";
 
@@ -538,9 +539,6 @@
 	if (!conf.warnmsg_file) {
 		conf.warnmsg_file = g_strdup(DATA_DIR "/tpl/warnmsg.tpl");
 	}
-	if (!conf.lock_dir) {
-		conf.lock_dir = g_strdup_printf("%s/lock/", conf.spool_dir);
-	}
 	if (!conf.mbox_default) {
 		conf.mbox_default = g_strdup("mbox");
 	}
--- a/src/masqmail.c	Tue May 29 22:15:55 2012 +0200
+++ b/src/masqmail.c	Wed May 30 09:38:38 2012 +0200
@@ -104,6 +104,43 @@
 	return NULL;
 }
 
+/*
+** Create any missing directory in pathname `dir'. (Like `mkdir -p'.)
+** The code is taken from nmh.
+*/
+gboolean
+makedir_rec(char *dir, int perms)
+{
+	char path[PATH_MAX];
+	char *cp, *c;
+	int had_an_error = 0;
+
+	c = strncpy(path, dir, sizeof(path));
+
+	while (!had_an_error && (c = strchr(c+1, '/'))) {
+		*c = '\0';
+		/* Create an outer directory. */
+		if (mkdir(path, perms) == -1 && errno != EEXIST) {
+			fprintf(stderr, "unable to create `%s': %s\n",
+					path, strerror(errno));
+			had_an_error = 1;
+		}
+		*c = '/';
+	}      
+
+	/*
+	** Create the innermost nested subdirectory of the
+	** path we're being asked to create.
+	*/
+	if (!had_an_error && mkdir(dir, perms)==-1 && errno != EEXIST) {
+		fprintf(stderr, "unable to create `%s': %s\n",
+				dir, strerror(errno));
+		had_an_error = 1;
+	}      
+
+	return (had_an_error) ? 0 : 1;
+}
+
 gboolean
 write_pidfile(gchar *name)
 {
@@ -146,6 +183,7 @@
 	}
 
 	signal(SIGTERM, sigterm_handler);
+	makedir_rec(PID_DIR, 0755);
 	write_pidfile(PID_DIR "/masqmail.pid");
 
 	conf.do_verbose = FALSE;
@@ -655,6 +693,7 @@
 	**  breaking security.
 	*/
 	if ((strcmp(conf_file, CONF_FILE) != 0) && (conf.orig_uid != 0)) {
+		logwrite(LOG_NOTICE, "Changing to run_as_user.\n");
 		conf.run_as_user = TRUE;
 		set_euidgid(conf.orig_uid, conf.orig_gid, NULL, NULL);
 		if (setgid(conf.orig_gid)) {
@@ -715,6 +754,18 @@
 		}
 	}
 
+	if (conf.run_as_user) {
+		logwrite(LOG_NOTICE, "Using spool directory `%s' for "
+				"lock files.\n", conf.spool_dir);
+		conf.lock_dir = conf.spool_dir;
+	} else {
+		int olduid, oldgid;
+
+		set_euidgid(conf.mail_uid, conf.mail_gid, &olduid, &oldgid);
+		makedir_rec(conf.lock_dir, 0775);
+		set_euidgid(olduid, oldgid, NULL, NULL);
+	}
+
 	if (!logopen()) {
 		fprintf(stderr, "could not open log file\n");
 		exit(1);
--- a/src/md5/Makefile.in	Tue May 29 22:15:55 2012 +0200
+++ b/src/md5/Makefile.in	Wed May 30 09:38:38 2012 +0200
@@ -168,6 +168,7 @@
 top_srcdir = @top_srcdir@
 with_confdir = @with_confdir@
 with_group = @with_group@
+with_lockdir = @with_lockdir@
 with_logdir = @with_logdir@
 with_piddir = @with_piddir@
 with_spooldir = @with_spooldir@