tools/gpio/gpio-watch.c

Source file repositories/reference/linux-study-clean/tools/gpio/gpio-watch.c

File Facts

System
Linux kernel
Corpus path
tools/gpio/gpio-watch.c
Extension
.c
Size
2165 bytes
Lines
101
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (ret) {
			perror("unable to set up line watch");
			return EXIT_FAILURE;
		}
	}

	pfd.fd = fd;
	pfd.events = POLLIN | POLLPRI;

	for (;;) {
		ret = poll(&pfd, 1, 5000);
		if (ret < 0) {
			perror("error polling the linechanged fd");
			return EXIT_FAILURE;
		} else if (ret > 0) {
			memset(&chg, 0, sizeof(chg));
			rd = read(pfd.fd, &chg, sizeof(chg));
			if (rd < 0 || rd != sizeof(chg)) {
				if (rd != sizeof(chg))
					errno = EIO;

				perror("error reading line change event");
				return EXIT_FAILURE;
			}

			switch (chg.event_type) {
			case GPIO_V2_LINE_CHANGED_REQUESTED:
				event = "requested";
				break;
			case GPIO_V2_LINE_CHANGED_RELEASED:
				event = "released";
				break;
			case GPIO_V2_LINE_CHANGED_CONFIG:
				event = "config changed";
				break;
			default:
				fprintf(stderr,
					"invalid event type received from the kernel\n");
				return EXIT_FAILURE;
			}

			printf("line %u: %s at %" PRIu64 "\n",
			       chg.info.offset, event, (uint64_t)chg.timestamp_ns);
		}
	}

	return 0;

err_usage:
	printf("%s: <gpiochip> <line0> <line1> ...\n", argv[0]);
	return EXIT_FAILURE;
}

Annotation

Implementation Notes