hv_vss_daemon.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <sys/ioctl.h>
  23. #include <linux/types.h>
  24. #include <fcntl.h>
  25. #include <stdio.h>
  26. #include <mntent.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31. #include <errno.h>
  32. #include <arpa/inet.h>
  33. #include <linux/fs.h>
  34. #include <linux/connector.h>
  35. #include <linux/hyperv.h>
  36. #include <linux/netlink.h>
  37. #include <syslog.h>
  38. static char vss_recv_buffer[4096];
  39. static char vss_send_buffer[4096];
  40. static struct sockaddr_nl addr;
  41. #ifndef SOL_NETLINK
  42. #define SOL_NETLINK 270
  43. #endif
  44. static int vss_do_freeze(char *dir, unsigned int cmd, char *fs_op)
  45. {
  46. int ret, fd = open(dir, O_RDONLY);
  47. if (fd < 0)
  48. return 1;
  49. ret = ioctl(fd, cmd, 0);
  50. syslog(LOG_INFO, "VSS: %s of %s: %s\n", fs_op, dir, strerror(errno));
  51. close(fd);
  52. return !!ret;
  53. }
  54. static int vss_operate(int operation)
  55. {
  56. char *fs_op;
  57. char match[] = "/dev/";
  58. FILE *mounts;
  59. struct mntent *ent;
  60. unsigned int cmd;
  61. int error = 0, root_seen = 0;
  62. switch (operation) {
  63. case VSS_OP_FREEZE:
  64. cmd = FIFREEZE;
  65. fs_op = "freeze";
  66. break;
  67. case VSS_OP_THAW:
  68. cmd = FITHAW;
  69. fs_op = "thaw";
  70. break;
  71. default:
  72. return -1;
  73. }
  74. mounts = setmntent("/proc/mounts", "r");
  75. if (mounts == NULL)
  76. return -1;
  77. while ((ent = getmntent(mounts))) {
  78. if (strncmp(ent->mnt_fsname, match, strlen(match)))
  79. continue;
  80. if (strcmp(ent->mnt_type, "iso9660") == 0)
  81. continue;
  82. if (strcmp(ent->mnt_dir, "/") == 0) {
  83. root_seen = 1;
  84. continue;
  85. }
  86. error |= vss_do_freeze(ent->mnt_dir, cmd, fs_op);
  87. }
  88. endmntent(mounts);
  89. if (root_seen) {
  90. error |= vss_do_freeze("/", cmd, fs_op);
  91. }
  92. return error;
  93. }
  94. static int netlink_send(int fd, struct cn_msg *msg)
  95. {
  96. struct nlmsghdr *nlh;
  97. unsigned int size;
  98. struct msghdr message;
  99. char buffer[64];
  100. struct iovec iov[2];
  101. size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
  102. nlh = (struct nlmsghdr *)buffer;
  103. nlh->nlmsg_seq = 0;
  104. nlh->nlmsg_pid = getpid();
  105. nlh->nlmsg_type = NLMSG_DONE;
  106. nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
  107. nlh->nlmsg_flags = 0;
  108. iov[0].iov_base = nlh;
  109. iov[0].iov_len = sizeof(*nlh);
  110. iov[1].iov_base = msg;
  111. iov[1].iov_len = size;
  112. memset(&message, 0, sizeof(message));
  113. message.msg_name = &addr;
  114. message.msg_namelen = sizeof(addr);
  115. message.msg_iov = iov;
  116. message.msg_iovlen = 2;
  117. return sendmsg(fd, &message, 0);
  118. }
  119. int main(void)
  120. {
  121. int fd, len, nl_group;
  122. int error;
  123. struct cn_msg *message;
  124. struct pollfd pfd;
  125. struct nlmsghdr *incoming_msg;
  126. struct cn_msg *incoming_cn_msg;
  127. int op;
  128. struct hv_vss_msg *vss_msg;
  129. if (daemon(1, 0))
  130. return 1;
  131. openlog("Hyper-V VSS", 0, LOG_USER);
  132. syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
  133. fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
  134. if (fd < 0) {
  135. syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
  136. exit(EXIT_FAILURE);
  137. }
  138. addr.nl_family = AF_NETLINK;
  139. addr.nl_pad = 0;
  140. addr.nl_pid = 0;
  141. addr.nl_groups = 0;
  142. error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  143. if (error < 0) {
  144. syslog(LOG_ERR, "bind failed; error:%d", error);
  145. close(fd);
  146. exit(EXIT_FAILURE);
  147. }
  148. nl_group = CN_VSS_IDX;
  149. setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &nl_group, sizeof(nl_group));
  150. /*
  151. * Register ourselves with the kernel.
  152. */
  153. message = (struct cn_msg *)vss_send_buffer;
  154. message->id.idx = CN_VSS_IDX;
  155. message->id.val = CN_VSS_VAL;
  156. message->ack = 0;
  157. vss_msg = (struct hv_vss_msg *)message->data;
  158. vss_msg->vss_hdr.operation = VSS_OP_REGISTER;
  159. message->len = sizeof(struct hv_vss_msg);
  160. len = netlink_send(fd, message);
  161. if (len < 0) {
  162. syslog(LOG_ERR, "netlink_send failed; error:%d", len);
  163. close(fd);
  164. exit(EXIT_FAILURE);
  165. }
  166. pfd.fd = fd;
  167. while (1) {
  168. struct sockaddr *addr_p = (struct sockaddr *) &addr;
  169. socklen_t addr_l = sizeof(addr);
  170. pfd.events = POLLIN;
  171. pfd.revents = 0;
  172. poll(&pfd, 1, -1);
  173. len = recvfrom(fd, vss_recv_buffer, sizeof(vss_recv_buffer), 0,
  174. addr_p, &addr_l);
  175. if (len < 0) {
  176. syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
  177. addr.nl_pid, errno, strerror(errno));
  178. close(fd);
  179. return -1;
  180. }
  181. if (addr.nl_pid) {
  182. syslog(LOG_WARNING,
  183. "Received packet from untrusted pid:%u",
  184. addr.nl_pid);
  185. continue;
  186. }
  187. incoming_msg = (struct nlmsghdr *)vss_recv_buffer;
  188. if (incoming_msg->nlmsg_type != NLMSG_DONE)
  189. continue;
  190. incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
  191. vss_msg = (struct hv_vss_msg *)incoming_cn_msg->data;
  192. op = vss_msg->vss_hdr.operation;
  193. error = HV_S_OK;
  194. switch (op) {
  195. case VSS_OP_FREEZE:
  196. case VSS_OP_THAW:
  197. error = vss_operate(op);
  198. if (error)
  199. error = HV_E_FAIL;
  200. break;
  201. default:
  202. syslog(LOG_ERR, "Illegal op:%d\n", op);
  203. }
  204. vss_msg->error = error;
  205. len = netlink_send(fd, incoming_cn_msg);
  206. if (len < 0) {
  207. syslog(LOG_ERR, "net_link send failed; error:%d", len);
  208. exit(EXIT_FAILURE);
  209. }
  210. }
  211. }