[sheepdog] [PATCH v2] logger: fix sd_backtrace()
Liu Yuan
namei.unix at gmail.com
Mon Feb 25 09:11:48 CET 2013
From: Liu Yuan <tailai.ly at taobao.com>
- restructure the code
- check fgets and readlink retval to mute the gcc warning
- fix resource leak that doesn't call pclose().
- use MAX_PATH for path
Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
- fix checkpatch warning
lib/logger.c | 28 +++++++++++++++++++---------
1 file changed, 19 insertions(+), 9 deletions(-)
diff --git a/lib/logger.c b/lib/logger.c
index dc72303..a7aab54 100644
--- a/lib/logger.c
+++ b/lib/logger.c
@@ -631,7 +631,8 @@ notrace void sd_backtrace(void)
for (i = 1; i < n; i++) { /* addrs[0] is here, so skip it */
void *addr = addrs[i];
- char cmd[1024], path[256] = {0}, info[256] = {0}, **str;
+ char cmd[PATH_MAX], path[PATH_MAX] = {0}, info[256] = {0},
+ **str;
FILE *f;
/* the called function is at the previous address
@@ -639,21 +640,30 @@ notrace void sd_backtrace(void)
addr = (void *)((char *)addr - 1);
/* try to get a line number with addr2line if possible */
- readlink("/proc/self/exe", path, sizeof(path));
+ if (readlink("/proc/self/exe", 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);
f = popen(cmd, "r");
- if (f != NULL) {
- fgets(info, sizeof(info), f);
- if (info[0] != '?' && info[0] != '\0') {
- sd_printf(SDOG_EMERG, "%s\n", info);
- continue;
- }
- }
+ if (!f)
+ goto fallback;
+ if (fgets(info, sizeof(info), f) == NULL)
+ goto fallback_close;
+
+ if (info[0] != '?' && info[0] != '\0')
+ sd_printf(SDOG_EMERG, "%s\n", info);
+ else
+ goto fallback_close;
+ pclose(f);
+ continue;
/* failed to get a line number, so simply use
* backtrace_symbols instead */
+fallback_close:
+ pclose(f);
+fallback:
str = backtrace_symbols(&addr, 1);
sd_printf(SDOG_EMERG, "%s\n", *str);
free(str);
--
1.7.9.5
More information about the sheepdog
mailing list