From 41dec6436242c4daccdd9fdee3ac052004c439bd Mon Sep 17 00:00:00 2001
From: Laurent Bercot
Date: Mon, 16 Mar 2026 21:09:38 +0000
Subject: Refactor ftrig using sass; clean up API a bit, update its users
---
src/pipe-tools/s6-ftrig-wait.c | 62 +++++++++++++++++++++++++-----------------
1 file changed, 37 insertions(+), 25 deletions(-)
(limited to 'src/pipe-tools/s6-ftrig-wait.c')
diff --git a/src/pipe-tools/s6-ftrig-wait.c b/src/pipe-tools/s6-ftrig-wait.c
index d0e3a1a..87b573d 100644
--- a/src/pipe-tools/s6-ftrig-wait.c
+++ b/src/pipe-tools/s6-ftrig-wait.c
@@ -1,49 +1,61 @@
/* ISC license. */
+#include
#include
+#include
#include
+
#include
-#include
+#include
#include
#include
#include
+#include
+
#include
#define USAGE "s6-ftrig-wait [ -t timeout ] fifodir regexp"
+enum gola_e
+{
+ GOLA_TIMEOUT,
+ GOLA_N
+} ;
+
int main (int argc, char const *const *argv)
{
- tain deadline, tto ;
- ftrigr_t a = FTRIGR_ZERO ;
- uint16_t id ;
- char pack[2] = " \n" ;
+ static gol_arg const rgola[GOLA_N] =
+ {
+ { .so = 't', .lo = "timeout", .i = GOLA_TIMEOUT },
+ } ;
+ char const *wgola[GOLA_N] = { 0 } ;
+ tain deadline ;
+ tain tto = TAIN_INFINITE_RELATIVE ;
+ ftrigr a = FTRIGR_ZERO ;
+ uint32_t id ;
+ unsigned int golc ;
+ struct iovec v[2] = { [1] = { .iov_base = "\n", .iov_len = 1 } } ;
+
PROG = "s6-ftrig-wait" ;
+ golc = gol_main(argc, argv, 0, 0, rgola, GOLA_N, 0, wgola) ;
+ argc -= golc ; argv += golc ;
+ if (argc < 2) strerr_dieusage(100, USAGE) ;
+ if (wgola[GOLA_TIMEOUT])
{
unsigned int t = 0 ;
- for (;;)
- {
- int opt = lgetopt(argc, argv, "t:") ;
- if (opt == -1) break ;
- switch (opt)
- {
- case 't' : if (uint0_scan(subgetopt_here.arg, &t)) break ;
- default : strerr_dieusage(100, USAGE) ;
- }
- }
+ if (!uint0_scan(wgola[GOLA_TIMEOUT], &t))
+ strerr_dief1x(100, "timeout must be an unsigned integer") ;
if (t) tain_from_millisecs(&tto, t) ;
- else tto = tain_infinite_relative ;
- argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ;
}
- if (argc < 2) strerr_dieusage(100, USAGE) ;
- tain_now_set_stopwatch_g() ;
+ if (!tain_now_set_stopwatch_g()) strerr_diefu1sys(111, "tain_now") ;
tain_add_g(&deadline, &tto) ;
if (!ftrigr_startf_g(&a, &deadline)) strerr_diefu1sys(111, "ftrigr_startf") ;
- id = ftrigr_subscribe_g(&a, argv[0], argv[1], 0, &deadline) ;
- if (!id) strerr_diefu4sys(111, "subscribe to ", argv[0], " with regexp ", argv[1]) ;
- if (ftrigr_wait_or_g(&a, &id, 1, &deadline, &pack[0]) == -1)
- strerr_diefu2sys((errno == ETIMEDOUT) ? 1 : 111, "match regexp on ", argv[1]) ;
- if (allwrite(1, pack, 2) < 2) strerr_diefu1sys(111, "write to stdout") ;
- return 0 ;
+ if (!ftrigr_subscribe_g(&a, &id, 0, 0, argv[0], argv[1], &deadline))
+ strerr_diefu4sys(111, "subscribe to ", argv[0], " with regexp ", argv[1]) ;
+ if (ftrigr_wait_or_g(&a, &id, 1, &v[0], &deadline) == -1)
+ strerr_diefu2sys((errno == ETIMEDOUT) ? 99 : 111, "match regexp on ", argv[1]) ;
+ if (allwritev(1, v, 2) < siovec_len(v, 2)) strerr_diefu1sys(111, "write to stdout") ;
+ _exit(0) ;
}
--
cgit v1.3.1
From 24637cd3776cda5fd0a919ba9343ba86d82d3e04 Mon Sep 17 00:00:00 2001
From: Laurent Bercot
Date: Wed, 18 Mar 2026 01:57:01 +0000
Subject: More fixes, ftrigr_string addition, doc update
---
INSTALL | 2 +-
NEWS | 13 ++++
doc/index.html | 6 +-
doc/libs6/ftrigr.html | 132 +++++++++++++++++--------------------
doc/libs6/s6-ftrigrd.html | 37 +++++------
doc/s6-setsid.html | 3 +-
doc/upgrade.html | 17 +++++
package/deps.mak | 9 ++-
src/include/s6/ftrigr.h | 13 ++--
src/libs6/deps-lib/s6 | 1 -
src/libs6/ftrigr_peek.c | 7 +-
src/libs6/ftrigr_peek1.c | 14 ----
src/libs6/ftrigr_wait_and.c | 4 +-
src/libs6/ftrigr_wait_or.c | 5 +-
src/pipe-tools/s6-ftrig-listen.c | 4 +-
src/pipe-tools/s6-ftrig-listen1.c | 6 +-
src/pipe-tools/s6-ftrig-wait.c | 6 +-
src/supervision/s6-notifyoncheck.c | 6 +-
src/supervision/s6_svlisten_loop.c | 13 ++--
19 files changed, 151 insertions(+), 147 deletions(-)
delete mode 100644 src/libs6/ftrigr_peek1.c
(limited to 'src/pipe-tools/s6-ftrig-wait.c')
diff --git a/INSTALL b/INSTALL
index dd6bdaf..2cb405e 100644
--- a/INSTALL
+++ b/INSTALL
@@ -6,7 +6,7 @@ Build Instructions
- A POSIX-compliant C development environment
- GNU make version 3.81 or later
- - skalibs version 2.14.5.1 or later: https://skarnet.org/software/skalibs/
+ - skalibs version 2.14.6.0 or later: https://skarnet.org/software/skalibs/
- Recommended: execline version 2.9.8.1 or later: https://skarnet.org/software/execline/
(You can disable this requirement at configure time, but will
lose some functionality.)
diff --git a/NEWS b/NEWS
index 061674f..a2f6592 100644
--- a/NEWS
+++ b/NEWS
@@ -1,5 +1,18 @@
Changelog for s6.
+In 2.15.0.0
+-----------
+
+ - Bugfixes.
+ - s6-svscan's scandir argument is now mandatory.
+ - s6-setsid now autodetects its controlling terminal by default.
+ - Complete ftrigr refactor. This doesn't impact CLI programs,
+except the next point.
+ - s6-ftrig-wait and s6-ftrig-listen1 now print the whole
+sequence of events (followed by a newline) to stdout, rather than
+just the triggering event.
+
+
In 2.14.0.1
-----------
diff --git a/doc/index.html b/doc/index.html
index 1baa6ab..925661e 100644
--- a/doc/index.html
+++ b/doc/index.html
@@ -83,7 +83,7 @@ with s6
A POSIX-compliant system with a standard C development environment
GNU make, version 3.81 or later
skalibs version
-2.14.5.1 or later. It's a build-time requirement. It's also a run-time
+2.14.6.0 or later. It's a build-time requirement. It's also a run-time
requirement if you link against the shared version of the skalibs
library.
(Optional, but really recommended for full functionality):
@@ -115,8 +115,8 @@ want nsswitch-like functionality:
Download
- - The current released version of s6 is 2.14.0.1.
-You can access its checksum here.
+ - The current released version of s6 is 2.15.0.0.
+You can access its checksum here.
- Alternatively, you can checkout a copy of the
s6
git repository:
diff --git a/doc/libs6/ftrigr.html b/doc/libs6/ftrigr.html
index 052d7bf..a8cf98b 100644
--- a/doc/libs6/ftrigr.html
+++ b/doc/libs6/ftrigr.html
@@ -43,11 +43,6 @@ and to always use wait_nohang() to reap children,
simply ignoring pids you don't know.
-
- If your application has trouble handling unknown
-children, consider using an ftrigrd service. (And fix your application!)
-
-
A programming example
@@ -61,8 +56,8 @@ for instance, illustrate how to use the ftrigr library.
- - Synchronous functions take a tain_t const *
-(deadline) parameter and a tain_t * (stamp)
+
- Synchronous functions take a tain const *
+(deadline) parameter and a tain * (stamp)
parameter. Those are pointers to tain_t structures containing absolute times;
the former represents a deadline (in most cases, this time will be in the
future) and the latter must be an accurate enough timestamp. These
@@ -100,28 +95,25 @@ control on.
Starting and ending a session
-ftrigr_t a = FTRIGR_ZERO ;
+ftrigr a = FTRIGR_ZERO ;
tain_t deadline, stamp ;
tain_now(&stamp) ;
tain_addsec(&deadline, &stamp, 2)
-// char const *path = FTRIGR_IPCPATH ;
-// ftrigr_start(&a, path, &deadline, &stamp) ;
ftrigr_startf(&a, &deadline, &stamp) ;
-ftrigr_start starts a session with an ftrigrd service listening on
-path.
ftrigr_startf starts a session with an ftrigrd process as a child
(which is the simplest usage).
-a is an ftrigr_t structure that must be declared in the stack and
-initialized to FTRIGR_ZERO.
+a is an ftrigr structure that can be declared in the stack and
+must be initialized to FTRIGR_ZERO.
stamp must be an accurate enough timestamp.
If the session initialization fails, the function returns 0 and errno is set;
else the function returns 1.
+
If the absolute time deadline is reached and the function
has not returned yet, it immediately returns 0 with errno set to ETIMEDOUT.
@@ -135,8 +127,8 @@ problem with the underlying processes.
You can have more than one session open in parallel, by declaring
-several distinct ftrigr_t structures and calling
-ftrigr_startf (or ftrigr_start) more than once.
+several distinct ftrigr structures and calling
+ftrigr_startf more than once.
However, this is useless, since one single session can handle
virtually as many concurrent fifodirs as your application needs.
@@ -156,8 +148,10 @@ ftrigr_end(&a) ;
char const *path = "/var/lib/myservice/fifodir" ;
char const *re = "a.*b|c*d" ;
uint32_t options = 0 ;
+uint32_t timeout = 60000 ;
+uint32_t id ;
-uint16_t id = ftrigr_subscribe (&a, path, re, options, &deadline, &stamp) ;
+int r = ftrigr_subscribe(&a, &id, options, timeout, path, re, options, &deadline, &stamp) ;
@@ -165,7 +159,9 @@ uint16_t id = ftrigr_subscribe (&a, path, re, options, &deadline, &s
s6-ftrigrd daemon, related to the open
session represented by the a structure, to subscribe to the
path fifodir, and to notify the application when it receives
-a series of events that matches the re regexp.
+a series of events that matches the re regexp.
+
+
options can be 0 or FTRIGR_REPEAT. If it is 0, the daemon will
automatically unsubscribe from path once re has been
matched by a series of events. If it is FTRIGR_REPEAT, it will remain
@@ -173,8 +169,16 @@ subscribed until told otherwise.
- ftrigr_subscribe() returns 0 and sets errno in case of failure, or
-a nonzero 16-bit number identifying the subscription in case of success.
+ If timeout is nonzero, it represents a number of milliseconds;
+after this delay, the daemon will automatically unsubscribe from
+path and report an ETIMEDOUT error to the client. It is not
+advised to use a nonzero timeout along with the FTRIGR_REPEAT option.
+
+
+
+ If it fails, ftrigr_subscribe() returns 0 and sets errno. If
+it succeeds, it returns 1 and stores a number identifying the subscription
+into id.
@@ -188,26 +192,29 @@ and events can be sent without the risk of a race condition.
Synchronously waiting for events
-uint16_t list[1] ;
+uint32_t list[1] = { id } ;
unsigned int n = 1 ;
-char trigger ;
-list[0] = id ;
+ftrigr_string fs ;
// r = ftrigr_wait_and(&a, list, n, &deadline, &stamp) ;
-r = ftrigr_wait_or(&a, list, n, &deadline, &stamp, &trigger) ;
+r = ftrigr_wait_or(&a, list, n, &deadline, &stamp, &fs) ;
ftrigr_wait_and() waits for all the n fifodirs
whose ids are listed in list to receive an event. It returns -1
-in case of error or timeout, or a non-negative integer in case of success.
+in case of error or timeout, or a non-negative integer in case of success.
+
+
+
ftrigr_wait_or() waits for one of the n fifodirs
whose ids are listed in list to receive an event. It returns -1
in case of error or timeout; if it succeeds, the return value is the
position in list, starting at 0, of the identifier that received
-an event; and trigger is set to the character that triggered that
-event, i.e. the last character of a sequence that matched the regular
-expression re used in the subscription.
+an event; and fs is the list of events that were received
+since the subscription and matched the re regular expression.
+fs.s is a char * pointing to the (not null-terminated)
+string of events, and fs.len is its length.
Asynchronously waiting for events
@@ -218,7 +225,7 @@ of usage examples.)
-int ftrigr_fd (ftrigr_t const *a)
+int ftrigr_fd (ftrigr const *a)
@@ -227,30 +234,19 @@ int ftrigr_fd (ftrigr_t const *a)
-int ftrigr_updateb (ftrigr_t *a)
+int ftrigr_update (ftrigr *a)
Call this function whenever the fd checks readability: it will
update a's internal structures with information from the
-s6-ftrigrd daemon. It returns -1 if an error
-occurs; in case of success, it returns the number of identifiers for
-which something happened.
-
-
-
- When ftrigr_updateb returns,
-genalloc_s(uint16_t, &a->list) points to an array of
-genalloc_len(uint16_t, &a->list) 16-bit unsigned
-integers. Those integers are ids waiting to be passed to
-ftrigr_check or ftrigr_checksa.
-The number of ids already acknowledged is stored in
-a->head, so the first unacknowledged id is
-genalloc_s(uint16_t, &a->list)[a->head].
+s6-ftrigrd daemon. It returns -1 (and sets
+errno) if an error occurs, 0 if there were no events, and 1 if events
+were received.
-int ftrigr_check (ftrigr_t *a, uint16_t id, char *what)
+int ftrigr_peek (ftrigr *a, uint32_t id, ftrigr_string *fs)
@@ -263,49 +259,39 @@ call to ftrigr_updateb().
number may have been transmitted from
s6-ftrigrd.
If no notification happened yet, returns 0.
- If something happened, writes the character that triggered the
-latest notification into what and returns the number of
-times that an event happened to this identifier since the last
-call to ftrigr_check().
+ If something happened, returns 1, and fs contains
+the string of events that were received since the subscription or
+since the last call to ftrigr_ack (see below).
+fs->s is a pointer to the non-null-terminated string,
+and fs->len is its length.
-int ftrigr_checksa (ftrigr_t *a, uint16_t id, stralloc *what)
+void ftrigr_ack (ftrigr *a, uint32_t id)
- Checks whether an event happened to id. Use after a
-call to ftrigr_update(), as an alternative to ftrigr_check().
+ Resets the stored string of events. The next invocation of
+ftrigr_peek() will only show new events, if any.
-
- - If an error occurred, returns -1 and sets errno. The error
-number may have been transmitted from
-s6-ftrigrd.
- - If no notification happened yet, returns 0.
- - If something happened, appends one character to the end of the what
-stralloc
-for every time a notification was triggered since the last call
-to ftrigr_check(). Each character is the one that triggered
-a notification. The function then returns 1.
-
-
-void ftrigr_ack (ftrigr_t *a, size_t n)
+int ftrigr_release (ftrigr *a, uint32_t id)
- Acknowledges reading n ids from the id list updated by
-ftrigr_updateb.
+ Frees the resources used by subscription id. Use this
+after getting an event from a subscription done without the
+FTRIGR_REPEAT flag and reading its results, if you want to
+keep the session open and perform more subscriptions.
-
-int ftrigr_update (ftrigr_t *a)
-
-
- Acknowledges all the pending ids (i.e. clears the stored id list)
-then calls ftrigr_updateb().
+ If subscription id was given the FTRIGR_REPEAT flag,
+use ftrigr_unsubscribe() instead. If you're not going
+to perform other subscriptions, ftrigr_end() will
+free all the resources without you needing to call
+ftrigr_release() first.