Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2997315AbdD3XW7 (ORCPT ); Sun, 30 Apr 2017 19:22:59 -0400 Received: from a2nlsmtp01-05.prod.iad2.secureserver.net ([198.71.225.49]:55122 "EHLO a2nlsmtp01-05.prod.iad2.secureserver.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S968558AbdD3XWW (ORCPT ); Sun, 30 Apr 2017 19:22:22 -0400 x-originating-ip: 107.180.71.197 From: kys@exchange.microsoft.com To: davem@davemloft.net, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, devel@linuxdriverproject.org, olaf@aepfle.de, apw@canonical.com, jasowang@redhat.com, leann.ogasawara@canonical.comi, marcelo.cerri@canonical.com, sthemmin@microsoft.com Cc: Vitaly Kuznetsov , "K. Y. Srinivasan" Subject: [PATCH 2/6] tools: hv: properly handle long paths Date: Sun, 30 Apr 2017 16:21:15 -0700 Message-Id: <1493594479-25329-2-git-send-email-kys@exchange.microsoft.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1493594420-25214-1-git-send-email-kys@exchange.microsoft.com> References: <1493594420-25214-1-git-send-email-kys@exchange.microsoft.com> Reply-To: kys@microsoft.com X-CMAE-Envelope: MS4wfC/+Kkw4obp4d50ijNY37Fc0yOF6BvbMPs0mxBoKPHtidI/WntaQcQmhC58X0FHuOwJQoRFvawoeNB2/TkaRzg6XREaaBvo5npYhRydtcV5ZiSgnlGd8 xpJsibkHwy2VN2E723sRgelgMwEp8BMOGntm7quqd0kP+89Oxh7dz/ikKnMPjHsYuEVAJHMbgdrDk+iNvXojnsZP6UnVaQy6+wqLmHBSOMDedrkGsQN2Ri8g JdTEErQ0QiZroEvXdkcAP0osH8pssK8/5HBPoT4vg+wsfrlvXOiOK9xQUD4wqaea5OHWW2QM1UUz2r9ensvvZSzroW8BxrGCd/ruazcCZPd2PVOxykynpbQW gacw8EzOHLwguAJ00Jv+ncOyE17ZHHdFub6Ye/T0D/dj/ml8M+HKCmj5+UUIZsFf1U/E2cLwI5ALqe5XBSeOI4H6qQXwDnYbiG77K/If6EYVZDiymMOoK5rQ UFKPahIIoFqaxD477pd4FHlLznegcpRs3gAnEm8g3cQbkrDZuHHP7i7c2NkldBY5dAq0qdeAirhmQITiW/e/yZwg8IKNK+HY24jyXg== Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3337 Lines: 130 From: Vitaly Kuznetsov Paths can be up to PATH_MAX long and PATH_MAX is usually greater than 256. While on it, simplify path reconstruction to a simple snprintf(), define and reuse KVP_NET_DIR. Suggested-by: Tomas Hozza Signed-off-by: Vitaly Kuznetsov Signed-off-by: K. Y. Srinivasan --- tools/hv/hv_kvp_daemon.c | 44 ++++++++++++++++++-------------------------- 1 files changed, 18 insertions(+), 26 deletions(-) diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c index f1758fc..88b20e0 100644 --- a/tools/hv/hv_kvp_daemon.c +++ b/tools/hv/hv_kvp_daemon.c @@ -39,6 +39,7 @@ #include #include #include +#include #include /* @@ -97,6 +98,8 @@ enum { #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 @@ -596,26 +599,21 @@ void kvp_get_os_info(void) 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) @@ -653,12 +651,12 @@ void kvp_get_os_info(void) 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) @@ -688,28 +686,22 @@ void kvp_get_os_info(void) 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) @@ -1218,9 +1210,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; /* -- 1.7.1