hv_vss_daemon.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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;
  95. unsigned int size;
  96. struct msghdr message;
  97. char buffer[64];
  98. struct iovec iov[2];
  99. size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
  100. nlh = (struct nlmsghdr *)buffer;
  101. nlh->nlmsg_seq = 0;
  102. nlh->nlmsg_pid = getpid();
  103. nlh->nlmsg_type = NLMSG_DONE;
  104. nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
  105. nlh->nlmsg_flags = 0;
  106. iov[0].iov_base = nlh;
  107. iov[0].iov_len = sizeof(*nlh);
  108. iov[1].iov_base = msg;
  109. iov[1].iov_len = size;
  110. memset(&message, 0, sizeof(message));
  111. message.msg_name = &addr;
  112. message.msg_namelen = sizeof(addr);
  113. message.msg_iov = iov;
  114. message.msg_iovlen = 2;
  115. return sendmsg(fd, &message, 0);
  116. }
  117. int main(void)
  118. {
  119. int fd, len, nl_group;
  120. int error;
  121. struct cn_msg *message;
  122. struct pollfd pfd;
  123. struct nlmsghdr *incoming_msg;
  124. struct cn_msg *incoming_cn_msg;
  125. int op;
  126. struct hv_vss_msg *vss_msg;
  127. char *vss_send_buffer;
  128. char *vss_recv_buffer;
  129. size_t vss_recv_buffer_len;
  130. if (daemon(1, 0))
  131. return 1;
  132. openlog("Hyper-V VSS", 0, LOG_USER);
  133. syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
  134. vss_recv_buffer_len = NLMSG_HDRLEN + sizeof(struct cn_msg) + sizeof(struct hv_vss_msg);
  135. vss_send_buffer = calloc(1, vss_recv_buffer_len);
  136. vss_recv_buffer = calloc(1, vss_recv_buffer_len);
  137. if (!(vss_send_buffer && vss_recv_buffer)) {
  138. syslog(LOG_ERR, "Failed to allocate netlink buffers");
  139. exit(EXIT_FAILURE);
  140. }
  141. fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
  142. if (fd < 0) {
  143. syslog(LOG_ERR, "netlink socket creation failed; error:%d %s",
  144. errno, strerror(errno));
  145. exit(EXIT_FAILURE);
  146. }
  147. addr.nl_family = AF_NETLINK;
  148. addr.nl_pad = 0;
  149. addr.nl_pid = 0;
  150. addr.nl_groups = 0;
  151. error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  152. if (error < 0) {
  153. syslog(LOG_ERR, "bind failed; error:%d %s", errno, strerror(errno));
  154. close(fd);
  155. exit(EXIT_FAILURE);
  156. }
  157. nl_group = CN_VSS_IDX;
  158. if (setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &nl_group, sizeof(nl_group)) < 0) {
  159. syslog(LOG_ERR, "setsockopt failed; error:%d %s", errno, strerror(errno));
  160. close(fd);
  161. exit(EXIT_FAILURE);
  162. }
  163. /*
  164. * Register ourselves with the kernel.
  165. */
  166. message = (struct cn_msg *)vss_send_buffer;
  167. message->id.idx = CN_VSS_IDX;
  168. message->id.val = CN_VSS_VAL;
  169. message->ack = 0;
  170. vss_msg = (struct hv_vss_msg *)message->data;
  171. vss_msg->vss_hdr.operation = VSS_OP_REGISTER;
  172. message->len = sizeof(struct hv_vss_msg);
  173. len = netlink_send(fd, message);
  174. if (len < 0) {
  175. syslog(LOG_ERR, "netlink_send failed; error:%d %s", errno, strerror(errno));
  176. close(fd);
  177. exit(EXIT_FAILURE);
  178. }
  179. pfd.fd = fd;
  180. while (1) {
  181. struct sockaddr *addr_p = (struct sockaddr *) &addr;
  182. socklen_t addr_l = sizeof(addr);
  183. pfd.events = POLLIN;
  184. pfd.revents = 0;
  185. if (poll(&pfd, 1, -1) < 0) {
  186. syslog(LOG_ERR, "poll failed; error:%d %s", errno, strerror(errno));
  187. if (errno == EINVAL) {
  188. close(fd);
  189. exit(EXIT_FAILURE);
  190. }
  191. else
  192. continue;
  193. }
  194. len = recvfrom(fd, vss_recv_buffer, vss_recv_buffer_len, 0,
  195. addr_p, &addr_l);
  196. if (len < 0) {
  197. syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
  198. addr.nl_pid, errno, strerror(errno));
  199. close(fd);
  200. return -1;
  201. }
  202. if (addr.nl_pid) {
  203. syslog(LOG_WARNING,
  204. "Received packet from untrusted pid:%u",
  205. addr.nl_pid);
  206. continue;
  207. }
  208. incoming_msg = (struct nlmsghdr *)vss_recv_buffer;
  209. if (incoming_msg->nlmsg_type != NLMSG_DONE)
  210. continue;
  211. incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
  212. vss_msg = (struct hv_vss_msg *)incoming_cn_msg->data;
  213. op = vss_msg->vss_hdr.operation;
  214. error = HV_S_OK;
  215. switch (op) {
  216. case VSS_OP_FREEZE:
  217. case VSS_OP_THAW:
  218. error = vss_operate(op);
  219. if (error)
  220. error = HV_E_FAIL;
  221. break;
  222. default:
  223. syslog(LOG_ERR, "Illegal op:%d\n", op);
  224. }
  225. vss_msg->error = error;
  226. len = netlink_send(fd, incoming_cn_msg);
  227. if (len < 0) {
  228. syslog(LOG_ERR, "net_link send failed; error:%d %s",
  229. errno, strerror(errno));
  230. exit(EXIT_FAILURE);
  231. }
  232. }
  233. }