]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/freezer.c
utils: split into {file,string}_utils.{c,h}
[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 #include "config.h"
24
25 #include <stdio.h>
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 "cgroup.h"
35 #include "commands.h"
36 #include "error.h"
37 #include "log.h"
38 #include "lxc.h"
39 #include "monitor.h"
40 #include "state.h"
41 #include "string_utils.h"
42
43 lxc_log_define(freezer, lxc);
44
45 static int do_freeze_thaw(bool freeze, const char *name, const char *lxcpath)
46 {
47 int ret;
48 char v[100];
49 struct cgroup_ops *cgroup_ops;
50 const char *state = freeze ? "FROZEN" : "THAWED";
51 size_t state_len = 6;
52 lxc_state_t new_state = freeze ? FROZEN : THAWED;
53
54 cgroup_ops = cgroup_init(NULL);
55 if (!cgroup_ops)
56 return -1;
57
58 ret = cgroup_ops->set(cgroup_ops, "freezer.state", state, name, lxcpath);
59 if (ret < 0) {
60 cgroup_exit(cgroup_ops);
61 ERROR("Failed to freeze %s", name);
62 return -1;
63 }
64
65 for (;;) {
66 ret = cgroup_ops->get(cgroup_ops, "freezer.state", v, sizeof(v), name, lxcpath);
67 if (ret < 0) {
68 cgroup_exit(cgroup_ops);
69 ERROR("Failed to get freezer state of %s", name);
70 return -1;
71 }
72
73 v[99] = '\0';
74 v[lxc_char_right_gc(v, strlen(v))] = '\0';
75
76 ret = strncmp(v, state, state_len);
77 if (ret == 0) {
78 cgroup_exit(cgroup_ops);
79 lxc_cmd_serve_state_clients(name, lxcpath, new_state);
80 lxc_monitor_send_state(name, new_state, lxcpath);
81 return 0;
82 }
83
84 sleep(1);
85 }
86 }
87
88 int lxc_freeze(const char *name, const char *lxcpath)
89 {
90 lxc_cmd_serve_state_clients(name, lxcpath, FREEZING);
91 lxc_monitor_send_state(name, FREEZING, lxcpath);
92 return do_freeze_thaw(true, name, lxcpath);
93 }
94
95 int lxc_unfreeze(const char *name, const char *lxcpath)
96 {
97 return do_freeze_thaw(false, name, lxcpath);
98 }