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