/*
 * vetro-files: the daemon of Vetro's file manager in the guest (M8,
 * ADR 0020, protocol in docs/specs/files.md).
 *
 * Listens on virtio-vsock (port 5200, from any CID) and serves the host
 * reads and writes of the guest's files through the guest kernel:
 * no direct access to the disk image, which with the guest running
 * would corrupt the file system.
 *
 *   list, stat (type, size, mtime, mode, uid/gid, symlink target,
 *   SELinux context from the security.selinux xattr if present),
 *   chunked read, atomic write (temporary file in the same
 *   directory, then rename: owner, mode and xattrs of the file being
 *   replaced are kept; new files take the owner and SELinux context
 *   of the directory), create, mkdir, delete (also
 *   recursive), rename, watch with inotify (live events);
 *   SQL on an SQLite database (ADR 0021) with the real SQLite engine, linked
 *   statically, in a child process with the uid and gid of the database
 *   owner: locks, journal and WAL like the app's.
 *
 * One process, one poll() loop: up to MAX_CLIENTS connections, each
 * with its own inotify and an output buffer (non-blocking writes: a host
 * that doesn't read doesn't stall the others). Prints only on a fatal error.
 *
 * Built static with musl by tools/guest-kernel/build.sh (like
 * vetro-dev) and started by /init if there is a virtio-vsock device. In
 * the future it goes into the Android image (userdebug, root), with bionic: it
 * uses only POSIX and Linux UAPI headers.
 *
 *   vetro-files [-p PORT]
 *
 * Multi-call program like BusyBox: with -DVETRO_SQLITE_SHELL and
 * argv[0] "sqlite3" the official SQLite shell starts (shell.c from the same
 * source), so the initramfs holds a single copy of the engine.
 */
#define _GNU_SOURCE
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <ftw.h>
#include <limits.h>
#include <poll.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/inotify.h>
#include <grp.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/xattr.h>
#include <unistd.h>
#include <linux/vm_sockets.h>
#include "sqlite3.h"

#define DEFAULT_PORT 5200
#define MAGIC 0x46525456u /* "VTRF" in little endian */
#define VERSION 2
/* Maximum bytes for READ and WDATA. */
#define MAX_CHUNK (1u << 20)
/* Maximum length of a request (WDATA plus the rest). */
#define MAX_REQUEST (MAX_CHUNK + 16384u)
#define MAX_CLIENTS 8
#define MAX_HANDLES 16
/* Beyond this many output bytes no further requests or events are read. */
#define OUT_HIGH (8u << 20)
#define TMP_PREFIX ".vetro-tmp."
#define SELINUX_XATTR "security.selinux"

enum {
	T_STAT = 1,
	T_LIST = 2,
	T_READ = 3,
	T_WOPEN = 4,
	T_WDATA = 5,
	T_WCOMMIT = 6,
	T_WABORT = 7,
	T_MKDIR = 8,
	T_CREATE = 9,
	T_DELETE = 10,
	T_RENAME = 11,
	T_WATCH = 12,
	T_UNWATCH = 13,
	T_SQL = 14,
	T_HELLO = 0x80,
	T_REPLY = 0x81,
	T_EVENT = 0x82,
};

enum { K_OTHER, K_FILE, K_DIR, K_SYMLINK, K_CHAR, K_BLOCK, K_FIFO, K_SOCKET };

#define WATCH_MASK                                                                                 \
	(IN_CREATE | IN_DELETE | IN_MODIFY | IN_CLOSE_WRITE | IN_MOVED_FROM | IN_MOVED_TO | IN_ATTRIB | \
	 IN_DELETE_SELF | IN_MOVE_SELF)

struct buf {
	uint8_t *p;
	size_t len, cap;
};

struct handle {
	int used;
	uint32_t id;
	int fd;
	int err; /* first error of a WDATA: the WCOMMIT fails */
	int existed;
	struct stat st; /* of the replaced file */
	uint32_t mode;  /* for a new file */
	char *tmp, *target;
};

struct client {
	int fd, ino;
	struct buf in, out;
	size_t out_off;
	struct handle h[MAX_HANDLES];
};

static struct client clients[MAX_CLIENTS];
static unsigned long tmp_counter;

/* ---- buffer ------------------------------------------------------------ */

static void need(struct buf *b, size_t n)
{
	if (b->len + n <= b->cap)
		return;
	size_t cap = b->cap ? b->cap : 4096;
	while (cap < b->len + n)
		cap *= 2;
	uint8_t *p = realloc(b->p, cap);
	if (!p) {
		fprintf(stderr, "vetro-files: out of memory\n");
		exit(1);
	}
	b->p = p;
	b->cap = cap;
}

static void put(struct buf *b, const void *d, size_t n)
{
	need(b, n);
	memcpy(b->p + b->len, d, n);
	b->len += n;
}

static void put_u8(struct buf *b, uint8_t v) { put(b, &v, 1); }

static void put_u16(struct buf *b, uint16_t v)
{
	uint8_t x[2] = {(uint8_t)v, (uint8_t)(v >> 8)};
	put(b, x, 2);
}

static void put_u32(struct buf *b, uint32_t v)
{
	uint8_t x[4] = {(uint8_t)v, (uint8_t)(v >> 8), (uint8_t)(v >> 16), (uint8_t)(v >> 24)};
	put(b, x, 4);
}

static void put_u64(struct buf *b, uint64_t v)
{
	put_u32(b, (uint32_t)v);
	put_u32(b, (uint32_t)(v >> 32));
}

static void put_str(struct buf *b, const char *s, size_t n)
{
	if (n > 0xffff)
		n = 0xffff;
	put_u16(b, (uint16_t)n);
	put(b, s, n);
}

static void put_cstr(struct buf *b, const char *s) { put_str(b, s, strlen(s)); }

static void set_u32(struct buf *b, size_t at, uint32_t v)
{
	b->p[at] = (uint8_t)v;
	b->p[at + 1] = (uint8_t)(v >> 8);
	b->p[at + 2] = (uint8_t)(v >> 16);
	b->p[at + 3] = (uint8_t)(v >> 24);
}

/* Start of a frame: length (to be filled in), type, id. */
static size_t frame_begin(struct buf *b, uint8_t type, uint32_t id)
{
	size_t at = b->len;
	put_u32(b, 0);
	put_u8(b, type);
	put_u32(b, id);
	return at;
}

static void frame_end(struct buf *b, size_t at) { set_u32(b, at, (uint32_t)(b->len - at - 4)); }

/* ---- reading requests ------------------------------------------------------ */

struct rd {
	const uint8_t *p;
	size_t len, pos;
	int bad;
};

static const uint8_t *take(struct rd *r, size_t n)
{
	if (r->bad || r->len - r->pos < n) {
		r->bad = 1;
		return NULL;
	}
	const uint8_t *p = r->p + r->pos;
	r->pos += n;
	return p;
}

static uint8_t get_u8(struct rd *r)
{
	const uint8_t *p = take(r, 1);
	return p ? p[0] : 0;
}

static uint16_t get_u16(struct rd *r)
{
	const uint8_t *p = take(r, 2);
	return p ? (uint16_t)(p[0] | p[1] << 8) : 0;
}

static uint32_t get_u32(struct rd *r)
{
	const uint8_t *p = take(r, 4);
	return p ? (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24 : 0;
}

static uint64_t get_u64(struct rd *r)
{
	uint64_t lo = get_u32(r);
	return lo | (uint64_t)get_u32(r) << 32;
}

/* A path: u16 length, bytes without NUL. Returns a NUL-terminated copy. */
static char *get_path(struct rd *r)
{
	uint16_t n = get_u16(r);
	const uint8_t *p = take(r, n);
	if (!p || n == 0 || memchr(p, 0, n)) {
		r->bad = 1;
		return NULL;
	}
	char *s = malloc((size_t)n + 1);
	if (!s)
		exit(1);
	memcpy(s, p, n);
	s[n] = 0;
	return s;
}

/* ---- stat --------------------------------------------------------------- */

static uint8_t kind_of(mode_t m)
{
	if (S_ISREG(m))
		return K_FILE;
	if (S_ISDIR(m))
		return K_DIR;
	if (S_ISLNK(m))
		return K_SYMLINK;
	if (S_ISCHR(m))
		return K_CHAR;
	if (S_ISBLK(m))
		return K_BLOCK;
	if (S_ISFIFO(m))
		return K_FIFO;
	if (S_ISSOCK(m))
		return K_SOCKET;
	return K_OTHER;
}

/* SELinux context of the file (without following links); 0 if absent. */
static ssize_t selinux_of(const char *path, char *out, size_t cap)
{
	ssize_t n = lgetxattr(path, SELINUX_XATTR, out, cap);
	if (n < 0)
		return 0;
	while (n > 0 && out[n - 1] == 0)
		n--;
	return n;
}

/* kind, mode, uid, gid, size, mtime (s, ns), nlink, target, context. */
static void put_stat(struct buf *b, const char *path, const struct stat *st)
{
	put_u8(b, kind_of(st->st_mode));
	put_u32(b, (uint32_t)st->st_mode);
	put_u32(b, (uint32_t)st->st_uid);
	put_u32(b, (uint32_t)st->st_gid);
	put_u64(b, (uint64_t)st->st_size);
	put_u64(b, (uint64_t)(int64_t)st->st_mtim.tv_sec);
	put_u32(b, (uint32_t)st->st_mtim.tv_nsec);
	put_u32(b, (uint32_t)st->st_nlink);
	char link[PATH_MAX];
	ssize_t n = S_ISLNK(st->st_mode) ? readlink(path, link, sizeof(link)) : 0;
	put_str(b, link, n > 0 ? (size_t)n : 0);
	char ctx[256];
	put_str(b, ctx, (size_t)selinux_of(path, ctx, sizeof(ctx)));
}

/* ---- operations ---------------------------------------------------------- */

static int cmp_names(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); }

static int do_list(struct buf *o, const char *path)
{
	DIR *d = opendir(path);
	if (!d)
		return errno;
	size_t n = 0, cap = 64;
	char **names = malloc(cap * sizeof(char *));
	struct dirent *e;
	while ((e = readdir(d))) {
		if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
			continue;
		if (n == cap)
			names = realloc(names, (cap *= 2) * sizeof(char *));
		names[n++] = strdup(e->d_name);
	}
	closedir(d);
	/* Fixed order (strcmp), whatever the file system's order is. */
	qsort(names, n, sizeof(char *), cmp_names);
	size_t count_at = o->len;
	put_u32(o, 0);
	uint32_t count = 0;
	size_t plen = strlen(path);
	for (size_t i = 0; i < n; i++) {
		size_t len = plen + 1 + strlen(names[i]) + 1;
		char *full = malloc(len);
		snprintf(full, len, "%s%s%s", path, plen && path[plen - 1] == '/' ? "" : "/", names[i]);
		struct stat st;
		if (lstat(full, &st) == 0) {
			put_cstr(o, names[i]);
			put_stat(o, full, &st);
			count++;
		}
		free(full);
		free(names[i]);
	}
	free(names);
	set_u32(o, count_at, count);
	return 0;
}

static int do_read(struct buf *o, const char *path, uint64_t off, uint32_t len)
{
	if (len > MAX_CHUNK)
		len = MAX_CHUNK;
	int fd = open(path, O_RDONLY | O_CLOEXEC);
	if (fd < 0)
		return errno;
	struct stat st;
	if (fstat(fd, &st)) {
		int e = errno;
		close(fd);
		return e;
	}
	if (S_ISDIR(st.st_mode)) {
		close(fd);
		return EISDIR;
	}
	put_u64(o, (uint64_t)st.st_size);
	size_t len_at = o->len;
	put_u32(o, 0);
	need(o, len);
	uint32_t got = 0;
	while (got < len) {
		ssize_t r = pread(fd, o->p + o->len, len - got, (off_t)(off + got));
		if (r < 0) {
			if (errno == EINTR)
				continue;
			int e = errno;
			close(fd);
			return e;
		}
		if (r == 0)
			break;
		o->len += (size_t)r;
		got += (uint32_t)r;
	}
	close(fd);
	set_u32(o, len_at, got);
	return 0;
}

/* Directory and name of a path (copies). */
static void split(const char *path, char **dir, char **base)
{
	const char *s = strrchr(path, '/');
	if (!s) {
		*dir = strdup(".");
		*base = strdup(path);
	} else if (s == path) {
		*dir = strdup("/");
		*base = strdup(s + 1);
	} else {
		*dir = strndup(path, (size_t)(s - path));
		*base = strdup(s + 1);
	}
}

/*
 * Owner and SELinux context of the directory containing `path`, applied to a
 * new file (`fd` >= 0) or to `path` itself: as Android does for an app's
 * files, which have the uid, gid and context of its data directory.
 */
static int inherit_parent(const char *path, int fd)
{
	char *dir, *base;
	split(path, &dir, &base);
	struct stat ps;
	int e = 0;
	if (stat(dir, &ps))
		e = errno;
	else if ((fd >= 0 ? fchown(fd, ps.st_uid, ps.st_gid) : lchown(path, ps.st_uid, ps.st_gid)) && errno != EPERM)
		e = errno;
	if (!e) {
		char ctx[256];
		ssize_t n = lgetxattr(dir, SELINUX_XATTR, ctx, sizeof(ctx));
		if (n > 0 && (fd >= 0 ? fsetxattr(fd, SELINUX_XATTR, ctx, (size_t)n, 0)
				      : lsetxattr(path, SELINUX_XATTR, ctx, (size_t)n, 0)))
			e = errno;
	}
	free(dir);
	free(base);
	return e;
}

/* Copies the xattrs of `src` to `fd`. security.selinux is mandatory. */
static int copy_xattrs(const char *src, int fd)
{
	ssize_t n = llistxattr(src, NULL, 0);
	if (n <= 0)
		return 0; /* none, or a file system without xattrs */
	char *names = malloc((size_t)n);
	n = llistxattr(src, names, (size_t)n);
	int e = 0;
	for (ssize_t i = 0; n > 0 && i < n && !e; i += (ssize_t)strlen(names + i) + 1) {
		const char *name = names + i;
		ssize_t vn = lgetxattr(src, name, NULL, 0);
		if (vn < 0)
			continue;
		char *v = malloc(vn ? (size_t)vn : 1);
		vn = lgetxattr(src, name, v, (size_t)vn);
		if (vn >= 0 && fsetxattr(fd, name, v, (size_t)vn, 0) && !strcmp(name, SELINUX_XATTR))
			e = errno;
		free(v);
	}
	free(names);
	return e;
}

static struct handle *find_handle(struct client *c, uint32_t id)
{
	for (int i = 0; i < MAX_HANDLES; i++)
		if (c->h[i].used && c->h[i].id == id)
			return &c->h[i];
	return NULL;
}

static void drop_handle(struct handle *h, int unlink_tmp)
{
	if (h->fd >= 0)
		close(h->fd);
	if (unlink_tmp && h->tmp)
		unlink(h->tmp);
	free(h->tmp);
	free(h->target);
	memset(h, 0, sizeof(*h));
	h->fd = -1;
}

static int do_wopen(struct client *c, uint32_t id, const char *path, uint32_t mode, uint8_t flags)
{
	if (find_handle(c, id))
		return EBUSY;
	struct handle *h = NULL;
	for (int i = 0; i < MAX_HANDLES && !h; i++)
		if (!c->h[i].used)
			h = &c->h[i];
	if (!h)
		return EMFILE;
	/* A symbolic link stays: the file it points to is replaced. */
	struct stat ls;
	char *target = NULL;
	if (lstat(path, &ls) == 0 && S_ISLNK(ls.st_mode)) {
		target = realpath(path, NULL);
		if (!target)
			return errno;
	} else {
		target = strdup(path);
	}
	struct stat st;
	int existed = stat(target, &st) == 0;
	int e = 0;
	if (!existed && errno != ENOENT)
		e = errno;
	else if (existed && S_ISDIR(st.st_mode))
		e = EISDIR;
	else if (existed && !S_ISREG(st.st_mode))
		e = EINVAL;
	else if (existed && (flags & 1))
		e = EEXIST;
	if (e) {
		free(target);
		return e;
	}
	char *dir, *base;
	split(target, &dir, &base);
	size_t len = strlen(dir) + strlen(base) + 64;
	char *tmp = malloc(len);
	/* Long names: without the original name (NAME_MAX). */
	snprintf(tmp, len, "%s/%s%lu.%s", dir, TMP_PREFIX, ++tmp_counter, strlen(base) > 200 ? "x" : base);
	free(dir);
	free(base);
	int fd = open(tmp, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0600);
	if (fd < 0) {
		e = errno;
		free(tmp);
		free(target);
		return e;
	}
	memset(h, 0, sizeof(*h));
	h->used = 1;
	h->id = id;
	h->fd = fd;
	h->existed = existed;
	if (existed)
		h->st = st;
	h->mode = mode & 07777;
	h->tmp = tmp;
	h->target = target;
	return 0;
}

static int do_wdata(struct client *c, uint32_t id, uint64_t off, const uint8_t *data, uint32_t len)
{
	struct handle *h = find_handle(c, id);
	if (!h)
		return EBADF;
	if (h->err)
		return h->err;
	for (uint32_t done = 0; done < len;) {
		ssize_t w = pwrite(h->fd, data + done, len - done, (off_t)(off + done));
		if (w < 0 && errno == EINTR)
			continue;
		if (w <= 0) {
			h->err = w < 0 ? errno : EIO;
			return h->err;
		}
		done += (uint32_t)w;
	}
	return 0;
}

static int do_wcommit(struct client *c, struct buf *o, uint32_t id)
{
	struct handle *h = find_handle(c, id);
	if (!h)
		return EBADF;
	int e = h->err;
	if (!e && h->existed) {
		/* chown before chmod: chown clears setuid and setgid. */
		if (fchown(h->fd, h->st.st_uid, h->st.st_gid) || fchmod(h->fd, h->st.st_mode & 07777))
			e = errno;
		else
			e = copy_xattrs(h->target, h->fd);
	} else if (!e) {
		e = inherit_parent(h->target, h->fd);
		if (!e && fchmod(h->fd, h->mode))
			e = errno;
	}
	if (!e && fsync(h->fd))
		e = errno;
	if (!e) {
		close(h->fd);
		h->fd = -1;
		if (rename(h->tmp, h->target))
			e = errno;
	}
	if (e) {
		drop_handle(h, 1);
		return e;
	}
	char *dir, *base;
	split(h->target, &dir, &base);
	int dfd = open(dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
	if (dfd >= 0) {
		fsync(dfd);
		close(dfd);
	}
	free(dir);
	free(base);
	struct stat st;
	if (lstat(h->target, &st))
		e = errno;
	else
		put_stat(o, h->target, &st);
	drop_handle(h, 0);
	return e;
}

static int do_mkdir(const char *path, uint32_t mode)
{
	if (mkdir(path, mode & 07777))
		return errno;
	int e = inherit_parent(path, -1);
	if (!e && chmod(path, mode & 07777))
		e = errno;
	return e;
}

static int do_create(const char *path, uint32_t mode)
{
	int fd = open(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode & 07777);
	if (fd < 0)
		return errno;
	int e = inherit_parent(path, fd);
	if (!e && fchmod(fd, mode & 07777))
		e = errno;
	close(fd);
	return e;
}

static int rm_one(const char *path, const struct stat *st, int flag, struct FTW *f)
{
	(void)st;
	(void)f;
	return (flag == FTW_DP ? rmdir(path) : unlink(path)) ? errno : 0;
}

static int do_delete(const char *path, uint8_t flags)
{
	struct stat st;
	if (lstat(path, &st))
		return errno;
	if (!S_ISDIR(st.st_mode))
		return unlink(path) ? errno : 0;
	if (flags & 1) {
		int r = nftw(path, rm_one, 16, FTW_DEPTH | FTW_PHYS);
		return r < 0 ? errno : r;
	}
	return rmdir(path) ? errno : 0;
}

/* ---- SQL (ADR 0021) --------------------------------------------------------- */

/* Value types in the protocol. */
enum { V_NULL, V_INT, V_REAL, V_TEXT, V_BLOB };

#define SQL_READONLY 1
#define SQL_ANY_CHANGES 0xffffffffu
/* Maximum rows and bytes returned. */
#define SQL_MAX_ROWS 10000u
#define SQL_MAX_BYTES (16u << 20)
#define SQL_BUSY_MS 2000

struct sqlreq {
	uint8_t flags;
	uint32_t expect;
	const uint8_t *sql;
	uint32_t sql_len;
	uint16_t nparams;
	const uint8_t *params; /* encoded values, already checked */
	size_t params_len;
};

/* Skips (and checks) an encoded value. */
static void skip_value(struct rd *r)
{
	uint8_t t = get_u8(r);
	switch (t) {
	case V_NULL:
		break;
	case V_INT:
	case V_REAL:
		take(r, 8);
		break;
	case V_TEXT:
	case V_BLOB:
		take(r, get_u32(r));
		break;
	default:
		r->bad = 1;
	}
}

/* Binds parameter `i` (from 1) to the value read from `r`. */
static int bind_value(sqlite3_stmt *st, int i, struct rd *r)
{
	uint8_t t = get_u8(r);
	switch (t) {
	case V_INT:
		return sqlite3_bind_int64(st, i, (sqlite3_int64)get_u64(r));
	case V_REAL: {
		uint64_t bits = get_u64(r);
		double d;
		memcpy(&d, &bits, sizeof(d));
		return sqlite3_bind_double(st, i, d);
	}
	case V_TEXT: {
		uint32_t n = get_u32(r);
		const uint8_t *s = take(r, n);
		return sqlite3_bind_text64(st, i, s ? (const char *)s : "", n, SQLITE_TRANSIENT, SQLITE_UTF8);
	}
	case V_BLOB: {
		uint32_t n = get_u32(r);
		const uint8_t *b = take(r, n);
		return n ? sqlite3_bind_blob64(st, i, b, n, SQLITE_TRANSIENT) : sqlite3_bind_zeroblob(st, i, 0);
	}
	default:
		return sqlite3_bind_null(st, i);
	}
}

/* Binds parameters 1..n of the statement (missing ones stay NULL). */
static int bind_all(sqlite3_stmt *st, const struct sqlreq *q)
{
	struct rd r = {q->params, q->params_len, 0, 0};
	int n = sqlite3_bind_parameter_count(st);
	for (int i = 1; i <= n && i <= (int)q->nparams; i++) {
		int rc = bind_value(st, i, &r);
		if (rc != SQLITE_OK)
			return rc;
	}
	return SQLITE_OK;
}

static void put_value(struct buf *o, sqlite3_stmt *st, int c)
{
	switch (sqlite3_column_type(st, c)) {
	case SQLITE_INTEGER:
		put_u8(o, V_INT);
		put_u64(o, (uint64_t)sqlite3_column_int64(st, c));
		break;
	case SQLITE_FLOAT: {
		double d = sqlite3_column_double(st, c);
		uint64_t bits;
		memcpy(&bits, &d, sizeof(bits));
		put_u8(o, V_REAL);
		put_u64(o, bits);
		break;
	}
	case SQLITE_TEXT: {
		const unsigned char *s = sqlite3_column_text(st, c);
		uint32_t n = (uint32_t)sqlite3_column_bytes(st, c);
		put_u8(o, V_TEXT);
		put_u32(o, n);
		put(o, s, n);
		break;
	}
	case SQLITE_BLOB: {
		const void *b = sqlite3_column_blob(st, c);
		uint32_t n = (uint32_t)sqlite3_column_bytes(st, c);
		put_u8(o, V_BLOB);
		put_u32(o, n);
		if (n)
			put(o, b, n);
		break;
	}
	default:
		put_u8(o, V_NULL);
	}
}

struct sqlres {
	struct buf cols, rows;
	uint16_t ncols;
	uint32_t nrows;
	int truncated;
	uint64_t changes;
	char *msg;
};

static void set_msg(struct sqlres *res, const char *m)
{
	if (!res->msg)
		res->msg = strdup(m);
}

/*
 * Runs the statements of `q` on the open database: all in one transaction
 * (except read-only); rows and columns of the last statement that
 * returns any. Returns the SQLite code.
 */
static int sql_run(sqlite3 *db, const struct sqlreq *q, struct sqlres *res)
{
	int ro = q->flags & SQL_READONLY;
	int rc = ro ? SQLITE_OK : sqlite3_exec(db, "BEGIN IMMEDIATE", NULL, NULL, NULL);
	if (rc != SQLITE_OK)
		set_msg(res, sqlite3_errmsg(db));
	const char *tail = (const char *)q->sql;
	const char *end = tail + q->sql_len;
	while (rc == SQLITE_OK && tail < end) {
		sqlite3_stmt *st = NULL;
		const char *next = NULL;
		rc = sqlite3_prepare_v2(db, tail, (int)(end - tail), &st, &next);
		if (rc != SQLITE_OK) {
			set_msg(res, sqlite3_errmsg(db));
			break;
		}
		tail = next;
		if (!st)
			continue; /* whitespace or comments */
		rc = bind_all(st, q);
		int n = sqlite3_column_count(st);
		if (rc == SQLITE_OK && n > 0) {
			/* A statement with columns: its rows replace the others. */
			res->cols.len = res->rows.len = 0;
			res->nrows = 0;
			res->truncated = 0;
			res->ncols = (uint16_t)(n > 0xffff ? 0xffff : n);
			for (int c = 0; c < res->ncols; c++) {
				const char *name = sqlite3_column_name(st, c);
				put_cstr(&res->cols, name ? name : "");
			}
		}
		sqlite3_int64 before = sqlite3_total_changes64(db);
		while (rc == SQLITE_OK) {
			int s = sqlite3_step(st);
			if (s == SQLITE_DONE)
				break;
			if (s != SQLITE_ROW) {
				rc = s;
				break;
			}
			if (res->nrows >= SQL_MAX_ROWS || res->rows.len >= SQL_MAX_BYTES) {
				res->truncated = 1;
				continue;
			}
			for (int c = 0; c < res->ncols; c++)
				put_value(&res->rows, st, c);
			res->nrows++;
		}
		/* Rows changed by the statement itself (not by triggers). */
		if (rc == SQLITE_OK && sqlite3_total_changes64(db) != before)
			res->changes += (uint64_t)sqlite3_changes64(db);
		if (rc != SQLITE_OK)
			set_msg(res, sqlite3_errmsg(db));
		sqlite3_finalize(st);
	}
	if (rc == SQLITE_OK && q->expect != SQL_ANY_CHANGES && res->changes != q->expect) {
		rc = SQLITE_CONSTRAINT;
		char m[128];
		snprintf(m, sizeof(m), "%llu righe cambiate, attese %u: annullato", (unsigned long long)res->changes,
			 q->expect);
		set_msg(res, m);
	}
	if (!ro) {
		if (rc == SQLITE_OK) {
			rc = sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
			if (rc != SQLITE_OK)
				set_msg(res, sqlite3_errmsg(db));
		}
		if (rc != SQLITE_OK && !sqlite3_get_autocommit(db))
			sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL);
	}
	return rc;
}

/*
 * The child: becomes the database owner, opens it, runs and writes
 * `u32 errno` to the pipe, then (if 0) the body of the SQL reply.
 */
static void sql_child(int out, const char *path, const struct stat *st, const struct sqlreq *q)
{
	struct buf o = {0};
	uint32_t e = 0;
	if (geteuid() == 0 && (setgroups(0, NULL) || setresgid(st->st_gid, st->st_gid, st->st_gid) ||
			       setresuid(st->st_uid, st->st_uid, st->st_uid)))
		e = (uint32_t)errno;
	put_u32(&o, e);
	if (!e) {
		sqlite3 *db = NULL;
		int flags = (q->flags & SQL_READONLY ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE);
		int rc = sqlite3_open_v2(path, &db, flags, NULL);
		struct sqlres res = {0};
		if (rc == SQLITE_OK) {
			sqlite3_busy_timeout(db, SQL_BUSY_MS);
			rc = sql_run(db, q, &res);
		} else {
			set_msg(&res, db ? sqlite3_errmsg(db) : sqlite3_errstr(rc));
		}
		put_u32(&o, (uint32_t)rc);
		put_cstr(&o, rc == SQLITE_OK || !res.msg ? "" : res.msg);
		if (rc == SQLITE_OK) {
			put_u64(&o, res.changes);
			put_u64(&o, (uint64_t)sqlite3_last_insert_rowid(db));
			put_u8(&o, (uint8_t)res.truncated);
			put_u16(&o, res.ncols);
			if (res.cols.len)
				put(&o, res.cols.p, res.cols.len);
			put_u32(&o, res.nrows);
			if (res.rows.len)
				put(&o, res.rows.p, res.rows.len);
		}
		sqlite3_close_v2(db);
	}
	for (size_t done = 0; done < o.len;) {
		ssize_t w = write(out, o.p + done, o.len - done);
		if (w < 0 && errno == EINTR)
			continue;
		if (w <= 0)
			_exit(1);
		done += (size_t)w;
	}
	_exit(0);
}

/*
 * The files SQLite keeps next to the database (-wal, -shm, -journal)
 * take the owner and SELinux context of the database, if different.
 */
static void sql_fix_sidecars(const char *path, const struct stat *st)
{
	static const char *const suffix[] = {"-wal", "-shm", "-journal"};
	char ctx[256];
	ssize_t cn = lgetxattr(path, SELINUX_XATTR, ctx, sizeof(ctx));
	for (size_t i = 0; i < sizeof(suffix) / sizeof(suffix[0]); i++) {
		size_t len = strlen(path) + 16;
		char *side = malloc(len);
		snprintf(side, len, "%s%s", path, suffix[i]);
		struct stat ss;
		if (lstat(side, &ss) == 0 && S_ISREG(ss.st_mode)) {
			if ((ss.st_uid != st->st_uid || ss.st_gid != st->st_gid) && lchown(side, st->st_uid, st->st_gid))
				fprintf(stderr, "vetro-files: chown %s: %s\n", side, strerror(errno));
			char have[256];
			ssize_t hn = lgetxattr(side, SELINUX_XATTR, have, sizeof(have));
			if (cn > 0 && (hn != cn || memcmp(have, ctx, (size_t)cn)) &&
			    lsetxattr(side, SELINUX_XATTR, ctx, (size_t)cn, 0))
				fprintf(stderr, "vetro-files: context of %s: %s\n", side, strerror(errno));
		}
		free(side);
	}
}

static int do_sql(struct buf *o, const char *path, const struct sqlreq *q)
{
	struct stat st;
	if (stat(path, &st))
		return errno;
	if (S_ISDIR(st.st_mode))
		return EISDIR;
	if (!S_ISREG(st.st_mode))
		return EINVAL;
	int p[2];
	if (pipe2(p, O_CLOEXEC))
		return errno;
	pid_t pid = fork();
	if (pid < 0) {
		int e = errno;
		close(p[0]);
		close(p[1]);
		return e;
	}
	if (pid == 0) {
		close(p[0]);
		sql_child(p[1], path, &st, q);
	}
	close(p[1]);
	/* The child writes everything and exits: read to the end, then wait. */
	size_t start = o->len;
	uint8_t tmp[65536];
	for (;;) {
		ssize_t n = read(p[0], tmp, sizeof(tmp));
		if (n < 0 && errno == EINTR)
			continue;
		if (n <= 0)
			break;
		put(o, tmp, (size_t)n);
	}
	close(p[0]);
	int status = 0;
	while (waitpid(pid, &status, 0) < 0 && errno == EINTR)
		;
	if (!(q->flags & SQL_READONLY))
		sql_fix_sidecars(path, &st);
	size_t got = o->len - start;
	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0 || got < 4) {
		o->len = start;
		return EIO;
	}
	uint32_t e = (uint32_t)o->p[start] | (uint32_t)o->p[start + 1] << 8 | (uint32_t)o->p[start + 2] << 16 |
		     (uint32_t)o->p[start + 3] << 24;
	/* The body is what follows the child's errno. */
	memmove(o->p + start, o->p + start + 4, got - 4);
	o->len -= 4;
	if (e) {
		o->len = start;
		return (int)e;
	}
	return 0;
}

/* Reads the rest of an SQL request (after the path). */
static void get_sqlreq(struct rd *r, struct sqlreq *q)
{
	q->flags = get_u8(r);
	q->expect = get_u32(r);
	q->sql_len = get_u32(r);
	q->sql = take(r, q->sql_len);
	q->nparams = get_u16(r);
	size_t at = r->pos;
	for (unsigned i = 0; i < q->nparams && !r->bad; i++)
		skip_value(r);
	q->params = r->p + at;
	q->params_len = r->pos - at;
}

/* ---- connections ----------------------------------------------------------- */

static void reply_hello(struct client *c)
{
	size_t at = frame_begin(&c->out, T_HELLO, 0);
	put_u32(&c->out, MAGIC);
	put_u16(&c->out, VERSION);
	put_u16(&c->out, access("/sys/fs/selinux", F_OK) == 0 ? 1 : 0);
	put_u32(&c->out, MAX_CHUNK);
	frame_end(&c->out, at);
}

static void handle_request(struct client *c, const uint8_t *p, size_t len)
{
	struct rd r = {p, len, 0, 0};
	uint8_t type = get_u8(&r);
	uint32_t id = get_u32(&r);
	struct buf *o = &c->out;
	size_t at = frame_begin(o, T_REPLY, id);
	size_t status_at = o->len;
	put_u32(o, 0);
	size_t body = o->len;
	int e = EPROTO;
	char *path = NULL, *path2 = NULL;
	switch (type) {
	case T_STAT: {
		path = get_path(&r);
		struct stat st;
		if (r.bad)
			break;
		e = lstat(path, &st) ? errno : 0;
		if (!e)
			put_stat(o, path, &st);
		break;
	}
	case T_LIST:
		path = get_path(&r);
		if (!r.bad)
			e = do_list(o, path);
		break;
	case T_READ: {
		path = get_path(&r);
		uint64_t off = get_u64(&r);
		uint32_t n = get_u32(&r);
		if (!r.bad)
			e = do_read(o, path, off, n);
		break;
	}
	case T_WOPEN: {
		uint32_t h = get_u32(&r);
		path = get_path(&r);
		uint32_t mode = get_u32(&r);
		uint8_t flags = get_u8(&r);
		if (!r.bad)
			e = do_wopen(c, h, path, mode, flags);
		break;
	}
	case T_WDATA: {
		uint32_t h = get_u32(&r);
		uint64_t off = get_u64(&r);
		uint32_t n = get_u32(&r);
		const uint8_t *d = take(&r, n);
		if (!r.bad)
			e = n > MAX_CHUNK ? EMSGSIZE : do_wdata(c, h, off, d, n);
		break;
	}
	case T_WCOMMIT: {
		uint32_t h = get_u32(&r);
		if (!r.bad)
			e = do_wcommit(c, o, h);
		break;
	}
	case T_WABORT: {
		uint32_t h = get_u32(&r);
		struct handle *hd = r.bad ? NULL : find_handle(c, h);
		e = r.bad ? EPROTO : hd ? 0 : EBADF;
		if (hd)
			drop_handle(hd, 1);
		break;
	}
	case T_MKDIR: {
		path = get_path(&r);
		uint32_t mode = get_u32(&r);
		if (!r.bad)
			e = do_mkdir(path, mode);
		break;
	}
	case T_CREATE: {
		path = get_path(&r);
		uint32_t mode = get_u32(&r);
		if (!r.bad)
			e = do_create(path, mode);
		break;
	}
	case T_DELETE: {
		path = get_path(&r);
		uint8_t flags = get_u8(&r);
		if (!r.bad)
			e = do_delete(path, flags);
		break;
	}
	case T_RENAME:
		path = get_path(&r);
		path2 = get_path(&r);
		if (!r.bad)
			e = rename(path, path2) ? errno : 0;
		break;
	case T_WATCH: {
		path = get_path(&r);
		if (r.bad)
			break;
		int wd = c->ino < 0 ? (errno = ENOSYS, -1) : inotify_add_watch(c->ino, path, WATCH_MASK);
		e = wd < 0 ? errno : 0;
		if (!e)
			put_u32(o, (uint32_t)wd);
		break;
	}
	case T_UNWATCH: {
		uint32_t wd = get_u32(&r);
		if (!r.bad)
			e = inotify_rm_watch(c->ino, (int)wd) ? errno : 0;
		break;
	}
	case T_SQL: {
		path = get_path(&r);
		struct sqlreq q;
		get_sqlreq(&r, &q);
		if (!r.bad)
			e = do_sql(o, path, &q);
		break;
	}
	default:
		e = ENOSYS;
		break;
	}
	free(path);
	free(path2);
	if (e)
		o->len = body; /* no body with an error */
	set_u32(o, status_at, (uint32_t)e);
	frame_end(o, at);
}

static void drop_client(struct client *c)
{
	for (int i = 0; i < MAX_HANDLES; i++)
		if (c->h[i].used)
			drop_handle(&c->h[i], 1);
	close(c->fd);
	if (c->ino >= 0)
		close(c->ino);
	free(c->in.p);
	free(c->out.p);
	memset(c, 0, sizeof(*c));
	c->fd = -1;
}

/* Writes as much as the socket accepts. -1 if the connection is lost. */
static int flush_out(struct client *c)
{
	while (c->out_off < c->out.len) {
		ssize_t w = send(c->fd, c->out.p + c->out_off, c->out.len - c->out_off, MSG_NOSIGNAL);
		if (w < 0 && errno == EINTR)
			continue;
		if (w < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
			return 0;
		if (w <= 0)
			return -1;
		c->out_off += (size_t)w;
	}
	c->out.len = 0;
	c->out_off = 0;
	return 0;
}

/* Reads from the socket and serves complete requests. -1 if the connection has ended. */
static int serve_input(struct client *c)
{
	uint8_t tmp[65536];
	ssize_t n = recv(c->fd, tmp, sizeof(tmp), 0);
	if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR))
		return 0;
	if (n <= 0)
		return -1;
	put(&c->in, tmp, (size_t)n);
	size_t pos = 0;
	while (c->in.len - pos >= 4) {
		const uint8_t *p = c->in.p + pos;
		uint32_t len = (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24;
		if (len < 5 || len > MAX_REQUEST)
			return -1;
		if (c->in.len - pos - 4 < len)
			break;
		handle_request(c, p + 4, len);
		pos += 4 + (size_t)len;
	}
	memmove(c->in.p, c->in.p + pos, c->in.len - pos);
	c->in.len -= pos;
	return 0;
}

/* inotify events to the host, except those of our temporary files. */
static int serve_events(struct client *c)
{
	char evbuf[65536] __attribute__((aligned(__alignof__(struct inotify_event))));
	ssize_t n = read(c->ino, evbuf, sizeof(evbuf));
	if (n <= 0)
		return 0;
	for (char *p = evbuf; p < evbuf + n;) {
		struct inotify_event *ev = (struct inotify_event *)p;
		const char *name = ev->len ? ev->name : "";
		if (strncmp(name, TMP_PREFIX, strlen(TMP_PREFIX))) {
			size_t at = frame_begin(&c->out, T_EVENT, 0);
			put_u32(&c->out, (uint32_t)ev->wd);
			put_u32(&c->out, ev->mask);
			put_u32(&c->out, ev->cookie);
			put_cstr(&c->out, name);
			frame_end(&c->out, at);
		}
		p += sizeof(struct inotify_event) + ev->len;
	}
	return 0;
}

#ifdef VETRO_SQLITE_SHELL
/* main of shell.c, compiled with -Dmain=sqlite3_shell_main. */
int sqlite3_shell_main(int argc, char **argv);
#endif

int main(int argc, char **argv)
{
#ifdef VETRO_SQLITE_SHELL
	const char *me = strrchr(argv[0], '/');
	if (!strcmp(me ? me + 1 : argv[0], "sqlite3"))
		return sqlite3_shell_main(argc, argv);
#endif
	unsigned port = DEFAULT_PORT;
	if (argc == 3 && !strcmp(argv[1], "-p"))
		port = (unsigned)strtoul(argv[2], NULL, 10);
	else if (argc != 1) {
		fprintf(stderr, "usage: vetro-files [-p PORT]\n");
		return 2;
	}
	signal(SIGPIPE, SIG_IGN);
	/* The modes requested by the host apply as they are. */
	umask(0);
	int ls = socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC, 0);
	struct sockaddr_vm a = {.svm_family = AF_VSOCK, .svm_cid = VMADDR_CID_ANY, .svm_port = port};
	if (ls < 0 || bind(ls, (struct sockaddr *)&a, sizeof(a)) || listen(ls, MAX_CLIENTS)) {
		fprintf(stderr, "vetro-files: vsock port %u: %s\n", port, strerror(errno));
		return 1;
	}
	for (int i = 0; i < MAX_CLIENTS; i++)
		clients[i].fd = -1;
	for (;;) {
		struct pollfd pf[1 + 2 * MAX_CLIENTS];
		int who[1 + 2 * MAX_CLIENTS];
		int n = 0;
		pf[n] = (struct pollfd){.fd = ls, .events = POLLIN};
		who[n++] = -1;
		for (int i = 0; i < MAX_CLIENTS; i++) {
			struct client *c = &clients[i];
			if (c->fd < 0)
				continue;
			int busy = c->out.len - c->out_off > OUT_HIGH;
			pf[n] = (struct pollfd){.fd = c->fd,
						.events = (short)((busy ? 0 : POLLIN) | (c->out.len > c->out_off ? POLLOUT : 0))};
			who[n++] = i;
			pf[n] = (struct pollfd){.fd = busy || c->ino < 0 ? -1 : c->ino, .events = POLLIN};
			who[n++] = i;
		}
		if (poll(pf, (nfds_t)n, -1) < 0) {
			if (errno == EINTR)
				continue;
			fprintf(stderr, "vetro-files: poll: %s\n", strerror(errno));
			return 1;
		}
		for (int k = 0; k < n; k++) {
			if (!pf[k].revents)
				continue;
			if (who[k] < 0) {
				int fd = accept4(ls, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC);
				if (fd < 0)
					continue;
				struct client *c = NULL;
				for (int i = 0; i < MAX_CLIENTS && !c; i++)
					if (clients[i].fd < 0)
						c = &clients[i];
				if (!c) {
					close(fd);
					continue;
				}
				/* Without inotify in the kernel (-1) WATCH replies with the error. */
				int ino = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
				memset(c, 0, sizeof(*c));
				c->fd = fd;
				c->ino = ino;
				for (int h = 0; h < MAX_HANDLES; h++)
					c->h[h].fd = -1;
				reply_hello(c);
				if (flush_out(c))
					drop_client(c);
				continue;
			}
			struct client *c = &clients[who[k]];
			if (c->fd < 0)
				continue;
			int bad = 0;
			if (pf[k].fd >= 0 && pf[k].fd == c->ino) {
				serve_events(c);
			} else {
				if (pf[k].revents & (POLLIN | POLLHUP | POLLERR))
					bad = serve_input(c);
			}
			if (!bad)
				bad = flush_out(c);
			if (bad)
				drop_client(c);
		}
	}
}
