]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/freezer.c
licensing: Add missing headers and FSF address
[mirror_lxc.git] / src / lxc / freezer.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #undef _GNU_SOURCE
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include <sys/types.h>
32 #include <sys/param.h>
33
34 #include "error.h"
35 #include "state.h"
36 #include "monitor.h"
37
38 #include <lxc/log.h>
39 #include <lxc/cgroup.h>
40
41 lxc_log_define(lxc_freezer, lxc);
42
43 static int do_unfreeze(const char *nsgroup, int freeze, const char *name, const char *lxcpath)
44 {
45 char freezer[MAXPATHLEN], *f;
46 char tmpf[32];
47 int fd, ret;
48
49 ret = snprintf(freezer, MAXPATHLEN, "%s/freezer.state", nsgroup);
50 if (ret >= MAXPATHLEN) {
51 ERROR("freezer.state name too long");
52 return -1;
53 }
54
55 fd = open(freezer, O_RDWR);
56 if (fd < 0) {
57 SYSERROR("failed to open freezer at '%s'", nsgroup);
58 return -1;
59 }
60
61 if (freeze) {
62 f = "FROZEN";
63 ret = write(fd, f, strlen(f) + 1);
64 } else {
65 f = "THAWED";
66 ret = write(fd, f, strlen(f) + 1);
67
68 /* compatibility code with old freezer interface */
69 if (ret < 0) {
70 f = "RUNNING";
71 ret = write(fd, f, strlen(f) + 1) < 0;
72 }
73 }
74
75 if (ret < 0) {
76 SYSERROR("failed to write '%s' to '%s'", f, freezer);
77 goto out;
78 }
79
80 while (1) {
81 ret = lseek(fd, 0L, SEEK_SET);
82 if (ret < 0) {
83 SYSERROR("failed to lseek on file '%s'", freezer);
84 goto out;
85 }
86
87 ret = read(fd, tmpf, sizeof(tmpf));
88 if (ret < 0) {
89 SYSERROR("failed to read to '%s'", freezer);
90 goto out;
91 }
92
93 ret = strncmp(f, tmpf, strlen(f));
94 if (!ret)
95 {
96 if (name)
97 lxc_monitor_send_state(name, freeze ? FROZEN : THAWED, lxcpath);
98 break; /* Success */
99 }
100
101 sleep(1);
102
103 ret = lseek(fd, 0L, SEEK_SET);
104 if (ret < 0) {
105 SYSERROR("failed to lseek on file '%s'", freezer);
106 goto out;
107 }
108
109 ret = write(fd, f, strlen(f) + 1);
110 if (ret < 0) {
111 SYSERROR("failed to write '%s' to '%s'", f, freezer);
112 goto out;
113 }
114 }
115
116 out:
117 close(fd);
118 return ret;
119 }
120
121 static int freeze_unfreeze(const char *name, int freeze, const char *lxcpath)
122 {
123 char *cgabspath;
124 int ret;
125
126 cgabspath = lxc_cgroup_path_get("freezer", name, lxcpath);
127 if (!cgabspath)
128 return -1;
129
130 ret = do_unfreeze(cgabspath, freeze, name, lxcpath);
131 free(cgabspath);
132 return ret;
133 }
134
135 int lxc_freeze(const char *name, const char *lxcpath)
136 {
137 lxc_monitor_send_state(name, FREEZING, lxcpath);
138 return freeze_unfreeze(name, 1, lxcpath);
139 }
140
141 int lxc_unfreeze(const char *name, const char *lxcpath)
142 {
143 return freeze_unfreeze(name, 0, lxcpath);
144 }
145
146 int lxc_unfreeze_bypath(const char *cgrelpath)
147 {
148 char cgabspath[MAXPATHLEN];
149 int len, ret;
150
151 if (!get_subsys_mount(cgabspath, "freezer"))
152 return -1;
153 len = strlen(cgabspath);
154 ret = snprintf(cgabspath+len, MAXPATHLEN-len, "/%s", cgrelpath);
155 if (ret < 0 || ret >= MAXPATHLEN-len) {
156 ERROR("freezer path name too long");
157 return -1;
158 }
159
160 return do_unfreeze(cgabspath, 0, NULL, NULL);
161 }