l2md: add oneshot option to sync once and exit

By default l2md will loop forever and periodically fetch and
sync. Add a config option that enables l2md to sync once and
exit, so that it could also be invoked for oneshot operation
(e.g. via crontab), instead of running as a daemon service.

Signed-off-by: Anthony Iliopoulos <ailiopoulos@suse.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
diff --git a/config.c b/config.c
index dd277cb..e4dab04 100644
--- a/config.c
+++ b/config.c
@@ -42,6 +42,8 @@
 				url->oid_known ? url->oid : "[unknown]");
 		}
 	}
+
+	verbose("oneshot = %d\n", cfg->oneshot);
 }
 
 static void config_probe_oids(struct config *cfg)
@@ -258,6 +260,8 @@
 				config_set_out(cfg, tmp, true);
 			} else if (sscanf(buff, "\tbase = %1023s", tmp) == 1) {
 				config_set_basedir(cfg, tmp);
+			} else if (sscanf(buff, "\toneshot = %u", &val) == 1) {
+				cfg->oneshot = val;
 			} else {
 				goto state_next;
 			}
diff --git a/env.c b/env.c
index f72d15c..f821562 100644
--- a/env.c
+++ b/env.c
@@ -66,9 +66,12 @@
 
 void sync_done(struct config *cfg)
 {
-	verbose("Sync done. Sleeping %us.\n", cfg->general.period);
+	verbose("Sync done. ");
 
-	sleep(cfg->general.period);
+	if (!cfg->oneshot) {
+		verbose("Sleeping %us.\n", cfg->general.period);
+		sleep(cfg->general.period);
+	}
 }
 
 void sync_env(struct config *cfg)
diff --git a/l2md.c b/l2md.c
index 458cda0..d4f7445 100644
--- a/l2md.c
+++ b/l2md.c
@@ -37,13 +37,13 @@
 	bootstrap_env(cfg);
 	signal(SIGINT, signal_handler);
 
-	while (!sigint) {
+	do {
 		sync_mail(cfg);
 		sync_done(cfg);
 		if (sigint)
 			break;
 		sync_env(cfg);
-	}
+	} while (!sigint && !cfg->oneshot);
 
 	config_uninit(cfg);
 	return 0;
diff --git a/l2md.h b/l2md.h
index c127937..46ad443 100644
--- a/l2md.h
+++ b/l2md.h
@@ -72,6 +72,7 @@
 	struct config_repo     *repos;
 	uint32_t                repos_num;
 	const struct mail_ops  *ops;
+	bool			oneshot;
 };
 
 #define repo_for_each(cfg, repo, i) \