diff options
author | Vitaly Kuznetsov <vkuznets@redhat.com> | 2017-04-30 19:21:15 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2017-05-18 10:55:28 -0400 |
commit | a1a7ea6bd6d55b6620c9c0a43bf684d6c427172a (patch) | |
tree | 53b1da2e829dc82b4e771653a3c5f72d4332a03b /tools/hv | |
parent | 6113e3d28149dee33f10e92df6a16efb2b9cf473 (diff) |
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: Tomas Hozza <thozza@redhat.com>
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'tools/hv')
-rw-r--r-- | 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 f1758fcbc37d..88b20e007c05 100644 --- a/tools/hv/hv_kvp_daemon.c +++ b/tools/hv/hv_kvp_daemon.c | |||
@@ -39,6 +39,7 @@ | |||
39 | #include <fcntl.h> | 39 | #include <fcntl.h> |
40 | #include <dirent.h> | 40 | #include <dirent.h> |
41 | #include <net/if.h> | 41 | #include <net/if.h> |
42 | #include <limits.h> | ||
42 | #include <getopt.h> | 43 | #include <getopt.h> |
43 | 44 | ||
44 | /* | 45 | /* |
@@ -97,6 +98,8 @@ static struct utsname uts_buf; | |||
97 | #define KVP_SCRIPTS_PATH "/usr/libexec/hypervkvpd/" | 98 | #define KVP_SCRIPTS_PATH "/usr/libexec/hypervkvpd/" |
98 | #endif | 99 | #endif |
99 | 100 | ||
101 | #define KVP_NET_DIR "/sys/class/net/" | ||
102 | |||
100 | #define MAX_FILE_NAME 100 | 103 | #define MAX_FILE_NAME 100 |
101 | #define ENTRIES_PER_BLOCK 50 | 104 | #define ENTRIES_PER_BLOCK 50 |
102 | 105 | ||
@@ -596,26 +599,21 @@ static char *kvp_get_if_name(char *guid) | |||
596 | DIR *dir; | 599 | DIR *dir; |
597 | struct dirent *entry; | 600 | struct dirent *entry; |
598 | FILE *file; | 601 | FILE *file; |
599 | char *p, *q, *x; | 602 | char *p, *x; |
600 | char *if_name = NULL; | 603 | char *if_name = NULL; |
601 | char buf[256]; | 604 | char buf[256]; |
602 | char *kvp_net_dir = "/sys/class/net/"; | 605 | char dev_id[PATH_MAX]; |
603 | char dev_id[256]; | ||
604 | 606 | ||
605 | dir = opendir(kvp_net_dir); | 607 | dir = opendir(KVP_NET_DIR); |
606 | if (dir == NULL) | 608 | if (dir == NULL) |
607 | return NULL; | 609 | return NULL; |
608 | 610 | ||
609 | snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir); | ||
610 | q = dev_id + strlen(kvp_net_dir); | ||
611 | |||
612 | while ((entry = readdir(dir)) != NULL) { | 611 | while ((entry = readdir(dir)) != NULL) { |
613 | /* | 612 | /* |
614 | * Set the state for the next pass. | 613 | * Set the state for the next pass. |
615 | */ | 614 | */ |
616 | *q = '\0'; | 615 | snprintf(dev_id, sizeof(dev_id), "%s%s/device/device_id", |
617 | strcat(dev_id, entry->d_name); | 616 | KVP_NET_DIR, entry->d_name); |
618 | strcat(dev_id, "/device/device_id"); | ||
619 | 617 | ||
620 | file = fopen(dev_id, "r"); | 618 | file = fopen(dev_id, "r"); |
621 | if (file == NULL) | 619 | if (file == NULL) |
@@ -653,12 +651,12 @@ static char *kvp_if_name_to_mac(char *if_name) | |||
653 | FILE *file; | 651 | FILE *file; |
654 | char *p, *x; | 652 | char *p, *x; |
655 | char buf[256]; | 653 | char buf[256]; |
656 | char addr_file[256]; | 654 | char addr_file[PATH_MAX]; |
657 | unsigned int i; | 655 | unsigned int i; |
658 | char *mac_addr = NULL; | 656 | char *mac_addr = NULL; |
659 | 657 | ||
660 | snprintf(addr_file, sizeof(addr_file), "%s%s%s", "/sys/class/net/", | 658 | snprintf(addr_file, sizeof(addr_file), "%s%s%s", KVP_NET_DIR, |
661 | if_name, "/address"); | 659 | if_name, "/address"); |
662 | 660 | ||
663 | file = fopen(addr_file, "r"); | 661 | file = fopen(addr_file, "r"); |
664 | if (file == NULL) | 662 | if (file == NULL) |
@@ -688,28 +686,22 @@ static char *kvp_mac_to_if_name(char *mac) | |||
688 | DIR *dir; | 686 | DIR *dir; |
689 | struct dirent *entry; | 687 | struct dirent *entry; |
690 | FILE *file; | 688 | FILE *file; |
691 | char *p, *q, *x; | 689 | char *p, *x; |
692 | char *if_name = NULL; | 690 | char *if_name = NULL; |
693 | char buf[256]; | 691 | char buf[256]; |
694 | char *kvp_net_dir = "/sys/class/net/"; | 692 | char dev_id[PATH_MAX]; |
695 | char dev_id[256]; | ||
696 | unsigned int i; | 693 | unsigned int i; |
697 | 694 | ||
698 | dir = opendir(kvp_net_dir); | 695 | dir = opendir(KVP_NET_DIR); |
699 | if (dir == NULL) | 696 | if (dir == NULL) |
700 | return NULL; | 697 | return NULL; |
701 | 698 | ||
702 | snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir); | ||
703 | q = dev_id + strlen(kvp_net_dir); | ||
704 | |||
705 | while ((entry = readdir(dir)) != NULL) { | 699 | while ((entry = readdir(dir)) != NULL) { |
706 | /* | 700 | /* |
707 | * Set the state for the next pass. | 701 | * Set the state for the next pass. |
708 | */ | 702 | */ |
709 | *q = '\0'; | 703 | snprintf(dev_id, sizeof(dev_id), "%s%s/address", KVP_NET_DIR, |
710 | 704 | entry->d_name); | |
711 | strcat(dev_id, entry->d_name); | ||
712 | strcat(dev_id, "/address"); | ||
713 | 705 | ||
714 | file = fopen(dev_id, "r"); | 706 | file = fopen(dev_id, "r"); |
715 | if (file == NULL) | 707 | if (file == NULL) |
@@ -1218,9 +1210,9 @@ static int process_ip_string(FILE *f, char *ip_string, int type) | |||
1218 | static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val) | 1210 | static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val) |
1219 | { | 1211 | { |
1220 | int error = 0; | 1212 | int error = 0; |
1221 | char if_file[128]; | 1213 | char if_file[PATH_MAX]; |
1222 | FILE *file; | 1214 | FILE *file; |
1223 | char cmd[512]; | 1215 | char cmd[PATH_MAX]; |
1224 | char *mac_addr; | 1216 | char *mac_addr; |
1225 | 1217 | ||
1226 | /* | 1218 | /* |