From d6d27063c1bef41888d6310d992106c205859353 Mon Sep 17 00:00:00 2001 From: Pierre-Marc Fournier Date: Tue, 23 Feb 2010 18:56:27 -0500 Subject: [PATCH] Don't call system() in the constructor path This leads to an infinite process creation loop. Instead of calling mkdir -p on the shell, add an mkdir -p function. --- libustcomm/ustcomm.c | 59 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/libustcomm/ustcomm.c b/libustcomm/ustcomm.c index 0ae70f9..e855825 100644 --- a/libustcomm/ustcomm.c +++ b/libustcomm/ustcomm.c @@ -52,6 +52,55 @@ // backtrace_symbols_fd(buffer, result, STDERR_FILENO); //} +static int mkdir_p(const char *path, mode_t mode) +{ + char *path_p; + char *tmp; + + int retval = 0; + int result; + + tmp = malloc(strlen(path) + 1); + if (tmp == NULL) + return -1; + + /* skip first / */ + path_p = path+1; + + for(;;) { + while (*path_p != '/') { + if(*path_p == 0) + break; + ++path_p; + } + if (*path_p == '/') { + strncpy(tmp, path, path_p - path); + tmp[path_p-path] = '\0'; + if (tmp[path_p - path - 1] != '/') { + result = mkdir(tmp, mode); + if(result == -1) { + if (!(errno == EEXIST || errno == EACCES || errno == EROFS)) { + /* Then this is a real error */ + retval = -1; + break; + } + } + } + /* pass / */ + path_p++; + } else { + /* last component */ + result = mkdir(path, mode); + if (result == -1) + retval = -1; + break; + } + } + + free(tmp); + return retval; +} + char *strdup_malloc(const char *s) { char *retval; @@ -540,17 +589,11 @@ static int ensure_dir_exists(const char *dir) } else if(result == -1) { /* ENOENT */ - char buf[200]; int result; - result = snprintf(buf, sizeof(buf), "mkdir -p \"%s\"", dir); - if(result >= sizeof(buf)) { - ERR("snprintf buffer overflow"); - return -1; - } - result = system(buf); + result = mkdir_p(dir, 0777); if(result != 0) { - ERR("executing command %s", buf); + ERR("executing in recursive creation of directory %s", dir); return -1; } } -- 2.34.1