^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * An implementation of key value pair (KVP) functionality for Linux.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2010, Novell, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author : K. Y. Srinivasan <ksrinivasan@novell.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * This program is free software; you can redistribute it and/or modify it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * under the terms of the GNU General Public License version 2 as published
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * by the Free Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * This program is distributed in the hope that it will be useful, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * NON INFRINGEMENT. See the GNU General Public License for more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * You should have received a copy of the GNU General Public License
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * along with this program; if not, write to the Free Software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <sys/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <sys/utsname.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <arpa/inet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/hyperv.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <ifaddrs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <netdb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <syslog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <dirent.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <net/if.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <getopt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * KVP protocol: The user mode component first registers with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * the kernel component. Subsequently, the kernel component requests, data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * for the specified keys. In response to this message the user mode component
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * fills in the value corresponding to the specified key. We overload the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * sequence field in the cn_msg header to define our KVP message types.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * We use this infrastructure for also supporting queries from user mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * application for state that may be maintained in the KVP kernel component.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) enum key_index {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) FullyQualifiedDomainName = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) IntegrationServicesVersion, /*This key is serviced in the kernel*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) NetworkAddressIPv4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) NetworkAddressIPv6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) OSBuildNumber,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) OSName,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) OSMajorVersion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) OSMinorVersion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) OSVersion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) ProcessorArchitecture
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) IPADDR = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) NETMASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) GATEWAY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) DNS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static int in_hand_shake;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static char *os_name = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static char *os_major = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static char *os_minor = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static char *processor_arch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static char *os_build;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static char *os_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static char *lic_version = "Unknown version";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static char full_domain_name[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static struct utsname uts_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * The location of the interface configuration file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #define KVP_CONFIG_LOC "/var/lib/hyperv"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #ifndef KVP_SCRIPTS_PATH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #define KVP_SCRIPTS_PATH "/usr/libexec/hypervkvpd/"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #define KVP_NET_DIR "/sys/class/net/"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #define MAX_FILE_NAME 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #define ENTRIES_PER_BLOCK 50
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct kvp_record {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) char key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) char value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct kvp_file_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) int num_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct kvp_record *records;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) int num_records;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) char fname[MAX_FILE_NAME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static void kvp_acquire_lock(int pool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) fl.l_pid = getpid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) syslog(LOG_ERR, "Failed to acquire the lock pool: %d; error: %d %s", pool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) errno, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static void kvp_release_lock(int pool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct flock fl = {F_UNLCK, SEEK_SET, 0, 0, 0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) fl.l_pid = getpid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) syslog(LOG_ERR, "Failed to release the lock pool: %d; error: %d %s", pool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) errno, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static void kvp_update_file(int pool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) FILE *filep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * We are going to write our in-memory registry out to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * disk; acquire the lock first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) kvp_acquire_lock(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) filep = fopen(kvp_file_info[pool].fname, "we");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (!filep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) syslog(LOG_ERR, "Failed to open file, pool: %d; error: %d %s", pool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) errno, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) kvp_release_lock(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) fwrite(kvp_file_info[pool].records, sizeof(struct kvp_record),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) kvp_file_info[pool].num_records, filep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (ferror(filep) || fclose(filep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) kvp_release_lock(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) syslog(LOG_ERR, "Failed to write file, pool: %d", pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) kvp_release_lock(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static void kvp_update_mem_state(int pool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) FILE *filep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) size_t records_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct kvp_record *record = kvp_file_info[pool].records;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct kvp_record *readp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) int num_blocks = kvp_file_info[pool].num_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) kvp_acquire_lock(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) filep = fopen(kvp_file_info[pool].fname, "re");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (!filep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) syslog(LOG_ERR, "Failed to open file, pool: %d; error: %d %s", pool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) errno, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) kvp_release_lock(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) readp = &record[records_read];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) records_read += fread(readp, sizeof(struct kvp_record),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) ENTRIES_PER_BLOCK * num_blocks - records_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) filep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (ferror(filep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) syslog(LOG_ERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) "Failed to read file, pool: %d; error: %d %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) pool, errno, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) kvp_release_lock(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (!feof(filep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * We have more data to read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) num_blocks++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) record = realloc(record, alloc_unit * num_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (record == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) syslog(LOG_ERR, "malloc failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) kvp_release_lock(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) kvp_file_info[pool].num_blocks = num_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) kvp_file_info[pool].records = record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) kvp_file_info[pool].num_records = records_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) fclose(filep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) kvp_release_lock(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static int kvp_file_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) char *fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (access(KVP_CONFIG_LOC, F_OK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (mkdir(KVP_CONFIG_LOC, 0755 /* rwxr-xr-x */)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) syslog(LOG_ERR, "Failed to create '%s'; error: %d %s", KVP_CONFIG_LOC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) errno, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) for (i = 0; i < KVP_POOL_COUNT; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) fname = kvp_file_info[i].fname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) sprintf(fname, "%s/.kvp_pool_%d", KVP_CONFIG_LOC, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) fd = open(fname, O_RDWR | O_CREAT | O_CLOEXEC, 0644 /* rw-r--r-- */);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (fd == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) kvp_file_info[i].fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) kvp_file_info[i].num_blocks = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) kvp_file_info[i].records = malloc(alloc_unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (kvp_file_info[i].records == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) kvp_file_info[i].num_records = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) kvp_update_mem_state(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) static int kvp_key_delete(int pool, const __u8 *key, int key_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) int j, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) int num_records;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct kvp_record *record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * First update the in-memory state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) kvp_update_mem_state(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) num_records = kvp_file_info[pool].num_records;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) record = kvp_file_info[pool].records;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) for (i = 0; i < num_records; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (memcmp(key, record[i].key, key_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * Found a match; just move the remaining
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * entries up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (i == (num_records - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) kvp_file_info[pool].num_records--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) kvp_update_file(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) j = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) k = j + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) for (; k < num_records; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) strcpy(record[j].key, record[k].key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) strcpy(record[j].value, record[k].value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) j++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) kvp_file_info[pool].num_records--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) kvp_update_file(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static int kvp_key_add_or_modify(int pool, const __u8 *key, int key_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) const __u8 *value, int value_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) int num_records;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct kvp_record *record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) int num_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * First update the in-memory state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) kvp_update_mem_state(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) num_records = kvp_file_info[pool].num_records;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) record = kvp_file_info[pool].records;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) num_blocks = kvp_file_info[pool].num_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) for (i = 0; i < num_records; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (memcmp(key, record[i].key, key_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * Found a match; just update the value -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * this is the modify case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) memcpy(record[i].value, value, value_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) kvp_update_file(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * Need to add a new entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) /* Need to allocate a larger array for reg entries. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) record = realloc(record, sizeof(struct kvp_record) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) ENTRIES_PER_BLOCK * (num_blocks + 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (record == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) kvp_file_info[pool].num_blocks++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) memcpy(record[i].value, value, value_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) memcpy(record[i].key, key, key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) kvp_file_info[pool].records = record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) kvp_file_info[pool].num_records++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) kvp_update_file(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) static int kvp_get_value(int pool, const __u8 *key, int key_size, __u8 *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) int value_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) int num_records;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) struct kvp_record *record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * First update the in-memory state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) kvp_update_mem_state(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) num_records = kvp_file_info[pool].num_records;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) record = kvp_file_info[pool].records;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) for (i = 0; i < num_records; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (memcmp(key, record[i].key, key_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * Found a match; just copy the value out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) memcpy(value, record[i].value, value_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) static int kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) __u8 *value, int value_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) struct kvp_record *record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * First update our in-memory database.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) kvp_update_mem_state(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) record = kvp_file_info[pool].records;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (index >= kvp_file_info[pool].num_records) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) memcpy(key, record[index].key, key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) memcpy(value, record[index].value, value_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) void kvp_get_os_info(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) FILE *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) char *p, buf[512];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) uname(&uts_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) os_version = uts_buf.release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) os_build = strdup(uts_buf.release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) os_name = uts_buf.sysname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) processor_arch = uts_buf.machine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * The current windows host (win7) expects the build
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) * string to be of the form: x.y.z
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) * Strip additional information we may have.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) p = strchr(os_version, '-');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) *p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) * Parse the /etc/os-release file if present:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) * https://www.freedesktop.org/software/systemd/man/os-release.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) file = fopen("/etc/os-release", "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) if (file != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) while (fgets(buf, sizeof(buf), file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) char *value, *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) /* Ignore comments */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (buf[0] == '#')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) /* Split into name=value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) p = strchr(buf, '=');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) *p++ = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) /* Remove quotes and newline; un-escape */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) value = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) q = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (*p == '\\') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) ++p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) if (!*p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) *q++ = *p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) } else if (*p == '\'' || *p == '"' ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) *p == '\n') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) ++p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) *q++ = *p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) *q = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) if (!strcmp(buf, "NAME")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) p = strdup(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) os_name = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) } else if (!strcmp(buf, "VERSION_ID")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) p = strdup(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) os_major = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) fclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) /* Fallback for older RH/SUSE releases */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) file = fopen("/etc/SuSE-release", "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if (file != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) goto kvp_osinfo_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) file = fopen("/etc/redhat-release", "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (file != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) goto kvp_osinfo_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) * We don't have information about the os.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) kvp_osinfo_found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) /* up to three lines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) p = fgets(buf, sizeof(buf), file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) p = strchr(buf, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) *p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) p = strdup(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) os_name = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) /* second line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) p = fgets(buf, sizeof(buf), file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) p = strchr(buf, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) *p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) p = strdup(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) os_major = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) /* third line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) p = fgets(buf, sizeof(buf), file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) p = strchr(buf, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) *p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) p = strdup(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) os_minor = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) fclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) * Retrieve an interface name corresponding to the specified guid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) * If there is a match, the function returns a pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * to the interface name and if not, a NULL is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * If a match is found, the caller is responsible for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) * freeing the memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) static char *kvp_get_if_name(char *guid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) DIR *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) struct dirent *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) FILE *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) char *p, *x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) char *if_name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) char buf[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) char dev_id[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) dir = opendir(KVP_NET_DIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) if (dir == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) while ((entry = readdir(dir)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) * Set the state for the next pass.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) snprintf(dev_id, sizeof(dev_id), "%s%s/device/device_id",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) KVP_NET_DIR, entry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) file = fopen(dev_id, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) if (file == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) p = fgets(buf, sizeof(buf), file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) x = strchr(p, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) if (x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) *x = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) if (!strcmp(p, guid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) * Found the guid match; return the interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) * name. The caller will free the memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) if_name = strdup(entry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) fclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) fclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) closedir(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) return if_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) * Retrieve the MAC address given the interface name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) static char *kvp_if_name_to_mac(char *if_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) FILE *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) char *p, *x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) char buf[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) char addr_file[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) char *mac_addr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) snprintf(addr_file, sizeof(addr_file), "%s%s%s", KVP_NET_DIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) if_name, "/address");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) file = fopen(addr_file, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) if (file == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) p = fgets(buf, sizeof(buf), file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) x = strchr(p, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) if (x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) *x = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) for (i = 0; i < strlen(p); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) p[i] = toupper(p[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) mac_addr = strdup(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) fclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) return mac_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) static void kvp_process_ipconfig_file(char *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) char *config_buf, unsigned int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) int element_size, int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) char buf[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) char *x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) FILE *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) * First execute the command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) file = popen(cmd, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (file == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) if (offset == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) memset(config_buf, 0, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) if (len < strlen(config_buf) + element_size + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) x = strchr(p, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) if (x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) *x = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) strcat(config_buf, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) strcat(config_buf, ";");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) pclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) static void kvp_get_ipconfig_info(char *if_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) struct hv_kvp_ipaddr_value *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) char cmd[512];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) char dhcp_info[128];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) FILE *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) * Get the address of default gateway (ipv4).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) sprintf(cmd, "%s %s", "ip route show dev", if_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) strcat(cmd, " | awk '/default/ {print $3 }'");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) * Execute the command to gather gateway info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) (MAX_GATEWAY_SIZE * 2), INET_ADDRSTRLEN, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) * Get the address of default gateway (ipv6).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) sprintf(cmd, "%s %s", "ip -f inet6 route show dev", if_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) strcat(cmd, " | awk '/default/ {print $3 }'");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) * Execute the command to gather gateway info (ipv6).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) (MAX_GATEWAY_SIZE * 2), INET6_ADDRSTRLEN, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) * Gather the DNS state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) * Since there is no standard way to get this information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) * across various distributions of interest; we just invoke
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) * an external script that needs to be ported across distros
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) * of interest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) * Following is the expected format of the information from the script:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) * ipaddr1 (nameserver1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) * ipaddr2 (nameserver2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) * .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) * .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) sprintf(cmd, KVP_SCRIPTS_PATH "%s", "hv_get_dns_info");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) * Execute the command to gather DNS info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) kvp_process_ipconfig_file(cmd, (char *)buffer->dns_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) (MAX_IP_ADDR_SIZE * 2), INET_ADDRSTRLEN, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * Gather the DHCP state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * We will gather this state by invoking an external script.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) * The parameter to the script is the interface name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) * Here is the expected output:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) * Enabled: DHCP enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) sprintf(cmd, KVP_SCRIPTS_PATH "%s %s", "hv_get_dhcp_info", if_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) file = popen(cmd, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) if (file == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) p = fgets(dhcp_info, sizeof(dhcp_info), file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) if (p == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) pclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) if (!strncmp(p, "Enabled", 7))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) buffer->dhcp_enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) buffer->dhcp_enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) pclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) static unsigned int hweight32(unsigned int *w)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) unsigned int res = *w - ((*w >> 1) & 0x55555555);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) res = (res + (res >> 4)) & 0x0F0F0F0F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) res = res + (res >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) return (res + (res >> 16)) & 0x000000FF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) static int kvp_process_ip_address(void *addrp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) int family, char *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) int length, int *offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) struct sockaddr_in *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) struct sockaddr_in6 *addr6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) int addr_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) char tmp[50];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) const char *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) if (family == AF_INET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) addr = (struct sockaddr_in *)addrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) str = inet_ntop(family, &addr->sin_addr, tmp, 50);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) addr_length = INET_ADDRSTRLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) addr6 = (struct sockaddr_in6 *)addrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) addr_length = INET6_ADDRSTRLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) if ((length - *offset) < addr_length + 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) return HV_E_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) if (str == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) strcpy(buffer, "inet_ntop failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) return HV_E_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) if (*offset == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) strcpy(buffer, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) strcat(buffer, ";");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) strcat(buffer, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) *offset += strlen(str) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) kvp_get_ip_info(int family, char *if_name, int op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) void *out_buffer, unsigned int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) struct ifaddrs *ifap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) struct ifaddrs *curp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) int offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) int sn_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) char *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) struct hv_kvp_ipaddr_value *ip_buffer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) char cidr_mask[5]; /* /xyz */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) int weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) unsigned int *w;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) char *sn_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) struct sockaddr_in6 *addr6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) if (op == KVP_OP_ENUMERATE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) buffer = out_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) ip_buffer = out_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) buffer = (char *)ip_buffer->ip_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) ip_buffer->addr_family = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) * On entry into this function, the buffer is capable of holding the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) * maximum key value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) if (getifaddrs(&ifap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) strcpy(buffer, "getifaddrs failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) return HV_E_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) curp = ifap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) while (curp != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) if (curp->ifa_addr == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) curp = curp->ifa_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) if ((if_name != NULL) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) (strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) * We want info about a specific interface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) * just continue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) curp = curp->ifa_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) * We only support two address families: AF_INET and AF_INET6.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) * If a family value of 0 is specified, we collect both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) * supported address families; if not we gather info on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) * the specified address family.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) if ((((family != 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) (curp->ifa_addr->sa_family != family))) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) (curp->ifa_flags & IFF_LOOPBACK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) curp = curp->ifa_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) if ((curp->ifa_addr->sa_family != AF_INET) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) (curp->ifa_addr->sa_family != AF_INET6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) curp = curp->ifa_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) if (op == KVP_OP_GET_IP_INFO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) * Gather info other than the IP address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) * IP address info will be gathered later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) if (curp->ifa_addr->sa_family == AF_INET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) * Get subnet info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) error = kvp_process_ip_address(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) curp->ifa_netmask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) AF_INET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) (char *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) ip_buffer->sub_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) &sn_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) goto gather_ipaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) * Get subnet info in CIDR format.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) weight = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) sn_str = (char *)ip_buffer->sub_net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) addr6 = (struct sockaddr_in6 *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) curp->ifa_netmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) w = addr6->sin6_addr.s6_addr32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) for (i = 0; i < 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) weight += hweight32(&w[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) sprintf(cidr_mask, "/%d", weight);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) if (length < sn_offset + strlen(cidr_mask) + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) goto gather_ipaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) if (sn_offset == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) strcpy(sn_str, cidr_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) strcat((char *)ip_buffer->sub_net, ";");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) strcat(sn_str, cidr_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) sn_offset += strlen(sn_str) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) * Collect other ip related configuration info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) kvp_get_ipconfig_info(if_name, ip_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) gather_ipaddr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) error = kvp_process_ip_address(curp->ifa_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) curp->ifa_addr->sa_family,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) length, &offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) goto getaddr_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) curp = curp->ifa_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) getaddr_done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) freeifaddrs(ifap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) * Retrieve the IP given the MAC address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) static int kvp_mac_to_ip(struct hv_kvp_ipaddr_value *kvp_ip_val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) char *mac = (char *)kvp_ip_val->adapter_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) DIR *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) struct dirent *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) FILE *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) char *p, *x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) char *if_name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) char buf[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) char dev_id[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) int error = HV_E_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) dir = opendir(KVP_NET_DIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) if (dir == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) return HV_E_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) while ((entry = readdir(dir)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) * Set the state for the next pass.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) snprintf(dev_id, sizeof(dev_id), "%s%s/address", KVP_NET_DIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) entry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) file = fopen(dev_id, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) if (file == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) p = fgets(buf, sizeof(buf), file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) fclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) x = strchr(p, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) if (x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) *x = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) for (i = 0; i < strlen(p); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) p[i] = toupper(p[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) if (strcmp(p, mac))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) * Found the MAC match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) * A NIC (e.g. VF) matching the MAC, but without IP, is skipped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) if_name = entry->d_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) if (!if_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) error = kvp_get_ip_info(0, if_name, KVP_OP_GET_IP_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) kvp_ip_val, MAX_IP_ADDR_SIZE * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) if (!error && strlen((char *)kvp_ip_val->ip_addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) closedir(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) static int expand_ipv6(char *addr, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) struct in6_addr v6_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) ret = inet_pton(AF_INET6, addr, &v6_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) if (ret != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) if (type == NETMASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) sprintf(addr, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) "%02x%02x:%02x%02x:%02x%02x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) (int)v6_addr.s6_addr[0], (int)v6_addr.s6_addr[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) (int)v6_addr.s6_addr[2], (int)v6_addr.s6_addr[3],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) (int)v6_addr.s6_addr[4], (int)v6_addr.s6_addr[5],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) (int)v6_addr.s6_addr[6], (int)v6_addr.s6_addr[7],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) (int)v6_addr.s6_addr[8], (int)v6_addr.s6_addr[9],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) (int)v6_addr.s6_addr[10], (int)v6_addr.s6_addr[11],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) (int)v6_addr.s6_addr[12], (int)v6_addr.s6_addr[13],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) (int)v6_addr.s6_addr[14], (int)v6_addr.s6_addr[15]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) static int is_ipv4(char *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) struct in_addr ipv4_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) ret = inet_pton(AF_INET, addr, &ipv4_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) if (ret == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) static int parse_ip_val_buffer(char *in_buf, int *offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) char *out_buf, int out_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) char *x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) char *start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) * in_buf has sequence of characters that are separated by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) * the character ';'. The last sequence does not have the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) * terminating ";" character.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) start = in_buf + *offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) x = strchr(start, ';');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) if (x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) *x = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) x = start + strlen(start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) if (strlen(start) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) * Get rid of leading spaces.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) while (start[i] == ' ')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) if ((x - start) <= out_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) strcpy(out_buf, (start + i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) *offset += (x - start) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) static int kvp_write_file(FILE *f, char *s1, char *s2, char *s3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) ret = fprintf(f, "%s%s%s%s\n", s1, s2, "=", s3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) return HV_E_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) static int process_ip_string(FILE *f, char *ip_string, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) char addr[INET6_ADDRSTRLEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) int j = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) char str[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) char sub_str[13];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) int offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) memset(addr, 0, sizeof(addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) while (parse_ip_val_buffer(ip_string, &offset, addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) (MAX_IP_ADDR_SIZE * 2))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) sub_str[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) if (is_ipv4(addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) case IPADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) snprintf(str, sizeof(str), "%s", "IPADDR");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) case NETMASK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) snprintf(str, sizeof(str), "%s", "NETMASK");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) case GATEWAY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) snprintf(str, sizeof(str), "%s", "GATEWAY");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) case DNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) snprintf(str, sizeof(str), "%s", "DNS");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) if (type == DNS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) snprintf(sub_str, sizeof(sub_str), "%d", ++i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) } else if (type == GATEWAY && i == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) ++i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) snprintf(sub_str, sizeof(sub_str), "%d", i++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) } else if (expand_ipv6(addr, type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) case IPADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) snprintf(str, sizeof(str), "%s", "IPV6ADDR");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) case NETMASK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) snprintf(str, sizeof(str), "%s", "IPV6NETMASK");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) case GATEWAY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) snprintf(str, sizeof(str), "%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) "IPV6_DEFAULTGW");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) case DNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) snprintf(str, sizeof(str), "%s", "DNS");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) if (type == DNS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) snprintf(sub_str, sizeof(sub_str), "%d", ++i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) } else if (j == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) ++j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) snprintf(sub_str, sizeof(sub_str), "_%d", j++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) return HV_INVALIDARG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) error = kvp_write_file(f, str, sub_str, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) memset(addr, 0, sizeof(addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) char if_file[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) FILE *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) char cmd[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) char *mac_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) int str_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) * Set the configuration for the specified interface with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) * the information provided. Since there is no standard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) * way to configure an interface, we will have an external
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) * script that does the job of configuring the interface and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) * flushing the configuration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) * The parameters passed to this external script are:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) * 1. A configuration file that has the specified configuration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) * We will embed the name of the interface in the configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) * file: ifcfg-ethx (where ethx is the interface name).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) * The information provided here may be more than what is needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) * in a given distro to configure the interface and so are free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) * ignore information that may not be relevant.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) * Here is the format of the ip configuration file:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) * HWADDR=macaddr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) * DEVICE=interface name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) * BOOTPROTO=<protocol> (where <protocol> is "dhcp" if DHCP is configured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) * or "none" if no boot-time protocol should be used)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) * IPADDR0=ipaddr1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) * IPADDR1=ipaddr2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) * IPADDRx=ipaddry (where y = x + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) * NETMASK0=netmask1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) * NETMASKx=netmasky (where y = x + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) * GATEWAY=ipaddr1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) * GATEWAYx=ipaddry (where y = x + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) * DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) * IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) * tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) * IPV6NETMASK.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) * The host can specify multiple ipv4 and ipv6 addresses to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) * configured for the interface. Furthermore, the configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) * needs to be persistent. A subsequent GET call on the interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) * is expected to return the configuration that is set via the SET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) * call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) snprintf(if_file, sizeof(if_file), "%s%s%s", KVP_CONFIG_LOC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) "/ifcfg-", if_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) file = fopen(if_file, "w");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) if (file == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) syslog(LOG_ERR, "Failed to open config file; error: %d %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) errno, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) return HV_E_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) * First write out the MAC address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) mac_addr = kvp_if_name_to_mac(if_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) if (mac_addr == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) error = HV_E_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) goto setval_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) error = kvp_write_file(file, "HWADDR", "", mac_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) free(mac_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) goto setval_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) error = kvp_write_file(file, "DEVICE", "", if_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) goto setval_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) * The dhcp_enabled flag is only for IPv4. In the case the host only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) * injects an IPv6 address, the flag is true, but we still need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) * proceed to parse and pass the IPv6 information to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) * disto-specific script hv_set_ifconfig.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) if (new_val->dhcp_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) error = kvp_write_file(file, "BOOTPROTO", "", "dhcp");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) goto setval_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) error = kvp_write_file(file, "BOOTPROTO", "", "none");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) goto setval_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) * Write the configuration for ipaddress, netmask, gateway and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) * name servers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) error = process_ip_string(file, (char *)new_val->ip_addr, IPADDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) goto setval_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) error = process_ip_string(file, (char *)new_val->sub_net, NETMASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) goto setval_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) error = process_ip_string(file, (char *)new_val->gate_way, GATEWAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) goto setval_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) error = process_ip_string(file, (char *)new_val->dns_addr, DNS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) goto setval_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) fclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) * Now that we have populated the configuration file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) * invoke the external script to do its magic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) str_len = snprintf(cmd, sizeof(cmd), KVP_SCRIPTS_PATH "%s %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) "hv_set_ifconfig", if_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) * This is a little overcautious, but it's necessary to suppress some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) * false warnings from gcc 8.0.1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) if (str_len <= 0 || (unsigned int)str_len >= sizeof(cmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) syslog(LOG_ERR, "Cmd '%s' (len=%d) may be too long",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) cmd, str_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) return HV_E_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) if (system(cmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) syslog(LOG_ERR, "Failed to execute cmd '%s'; error: %d %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) cmd, errno, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) return HV_E_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) setval_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) syslog(LOG_ERR, "Failed to write config file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) fclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) kvp_get_domain_name(char *buffer, int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) struct addrinfo hints, *info ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) gethostname(buffer, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) memset(&hints, 0, sizeof(hints));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) hints.ai_socktype = SOCK_STREAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) hints.ai_flags = AI_CANONNAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) error = getaddrinfo(buffer, NULL, &hints, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) if (error != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) snprintf(buffer, length, "getaddrinfo failed: 0x%x %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) error, gai_strerror(error));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) snprintf(buffer, length, "%s", info->ai_canonname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) freeaddrinfo(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) void print_usage(char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) fprintf(stderr, "Usage: %s [options]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) "Options are:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) " -n, --no-daemon stay in foreground, don't daemonize\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) " -h, --help print this help\n", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) int kvp_fd = -1, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) struct pollfd pfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) struct hv_kvp_msg hv_msg[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) char *key_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) char *key_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) int op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) int pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) char *if_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) struct hv_kvp_ipaddr_value *kvp_ip_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) int daemonize = 1, long_index = 0, opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) static struct option long_options[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) {"help", no_argument, 0, 'h' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) {"no-daemon", no_argument, 0, 'n' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) {0, 0, 0, 0 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) while ((opt = getopt_long(argc, argv, "hn", long_options,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) &long_index)) != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) switch (opt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) case 'n':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) daemonize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) case 'h':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) print_usage(argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) print_usage(argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) if (daemonize && daemon(1, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) openlog("KVP", 0, LOG_USER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) * Retrieve OS release information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) kvp_get_os_info();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) * Cache Fully Qualified Domain Name because getaddrinfo takes an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) * unpredictable amount of time to finish.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) kvp_get_domain_name(full_domain_name, sizeof(full_domain_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) if (kvp_file_init()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) syslog(LOG_ERR, "Failed to initialize the pools");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) reopen_kvp_fd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) if (kvp_fd != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) close(kvp_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) in_hand_shake = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) kvp_fd = open("/dev/vmbus/hv_kvp", O_RDWR | O_CLOEXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) if (kvp_fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) syslog(LOG_ERR, "open /dev/vmbus/hv_kvp failed; error: %d %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) errno, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) * Register ourselves with the kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) len = write(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) if (len != sizeof(struct hv_kvp_msg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) syslog(LOG_ERR, "registration to kernel failed; error: %d %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) errno, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) close(kvp_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) pfd.fd = kvp_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) pfd.events = POLLIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) pfd.revents = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) if (poll(&pfd, 1, -1) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) syslog(LOG_ERR, "poll failed; error: %d %s", errno, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) if (errno == EINVAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) close(kvp_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) len = read(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) if (len != sizeof(struct hv_kvp_msg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) syslog(LOG_ERR, "read failed; error:%d %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) errno, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) goto reopen_kvp_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) * We will use the KVP header information to pass back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) * the error from this daemon. So, first copy the state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) * and set the error code to success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) op = hv_msg->kvp_hdr.operation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) pool = hv_msg->kvp_hdr.pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) hv_msg->error = HV_S_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) * Driver is registering with us; stash away the version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) * information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) in_hand_shake = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) p = (char *)hv_msg->body.kvp_register.version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) lic_version = malloc(strlen(p) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) if (lic_version) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) strcpy(lic_version, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) syslog(LOG_INFO, "KVP LIC Version: %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) lic_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) syslog(LOG_ERR, "malloc failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) switch (op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) case KVP_OP_GET_IP_INFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) kvp_ip_val = &hv_msg->body.kvp_ip_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) error = kvp_mac_to_ip(kvp_ip_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) hv_msg->error = error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) case KVP_OP_SET_IP_INFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) kvp_ip_val = &hv_msg->body.kvp_ip_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) if_name = kvp_get_if_name(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) (char *)kvp_ip_val->adapter_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) if (if_name == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) * We could not map the guid to an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) * interface name; return error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) hv_msg->error = HV_GUID_NOTFOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) error = kvp_set_ip_info(if_name, kvp_ip_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) hv_msg->error = error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) free(if_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) case KVP_OP_SET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) if (kvp_key_add_or_modify(pool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) hv_msg->body.kvp_set.data.key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) hv_msg->body.kvp_set.data.key_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) hv_msg->body.kvp_set.data.value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) hv_msg->body.kvp_set.data.value_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) hv_msg->error = HV_S_CONT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) case KVP_OP_GET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) if (kvp_get_value(pool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) hv_msg->body.kvp_set.data.key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) hv_msg->body.kvp_set.data.key_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) hv_msg->body.kvp_set.data.value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) hv_msg->body.kvp_set.data.value_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) hv_msg->error = HV_S_CONT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) case KVP_OP_DELETE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) if (kvp_key_delete(pool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) hv_msg->body.kvp_delete.key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) hv_msg->body.kvp_delete.key_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) hv_msg->error = HV_S_CONT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) if (op != KVP_OP_ENUMERATE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) goto kvp_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) * If the pool is KVP_POOL_AUTO, dynamically generate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) * both the key and the value; if not read from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) * appropriate pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) if (pool != KVP_POOL_AUTO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) if (kvp_pool_enumerate(pool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) hv_msg->body.kvp_enum_data.index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) hv_msg->body.kvp_enum_data.data.key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) HV_KVP_EXCHANGE_MAX_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) hv_msg->body.kvp_enum_data.data.value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) hv_msg->error = HV_S_CONT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) goto kvp_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) switch (hv_msg->body.kvp_enum_data.index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) case FullyQualifiedDomainName:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) strcpy(key_value, full_domain_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) strcpy(key_name, "FullyQualifiedDomainName");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) case IntegrationServicesVersion:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) strcpy(key_name, "IntegrationServicesVersion");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) strcpy(key_value, lic_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) case NetworkAddressIPv4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) kvp_get_ip_info(AF_INET, NULL, KVP_OP_ENUMERATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) strcpy(key_name, "NetworkAddressIPv4");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) case NetworkAddressIPv6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) kvp_get_ip_info(AF_INET6, NULL, KVP_OP_ENUMERATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) strcpy(key_name, "NetworkAddressIPv6");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) case OSBuildNumber:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) strcpy(key_value, os_build);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) strcpy(key_name, "OSBuildNumber");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) case OSName:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) strcpy(key_value, os_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) strcpy(key_name, "OSName");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) case OSMajorVersion:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) strcpy(key_value, os_major);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) strcpy(key_name, "OSMajorVersion");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) case OSMinorVersion:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) strcpy(key_value, os_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) strcpy(key_name, "OSMinorVersion");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) case OSVersion:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) strcpy(key_value, os_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) strcpy(key_name, "OSVersion");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) case ProcessorArchitecture:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) strcpy(key_value, processor_arch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) strcpy(key_name, "ProcessorArchitecture");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) hv_msg->error = HV_S_CONT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) * Send the value back to the kernel. Note: the write() may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) * return an error due to hibernation; we can ignore the error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) * by resetting the dev file, i.e. closing and re-opening it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) kvp_done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) len = write(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) if (len != sizeof(struct hv_kvp_msg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) syslog(LOG_ERR, "write failed; error: %d %s", errno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) goto reopen_kvp_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) close(kvp_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) }