hv_vss_daemon.c 6.0 KB

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