Create socket directly from ctl_connect()
Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
This commit is contained in:
27
src/ctl.c
27
src/ctl.c
@@ -9,9 +9,16 @@
|
||||
#include <sys/stat.h>
|
||||
#include <sys/un.h>
|
||||
|
||||
#define CTL_BIND_MAX 64
|
||||
|
||||
int
|
||||
ctl_reply(int fd, struct ctl_msg *res, struct ctl_msg *req)
|
||||
{
|
||||
if (fd == -1) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((send(fd, req, sizeof(struct ctl_msg), 0) == -1) ||
|
||||
(recv(fd, res, sizeof(struct ctl_msg), 0) == -1))
|
||||
return -1;
|
||||
@@ -58,7 +65,7 @@ ctl_bind(int fd, const char *dir, const char *file)
|
||||
struct sockaddr_un sun;
|
||||
|
||||
if (str_empty(file)) {
|
||||
for (int i = 0; i < 64; i++) {
|
||||
for (int i = 0; i < CTL_BIND_MAX; i++) {
|
||||
if (snprintf(tmp, sizeof(tmp), ".%i", i) >= sizeof(tmp))
|
||||
return -1;
|
||||
|
||||
@@ -124,9 +131,9 @@ ctl_create(const char *dir, const char *file)
|
||||
}
|
||||
|
||||
int
|
||||
ctl_connect(int fd, const char *dir, const char *file)
|
||||
ctl_connect(const char *dir, const char *file)
|
||||
{
|
||||
if (fd < 0 || str_empty(dir)) {
|
||||
if (str_empty(dir)) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
@@ -160,5 +167,17 @@ ctl_connect(int fd, const char *dir, const char *file)
|
||||
if (ctl_setsun(&sun, dir, file))
|
||||
return -1;
|
||||
|
||||
return connect(fd, (struct sockaddr *)&sun, sizeof(sun));
|
||||
int fd = ctl_create(dir, NULL);
|
||||
|
||||
if (fd == -1)
|
||||
return -1;
|
||||
|
||||
if (connect(fd, (struct sockaddr *)&sun, sizeof(sun))) {
|
||||
int err = errno;
|
||||
ctl_delete(fd);
|
||||
errno = err;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
@@ -39,6 +39,6 @@ struct ctl_msg {
|
||||
};
|
||||
|
||||
int ctl_create (const char *, const char *);
|
||||
int ctl_connect (int, const char *, const char *);
|
||||
int ctl_connect (const char *, const char *);
|
||||
int ctl_reply (int, struct ctl_msg *, struct ctl_msg *);
|
||||
void ctl_delete (int);
|
||||
|
||||
@@ -39,15 +39,14 @@ gt_path(int argc, char **argv)
|
||||
return 0; // TODO
|
||||
}
|
||||
|
||||
int fd = ctl_create("/run/" PACKAGE_NAME, NULL);
|
||||
int fd = ctl_connect("/run/" PACKAGE_NAME, dev);
|
||||
|
||||
if (fd == -1) {
|
||||
perror("path");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ((ctl_connect(fd, "/run/" PACKAGE_NAME, dev) == -1) ||
|
||||
(ctl_reply(fd, &res, &req))) {
|
||||
if (ctl_reply(fd, &res, &req)) {
|
||||
perror("path");
|
||||
ctl_delete(fd);
|
||||
return 1;
|
||||
|
||||
@@ -127,19 +127,13 @@ gt_set(int argc, char **argv)
|
||||
if (argz(pathz, argc, argv))
|
||||
return 1;
|
||||
|
||||
int fd = ctl_create("/run/" PACKAGE_NAME, NULL);
|
||||
int fd = ctl_connect("/run/" PACKAGE_NAME, dev);
|
||||
|
||||
if (fd == -1) {
|
||||
perror("set");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ctl_connect(fd, "/run/" PACKAGE_NAME, dev) == -1) {
|
||||
perror("set");
|
||||
ctl_delete(fd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
|
||||
if (argz_is_set(pathz, "mtu"))
|
||||
|
||||
@@ -41,9 +41,6 @@ gt_ss_addr(char *str, size_t size, struct sockaddr_storage *ss)
|
||||
static int
|
||||
gt_show_dev_status(int fd, const char *dev)
|
||||
{
|
||||
if (ctl_connect(fd, "/run/" PACKAGE_NAME, dev) == -1)
|
||||
return -1;
|
||||
|
||||
struct ctl_msg res, req = {.type = CTL_STATUS};
|
||||
|
||||
if (ctl_reply(fd, &res, &req))
|
||||
@@ -89,9 +86,9 @@ gt_show_dev_status(int fd, const char *dev)
|
||||
static int
|
||||
gt_show_dev(const char *dev)
|
||||
{
|
||||
int fd = ctl_create("/run/" PACKAGE_NAME, NULL);
|
||||
int fd = ctl_connect("/run/" PACKAGE_NAME, dev);
|
||||
|
||||
if (fd < 0) {
|
||||
if (fd == -1) {
|
||||
perror(dev);
|
||||
return -1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user