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