commit bcfc411bb8f60ca18b84d9b08e6e02fe31adf7cc
parent 9f0138605a66e66d2784f4e4a42b7c5ccfa1d44a
Author: cowmonk <cowmonk@based.pt>
Date: Tue, 10 Nov 2026 08:02:56 -0700
WIP iwctl client
Diffstat:
10 files changed, 326 insertions(+), 14 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -33,6 +33,7 @@ src/iwd.config.5
src/iwd.network.5
src/iwd.service
client/iwctl
+ctl/iwctl
client/iwctl.1
monitor/iwmon
monitor/iwmon.1
diff --git a/Makefile b/Makefile
@@ -1,6 +1,6 @@
.POSIX:
-all: src/iwd
+all: src/iwd ctl/iwctl
include config.mk
include mk/sources.mk
@@ -9,9 +9,11 @@ BUILD_CPPFLAGS = -I$(ELL_DIR) -I. -DHAVE_CONFIG_H \
-Dtypeof=__typeof__ \
-DVERSION='"$(VERSION)"' -DDAEMON_CONFIGDIR='"$(CONFIGDIR)"' \
-DDAEMON_STORAGEDIR='"$(STORAGEDIR)"' \
+ -DCONTROL_SOCKET_PATH='"$(CONTROLSOCKET)"' \
-DUNITDIR='"unit/"' -DCERTDIR='"unit/"' $(CPPFLAGS)
BUILD_CFLAGS = -std=c99 -fvisibility=hidden $(OPT) $(CFLAGS)
-IWD_OBJ = $(IWD_SRC:.c=.o)
+IWD_OBJ = $(IWD_SRC:.c=.o) src/control.o
+CTL_OBJ = ctl/main.o
MONITOR_OBJ = $(MONITOR_SRC:.c=.o)
MANPAGES = src/iwd.8 src/iwd.debug.7 src/iwd.config.5 \
src/iwd.network.5 src/iwd.ap.5 monitor/iwmon.1
@@ -24,7 +26,9 @@ CERTS = unit/cert-ca.pem unit/cert-server.pem \
.c.o:
$(CC) $(BUILD_CPPFLAGS) $(BUILD_CFLAGS) -c -o $@ $<
-$(IWD_OBJ) $(MONITOR_OBJ) $(TEST_OBJ): Makefile config.h config.mk mk/sources.mk $(HEADERS) $(ELL_HEADERS) $(ELL_DIR)/ell/ell.h
+$(IWD_OBJ) $(CTL_OBJ) $(MONITOR_OBJ) $(TEST_OBJ): Makefile config.h config.mk mk/sources.mk $(HEADERS) $(ELL_HEADERS) $(ELL_DIR)/ell/ell.h
+
+src/main.o src/control.o ctl/main.o: src/control.h shared/control.h
$(ELL_OBJ): Makefile config.h config.mk mk/sources.mk $(ELL_HEADERS) $(ELL_DIR)/ell/ell.h
mkdir -p build/ell
@@ -38,6 +42,9 @@ libell.a: $(ELL_OBJ)
src/iwd: $(IWD_OBJ) libell.a
$(CC) $(LDFLAGS) -o $@ $(IWD_OBJ) libell.a $(LDLIBS)
+ctl/iwctl: $(CTL_OBJ) libell.a
+ $(CC) $(LDFLAGS) -o $@ $(CTL_OBJ) libell.a $(LDLIBS)
+
monitor: monitor/iwmon
monitor/iwmon: $(MONITOR_OBJ) libell.a
@@ -70,7 +77,8 @@ man: $(MANPAGES)
install: all
mkdir -p $(DESTDIR)$(BINDIR) $(DESTDIR)$(CONFIGDIR) $(DESTDIR)$(STORAGEDIR)
cp src/iwd $(DESTDIR)$(BINDIR)/iwd
- chmod 755 $(DESTDIR)$(BINDIR)/iwd
+ cp ctl/iwctl $(DESTDIR)$(BINDIR)/iwctl
+ chmod 755 $(DESTDIR)$(BINDIR)/iwd $(DESTDIR)$(BINDIR)/iwctl
chmod 700 $(DESTDIR)$(STORAGEDIR)
install-monitor: monitor
@@ -88,7 +96,7 @@ install-man: man
done
uninstall:
- rm -f $(DESTDIR)$(BINDIR)/iwd $(DESTDIR)$(BINDIR)/iwmon
+ rm -f $(DESTDIR)$(BINDIR)/iwd $(DESTDIR)$(BINDIR)/iwctl $(DESTDIR)$(BINDIR)/iwmon
for page in $(MANPAGES); do \
section=$$(printf '%s\n' "$$page" | sed 's/.*\.//'); \
name=$$(basename "$$page"); \
@@ -96,6 +104,6 @@ uninstall:
done
clean:
- rm -f $(IWD_OBJ) $(MONITOR_OBJ) $(TEST_OBJ) $(ELL_OBJ) libell.a \
- src/iwd monitor/iwmon $(TESTS) $(MANPAGES) \
+ rm -f $(IWD_OBJ) $(CTL_OBJ) $(MONITOR_OBJ) $(TEST_OBJ) $(ELL_OBJ) libell.a \
+ src/iwd ctl/iwctl monitor/iwmon $(TESTS) $(MANPAGES) \
unit/cert-*.pem unit/cert-*.csr unit/cert-*.srl unit/tls-settings.8021x
diff --git a/README b/README
@@ -83,12 +83,7 @@ The check runner validates TAP output as well as process exit status.
Current control support
-----------------------
-There is no D-Bus build switch in the supported Makefile, and no iwctl
-target yet. Networks are configured through iwd's existing profile files.
-The daemon still contains upstream D-Bus helpers, and ELL's D-Bus objects
-are compiled to satisfy remaining internal references. No external D-Bus
-library or running bus is required. Removing those internal references
-and adding a local socket client are a future milestone for this project.
+Read ctl/README.
Upstream updates
----------------
diff --git a/config.mk b/config.mk
@@ -4,6 +4,7 @@ BINDIR = $(PREFIX)/bin
MANDIR = $(PREFIX)/share/man
CONFIGDIR = /etc/iwd
STORAGEDIR = /var/lib/iwd
+CONTROLSOCKET = /run/eiwd.sock
# Command-line assignments override these settings. CFLAGS, CPPFLAGS and
# LDFLAGS can also be supplied through the environment.
diff --git a/ctl/README b/ctl/README
@@ -0,0 +1,48 @@
+better* iwctl
+=============
+
+After wracking my brain over how I'm going to do this, I'm just going to
+literally wing it.
+
+This is the first piece of the new iwctl. It connects to iwd over a local
+Unix socket, sends ping!, and expects pong! back. On success it prints:
+
+ ping! pong!
+
+Start the daemon in one terminal (as root):
+
+ ./src/iwd
+
+Then run the client in another (also as root):
+
+ ./ctl/iwctl
+
+The default socket is /run/eiwd.sock, set by CONTROLSOCKET in config.mk.
+IWD_CONTROL_SOCKET overrides the path at runtime; set the same value for
+both processes. The socket is created with mode 0600, so the client needs
+to run as its owner, normally root (sorry). The exchange uses SOCK_SEQPACKET.
+It allows 16 active clients since idk, one of these days someone will need
+that I suppose.
+
+Source map, relative to the project root for any contributors:
+
+ ctl/main.c client connection, exchange, and result
+ shared/control.h protocol definitions (very large tmb)
+ src/control.c listener and client handling in ELL's event loop
+ src/control.h control_init() and control_exit()
+ src/main.c starts and stops the listener with iwd
+
+Normal shutdown removes the socket. Existing paths are never overwritten;
+a stale socket left by a crash needs manual removal after confirming that
+the old daemon is no longer running. TL;DR just your regular dameon/client
+socket stuffs.
+
+*footnote: ahem, the author (me) makes no guarantees that this is going to
+be good.
+
+TODOs (in no particular order)
+------------------------------
+ - ability to get devices (probably first thing)
+ - list networks && scanning
+ - connect to WPA/psk network
+ - misc options and commands
diff --git a/ctl/main.c b/ctl/main.c
@@ -0,0 +1,63 @@
+#include "config.h"
+#include "shared/control.h"
+
+#include <ell/ell.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <sys/un.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ struct sockaddr_un address = { .sun_family = AF_UNIX };
+ struct timeval timeout = { .tv_sec = 3 };
+ const char *path = getenv("IWD_CONTROL_SOCKET");
+ char reply[sizeof(CONTROL_PONG)];
+ ssize_t len;
+ int fd;
+
+ l_log_set_stderr();
+ l_log_set_ident("iwctl");
+ if (!path)
+ path = CONTROL_SOCKET_PATH;
+ if (!*path || strlen(path) >= sizeof(address.sun_path)) {
+ l_error("Invalid socket path");
+ return EXIT_FAILURE;
+ }
+ memcpy(address.sun_path, path, strlen(path) + 1);
+ fd = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0);
+ if (fd < 0)
+ goto error;
+ if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0 ||
+ setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) < 0 ||
+ connect(fd, (struct sockaddr *)&address, sizeof(address)) < 0)
+ goto error;
+ do {
+ len = send(fd, CONTROL_PING, sizeof(CONTROL_PING) - 1, MSG_NOSIGNAL);
+ } while (len < 0 && errno == EINTR);
+ if (len < 0)
+ goto error;
+ do {
+ len = recv(fd, reply, sizeof(reply), MSG_TRUNC);
+ } while (len < 0 && errno == EINTR);
+ if (len < 0)
+ goto error;
+ if (len != sizeof(CONTROL_PONG) - 1 ||
+ memcmp(reply, CONTROL_PONG, sizeof(CONTROL_PONG) - 1)) {
+ errno = EPROTO;
+ goto error;
+ }
+ close(fd);
+ return puts("ping! pong!") == EOF ? EXIT_FAILURE : EXIT_SUCCESS;
+
+error:
+ l_error("%s: %s", path, strerror(errno));
+ if (fd >= 0)
+ close(fd);
+ return EXIT_FAILURE;
+}
diff --git a/shared/control.h b/shared/control.h
@@ -0,0 +1,8 @@
+#ifndef EIWD_CONTROL_PROTOCOL_H
+#define EIWD_CONTROL_PROTOCOL_H
+
+/* very extensive and feature complete shared protocol */
+#define CONTROL_PING "ping!"
+#define CONTROL_PONG "pong!"
+
+#endif
diff --git a/src/control.c b/src/control.c
@@ -0,0 +1,169 @@
+#include "config.h"
+#include "src/control.h"
+#include "shared/control.h"
+
+#include <ell/ell.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/un.h>
+#include <unistd.h>
+
+struct ControlClient {
+ struct l_io *io;
+ struct l_timeout *timeout;
+};
+
+static struct l_io *listener;
+static struct l_queue *clients;
+static char *socket_path;
+
+static void
+client_free(void *data)
+{
+ struct ControlClient *client = data;
+
+ l_io_set_disconnect_handler(client->io, NULL, NULL, NULL);
+ l_io_destroy(client->io);
+ l_timeout_remove(client->timeout);
+ l_free(client);
+}
+
+static void
+client_disconnect(struct l_io *io, void *data)
+{
+ l_queue_remove(clients, data);
+ client_free(data);
+}
+
+static void
+client_timeout(struct l_timeout *timeout, void *data)
+{
+ l_queue_remove(clients, data);
+ client_free(data);
+}
+
+static bool
+client_read(struct l_io *io, void *data)
+{
+ char request[sizeof(CONTROL_PING)];
+ ssize_t len;
+ int fd = l_io_get_fd(io);
+
+ len = recv(fd, request, sizeof(request), MSG_TRUNC);
+ if (len < 0 && (errno == EINTR || errno == EAGAIN))
+ return true;
+ if (len == sizeof(CONTROL_PING) - 1 &&
+ !memcmp(request, CONTROL_PING, len)) {
+ do {
+ len = send(fd, CONTROL_PONG, sizeof(CONTROL_PONG) - 1,
+ MSG_NOSIGNAL);
+ } while (len < 0 && errno == EINTR);
+ }
+
+ /* Let the disconnect handler release this client's watch. */
+ shutdown(fd, SHUT_RDWR);
+ return false;
+}
+
+static bool
+accept_client(struct l_io *io, void *data)
+{
+ struct ControlClient *client;
+ int fd;
+
+ fd = accept4(l_io_get_fd(io), NULL, NULL, SOCK_CLOEXEC | SOCK_NONBLOCK);
+ if (fd < 0)
+ return true;
+ if (l_queue_length(clients) >= 16) {
+ close(fd);
+ return true;
+ }
+
+ client = l_new(struct ControlClient, 1);
+ client->io = l_io_new(fd);
+ if (!client->io) {
+ close(fd);
+ l_free(client);
+ return true;
+ }
+ l_io_set_close_on_destroy(client->io, true);
+ if (!l_io_set_read_handler(client->io, client_read, client, NULL)) {
+ client_free(client);
+ return true;
+ }
+ l_io_set_disconnect_handler(client->io, client_disconnect, client, NULL);
+ client->timeout = l_timeout_create(5, client_timeout, client, NULL);
+ if (!client->timeout) {
+ client_free(client);
+ return true;
+ }
+ l_queue_push_tail(clients, client);
+ return true;
+}
+
+int
+control_init(const char *path)
+{
+ struct sockaddr_un address = { .sun_family = AF_UNIX };
+ mode_t mask;
+ int fd, err;
+
+ if (listener)
+ return -EALREADY;
+ if (!*path)
+ return -EINVAL;
+ if (strlen(path) >= sizeof(address.sun_path))
+ return -ENAMETOOLONG;
+ memcpy(address.sun_path, path, strlen(path) + 1);
+ fd = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
+ if (fd < 0)
+ return -errno;
+
+ mask = umask(0177);
+ err = bind(fd, (struct sockaddr *)&address, sizeof(address));
+ if (err < 0)
+ err = -errno;
+ umask(mask);
+ if (err < 0)
+ goto close_fd;
+ if (listen(fd, 16) < 0) {
+ err = -errno;
+ goto unlink_socket;
+ }
+ listener = l_io_new(fd);
+ if (!listener) {
+ err = -EIO;
+ goto unlink_socket;
+ }
+ l_io_set_close_on_destroy(listener, true);
+ if (!l_io_set_read_handler(listener, accept_client, NULL, NULL)) {
+ l_io_destroy(listener);
+ listener = NULL;
+ unlink(path);
+ return -EIO;
+ }
+ clients = l_queue_new();
+ socket_path = l_strdup(path);
+ return 0;
+
+unlink_socket:
+ unlink(path);
+close_fd:
+ close(fd);
+ return err;
+}
+
+void
+control_exit(void)
+{
+ l_io_destroy(listener);
+ listener = NULL;
+ l_queue_destroy(clients, client_free);
+ clients = NULL;
+ if (socket_path)
+ unlink(socket_path);
+ l_free(socket_path);
+ socket_path = NULL;
+}
diff --git a/src/control.h b/src/control.h
@@ -0,0 +1,7 @@
+#ifndef EIWD_CONTROL_H
+#define EIWD_CONTROL_H
+
+int control_init(const char *path);
+void control_exit(void);
+
+#endif
diff --git a/src/main.c b/src/main.c
@@ -51,6 +51,7 @@
#include "src/anqp.h"
#include "src/netconfig.h"
#include "src/crypto.h"
+#include "src/control.h"
#include "src/backtrace.h"
@@ -471,8 +472,9 @@ int main(int argc, char *argv[])
int exit_status;
struct l_dbus *dbus;
const char *config_dir;
+ const char *control_path;
char **config_dirs;
- int i;
+ int i, err;
for (;;) {
int opt;
@@ -616,8 +618,18 @@ int main(int argc, char *argv[])
if (!setup_system_key())
goto failed_storage;
+ control_path = getenv("IWD_CONTROL_SOCKET");
+ if (!control_path)
+ control_path = CONTROL_SOCKET_PATH;
+ err = control_init(control_path);
+ if (err < 0) {
+ l_error("Unable to listen on %s: %s", control_path, strerror(-err));
+ goto failed_storage;
+ }
+
exit_status = l_main_run_with_signal(signal_handler, NULL);
+ control_exit();
iwd_modules_exit();
storage_exit();