hv_vss_daemon.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * An implementation of the host initiated guest snapshot for Hyper-V.
  3. *
  4. *
  5. * Copyright (C) 2013, Microsoft, Inc.
  6. * Author : K. Y. Srinivasan <kys@microsoft.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. */
  19. #include <sys/types.h>
  20. #include <sys/socket.h>
  21. #include <sys/poll.h>
  22. #include <linux/types.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <errno.h>
  29. #include <arpa/inet.h>
  30. #include <linux/connector.h>
  31. #include <linux/hyperv.h>
  32. #include <linux/netlink.h>
  33. #include <syslog.h>
  34. static char vss_recv_buffer[4096];
  35. static char vss_send_buffer[4096];
  36. static struct sockaddr_nl addr;
  37. #ifndef SOL_NETLINK
  38. #define SOL_NETLINK 270
  39. #endif
  40. static int vss_operate(int operation)
  41. {
  42. char *fs_op;
  43. char cmd[512];
  44. char buf[512];
  45. FILE *file;
  46. char *p;
  47. char *x;
  48. int error = 0;
  49. switch (operation) {
  50. case VSS_OP_FREEZE:
  51. fs_op = "-f ";
  52. break;
  53. case VSS_OP_THAW:
  54. fs_op = "-u ";
  55. break;
  56. default:
  57. return -1;
  58. }
  59. file = popen("mount | awk '/^\\/dev\\// { print $3}'", "r");
  60. if (file == NULL)
  61. return -1;
  62. while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
  63. x = strchr(p, '\n');
  64. *x = '\0';
  65. if (!strncmp(p, "/", sizeof("/")))
  66. continue;
  67. sprintf(cmd, "%s %s %s", "fsfreeze ", fs_op, p);
  68. syslog(LOG_INFO, "VSS cmd is %s\n", cmd);
  69. error = system(cmd);
  70. }
  71. pclose(file);
  72. sprintf(cmd, "%s %s %s", "fsfreeze ", fs_op, "/");
  73. syslog(LOG_INFO, "VSS cmd is %s\n", cmd);
  74. error = system(cmd);
  75. return error;
  76. }
  77. static int netlink_send(int fd, struct cn_msg *msg)
  78. {
  79. struct nlmsghdr *nlh;
  80. unsigned int size;
  81. struct msghdr message;
  82. char buffer[64];
  83. struct iovec iov[2];
  84. size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
  85. nlh = (struct nlmsghdr *)buffer;
  86. nlh->nlmsg_seq = 0;
  87. nlh->nlmsg_pid = getpid();
  88. nlh->nlmsg_type = NLMSG_DONE;
  89. nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
  90. nlh->nlmsg_flags = 0;
  91. iov[0].iov_base = nlh;
  92. iov[0].iov_len = sizeof(*nlh);
  93. iov[1].iov_base = msg;
  94. iov[1].iov_len = size;
  95. memset(&message, 0, sizeof(message));
  96. message.msg_name = &addr;
  97. message.msg_namelen = sizeof(addr);
  98. message.msg_iov = iov;
  99. message.msg_iovlen = 2;
  100. return sendmsg(fd, &message, 0);
  101. }
  102. int main(void)
  103. {
  104. int fd, len, nl_group;
  105. int error;
  106. struct cn_msg *message;
  107. struct pollfd pfd;
  108. struct nlmsghdr *incoming_msg;
  109. struct cn_msg *incoming_cn_msg;
  110. int op;
  111. struct hv_vss_msg *vss_msg;
  112. if (daemon(1, 0))
  113. return 1;
  114. openlog("Hyper-V VSS", 0, LOG_USER);
  115. syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
  116. fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
  117. if (fd < 0) {
  118. syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
  119. exit(EXIT_FAILURE);
  120. }
  121. addr.nl_family = AF_NETLINK;
  122. addr.nl_pad = 0;
  123. addr.nl_pid = 0;
  124. addr.nl_groups = 0;
  125. error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  126. if (error < 0) {
  127. syslog(LOG_ERR, "bind failed; error:%d", error);
  128. close(fd);
  129. exit(EXIT_FAILURE);
  130. }
  131. nl_group = CN_VSS_IDX;
  132. setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &nl_group, sizeof(nl_group));
  133. /*
  134. * Register ourselves with the kernel.
  135. */
  136. message = (struct cn_msg *)vss_send_buffer;
  137. message->id.idx = CN_VSS_IDX;
  138. message->id.val = CN_VSS_VAL;
  139. message->ack = 0;
  140. vss_msg = (struct hv_vss_msg *)message->data;
  141. vss_msg->vss_hdr.operation = VSS_OP_REGISTER;
  142. message->len = sizeof(struct hv_vss_msg);
  143. len = netlink_send(fd, message);
  144. if (len < 0) {
  145. syslog(LOG_ERR, "netlink_send failed; error:%d", len);
  146. close(fd);
  147. exit(EXIT_FAILURE);
  148. }
  149. pfd.fd = fd;
  150. while (1) {
  151. struct sockaddr *addr_p = (struct sockaddr *) &addr;
  152. socklen_t addr_l = sizeof(addr);
  153. pfd.events = POLLIN;
  154. pfd.revents = 0;
  155. poll(&pfd, 1, -1);
  156. len = recvfrom(fd, vss_recv_buffer, sizeof(vss_recv_buffer), 0,
  157. addr_p, &addr_l);
  158. if (len < 0) {
  159. syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
  160. addr.nl_pid, errno, strerror(errno));
  161. close(fd);
  162. return -1;
  163. }
  164. if (addr.nl_pid) {
  165. syslog(LOG_WARNING,
  166. "Received packet from untrusted pid:%u",
  167. addr.nl_pid);
  168. continue;
  169. }
  170. incoming_msg = (struct nlmsghdr *)vss_recv_buffer;
  171. if (incoming_msg->nlmsg_type != NLMSG_DONE)
  172. continue;
  173. incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
  174. vss_msg = (struct hv_vss_msg *)incoming_cn_msg->data;
  175. op = vss_msg->vss_hdr.operation;
  176. error = HV_S_OK;
  177. switch (op) {
  178. case VSS_OP_FREEZE:
  179. case VSS_OP_THAW:
  180. error = vss_operate(op);
  181. if (error)
  182. error = HV_E_FAIL;
  183. break;
  184. default:
  185. syslog(LOG_ERR, "Illegal op:%d\n", op);
  186. }
  187. vss_msg->error = error;
  188. len = netlink_send(fd, incoming_cn_msg);
  189. if (len < 0) {
  190. syslog(LOG_ERR, "net_link send failed; error:%d", len);
  191. exit(EXIT_FAILURE);
  192. }
  193. }
  194. }