From 18c1b296af3e137aef668a73a5977779baaa31e2f74a0ba9d52ad62dd11030e2 Mon Sep 17 00:00:00 2001 From: Olaf Hering Date: Wed, 7 Aug 2013 13:55:15 +0000 Subject: [PATCH 1/3] - use full nlmsghdr in netlink_send - correct payload size in netlink_send - use single send+recv buffer - log errors to syslog in kvp_set_ip_info - check return value of system in hv_kvp_daemon - in kvp_set_ip_info free mac_addr right after usage - check return value of daemon to fix compiler warning. OBS-URL: https://build.opensuse.org/package/show/Virtualization/hyper-v?expand=0&rev=66 --- hyper-v.changes | 11 +++++++++ hyper-v.tools.hv.hv_kvp_daemon.c | 42 +++++++++++++++----------------- hyper-v.tools.hv.hv_vss_daemon.c | 25 +++++++------------ 3 files changed, 40 insertions(+), 38 deletions(-) diff --git a/hyper-v.changes b/hyper-v.changes index 8f61615..5ddea92 100644 --- a/hyper-v.changes +++ b/hyper-v.changes @@ -1,3 +1,14 @@ +------------------------------------------------------------------- +Wed Aug 7 15:54:19 CEST 2013 - ohering@suse.de + +- use full nlmsghdr in netlink_send +- correct payload size in netlink_send +- use single send+recv buffer +- log errors to syslog in kvp_set_ip_info +- check return value of system in hv_kvp_daemon +- in kvp_set_ip_info free mac_addr right after usage +- check return value of daemon to fix compiler warning. + ------------------------------------------------------------------- Thu Aug 1 14:21:57 CEST 2013 - ohering@suse.de diff --git a/hyper-v.tools.hv.hv_kvp_daemon.c b/hyper-v.tools.hv.hv_kvp_daemon.c index 252e5d6..7a95a86 100644 --- a/hyper-v.tools.hv.hv_kvp_daemon.c +++ b/hyper-v.tools.hv.hv_kvp_daemon.c @@ -1299,6 +1299,7 @@ static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val) } error = kvp_write_file(file, "HWADDR", "", mac_addr); + free(mac_addr); if (error) goto setval_error; @@ -1344,7 +1345,6 @@ static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val) goto setval_error; setval_done: - free(mac_addr); fclose(file); /* @@ -1353,12 +1353,16 @@ setval_done: */ snprintf(cmd, sizeof(cmd), "%s %s", "hv_set_ifconfig", if_file); - system(cmd); + if (system(cmd)) { + syslog(LOG_ERR, "Failed to execute cmd '%s'; error: %d %s", + cmd, errno, strerror(errno)); + return HV_E_FAIL; + } return 0; setval_error: - syslog(LOG_ERR, "Failed to write config file"); - free(mac_addr); + syslog(LOG_ERR, "Failed to write config file. error: %d %s", + errno, strerror(errno)); fclose(file); return error; } @@ -1389,23 +1393,18 @@ kvp_get_domain_name(char *buffer, int length) static int netlink_send(int fd, struct cn_msg *msg) { - struct nlmsghdr *nlh; + struct nlmsghdr nlh = { .nlmsg_type = NLMSG_DONE }; unsigned int size; struct msghdr message; - char buffer[64]; struct iovec iov[2]; - size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len); + size = sizeof(struct cn_msg) + msg->len; - nlh = (struct nlmsghdr *)buffer; - nlh->nlmsg_seq = 0; - nlh->nlmsg_pid = getpid(); - nlh->nlmsg_type = NLMSG_DONE; - nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh)); - nlh->nlmsg_flags = 0; + nlh.nlmsg_pid = getpid(); + nlh.nlmsg_len = NLMSG_LENGTH(size); - iov[0].iov_base = nlh; - iov[0].iov_len = sizeof(*nlh); + iov[0].iov_base = &nlh; + iov[0].iov_len = sizeof(nlh); iov[1].iov_base = msg; iov[1].iov_len = size; @@ -1435,19 +1434,18 @@ int main(void) int pool; char *if_name; struct hv_kvp_ipaddr_value *kvp_ip_val; - char *kvp_send_buffer; char *kvp_recv_buffer; size_t kvp_recv_buffer_len; - daemon(1, 0); + if (daemon(1, 0)) + return 1; openlog("KVP", 0, LOG_USER); syslog(LOG_INFO, "KVP starting; pid is:%d", getpid()); - kvp_recv_buffer_len = NLMSG_HDRLEN + sizeof(struct cn_msg) + sizeof(struct hv_kvp_msg); - kvp_send_buffer = calloc(1, kvp_recv_buffer_len); + kvp_recv_buffer_len = NLMSG_LENGTH(0) + sizeof(struct cn_msg) + sizeof(struct hv_kvp_msg); kvp_recv_buffer = calloc(1, kvp_recv_buffer_len); - if (!(kvp_send_buffer && kvp_recv_buffer)) { - syslog(LOG_ERR, "Failed to allocate netlink buffers"); + if (!kvp_recv_buffer) { + syslog(LOG_ERR, "Failed to allocate netlink buffer"); exit(EXIT_FAILURE); } /* @@ -1489,7 +1487,7 @@ int main(void) /* * Register ourselves with the kernel. */ - message = (struct cn_msg *)kvp_send_buffer; + message = (struct cn_msg *)kvp_recv_buffer; message->id.idx = CN_KVP_IDX; message->id.val = CN_KVP_VAL; diff --git a/hyper-v.tools.hv.hv_vss_daemon.c b/hyper-v.tools.hv.hv_vss_daemon.c index 797b4c1..630f182 100644 --- a/hyper-v.tools.hv.hv_vss_daemon.c +++ b/hyper-v.tools.hv.hv_vss_daemon.c @@ -105,23 +105,18 @@ static int vss_operate(int operation) static int netlink_send(int fd, struct cn_msg *msg) { - struct nlmsghdr *nlh; + struct nlmsghdr nlh = { .nlmsg_type = NLMSG_DONE }; unsigned int size; struct msghdr message; - char buffer[64]; struct iovec iov[2]; - size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len); + size = sizeof(struct cn_msg) + msg->len; - nlh = (struct nlmsghdr *)buffer; - nlh->nlmsg_seq = 0; - nlh->nlmsg_pid = getpid(); - nlh->nlmsg_type = NLMSG_DONE; - nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh)); - nlh->nlmsg_flags = 0; + nlh.nlmsg_pid = getpid(); + nlh.nlmsg_len = NLMSG_LENGTH(size); - iov[0].iov_base = nlh; - iov[0].iov_len = sizeof(*nlh); + iov[0].iov_base = &nlh; + iov[0].iov_len = sizeof(nlh); iov[1].iov_base = msg; iov[1].iov_len = size; @@ -145,7 +140,6 @@ int main(void) struct cn_msg *incoming_cn_msg; int op; struct hv_vss_msg *vss_msg; - char *vss_send_buffer; char *vss_recv_buffer; size_t vss_recv_buffer_len; @@ -155,10 +149,9 @@ int main(void) openlog("Hyper-V VSS", 0, LOG_USER); syslog(LOG_INFO, "VSS starting; pid is:%d", getpid()); - vss_recv_buffer_len = NLMSG_HDRLEN + sizeof(struct cn_msg) + sizeof(struct hv_vss_msg); - vss_send_buffer = calloc(1, vss_recv_buffer_len); + vss_recv_buffer_len = NLMSG_LENGTH(0) + sizeof(struct cn_msg) + sizeof(struct hv_vss_msg); vss_recv_buffer = calloc(1, vss_recv_buffer_len); - if (!(vss_send_buffer && vss_recv_buffer)) { + if (!vss_recv_buffer) { syslog(LOG_ERR, "Failed to allocate netlink buffers"); exit(EXIT_FAILURE); } @@ -185,7 +178,7 @@ int main(void) /* * Register ourselves with the kernel. */ - message = (struct cn_msg *)vss_send_buffer; + message = (struct cn_msg *)vss_recv_buffer; message->id.idx = CN_VSS_IDX; message->id.val = CN_VSS_VAL; message->ack = 0; From f4e9f3f4250a74bc1afc2ad7a8896a08575a922a3c02d5a875fccc4767ded8bb Mon Sep 17 00:00:00 2001 From: Olaf Hering Date: Wed, 7 Aug 2013 17:12:54 +0000 Subject: [PATCH 2/3] - cache FQDN in kvp_daemon to avoid timeouts OBS-URL: https://build.opensuse.org/package/show/Virtualization/hyper-v?expand=0&rev=67 --- hyper-v.changes | 3 ++- hyper-v.tools.hv.hv_kvp_daemon.c | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/hyper-v.changes b/hyper-v.changes index 5ddea92..98e656d 100644 --- a/hyper-v.changes +++ b/hyper-v.changes @@ -1,6 +1,7 @@ ------------------------------------------------------------------- -Wed Aug 7 15:54:19 CEST 2013 - ohering@suse.de +Wed Aug 7 19:04:35 CEST 2013 - ohering@suse.de +- cache FQDN in kvp_daemon to avoid timeouts - use full nlmsghdr in netlink_send - correct payload size in netlink_send - use single send+recv buffer diff --git a/hyper-v.tools.hv.hv_kvp_daemon.c b/hyper-v.tools.hv.hv_kvp_daemon.c index 7a95a86..2d5e806 100644 --- a/hyper-v.tools.hv.hv_kvp_daemon.c +++ b/hyper-v.tools.hv.hv_kvp_daemon.c @@ -89,6 +89,7 @@ static char *processor_arch; static char *os_build; static char *os_version; static char *lic_version = "Unknown version"; +static char full_domain_name[HV_KVP_EXCHANGE_MAX_VALUE_SIZE]; static struct utsname uts_buf; /* @@ -1368,7 +1369,7 @@ setval_error: } -static int +static void kvp_get_domain_name(char *buffer, int length) { struct addrinfo hints, *info ; @@ -1382,12 +1383,12 @@ kvp_get_domain_name(char *buffer, int length) error = getaddrinfo(buffer, NULL, &hints, &info); if (error != 0) { - strcpy(buffer, "getaddrinfo failed\n"); - return error; + snprintf(buffer, length, "getaddrinfo failed: 0x%x %s", + error, gai_strerror(error)); + return; } - strcpy(buffer, info->ai_canonname); + snprintf(buffer, length, "%s", info->ai_canonname); freeaddrinfo(info); - return error; } static int @@ -1452,6 +1453,11 @@ int main(void) * Retrieve OS release information. */ kvp_get_os_info(); + /* + * Cache Fully Qualified Domain Name because getaddrinfo takes an + * unpredicatable amount of time to finish. + */ + kvp_get_domain_name(full_domain_name, sizeof(full_domain_name)); if (kvp_file_init()) { syslog(LOG_ERR, "Failed to initialize the pools"); @@ -1670,8 +1676,7 @@ int main(void) switch (hv_msg->body.kvp_enum_data.index) { case FullyQualifiedDomainName: - kvp_get_domain_name(key_value, - HV_KVP_EXCHANGE_MAX_VALUE_SIZE); + strcpy(key_value, full_domain_name); strcpy(key_name, "FullyQualifiedDomainName"); break; case IntegrationServicesVersion: From efc1b8047e6b73316b0b941d0b5bef6faf228f862b45564a65cefe923d5e5086 Mon Sep 17 00:00:00 2001 From: Olaf Hering Date: Wed, 7 Aug 2013 17:37:54 +0000 Subject: [PATCH 3/3] - cache FQDN in kvp_daemon to avoid timeouts (bnc#828714) OBS-URL: https://build.opensuse.org/package/show/Virtualization/hyper-v?expand=0&rev=68 --- hyper-v.changes | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hyper-v.changes b/hyper-v.changes index 98e656d..5873e23 100644 --- a/hyper-v.changes +++ b/hyper-v.changes @@ -1,7 +1,7 @@ ------------------------------------------------------------------- Wed Aug 7 19:04:35 CEST 2013 - ohering@suse.de -- cache FQDN in kvp_daemon to avoid timeouts +- cache FQDN in kvp_daemon to avoid timeouts (bnc#828714) - use full nlmsghdr in netlink_send - correct payload size in netlink_send - use single send+recv buffer