Skip to contentSkip to conversation

tools: new tool - gpioctl

RFC PATCH Superseded Bartosz Golaszewski 5 participants 11 messages Oct 16 – Oct 21

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover tools: new tool - gpioctl

Here's is something I've had in the works for about a year now. It's supposed to address the biggest issue with gpioset, namely the lack of persistence beyond the process' lifetime by working somewhat similarly to tmux: spawning a daemon on first invocation that retains the state for a particular user. This daemon listens on a unix socket and can handle clients that are different processes executed over time.

It's finally in a state where I'm happy showing it to others, so please take a look.

--

Provide gpioctl, a new command-line tool for persistent GPIO line control. Unlike gpioget and gpioset which release lines on exit, gpioctl holds requested lines in a background daemon so their state survives across multiple client invocations.

Before adding the new program: move some code we'll reuse out of gpioset and gpiomon and into tools-common.

---
Bartosz Golaszewski (3):
tools: factor out line value parsing into tools-common
tools: factor out line event formatting into tools-common
tools: implement gpioctl

 TODO                        |   14 -
 docs/.gitignore             |    2 +
 docs/conf.py                |    8 +
 docs/gpio_tools.rst         |   10 +-
 docs/gpioctl_top.rst        |  156 +++
 man/meson.build             |   39 +
 tools/gpio-tools-test.bash  | 1756 ++++++++++++++++++++++++++
 tools/gpioctl-test-helper.c |   58 +
 tools/gpioctl.c             | 2838 +++++++++++++++++++++++++++++++++++++++++++
 tools/gpiomon.c             |  128 +-
 tools/gpioset.c             |   91 +-
 tools/meson.build           |   21 +-
 tools/tools-common.c        |  180 ++-
 tools/tools-common.h        |    9 +
 14 files changed, 5091 insertions(+), 219 deletions(-)

---
base-commit: 25c56387337390bbbe42d145faa829c35f9c0611
change-id: 20251001-gpioctl-336556e702fd

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>
#1 lore raw

RFC PATCH 1/3 tools: factor out line value parsing into tools-common

Move functions for parsing of line value arguments to tools-common so that both gpioset and the upcoming gpioctl can share them.

---
 tools/gpioset.c      | 91 +---------------------------------------------------
 tools/tools-common.c | 78 ++++++++++++++++++++++++++++++++++++++++++++
 tools/tools-common.h |  5 +++
 3 files changed, 84 insertions(+), 90 deletions(-)
tools/gpioset.c +1 -90
index fce4a7de222f93121c99843a761c5308e021343a..95e99fe6543312ccbece1ec3b0b37ae5d4878790 100644
@@ -227,95 +227,6 @@ static int parse_config(int argc, char **argv, struct config *cfg)
 	return optind;
 }
 
-static enum gpiod_line_value parse_value(const char *option)
-{
-	if (strcmp(option, "0") == 0)
-		return GPIOD_LINE_VALUE_INACTIVE;
-	if (strcmp(option, "1") == 0)
-		return GPIOD_LINE_VALUE_ACTIVE;
-	if (strcmp(option, "inactive") == 0)
-		return GPIOD_LINE_VALUE_INACTIVE;
-	if (strcmp(option, "active") == 0)
-		return GPIOD_LINE_VALUE_ACTIVE;
-	if (strcmp(option, "off") == 0)
-		return GPIOD_LINE_VALUE_INACTIVE;
-	if (strcmp(option, "on") == 0)
-		return GPIOD_LINE_VALUE_ACTIVE;
-	if (strcmp(option, "false") == 0)
-		return GPIOD_LINE_VALUE_INACTIVE;
-	if (strcmp(option, "true") == 0)
-		return GPIOD_LINE_VALUE_ACTIVE;
-
-	return GPIOD_LINE_VALUE_ERROR;
-}
-
-/*
- * Parse line id and values from lvs into lines and values.
- *
- * Accepted forms:
- *     'line=value'
- *     '"line"=value'
- *
- * If line id is quoted then it is returned unquoted.
- */
-static bool parse_line_values(int num_lines, char **lvs, char **lines,
-			      enum gpiod_line_value *values, bool interactive)
-{
-	char *value, *line;
-	int i;
-
-	for (i = 0; i < num_lines; i++) {
-		line = lvs[i];
-
-		if (*line != '"') {
-			value = strchr(line, '=');
-		} else {
-			line++;
-			value = strstr(line, "\"=");
-			if (value) {
-				*value = '\0';
-				value++;
-			}
-		}
-
-		if (!value) {
-			if (interactive)
-				printf("invalid line value: '%s'\n", lvs[i]);
-			else
-				print_error("invalid line value: '%s'", lvs[i]);
-
-			return false;
-		}
-
-		*value = '\0';
-		value++;
-		values[i] = parse_value(value);
-
-		if (values[i] == GPIOD_LINE_VALUE_ERROR) {
-			if (interactive)
-				printf("invalid line value: '%s'\n", value);
-			else
-				print_error("invalid line value: '%s'", value);
-
-			return false;
-		}
-
-		lines[i] = line;
-	}
-
-	return true;
-}
-
-/*
- * Parse line id and values from lvs into lines and values, or die trying.
- */
-static void parse_line_values_or_die(int num_lines, char **lvs, char **lines,
-				     enum gpiod_line_value *values)
-{
-	if (!parse_line_values(num_lines, lvs, lines, values, false))
-		exit(EXIT_FAILURE);
-}
-
 static void print_banner(int num_lines, char **lines)
 {
 	int i;
@@ -794,7 +705,7 @@ static void interact(struct gpiod_line_request **requests,
 			if (num_lines == 0)
 				printf("at least one GPIO line value must be specified\n");
 			else if (parse_line_values(num_lines, &words[1], lines,
-						   values, true) &&
+						   values) &&
 				 valid_lines(resolver, num_lines, lines)) {
 				set_line_values_subset(resolver, num_lines,
 						       lines, values);
tools/tools-common.c +78 -0

84 lines not on this page · raw message

tools/tools-common.h +5 -0
index 85ee765255bbba991cd2e1fbe6b727a9e998d47e..5083b0908584f98a00321ca3fadf46f55c0a3066 100644
@@ -35,6 +35,11 @@ void die_oom(void) NORETURN;
 void die_perror(const char *fmt, ...) NORETURN PRINTF(1, 2);
 void print_version(void);
 int parse_bias_or_die(const char *option);
+int parse_line_value_or_die(const char *option);
+bool parse_line_values(int num_lines, char **lvs, char **lines,
+		       enum gpiod_line_value *values);
+void parse_line_values_or_die(int num_lines, char **lvs, char **lines,
+			      enum gpiod_line_value *values);
 long long parse_period(const char *option);
 unsigned long long parse_period_or_die(const char *option);
 void sleep_us(unsigned long long period);
Signature · 1 line
2.47.3
#2 lore raw

RFC PATCH 2/3 tools: factor out line event formatting into tools-common

In reply to Bartosz Golaszewski

Move helpers used by gpiomon for formatting of line events and printing of the help text for format specifiers to tools-common so that both gpiomon and the upcoming gpioctl can reuse the same code.

---
 tools/gpiomon.c      | 128 +++++++--------------------------------------------
 tools/tools-common.c | 100 ++++++++++++++++++++++++++++++++++++++++
 tools/tools-common.h |   4 ++
 3 files changed, 120 insertions(+), 112 deletions(-)
tools/gpiomon.c +16 -112
index 2b9a69b8aea60d62d42b34dae510ac0a83e0f013..79006ed06e64f1a7fa0afc8e14904b915cf9f695 100644
@@ -79,16 +79,7 @@ static void print_help(void)
 	printf("  -v, --version\t\toutput version information and exit\n");
 	print_chip_help();
 	print_period_help();
-	printf("\n");
-	printf("Format specifiers:\n");
-	printf("  %%o   GPIO line offset\n");
-	printf("  %%l   GPIO line name\n");
-	printf("  %%c   GPIO chip name\n");
-	printf("  %%e   numeric edge event type ('1' - rising or '2' - falling)\n");
-	printf("  %%E   edge event type ('rising' or 'falling')\n");
-	printf("  %%S   event timestamp as seconds\n");
-	printf("  %%U   event timestamp as UTC\n");
-	printf("  %%L   event timestamp as local time\n");
+	print_event_format_help(true);
 }
 
 static int parse_edges_or_die(const char *option)
@@ -254,115 +245,28 @@ static void print_banner(int num_lines, char **lines)
 	}
 }
 
-static void event_print_formatted(struct gpiod_edge_event *event,
-				  struct line_resolver *resolver, int chip_num,
-				  struct config *cfg)
-{
-	const char *lname, *prev, *curr;
-	unsigned int offset;
-	uint64_t evtime;
-	int evtype;
-	char fmt;
-
-	offset = gpiod_edge_event_get_line_offset(event);
-	evtime = gpiod_edge_event_get_timestamp_ns(event);
-	evtype = gpiod_edge_event_get_event_type(event);
-
-	for (prev = curr = cfg->fmt;;) {
-		curr = strchr(curr, '%');
-		if (!curr) {
-			fputs(prev, stdout);
-			break;
-		}
-
-		if (prev != curr)
-			fwrite(prev, curr - prev, 1, stdout);
-
-		fmt = *(curr + 1);
-
-		switch (fmt) {
-		case 'c':
-			fputs(get_chip_name(resolver, chip_num), stdout);
-			break;
-		case 'e':
-			printf("%d", evtype);
-			break;
-		case 'E':
-			if (evtype == GPIOD_EDGE_EVENT_RISING_EDGE)
-				fputs("rising", stdout);
-			else
-				fputs("falling", stdout);
-			break;
-		case 'l':
-			lname = get_line_name(resolver, chip_num, offset);
-			if (!lname)
-				lname = "unnamed";
-			fputs(lname, stdout);
-			break;
-		case 'L':
-			print_event_time(evtime, 2);
-			break;
-		case 'o':
-			printf("%u", offset);
-			break;
-		case 'S':
-			print_event_time(evtime, 0);
-			break;
-		case 'U':
-			print_event_time(evtime, 1);
-			break;
-		case '%':
-			fputc('%', stdout);
-			break;
-		case '\0':
-			fputc('%', stdout);
-			goto end;
-		default:
-			fwrite(curr, 2, 1, stdout);
-			break;
-		}
-
-		curr += 2;
-		prev = curr;
-	}
-
-end:
-	fputc('\n', stdout);
-}
-
-static void event_print_human_readable(struct gpiod_edge_event *event,
-				       struct line_resolver *resolver,
-				       int chip_num, struct config *cfg)
-{
-	unsigned int offset;
-	uint64_t evtime;
-
-	offset = gpiod_edge_event_get_line_offset(event);
-	evtime = gpiod_edge_event_get_timestamp_ns(event);
-
-	print_event_time(evtime, cfg->timestamp_fmt);
-
-	if (gpiod_edge_event_get_event_type(event) ==
-	    GPIOD_EDGE_EVENT_RISING_EDGE)
-		fputs("\trising\t", stdout);
-	else
-		fputs("\tfalling\t", stdout);
-
-	print_line_id(resolver, chip_num, offset, cfg->chip_id, cfg->unquoted);
-	fputc('\n', stdout);
-}
-
 static void event_print(struct gpiod_edge_event *event,
 			struct line_resolver *resolver, int chip_num,
 			struct config *cfg)
 {
+	const char *line_name, *chip_name;
+	unsigned int offset;
+
 	if (cfg->quiet)
 		return;
 
-	if (cfg->fmt)
-		event_print_formatted(event, resolver, chip_num, cfg);
-	else
-		event_print_human_readable(event, resolver, chip_num, cfg);
+	offset = gpiod_edge_event_get_line_offset(event);
+	line_name = get_line_name(resolver, chip_num, offset);
+	chip_name = cfg->chip_id ? get_chip_name(resolver, chip_num) : NULL;
+
+	print_event(gpiod_edge_event_get_timestamp_ns(event),
+		    offset,
+		    gpiod_edge_event_get_event_type(event),
+		    cfg->timestamp_fmt,
+		    cfg->fmt,
+		    line_name,
+		    chip_name,
+		    cfg->unquoted);
 }
 
 int main(int argc, char **argv)
tools/tools-common.c +100 -0

112 lines not on this page · raw message

tools/tools-common.h +4 -0
index 5083b0908584f98a00321ca3fadf46f55c0a3066..95517fcd016de89e0b01c3ffc6630a30c67ba933 100644
@@ -48,7 +48,11 @@ unsigned int parse_uint_or_die(const char *option);
 void print_bias_help(void);
 void print_chip_help(void);
 void print_period_help(void);
+void print_event_format_help(bool include_line_chip);
 void print_event_time(uint64_t evtime, int format);
+void print_event(uint64_t timestamp_ns, unsigned int offset, int event_type,
+		 int timestamp_fmt, const char *fmt,
+		 const char *line_name, const char *chip_name, bool unquoted);
 void print_line_attributes(struct gpiod_line_info *info, bool unquoted_strings);
 void print_line_id(struct line_resolver *resolver, int chip_num,
 		   unsigned int offset, const char *chip_id, bool unquoted);
Signature · 1 line
2.47.3
#3 lore raw

Re: [PATCH RFC libgpiod 2/3] tools: factor out line event formatting into tools-common

Quoted 3 lines · Bartosz Golaszewski

Move helpers used by gpiomon for formatting of line events and printing of the help text for format specifiers to tools-common so that both gpiomon and the upcoming gpioctl can reuse the same code.

...

tools/tools-common.c void print_period_help(void) · line 310

Quoted 3 lines
tools/tools-common.c +3 -0
+void print_event_format_help(bool include_line_chip)
+{
+	printf("\n");

This is really confusing. If you want to separate, do it in the caller.

tools/tools-common.c void print_period_help(void) · line 316

Quoted 7 lines
tools/tools-common.c +6 -0
+	printf("Format specifiers:\n");
+	printf("  %%o   GPIO line offset\n");
 
+	if (include_line_chip) {
+		printf("  %%l   GPIO line name\n");
+		printf("  %%c   GPIO chip name\n");
+	}

Hmm... Personally I don't like such an approach. The alternatives are:
- having 3 (if the order is strictly requested like this) or 2 helpers and split the function to two instead of boolean parameter.

Quoted 6 lines
tools/tools-common.c +6 -0
+	printf("  %%e   numeric edge event type ('1' - rising or '2' - falling)\n");
+	printf("  %%E   edge event type ('rising' or 'falling')\n");
+	printf("  %%S   event timestamp as seconds\n");
+	printf("  %%U   event timestamp as UTC\n");
+	printf("  %%L   event timestamp as local time\n");
+}

...

Quoted 14 lines
tools/tools-common.c +13 -0
+void print_event(uint64_t timestamp_ns, unsigned int offset, int event_type,
+		 int timestamp_fmt, const char *fmt,
+		 const char *line_name, const char *chip_name, bool unquoted)
+{
+	const char *prev, *curr;
+	char spec;
+
+	if (!fmt) {
+		print_event_time(timestamp_ns, timestamp_fmt);
+		if (event_type == GPIOD_EDGE_EVENT_RISING_EDGE)
+			fputs("\trising\t", stdout);
+		else
+			fputs("\tfalling\t", stdout);
 

tools/tools-common.c void print_event_time(uint64_t evtime, int format) · line 378

Quoted 10 lines
tools/tools-common.c +10 -0
+		if (!line_name) {
+			if (chip_name)
+				printf("%s %u", chip_name, offset);
+			else
+				printf("%u", offset);
+		} else {
+			if (chip_name)
+				printf("%s %u ", chip_name, offset);
+			printf(unquoted ? "%s" : "\"%s\"", line_name);
+		}

Independently on the original code, I would go with positive test

		if (line_name) {
			...
		} else {
			...
		}
Quoted 45 lines
tools/tools-common.c +44 -0
+		fputc('\n', stdout);
+		return;
+	}
 
+	for (prev = curr = fmt;;) {
+		curr = strchr(curr, '%');
+		if (!curr) {
+			fputs(prev, stdout);
+			break;
+		}
+
+		if (prev != curr)
+			fwrite(prev, curr - prev, 1, stdout);
+
+		spec = *(curr + 1);
+
+		switch (spec) {
+		case 'c':
+			fputs(chip_name ? chip_name : "unknown", stdout);
+			break;
+		case 'e':
+			printf("%d", event_type);
+			break;
+		case 'E':
+			fputs(event_type == GPIOD_EDGE_EVENT_RISING_EDGE ?
+			      "rising" : "falling", stdout);
+			break;
+		case 'l':
+			fputs(line_name ? line_name : "unnamed", stdout);
+			break;
+		case 'L':
+			print_event_time(timestamp_ns, 2);
+			break;
+		case 'o':
+			printf("%u", offset);
+			break;
+		case 'S':
+			print_event_time(timestamp_ns, 0);
+			break;
+		case 'U':
+			print_event_time(timestamp_ns, 1);
+			break;
+		case '%':
+			fputc('%', stdout);
+			break;

tools/tools-common.c void print_event_time(uint64_t evtime, int format) · line 433

Quoted 10 lines
tools/tools-common.c +9 -0
+		case '\0':
+			fputc('%', stdout);
+			goto end;
+		default:
+			fwrite(curr, 2, 1, stdout);
+			break;
+		}
 
+		curr += 2;
+		prev = curr;

Can be in for-line.

Quoted 5 lines
tools/tools-common.c +5 -0
+	}
+
+end:
+	fputc('\n', stdout);
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Re: [PATCH RFC libgpiod 2/3] tools: factor out line event formatting into tools-common

Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Bartosz Golaszewski

Move helpers used by gpiomon for formatting of line events and printing of the help text for format specifiers to tools-common so that both gpiomon and the upcoming gpioctl can reuse the same code.

Quoted 8 lines

...

tools/tools-common.c void print_period_help(void) · line 310

Quoted 3 lines
tools/tools-common.c +3 -0
+void print_event_format_help(bool include_line_chip)
+{
+	printf("\n");

This is really confusing. If you want to separate, do it in the caller.

We've been already doing it this way, for instance in print_chip_help(). I don't care enough to change it but feel free to submit a patch if that bothers you. :)

Quoted 12 lines

tools/tools-common.c void print_period_help(void) · line 316

Quoted 7 lines
tools/tools-common.c +6 -0
+	printf("Format specifiers:\n");
+	printf("  %%o   GPIO line offset\n");
 
+	if (include_line_chip) {
+		printf("  %%l   GPIO line name\n");
+		printf("  %%c   GPIO chip name\n");
+	}

Hmm... Personally I don't like such an approach. The alternatives are:
- having 3 (if the order is strictly requested like this) or 2 helpers and split the function to two instead of boolean parameter.

Or even better: just implement the two above in gpioctl too. I'll do this for v2.

Quoted 100 lines
Quoted 6 lines
tools/tools-common.c +6 -0
+	printf("  %%e   numeric edge event type ('1' - rising or '2' - falling)\n");
+	printf("  %%E   edge event type ('rising' or 'falling')\n");
+	printf("  %%S   event timestamp as seconds\n");
+	printf("  %%U   event timestamp as UTC\n");
+	printf("  %%L   event timestamp as local time\n");
+}

...

Quoted 14 lines
tools/tools-common.c +13 -0
+void print_event(uint64_t timestamp_ns, unsigned int offset, int event_type,
+		 int timestamp_fmt, const char *fmt,
+		 const char *line_name, const char *chip_name, bool unquoted)
+{
+	const char *prev, *curr;
+	char spec;
+
+	if (!fmt) {
+		print_event_time(timestamp_ns, timestamp_fmt);
+		if (event_type == GPIOD_EDGE_EVENT_RISING_EDGE)
+			fputs("\trising\t", stdout);
+		else
+			fputs("\tfalling\t", stdout);
 

tools/tools-common.c void print_event_time(uint64_t evtime, int format) · line 378

Quoted 10 lines
tools/tools-common.c +10 -0
+		if (!line_name) {
+			if (chip_name)
+				printf("%s %u", chip_name, offset);
+			else
+				printf("%u", offset);
+		} else {
+			if (chip_name)
+				printf("%s %u ", chip_name, offset);
+			printf(unquoted ? "%s" : "\"%s\"", line_name);
+		}

Independently on the original code, I would go with positive test

		if (line_name) {
			...
		} else {
			...
		}
Quoted 45 lines
tools/tools-common.c +44 -0
+		fputc('\n', stdout);
+		return;
+	}
 
+	for (prev = curr = fmt;;) {
+		curr = strchr(curr, '%');
+		if (!curr) {
+			fputs(prev, stdout);
+			break;
+		}
+
+		if (prev != curr)
+			fwrite(prev, curr - prev, 1, stdout);
+
+		spec = *(curr + 1);
+
+		switch (spec) {
+		case 'c':
+			fputs(chip_name ? chip_name : "unknown", stdout);
+			break;
+		case 'e':
+			printf("%d", event_type);
+			break;
+		case 'E':
+			fputs(event_type == GPIOD_EDGE_EVENT_RISING_EDGE ?
+			      "rising" : "falling", stdout);
+			break;
+		case 'l':
+			fputs(line_name ? line_name : "unnamed", stdout);
+			break;
+		case 'L':
+			print_event_time(timestamp_ns, 2);
+			break;
+		case 'o':
+			printf("%u", offset);
+			break;
+		case 'S':
+			print_event_time(timestamp_ns, 0);
+			break;
+		case 'U':
+			print_event_time(timestamp_ns, 1);
+			break;
+		case '%':
+			fputc('%', stdout);
+			break;

tools/tools-common.c void print_event_time(uint64_t evtime, int format) · line 433

Quoted 10 lines
tools/tools-common.c +9 -0
+		case '\0':
+			fputc('%', stdout);
+			goto end;
+		default:
+			fwrite(curr, 2, 1, stdout);
+			break;
+		}
 
+		curr += 2;
+		prev = curr;

Can be in for-line.

Quoted 12 lines

tools/tools-common.c void print_event_time(uint64_t evtime, int format) · line 438

Quoted 5 lines
tools/tools-common.c +5 -0
+	}
+
+end:
+	fputc('\n', stdout);
+}

--
With Best Regards,
Andy Shevchenko

Bart

#5 lore raw

RFC PATCH 3/3 tools: implement gpioctl

In reply to Bartosz Golaszewski

Provide gpioctl, a new command-line tool for persistent GPIO line control. Unlike gpioget and gpioset which release lines on exit, gpioctl holds requested lines in a background daemon so their state survives across multiple client invocations.

The client auto-spawns the daemon on first use. Communication uses a per-user abstract Unix-domain socket, providing automatic cleanup and per-user isolation without filesystem artifacts.

Each set of lines requested through the tool is assigned a unique ID and referred to as a "request". The following sub-commands are provided:

request - Request a set of GPIO lines by offset or name. The daemon
holds them after the client exits.

  release      - Release a previously made request by ID.
  requests     - List all requests currently held by the daemon.
  get          - Read values of requested lines.
  set          - Set the output values of requested lines.
  monitor      - Subscribe to edge events on a held request.
  reconfigure  - Change the settings of a held request in-place without

releasing it (e.g. switch direction, add edge detection).

There are also "hidden" sub-commands that are not documented in the help text but can be used for debugging purposes:

  ping         - Verify the daemon is running (spawns it if not).
  stop         - Ask the daemon to exit, releasing all held lines.
  debug        - Run the daemon in the foreground with logging enabled.

The wire protocol is intentionally private and unstable: only gpioctl itself speaks it, so no ABI commitment is made.

Also add test-cases covering all sub-commands and configuration options. Update documentation, man page generation (main page plus one page per sub-command), and mark the persistent-daemon TODO item as complete.

---
 TODO                        |   14 -
 docs/.gitignore             |    2 +
 docs/conf.py                |    8 +
 docs/gpio_tools.rst         |   10 +-
 docs/gpioctl_top.rst        |  156 +++
 man/meson.build             |   39 +
 tools/gpio-tools-test.bash  | 1756 ++++++++++++++++++++++++++
 tools/gpioctl-test-helper.c |   58 +
 tools/gpioctl.c             | 2838 +++++++++++++++++++++++++++++++++++++++++++
 tools/meson.build           |   21 +-
 tools/tools-common.c        |    2 +-
 11 files changed, 4887 insertions(+), 17 deletions(-)
TODO +0 -14
index 95bb3e5dfc3c7f5a8d46ae9b144b5a43866abd7b..c9c127ad785c3786648b7058da4df938beb19f1a 100644
@@ -8,20 +8,6 @@ serve as the starting point.
 
 ==========
 
-* implement a simple daemon for controlling GPIOs in C together with a client
-  program
-
-This is by far the lowest priority task. Similarly as with the dbus daemon:
-the goal is to provide a centralized agent controlling GPIOs with a simple
-interface consisting of a command line client communicating with the server
-over unix sockets.
-
-In this case however the goal is to have as few dependencies as possible. This
-is because for some small systems dbus is overkill. Since we won't be using any
-standardized protocol, it will take much more effort to implement it correctly.
-
-----------
-
 * improve gpioset --interactive tab completion
 
 The existing tab completion uses libedit's readline emulation layer which
docs/.gitignore +2 -0
index 61dac9a54aa0fef3ef44b993470ccd789ca630f2..2046e4c1b2c0552fa3ad2762ac47d2f762948233 100644
@@ -11,6 +11,8 @@ gpioget.rst
 gpioset.rst
 gpiomon.rst
 gpionotify.rst
+gpioctl.rst
+gpioctl-*.rst
 
 gpio-manager.rst
 
docs/conf.py +8 -0
index 831d5f01235081e70c552c9a30e2f4711abd1bfb..48f0bae506018ab2f0e1386e2dee933c6cffc66e 100644
@@ -136,6 +136,14 @@ for page in [
     "gpioset",
     "gpiomon",
     "gpionotify",
+    "gpioctl",
+    "gpioctl-get",
+    "gpioctl-monitor",
+    "gpioctl-reconfigure",
+    "gpioctl-release",
+    "gpioctl-request",
+    "gpioctl-requests",
+    "gpioctl-set",
     "gpio-manager",
     "gpiocli",
     "gpiocli-detect",
docs/gpio_tools.rst +9 -1
index 2876970e4028451b05f7b20a551fc97d0a1685c2..08b09b17c3324bfacf29cc41c1b348d63623928f 100644
@@ -16,7 +16,8 @@ Overview
 The **libgpiod** project includes a suite of **command-line tools** to
 facilitate GPIO manipulation from console and shell scripts.
 
-There are currently six command-line tools available:
+There are currently six stateless command-line tools available, plus
+``gpioctl`` for persistent line control:
 
 * **gpiodetect**: list all gpiochips present on the system, their names, labels
   and number of GPIO lines
@@ -32,6 +33,8 @@ There are currently six command-line tools available:
 * **gpionotify**: wait for changed to the info for GPIO lines, specify which
   changes to watch for, how many events to process before exiting, or if the
   events should be reported to the console
+* **gpioctl**: request and persistently hold GPIO lines via a per-user background
+  daemon; supports get, set, monitor and reconfigure operations on held requests
 
 Shared library
 --------------
@@ -60,6 +63,11 @@ of libgpiotools.
    gpiomon<gpiomon>
    gpionotify<gpionotify>
 
+.. toctree::
+   :maxdepth: 1
+
+   gpioctl<gpioctl_top>
+
 Examples
 --------
 
docs/gpioctl_top.rst new file +156 -0

156 lines not on this page · raw message

man/meson.build +39 -0
index f3a2d4e83698e3f9ca1a8ae35cdf3642dca90f23..a7a0ec3da1105e04be1339615139bfc5293c9b87 100644
@@ -22,6 +22,45 @@ if tools_enabled
       install_dir: man1_dir,
     )
   endforeach
+
+  custom_target('gpioctl.1',
+    output: 'gpioctl.1',
+    command: [
+      help2man,
+      tools_exes['gpioctl'],
+      '--include', man_template,
+      '--no-info',
+      '--name=libgpiod command-line utility',
+      '--help-option=--help',
+      '--manual=gpioctl',
+      '--output=@OUTPUT@',
+    ],
+    install: true,
+    install_dir: man1_dir,
+  )
+
+  gpioctl_cmds = [
+    'get', 'monitor', 'reconfigure',
+    'release', 'request', 'requests', 'set',
+  ]
+
+  foreach cmd : gpioctl_cmds
+    custom_target('gpioctl-' + cmd + '.1',
+      output: 'gpioctl-' + cmd + '.1',
+      command: [
+        help2man,
+        tools_exes['gpioctl'],
+        '--include', man_template,
+        '--no-info',
+        '--name=libgpiod command-line utility',
+        '--help-option=' + cmd + ' --help',
+        '--manual=gpioctl-' + cmd,
+        '--output=@OUTPUT@',
+      ],
+      install: true,
+      install_dir: man1_dir,
+    )
+  endforeach
 endif
 
 if dbus_enabled
tools/gpio-tools-test.bash +1756 -0

1,762 lines not on this page · raw message

tools/gpioctl-test-helper.c new file +58 -0

58 lines not on this page · raw message

tools/gpioctl.c new file +2838 -0

909 lines not on this page · raw message

tools/meson.build +20 -1
index 5ecf7dc5d221d52243478f9a126e6512d2408981..70c4d2fe5484e5621da2184bd6af2041b4fd8de3 100644

Lines of this file are not shown. View the raw message.

tools/tools-common.c +1 -1
index d6cfcb4fd6b1aa24b201556dbeab9f095b1bf5f0..0f98f40696d738744c7ce870c2961b8a0018e272 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.47.3

Part of the diff is not shown here. View the raw message.

maintainer
#6 lore raw

Re: [PATCH RFC libgpiod 3/3] tools: implement gpioctl

Quoted 29 lines · Bartosz Golaszewski

Provide gpioctl, a new command-line tool for persistent GPIO line control. Unlike gpioget and gpioset which release lines on exit, gpioctl holds requested lines in a background daemon so their state survives across multiple client invocations.

The client auto-spawns the daemon on first use. Communication uses a per-user abstract Unix-domain socket, providing automatic cleanup and per-user isolation without filesystem artifacts.

Each set of lines requested through the tool is assigned a unique ID and referred to as a "request". The following sub-commands are provided:

request - Request a set of GPIO lines by offset or name. The daemon
holds them after the client exits.

  release      - Release a previously made request by ID.
  requests     - List all requests currently held by the daemon.
  get          - Read values of requested lines.
  set          - Set the output values of requested lines.
  monitor      - Subscribe to edge events on a held request.
  reconfigure  - Change the settings of a held request in-place without

releasing it (e.g. switch direction, add edge detection).

There are also "hidden" sub-commands that are not documented in the help text but can be used for debugging purposes:

  ping         - Verify the daemon is running (spawns it if not).
  stop         - Ask the daemon to exit, releasing all held lines.
  debug        - Run the daemon in the foreground with logging enabled.
Quoted 8 lines

The wire protocol is intentionally private and unstable: only gpioctl itself speaks it, so no ABI commitment is made.

Also add test-cases covering all sub-commands and configuration options. Update documentation, man page generation (main page plus one page per sub-command), and mark the persistent-daemon TODO item as complete.

A big Acked-by: Linus Walleij <[email protected]> for the entire idea and approach.

I can't claim to have read the code in any detail, because it is quite a lot. I can run it through Sashiko if you want, hehe :D

I think this will be very helpful when doing automation using different AI tools, but also for pretty elaborate scripting.

I think it will soon by itself develop support for scripting, have you considered just integrating LUA? I was thinking that it is very soon natural to ask for something like what you can do at a U-Boot prompt.

request; sleep 0.1; ping; exit if no ping; get; if set get until low; ... etc.

This kind of scritping engines are easy to build up in LUA...

My main thinking is to forestall any such scriptable feature requests that will otherwise invariably appear.

Of course it is easy to call up the tool in separate invocations, but that will have the overhead of executing a binary, creating a process etc etc. With a scripting language built into gpioctl you have no limits to what you can do.

Yours,
Linus Walleij

#7 lore raw

Re: [PATCH RFC libgpiod 3/3] tools: implement gpioctl

Quoted 39 lines · Linus Walleij
Quoted 36 lines · Bartosz Golaszewski

Provide gpioctl, a new command-line tool for persistent GPIO line control. Unlike gpioget and gpioset which release lines on exit, gpioctl holds requested lines in a background daemon so their state survives across multiple client invocations.

The client auto-spawns the daemon on first use. Communication uses a per-user abstract Unix-domain socket, providing automatic cleanup and per-user isolation without filesystem artifacts.

Each set of lines requested through the tool is assigned a unique ID and referred to as a "request". The following sub-commands are provided:

request - Request a set of GPIO lines by offset or name. The daemon
holds them after the client exits.

  release      - Release a previously made request by ID.
  requests     - List all requests currently held by the daemon.
  get          - Read values of requested lines.
  set          - Set the output values of requested lines.
  monitor      - Subscribe to edge events on a held request.
  reconfigure  - Change the settings of a held request in-place without

releasing it (e.g. switch direction, add edge detection).

There are also "hidden" sub-commands that are not documented in the help text but can be used for debugging purposes:

  ping         - Verify the daemon is running (spawns it if not).
  stop         - Ask the daemon to exit, releasing all held lines.
  debug        - Run the daemon in the foreground with logging enabled.

The wire protocol is intentionally private and unstable: only gpioctl itself speaks it, so no ABI commitment is made.

Also add test-cases covering all sub-commands and configuration options. Update documentation, man page generation (main page plus one page per sub-command), and mark the persistent-daemon TODO item as complete.

Quoted 8 lines
Quoted 1 line

A big Acked-by: Linus Walleij <[email protected]> for the entire idea and approach.

I can't claim to have read the code in any detail, because it is quite a lot. I can run it through Sashiko if you want, hehe :D

Can we do this with non-kernel code? Or are you running the model locally? Because in any case: absolutely please do and share the results!

Quoted 3 lines

I think this will be very helpful when doing automation using different AI tools, but also for pretty elaborate scripting.

It is mostly for scripting TBH. I figured this is the closest we'll get to seamless peristence of GPIO state. It works kind of like the DBus daemon minus DBus.

Quoted 5 lines

I think it will soon by itself develop support for scripting, have you considered just integrating LUA? I was thinking that it is very soon natural to ask for something like what you can do at a U-Boot prompt.

Like what gpioset already has with interactive mode? :)

Quoted 5 lines

request; sleep 0.1; ping; exit if no ping; get; if set get until low; ... etc.

This kind of scritping engines are easy to build up in LUA...

No, not really. If you need to do this from a single process, just use python. Or maybe someone will write lua bindings some day. I don't really plan on doing anything like this myself and this program is likely the final big development for libgpiod. Once that lands, I will consider the project mostly complete.

Quoted 9 lines

My main thinking is to forestall any such scriptable feature requests that will otherwise invariably appear.

Of course it is easy to call up the tool in separate invocations, but that will have the overhead of executing a binary, creating a process etc etc. With a scripting language built into gpioctl you have no limits to what you can do.

Yeah, like I said: thanks to Vincent's work, the python bindings are in great shape and always up to date with current developments in the python world.

I don't think stuffing lua into gpio-tools makes much sense for what it's being used for.

Bart

#8 lore raw

Re: [PATCH RFC libgpiod 3/3] tools: implement gpioctl

In reply to Linus Walleij
Quoted 35 lines · Linus Walleij
Quoted 32 lines · Bartosz Golaszewski

Provide gpioctl, a new command-line tool for persistent GPIO line control. Unlike gpioget and gpioset which release lines on exit, gpioctl holds requested lines in a background daemon so their state survives across multiple client invocations.

The client auto-spawns the daemon on first use. Communication uses a per-user abstract Unix-domain socket, providing automatic cleanup and per-user isolation without filesystem artifacts.

Each set of lines requested through the tool is assigned a unique ID and referred to as a "request". The following sub-commands are provided:

request - Request a set of GPIO lines by offset or name. The daemon
holds them after the client exits.

  release      - Release a previously made request by ID.
  requests     - List all requests currently held by the daemon.
  get          - Read values of requested lines.
  set          - Set the output values of requested lines.
  monitor      - Subscribe to edge events on a held request.
  reconfigure  - Change the settings of a held request in-place without

releasing it (e.g. switch direction, add edge detection).

There are also "hidden" sub-commands that are not documented in the help text but can be used for debugging purposes:

  ping         - Verify the daemon is running (spawns it if not).
  stop         - Ask the daemon to exit, releasing all held lines.
  debug        - Run the daemon in the foreground with logging enabled.

The wire protocol is intentionally private and unstable: only gpioctl itself speaks it, so no ABI commitment is made.

Quoted 8 lines
Quoted 5 lines

Also add test-cases covering all sub-commands and configuration options. Update documentation, man page generation (main page plus one page per sub-command), and mark the persistent-daemon TODO item as complete.

A big Acked-by: Linus Walleij <[email protected]> for the entire idea and approach.

Seconded here. But the amount is simply unreviewable by a human.

Quoted 5 lines

I can't claim to have read the code in any detail, because it is quite a lot. I can run it through Sashiko if you want, hehe :D

I think this will be very helpful when doing automation using different AI tools, but also for pretty elaborate scripting.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Acked-by on v1 3/3
#9 lore raw

RE: [PATCH RFC libgpiod 3/3] tools: implement gpioctl

In reply to Bartosz Golaszewski
Quoted 12 lines

-----Original Message-----
From: Bartosz Golaszewski <[email protected]>
Sent: Friday, October 16, 2026 9:18 AM
To: Linus Walleij <[email protected]>; Kent Gibson <[email protected]>; Vincent Fazio <[email protected]>; Andy Shevchenko <[email protected]>
Cc: [email protected]; [email protected]; Bartosz Golaszewski <[email protected]>
Subject: [External] - [PATCH RFC libgpiod 3/3] tools: implement gpioctl

Quoted 5 lines

Provide gpioctl, a new command-line tool for persistent GPIO line control. Unlike gpioget and gpioset which release lines on exit, gpioctl holds requested lines in a background daemon so their state survives across multiple client invocations.

This would be very useful for us since we have scenarios where we're backgrounding gpioset to sticky the state and then using pgrep to find and kill them.

docs/gpioctl_top.rst line 20

Quoted 13 lines
docs/gpioctl_top.rst new file +9 -0
+
+Persistent GPIO control tool
+=============================
+
+``gpioctl`` provides GPIO control with **persistence**: a per-user background
+daemon holds requested lines even after the client process exits, unlike
+``gpioget`` or ``gpioset`` which release their lines on exit.
+
+The client auto-spawns the daemon on first use and communicates with it

over

docs/gpioctl_top.rst new file +3 -0
+a per-user abstract Unix-domain socket. The daemon exits automatically after
+60 seconds of inactivity.
+

C is not really my forte unfortunately, so RTFC won't help, but is there any mechanism to either raise the default or extend this timeout or "sticky until stopped"?

Our use case would be: we need to set a pin and do some long operation like program a SPI flash where 60 seconds won't be enough.

I suppose callers could simply request the pin state via get but something like ping would work if it reset the inactivity counter which it looks like it might.

Since it's "hidden", I suppose ping is unstable and shouldn't be counted on for scripting?

I won't be useful in reviewing the actual code but from a "would this utility be useful?", two thumbs up.

#10 lore raw

Re: [PATCH RFC libgpiod 3/3] tools: implement gpioctl

Quoted 13 lines · Vincent Fazio
Quoted 13 lines

-----Original Message-----
From: Bartosz Golaszewski <[email protected]>
Sent: Friday, October 16, 2026 9:18 AM
To: Linus Walleij <[email protected]>; Kent Gibson <[email protected]>; Vincent Fazio <[email protected]>; Andy Shevchenko <[email protected]>
Cc: [email protected]; [email protected]; Bartosz Golaszewski <[email protected]>
Subject: [External] - [PATCH RFC libgpiod 3/3] tools: implement gpioctl

Quoted 8 lines
Quoted 5 lines

Provide gpioctl, a new command-line tool for persistent GPIO line control. Unlike gpioget and gpioset which release lines on exit, gpioctl holds requested lines in a background daemon so their state survives across multiple client invocations.

This would be very useful for us since we have scenarios where we're backgrounding gpioset to sticky the state and then using pgrep to find and kill them.

I'm glad to hear that. Testing the functionality of this is very helpful too!

Quoted 16 lines

docs/gpioctl_top.rst line 20

Quoted 13 lines
docs/gpioctl_top.rst new file +9 -0
+
+Persistent GPIO control tool
+=============================
+
+``gpioctl`` provides GPIO control with **persistence**: a per-user background
+daemon holds requested lines even after the client process exits, unlike
+``gpioget`` or ``gpioset`` which release their lines on exit.
+
+The client auto-spawns the daemon on first use and communicates with it

over

docs/gpioctl_top.rst new file +3 -0
+a per-user abstract Unix-domain socket. The daemon exits automatically after
+60 seconds of inactivity.
+

C is not really my forte unfortunately, so RTFC won't help, but is there any mechanism to either raise the default or extend this timeout or "sticky until stopped"?

Quoted 2 lines

Our use case would be: we need to set a pin and do some long operation like program a SPI flash where 60 seconds won't be enough.

Oh, maybe the docs aren't very clear. If you request a pin, the daemon stays alive indefinitely. It will only exit if there are no active requests and no new connections within those 60 seconds.

So:

    gpioctl request --output foo=active

spawns the daemon and until you do

gpioctl release <request ID>

or

gpioctl stop

It will stay alive.

Let me know if this is what you need or if you actually need to make the idle timer configurable for some reason.

Bart

Quoted 7 lines

I suppose callers could simply request the pin state via get but something like ping would work if it reset the inactivity counter which it looks like it might.

Since it's "hidden", I suppose ping is unstable and shouldn't be counted on for scripting?

I won't be useful in reviewing the actual code but from a "would this utility be useful?", two thumbs up.