]>
Commit | Line | Data |
---|---|---|
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 | 23 | |
d38dd64a CB |
24 | #ifndef _GNU_SOURCE |
25 | #define _GNU_SOURCE 1 | |
26 | #endif | |
27 | #include <errno.h> | |
28 | #include <fcntl.h> | |
0ad19a3f | 29 | #include <stdio.h> |
0ad19a3f | 30 | #include <stdlib.h> |
0ad19a3f | 31 | #include <string.h> |
0ad19a3f | 32 | #include <sys/param.h> |
d38dd64a CB |
33 | #include <sys/types.h> |
34 | #include <unistd.h> | |
0ad19a3f | 35 | |
2202afc9 | 36 | #include "cgroup.h" |
974a8aba | 37 | #include "commands.h" |
d38dd64a | 38 | #include "config.h" |
e2bcd7db | 39 | #include "error.h" |
f2363e38 | 40 | #include "log.h" |
4fb3cba5 | 41 | #include "lxc.h" |
fd5838e5 | 42 | #include "monitor.h" |
fd5838e5 | 43 | #include "state.h" |
37ef15bb | 44 | #include "string_utils.h" |
36eb9bde | 45 | |
ac2cecc4 | 46 | lxc_log_define(freezer, lxc); |
0ad19a3f | 47 | |
5a087e05 CB |
48 | static int do_freeze_thaw(bool freeze, struct lxc_conf *conf, const char *name, |
49 | const char *lxcpath) | |
4fb3cba5 | 50 | { |
fd5838e5 | 51 | int ret; |
4fb3cba5 | 52 | char v[100]; |
2202afc9 | 53 | struct cgroup_ops *cgroup_ops; |
02f71d7e | 54 | const char *state; |
db1228b3 | 55 | size_t state_len; |
974a8aba | 56 | lxc_state_t new_state = freeze ? FROZEN : THAWED; |
4fb3cba5 | 57 | |
02f71d7e | 58 | state = lxc_state2str(new_state); |
db1228b3 | 59 | state_len = strlen(state); |
9eb9ce3e | 60 | |
5a087e05 | 61 | cgroup_ops = cgroup_init(conf); |
2202afc9 CB |
62 | if (!cgroup_ops) |
63 | return -1; | |
64 | ||
65 | ret = cgroup_ops->set(cgroup_ops, "freezer.state", state, name, lxcpath); | |
fd5838e5 | 66 | if (ret < 0) { |
2202afc9 | 67 | cgroup_exit(cgroup_ops); |
02f71d7e CB |
68 | ERROR("Failed to %s %s", |
69 | (new_state == FROZEN ? "freeze" : "unfreeze"), name); | |
4fb3cba5 DE |
70 | return -1; |
71 | } | |
fd5838e5 CB |
72 | |
73 | for (;;) { | |
02f71d7e CB |
74 | ret = cgroup_ops->get(cgroup_ops, "freezer.state", v, sizeof(v), |
75 | name, lxcpath); | |
fd5838e5 | 76 | if (ret < 0) { |
2202afc9 | 77 | cgroup_exit(cgroup_ops); |
fd5838e5 | 78 | ERROR("Failed to get freezer state of %s", name); |
4fb3cba5 DE |
79 | return -1; |
80 | } | |
fd5838e5 | 81 | |
02f71d7e | 82 | v[sizeof(v) - 1] = '\0'; |
fd5838e5 CB |
83 | v[lxc_char_right_gc(v, strlen(v))] = '\0'; |
84 | ||
974a8aba | 85 | ret = strncmp(v, state, state_len); |
fd5838e5 | 86 | if (ret == 0) { |
2202afc9 | 87 | cgroup_exit(cgroup_ops); |
974a8aba CB |
88 | lxc_cmd_serve_state_clients(name, lxcpath, new_state); |
89 | lxc_monitor_send_state(name, new_state, lxcpath); | |
4fb3cba5 DE |
90 | return 0; |
91 | } | |
fd5838e5 | 92 | |
4fb3cba5 DE |
93 | sleep(1); |
94 | } | |
95 | } | |
ae5c8b8e | 96 | |
5a087e05 | 97 | int lxc_freeze(struct lxc_conf *conf, const char *name, const char *lxcpath) |
0ad19a3f | 98 | { |
974a8aba | 99 | lxc_cmd_serve_state_clients(name, lxcpath, FREEZING); |
9123e471 | 100 | lxc_monitor_send_state(name, FREEZING, lxcpath); |
5a087e05 | 101 | return do_freeze_thaw(true, conf, name, lxcpath); |
0ad19a3f | 102 | } |
103 | ||
5a087e05 | 104 | int lxc_unfreeze(struct lxc_conf *conf, const char *name, const char *lxcpath) |
0ad19a3f | 105 | { |
5a087e05 | 106 | return do_freeze_thaw(false, conf, name, lxcpath); |
0ad19a3f | 107 | } |