]>
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 <daniel.lezcano at free.fr>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/param.h>
37 #include <lxc/start.h>
38 #include <lxc/cgroup.h>
39 #include <lxc/monitor.h>
43 lxc_log_define(lxc_state
, lxc
);
45 static char *strstate
[] = {
46 "STOPPED", "STARTING", "RUNNING", "STOPPING",
47 "ABORTING", "FREEZING", "FROZEN", "THAWED",
50 const char *lxc_state2str(lxc_state_t state
)
52 if (state
< STOPPED
|| state
> MAX_STATE
- 1)
54 return strstate
[state
];
57 lxc_state_t
lxc_str2state(const char *state
)
61 len
= sizeof(strstate
)/sizeof(strstate
[0]);
62 for (i
= 0; i
< len
; i
++)
63 if (!strcmp(strstate
[i
], state
))
66 ERROR("invalid state '%s'", state
);
70 static lxc_state_t
freezer_state(const char *name
, const char *lxcpath
)
72 char *cgabspath
= NULL
;
73 char freezer
[MAXPATHLEN
];
74 char status
[MAXPATHLEN
];
78 cgabspath
= lxc_cgroup_path_get("freezer", name
, lxcpath
);
82 ret
= snprintf(freezer
, MAXPATHLEN
, "%s/freezer.state", cgabspath
);
83 if (ret
< 0 || ret
>= MAXPATHLEN
)
86 file
= fopen(freezer
, "r");
92 ret
= fscanf(file
, "%s", status
);
96 SYSERROR("failed to read %s", freezer
);
101 ret
= lxc_str2state(status
);
108 lxc_state_t
lxc_getstate(const char *name
, const char *lxcpath
)
110 lxc_state_t state
= freezer_state(name
, lxcpath
);
111 if (state
!= FROZEN
&& state
!= FREEZING
)
112 state
= lxc_cmd_get_state(name
, lxcpath
);
116 static int fillwaitedstates(const char *strstates
, int *states
)
118 char *token
, *saveptr
= NULL
;
119 char *strstates_dup
= strdup(strstates
);
125 token
= strtok_r(strstates_dup
, "|", &saveptr
);
128 state
= lxc_str2state(token
);
136 token
= strtok_r(NULL
, "|", &saveptr
);
142 extern int lxc_wait(const char *lxcname
, const char *states
, int timeout
, const char *lxcpath
)
146 int s
[MAX_STATE
] = { }, fd
;
148 if (fillwaitedstates(states
, s
))
151 if (lxc_monitord_spawn(lxcpath
))
154 fd
= lxc_monitor_open(lxcpath
);
159 * if container present,
160 * then check if already in requested state
163 state
= lxc_getstate(lxcname
, lxcpath
);
166 } else if ((state
>= 0) && (s
[state
])) {
172 int elapsed_time
, curtime
= 0;
178 retval
= gettimeofday(&tv
, NULL
);
183 if (lxc_monitor_read_timeout(fd
, &msg
, timeout
) < 0) {
184 /* try again if select interrupted by signal */
190 retval
= gettimeofday(&tv
, NULL
);
193 elapsed_time
= tv
.tv_sec
- curtime
;
194 if (timeout
- elapsed_time
<= 0)
196 timeout
-= elapsed_time
;
199 if (strcmp(lxcname
, msg
.name
)) {
209 if (msg
.value
< 0 || msg
.value
>= MAX_STATE
) {
210 ERROR("Receive an invalid state number '%d'",
225 /* just ignore garbage */
231 lxc_monitor_close(fd
);