Commit a1a7ea6b authored by Vitaly Kuznetsov's avatar Vitaly Kuznetsov Committed by Greg Kroah-Hartman

tools: hv: properly handle long paths

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: default avatarTomas Hozza <thozza@redhat.com>
Signed-off-by: default avatarVitaly Kuznetsov <vkuznets@redhat.com>
Signed-off-by: default avatarK. Y. Srinivasan <kys@microsoft.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 6113e3d2
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <dirent.h> #include <dirent.h>
#include <net/if.h> #include <net/if.h>
#include <limits.h>
#include <getopt.h> #include <getopt.h>
/* /*
...@@ -97,6 +98,8 @@ static struct utsname uts_buf; ...@@ -97,6 +98,8 @@ static struct utsname uts_buf;
#define KVP_SCRIPTS_PATH "/usr/libexec/hypervkvpd/" #define KVP_SCRIPTS_PATH "/usr/libexec/hypervkvpd/"
#endif #endif
#define KVP_NET_DIR "/sys/class/net/"
#define MAX_FILE_NAME 100 #define MAX_FILE_NAME 100
#define ENTRIES_PER_BLOCK 50 #define ENTRIES_PER_BLOCK 50
...@@ -596,26 +599,21 @@ static char *kvp_get_if_name(char *guid) ...@@ -596,26 +599,21 @@ static char *kvp_get_if_name(char *guid)
DIR *dir; DIR *dir;
struct dirent *entry; struct dirent *entry;
FILE *file; FILE *file;
char *p, *q, *x; char *p, *x;
char *if_name = NULL; char *if_name = NULL;
char buf[256]; char buf[256];
char *kvp_net_dir = "/sys/class/net/"; char dev_id[PATH_MAX];
char dev_id[256];
dir = opendir(kvp_net_dir); dir = opendir(KVP_NET_DIR);
if (dir == NULL) if (dir == NULL)
return 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) { while ((entry = readdir(dir)) != NULL) {
/* /*
* Set the state for the next pass. * Set the state for the next pass.
*/ */
*q = '\0'; snprintf(dev_id, sizeof(dev_id), "%s%s/device/device_id",
strcat(dev_id, entry->d_name); KVP_NET_DIR, entry->d_name);
strcat(dev_id, "/device/device_id");
file = fopen(dev_id, "r"); file = fopen(dev_id, "r");
if (file == NULL) if (file == NULL)
...@@ -653,11 +651,11 @@ static char *kvp_if_name_to_mac(char *if_name) ...@@ -653,11 +651,11 @@ static char *kvp_if_name_to_mac(char *if_name)
FILE *file; FILE *file;
char *p, *x; char *p, *x;
char buf[256]; char buf[256];
char addr_file[256]; char addr_file[PATH_MAX];
unsigned int i; unsigned int i;
char *mac_addr = NULL; char *mac_addr = NULL;
snprintf(addr_file, sizeof(addr_file), "%s%s%s", "/sys/class/net/", snprintf(addr_file, sizeof(addr_file), "%s%s%s", KVP_NET_DIR,
if_name, "/address"); if_name, "/address");
file = fopen(addr_file, "r"); file = fopen(addr_file, "r");
...@@ -688,28 +686,22 @@ static char *kvp_mac_to_if_name(char *mac) ...@@ -688,28 +686,22 @@ static char *kvp_mac_to_if_name(char *mac)
DIR *dir; DIR *dir;
struct dirent *entry; struct dirent *entry;
FILE *file; FILE *file;
char *p, *q, *x; char *p, *x;
char *if_name = NULL; char *if_name = NULL;
char buf[256]; char buf[256];
char *kvp_net_dir = "/sys/class/net/"; char dev_id[PATH_MAX];
char dev_id[256];
unsigned int i; unsigned int i;
dir = opendir(kvp_net_dir); dir = opendir(KVP_NET_DIR);
if (dir == NULL) if (dir == NULL)
return 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) { while ((entry = readdir(dir)) != NULL) {
/* /*
* Set the state for the next pass. * Set the state for the next pass.
*/ */
*q = '\0'; snprintf(dev_id, sizeof(dev_id), "%s%s/address", KVP_NET_DIR,
entry->d_name);
strcat(dev_id, entry->d_name);
strcat(dev_id, "/address");
file = fopen(dev_id, "r"); file = fopen(dev_id, "r");
if (file == NULL) if (file == NULL)
...@@ -1218,9 +1210,9 @@ static int process_ip_string(FILE *f, char *ip_string, int type) ...@@ -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) static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
{ {
int error = 0; int error = 0;
char if_file[128]; char if_file[PATH_MAX];
FILE *file; FILE *file;
char cmd[512]; char cmd[PATH_MAX];
char *mac_addr; char *mac_addr;
/* /*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment