Subject: [PATCH/WIP v2 09/14] file-watcher: add --shutdown and --log options

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Fri, 17 Jan 2014 16:47:35 +0700

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 file-watcher.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/file-watcher.c b/file-watcher.c
index 1b4ac0a..df06529 100644
--- a/file-watcher.c
+++ b/file-watcher.c
@@ -113,6 +113,8 @@ static int handle_command(int fd)
 			remove_updated(arg + len + 1);
 	} else if (starts_with(msg, "watch ")) {
 		watch_paths(msg + 6, len - 6, fd, &sun);
+	} else if ((arg = skip_prefix(msg, "log "))) {
+		fprintf(stderr, "log %s\n", arg);
 	} else if (!strcmp(msg, "die")) {
 		exit(0);
 	} else {
@@ -168,11 +170,16 @@ int main(int argc, const char **argv)
 	struct pollfd pfd[2];
 	int fd, err, nr;
 	const char *prefix;
-	int daemon = 0, quiet = 0;
+	int daemon = 0, quiet = 0, shutdown = 0;
+	const char *log_string = NULL;
 	struct option options[] = {
 		OPT__QUIET(&quiet, N_("be quiet")),
 		OPT_BOOL(0, "daemon", &daemon,
-			 N_("run in background")),
+			 N_("run in background (default)")),
+		OPT_BOOL(0, "shutdown", &shutdown,
+			 N_("shut down running file-watcher daemon")),
+		OPT_STRING(0, "log", &log_string, "string",
+			   N_("string to log to index.watcher.log")),
 		OPT_END()
 	};
 
@@ -190,11 +197,24 @@ int main(int argc, const char **argv)
 	fd = socket(AF_UNIX, SOCK_DGRAM, 0);
 	sun.sun_family = AF_UNIX;
 	strlcpy(sun.sun_path, socket_path, sizeof(sun.sun_path));
+
+	if (shutdown || log_string) {
+		struct stat st;
+		if (stat(socket_path, &st) || !S_ISSOCK(st.st_mode))
+			return 0;
+		if (log_string && send_watcher(fd, &sun, "log %s", log_string) < 0)
+			die_errno("failed to shut file-watcher down");
+		if (shutdown && send_watcher(fd, &sun, "die") < 0)
+			die_errno("failed to shut file-watcher down");
+		return 0;
+	}
+
 	if (bind(fd, (struct sockaddr *)&sun, sizeof(sun))) {
 		if (quiet)
 			exit(128);
 		die_errno("unable to bind to %s", socket_path);
 	}
+
 	atexit(cleanup);
 	sigchain_push_common(cleanup_on_signal);
 
-- 
1.8.5.1.208.g05b12ea