Blame SOURCES/hv_fcopy_daemon.c

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