]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/state.c
lxc: use new logging system
[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 return -1;
59 }
60
61 int lxc_setstate(const char *name, lxc_state_t state)
62 {
63 int fd, err;
64 char file[MAXPATHLEN];
65 const char *str = lxc_state2str(state);
66
67 if (!str)
68 return -1;
69
70 snprintf(file, MAXPATHLEN, LXCPATH "/%s/state", name);
71
72 fd = open(file, O_WRONLY);
73 if (fd < 0) {
74 SYSERROR("failed to open %s file", file);
75 return -1;
76 }
77
78 if (flock(fd, LOCK_EX)) {
79 SYSERROR("failed to take the lock to %s", file);
80 goto out;
81 }
82
83 if (ftruncate(fd, 0)) {
84 SYSERROR("failed to truncate the file %s", file);
85 goto out;
86 }
87
88 if (write(fd, str, strlen(str)) < 0) {
89 SYSERROR("failed to write state to %s", file);
90 goto out;
91 }
92
93 err = 0;
94 out:
95 close(fd);
96
97 lxc_monitor_send_state(name, state);
98
99 return -err;
100 }
101
102 int lxc_mkstate(const char *name)
103 {
104 int fd;
105 char file[MAXPATHLEN];
106
107 snprintf(file, MAXPATHLEN, LXCPATH "/%s/state", name);
108 fd = creat(file, S_IRUSR|S_IWUSR);
109 if (fd < 0) {
110 SYSERROR("failed to create file %s", file);
111 return -1;
112 }
113 close(fd);
114 return 0;
115 }
116
117 int lxc_rmstate(const char *name)
118 {
119 char file[MAXPATHLEN];
120 snprintf(file, MAXPATHLEN, LXCPATH "/%s/state", name);
121 unlink(file);
122 return 0;
123 }
124
125 lxc_state_t lxc_getstate(const char *name)
126 {
127 int fd, err;
128 char file[MAXPATHLEN];
129
130 snprintf(file, MAXPATHLEN, LXCPATH "/%s/state", name);
131
132 fd = open(file, O_RDONLY);
133 if (fd < 0) {
134 SYSERROR("failed to open %s", file);
135 return -1;
136 }
137
138 if (flock(fd, LOCK_SH)) {
139 SYSERROR("failed to take the lock to %s", file);
140 close(fd);
141 return -1;
142 }
143
144 err = read(fd, file, strlen(file));
145 if (err < 0) {
146 SYSERROR("failed to read file %s", file);
147 close(fd);
148 return -1;
149 }
150 file[err] = '\0';
151
152 close(fd);
153 return lxc_str2state(file);
154 }
155
156 static int freezer_state(const char *name)
157 {
158 char freezer[MAXPATHLEN];
159 char status[MAXPATHLEN];
160 FILE *file;
161 int err;
162
163 snprintf(freezer, MAXPATHLEN,
164 LXCPATH "/%s/freezer.state", name);
165
166 file = fopen(freezer, "r");
167 if (!file)
168 return -1;
169
170 err = fscanf(file, "%s", status);
171 fclose(file);
172
173 if (err == EOF) {
174 SYSERROR("failed to read %s", freezer);
175 return -1;
176 }
177
178 return lxc_str2state(status);
179 }
180
181 lxc_state_t lxc_state(const char *name)
182 {
183 int state = freezer_state(name);
184 if (state != FROZEN && state != FREEZING)
185 state = lxc_getstate(name);
186 return state;
187 }