[sheepdog] [PATCH 1/2] sheep: exit when a passed path of log dir is not a directory

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Fri Jan 17 00:35:58 CET 2014


This patch lets sheep exit as soon as possible when the option of log
directory (specified with "-l dir=") is not a directory, but an
ordinal file. When a path of ordinal file is passed, logger process
runs as a defunct process. This behavior is confusing for
administrators.

The purpose of this change is preventing operation miss. If sheep is
passed a wrong path name of log directory, it should notify the
mistake to administrator. Running without correct logging is really
wrose in production environment.

In addition, this patch does a trivial cleaning. The variable path in
main() of sheep is too generic. So this patch renames it to log_path.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
 sheep/sheep.c |   22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/sheep/sheep.c b/sheep/sheep.c
index a550f22..09434c3 100644
--- a/sheep/sheep.c
+++ b/sheep/sheep.c
@@ -598,7 +598,7 @@ int main(int argc, char **argv)
 	int ch, longindex, ret, port = SD_LISTEN_PORT, io_port = SD_LISTEN_PORT;
 	int nr_vnodes = SD_DEFAULT_VNODES;
 	const char *dirp = DEFAULT_OBJECT_DIR, *short_options;
-	char *dir, *p, *pid_file = NULL, *bindaddr = NULL, path[PATH_MAX],
+	char *dir, *p, *pid_file = NULL, *bindaddr = NULL, log_path[PATH_MAX],
 	     *argp = NULL;
 	bool is_daemon = true, to_stdout = false, explicit_addr = false;
 	int64_t zone = -1;
@@ -606,6 +606,7 @@ int main(int argc, char **argv)
 	struct option *long_options;
 	const char *http_options = NULL;
 	static struct logger_user_info sheep_info;
+	struct stat logdir_st;
 
 	install_crash_handler(crash_handler);
 	signal(SIGPIPE, SIG_IGN);
@@ -765,7 +766,22 @@ int main(int argc, char **argv)
 		exit(1);
 	}
 
-	snprintf(path, sizeof(path), "%s/" LOG_FILE_NAME, logdir ?: dir);
+	if (logdir) {
+		memset(&logdir_st, 0, sizeof(logdir_st));
+		ret = stat(logdir, &logdir_st);
+		if (ret < 0) {
+			sd_err("stat() failed on %s, %m", logdir);
+			exit(1);
+		}
+
+		if (!S_ISDIR(logdir_st.st_mode)) {
+			sd_err("log dir: %s is not a directory", logdir);
+			exit(1);
+		}
+	}
+
+	snprintf(log_path, sizeof(log_path), "%s/" LOG_FILE_NAME,
+		 logdir ?: dir);
 
 	free(logdir);
 
@@ -774,7 +790,7 @@ int main(int argc, char **argv)
 	if (lock_and_daemon(is_daemon, dir))
 		exit(1);
 
-	ret = log_init(program_name, to_stdout, log_level, path);
+	ret = log_init(program_name, to_stdout, log_level, log_path);
 	if (ret)
 		exit(1);
 
-- 
1.7.10.4




More information about the sheepdog mailing list