/* hk_qemu_init.c v3 — loads harakiri.ko via init_module syscall directly.
 * No shell, no busybox needed. Reads the .ko, pushes it into the kernel,
 * runs the scheduler proof test (tag a spinner, watch nice change),
 * dumps status via /dev/kmsg, halts.
 */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/reboot.h>
#include <sys/syscall.h>
#include <sys/mount.h>
#include <sys/resource.h>
#include <errno.h>

static long init_module(void *img, unsigned long len, const char *args)
{
	return syscall(__NR_init_module, img, len, args);
}

static void kmsg(const char *s)
{
	int fd = open("/dev/kmsg", 1);
	if (fd >= 0) { dprintf(fd, "%s", s); close(fd); }
}

/* Read nice via getpriority syscall. Returns 99 on failure. */
static int read_nice(pid_t pid)
{
	int n;
	errno = 0;
	n = getpriority(PRIO_PROCESS, pid);
	if (n == -1 && errno != 0)
		return 99;
	return n;
}

static void write_file(const char *path, const char *data)
{
	FILE *f = fopen(path, "w");
	if (f) { fputs(data, f); fclose(f); }
	else printf("WARN: cannot open %s\n", path);
}

static void dump_status(void)
{
	FILE *f = fopen("/proc/harakiri/status", "r");
	char buf[256];
	if (!f) { printf("WARN: cannot open /proc/harakiri/status\n"); return; }
	printf("--- /proc/harakiri/status ---\n");
	while (fgets(buf, sizeof(buf), f))
		printf("%s", buf);
	printf("--- end status ---\n");
	fclose(f);
}

int main(void)
{
	int fd;
	struct stat st;
	void *img;

	printf("\n=== HARAKIRI QEMU TEST v3 ===\n");

	/* mount proc first — everything else needs it */
	mkdir("/proc", 0755);
	if (mount("proc", "/proc", "proc", 0, NULL) != 0)
		printf("WARN: proc mount failed\n");

	fd = open("/harakiri.ko", O_RDONLY);
	if (fd < 0) {
		perror("FAIL: open harakiri.ko");
		goto halt;
	}
	if (fstat(fd, &st) != 0) {
		perror("FAIL: fstat");
		goto halt;
	}

	img = mmap(0, st.st_size, PROT_READ | PROT_WRITE,
		   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
	if (img == (void *)-1) { printf("FAIL: mmap\n"); goto halt; }
	if (read(fd, img, st.st_size) != st.st_size) {
		printf("FAIL: read\n");
		goto halt;
	}
	close(fd);

	printf("[*] init_module(%lu bytes)...\n", (unsigned long)st.st_size);
	if (init_module(img, st.st_size, "") != 0) {
		perror("FAIL: init_module");
	} else {
		printf("[*] module loaded\n");
	}
	kmsg("harakiri-test: init_module done\n");

	/* --- scheduler proof test --- */
	{
		char tagbuf[32];
		pid_t spin;
		int nice_before, nice_after, nice_restored;

		spin = fork();
		if (spin == 0) {
			/* child: gentle spinner, yields so the VM stays alive */
			while (1) {
				volatile int i;
				for (i = 0; i < 100000; i++)
					;
				usleep(10000);
			}
		}

		printf("[*] spinner pid: %d\n", spin);
		nice_before = read_nice(spin);
		printf("[*] nice before: %d\n", nice_before);

		write_file("/proc/harakiri/mode", "gaming");
		dump_status();
		snprintf(tagbuf, sizeof(tagbuf), "%d 5", spin);
		write_file("/proc/harakiri/tag", tagbuf);
		sleep(1);
		nice_after = read_nice(spin);
		printf("[*] nice after tag (gaming+brood): %d\n", nice_after);

		snprintf(tagbuf, sizeof(tagbuf), "%d", spin);
		write_file("/proc/harakiri/untag", tagbuf);
		sleep(1);
		nice_restored = read_nice(spin);
		printf("[*] nice after untag: %d\n", nice_restored);

		printf("[*] RESULT: %d -> %d -> %d %s\n",
		       nice_before, nice_after, nice_restored,
		       (nice_before == 0 && nice_after == -10 &&
			nice_restored == 0) ? "PASS" : "FAIL");

		kill(spin, 9);
	}
	/* --- end test --- */

	printf("[*] sleeping 1s, then halting\n");
	fflush(stdout);
	sleep(1);

halt:
	printf("\n=== TEST COMPLETE ===\n");
	fflush(stdout);
	sleep(1);
	reboot(RB_POWER_OFF);
	return 0;
}
