]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/freezer.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include <sys/param.h>
12 #include <sys/types.h>
15 #include "cgroups/cgroup.h"
16 #include "cgroups/cgroup_utils.h"
24 #include "string_utils.h"
26 lxc_log_define(freezer
, lxc
);
28 static void notify_state_listeners(const char *name
, const char *lxcpath
,
31 (void)lxc_cmd_serve_state_clients(name
, lxcpath
, state
);
32 (void)lxc_monitor_send_state(name
, state
, lxcpath
);
35 static int do_freeze_thaw(bool freeze
, struct lxc_conf
*conf
, const char *name
,
38 call_cleaner(cgroup_exit
) struct cgroup_ops
*cgroup_ops
= NULL
;
39 lxc_state_t new_state
= freeze
? FROZEN
: THAWED
;
44 state
= lxc_state2str(new_state
);
45 state_len
= strlen(state
);
47 cgroup_ops
= cgroup_init(conf
);
51 ret
= cgroup_ops
->set(cgroup_ops
, "freezer.state", state
, name
, lxcpath
);
53 return log_error(-1, "Failed to %s %s",
54 freeze
? "freeze" : "unfreeze", name
);
57 char cur_state
[MAX_STATE_LENGTH
] = "";
59 ret
= cgroup_ops
->get(cgroup_ops
, "freezer.state", cur_state
,
60 sizeof(cur_state
), name
, lxcpath
);
62 return log_error(-1, "Failed to get freezer state of %s", name
);
64 cur_state
[lxc_char_right_gc(cur_state
, strlen(cur_state
))] = '\0';
65 ret
= strncmp(cur_state
, state
, state_len
);
67 notify_state_listeners(name
, lxcpath
, new_state
);
77 int lxc_freeze(struct lxc_conf
*conf
, const char *name
, const char *lxcpath
)
81 notify_state_listeners(name
, lxcpath
, FREEZING
);
82 if (unified_cgroup_hierarchy() > 0)
83 ret
= lxc_cmd_freeze(name
, lxcpath
, -1);
85 ret
= do_freeze_thaw(true, conf
, name
, lxcpath
);
86 notify_state_listeners(name
, lxcpath
, !ret
? FROZEN
: RUNNING
);
90 int lxc_unfreeze(struct lxc_conf
*conf
, const char *name
, const char *lxcpath
)
94 notify_state_listeners(name
, lxcpath
, THAWED
);
95 if (unified_cgroup_hierarchy() > 0)
96 ret
= lxc_cmd_unfreeze(name
, lxcpath
, -1);
98 ret
= do_freeze_thaw(false, conf
, name
, lxcpath
);
99 notify_state_listeners(name
, lxcpath
, !ret
? RUNNING
: FROZEN
);