]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/freezer.c
Makefile.am: use right .h file name for seccomp
[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 <dlezcano at fr.ibm.com>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #undef _GNU_SOURCE
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>
32 #include <sys/param.h>
33
34 #include "error.h"
35
36 #include <lxc/log.h>
37 #include <lxc/cgroup.h>
38
39 lxc_log_define(lxc_freezer, lxc);
40
41 static int freeze_unfreeze(const char *name, int freeze)
42 {
43 char *nsgroup;
44 char freezer[MAXPATHLEN], *f;
45 char tmpf[32];
46 int fd, ret;
47
48 ret = lxc_cgroup_path_get(&nsgroup, "freezer", name);
49 if (ret)
50 return -1;
51
52 ret = snprintf(freezer, MAXPATHLEN, "%s/freezer.state", nsgroup);
53 if (ret >= MAXPATHLEN) {
54 ERROR("freezer.state name too long");
55 return -1;
56 }
57
58 fd = open(freezer, O_RDWR);
59 if (fd < 0) {
60 SYSERROR("failed to open freezer for '%s'", name);
61 return -1;
62 }
63
64 if (freeze) {
65 f = "FROZEN";
66 ret = write(fd, f, strlen(f) + 1);
67 } else {
68 f = "THAWED";
69 ret = write(fd, f, strlen(f) + 1);
70
71 /* compatibility code with old freezer interface */
72 if (ret < 0) {
73 f = "RUNNING";
74 ret = write(fd, f, strlen(f) + 1) < 0;
75 }
76 }
77
78 if (ret < 0) {
79 SYSERROR("failed to write '%s' to '%s'", f, freezer);
80 goto out;
81 }
82
83 while (1) {
84 ret = lseek(fd, 0L, SEEK_SET);
85 if (ret < 0) {
86 SYSERROR("failed to lseek on file '%s'", freezer);
87 goto out;
88 }
89
90 ret = read(fd, tmpf, sizeof(tmpf));
91 if (ret < 0) {
92 SYSERROR("failed to read to '%s'", freezer);
93 goto out;
94 }
95
96 ret = strncmp(f, tmpf, strlen(f));
97 if (!ret)
98 break; /* Success */
99
100 sleep(1);
101
102 ret = lseek(fd, 0L, SEEK_SET);
103 if (ret < 0) {
104 SYSERROR("failed to lseek on file '%s'", freezer);
105 goto out;
106 }
107
108 ret = write(fd, f, strlen(f) + 1);
109 if (ret < 0) {
110 SYSERROR("failed to write '%s' to '%s'", f, freezer);
111 goto out;
112 }
113 }
114
115 out:
116 close(fd);
117 return ret;
118 }
119
120 int lxc_freeze(const char *name)
121 {
122 return freeze_unfreeze(name, 1);
123 }
124
125 int lxc_unfreeze(const char *name)
126 {
127 return freeze_unfreeze(name, 0);
128 }
129