/*
 * probe.c - DF-2997 syscall probe: run one VFS op and report errno.
 *
 *   probe m <path>   mkdir(path, 0777)
 *   probe w <path>   open(path, O_CREAT|O_WRONLY, 0666)
 *   probe s <path>   stat(path)
 *   probe n <path>   mknod(path, S_IFIFO|0666, 0)
 *
 * Prints "OP path: errno=N (strerror)" on failure, "OP path: OK" on
 * success.  Used to demonstrate that the FIRST malicious reply produces a
 * clean user-visible error, while the REPEAT of the same syscall wedges
 * forever (D-state, kill -9 ineffective).
 */
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char **argv)
{
	const char *op, *path;
	int r = -1;

	if (argc != 3) {
		fprintf(stderr, "usage: probe m|w|s <path>\n");
		return 2;
	}
	op = argv[1];
	path = argv[2];
	if (strcmp(op, "m") == 0) {
		r = mkdir(path, 0777);
	} else if (strcmp(op, "w") == 0) {
		r = open(path, O_CREAT | O_WRONLY, 0666);
		if (r >= 0)
			close(r);
	} else if (strcmp(op, "s") == 0) {
		struct stat st;
		r = stat(path, &st);
	} else if (strcmp(op, "n") == 0) {
		r = mknod(path, S_IFIFO | 0666, 0);
	} else {
		fprintf(stderr, "bad op\n");
		return 2;
	}
	if (r < 0)
		printf("%s %s: FAIL errno=%d (%s)\n", op, path, errno,
		       strerror(errno));
	else
		printf("%s %s: OK\n", op, path);
	fflush(stdout);
	return (r < 0);
}
