aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNatanael Copa <ncopa@alpinelinux.org>2011-09-21 15:29:05 +0200
committerNatanael Copa <ncopa@alpinelinux.org>2011-09-21 15:29:05 +0200
commit9c73acd621249be32e89a657a12489e40e1a02fb (patch)
treeb81d2e9bff162642b141e04746c5b88763394a78
parent05ed02cf3a124e228480da65e09b88751c0859e6 (diff)
downloadpingu-9c73acd621249be32e89a657a12489e40e1a02fb.tar.bz2
pingu-9c73acd621249be32e89a657a12489e40e1a02fb.tar.xz
pinguctl: initial admin client
-rw-r--r--.gitignore1
-rw-r--r--Makefile13
-rw-r--r--pinguctl.c85
3 files changed, 97 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index 70b9845..178190c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
pingu
+pinguctl
mtu
*.o
diff --git a/Makefile b/Makefile
index d9d73be..7575be4 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
-TARGETS = mtu pingu
+TARGETS = mtu pingu pinguctl
VERSION = 0.5
CFLAGS ?= -g
@@ -30,18 +30,27 @@ pingu_OBJS = \
pingu_LIBS = -lev
+pinguctl_OBJS = \
+ log.o \
+ pinguctl.o
+
+pinguctl_LIBS =
+
mtu_OBJS = \
mtu.o \
netlink.o \
icmp.o
-ALL_OBJS= $(pingu_OBJS) $(mtu_OBJS)
+ALL_OBJS= $(pingu_OBJS) $(pinguctl_OBJS) $(mtu_OBJS)
all: $(TARGETS)
pingu: $(pingu_OBJS)
$(CC) $(LDFLAGS) $(pingu_OBJS) $(pingu_LIBS) -o $@
+pinguctl: $(pinguctl_OBJS)
+ $(CC) $(LDFLAGS) $(pinguctl_OBJS) $(pinguctl_LIBS) -o $@
+
mtu: $(mtu_OBJS)
install: $(TARGETS)
diff --git a/pinguctl.c b/pinguctl.c
new file mode 100644
index 0000000..6788794
--- /dev/null
+++ b/pinguctl.c
@@ -0,0 +1,85 @@
+
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include "log.h"
+#include "pingu_adm.h"
+
+static int adm_init(const char *socket_path)
+{
+ struct sockaddr_un sun;
+ int fd;
+
+ memset(&sun, 0, sizeof(sun));
+ sun.sun_family = AF_UNIX;
+ strncpy(sun.sun_path, socket_path, sizeof(sun.sun_path));
+
+ fd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (fd < 0) {
+ log_perror("socket");
+ return -1;
+ }
+
+ if (connect(fd, (struct sockaddr *) &sun, sizeof(sun)) < 0) {
+ log_perror(socket_path);
+ close(fd);
+ return -1;
+ }
+
+ return fd;
+}
+
+static int adm_send_cmd(int fd, const char *cmd)
+{
+ char buf[256];
+ size_t len;
+
+ snprintf(buf, sizeof(buf), "%s\n", cmd);
+ len = strlen(buf);
+ if (write(fd, buf, len) != len)
+ return -1;
+ return len;
+}
+
+static int adm_recv(int fd)
+{
+ char buf[1024];
+ int n;
+
+ n = recv(fd, buf, sizeof(buf), 0);
+ if (n > 0)
+ write(STDOUT_FILENO, buf, n);
+ return n;
+}
+
+int main(int argc, char *argv[])
+{
+ const char *socket_path = DEFAULT_ADM_SOCKET;
+ int i, fd;
+
+ while ((i = getopt(argc, argv, "a:")) != -1) {
+ switch (i) {
+ case 'a':
+ socket_path = optarg;
+ break;
+ }
+ argc -= optind;
+ argv += optind;
+ }
+ log_init("pinguctl", 0);
+ fd = adm_init(socket_path);
+ if (fd < 0)
+ return 1;
+
+ for (i = 1; i < argc; i++) {
+ if (adm_send_cmd(fd, argv[i]) < 0 || adm_recv(fd) < 0)
+ return 1;
+ }
+
+ close(fd);
+ return 0;
+}