#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/statfs.h>
#include <unistd.h>

static void fail(const char *what) { perror(what); exit(1); }
static unsigned int resident(void *address, size_t size) {
    unsigned char vector = 0;
    if (mincore(address, size, &vector)) fail("mincore");
    return vector & 1;
}

int main(void) {
    long page_size = sysconf(_SC_PAGESIZE);
    if (page_size <= 0) fail("page size");
    size_t size = (size_t)page_size;
    char path[] = "./mincore-cow-XXXXXX";
    int fd = mkstemp(path);
    if (fd < 0) fail("mkstemp");
    if (unlink(path)) fail("unlink");
    unsigned char *payload = malloc(size);
    if (!payload) fail("malloc");
    memset(payload, 'A', size);
    size_t written = 0;
    while (written < size) {
        ssize_t n = write(fd, payload + written, size - written);
        if (n < 0 && errno == EINTR) continue;
        if (n <= 0) fail("write");
        written += (size_t)n;
    }
    free(payload);
    if (fsync(fd)) fail("fsync");
    struct statfs fs;
    if (fstatfs(fd, &fs)) fail("fstatfs");
    volatile unsigned char *cow = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
    volatile unsigned char *fresh = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
    if ((void *)cow == MAP_FAILED || (void *)fresh == MAP_FAILED) fail("mmap");
    cow[0] = 'B';
    if (mlock((void *)cow, size)) fail("mlock one private page");
    int advice = posix_fadvise(fd, 0, (off_t)size, POSIX_FADV_DONTNEED);
    if (advice) { errno = advice; fail("posix_fadvise"); }
    /* Query both mappings before touching the fresh mapping or rereading the file. */
    unsigned int private_after_drop = resident((void *)cow, size);
    unsigned int fresh_after_drop = resident((void *)fresh, size);
    unsigned int private_byte = cow[0];
    unsigned int fresh_byte = fresh[0];
    unsigned int fresh_after_read = resident((void *)fresh, size);
    unsigned char file_byte;
    if (pread(fd, &file_byte, 1, 0) != 1) fail("pread");
    if (munlock((void *)cow, size)) fail("munlock");
    if (madvise((void *)cow, size, MADV_DONTNEED)) fail("madvise private copy");
    unsigned int private_after_discard_byte = cow[0];
    if (private_byte != 'B' || fresh_byte != 'A' || file_byte != 'A' ||
        private_after_discard_byte != 'A') {
        fputs("copy-on-write content control failed\n", stderr);
        return 1;
    }
    printf("{\"page_size\":%ld,\"filesystem_magic\":\"0x%lx\","
           "\"private_after_file_advice\":%u,\"fresh_after_file_advice\":%u,"
           "\"private_byte\":%u,\"fresh_byte\":%u,\"file_byte\":%u,"
           "\"fresh_after_read\":%u,\"private_after_discard_byte\":%u}\n",
           page_size, (unsigned long)fs.f_type, private_after_drop, fresh_after_drop,
           private_byte, fresh_byte, file_byte, fresh_after_read, private_after_discard_byte);
    if (munmap((void *)cow, size) || munmap((void *)fresh, size) || close(fd)) fail("cleanup");
    return 0;
}
