return 0;
}
-#ifndef __linux__
static void close_and_unlink(int fd)
{
struct sockaddr_un addr;
unlink(addr.sun_path);
close(fd);
}
-#endif
static int userspace_interface_fd(const char *interface)
{
struct stat sbuf;
- struct sockaddr_un addr = { .sun_family = AF_UNIX };
-#ifndef __linux__
- struct sockaddr_un bind_addr = { .sun_family = AF_UNIX };
+ struct sockaddr_un addr = { .sun_family = AF_UNIX }, bind_addr = { .sun_family = AF_UNIX };
mode_t old_umask;
-#endif
int fd = -1, ret;
ret = -EINVAL;
ret = snprintf(addr.sun_path, sizeof(addr.sun_path) - 1, SOCK_PATH "%s" SOCK_SUFFIX, interface);
if (ret < 0)
goto out;
-#ifndef __linux__
ret = snprintf(bind_addr.sun_path, sizeof(bind_addr.sun_path) - 1, SOCK_PATH ".wg-tool-%s-%d.client", interface, getpid());
if (ret < 0)
goto out;
unlink(bind_addr.sun_path);
-#endif
ret = stat(addr.sun_path, &sbuf);
if (ret < 0)
goto out;
ret = fd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (ret < 0)
goto out;
-#ifdef __linux__
- ret = bind(fd, (struct sockaddr *)&addr, sizeof(sa_family_t));
-#else
old_umask = umask(0077);
ret = bind(fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
umask(old_umask);
-#endif
if (ret < 0)
goto out;
}
out:
if (ret && fd >= 0)
-#ifdef __linux__
- close(fd);
-#else
close_and_unlink(fd);
-#endif
if (!ret)
ret = fd;
return ret;
int fd = userspace_interface_fd(interface);
if (fd < 0)
return false;
-#ifdef __linux__
- close(fd);
-#else
close_and_unlink(fd);
-#endif
return true;
}
goto out;
ret = ret_code;
out:
-#ifdef __linux__
- close(fd);
-#else
close_and_unlink(fd);
-#endif
return (int)ret;
}
out:
if (*dev && ret)
free(*dev);
-#ifdef __linux__
- close(fd);
-#else
close_and_unlink(fd);
-#endif
errno = -ret;
return ret;
}