]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/state.c
avoid two times error msg about invalid state
[mirror_lxc.git] / src / 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/lxc.h>
35 #include <lxc/log.h>
36
37 lxc_log_define(lxc_state, lxc);
38
39 static char *strstate[] = {
40 "STOPPED", "STARTING", "RUNNING", "STOPPING",
41 "ABORTING", "FREEZING", "FROZEN",
42 };
43
44 const char *lxc_state2str(lxc_state_t state)
45 {
46 if (state < STOPPED || state > MAX_STATE - 1)
47 return NULL;
48 return strstate[state];
49 }
50
51 lxc_state_t lxc_str2state(const char *state)
52 {
53 int i, len;
54 len = sizeof(strstate)/sizeof(strstate[0]);
55 for (i = 0; i < len; i++)
56 if (!strcmp(strstate[i], state))
57 return i;
58
59 ERROR("invalid state '%s'", state);
60 return -1;
61 }
62
63 int lxc_setstate(const char *name, lxc_state_t state)
64 {
65 int fd, err = -1;
66 char file[MAXPATHLEN];
67 const char *str = lxc_state2str(state);
68
69 if (!str)
70 return err;
71
72 snprintf(file, MAXPATHLEN, LXCPATH "/%s/state", name);
73
74 fd = open(file, O_WRONLY);
75 if (fd < 0) {
76 SYSERROR("failed to open %s file", file);
77 return err;
78 }
79
80 if (flock(fd, LOCK_EX)) {
81 SYSERROR("failed to take the lock to %s", file);
82 goto out;
83 }
84
85 if (ftruncate(fd, 0)) {
86 SYSERROR("failed to truncate the file %s", file);
87 goto out;
88 }
89
90 if (write(fd, str, strlen(str)) < 0) {
91 SYSERROR("failed to write state to %s", file);
92 goto out;
93 }
94
95 err = 0;
96 out:
97 close(fd);
98
99 lxc_monitor_send_state(name, state);
100
101 return err;
102 }
103
104 int lxc_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 SYSERROR("failed to create file %s", file);
113 return -1;
114 }
115 close(fd);
116 return 0;
117 }
118
119 int lxc_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 SYSERROR("failed to open %s", file);
137 return -1;
138 }
139
140 if (flock(fd, LOCK_SH)) {
141 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 SYSERROR("failed to read file %s", file);
149 close(fd);
150 return -1;
151 }
152 file[err] = '\0';
153
154 close(fd);
155 return lxc_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.state", name);
167
168 file = fopen(freezer, "r");
169 if (!file)
170 return -1;
171
172 err = fscanf(file, "%s", status);
173 fclose(file);
174
175 if (err == EOF) {
176 SYSERROR("failed to read %s", freezer);
177 return -1;
178 }
179
180 return lxc_str2state(status);
181 }
182
183 lxc_state_t lxc_state(const char *name)
184 {
185 int state = freezer_state(name);
186 if (state != FROZEN && state != FREEZING)
187 state = lxc_getstate(name);
188 return state;
189 }