hv_kvp_daemon.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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 <linux/types.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <unistd.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #include <arpa/inet.h>
  34. #include <linux/connector.h>
  35. #include <linux/hyperv.h>
  36. #include <linux/netlink.h>
  37. #include <ifaddrs.h>
  38. #include <netdb.h>
  39. #include <syslog.h>
  40. #include <sys/stat.h>
  41. #include <fcntl.h>
  42. /*
  43. * KVP protocol: The user mode component first registers with the
  44. * the kernel component. Subsequently, the kernel component requests, data
  45. * for the specified keys. In response to this message the user mode component
  46. * fills in the value corresponding to the specified key. We overload the
  47. * sequence field in the cn_msg header to define our KVP message types.
  48. *
  49. * We use this infrastructure for also supporting queries from user mode
  50. * application for state that may be maintained in the KVP kernel component.
  51. *
  52. */
  53. enum key_index {
  54. FullyQualifiedDomainName = 0,
  55. IntegrationServicesVersion, /*This key is serviced in the kernel*/
  56. NetworkAddressIPv4,
  57. NetworkAddressIPv6,
  58. OSBuildNumber,
  59. OSName,
  60. OSMajorVersion,
  61. OSMinorVersion,
  62. OSVersion,
  63. ProcessorArchitecture
  64. };
  65. static char kvp_send_buffer[4096];
  66. static char kvp_recv_buffer[4096 * 2];
  67. static struct sockaddr_nl addr;
  68. static int in_hand_shake = 1;
  69. static char *os_name = "";
  70. static char *os_major = "";
  71. static char *os_minor = "";
  72. static char *processor_arch;
  73. static char *os_build;
  74. static char *lic_version = "Unknown version";
  75. static struct utsname uts_buf;
  76. #define MAX_FILE_NAME 100
  77. #define ENTRIES_PER_BLOCK 50
  78. struct kvp_record {
  79. __u8 key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
  80. __u8 value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
  81. };
  82. struct kvp_file_state {
  83. int fd;
  84. int num_blocks;
  85. struct kvp_record *records;
  86. int num_records;
  87. __u8 fname[MAX_FILE_NAME];
  88. };
  89. static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT];
  90. static void kvp_acquire_lock(int pool)
  91. {
  92. struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
  93. fl.l_pid = getpid();
  94. if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) {
  95. syslog(LOG_ERR, "Failed to acquire the lock pool: %d", pool);
  96. exit(-1);
  97. }
  98. }
  99. static void kvp_release_lock(int pool)
  100. {
  101. struct flock fl = {F_UNLCK, SEEK_SET, 0, 0, 0};
  102. fl.l_pid = getpid();
  103. if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) {
  104. perror("fcntl");
  105. syslog(LOG_ERR, "Failed to release the lock pool: %d", pool);
  106. exit(-1);
  107. }
  108. }
  109. static void kvp_update_file(int pool)
  110. {
  111. FILE *filep;
  112. size_t bytes_written;
  113. /*
  114. * We are going to write our in-memory registry out to
  115. * disk; acquire the lock first.
  116. */
  117. kvp_acquire_lock(pool);
  118. filep = fopen(kvp_file_info[pool].fname, "w");
  119. if (!filep) {
  120. kvp_release_lock(pool);
  121. syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
  122. exit(-1);
  123. }
  124. bytes_written = fwrite(kvp_file_info[pool].records,
  125. sizeof(struct kvp_record),
  126. kvp_file_info[pool].num_records, filep);
  127. fflush(filep);
  128. kvp_release_lock(pool);
  129. }
  130. static void kvp_update_mem_state(int pool)
  131. {
  132. FILE *filep;
  133. size_t records_read = 0;
  134. struct kvp_record *record = kvp_file_info[pool].records;
  135. struct kvp_record *readp;
  136. int num_blocks = kvp_file_info[pool].num_blocks;
  137. int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
  138. kvp_acquire_lock(pool);
  139. filep = fopen(kvp_file_info[pool].fname, "r");
  140. if (!filep) {
  141. kvp_release_lock(pool);
  142. syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
  143. exit(-1);
  144. }
  145. while (!feof(filep)) {
  146. readp = &record[records_read];
  147. records_read += fread(readp, sizeof(struct kvp_record),
  148. ENTRIES_PER_BLOCK * num_blocks,
  149. filep);
  150. if (!feof(filep)) {
  151. /*
  152. * We have more data to read.
  153. */
  154. num_blocks++;
  155. record = realloc(record, alloc_unit * num_blocks);
  156. if (record == NULL) {
  157. syslog(LOG_ERR, "malloc failed");
  158. exit(-1);
  159. }
  160. continue;
  161. }
  162. break;
  163. }
  164. kvp_file_info[pool].num_blocks = num_blocks;
  165. kvp_file_info[pool].records = record;
  166. kvp_file_info[pool].num_records = records_read;
  167. kvp_release_lock(pool);
  168. }
  169. static int kvp_file_init(void)
  170. {
  171. int ret, fd;
  172. FILE *filep;
  173. size_t records_read;
  174. __u8 *fname;
  175. struct kvp_record *record;
  176. struct kvp_record *readp;
  177. int num_blocks;
  178. int i;
  179. int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
  180. if (access("/var/opt/hyperv", F_OK)) {
  181. if (mkdir("/var/opt/hyperv", S_IRUSR | S_IWUSR | S_IROTH)) {
  182. syslog(LOG_ERR, " Failed to create /var/opt/hyperv");
  183. exit(-1);
  184. }
  185. }
  186. for (i = 0; i < KVP_POOL_COUNT; i++) {
  187. fname = kvp_file_info[i].fname;
  188. records_read = 0;
  189. num_blocks = 1;
  190. sprintf(fname, "/var/opt/hyperv/.kvp_pool_%d", i);
  191. fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IROTH);
  192. if (fd == -1)
  193. return 1;
  194. filep = fopen(fname, "r");
  195. if (!filep)
  196. return 1;
  197. record = malloc(alloc_unit * num_blocks);
  198. if (record == NULL) {
  199. fclose(filep);
  200. return 1;
  201. }
  202. while (!feof(filep)) {
  203. readp = &record[records_read];
  204. records_read += fread(readp, sizeof(struct kvp_record),
  205. ENTRIES_PER_BLOCK,
  206. filep);
  207. if (!feof(filep)) {
  208. /*
  209. * We have more data to read.
  210. */
  211. num_blocks++;
  212. record = realloc(record, alloc_unit *
  213. num_blocks);
  214. if (record == NULL) {
  215. fclose(filep);
  216. return 1;
  217. }
  218. continue;
  219. }
  220. break;
  221. }
  222. kvp_file_info[i].fd = fd;
  223. kvp_file_info[i].num_blocks = num_blocks;
  224. kvp_file_info[i].records = record;
  225. kvp_file_info[i].num_records = records_read;
  226. fclose(filep);
  227. }
  228. return 0;
  229. }
  230. static int kvp_key_delete(int pool, __u8 *key, int key_size)
  231. {
  232. int i;
  233. int j, k;
  234. int num_records;
  235. struct kvp_record *record;
  236. /*
  237. * First update the in-memory state.
  238. */
  239. kvp_update_mem_state(pool);
  240. num_records = kvp_file_info[pool].num_records;
  241. record = kvp_file_info[pool].records;
  242. for (i = 0; i < num_records; i++) {
  243. if (memcmp(key, record[i].key, key_size))
  244. continue;
  245. /*
  246. * Found a match; just move the remaining
  247. * entries up.
  248. */
  249. if (i == num_records) {
  250. kvp_file_info[pool].num_records--;
  251. kvp_update_file(pool);
  252. return 0;
  253. }
  254. j = i;
  255. k = j + 1;
  256. for (; k < num_records; k++) {
  257. strcpy(record[j].key, record[k].key);
  258. strcpy(record[j].value, record[k].value);
  259. j++;
  260. }
  261. kvp_file_info[pool].num_records--;
  262. kvp_update_file(pool);
  263. return 0;
  264. }
  265. return 1;
  266. }
  267. static int kvp_key_add_or_modify(int pool, __u8 *key, int key_size, __u8 *value,
  268. int value_size)
  269. {
  270. int i;
  271. int j, k;
  272. int num_records;
  273. struct kvp_record *record;
  274. int num_blocks;
  275. if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
  276. (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
  277. return 1;
  278. /*
  279. * First update the in-memory state.
  280. */
  281. kvp_update_mem_state(pool);
  282. num_records = kvp_file_info[pool].num_records;
  283. record = kvp_file_info[pool].records;
  284. num_blocks = kvp_file_info[pool].num_blocks;
  285. for (i = 0; i < num_records; i++) {
  286. if (memcmp(key, record[i].key, key_size))
  287. continue;
  288. /*
  289. * Found a match; just update the value -
  290. * this is the modify case.
  291. */
  292. memcpy(record[i].value, value, value_size);
  293. kvp_update_file(pool);
  294. return 0;
  295. }
  296. /*
  297. * Need to add a new entry;
  298. */
  299. if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
  300. /* Need to allocate a larger array for reg entries. */
  301. record = realloc(record, sizeof(struct kvp_record) *
  302. ENTRIES_PER_BLOCK * (num_blocks + 1));
  303. if (record == NULL)
  304. return 1;
  305. kvp_file_info[pool].num_blocks++;
  306. }
  307. memcpy(record[i].value, value, value_size);
  308. memcpy(record[i].key, key, key_size);
  309. kvp_file_info[pool].records = record;
  310. kvp_file_info[pool].num_records++;
  311. kvp_update_file(pool);
  312. return 0;
  313. }
  314. static int kvp_get_value(int pool, __u8 *key, int key_size, __u8 *value,
  315. int value_size)
  316. {
  317. int i;
  318. int num_records;
  319. struct kvp_record *record;
  320. if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
  321. (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
  322. return 1;
  323. /*
  324. * First update the in-memory state.
  325. */
  326. kvp_update_mem_state(pool);
  327. num_records = kvp_file_info[pool].num_records;
  328. record = kvp_file_info[pool].records;
  329. for (i = 0; i < num_records; i++) {
  330. if (memcmp(key, record[i].key, key_size))
  331. continue;
  332. /*
  333. * Found a match; just copy the value out.
  334. */
  335. memcpy(value, record[i].value, value_size);
  336. return 0;
  337. }
  338. return 1;
  339. }
  340. static int kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
  341. __u8 *value, int value_size)
  342. {
  343. struct kvp_record *record;
  344. /*
  345. * First update our in-memory database.
  346. */
  347. kvp_update_mem_state(pool);
  348. record = kvp_file_info[pool].records;
  349. if (index >= kvp_file_info[pool].num_records) {
  350. return 1;
  351. }
  352. memcpy(key, record[index].key, key_size);
  353. memcpy(value, record[index].value, value_size);
  354. return 0;
  355. }
  356. void kvp_get_os_info(void)
  357. {
  358. FILE *file;
  359. char *p, buf[512];
  360. uname(&uts_buf);
  361. os_build = uts_buf.release;
  362. processor_arch = uts_buf.machine;
  363. /*
  364. * The current windows host (win7) expects the build
  365. * string to be of the form: x.y.z
  366. * Strip additional information we may have.
  367. */
  368. p = strchr(os_build, '-');
  369. if (p)
  370. *p = '\0';
  371. file = fopen("/etc/SuSE-release", "r");
  372. if (file != NULL)
  373. goto kvp_osinfo_found;
  374. file = fopen("/etc/redhat-release", "r");
  375. if (file != NULL)
  376. goto kvp_osinfo_found;
  377. /*
  378. * Add code for other supported platforms.
  379. */
  380. /*
  381. * We don't have information about the os.
  382. */
  383. os_name = uts_buf.sysname;
  384. return;
  385. kvp_osinfo_found:
  386. /* up to three lines */
  387. p = fgets(buf, sizeof(buf), file);
  388. if (p) {
  389. p = strchr(buf, '\n');
  390. if (p)
  391. *p = '\0';
  392. p = strdup(buf);
  393. if (!p)
  394. goto done;
  395. os_name = p;
  396. /* second line */
  397. p = fgets(buf, sizeof(buf), file);
  398. if (p) {
  399. p = strchr(buf, '\n');
  400. if (p)
  401. *p = '\0';
  402. p = strdup(buf);
  403. if (!p)
  404. goto done;
  405. os_major = p;
  406. /* third line */
  407. p = fgets(buf, sizeof(buf), file);
  408. if (p) {
  409. p = strchr(buf, '\n');
  410. if (p)
  411. *p = '\0';
  412. p = strdup(buf);
  413. if (p)
  414. os_minor = p;
  415. }
  416. }
  417. }
  418. done:
  419. fclose(file);
  420. return;
  421. }
  422. static unsigned int hweight32(unsigned int *w)
  423. {
  424. unsigned int res = *w - ((*w >> 1) & 0x55555555);
  425. res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
  426. res = (res + (res >> 4)) & 0x0F0F0F0F;
  427. res = res + (res >> 8);
  428. return (res + (res >> 16)) & 0x000000FF;
  429. }
  430. static int kvp_process_ip_address(void *addrp,
  431. int family, char *buffer,
  432. int length, int *offset)
  433. {
  434. struct sockaddr_in *addr;
  435. struct sockaddr_in6 *addr6;
  436. int addr_length;
  437. char tmp[50];
  438. const char *str;
  439. if (family == AF_INET) {
  440. addr = (struct sockaddr_in *)addrp;
  441. str = inet_ntop(family, &addr->sin_addr, tmp, 50);
  442. addr_length = INET_ADDRSTRLEN;
  443. } else {
  444. addr6 = (struct sockaddr_in6 *)addrp;
  445. str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
  446. addr_length = INET6_ADDRSTRLEN;
  447. }
  448. if ((length - *offset) < addr_length + 1)
  449. return 1;
  450. if (str == NULL) {
  451. strcpy(buffer, "inet_ntop failed\n");
  452. return 1;
  453. }
  454. if (*offset == 0)
  455. strcpy(buffer, tmp);
  456. else
  457. strcat(buffer, tmp);
  458. strcat(buffer, ";");
  459. *offset += strlen(str) + 1;
  460. return 0;
  461. }
  462. static int
  463. kvp_get_ip_address(int family, char *if_name, int op,
  464. void *out_buffer, int length)
  465. {
  466. struct ifaddrs *ifap;
  467. struct ifaddrs *curp;
  468. int offset = 0;
  469. int sn_offset = 0;
  470. int error = 0;
  471. char *buffer;
  472. struct hv_kvp_ipaddr_value *ip_buffer;
  473. char cidr_mask[5]; /* /xyz */
  474. int weight;
  475. int i;
  476. unsigned int *w;
  477. char *sn_str;
  478. struct sockaddr_in6 *addr6;
  479. if (op == KVP_OP_ENUMERATE) {
  480. buffer = out_buffer;
  481. } else {
  482. ip_buffer = out_buffer;
  483. buffer = (char *)ip_buffer->ip_addr;
  484. ip_buffer->addr_family = 0;
  485. }
  486. /*
  487. * On entry into this function, the buffer is capable of holding the
  488. * maximum key value.
  489. */
  490. if (getifaddrs(&ifap)) {
  491. strcpy(buffer, "getifaddrs failed\n");
  492. return 1;
  493. }
  494. curp = ifap;
  495. while (curp != NULL) {
  496. if (curp->ifa_addr == NULL) {
  497. curp = curp->ifa_next;
  498. continue;
  499. }
  500. if ((if_name != NULL) &&
  501. (strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
  502. /*
  503. * We want info about a specific interface;
  504. * just continue.
  505. */
  506. curp = curp->ifa_next;
  507. continue;
  508. }
  509. /*
  510. * We only support two address families: AF_INET and AF_INET6.
  511. * If a family value of 0 is specified, we collect both
  512. * supported address families; if not we gather info on
  513. * the specified address family.
  514. */
  515. if ((family != 0) && (curp->ifa_addr->sa_family != family)) {
  516. curp = curp->ifa_next;
  517. continue;
  518. }
  519. if ((curp->ifa_addr->sa_family != AF_INET) &&
  520. (curp->ifa_addr->sa_family != AF_INET6)) {
  521. curp = curp->ifa_next;
  522. continue;
  523. }
  524. if (op == KVP_OP_GET_IP_INFO) {
  525. /*
  526. * Gather info other than the IP address.
  527. * IP address info will be gathered later.
  528. */
  529. if (curp->ifa_addr->sa_family == AF_INET) {
  530. ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
  531. /*
  532. * Get subnet info.
  533. */
  534. error = kvp_process_ip_address(
  535. curp->ifa_netmask,
  536. AF_INET,
  537. (char *)
  538. ip_buffer->sub_net,
  539. length,
  540. &sn_offset);
  541. if (error)
  542. goto gather_ipaddr;
  543. } else {
  544. ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
  545. /*
  546. * Get subnet info in CIDR format.
  547. */
  548. weight = 0;
  549. sn_str = (char *)ip_buffer->sub_net;
  550. addr6 = (struct sockaddr_in6 *)
  551. curp->ifa_netmask;
  552. w = addr6->sin6_addr.s6_addr32;
  553. for (i = 0; i < 4; i++)
  554. weight += hweight32(&w[i]);
  555. sprintf(cidr_mask, "/%d", weight);
  556. if ((length - sn_offset) <
  557. (strlen(cidr_mask) + 1))
  558. goto gather_ipaddr;
  559. if (sn_offset == 0)
  560. strcpy(sn_str, cidr_mask);
  561. else
  562. strcat(sn_str, cidr_mask);
  563. strcat((char *)ip_buffer->sub_net, ";");
  564. sn_offset += strlen(sn_str) + 1;
  565. }
  566. }
  567. gather_ipaddr:
  568. error = kvp_process_ip_address(curp->ifa_addr,
  569. curp->ifa_addr->sa_family,
  570. buffer,
  571. length, &offset);
  572. if (error)
  573. goto getaddr_done;
  574. curp = curp->ifa_next;
  575. }
  576. getaddr_done:
  577. freeifaddrs(ifap);
  578. return error;
  579. }
  580. static int
  581. kvp_get_domain_name(char *buffer, int length)
  582. {
  583. struct addrinfo hints, *info ;
  584. int error = 0;
  585. gethostname(buffer, length);
  586. memset(&hints, 0, sizeof(hints));
  587. hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
  588. hints.ai_socktype = SOCK_STREAM;
  589. hints.ai_flags = AI_CANONNAME;
  590. error = getaddrinfo(buffer, NULL, &hints, &info);
  591. if (error != 0) {
  592. strcpy(buffer, "getaddrinfo failed\n");
  593. return error;
  594. }
  595. strcpy(buffer, info->ai_canonname);
  596. freeaddrinfo(info);
  597. return error;
  598. }
  599. static int
  600. netlink_send(int fd, struct cn_msg *msg)
  601. {
  602. struct nlmsghdr *nlh;
  603. unsigned int size;
  604. struct msghdr message;
  605. char buffer[64];
  606. struct iovec iov[2];
  607. size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
  608. nlh = (struct nlmsghdr *)buffer;
  609. nlh->nlmsg_seq = 0;
  610. nlh->nlmsg_pid = getpid();
  611. nlh->nlmsg_type = NLMSG_DONE;
  612. nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
  613. nlh->nlmsg_flags = 0;
  614. iov[0].iov_base = nlh;
  615. iov[0].iov_len = sizeof(*nlh);
  616. iov[1].iov_base = msg;
  617. iov[1].iov_len = size;
  618. memset(&message, 0, sizeof(message));
  619. message.msg_name = &addr;
  620. message.msg_namelen = sizeof(addr);
  621. message.msg_iov = iov;
  622. message.msg_iovlen = 2;
  623. return sendmsg(fd, &message, 0);
  624. }
  625. int main(void)
  626. {
  627. int fd, len, sock_opt;
  628. int error;
  629. struct cn_msg *message;
  630. struct pollfd pfd;
  631. struct nlmsghdr *incoming_msg;
  632. struct cn_msg *incoming_cn_msg;
  633. struct hv_kvp_msg *hv_msg;
  634. char *p;
  635. char *key_value;
  636. char *key_name;
  637. int op;
  638. int pool;
  639. daemon(1, 0);
  640. openlog("KVP", 0, LOG_USER);
  641. syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
  642. /*
  643. * Retrieve OS release information.
  644. */
  645. kvp_get_os_info();
  646. if (kvp_file_init()) {
  647. syslog(LOG_ERR, "Failed to initialize the pools");
  648. exit(-1);
  649. }
  650. fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
  651. if (fd < 0) {
  652. syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
  653. exit(-1);
  654. }
  655. addr.nl_family = AF_NETLINK;
  656. addr.nl_pad = 0;
  657. addr.nl_pid = 0;
  658. addr.nl_groups = CN_KVP_IDX;
  659. error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  660. if (error < 0) {
  661. syslog(LOG_ERR, "bind failed; error:%d", error);
  662. close(fd);
  663. exit(-1);
  664. }
  665. sock_opt = addr.nl_groups;
  666. setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt));
  667. /*
  668. * Register ourselves with the kernel.
  669. */
  670. message = (struct cn_msg *)kvp_send_buffer;
  671. message->id.idx = CN_KVP_IDX;
  672. message->id.val = CN_KVP_VAL;
  673. hv_msg = (struct hv_kvp_msg *)message->data;
  674. hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
  675. message->ack = 0;
  676. message->len = sizeof(struct hv_kvp_msg);
  677. len = netlink_send(fd, message);
  678. if (len < 0) {
  679. syslog(LOG_ERR, "netlink_send failed; error:%d", len);
  680. close(fd);
  681. exit(-1);
  682. }
  683. pfd.fd = fd;
  684. while (1) {
  685. struct sockaddr *addr_p = (struct sockaddr *) &addr;
  686. socklen_t addr_l = sizeof(addr);
  687. pfd.events = POLLIN;
  688. pfd.revents = 0;
  689. poll(&pfd, 1, -1);
  690. len = recvfrom(fd, kvp_recv_buffer, sizeof(kvp_recv_buffer), 0,
  691. addr_p, &addr_l);
  692. if (len < 0 || addr.nl_pid) {
  693. syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
  694. addr.nl_pid, errno, strerror(errno));
  695. close(fd);
  696. return -1;
  697. }
  698. incoming_msg = (struct nlmsghdr *)kvp_recv_buffer;
  699. incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
  700. hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
  701. /*
  702. * We will use the KVP header information to pass back
  703. * the error from this daemon. So, first copy the state
  704. * and set the error code to success.
  705. */
  706. op = hv_msg->kvp_hdr.operation;
  707. pool = hv_msg->kvp_hdr.pool;
  708. hv_msg->error = HV_S_OK;
  709. if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
  710. /*
  711. * Driver is registering with us; stash away the version
  712. * information.
  713. */
  714. in_hand_shake = 0;
  715. p = (char *)hv_msg->body.kvp_register.version;
  716. lic_version = malloc(strlen(p) + 1);
  717. if (lic_version) {
  718. strcpy(lic_version, p);
  719. syslog(LOG_INFO, "KVP LIC Version: %s",
  720. lic_version);
  721. } else {
  722. syslog(LOG_ERR, "malloc failed");
  723. }
  724. continue;
  725. }
  726. switch (op) {
  727. case KVP_OP_SET:
  728. if (kvp_key_add_or_modify(pool,
  729. hv_msg->body.kvp_set.data.key,
  730. hv_msg->body.kvp_set.data.key_size,
  731. hv_msg->body.kvp_set.data.value,
  732. hv_msg->body.kvp_set.data.value_size))
  733. hv_msg->error = HV_S_CONT;
  734. break;
  735. case KVP_OP_GET:
  736. if (kvp_get_value(pool,
  737. hv_msg->body.kvp_set.data.key,
  738. hv_msg->body.kvp_set.data.key_size,
  739. hv_msg->body.kvp_set.data.value,
  740. hv_msg->body.kvp_set.data.value_size))
  741. hv_msg->error = HV_S_CONT;
  742. break;
  743. case KVP_OP_DELETE:
  744. if (kvp_key_delete(pool,
  745. hv_msg->body.kvp_delete.key,
  746. hv_msg->body.kvp_delete.key_size))
  747. hv_msg->error = HV_S_CONT;
  748. break;
  749. default:
  750. break;
  751. }
  752. if (op != KVP_OP_ENUMERATE)
  753. goto kvp_done;
  754. /*
  755. * If the pool is KVP_POOL_AUTO, dynamically generate
  756. * both the key and the value; if not read from the
  757. * appropriate pool.
  758. */
  759. if (pool != KVP_POOL_AUTO) {
  760. if (kvp_pool_enumerate(pool,
  761. hv_msg->body.kvp_enum_data.index,
  762. hv_msg->body.kvp_enum_data.data.key,
  763. HV_KVP_EXCHANGE_MAX_KEY_SIZE,
  764. hv_msg->body.kvp_enum_data.data.value,
  765. HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
  766. hv_msg->error = HV_S_CONT;
  767. goto kvp_done;
  768. }
  769. hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
  770. key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
  771. key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
  772. switch (hv_msg->body.kvp_enum_data.index) {
  773. case FullyQualifiedDomainName:
  774. kvp_get_domain_name(key_value,
  775. HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
  776. strcpy(key_name, "FullyQualifiedDomainName");
  777. break;
  778. case IntegrationServicesVersion:
  779. strcpy(key_name, "IntegrationServicesVersion");
  780. strcpy(key_value, lic_version);
  781. break;
  782. case NetworkAddressIPv4:
  783. kvp_get_ip_address(AF_INET, NULL, KVP_OP_ENUMERATE,
  784. key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
  785. strcpy(key_name, "NetworkAddressIPv4");
  786. break;
  787. case NetworkAddressIPv6:
  788. kvp_get_ip_address(AF_INET6, NULL, KVP_OP_ENUMERATE,
  789. key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
  790. strcpy(key_name, "NetworkAddressIPv6");
  791. break;
  792. case OSBuildNumber:
  793. strcpy(key_value, os_build);
  794. strcpy(key_name, "OSBuildNumber");
  795. break;
  796. case OSName:
  797. strcpy(key_value, os_name);
  798. strcpy(key_name, "OSName");
  799. break;
  800. case OSMajorVersion:
  801. strcpy(key_value, os_major);
  802. strcpy(key_name, "OSMajorVersion");
  803. break;
  804. case OSMinorVersion:
  805. strcpy(key_value, os_minor);
  806. strcpy(key_name, "OSMinorVersion");
  807. break;
  808. case OSVersion:
  809. strcpy(key_value, os_build);
  810. strcpy(key_name, "OSVersion");
  811. break;
  812. case ProcessorArchitecture:
  813. strcpy(key_value, processor_arch);
  814. strcpy(key_name, "ProcessorArchitecture");
  815. break;
  816. default:
  817. hv_msg->error = HV_S_CONT;
  818. break;
  819. }
  820. /*
  821. * Send the value back to the kernel. The response is
  822. * already in the receive buffer. Update the cn_msg header to
  823. * reflect the key value that has been added to the message
  824. */
  825. kvp_done:
  826. incoming_cn_msg->id.idx = CN_KVP_IDX;
  827. incoming_cn_msg->id.val = CN_KVP_VAL;
  828. incoming_cn_msg->ack = 0;
  829. incoming_cn_msg->len = sizeof(struct hv_kvp_msg);
  830. len = netlink_send(fd, incoming_cn_msg);
  831. if (len < 0) {
  832. syslog(LOG_ERR, "net_link send failed; error:%d", len);
  833. exit(-1);
  834. }
  835. }
  836. }