Fix ctl_connect()

Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
This commit is contained in:
Adrien Gallouët
2018-03-15 22:05:42 +00:00
parent c06abdbe3c
commit 245b8e671e

View File

@@ -133,13 +133,15 @@ ctl_create(const char *dir, const char *file)
int int
ctl_connect(const char *dir, const char *file) ctl_connect(const char *dir, const char *file)
{ {
DIR *dp = NULL;
if (str_empty(dir)) { if (str_empty(dir)) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
if (!file) { if (!file) {
DIR *dp = opendir(dir); dp = opendir(dir);
if (!dp) if (!dp)
return -1; return -1;
@@ -151,20 +153,30 @@ ctl_connect(const char *dir, const char *file)
continue; continue;
if (file) { if (file) {
closedir(dp); file = NULL;
errno = ENOENT; break;
return -1;
} }
file = &d->d_name[0]; file = &d->d_name[0];
} }
closedir(dp); if (!file) {
closedir(dp);
errno = ENOENT;
return -1;
}
} }
struct sockaddr_un sun; struct sockaddr_un sun;
const int ret = ctl_setsun(&sun, dir, file);
if (ctl_setsun(&sun, dir, file)) if (dp) {
int err = errno;
closedir(dp);
errno = err;
}
if (ret)
return -1; return -1;
int fd = ctl_create(dir, NULL); int fd = ctl_create(dir, NULL);