]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/freezer.c
confile: add lxc.cgroup.keep
[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 39#include "monitor.h"
fd5838e5 40#include "state.h"
37ef15bb 41#include "string_utils.h"
36eb9bde 42
ac2cecc4 43lxc_log_define(freezer, lxc);
0ad19a3f 44
974a8aba 45static int do_freeze_thaw(bool freeze, const char *name, const char *lxcpath)
4fb3cba5 46{
fd5838e5 47 int ret;
4fb3cba5 48 char v[100];
2202afc9 49 struct cgroup_ops *cgroup_ops;
4fb3cba5 50 const char *state = freeze ? "FROZEN" : "THAWED";
974a8aba
CB
51 size_t state_len = 6;
52 lxc_state_t new_state = freeze ? FROZEN : THAWED;
4fb3cba5 53
2202afc9
CB
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);
fd5838e5 59 if (ret < 0) {
2202afc9 60 cgroup_exit(cgroup_ops);
fd5838e5 61 ERROR("Failed to freeze %s", name);
4fb3cba5
DE
62 return -1;
63 }
fd5838e5
CB
64
65 for (;;) {
2202afc9 66 ret = cgroup_ops->get(cgroup_ops, "freezer.state", v, sizeof(v), name, lxcpath);
fd5838e5 67 if (ret < 0) {
2202afc9 68 cgroup_exit(cgroup_ops);
fd5838e5 69 ERROR("Failed to get freezer state of %s", name);
4fb3cba5
DE
70 return -1;
71 }
fd5838e5
CB
72
73 v[99] = '\0';
74 v[lxc_char_right_gc(v, strlen(v))] = '\0';
75
974a8aba 76 ret = strncmp(v, state, state_len);
fd5838e5 77 if (ret == 0) {
2202afc9 78 cgroup_exit(cgroup_ops);
974a8aba
CB
79 lxc_cmd_serve_state_clients(name, lxcpath, new_state);
80 lxc_monitor_send_state(name, new_state, lxcpath);
4fb3cba5
DE
81 return 0;
82 }
fd5838e5 83
4fb3cba5
DE
84 sleep(1);
85 }
86}
ae5c8b8e 87
9123e471 88int lxc_freeze(const char *name, const char *lxcpath)
0ad19a3f 89{
974a8aba 90 lxc_cmd_serve_state_clients(name, lxcpath, FREEZING);
9123e471 91 lxc_monitor_send_state(name, FREEZING, lxcpath);
974a8aba 92 return do_freeze_thaw(true, name, lxcpath);
0ad19a3f 93}
94
9123e471 95int lxc_unfreeze(const char *name, const char *lxcpath)
0ad19a3f 96{
974a8aba 97 return do_freeze_thaw(false, name, lxcpath);
0ad19a3f 98}