Blame SOURCES/hv_fcopy_daemon.c

b19ffd
/*
b19ffd
 * An implementation of host to guest copy functionality for Linux.
b19ffd
 *
b19ffd
 * Copyright (C) 2014, Microsoft, Inc.
b19ffd
 *
b19ffd
 * Author : K. Y. Srinivasan <kys@microsoft.com>
b19ffd
 *
b19ffd
 * This program is free software; you can redistribute it and/or modify it
b19ffd
 * under the terms of the GNU General Public License version 2 as published
b19ffd
 * by the Free Software Foundation.
b19ffd
 *
b19ffd
 * This program is distributed in the hope that it will be useful, but
b19ffd
 * WITHOUT ANY WARRANTY; without even the implied warranty of
b19ffd
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
b19ffd
 * NON INFRINGEMENT.  See the GNU General Public License for more
b19ffd
 * details.
b19ffd
 */
b19ffd
b19ffd
b19ffd
#include <sys/types.h>
b19ffd
#include <stdio.h>
b19ffd
#include <stdlib.h>
b19ffd
#include <unistd.h>
b19ffd
#include <string.h>
b19ffd
#include <errno.h>
b19ffd
#include <linux/hyperv.h>
b19ffd
#include <linux/limits.h>
b19ffd
#include <syslog.h>
b19ffd
#include <sys/stat.h>
b19ffd
#include <fcntl.h>
b19ffd
#include <getopt.h>
b19ffd
b19ffd
static int target_fd;
b19ffd
static char target_fname[PATH_MAX];
b19ffd
static unsigned long long filesize;
b19ffd
b19ffd
static int hv_start_fcopy(struct hv_start_fcopy *smsg)
b19ffd
{
b19ffd
	int error = HV_E_FAIL;
b19ffd
	char *q, *p;
b19ffd
b19ffd
	filesize = 0;
b19ffd
	p = (char *)smsg->path_name;
b19ffd
	snprintf(target_fname, sizeof(target_fname), "%s/%s",
b19ffd
		 (char *)smsg->path_name, (char *)smsg->file_name);
b19ffd
b19ffd
	syslog(LOG_INFO, "Target file name: %s", target_fname);
b19ffd
	/*
b19ffd
	 * Check to see if the path is already in place; if not,
b19ffd
	 * create if required.
b19ffd
	 */
b19ffd
	while ((q = strchr(p, '/')) != NULL) {
b19ffd
		if (q == p) {
b19ffd
			p++;
b19ffd
			continue;
b19ffd
		}
b19ffd
		*q = '\0';
b19ffd
		if (access((char *)smsg->path_name, F_OK)) {
b19ffd
			if (smsg->copy_flags & CREATE_PATH) {
b19ffd
				if (mkdir((char *)smsg->path_name, 0755)) {
b19ffd
					syslog(LOG_ERR, "Failed to create %s",
b19ffd
						(char *)smsg->path_name);
b19ffd
					goto done;
b19ffd
				}
b19ffd
			} else {
b19ffd
				syslog(LOG_ERR, "Invalid path: %s",
b19ffd
					(char *)smsg->path_name);
b19ffd
				goto done;
b19ffd
			}
b19ffd
		}
b19ffd
		p = q + 1;
b19ffd
		*q = '/';
b19ffd
	}
b19ffd
b19ffd
	if (!access(target_fname, F_OK)) {
b19ffd
		syslog(LOG_INFO, "File: %s exists", target_fname);
b19ffd
		if (!(smsg->copy_flags & OVER_WRITE)) {
b19ffd
			error = HV_ERROR_ALREADY_EXISTS;
b19ffd
			goto done;
b19ffd
		}
b19ffd
	}
b19ffd
b19ffd
	target_fd = open(target_fname,
b19ffd
			 O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0744);
b19ffd
	if (target_fd == -1) {
b19ffd
		syslog(LOG_INFO, "Open Failed: %s", strerror(errno));
b19ffd
		goto done;
b19ffd
	}
b19ffd
b19ffd
	error = 0;
b19ffd
done:
b19ffd
	return error;
b19ffd
}
b19ffd
b19ffd
static int hv_copy_data(struct hv_do_fcopy *cpmsg)
b19ffd
{
b19ffd
	ssize_t bytes_written;
b19ffd
	int ret = 0;
b19ffd
b19ffd
	bytes_written = pwrite(target_fd, cpmsg->data, cpmsg->size,
b19ffd
				cpmsg->offset);
b19ffd
b19ffd
	filesize += cpmsg->size;
b19ffd
	if (bytes_written != cpmsg->size) {
b19ffd
		switch (errno) {
b19ffd
		case ENOSPC:
b19ffd
			ret = HV_ERROR_DISK_FULL;
b19ffd
			break;
b19ffd
		default:
b19ffd
			ret = HV_E_FAIL;
b19ffd
			break;
b19ffd
		}
b19ffd
		syslog(LOG_ERR, "pwrite failed to write %llu bytes: %ld (%s)",
b19ffd
		       filesize, (long)bytes_written, strerror(errno));
b19ffd
	}
b19ffd
b19ffd
	return ret;
b19ffd
}
b19ffd
b19ffd
static int hv_copy_finished(void)
b19ffd
{
b19ffd
	close(target_fd);
b19ffd
	return 0;
b19ffd
}
b19ffd
static int hv_copy_cancel(void)
b19ffd
{
b19ffd
	close(target_fd);
b19ffd
	unlink(target_fname);
b19ffd
	return 0;
b19ffd
b19ffd
}
b19ffd
b19ffd
void print_usage(char *argv[])
b19ffd
{
b19ffd
	fprintf(stderr, "Usage: %s [options]\n"
b19ffd
		"Options are:\n"
b19ffd
		"  -n, --no-daemon        stay in foreground, don't daemonize\n"
b19ffd
		"  -h, --help             print this help\n", argv[0]);
b19ffd
}
b19ffd
b19ffd
int main(int argc, char *argv[])
b19ffd
{
b19ffd
	int fcopy_fd;
b19ffd
	int error;
b19ffd
	int daemonize = 1, long_index = 0, opt;
b19ffd
	int version = FCOPY_CURRENT_VERSION;
b19ffd
	union {
b19ffd
		struct hv_fcopy_hdr hdr;
b19ffd
		struct hv_start_fcopy start;
b19ffd
		struct hv_do_fcopy copy;
b19ffd
		__u32 kernel_modver;
b19ffd
	} buffer = { };
b19ffd
	int in_handshake = 1;
b19ffd
b19ffd
	static struct option long_options[] = {
b19ffd
		{"help",	no_argument,	   0,  'h' },
b19ffd
		{"no-daemon",	no_argument,	   0,  'n' },
b19ffd
		{0,		0,		   0,  0   }
b19ffd
	};
b19ffd
b19ffd
	while ((opt = getopt_long(argc, argv, "hn", long_options,
b19ffd
				  &long_index)) != -1) {
b19ffd
		switch (opt) {
b19ffd
		case 'n':
b19ffd
			daemonize = 0;
b19ffd
			break;
b19ffd
		case 'h':
b19ffd
		default:
b19ffd
			print_usage(argv);
b19ffd
			exit(EXIT_FAILURE);
b19ffd
		}
b19ffd
	}
b19ffd
b19ffd
	if (daemonize && daemon(1, 0)) {
b19ffd
		syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno));
b19ffd
		exit(EXIT_FAILURE);
b19ffd
	}
b19ffd
b19ffd
	openlog("HV_FCOPY", 0, LOG_USER);
b19ffd
	syslog(LOG_INFO, "starting; pid is:%d", getpid());
b19ffd
b19ffd
	fcopy_fd = open("/dev/vmbus/hv_fcopy", O_RDWR);
b19ffd
b19ffd
	if (fcopy_fd < 0) {
b19ffd
		syslog(LOG_ERR, "open /dev/vmbus/hv_fcopy failed; error: %d %s",
b19ffd
			errno, strerror(errno));
b19ffd
		exit(EXIT_FAILURE);
b19ffd
	}
b19ffd
b19ffd
	/*
b19ffd
	 * Register with the kernel.
b19ffd
	 */
b19ffd
	if ((write(fcopy_fd, &version, sizeof(int))) != sizeof(int)) {
b19ffd
		syslog(LOG_ERR, "Registration failed: %s", strerror(errno));
b19ffd
		exit(EXIT_FAILURE);
b19ffd
	}
b19ffd
b19ffd
	while (1) {
b19ffd
		/*
b19ffd
		 * In this loop we process fcopy messages after the
b19ffd
		 * handshake is complete.
b19ffd
		 */
b19ffd
		ssize_t len;
b19ffd
b19ffd
		len = pread(fcopy_fd, &buffer, sizeof(buffer), 0);
b19ffd
		if (len < 0) {
b19ffd
			syslog(LOG_ERR, "pread failed: %s", strerror(errno));
b19ffd
			exit(EXIT_FAILURE);
b19ffd
		}
b19ffd
b19ffd
		if (in_handshake) {
b19ffd
			if (len != sizeof(buffer.kernel_modver)) {
b19ffd
				syslog(LOG_ERR, "invalid version negotiation");
b19ffd
				exit(EXIT_FAILURE);
b19ffd
			}
b19ffd
			in_handshake = 0;
b19ffd
			syslog(LOG_INFO, "kernel module version: %u",
b19ffd
			       buffer.kernel_modver);
b19ffd
			continue;
b19ffd
		}
b19ffd
b19ffd
		switch (buffer.hdr.operation) {
b19ffd
		case START_FILE_COPY:
b19ffd
			error = hv_start_fcopy(&buffer.start);
b19ffd
			break;
b19ffd
		case WRITE_TO_FILE:
b19ffd
			error = hv_copy_data(&buffer.copy);
b19ffd
			break;
b19ffd
		case COMPLETE_FCOPY:
b19ffd
			error = hv_copy_finished();
b19ffd
			break;
b19ffd
		case CANCEL_FCOPY:
b19ffd
			error = hv_copy_cancel();
b19ffd
			break;
b19ffd
b19ffd
		default:
b19ffd
			syslog(LOG_ERR, "Unknown operation: %d",
b19ffd
				buffer.hdr.operation);
b19ffd
b19ffd
		}
b19ffd
b19ffd
		if (pwrite(fcopy_fd, &error, sizeof(int), 0) != sizeof(int)) {
b19ffd
			syslog(LOG_ERR, "pwrite failed: %s", strerror(errno));
b19ffd
			exit(EXIT_FAILURE);
b19ffd
		}
b19ffd
	}
b19ffd
}