Accepting request 562485 from Virtualization

- update buffer handling in hv_fcopy_daemon
- remove unnecessary header files and netlink related code
- Avoid reading past allocated blocks from KVP file
- fix snprintf warning in kvp_daemon
- properly handle long paths
- kvp: configurable external scripts path
- vss: Thaw the filesystem and continue if freeze call has timed out
- vss: Skip freezing filesystems backed by loop

OBS-URL: https://build.opensuse.org/request/show/562485
OBS-URL: https://build.opensuse.org/package/show/openSUSE:Factory/hyper-v?expand=0&rev=35
This commit is contained in:
Dominique Leuenberger 2018-01-16 08:30:54 +00:00 committed by Git OBS Bridge
commit c7429b6633
5 changed files with 89 additions and 126 deletions

View File

@ -1,3 +1,15 @@
-------------------------------------------------------------------
Mon Jan 8 10:45:35 UTC 2018 - ohering@suse.de
- update buffer handling in hv_fcopy_daemon
- remove unnecessary header files and netlink related code
- Avoid reading past allocated blocks from KVP file
- fix snprintf warning in kvp_daemon
- properly handle long paths
- kvp: configurable external scripts path
- vss: Thaw the filesystem and continue if freeze call has timed out
- vss: Skip freezing filesystems backed by loop
-------------------------------------------------------------------
Wed Jun 28 12:04:38 UTC 2017 - ohering@suse.de

View File

@ -1,7 +1,7 @@
#
# spec file for package hyper-v
#
# Copyright (c) 2017 SUSE LINUX GmbH, Nuernberg, Germany.
# Copyright (c) 2018 SUSE LINUX GmbH, Nuernberg, Germany.
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
@ -87,6 +87,7 @@ gcc \
-include %{hv_kvp_daemon}.h \
-DCN_KVP_IDX=0x9 \
-DCN_KVP_VAL=0x1 \
-DKVP_SCRIPTS_PATH= \
-o %{hv_kvp_daemon}
gcc \
$RPM_OPT_FLAGS \
@ -130,7 +131,7 @@ chmod 755 $RPM_BUILD_ROOT%{helper_dir}/bin/*
d=$RPM_BUILD_ROOT%{_unitdir}
mkdir -vp ${d}
#
cat > ${d}/%{hv_kvp_daemon}.service <<EOF
tee ${d}/%{hv_kvp_daemon}.service <<EOF
# started via %{_udevrulesdir}/%{hv_kvp_daemon}.rules
[Unit]
Description=Hyper-V KVP Daemon
@ -152,7 +153,7 @@ Restart=no
WantedBy=default.target
EOF
#
cat > ${d}/%{hv_vss_daemon}.service <<EOF
tee ${d}/%{hv_vss_daemon}.service <<EOF
# started via %{_udevrulesdir}/%{hv_vss_daemon}.rules
[Unit]
Description=Hyper-V VSS Daemon
@ -170,7 +171,7 @@ Restart=no
WantedBy=default.target
EOF
#
cat > ${d}/%{hv_fcopy_daemon}.service <<EOF
tee ${d}/%{hv_fcopy_daemon}.service <<EOF
# started via %{_udevrulesdir}/%{hv_fcopy_daemon}.rules
[Unit]
Description=Hyper-V host to guest file copy daemon
@ -183,25 +184,23 @@ ExecStart=${bindir}/%{hv_fcopy_daemon} --no-daemon
WantedBy=default.target
EOF
#
head -n 42 ${d}/*.service
#
#
d=$RPM_BUILD_ROOT%{_udevrulesdir}
mkdir -vp ${d}
cat > ${d}/%{hv_kvp_daemon}.rules <<EOF
tee ${d}/%{hv_kvp_daemon}.rules <<EOF
ACTION=="add", KERNEL=="vmbus/hv_kvp", TAG+="systemd", ENV{SYSTEMD_WANTS}+="%{hv_kvp_daemon}.service"
EOF
cat > ${d}/%{hv_vss_daemon}.rules <<EOF
tee ${d}/%{hv_vss_daemon}.rules <<EOF
ACTION=="add", KERNEL=="vmbus/hv_vss", TAG+="systemd", ENV{SYSTEMD_WANTS}+="%{hv_vss_daemon}.service"
EOF
cat > ${d}/%{hv_fcopy_daemon}.rules <<EOF
tee ${d}/%{hv_fcopy_daemon}.rules <<EOF
ACTION=="add", KERNEL=="vmbus/hv_fcopy", TAG+="systemd", ENV{SYSTEMD_WANTS}+="%{hv_fcopy_daemon}.service"
EOF
#
head -n 42 ${d}/*.rules
#
helper=inst_sys.sh
cat > $RPM_BUILD_ROOT${bindir}/${helper} <<EOF
tee $RPM_BUILD_ROOT${bindir}/${helper} <<EOF
#!/bin/bash
# Starting daemons via RUN== in udev rules is not supported.
# In inst-sys systemd is not used, so start all daemons manually.
@ -224,7 +223,6 @@ then
fi
EOF
chmod 755 $RPM_BUILD_ROOT${bindir}/${helper}
head -n 123 $RPM_BUILD_ROOT${bindir}/${helper}
#
%else
mkdir -p $RPM_BUILD_ROOT/etc/init.d

View File

@ -18,21 +18,15 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/poll.h>
#include <linux/types.h>
#include <linux/kdev_t.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <linux/hyperv.h>
#include <syslog.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <getopt.h>
static int target_fd;
@ -145,14 +139,17 @@ void print_usage(char *argv[])
int main(int argc, char *argv[])
{
int fcopy_fd, len;
int fcopy_fd;
int error;
int daemonize = 1, long_index = 0, opt;
int version = FCOPY_CURRENT_VERSION;
char *buffer[4096 * 2];
struct hv_fcopy_hdr *in_msg;
union {
struct hv_fcopy_hdr hdr;
struct hv_start_fcopy start;
struct hv_do_fcopy copy;
__u32 kernel_modver;
} buffer = { };
int in_handshake = 1;
__u32 kernel_modver;
static struct option long_options[] = {
{"help", no_argument, 0, 'h' },
@ -202,32 +199,31 @@ int main(int argc, char *argv[])
* In this loop we process fcopy messages after the
* handshake is complete.
*/
len = pread(fcopy_fd, buffer, (4096 * 2), 0);
ssize_t len;
len = pread(fcopy_fd, &buffer, sizeof(buffer), 0);
if (len < 0) {
syslog(LOG_ERR, "pread failed: %s", strerror(errno));
exit(EXIT_FAILURE);
}
if (in_handshake) {
if (len != sizeof(kernel_modver)) {
if (len != sizeof(buffer.kernel_modver)) {
syslog(LOG_ERR, "invalid version negotiation");
exit(EXIT_FAILURE);
}
kernel_modver = *(__u32 *)buffer;
in_handshake = 0;
syslog(LOG_INFO, "kernel module version: %d",
kernel_modver);
syslog(LOG_INFO, "kernel module version: %u",
buffer.kernel_modver);
continue;
}
in_msg = (struct hv_fcopy_hdr *)buffer;
switch (in_msg->operation) {
switch (buffer.hdr.operation) {
case START_FILE_COPY:
error = hv_start_fcopy((struct hv_start_fcopy *)in_msg);
error = hv_start_fcopy(&buffer.start);
break;
case WRITE_TO_FILE:
error = hv_copy_data((struct hv_do_fcopy *)in_msg);
error = hv_copy_data(&buffer.copy);
break;
case COMPLETE_FCOPY:
error = hv_copy_finished();
@ -238,7 +234,7 @@ int main(int argc, char *argv[])
default:
syslog(LOG_ERR, "Unknown operation: %d",
in_msg->operation);
buffer.hdr.operation);
}

View File

@ -22,8 +22,6 @@
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/poll.h>
#include <sys/utsname.h>
#include <stdio.h>
@ -34,7 +32,6 @@
#include <errno.h>
#include <arpa/inet.h>
#include <linux/hyperv.h>
#include <linux/netlink.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <syslog.h>
@ -42,6 +39,7 @@
#include <fcntl.h>
#include <dirent.h>
#include <net/if.h>
#include <limits.h>
#include <getopt.h>
/*
@ -96,13 +94,15 @@ static struct utsname uts_buf;
#define KVP_CONFIG_LOC "/var/lib/hyperv"
#ifndef KVP_SCRIPTS_PATH
#define KVP_SCRIPTS_PATH "/usr/libexec/hypervkvpd/"
#endif
#define KVP_NET_DIR "/sys/class/net/"
#define MAX_FILE_NAME 100
#define ENTRIES_PER_BLOCK 50
#ifndef SOL_NETLINK
#define SOL_NETLINK 270
#endif
struct kvp_record {
char key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
char value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
@ -193,11 +193,14 @@ static void kvp_update_mem_state(int pool)
for (;;) {
readp = &record[records_read];
records_read += fread(readp, sizeof(struct kvp_record),
ENTRIES_PER_BLOCK * num_blocks,
filep);
ENTRIES_PER_BLOCK * num_blocks - records_read,
filep);
if (ferror(filep)) {
syslog(LOG_ERR, "Failed to read file, pool: %d", pool);
syslog(LOG_ERR,
"Failed to read file, pool: %d; error: %d %s",
pool, errno, strerror(errno));
kvp_release_lock(pool);
exit(EXIT_FAILURE);
}
@ -210,6 +213,7 @@ static void kvp_update_mem_state(int pool)
if (record == NULL) {
syslog(LOG_ERR, "malloc failed");
kvp_release_lock(pool);
exit(EXIT_FAILURE);
}
continue;
@ -224,15 +228,11 @@ static void kvp_update_mem_state(int pool)
fclose(filep);
kvp_release_lock(pool);
}
static int kvp_file_init(void)
{
int fd;
FILE *filep;
size_t records_read;
char *fname;
struct kvp_record *record;
struct kvp_record *readp;
int num_blocks;
int i;
int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
@ -246,61 +246,19 @@ static int kvp_file_init(void)
for (i = 0; i < KVP_POOL_COUNT; i++) {
fname = kvp_file_info[i].fname;
records_read = 0;
num_blocks = 1;
sprintf(fname, "%s/.kvp_pool_%d", KVP_CONFIG_LOC, i);
fd = open(fname, O_RDWR | O_CREAT | O_CLOEXEC, 0644 /* rw-r--r-- */);
if (fd == -1)
return 1;
filep = fopen(fname, "re");
if (!filep) {
close(fd);
return 1;
}
record = malloc(alloc_unit * num_blocks);
if (record == NULL) {
fclose(filep);
close(fd);
return 1;
}
for (;;) {
readp = &record[records_read];
records_read += fread(readp, sizeof(struct kvp_record),
ENTRIES_PER_BLOCK,
filep);
if (ferror(filep)) {
syslog(LOG_ERR, "Failed to read file, pool: %d",
i);
exit(EXIT_FAILURE);
}
if (!feof(filep)) {
/*
* We have more data to read.
*/
num_blocks++;
record = realloc(record, alloc_unit *
num_blocks);
if (record == NULL) {
fclose(filep);
close(fd);
return 1;
}
continue;
}
break;
}
kvp_file_info[i].fd = fd;
kvp_file_info[i].num_blocks = num_blocks;
kvp_file_info[i].records = record;
kvp_file_info[i].num_records = records_read;
fclose(filep);
kvp_file_info[i].num_blocks = 1;
kvp_file_info[i].records = malloc(alloc_unit);
if (kvp_file_info[i].records == NULL)
return 1;
kvp_file_info[i].num_records = 0;
kvp_update_mem_state(i);
}
return 0;
@ -599,26 +557,21 @@ static char *kvp_get_if_name(char *guid)
DIR *dir;
struct dirent *entry;
FILE *file;
char *p, *q, *x;
char *p, *x;
char *if_name = NULL;
char buf[256];
char *kvp_net_dir = "/sys/class/net/";
char dev_id[256];
char dev_id[PATH_MAX];
dir = opendir(kvp_net_dir);
dir = opendir(KVP_NET_DIR);
if (dir == NULL)
return NULL;
snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir);
q = dev_id + strlen(kvp_net_dir);
while ((entry = readdir(dir)) != NULL) {
/*
* Set the state for the next pass.
*/
*q = '\0';
strcat(dev_id, entry->d_name);
strcat(dev_id, "/device/device_id");
snprintf(dev_id, sizeof(dev_id), "%s%s/device/device_id",
KVP_NET_DIR, entry->d_name);
file = fopen(dev_id, "r");
if (file == NULL)
@ -656,12 +609,12 @@ static char *kvp_if_name_to_mac(char *if_name)
FILE *file;
char *p, *x;
char buf[256];
char addr_file[256];
char addr_file[PATH_MAX];
unsigned int i;
char *mac_addr = NULL;
snprintf(addr_file, sizeof(addr_file), "%s%s%s", "/sys/class/net/",
if_name, "/address");
snprintf(addr_file, sizeof(addr_file), "%s%s%s", KVP_NET_DIR,
if_name, "/address");
file = fopen(addr_file, "r");
if (file == NULL)
@ -691,28 +644,22 @@ static char *kvp_mac_to_if_name(char *mac)
DIR *dir;
struct dirent *entry;
FILE *file;
char *p, *q, *x;
char *p, *x;
char *if_name = NULL;
char buf[256];
char *kvp_net_dir = "/sys/class/net/";
char dev_id[256];
char dev_id[PATH_MAX];
unsigned int i;
dir = opendir(kvp_net_dir);
dir = opendir(KVP_NET_DIR);
if (dir == NULL)
return NULL;
snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir);
q = dev_id + strlen(kvp_net_dir);
while ((entry = readdir(dir)) != NULL) {
/*
* Set the state for the next pass.
*/
*q = '\0';
strcat(dev_id, entry->d_name);
strcat(dev_id, "/address");
snprintf(dev_id, sizeof(dev_id), "%s%s/address", KVP_NET_DIR,
entry->d_name);
file = fopen(dev_id, "r");
if (file == NULL)
@ -825,7 +772,7 @@ static void kvp_get_ipconfig_info(char *if_name,
* .
*/
sprintf(cmd, "%s", "hv_get_dns_info");
sprintf(cmd, KVP_SCRIPTS_PATH "%s", "hv_get_dns_info");
/*
* Execute the command to gather DNS info.
@ -842,7 +789,7 @@ static void kvp_get_ipconfig_info(char *if_name,
* Enabled: DHCP enabled.
*/
sprintf(cmd, "%s %s", "hv_get_dhcp_info", if_name);
sprintf(cmd, KVP_SCRIPTS_PATH "%s %s", "hv_get_dhcp_info", if_name);
file = popen(cmd, "r");
if (file == NULL)
@ -1147,7 +1094,7 @@ static int process_ip_string(FILE *f, char *ip_string, int type)
int i = 0;
int j = 0;
char str[256];
char sub_str[10];
char sub_str[13];
int offset = 0;
memset(addr, 0, sizeof(addr));
@ -1221,9 +1168,9 @@ static int process_ip_string(FILE *f, char *ip_string, int type)
static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
{
int error = 0;
char if_file[128];
char if_file[PATH_MAX];
FILE *file;
char cmd[512];
char cmd[PATH_MAX];
char *mac_addr;
/*
@ -1348,7 +1295,8 @@ static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
* invoke the external script to do its magic.
*/
snprintf(cmd, sizeof(cmd), "%s %s", "hv_set_ifconfig", if_file);
snprintf(cmd, sizeof(cmd), KVP_SCRIPTS_PATH "%s %s",
"hv_set_ifconfig", if_file);
if (system(cmd)) {
syslog(LOG_ERR, "Failed to execute cmd '%s'; error: %d %s",
cmd, errno, strerror(errno));

View File

@ -21,6 +21,7 @@
#include <sys/types.h>
#include <sys/poll.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <mntent.h>
@ -30,6 +31,7 @@
#include <ctype.h>
#include <errno.h>
#include <linux/fs.h>
#include <linux/major.h>
#include <linux/hyperv.h>
#include <syslog.h>
#include <getopt.h>
@ -70,6 +72,7 @@ static int vss_operate(int operation)
char match[] = "/dev/";
FILE *mounts;
struct mntent *ent;
struct stat sb;
char errdir[1024] = {0};
unsigned int cmd;
int error = 0, root_seen = 0, save_errno = 0;
@ -92,6 +95,10 @@ static int vss_operate(int operation)
while ((ent = getmntent(mounts))) {
if (strncmp(ent->mnt_fsname, match, strlen(match)))
continue;
if (stat(ent->mnt_fsname, &sb) == -1)
continue;
if (S_ISBLK(sb.st_mode) && major(sb.st_rdev) == LOOP_MAJOR)
continue;
if (hasmntopt(ent, MNTOPT_RO) != NULL)
continue;
if (strcmp(ent->mnt_type, "vfat") == 0)
@ -261,7 +268,9 @@ int main(int argc, char *argv[])
if (len != sizeof(struct hv_vss_msg)) {
syslog(LOG_ERR, "write failed; error: %d %s", errno,
strerror(errno));
exit(EXIT_FAILURE);
if (op == VSS_OP_FREEZE)
vss_operate(VSS_OP_THAW);
}
}