]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/freezer.c
lxc-user-nic: bugfixes
[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
e2bcd7db 34#include "error.h"
d27b0806
DE
35#include "state.h"
36#include "monitor.h"
f2363e38 37#include "log.h"
4fb3cba5 38#include "lxc.h"
36eb9bde
CLG
39
40lxc_log_define(lxc_freezer, lxc);
0ad19a3f 41
4fb3cba5
DE
42lxc_state_t freezer_state(const char *name, const char *lxcpath)
43{
44 char v[100];
45 if (lxc_cgroup_get("freezer.state", v, 100, name, lxcpath) < 0)
46 return -1;
47
48 if (v[strlen(v)-1] == '\n')
49 v[strlen(v)-1] = '\0';
50 return lxc_str2state(v);
51}
52
53static int do_freeze_thaw(int freeze, const char *name, const char *lxcpath)
54{
55 char v[100];
56 const char *state = freeze ? "FROZEN" : "THAWED";
57
58 if (lxc_cgroup_set("freezer.state", state, name, lxcpath) < 0) {
59 ERROR("Failed to freeze %s:%s", lxcpath, name);
60 return -1;
61 }
62 while (1) {
63 if (lxc_cgroup_get("freezer.state", v, 100, name, lxcpath) < 0) {
64 ERROR("Failed to get new freezer state for %s:%s", lxcpath, name);
65 return -1;
66 }
67 if (v[strlen(v)-1] == '\n')
68 v[strlen(v)-1] = '\0';
69 if (strncmp(v, state, strlen(state)) == 0) {
70 if (name)
71 lxc_monitor_send_state(name, freeze ? FROZEN : THAWED, lxcpath);
72 return 0;
73 }
74 sleep(1);
75 }
76}
ae5c8b8e 77
9123e471 78int lxc_freeze(const char *name, const char *lxcpath)
0ad19a3f 79{
9123e471 80 lxc_monitor_send_state(name, FREEZING, lxcpath);
4fb3cba5 81 return do_freeze_thaw(1, name, lxcpath);
0ad19a3f 82}
83
9123e471 84int lxc_unfreeze(const char *name, const char *lxcpath)
0ad19a3f 85{
4fb3cba5 86 return do_freeze_thaw(0, name, lxcpath);
0ad19a3f 87}