hv_vss_daemon.c 5.9 KB

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