]>
git.proxmox.com Git - mirror_lxc.git/blob - src/liblxc/lxc_state.c
86e4d9f6ba68014f9bfa00c59af547fe970ecafc
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
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.
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.
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
28 #include <sys/types.h>
29 #include <sys/types.h>
30 #include <sys/param.h>
36 static char *strstate
[] = {
37 "STOPPED", "STARTING", "RUNNING", "STOPPING",
38 "ABORTING", "FREEZING", "FROZEN",
41 const char *state2str(lxc_state_t state
)
43 if (state
< STOPPED
|| state
> MAX_STATE
- 1)
45 return strstate
[state
];
48 lxc_state_t
str2state(const char *state
)
51 len
= sizeof(strstate
)/sizeof(strstate
[0]);
52 for (i
= 0; i
< len
; i
++)
53 if (!strcmp(strstate
[i
], state
))
58 int lxc_setstate(const char *name
, lxc_state_t state
)
61 char file
[MAXPATHLEN
];
62 const char *str
= state2str(state
);
67 snprintf(file
, MAXPATHLEN
, LXCPATH
"/%s/state", name
);
69 fd
= open(file
, O_WRONLY
);
71 lxc_log_syserror("failed to open %s file", file
);
75 if (flock(fd
, LOCK_EX
)) {
76 lxc_log_syserror("failed to take the lock to %s", file
);
80 if (ftruncate(fd
, 0)) {
81 lxc_log_syserror("failed to truncate the file %s", file
);
85 if (write(fd
, str
, strlen(str
)) < 0) {
86 lxc_log_syserror("failed to write state to %s", file
);
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.
104 int mkstate(const char *name
)
107 char file
[MAXPATHLEN
];
109 snprintf(file
, MAXPATHLEN
, LXCPATH
"/%s/state", name
);
110 fd
= creat(file
, S_IRUSR
|S_IWUSR
);
112 lxc_log_syserror("failed to create file %s", file
);
119 int rmstate(const char *name
)
121 char file
[MAXPATHLEN
];
122 snprintf(file
, MAXPATHLEN
, LXCPATH
"/%s/state", name
);
127 lxc_state_t
lxc_getstate(const char *name
)
130 char file
[MAXPATHLEN
];
132 snprintf(file
, MAXPATHLEN
, LXCPATH
"/%s/state", name
);
134 fd
= open(file
, O_RDONLY
);
136 lxc_log_syserror("failed to open %s", file
);
140 if (flock(fd
, LOCK_SH
)) {
141 lxc_log_syserror("failed to take the lock to %s", file
);
146 err
= read(fd
, file
, strlen(file
));
148 lxc_log_syserror("failed to read file %s", file
);
155 return str2state(file
);
158 static int freezer_state(const char *name
)
160 char freezer
[MAXPATHLEN
];
161 char status
[MAXPATHLEN
];
165 snprintf(freezer
, MAXPATHLEN
,
166 LXCPATH
"/%s/freezer.freeze", name
);
168 file
= fopen(freezer
, "r");
170 lxc_log_syserror("failed to open %s", freezer
);
174 err
= fscanf(file
, "%s", status
);
178 lxc_log_syserror("failed to read %s", freezer
);
182 return str2state(status
);
185 lxc_state_t
lxc_state(const char *name
)
187 int state
= freezer_state(name
);
188 if (state
!= FROZEN
&& state
!= FREEZING
)
189 state
= lxc_getstate(name
);