hv_vss_daemon.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. #include <getopt.h>
  38. static struct sockaddr_nl addr;
  39. #ifndef SOL_NETLINK
  40. #define SOL_NETLINK 270
  41. #endif
  42. /* Don't use syslog() in the function since that can cause write to disk */
  43. static int vss_do_freeze(char *dir, unsigned int cmd)
  44. {
  45. int ret, fd = open(dir, O_RDONLY);
  46. if (fd < 0)
  47. return 1;
  48. ret = ioctl(fd, cmd, 0);
  49. /*
  50. * If a partition is mounted more than once, only the first
  51. * FREEZE/THAW can succeed and the later ones will get
  52. * EBUSY/EINVAL respectively: there could be 2 cases:
  53. * 1) a user may mount the same partition to differnt directories
  54. * by mistake or on purpose;
  55. * 2) The subvolume of btrfs appears to have the same partition
  56. * mounted more than once.
  57. */
  58. if (ret) {
  59. if ((cmd == FIFREEZE && errno == EBUSY) ||
  60. (cmd == FITHAW && errno == EINVAL)) {
  61. close(fd);
  62. return 0;
  63. }
  64. }
  65. close(fd);
  66. return !!ret;
  67. }
  68. static int vss_operate(int operation)
  69. {
  70. char match[] = "/dev/";
  71. FILE *mounts;
  72. struct mntent *ent;
  73. char errdir[1024] = {0};
  74. unsigned int cmd;
  75. int error = 0, root_seen = 0, save_errno = 0;
  76. switch (operation) {
  77. case VSS_OP_FREEZE:
  78. cmd = FIFREEZE;
  79. break;
  80. case VSS_OP_THAW:
  81. cmd = FITHAW;
  82. break;
  83. default:
  84. return -1;
  85. }
  86. mounts = setmntent("/proc/mounts", "r");
  87. if (mounts == NULL)
  88. return -1;
  89. while ((ent = getmntent(mounts))) {
  90. if (strncmp(ent->mnt_fsname, match, strlen(match)))
  91. continue;
  92. if (hasmntopt(ent, MNTOPT_RO) != NULL)
  93. continue;
  94. if (strcmp(ent->mnt_type, "vfat") == 0)
  95. continue;
  96. if (strcmp(ent->mnt_dir, "/") == 0) {
  97. root_seen = 1;
  98. continue;
  99. }
  100. error |= vss_do_freeze(ent->mnt_dir, cmd);
  101. if (error && operation == VSS_OP_FREEZE)
  102. goto err;
  103. }
  104. endmntent(mounts);
  105. if (root_seen) {
  106. error |= vss_do_freeze("/", cmd);
  107. if (error && operation == VSS_OP_FREEZE)
  108. goto err;
  109. }
  110. goto out;
  111. err:
  112. save_errno = errno;
  113. if (ent) {
  114. strncpy(errdir, ent->mnt_dir, sizeof(errdir)-1);
  115. endmntent(mounts);
  116. }
  117. vss_operate(VSS_OP_THAW);
  118. /* Call syslog after we thaw all filesystems */
  119. if (ent)
  120. syslog(LOG_ERR, "FREEZE of %s failed; error:%d %s",
  121. errdir, save_errno, strerror(save_errno));
  122. else
  123. syslog(LOG_ERR, "FREEZE of / failed; error:%d %s", save_errno,
  124. strerror(save_errno));
  125. out:
  126. return error;
  127. }
  128. static int netlink_send(int fd, struct cn_msg *msg)
  129. {
  130. struct nlmsghdr nlh = { .nlmsg_type = NLMSG_DONE };
  131. unsigned int size;
  132. struct msghdr message;
  133. struct iovec iov[2];
  134. size = sizeof(struct cn_msg) + msg->len;
  135. nlh.nlmsg_pid = getpid();
  136. nlh.nlmsg_len = NLMSG_LENGTH(size);
  137. iov[0].iov_base = &nlh;
  138. iov[0].iov_len = sizeof(nlh);
  139. iov[1].iov_base = msg;
  140. iov[1].iov_len = size;
  141. memset(&message, 0, sizeof(message));
  142. message.msg_name = &addr;
  143. message.msg_namelen = sizeof(addr);
  144. message.msg_iov = iov;
  145. message.msg_iovlen = 2;
  146. return sendmsg(fd, &message, 0);
  147. }
  148. void print_usage(char *argv[])
  149. {
  150. fprintf(stderr, "Usage: %s [options]\n"
  151. "Options are:\n"
  152. " -n, --no-daemon stay in foreground, don't daemonize\n"
  153. " -h, --help print this help\n", argv[0]);
  154. }
  155. int main(int argc, char *argv[])
  156. {
  157. int fd, len, nl_group;
  158. int error;
  159. struct cn_msg *message;
  160. struct pollfd pfd;
  161. struct nlmsghdr *incoming_msg;
  162. struct cn_msg *incoming_cn_msg;
  163. int op;
  164. struct hv_vss_msg *vss_msg;
  165. char *vss_recv_buffer;
  166. size_t vss_recv_buffer_len;
  167. int daemonize = 1, long_index = 0, opt;
  168. static struct option long_options[] = {
  169. {"help", no_argument, 0, 'h' },
  170. {"no-daemon", no_argument, 0, 'n' },
  171. {0, 0, 0, 0 }
  172. };
  173. while ((opt = getopt_long(argc, argv, "hn", long_options,
  174. &long_index)) != -1) {
  175. switch (opt) {
  176. case 'n':
  177. daemonize = 0;
  178. break;
  179. case 'h':
  180. default:
  181. print_usage(argv);
  182. exit(EXIT_FAILURE);
  183. }
  184. }
  185. if (daemonize && daemon(1, 0))
  186. return 1;
  187. openlog("Hyper-V VSS", 0, LOG_USER);
  188. syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
  189. vss_recv_buffer_len = NLMSG_LENGTH(0) + sizeof(struct cn_msg) + sizeof(struct hv_vss_msg);
  190. vss_recv_buffer = calloc(1, vss_recv_buffer_len);
  191. if (!vss_recv_buffer) {
  192. syslog(LOG_ERR, "Failed to allocate netlink buffers");
  193. exit(EXIT_FAILURE);
  194. }
  195. fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
  196. if (fd < 0) {
  197. syslog(LOG_ERR, "netlink socket creation failed; error:%d %s",
  198. errno, strerror(errno));
  199. exit(EXIT_FAILURE);
  200. }
  201. addr.nl_family = AF_NETLINK;
  202. addr.nl_pad = 0;
  203. addr.nl_pid = 0;
  204. addr.nl_groups = 0;
  205. error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  206. if (error < 0) {
  207. syslog(LOG_ERR, "bind failed; error:%d %s", errno, strerror(errno));
  208. close(fd);
  209. exit(EXIT_FAILURE);
  210. }
  211. nl_group = CN_VSS_IDX;
  212. if (setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &nl_group, sizeof(nl_group)) < 0) {
  213. syslog(LOG_ERR, "setsockopt failed; error:%d %s", errno, strerror(errno));
  214. close(fd);
  215. exit(EXIT_FAILURE);
  216. }
  217. /*
  218. * Register ourselves with the kernel.
  219. */
  220. message = (struct cn_msg *)vss_recv_buffer;
  221. message->id.idx = CN_VSS_IDX;
  222. message->id.val = CN_VSS_VAL;
  223. message->ack = 0;
  224. vss_msg = (struct hv_vss_msg *)message->data;
  225. vss_msg->vss_hdr.operation = VSS_OP_REGISTER;
  226. message->len = sizeof(struct hv_vss_msg);
  227. len = netlink_send(fd, message);
  228. if (len < 0) {
  229. syslog(LOG_ERR, "netlink_send failed; error:%d %s", errno, strerror(errno));
  230. close(fd);
  231. exit(EXIT_FAILURE);
  232. }
  233. pfd.fd = fd;
  234. while (1) {
  235. struct sockaddr *addr_p = (struct sockaddr *) &addr;
  236. socklen_t addr_l = sizeof(addr);
  237. pfd.events = POLLIN;
  238. pfd.revents = 0;
  239. if (poll(&pfd, 1, -1) < 0) {
  240. syslog(LOG_ERR, "poll failed; error:%d %s", errno, strerror(errno));
  241. if (errno == EINVAL) {
  242. close(fd);
  243. exit(EXIT_FAILURE);
  244. }
  245. else
  246. continue;
  247. }
  248. len = recvfrom(fd, vss_recv_buffer, vss_recv_buffer_len, 0,
  249. addr_p, &addr_l);
  250. if (len < 0) {
  251. syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
  252. addr.nl_pid, errno, strerror(errno));
  253. close(fd);
  254. return -1;
  255. }
  256. if (addr.nl_pid) {
  257. syslog(LOG_WARNING,
  258. "Received packet from untrusted pid:%u",
  259. addr.nl_pid);
  260. continue;
  261. }
  262. incoming_msg = (struct nlmsghdr *)vss_recv_buffer;
  263. if (incoming_msg->nlmsg_type != NLMSG_DONE)
  264. continue;
  265. incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
  266. vss_msg = (struct hv_vss_msg *)incoming_cn_msg->data;
  267. op = vss_msg->vss_hdr.operation;
  268. error = HV_S_OK;
  269. switch (op) {
  270. case VSS_OP_FREEZE:
  271. case VSS_OP_THAW:
  272. error = vss_operate(op);
  273. syslog(LOG_INFO, "VSS: op=%s: %s\n",
  274. op == VSS_OP_FREEZE ? "FREEZE" : "THAW",
  275. error ? "failed" : "succeeded");
  276. if (error) {
  277. error = HV_E_FAIL;
  278. syslog(LOG_ERR, "op=%d failed!", op);
  279. syslog(LOG_ERR, "report it with these files:");
  280. syslog(LOG_ERR, "/etc/fstab and /proc/mounts");
  281. }
  282. break;
  283. default:
  284. syslog(LOG_ERR, "Illegal op:%d\n", op);
  285. }
  286. vss_msg->error = error;
  287. len = netlink_send(fd, incoming_cn_msg);
  288. if (len < 0) {
  289. syslog(LOG_ERR, "net_link send failed; error:%d %s",
  290. errno, strerror(errno));
  291. exit(EXIT_FAILURE);
  292. }
  293. }
  294. }