Skip to contentSkip to conversation

alfred: notify event listener via unix socket

PATCH Superseded Marek Lindner 2 participants 4 messages Apr 30, 2022 – May 1, 2022

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 alfred: notify event listener via unix socket

The alfred server instance accepts event notification registration via the unix socket. These notification sockets only inform registered parties of the availibility of an alfred datatype change. The actual data itself needs to be retrieved via the existing data retrieval mechanisms.

Unlike the update-command this event monitor allows:

- multiple parallel listeners
- programmatic access to changes without requiring multiple processes

The alfred client allows to monitor events via the newly added '-E' (event monitor) command line option. Serving as debugging tool and example code at the same time.

---
 alfred.h     |  15 ++++++
 client.c     |  54 ++++++++++++++++++++
 main.c       |  10 +++-
 man/alfred.8 |   3 ++
 packet.h     |  26 ++++++++++
 recv.c       |   4 +-
 server.c     |   5 ++
 unix_sock.c  | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 257 insertions(+), 2 deletions(-)
alfred.h +15 -0
index 2d98a30..f442c48 100644
@@ -94,6 +94,7 @@ enum clientmode {
 	CLIENT_CHANGE_INTERFACE,
 	CLIENT_CHANGE_BAT_IFACE,
 	CLIENT_SERVER_STATUS,
+	CLIENT_EVENT_MONITOR,
 };
 
 struct interface {
@@ -110,8 +111,15 @@ struct interface {
 	struct list_head list;
 };
 
+struct event_listener {
+	int fd;
+
+	struct list_head list;
+};
+
 struct globals {
 	struct list_head interfaces;
+	struct list_head event_listeners;
 
 	char *net_iface;
 	struct server *best_server;	/* NULL if we are a server ourselves */
@@ -157,6 +165,7 @@ int alfred_client_modeswitch(struct globals *globals);
 int alfred_client_change_interface(struct globals *globals);
 int alfred_client_change_bat_iface(struct globals *globals);
 int alfred_client_server_status(struct globals *globals);
+int alfred_client_event_monitor(struct globals *globals);
 /* recv.c */
 int recv_alfred_packet(struct globals *globals, struct interface *interface,
 		       int recv_sock);
@@ -186,6 +195,12 @@ int unix_sock_open_client(struct globals *globals);
 int unix_sock_close(struct globals *globals);
 int unix_sock_req_data_finish(struct globals *globals,
 			      struct transaction_head *head);
+int unix_sock_events_select_prepare(struct globals *globals, fd_set *fds,
+				    fd_set *errfds, int maxsock);
+void unix_sock_events_select_handle(struct globals *globals,
+				    fd_set *fds, fd_set *errfds);
+void unix_sock_events_close_all(struct globals *globals);
+void unix_sock_event_notify(struct globals *globals, uint8_t type);
 /* vis.c */
 int vis_update_data(struct globals *globals);
 /* netsock.c */
client.c +54 -0

57 lines not on this page · raw message

main.c +9 -1
index 68d6efd..98bf64d 100644
@@ -39,6 +39,7 @@ static void alfred_usage(void)
 	printf("  -I, --change-interface [interface]  change to the specified interface(s)\n");
 	printf("  -B, --change-bat-iface [interface]  change to the specified batman-adv interface\n");
 	printf("  -S, --server-status                 request server status info such as mode & interfaces\n");
+	printf("  -E, --event-monitor                 monitor alfred data record update eventss\n");
 	printf("\n");
 	printf("server mode options:\n");
 	printf("  -i, --interface                     specify the interface (or comma separated list of interfaces) to listen on\n");
@@ -164,6 +165,7 @@ static struct globals *alfred_init(int argc, char *argv[])
 		{"change-interface",	required_argument,	NULL,	'I'},
 		{"change-bat-iface",	required_argument,	NULL,	'B'},
 		{"server-status",	required_argument,	NULL,	'S'},
+		{"event-monitor",	required_argument,	NULL,	'E'},
 		{"unix-path",		required_argument,	NULL,	'u'},
 		{"update-command",	required_argument,	NULL,	'c'},
 		{"version",		no_argument,		NULL,	'v'},
@@ -181,6 +183,7 @@ static struct globals *alfred_init(int argc, char *argv[])
 	memset(globals, 0, sizeof(*globals));
 
 	INIT_LIST_HEAD(&globals->interfaces);
+	INIT_LIST_HEAD(&globals->event_listeners);
 	globals->net_iface = NULL;
 	globals->opmode = OPMODE_SECONDARY;
 	globals->clientmode = CLIENT_NONE;
@@ -198,7 +201,7 @@ static struct globals *alfred_init(int argc, char *argv[])
 
 	time_random_seed();
 
-	while ((opt = getopt_long(argc, argv, "ms:r:hi:b:vV:M:I:B:Su:dc:p:4:f", long_options,
+	while ((opt = getopt_long(argc, argv, "ms:r:hi:b:vV:M:I:B:SEu:dc:p:4:f", long_options,
 				  &opt_ind)) != -1) {
 		switch (opt) {
 		case 'r':
@@ -263,6 +266,9 @@ static struct globals *alfred_init(int argc, char *argv[])
 		case 'S':
 			globals->clientmode = CLIENT_SERVER_STATUS;
 			break;
+		case 'E':
+			globals->clientmode = CLIENT_EVENT_MONITOR;
+			break;
 		case 'u':
 			globals->unix_path = optarg;
 			break;
@@ -328,6 +334,8 @@ int main(int argc, char *argv[])
 		return alfred_client_change_bat_iface(globals);
 	case CLIENT_SERVER_STATUS:
 		return alfred_client_server_status(globals);
+	case CLIENT_EVENT_MONITOR:
+		return alfred_client_event_monitor(globals);
 	}
 
 	return 0;
man/alfred.8 +3 -0
index cf0eafc..ca83f02 100644
@@ -97,6 +97,9 @@ Change the alfred server to use the new \fBbatman-adv interface\fP
 .TP
 \fB\-S\fP, \fB\-\-server\-status\fP
 Request server status information such as mode & interfaces\fP
+.TP
+\fB\-E\fP, \fB\-\-event\-monitor\fP
+Start alfred event monitor connecting to the alfred server and reporting update events\fP
 .
 .SH SERVER OPTIONS
 .TP
packet.h +26 -0
index 0c1a2eb..84b027f 100644
@@ -59,6 +59,8 @@ struct alfred_transaction_mgmt {
  * @ALFRED_STATUS_ERROR: Error was detected during the transaction
  * @ALFRED_MODESWITCH: Switch between different operation modes
  * @ALFRED_CHANGE_INTERFACE: Change the listening interface
+ * @ALFRED_EVENT_REGISTER: Request to be notified about alfred update events
+ * @ALFRED_EVENT_NOTIFY: Data record update has been received
  */
 enum alfred_packet_type {
 	ALFRED_PUSH_DATA = 0,
@@ -70,6 +72,8 @@ enum alfred_packet_type {
 	ALFRED_CHANGE_INTERFACE = 6,
 	ALFRED_CHANGE_BAT_IFACE = 7,
 	ALFRED_SERVER_STATUS = 8,
+	ALFRED_EVENT_REGISTER = 9,
+	ALFRED_EVENT_NOTIFY = 10,
 };
 
 /* packets */
@@ -229,6 +233,28 @@ struct alfred_server_status_rep_v0 {
 	struct alfred_tlv header;
 } __packed;
 
+/**
+ * struct alfred_event_register_v0 - event registration message
+ * @header: TLV header describing the complete packet
+ *
+ * Sent by the client to daemon to register for data record updates
+ */
+struct alfred_event_register_v0 {
+	struct alfred_tlv header;
+} __packed;
+
+/**
+ * struct alfred_event_notify_v0 - event notification message
+ * @header: TLV header describing the complete packet
+ * @type: Type of the data triggering the event
+ *
+ * Sent by the daemon to client on data record update
+ */
+struct alfred_event_notify_v0 {
+	struct alfred_tlv header;
+	uint8_t type;
+} __packed;
+
 /**
  * struct alfred_status_v0 - Status info of a transaction
  * @header: TLV header describing the complete packet
recv.c +3 -1
index 8acad10..36b3a49 100644
@@ -76,8 +76,10 @@ static int finish_alfred_push_data(struct globals *globals,
 		/* check that data was changed */
 		if (new_entry_created ||
 		    dataset->data.header.length != data_len ||
-		    memcmp(dataset->buf, data->data, data_len) != 0)
+		    memcmp(dataset->buf, data->data, data_len) != 0) {
 			changed_data_type(globals, data->header.type);
+			unix_sock_event_notify(globals, data->header.type);
+		}
 
 		/* free old buffer */
 		if (dataset->buf) {
server.c +5 -0
index bfc37bc..4078fe6 100644
@@ -442,6 +442,8 @@ int alfred_server(struct globals *globals)
 
 		maxsock = netsock_prepare_select(globals, &fds, maxsock);
 		maxsock = netsock_prepare_select(globals, &errfds, maxsock);
+		maxsock = unix_sock_events_select_prepare(globals, &fds,
+							  &errfds, maxsock);
 
 		ret = pselect(maxsock + 1, &fds, NULL, &errfds, &tv, NULL);
 
@@ -450,6 +452,8 @@ int alfred_server(struct globals *globals)
 		} else if (ret) {
 			netsock_check_error(globals, &errfds);
 
+			unix_sock_events_select_handle(globals, &fds, &errfds);
+
 			if (FD_ISSET(globals->unix_sock, &fds)) {
 				unix_sock_read(globals);
 				continue;
@@ -478,5 +482,6 @@ int alfred_server(struct globals *globals)
 
 	netsock_close_all(globals);
 	unix_sock_close(globals);
+	unix_sock_events_close_all(globals);
 	return 0;
 }
unix_sock.c +142 -0

179 lines not on this page · raw message

Signature · 1 line
2.32.0.rc0
#1 lore raw
Quoted 3 lines · On Saturday, 30 April 2022 12:56:47 CEST Marek Lindner wrote:

The alfred server instance accepts event notification registration via the unix socket. These notification sockets only inform registered parties of the availibility of an alfred datatype change.

availability

Quoted 2 lines
client.c +2 -0
+int alfred_client_event_monitor(struct globals *globals)
+{

[...]

client.c err: · line 502

Quoted 1 line
client.c +1 -0
+		fprintf(stdout, "Event: type = %d\n", event_notify.type);

event_notify.type is unsigned and not signed.

main.c static void alfred_usage(void) · line 42

Quoted 9 lines
main.c +1 -0
index 68d6efd..98bf64d 100644
@@ -39,6 +39,7 @@ static void alfred_usage(void)
 	printf("  -I, --change-interface [interface]  change to the specified interface(s)\n");
 	printf("  -B, --change-bat-iface [interface]  change to the specified batman-adv interface\n");
 	printf("  -S, --server-status                 request server status info such as mode & interfaces\n");
+	printf("  -E, --event-monitor                 monitor alfred data record update eventss\n");

events

main.c static struct globals *alfred_init(int argc, char *argv[]) · line 168

Quoted 8 lines
main.c +1 -0
 	printf("\n");
 	printf("server mode options:\n");
 	printf("  -i, --interface                     specify the interface (or comma separated list of interfaces) to listen on\n");
@@ -164,6 +165,7 @@ static struct globals *alfred_init(int argc, char *argv[])
 		{"change-interface",	required_argument,	NULL,	'I'},
 		{"change-bat-iface",	required_argument,	NULL,	'B'},
 		{"server-status",	required_argument,	NULL,	'S'},
+		{"event-monitor",	required_argument,	NULL,	'E'},

Why does it require an argument but the usage doesn't describe the argument? See also getopt_long which also doesn't expect an argument

Quoted 5 lines
main.c +1 -0
@@ -138,10 +140,17 @@ static int unix_sock_add_data(struct globals *globals,
 			free(dataset);
 			goto err;
 		}
+		new_entry_created = true;

main.c static int unix_sock_add_data(struct globals *globals, · line 153

Quoted 10 lines
main.c +6 -0
 	}
 	dataset->data_source = SOURCE_LOCAL;
 	clock_gettime(CLOCK_MONOTONIC, &dataset->last_seen);
 
+	/* check that data was changed */
+	if (new_entry_created ||
+	    dataset->data.header.length != data_len ||
+	    memcmp(dataset->buf, data->data, data_len) != 0)
+		unix_sock_event_notify(globals, data->header.type);
+

I am wondering now if it could be interesting for the listener whether the data is from us or some remote (for example via the source mac). Does anyone else have an opinion about that?

Quoted 14 lines
main.c +14 -0
+static int unix_sock_register_listener(struct globals *globals, int client_sock)
+{
+	struct event_listener *listener;
+	int ret;
+
+	ret = fcntl(client_sock, F_GETFL, 0);
+	if (ret < 0) {
+		perror("failed to get file status flags");
+		goto err;
+	}
+
+	ret = fcntl(client_sock, F_SETFL, ret | O_NONBLOCK);
+	if (ret < 0) {
+		perror("failed to set file status flags");

main.c static int unix_sock_add_data(struct globals *globals,

Quoted 10 lines
main.c +10 -0
+		goto err;
+	}
+
+	listener = malloc(sizeof(*listener));
+	if (!listener)
+		goto err;
+
+	listener->fd = client_sock;
+	INIT_LIST_HEAD(&listener->list);
+	list_add_tail(&listener->list, &globals->event_listeners);

INIT_LIST_HEAD (of the prev/next pointer) is not necessary when you just overwrite the next/prev pointer in the next line via list_add_tail

main.c

Quoted 6 lines
main.c +6 -0
+
+static void unix_sock_event_listener_free(struct event_listener *listener)
+{
+	list_del_init(&listener->list);
+	close(listener->fd);
+	free(listener);

list_del_init has no benefit (only downsides) when you free the memory anyway at the end of the function

Quoted 8 lines
main.c +8 -0
+int unix_sock_events_select_prepare(struct globals *globals, fd_set *fds,
+				    fd_set *errfds, int maxsock)
+{
+	struct event_listener *listener;
+
+	list_for_each_entry(listener, &globals->event_listeners, list) {
+		if (listener->fd < 0)
+			continue;

main.c

Quoted 10 lines
main.c +10 -0
+
+		FD_SET(listener->fd, fds);
+		FD_SET(listener->fd, errfds);
+
+		if (maxsock < listener->fd)
+			maxsock = listener->fd;
+	}
+
+	return maxsock;
+}

I should really rewrite the socket mainloop using epoll.... but this is just a side note and not a problem in your patch.

Kind regards,
Sven

#2 lore raw
Quoted 3 lines · On Sunday, 1 May 2022 09:54:26 CEST Sven Eckelmann wrote:

main.c static void alfred_usage(void)

Quoted 3 lines
main.c +0 -0
 	printf("\n");
 	printf("server mode options:\n");
 	printf("  -i, --interface                     specify the interface 

(or

Quoted 4 lines

Review static struct globals *alfred_init(int argc, char · line 166

Quoted 4 lines
(quoted diff) +0 -0
 	comma separated list of interfaces) to listen on\n");> 
@@ -164,6 +165,7 @@ static struct globals *alfred_init(int argc, char
 *argv[])> 
 		{"change-interface",	required_argument,	NULL,	

'I'},

Quoted 1 line

Review static struct globals *alfred_init(int argc, char · line 167

Quoted 1 line
(quoted diff) +0 -0
 		{"change-bat-iface",	required_argument,	NULL,	

'B'},

Quoted 5 lines

main.c static struct globals *alfred_init(int argc, char *argv[]) · line 168

Quoted 3 lines
main.c +1 -0
 		{"server-status",	required_argument,	NULL,	'S'},
 
+		{"event-monitor",	required_argument,	NULL,	'E'},

Why does it require an argument but the usage doesn't describe the argument?

Copy & paste error from server-status which does not require an argument either. Might need to fix in a separate patch.

Quoted 3 lines

I am wondering now if it could be interesting for the listener whether the data is from us or some remote (for example via the source mac). Does anyone else have an opinion about that?

Currently, no use-case comes to my mind.

Quoted 9 lines

unix_sock.c int unix_sock_close(struct globals *globals) · line 606

Quoted 6 lines
(quoted diff) +1 -0
+static void unix_sock_event_listener_free(struct event_listener

*listener)

unix_sock.c +4 -0
+{
+	list_del_init(&listener->list);
+	close(listener->fd);
+	free(listener);

list_del_init has no benefit (only downsides) when you free the memory anyway at the end of the function

What are those downsides you are referring to ?

Thanks for the review!

Kind regards,
Marek Lindner

#3 lore raw
Quoted 11 lines · On Sunday, 1 May 2022 11:10:13 CEST Marek Lindner wrote:
Quoted 9 lines

unix_sock.c int unix_sock_close(struct globals *globals) · line 606

Quoted 6 lines

+static void unix_sock_event_listener_free(struct event_listener *listener)

unix_sock.c +4 -0
+{
+	list_del_init(&listener->list);
+	close(listener->fd);
+	free(listener);

list_del_init has no benefit (only downsides) when you free the memory anyway at the end of the function

What are those downsides you are referring to ?

Additional writes for something which is dropped anyway.

Kind regards,
Sven