/* DF-5831 — rename ping-pong on the looked-up directory itself.
 *
 * Renames <base>/sub <-> <base>/sub2 forever.  Server-side (or local)
 * each rename bumps the "sub" namecache entry's nc_generation
 * (_cache_unlink_parent + _cache_link_parent both _cache_ncp_gen_enter/
 * exit), which is what the racing server-side nlookup() detects.
 *
 * usage: renamer <dir-containing-sub> [nprocs]
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

int
main(int argc, char **argv)
{
	char a[512], b[512];
	int i, n = 1;

	if (argc < 2) {
		fprintf(stderr, "usage: %s <dir-containing-sub> [nprocs]\n",
			argv[0]);
		exit(2);
	}
	if (argc > 2)
		n = atoi(argv[2]);
	for (i = 1; i < n; ++i) {
		pid_t p = fork();
		if (p == 0)
			break;
	}
	snprintf(a, sizeof(a), "%s/sub", argv[1]);
	snprintf(b, sizeof(b), "%s/sub2", argv[1]);
	for (;;) {
		if (rename(a, b) < 0 && errno == ENOENT)
			rename(b, a);
		else
			rename(b, a);
	}
	return (0);
}
