hv_kvp_daemon.c 36 KB

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