Browse code

bb11566 - added additional checks and more verbose error messages

Mickey Sola authored on 2016/05/19 03:16:43
Showing 1 changed files
... ...
@@ -104,45 +104,51 @@ int localserver(const struct optstruct *opts)
104 104
     strncpy(server.sun_path, optget(opts, "LocalSocket")->strarg, sizeof(server.sun_path));
105 105
     server.sun_path[sizeof(server.sun_path)-1]='\0';
106 106
 
107
-    cnt = 0;
108
-    sockdir = NULL;
109
-    pos = server.sun_path + strlen(server.sun_path);
110
-    while (pos != server.sun_path) {
111
-        if (*pos == '/') {
112
-            sockdir = strndup(server.sun_path, strlen(server.sun_path) - cnt);
113
-            break;
114
-        }
115
-        else {
116
-            pos--;
117
-            cnt++;
107
+    pos = NULL;
108
+    if ((pos = strstr(server.sun_path, "/")) && (pos = strstr(((char*) pos + 1), "/"))) {
109
+        cnt = 0;
110
+        sockdir = NULL;
111
+        pos = server.sun_path + strlen(server.sun_path);
112
+        while (pos != server.sun_path) {
113
+            if (*pos == '/') {
114
+                sockdir = strndup(server.sun_path, strlen(server.sun_path) - cnt);
115
+                break;
116
+            }
117
+            else {
118
+                pos--;
119
+                cnt++;
120
+            }
118 121
         }
119
-    }
120 122
 
121
-    if (stat(sockdir, &sb)) {
122
-        if (errno == ENOENT) {
123
-            mode_t sock_mode;
124
-            if(optget(opts, "LocalSocketMode")->enabled) {
125
-                char *end;
126
-                sock_mode = strtol(optget(opts, "LocalSocketMode")->strarg, &end, 8);
127
-
128
-                if(*end) {
129
-                    logg("!Invalid LocalSocketMode %s\n", optget(opts, "LocalSocketMode")->strarg);
130
-                    free(sockdir);
131
-                    return -1;
123
+        if (stat(sockdir, &sb)) {
124
+            if (errno == ENOENT) {
125
+                mode_t sock_mode;
126
+                if(optget(opts, "LocalSocketMode")->enabled) {
127
+                    char *end;
128
+                    sock_mode = strtol(optget(opts, "LocalSocketMode")->strarg, &end, 8);
129
+
130
+                    if(*end) {
131
+                        logg("!Invalid LocalSocketMode %s\n", optget(opts, "LocalSocketMode")->strarg);
132
+                        free(sockdir);
133
+                        return -1;
134
+                    }
135
+                } else {
136
+                    sock_mode = 0777;
132 137
                 }
133
-            } else {
134
-                sock_mode = 0777;
135
-            }
136 138
 
137
-            if (mkdir(sockdir, sock_mode)) {
138
-                logg("!LOCAL: Could not create socket directory: %s, %s\n", sockdir, strerror(errno));
139
-            }
140
-            else {
141
-                logg("LOCAL: Creating socket directory: %s\n", sockdir);
139
+                if (mkdir(sockdir, sock_mode)) {
140
+                    logg("!LOCAL: Could not create socket directory: %s: %s\n", sockdir, strerror(errno));
141
+                    if (errno == ENOENT) {
142
+                        logg("!LOCAL: Ensure parent directory exists.\n");
143
+                    }
144
+                }
145
+                else {
146
+                    logg("Localserver: Creating socket directory: %s\n", sockdir);
147
+                }
142 148
             }
143 149
         }
150
+        free(sockdir);
144 151
     }
145
-    free(sockdir);
146 152
 
147 153
     if((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
148 154
 	estr = strerror(errno);