hv_vss_daemon.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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_recv_buffer;
  123. size_t vss_recv_buffer_len;
  124. if (daemon(1, 0))
  125. return 1;
  126. openlog("Hyper-V VSS", 0, LOG_USER);
  127. syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
  128. vss_recv_buffer_len = NLMSG_LENGTH(0) + sizeof(struct cn_msg) + sizeof(struct hv_vss_msg);
  129. vss_recv_buffer = calloc(1, vss_recv_buffer_len);
  130. if (!vss_recv_buffer) {
  131. syslog(LOG_ERR, "Failed to allocate netlink buffers");
  132. exit(EXIT_FAILURE);
  133. }
  134. fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
  135. if (fd < 0) {
  136. syslog(LOG_ERR, "netlink socket creation failed; error:%d %s",
  137. errno, strerror(errno));
  138. exit(EXIT_FAILURE);
  139. }
  140. addr.nl_family = AF_NETLINK;
  141. addr.nl_pad = 0;
  142. addr.nl_pid = 0;
  143. addr.nl_groups = 0;
  144. error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  145. if (error < 0) {
  146. syslog(LOG_ERR, "bind failed; error:%d %s", errno, strerror(errno));
  147. close(fd);
  148. exit(EXIT_FAILURE);
  149. }
  150. nl_group = CN_VSS_IDX;
  151. if (setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &nl_group, sizeof(nl_group)) < 0) {
  152. syslog(LOG_ERR, "setsockopt failed; error:%d %s", errno, strerror(errno));
  153. close(fd);
  154. exit(EXIT_FAILURE);
  155. }
  156. /*
  157. * Register ourselves with the kernel.
  158. */
  159. message = (struct cn_msg *)vss_recv_buffer;
  160. message->id.idx = CN_VSS_IDX;
  161. message->id.val = CN_VSS_VAL;
  162. message->ack = 0;
  163. vss_msg = (struct hv_vss_msg *)message->data;
  164. vss_msg->vss_hdr.operation = VSS_OP_REGISTER;
  165. message->len = sizeof(struct hv_vss_msg);
  166. len = netlink_send(fd, message);
  167. if (len < 0) {
  168. syslog(LOG_ERR, "netlink_send failed; error:%d %s", errno, strerror(errno));
  169. close(fd);
  170. exit(EXIT_FAILURE);
  171. }
  172. pfd.fd = fd;
  173. while (1) {
  174. struct sockaddr *addr_p = (struct sockaddr *) &addr;
  175. socklen_t addr_l = sizeof(addr);
  176. pfd.events = POLLIN;
  177. pfd.revents = 0;
  178. if (poll(&pfd, 1, -1) < 0) {
  179. syslog(LOG_ERR, "poll failed; error:%d %s", errno, strerror(errno));
  180. if (errno == EINVAL) {
  181. close(fd);
  182. exit(EXIT_FAILURE);
  183. }
  184. else
  185. continue;
  186. }
  187. len = recvfrom(fd, vss_recv_buffer, vss_recv_buffer_len, 0,
  188. addr_p, &addr_l);
  189. if (len < 0) {
  190. syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
  191. addr.nl_pid, errno, strerror(errno));
  192. close(fd);
  193. return -1;
  194. }
  195. if (addr.nl_pid) {
  196. syslog(LOG_WARNING,
  197. "Received packet from untrusted pid:%u",
  198. addr.nl_pid);
  199. continue;
  200. }
  201. incoming_msg = (struct nlmsghdr *)vss_recv_buffer;
  202. if (incoming_msg->nlmsg_type != NLMSG_DONE)
  203. continue;
  204. incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
  205. vss_msg = (struct hv_vss_msg *)incoming_cn_msg->data;
  206. op = vss_msg->vss_hdr.operation;
  207. error = HV_S_OK;
  208. switch (op) {
  209. case VSS_OP_FREEZE:
  210. case VSS_OP_THAW:
  211. error = vss_operate(op);
  212. if (error)
  213. error = HV_E_FAIL;
  214. break;
  215. default:
  216. syslog(LOG_ERR, "Illegal op:%d\n", op);
  217. }
  218. vss_msg->error = error;
  219. len = netlink_send(fd, incoming_cn_msg);
  220. if (len < 0) {
  221. syslog(LOG_ERR, "net_link send failed; error:%d %s",
  222. errno, strerror(errno));
  223. exit(EXIT_FAILURE);
  224. }
  225. }
  226. }