]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/state.c
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>
37 lxc_log_define(lxc_state
, lxc
);
39 static char *strstate
[] = {
40 "STOPPED", "STARTING", "RUNNING", "STOPPING",
41 "ABORTING", "FREEZING", "FROZEN",
44 const char *lxc_state2str(lxc_state_t state
)
46 if (state
< STOPPED
|| state
> MAX_STATE
- 1)
48 return strstate
[state
];
51 lxc_state_t
lxc_str2state(const char *state
)
54 len
= sizeof(strstate
)/sizeof(strstate
[0]);
55 for (i
= 0; i
< len
; i
++)
56 if (!strcmp(strstate
[i
], state
))
61 int lxc_setstate(const char *name
, lxc_state_t state
)
64 char file
[MAXPATHLEN
];
65 const char *str
= lxc_state2str(state
);
70 snprintf(file
, MAXPATHLEN
, LXCPATH
"/%s/state", name
);
72 fd
= open(file
, O_WRONLY
);
74 SYSERROR("failed to open %s file", file
);
78 if (flock(fd
, LOCK_EX
)) {
79 SYSERROR("failed to take the lock to %s", file
);
83 if (ftruncate(fd
, 0)) {
84 SYSERROR("failed to truncate the file %s", file
);
88 if (write(fd
, str
, strlen(str
)) < 0) {
89 SYSERROR("failed to write state to %s", file
);
97 lxc_monitor_send_state(name
, state
);
102 int lxc_mkstate(const char *name
)
105 char file
[MAXPATHLEN
];
107 snprintf(file
, MAXPATHLEN
, LXCPATH
"/%s/state", name
);
108 fd
= creat(file
, S_IRUSR
|S_IWUSR
);
110 SYSERROR("failed to create file %s", file
);
117 int lxc_rmstate(const char *name
)
119 char file
[MAXPATHLEN
];
120 snprintf(file
, MAXPATHLEN
, LXCPATH
"/%s/state", name
);
125 lxc_state_t
lxc_getstate(const char *name
)
128 char file
[MAXPATHLEN
];
130 snprintf(file
, MAXPATHLEN
, LXCPATH
"/%s/state", name
);
132 fd
= open(file
, O_RDONLY
);
134 SYSERROR("failed to open %s", file
);
138 if (flock(fd
, LOCK_SH
)) {
139 SYSERROR("failed to take the lock to %s", file
);
144 err
= read(fd
, file
, strlen(file
));
146 SYSERROR("failed to read file %s", file
);
153 return lxc_str2state(file
);
156 static int freezer_state(const char *name
)
158 char freezer
[MAXPATHLEN
];
159 char status
[MAXPATHLEN
];
163 snprintf(freezer
, MAXPATHLEN
,
164 LXCPATH
"/%s/freezer.state", name
);
166 file
= fopen(freezer
, "r");
170 err
= fscanf(file
, "%s", status
);
174 SYSERROR("failed to read %s", freezer
);
178 return lxc_str2state(status
);
181 lxc_state_t
lxc_state(const char *name
)
183 int state
= freezer_state(name
);
184 if (state
!= FROZEN
&& state
!= FREEZING
)
185 state
= lxc_getstate(name
);