blob: 921c1bec030500fff56c65905c164b8d132448ed [file] [log] [blame]
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -07001/*
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
20
21#include <sys/types.h>
22#include <sys/socket.h>
23#include <sys/poll.h>
24#include <linux/types.h>
25#include <stdio.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/connector.h>
33#include <linux/hyperv.h>
34#include <linux/netlink.h>
35#include <syslog.h>
36
37static char vss_recv_buffer[4096];
38static char vss_send_buffer[4096];
39static struct sockaddr_nl addr;
40
41#ifndef SOL_NETLINK
42#define SOL_NETLINK 270
43#endif
44
45
46static int vss_operate(int operation)
47{
48 char *fs_op;
49 char cmd[512];
50 char buf[512];
51 FILE *file;
52 char *p;
53 char *x;
Olaf Heringeb8905b2013-04-24 07:48:48 -070054 int error = 0;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070055
56 switch (operation) {
57 case VSS_OP_FREEZE:
58 fs_op = "-f ";
59 break;
60 case VSS_OP_THAW:
61 fs_op = "-u ";
62 break;
Olaf Heringeb8905b2013-04-24 07:48:48 -070063 default:
64 return -1;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070065 }
66
Olaf Heringeb8905b2013-04-24 07:48:48 -070067 file = popen("mount | awk '/^\\/dev\\// { print $3}'", "r");
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070068 if (file == NULL)
Olaf Heringeb8905b2013-04-24 07:48:48 -070069 return -1;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070070
71 while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
72 x = strchr(p, '\n');
73 *x = '\0';
74 if (!strncmp(p, "/", sizeof("/")))
75 continue;
76
77 sprintf(cmd, "%s %s %s", "fsfreeze ", fs_op, p);
78 syslog(LOG_INFO, "VSS cmd is %s\n", cmd);
79 error = system(cmd);
80 }
81 pclose(file);
82
83 sprintf(cmd, "%s %s %s", "fsfreeze ", fs_op, "/");
84 syslog(LOG_INFO, "VSS cmd is %s\n", cmd);
85 error = system(cmd);
86
87 return error;
88}
89
90static int netlink_send(int fd, struct cn_msg *msg)
91{
92 struct nlmsghdr *nlh;
93 unsigned int size;
94 struct msghdr message;
95 char buffer[64];
96 struct iovec iov[2];
97
98 size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
99
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
107 iov[0].iov_base = nlh;
108 iov[0].iov_len = sizeof(*nlh);
109
110 iov[1].iov_base = msg;
111 iov[1].iov_len = size;
112
113 memset(&message, 0, sizeof(message));
114 message.msg_name = &addr;
115 message.msg_namelen = sizeof(addr);
116 message.msg_iov = iov;
117 message.msg_iovlen = 2;
118
119 return sendmsg(fd, &message, 0);
120}
121
122int main(void)
123{
124 int fd, len, nl_group;
125 int error;
126 struct cn_msg *message;
127 struct pollfd pfd;
128 struct nlmsghdr *incoming_msg;
129 struct cn_msg *incoming_cn_msg;
130 int op;
131 struct hv_vss_msg *vss_msg;
132
Olaf Heringeb8905b2013-04-24 07:48:48 -0700133 if (daemon(1, 0))
134 return 1;
135
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700136 openlog("Hyper-V VSS", 0, LOG_USER);
137 syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
138
139 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
140 if (fd < 0) {
141 syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
142 exit(EXIT_FAILURE);
143 }
144 addr.nl_family = AF_NETLINK;
145 addr.nl_pad = 0;
146 addr.nl_pid = 0;
147 addr.nl_groups = 0;
148
149
150 error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
151 if (error < 0) {
152 syslog(LOG_ERR, "bind failed; error:%d", error);
153 close(fd);
154 exit(EXIT_FAILURE);
155 }
156 nl_group = CN_VSS_IDX;
157 setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &nl_group, sizeof(nl_group));
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
168 message->len = sizeof(struct hv_vss_msg);
169
170 len = netlink_send(fd, message);
171 if (len < 0) {
172 syslog(LOG_ERR, "netlink_send failed; error:%d", len);
173 close(fd);
174 exit(EXIT_FAILURE);
175 }
176
177 pfd.fd = fd;
178
179 while (1) {
180 struct sockaddr *addr_p = (struct sockaddr *) &addr;
181 socklen_t addr_l = sizeof(addr);
182 pfd.events = POLLIN;
183 pfd.revents = 0;
184 poll(&pfd, 1, -1);
185
186 len = recvfrom(fd, vss_recv_buffer, sizeof(vss_recv_buffer), 0,
187 addr_p, &addr_l);
188
Olaf Hering5edf5ee2013-04-24 07:48:49 -0700189 if (len < 0) {
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700190 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
Olaf Hering5edf5ee2013-04-24 07:48:49 -0700196 if (addr.nl_pid) {
K. Y. Srinivasan038336a2013-04-24 07:48:50 -0700197 syslog(LOG_WARNING,
198 "Received packet from untrusted pid:%u",
199 addr.nl_pid);
Olaf Hering5edf5ee2013-04-24 07:48:49 -0700200 continue;
201 }
202
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700203 incoming_msg = (struct nlmsghdr *)vss_recv_buffer;
204
205 if (incoming_msg->nlmsg_type != NLMSG_DONE)
206 continue;
207
208 incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
209 vss_msg = (struct hv_vss_msg *)incoming_cn_msg->data;
210 op = vss_msg->vss_hdr.operation;
211 error = HV_S_OK;
212
213 switch (op) {
214 case VSS_OP_FREEZE:
215 case VSS_OP_THAW:
216 error = vss_operate(op);
217 if (error)
218 error = HV_E_FAIL;
219 break;
220 default:
221 syslog(LOG_ERR, "Illegal op:%d\n", op);
222 }
223 vss_msg->error = error;
224 len = netlink_send(fd, incoming_cn_msg);
225 if (len < 0) {
226 syslog(LOG_ERR, "net_link send failed; error:%d", len);
227 exit(EXIT_FAILURE);
228 }
229 }
230
231}