hv_kvp_daemon.c 36 KB

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