hv_kvp_daemon.c 36 KB

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