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