update TODO
[ust.git] / libustcomm / ustcomm.c
index e1a2d5b9554425283b04a283d93403a5d846ff96..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;
@@ -157,11 +206,12 @@ int ustcomm_request_consumer(pid_t pid, const char *channel)
 }
 
 /* returns 1 to indicate a message was received
- * returns 0 to indicate no message was received (cannot happen)
+ * returns 0 to indicate no message was received (end of stream)
  * returns -1 to indicate an error
  */
 
 #define RECV_INCREMENT 1
+#define RECV_INITIAL_BUF_SIZE 10
 
 static int recv_message_fd(int fd, char **msg)
 {
@@ -170,13 +220,20 @@ static int recv_message_fd(int fd, char **msg)
        char *buf = NULL;
        int buf_used_size = 0;
 
-       buf = malloc(10);
-       buf_alloc_size = 16;
+       buf = malloc(RECV_INITIAL_BUF_SIZE);
+       buf_alloc_size = RECV_INITIAL_BUF_SIZE;
 
        for(;;) {
                if(buf_used_size + RECV_INCREMENT > buf_alloc_size) {
+                       char *new_buf;
                        buf_alloc_size *= 2;
-                       buf = (char *) realloc(buf, buf_alloc_size);
+                       new_buf = (char *) realloc(buf, buf_alloc_size);
+                       if(new_buf == NULL) {
+                               ERR("realloc returned NULL");
+                               free(buf);
+                               return -1;
+                       }
+                       buf = new_buf;
                }
 
                /* FIXME: this is really inefficient; but with count>1 we would
@@ -197,7 +254,6 @@ static int recv_message_fd(int fd, char **msg)
                        }
                }
 
-
                buf_used_size += result;
 
                if(buf[buf_used_size-1] == 0) {
@@ -319,7 +375,7 @@ int ustcomm_recv_message(struct ustcomm_server *server, char **msg, struct ustco
                                if(src)
                                        src->fd = fds[idx].fd;
 
-                               if(**msg == 0) {
+                               if(retval == 0) {
                                        /* connection finished */
                                        close(fds[idx].fd);
 
@@ -533,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.023718 seconds and 4 git commands to generate.