]>
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
))
59 ERROR("invalid state '%s'", state
);
63 int lxc_setstate(const char *name
, lxc_state_t state
)
66 char file
[MAXPATHLEN
];
67 const char *str
= lxc_state2str(state
);
72 snprintf(file
, MAXPATHLEN
, LXCPATH
"/%s/state", name
);
74 fd
= open(file
, O_WRONLY
);
76 SYSERROR("failed to open %s file", file
);
80 if (flock(fd
, LOCK_EX
)) {
81 SYSERROR("failed to take the lock to %s", file
);
85 if (ftruncate(fd
, 0)) {
86 SYSERROR("failed to truncate the file %s", file
);
90 if (write(fd
, str
, strlen(str
)) < 0) {
91 SYSERROR("failed to write state to %s", file
);
99 lxc_monitor_send_state(name
, state
);
104 int lxc_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 SYSERROR("failed to create file %s", file
);
119 int lxc_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 SYSERROR("failed to open %s", file
);
140 if (flock(fd
, LOCK_SH
)) {
141 SYSERROR("failed to take the lock to %s", file
);
146 err
= read(fd
, file
, strlen(file
));
148 SYSERROR("failed to read file %s", file
);
155 return lxc_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.state", name
);
168 file
= fopen(freezer
, "r");
172 err
= fscanf(file
, "%s", status
);
176 SYSERROR("failed to read %s", freezer
);
180 return lxc_str2state(status
);
183 lxc_state_t
lxc_state(const char *name
)
185 int state
= freezer_state(name
);
186 if (state
!= FROZEN
&& state
!= FREEZING
)
187 state
= lxc_getstate(name
);