]> git.proxmox.com Git - mirror_lxc.git/blob - src/liblxc/lxc_state.c
86e4d9f6ba68014f9bfa00c59af547fe970ecafc
[mirror_lxc.git] / src / liblxc / lxc_state.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 #include <stdio.h>
24 #include <string.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/stat.h>
32 #include <sys/file.h>
33
34 #include <lxc.h>
35
36 static char *strstate[] = {
37 "STOPPED", "STARTING", "RUNNING", "STOPPING",
38 "ABORTING", "FREEZING", "FROZEN",
39 };
40
41 const char *state2str(lxc_state_t state)
42 {
43 if (state < STOPPED || state > MAX_STATE - 1)
44 return NULL;
45 return strstate[state];
46 }
47
48 lxc_state_t str2state(const char *state)
49 {
50 int i, len;
51 len = sizeof(strstate)/sizeof(strstate[0]);
52 for (i = 0; i < len; i++)
53 if (!strcmp(strstate[i], state))
54 return i;
55 return -1;
56 }
57
58 int lxc_setstate(const char *name, lxc_state_t state)
59 {
60 int fd, err;
61 char file[MAXPATHLEN];
62 const char *str = state2str(state);
63
64 if (!str)
65 return -1;
66
67 snprintf(file, MAXPATHLEN, LXCPATH "/%s/state", name);
68
69 fd = open(file, O_WRONLY);
70 if (fd < 0) {
71 lxc_log_syserror("failed to open %s file", file);
72 return -1;
73 }
74
75 if (flock(fd, LOCK_EX)) {
76 lxc_log_syserror("failed to take the lock to %s", file);
77 goto out;
78 }
79
80 if (ftruncate(fd, 0)) {
81 lxc_log_syserror("failed to truncate the file %s", file);
82 goto out;
83 }
84
85 if (write(fd, str, strlen(str)) < 0) {
86 lxc_log_syserror("failed to write state to %s", file);
87 goto out;
88 }
89
90 err = 0;
91 out:
92 close(fd);
93
94 /* let the event to be propagated, crappy but that works,
95 * otherwise the events will be folded into only one event,
96 * and I want to have them to be one by one in order
97 * to follow the different states of the container.
98 */
99 usleep(200000);
100
101 return -err;
102 }
103
104 int mkstate(const char *name)
105 {
106 int fd;
107 char file[MAXPATHLEN];
108
109 snprintf(file, MAXPATHLEN, LXCPATH "/%s/state", name);
110 fd = creat(file, S_IRUSR|S_IWUSR);
111 if (fd < 0) {
112 lxc_log_syserror("failed to create file %s", file);
113 return -1;
114 }
115 close(fd);
116 return 0;
117 }
118
119 int rmstate(const char *name)
120 {
121 char file[MAXPATHLEN];
122 snprintf(file, MAXPATHLEN, LXCPATH "/%s/state", name);
123 unlink(file);
124 return 0;
125 }
126
127 lxc_state_t lxc_getstate(const char *name)
128 {
129 int fd, err;
130 char file[MAXPATHLEN];
131
132 snprintf(file, MAXPATHLEN, LXCPATH "/%s/state", name);
133
134 fd = open(file, O_RDONLY);
135 if (fd < 0) {
136 lxc_log_syserror("failed to open %s", file);
137 return -1;
138 }
139
140 if (flock(fd, LOCK_SH)) {
141 lxc_log_syserror("failed to take the lock to %s", file);
142 close(fd);
143 return -1;
144 }
145
146 err = read(fd, file, strlen(file));
147 if (err < 0) {
148 lxc_log_syserror("failed to read file %s", file);
149 close(fd);
150 return -1;
151 }
152 file[err] = '\0';
153
154 close(fd);
155 return str2state(file);
156 }
157
158 static int freezer_state(const char *name)
159 {
160 char freezer[MAXPATHLEN];
161 char status[MAXPATHLEN];
162 FILE *file;
163 int err;
164
165 snprintf(freezer, MAXPATHLEN,
166 LXCPATH "/%s/freezer.freeze", name);
167
168 file = fopen(freezer, "r");
169 if (file < 0) {
170 lxc_log_syserror("failed to open %s", freezer);
171 return -1;
172 }
173
174 err = fscanf(file, "%s", status);
175 fclose(file);
176
177 if (err == EOF) {
178 lxc_log_syserror("failed to read %s", freezer);
179 return -1;
180 }
181
182 return str2state(status);
183 }
184
185 lxc_state_t lxc_state(const char *name)
186 {
187 int state = freezer_state(name);
188 if (state != FROZEN && state != FREEZING)
189 state = lxc_getstate(name);
190 return state;
191 }