Don't call system() in the constructor path
authorPierre-Marc Fournier <pierre-marc.fournier@polymtl.ca>
Tue, 23 Feb 2010 23:56:27 +0000 (18:56 -0500)
committerPierre-Marc Fournier <pierre-marc.fournier@polymtl.ca>
Tue, 23 Feb 2010 23:56:27 +0000 (18:56 -0500)
This leads to an infinite process creation loop.

Instead of calling mkdir -p on the shell, add an mkdir -p function.

libustcomm/ustcomm.c

index 0ae70f9af3b3a0eab970434743528371c1d9e333..e8558255e573f8f3cdf26d92ae3178bfc87026cc 100644 (file)
 //     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;
                }
        }
This page took 0.024869 seconds and 4 git commands to generate.