Blame SOURCES/hv_vss_daemon.c

7b0ce3
/*
7b0ce3
 * An implementation of the host initiated guest snapshot for Hyper-V.
7b0ce3
 *
7b0ce3
 *
7b0ce3
 * Copyright (C) 2013, Microsoft, Inc.
7b0ce3
 * Author : K. Y. Srinivasan <kys@microsoft.com>
7b0ce3
 *
7b0ce3
 * This program is free software; you can redistribute it and/or modify it
7b0ce3
 * under the terms of the GNU General Public License version 2 as published
7b0ce3
 * by the Free Software Foundation.
7b0ce3
 *
7b0ce3
 * This program is distributed in the hope that it will be useful, but
7b0ce3
 * WITHOUT ANY WARRANTY; without even the implied warranty of
7b0ce3
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
7b0ce3
 * NON INFRINGEMENT.  See the GNU General Public License for more
7b0ce3
 * details.
7b0ce3
 *
7b0ce3
 */
7b0ce3
7b0ce3
7b0ce3
#include <sys types.h="">
7b0ce3
#include <sys socket.h="">
7b0ce3
#include <sys poll.h="">
7b0ce3
#include <sys ioctl.h="">
7b0ce3
#include <fcntl.h>
7b0ce3
#include <stdio.h>
7b0ce3
#include <mntent.h>
7b0ce3
#include <stdlib.h>
7b0ce3
#include <unistd.h>
7b0ce3
#include <string.h>
7b0ce3
#include <ctype.h>
7b0ce3
#include <errno.h>
7b0ce3
#include <arpa inet.h="">
7b0ce3
#include <linux fs.h="">
7b0ce3
#include <linux connector.h="">
7b0ce3
#include <linux hyperv.h="">
7b0ce3
#include <linux netlink.h="">
7b0ce3
#include <syslog.h>
7b0ce3
7b0ce3
static struct sockaddr_nl addr;
7b0ce3
7b0ce3
#ifndef SOL_NETLINK
7b0ce3
#define SOL_NETLINK 270
7b0ce3
#endif
7b0ce3
7b0ce3
7b0ce3
static int vss_do_freeze(char *dir, unsigned int cmd, char *fs_op)
7b0ce3
{
7b0ce3
	int ret, fd = open(dir, O_RDONLY);
7b0ce3
7b0ce3
	if (fd < 0)
7b0ce3
		return 1;
7b0ce3
	ret = ioctl(fd, cmd, 0);
7b0ce3
	syslog(LOG_INFO, "VSS: %s of %s: %s\n", fs_op, dir, strerror(errno));
7b0ce3
	close(fd);
7b0ce3
	return !!ret;
7b0ce3
}
7b0ce3
7b0ce3
static int vss_operate(int operation)
7b0ce3
{
7b0ce3
	char *fs_op;
7b0ce3
	char match[] = "/dev/";
7b0ce3
	FILE *mounts;
7b0ce3
	struct mntent *ent;
7b0ce3
	unsigned int cmd;
7b0ce3
	int error = 0, root_seen = 0;
7b0ce3
7b0ce3
	switch (operation) {
7b0ce3
	case VSS_OP_FREEZE:
7b0ce3
		cmd = FIFREEZE;
7b0ce3
		fs_op = "freeze";
7b0ce3
		break;
7b0ce3
	case VSS_OP_THAW:
7b0ce3
		cmd = FITHAW;
7b0ce3
		fs_op = "thaw";
7b0ce3
		break;
7b0ce3
	default:
7b0ce3
		return -1;
7b0ce3
	}
7b0ce3
7b0ce3
	mounts = setmntent("/proc/mounts", "r");
7b0ce3
	if (mounts == NULL)
7b0ce3
		return -1;
7b0ce3
7b0ce3
	while ((ent = getmntent(mounts))) {
7b0ce3
		if (strncmp(ent->mnt_fsname, match, strlen(match)))
7b0ce3
			continue;
7b0ce3
		if (strcmp(ent->mnt_type, "iso9660") == 0)
7b0ce3
			continue;
41f5a8
		if (strcmp(ent->mnt_type, "vfat") == 0)
41f5a8
			continue;
7b0ce3
		if (strcmp(ent->mnt_dir, "/") == 0) {
7b0ce3
			root_seen = 1;
7b0ce3
			continue;
7b0ce3
		}
7b0ce3
		error |= vss_do_freeze(ent->mnt_dir, cmd, fs_op);
7b0ce3
	}
7b0ce3
	endmntent(mounts);
7b0ce3
7b0ce3
	if (root_seen) {
7b0ce3
		error |= vss_do_freeze("/", cmd, fs_op);
7b0ce3
	}
7b0ce3
7b0ce3
	return error;
7b0ce3
}
7b0ce3
7b0ce3
static int netlink_send(int fd, struct cn_msg *msg)
7b0ce3
{
7b0ce3
	struct nlmsghdr nlh = { .nlmsg_type = NLMSG_DONE };
7b0ce3
	unsigned int size;
7b0ce3
	struct msghdr message;
7b0ce3
	struct iovec iov[2];
7b0ce3
7b0ce3
	size = sizeof(struct cn_msg) + msg->len;
7b0ce3
7b0ce3
	nlh.nlmsg_pid = getpid();
7b0ce3
	nlh.nlmsg_len = NLMSG_LENGTH(size);
7b0ce3
7b0ce3
	iov[0].iov_base = &nlh;
7b0ce3
	iov[0].iov_len = sizeof(nlh);
7b0ce3
7b0ce3
	iov[1].iov_base = msg;
7b0ce3
	iov[1].iov_len = size;
7b0ce3
7b0ce3
	memset(&message, 0, sizeof(message));
7b0ce3
	message.msg_name = &addr;
7b0ce3
	message.msg_namelen = sizeof(addr);
7b0ce3
	message.msg_iov = iov;
7b0ce3
	message.msg_iovlen = 2;
7b0ce3
7b0ce3
	return sendmsg(fd, &message, 0);
7b0ce3
}
7b0ce3
7b0ce3
int main(void)
7b0ce3
{
7b0ce3
	int fd, len, nl_group;
7b0ce3
	int error;
7b0ce3
	struct cn_msg *message;
7b0ce3
	struct pollfd pfd;
7b0ce3
	struct nlmsghdr *incoming_msg;
7b0ce3
	struct cn_msg	*incoming_cn_msg;
7b0ce3
	int	op;
7b0ce3
	struct hv_vss_msg *vss_msg;
7b0ce3
	char *vss_recv_buffer;
7b0ce3
	size_t vss_recv_buffer_len;
7b0ce3
7b0ce3
	if (daemon(1, 0))
7b0ce3
		return 1;
7b0ce3
7b0ce3
	openlog("Hyper-V VSS", 0, LOG_USER);
7b0ce3
	syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
7b0ce3
41f5a8
	vss_recv_buffer_len = NLMSG_LENGTH(0) + sizeof(struct cn_msg) + sizeof(struct hv_vss_msg);
7b0ce3
	vss_recv_buffer = calloc(1, vss_recv_buffer_len);
41f5a8
	if (!vss_recv_buffer) {
7b0ce3
		syslog(LOG_ERR, "Failed to allocate netlink buffers");
7b0ce3
		exit(EXIT_FAILURE);
7b0ce3
	}
7b0ce3
7b0ce3
	fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
7b0ce3
	if (fd < 0) {
7b0ce3
		syslog(LOG_ERR, "netlink socket creation failed; error:%d %s",
7b0ce3
				errno, strerror(errno));
7b0ce3
		exit(EXIT_FAILURE);
7b0ce3
	}
7b0ce3
	addr.nl_family = AF_NETLINK;
7b0ce3
	addr.nl_pad = 0;
7b0ce3
	addr.nl_pid = 0;
7b0ce3
	addr.nl_groups = 0;
7b0ce3
7b0ce3
7b0ce3
	error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
7b0ce3
	if (error < 0) {
7b0ce3
		syslog(LOG_ERR, "bind failed; error:%d %s", errno, strerror(errno));
7b0ce3
		close(fd);
7b0ce3
		exit(EXIT_FAILURE);
7b0ce3
	}
7b0ce3
	nl_group = CN_VSS_IDX;
7b0ce3
	if (setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &nl_group, sizeof(nl_group)) < 0) {
7b0ce3
		syslog(LOG_ERR, "setsockopt failed; error:%d %s", errno, strerror(errno));
7b0ce3
		close(fd);
7b0ce3
		exit(EXIT_FAILURE);
7b0ce3
	}
7b0ce3
	/*
7b0ce3
	 * Register ourselves with the kernel.
7b0ce3
	 */
41f5a8
	message = (struct cn_msg *)vss_recv_buffer;
7b0ce3
	message->id.idx = CN_VSS_IDX;
7b0ce3
	message->id.val = CN_VSS_VAL;
7b0ce3
	message->ack = 0;
7b0ce3
	vss_msg = (struct hv_vss_msg *)message->data;
7b0ce3
	vss_msg->vss_hdr.operation = VSS_OP_REGISTER;
7b0ce3
7b0ce3
	message->len = sizeof(struct hv_vss_msg);
7b0ce3
7b0ce3
	len = netlink_send(fd, message);
7b0ce3
	if (len < 0) {
7b0ce3
		syslog(LOG_ERR, "netlink_send failed; error:%d %s", errno, strerror(errno));
7b0ce3
		close(fd);
7b0ce3
		exit(EXIT_FAILURE);
7b0ce3
	}
7b0ce3
7b0ce3
	pfd.fd = fd;
7b0ce3
7b0ce3
	while (1) {
7b0ce3
		struct sockaddr *addr_p = (struct sockaddr *) &addr;
7b0ce3
		socklen_t addr_l = sizeof(addr);
7b0ce3
		pfd.events = POLLIN;
7b0ce3
		pfd.revents = 0;
7b0ce3
7b0ce3
		if (poll(&pfd, 1, -1) < 0) {
7b0ce3
			syslog(LOG_ERR, "poll failed; error:%d %s", errno, strerror(errno));
7b0ce3
			if (errno == EINVAL) {
7b0ce3
				close(fd);
7b0ce3
				exit(EXIT_FAILURE);
7b0ce3
			}
7b0ce3
			else
7b0ce3
				continue;
7b0ce3
		}
7b0ce3
7b0ce3
		len = recvfrom(fd, vss_recv_buffer, vss_recv_buffer_len, 0,
7b0ce3
				addr_p, &addr_l);
7b0ce3
7b0ce3
		if (len < 0) {
7b0ce3
			syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
7b0ce3
					addr.nl_pid, errno, strerror(errno));
7b0ce3
			close(fd);
7b0ce3
			return -1;
7b0ce3
		}
7b0ce3
7b0ce3
		if (addr.nl_pid) {
7b0ce3
			syslog(LOG_WARNING,
7b0ce3
				"Received packet from untrusted pid:%u",
7b0ce3
				addr.nl_pid);
7b0ce3
			continue;
7b0ce3
		}
7b0ce3
7b0ce3
		incoming_msg = (struct nlmsghdr *)vss_recv_buffer;
7b0ce3
7b0ce3
		if (incoming_msg->nlmsg_type != NLMSG_DONE)
7b0ce3
			continue;
7b0ce3
7b0ce3
		incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
7b0ce3
		vss_msg = (struct hv_vss_msg *)incoming_cn_msg->data;
7b0ce3
		op = vss_msg->vss_hdr.operation;
7b0ce3
		error =  HV_S_OK;
7b0ce3
7b0ce3
		switch (op) {
7b0ce3
		case VSS_OP_FREEZE:
7b0ce3
		case VSS_OP_THAW:
7b0ce3
			error = vss_operate(op);
7b0ce3
			if (error)
7b0ce3
				error = HV_E_FAIL;
7b0ce3
			break;
7b0ce3
		default:
7b0ce3
			syslog(LOG_ERR, "Illegal op:%d\n", op);
7b0ce3
		}
7b0ce3
		vss_msg->error = error;
7b0ce3
		len = netlink_send(fd, incoming_cn_msg);
7b0ce3
		if (len < 0) {
7b0ce3
			syslog(LOG_ERR, "net_link send failed; error:%d %s",
7b0ce3
					errno, strerror(errno));
7b0ce3
			exit(EXIT_FAILURE);
7b0ce3
		}
7b0ce3
	}
7b0ce3
7b0ce3
}