[sheepdog] [PATCH v2] add helper to get a self path

MORITA Kazutaka morita.kazutaka at gmail.com
Mon Sep 2 07:45:50 CEST 2013


From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---

v2:
 - rename get_self_path() to my_exe_path()

 include/util.h      |    1 +
 lib/logger.c        |   24 +++++-------------------
 lib/util.c          |   14 ++++++++++++++
 sheep/trace/trace.c |    5 +----
 4 files changed, 21 insertions(+), 23 deletions(-)

diff --git a/include/util.h b/include/util.h
index ace763b..f82e13d 100644
--- a/include/util.h
+++ b/include/util.h
@@ -113,6 +113,7 @@ void reraise_crash_signal(int signo, int status);
 pid_t gettid(void);
 int tkill(int tid, int sig);
 bool is_xattr_enabled(const char *path);
+const char *my_exe_path(void);
 
 void find_zero_blocks(const void *buf, uint64_t *poffset, uint32_t *plen);
 void trim_zero_blocks(void *buf, uint64_t *offset, uint32_t *len);
diff --git a/lib/logger.c b/lib/logger.c
index 1a31314..6c6a315 100644
--- a/lib/logger.c
+++ b/lib/logger.c
@@ -668,14 +668,6 @@ void get_thread_name(char *name)
 
 #define SD_MAX_STACK_DEPTH 1024
 
-static int get_my_path(char *path, size_t size)
-{
-	/* readlink doesn't append '\0', so initialize here */
-	memset(path, 0, size);
-
-	return readlink("/proc/self/exe", path, size);
-}
-
 static bool check_gdb(void)
 {
 	return system("which gdb > /dev/null") == 0;
@@ -683,7 +675,7 @@ static bool check_gdb(void)
 
 static int gdb_cmd(const char *cmd)
 {
-	char path[PATH_MAX], time_str[256], cmd_str[ARG_MAX];
+	char time_str[256], cmd_str[ARG_MAX];
 	time_t ti;
 	struct tm tm;
 
@@ -692,9 +684,6 @@ static int gdb_cmd(const char *cmd)
 		return -1;
 	}
 
-	if (get_my_path(path, sizeof(path)) < 0)
-		return -1;
-
 	time(&ti);
 	localtime_r(&ti, &tm);
 	strftime(time_str, sizeof(time_str), "%b %2d %H:%M:%S ", &tm);
@@ -710,7 +699,7 @@ static int gdb_cmd(const char *cmd)
 		 " -ex 'echo ==\\n'"
 		 " -ex '%s'"
 		 " -ex 'set logging off'",
-		 path, getpid(), time_str, path, cmd, cmd);
+		 my_exe_path(), getpid(), time_str, my_exe_path(), cmd, cmd);
 
 	return system(cmd_str);
 }
@@ -737,7 +726,7 @@ void sd_backtrace(void)
 
 	for (i = 1; i < n; i++) { /* addrs[0] is here, so skip it */
 		void *addr = addrs[i];
-		char cmd[ARG_MAX], path[PATH_MAX], info[256], **str;
+		char cmd[ARG_MAX], info[256], **str;
 		FILE *f;
 
 		/*
@@ -747,12 +736,9 @@ void sd_backtrace(void)
 		addr = (void *)((char *)addr - 1);
 
 		/* try to get a line number with addr2line if possible */
-		if (get_my_path(path, sizeof(path)) < 0)
-			goto fallback;
-
 		snprintf(cmd, sizeof(cmd), "addr2line -s -e %s -f -i %p | "
-			"perl -e '@a=<>; chomp @a; print \"$a[1]: $a[0]\"'",
-			path, addr);
+			 "perl -e '@a=<>; chomp @a; print \"$a[1]: $a[0]\"'",
+			 my_exe_path(), addr);
 		f = popen(cmd, "r");
 		if (!f)
 			goto fallback;
diff --git a/lib/util.c b/lib/util.c
index 346b6bd..2519e4e 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -558,6 +558,20 @@ bool is_xattr_enabled(const char *path)
 	return !(ret == -1 && errno == ENOTSUP);
 }
 
+const char *my_exe_path(void)
+{
+	static __thread char path[PATH_MAX];
+	int ret;
+
+	if (path[0] == '\0') {
+		ret = readlink("/proc/self/exe", path, sizeof(path));
+		if (ret < -1)
+			panic("%m");
+	}
+
+	return path;
+}
+
 /*
  * If force_create is true, this function create the file even when the
  * temporary file exists.
diff --git a/sheep/trace/trace.c b/sheep/trace/trace.c
index 458d625..f4f11e7 100644
--- a/sheep/trace/trace.c
+++ b/sheep/trace/trace.c
@@ -321,12 +321,9 @@ static unsigned long find_mcount_call(unsigned long entry_addr)
 
 static bfd *get_bfd(void)
 {
-	char fname[PATH_MAX] = {0};
+	const char *fname = my_exe_path();
 	bfd *abfd;
 
-	if (readlink("/proc/self/exe", fname, sizeof(fname)) < 0)
-		panic("failed to get a path of the program.");
-
 	abfd = bfd_openr(fname, NULL);
 	if (abfd == 0) {
 		sd_err("cannot open %s", fname);
-- 
1.7.9.5




More information about the sheepdog mailing list