]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/freezer.c
cgroups: refactor cgroup handling
[mirror_lxc.git] / src / lxc / freezer.c
CommitLineData
0ad19a3f 1/*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
9afe19d6 7 * Daniel Lezcano <daniel.lezcano at free.fr>
0ad19a3f 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
250b1eec 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0ad19a3f 22 */
d06245b8
NC
23#include "config.h"
24
0ad19a3f 25#include <stdio.h>
0ad19a3f 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>
0ad19a3f 32#include <sys/param.h>
0ad19a3f 33
2202afc9 34#include "cgroup.h"
974a8aba 35#include "commands.h"
e2bcd7db 36#include "error.h"
f2363e38 37#include "log.h"
4fb3cba5 38#include "lxc.h"
fd5838e5
CB
39#include "monitor.h"
40#include "parse.h"
41#include "state.h"
36eb9bde
CLG
42
43lxc_log_define(lxc_freezer, lxc);
0ad19a3f 44
4fb3cba5
DE
45lxc_state_t freezer_state(const char *name, const char *lxcpath)
46{
fd5838e5 47 int ret;
4fb3cba5 48 char v[100];
2202afc9 49 struct cgroup_ops *cgroup_ops;
fd5838e5 50
2202afc9
CB
51 cgroup_ops = cgroup_init(NULL);
52 if (!cgroup_ops)
53 return -1;
54
55 ret = cgroup_ops->get(cgroup_ops, "freezer.state", v, sizeof(v), name, lxcpath);
56 cgroup_exit(cgroup_ops);
fd5838e5 57 if (ret < 0)
4fb3cba5
DE
58 return -1;
59
fd5838e5
CB
60 v[99] = '\0';
61 v[lxc_char_right_gc(v, strlen(v))] = '\0';
62
4fb3cba5
DE
63 return lxc_str2state(v);
64}
65
974a8aba 66static int do_freeze_thaw(bool freeze, const char *name, const char *lxcpath)
4fb3cba5 67{
fd5838e5 68 int ret;
4fb3cba5 69 char v[100];
2202afc9 70 struct cgroup_ops *cgroup_ops;
4fb3cba5 71 const char *state = freeze ? "FROZEN" : "THAWED";
974a8aba
CB
72 size_t state_len = 6;
73 lxc_state_t new_state = freeze ? FROZEN : THAWED;
4fb3cba5 74
2202afc9
CB
75 cgroup_ops = cgroup_init(NULL);
76 if (!cgroup_ops)
77 return -1;
78
79 ret = cgroup_ops->set(cgroup_ops, "freezer.state", state, name, lxcpath);
fd5838e5 80 if (ret < 0) {
2202afc9 81 cgroup_exit(cgroup_ops);
fd5838e5 82 ERROR("Failed to freeze %s", name);
4fb3cba5
DE
83 return -1;
84 }
fd5838e5
CB
85
86 for (;;) {
2202afc9 87 ret = cgroup_ops->get(cgroup_ops, "freezer.state", v, sizeof(v), name, lxcpath);
fd5838e5 88 if (ret < 0) {
2202afc9 89 cgroup_exit(cgroup_ops);
fd5838e5 90 ERROR("Failed to get freezer state of %s", name);
4fb3cba5
DE
91 return -1;
92 }
fd5838e5
CB
93
94 v[99] = '\0';
95 v[lxc_char_right_gc(v, strlen(v))] = '\0';
96
974a8aba 97 ret = strncmp(v, state, state_len);
fd5838e5 98 if (ret == 0) {
2202afc9 99 cgroup_exit(cgroup_ops);
974a8aba
CB
100 lxc_cmd_serve_state_clients(name, lxcpath, new_state);
101 lxc_monitor_send_state(name, new_state, lxcpath);
4fb3cba5
DE
102 return 0;
103 }
fd5838e5 104
4fb3cba5
DE
105 sleep(1);
106 }
107}
ae5c8b8e 108
9123e471 109int lxc_freeze(const char *name, const char *lxcpath)
0ad19a3f 110{
974a8aba 111 lxc_cmd_serve_state_clients(name, lxcpath, FREEZING);
9123e471 112 lxc_monitor_send_state(name, FREEZING, lxcpath);
974a8aba 113 return do_freeze_thaw(true, name, lxcpath);
0ad19a3f 114}
115
9123e471 116int lxc_unfreeze(const char *name, const char *lxcpath)
0ad19a3f 117{
974a8aba 118 return do_freeze_thaw(false, name, lxcpath);
0ad19a3f 119}