Autoselect the device when there is only one
Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
This commit is contained in:
27
src/ctl.c
27
src/ctl.c
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <dirent.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
@@ -125,11 +126,35 @@ ctl_create(const char *dir, const char *file)
|
|||||||
int
|
int
|
||||||
ctl_connect(int fd, const char *dir, const char *file)
|
ctl_connect(int fd, const char *dir, const char *file)
|
||||||
{
|
{
|
||||||
if (fd < 0 || str_empty(dir) || str_empty(file)) {
|
if (fd < 0 || str_empty(dir)) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!file) {
|
||||||
|
DIR *dp = opendir(dir);
|
||||||
|
|
||||||
|
if (!dp)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
struct dirent *d = NULL;
|
||||||
|
|
||||||
|
while (d = readdir(dp), d) {
|
||||||
|
if (d->d_name[0] == '.')
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (file) {
|
||||||
|
closedir(dp);
|
||||||
|
errno = ENOENT;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
file = &d->d_name[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir(dp);
|
||||||
|
}
|
||||||
|
|
||||||
struct sockaddr_un sun;
|
struct sockaddr_un sun;
|
||||||
|
|
||||||
if (ctl_setsun(&sun, dir, file))
|
if (ctl_setsun(&sun, dir, file))
|
||||||
|
|||||||
30
src/path.c
30
src/path.c
@@ -10,11 +10,10 @@
|
|||||||
int
|
int
|
||||||
gt_path(int argc, char **argv)
|
gt_path(int argc, char **argv)
|
||||||
{
|
{
|
||||||
const char *dev = "tun0";
|
const char *dev = NULL;
|
||||||
|
|
||||||
struct ctl_msg req = {
|
struct ctl_msg req = {
|
||||||
.type = CTL_STATE,
|
.type = CTL_STATE,
|
||||||
.path = {.state = MUD_UP},
|
|
||||||
}, res = {0};
|
}, res = {0};
|
||||||
|
|
||||||
struct argz pathz[] = {
|
struct argz pathz[] = {
|
||||||
@@ -26,33 +25,34 @@ gt_path(int argc, char **argv)
|
|||||||
if (argz(pathz, argc, argv))
|
if (argz(pathz, argc, argv))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
if (argz_is_set(pathz, "backup")) {
|
if (!req.path.addr.ss_family) {
|
||||||
|
return 0; // TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argz_is_set(pathz, "up")) {
|
||||||
|
req.path.state = MUD_UP;
|
||||||
|
} else if (argz_is_set(pathz, "backup")) {
|
||||||
req.path.state = MUD_BACKUP;
|
req.path.state = MUD_BACKUP;
|
||||||
} else if (argz_is_set(pathz, "down")) {
|
} else if (argz_is_set(pathz, "down")) {
|
||||||
req.path.state = MUD_DOWN;
|
req.path.state = MUD_DOWN;
|
||||||
|
} else {
|
||||||
|
return 0; // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
int fd = ctl_create("/run/" PACKAGE_NAME, NULL);
|
int fd = ctl_create("/run/" PACKAGE_NAME, NULL);
|
||||||
|
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
perror("ctl_create");
|
perror("path");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctl_connect(fd, "/run/" PACKAGE_NAME, dev) == -1) {
|
if ((ctl_connect(fd, "/run/" PACKAGE_NAME, dev) == -1) ||
|
||||||
gt_log("couldn't connect to %s\n", dev);
|
(ctl_reply(fd, &res, &req))) {
|
||||||
|
perror("path");
|
||||||
ctl_delete(fd);
|
ctl_delete(fd);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ret = 0;
|
|
||||||
|
|
||||||
if (ctl_reply(fd, &res, &req)) {
|
|
||||||
perror(dev);
|
|
||||||
ret = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ctl_delete(fd);
|
ctl_delete(fd);
|
||||||
|
return 0;
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ gt_set_timetolerance(int fd, unsigned long timetolerance)
|
|||||||
int
|
int
|
||||||
gt_set(int argc, char **argv)
|
gt_set(int argc, char **argv)
|
||||||
{
|
{
|
||||||
const char *dev = "tun0";
|
const char *dev = NULL;
|
||||||
unsigned long timetolerance = 0;
|
unsigned long timetolerance = 0;
|
||||||
unsigned long timeout = 0;
|
unsigned long timeout = 0;
|
||||||
size_t mtu = 0;
|
size_t mtu = 0;
|
||||||
@@ -66,12 +66,12 @@ gt_set(int argc, char **argv)
|
|||||||
int fd = ctl_create("/run/" PACKAGE_NAME, NULL);
|
int fd = ctl_create("/run/" PACKAGE_NAME, NULL);
|
||||||
|
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
perror("ctl_create");
|
perror("set");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctl_connect(fd, "/run/" PACKAGE_NAME, dev) == -1) {
|
if (ctl_connect(fd, "/run/" PACKAGE_NAME, dev) == -1) {
|
||||||
gt_log("couldn't connect to %s\n", dev);
|
perror("set");
|
||||||
ctl_delete(fd);
|
ctl_delete(fd);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user