patches and low-level development discussion
 help / color / mirror / code / Atom feed
blob 734f018da605e4359d3fa6422a01effbd9e878ea 2697 bytes (raw)
name: tools/sd-notify-adapter/sd-notify-adapter.c 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
 
// SPDX-License-Identifier: ISC
// SPDX-FileCopyrightText: 2025 Demi Marie Obenour <demiobenour@gmail.com>

#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <err.h>
#include <fcntl.h>
#include <poll.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>

#define ARRAY_SIZE(s) (sizeof(s)/sizeof(s[0]))

enum {
	socket_fd,
	notification_fd,
};

struct iovec msg;

#define READY "READY=1"
#define READY_SIZE (sizeof(READY) - 1)

static void process_notification(void)
{
	ssize_t first_recv_size = recv(socket_fd, msg.iov_base, msg.iov_len,
	                               MSG_TRUNC | MSG_PEEK);
	if (first_recv_size == -1) {
		if (errno == EINTR)
			return; // signal caught
		err(EXIT_FAILURE, "recv from notification socket");
	}
	size_t size = (size_t)first_recv_size;
	if (size == 0)
		return; // avoid arithmetic on NULL pointer
	if (size > msg.iov_len) {
		msg.iov_base = realloc(msg.iov_base, size);
		if (msg.iov_base == NULL)
			err(EXIT_FAILURE, "allocation failure");
		msg.iov_len = size;
	}
	ssize_t second_recv_size = recv(socket_fd, msg.iov_base, msg.iov_len,
	                                MSG_CMSG_CLOEXEC | MSG_TRUNC);
	if (second_recv_size == -1) {
		if (errno == EINTR)
			return;
		err(EXIT_FAILURE, "recv from notification socket");
	}
	assert(first_recv_size == second_recv_size);
	for (char *next, *cursor = msg.iov_base, *end = cursor + size;
	     cursor != NULL; cursor = (next == NULL ? NULL : next + 1)) {
		next = memchr(cursor, '\n', (size_t)(end - cursor));
		size_t message_size = (size_t)((next == NULL ? end : next) - cursor);
		if (message_size == READY_SIZE &&
		    memcmp(cursor, READY, READY_SIZE) == 0) {
			ssize_t write_size = write(notification_fd, "\n", 1);
			if (write_size != 1)
				err(EXIT_FAILURE, "writing to notification descriptor");
			exit(0);
		}
	}
}

int main(int argc, char **)
{
	if (argc != 1)
		errx(EXIT_FAILURE, "stdin is listening socket, stdout is notification pipe");
	for (;;) {
		struct pollfd p[] = {
			{
				.fd = socket_fd,
				.events = POLLIN,
				.revents = 0,
			},
			{
				.fd = notification_fd,
				.events = 0,
				.revents = 0,
			},
		};
		int r = poll(p, ARRAY_SIZE(p), -1);
		if (r < 0) {
			if (errno == EINTR)
				continue;
			err(EXIT_FAILURE, "poll");
		}
		if (p[0].revents) {
			if (p[0].revents & POLLERR)
				errx(EXIT_FAILURE, "unexpected POLLERR");
			if (p[0].revents & POLLIN)
				process_notification();
		}
		if (p[1].revents)
			errx(EXIT_FAILURE, "s6 closed its pipe before the child was ready");
	}
}

debug log:

solving 734f018da605e4359d3fa6422a01effbd9e878ea ...
found 734f018da605e4359d3fa6422a01effbd9e878ea in https://inbox.spectrum-os.org/spectrum-devel/20251107-udev-v6-1-176246281424@gmail.com/ ||
	https://inbox.spectrum-os.org/spectrum-devel/20251103-udev-v6-1-704649b85d6d@gmail.com/ ||
	https://inbox.spectrum-os.org/spectrum-devel/20251103-udev-v5-1-5c432ef1ddf8@gmail.com/

applying [1/1] https://inbox.spectrum-os.org/spectrum-devel/20251107-udev-v6-1-176246281424@gmail.com/
diff --git a/tools/sd-notify-adapter/sd-notify-adapter.c b/tools/sd-notify-adapter/sd-notify-adapter.c
new file mode 100644
index 0000000000000000000000000000000000000000..734f018da605e4359d3fa6422a01effbd9e878ea

Checking patch tools/sd-notify-adapter/sd-notify-adapter.c...
Applied patch tools/sd-notify-adapter/sd-notify-adapter.c cleanly.

skipping https://inbox.spectrum-os.org/spectrum-devel/20251103-udev-v6-1-704649b85d6d@gmail.com/ for 734f018da605e4359d3fa6422a01effbd9e878ea
skipping https://inbox.spectrum-os.org/spectrum-devel/20251103-udev-v5-1-5c432ef1ddf8@gmail.com/ for 734f018da605e4359d3fa6422a01effbd9e878ea
index at:
100644 734f018da605e4359d3fa6422a01effbd9e878ea	tools/sd-notify-adapter/sd-notify-adapter.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://spectrum-os.org/git/crosvm
	https://spectrum-os.org/git/doc
	https://spectrum-os.org/git/mktuntap
	https://spectrum-os.org/git/nixpkgs
	https://spectrum-os.org/git/spectrum
	https://spectrum-os.org/git/ucspi-vsock
	https://spectrum-os.org/git/www

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).