]>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
)
73 char freezer
[MAXPATHLEN
];
74 char status
[MAXPATHLEN
];
78 err
= lxc_cgroup_path_get(&nsgroup
, "freezer", name
, lxcpath
);
82 err
= snprintf(freezer
, MAXPATHLEN
, "%s/freezer.state", nsgroup
);
83 if (err
< 0 || err
>= MAXPATHLEN
)
86 file
= fopen(freezer
, "r");
90 err
= fscanf(file
, "%s", status
);
94 SYSERROR("failed to read %s", freezer
);
98 return lxc_str2state(status
);
106 lxc_state_t
lxc_getstate(const char *name
, const char *lxcpath
)
108 lxc_state_t state
= freezer_state(name
, lxcpath
);
109 if (state
!= FROZEN
&& state
!= FREEZING
)
110 state
= lxc_cmd_get_state(name
, lxcpath
);
114 static int fillwaitedstates(const char *strstates
, int *states
)
116 char *token
, *saveptr
= NULL
;
117 char *strstates_dup
= strdup(strstates
);
123 token
= strtok_r(strstates_dup
, "|", &saveptr
);
126 state
= lxc_str2state(token
);
134 token
= strtok_r(NULL
, "|", &saveptr
);
140 extern int lxc_wait(const char *lxcname
, const char *states
, int timeout
, const char *lxcpath
)
144 int s
[MAX_STATE
] = { }, fd
;
146 if (fillwaitedstates(states
, s
))
149 if (lxc_monitord_spawn(lxcpath
))
152 fd
= lxc_monitor_open(lxcpath
);
157 * if container present,
158 * then check if already in requested state
161 state
= lxc_getstate(lxcname
, lxcpath
);
164 } else if ((state
>= 0) && (s
[state
])) {
170 int elapsed_time
, curtime
= 0;
176 retval
= gettimeofday(&tv
, NULL
);
181 if (lxc_monitor_read_timeout(fd
, &msg
, timeout
) < 0) {
182 /* try again if select interrupted by signal */
188 retval
= gettimeofday(&tv
, NULL
);
191 elapsed_time
= tv
.tv_sec
- curtime
;
192 if (timeout
- elapsed_time
<= 0)
194 timeout
-= elapsed_time
;
197 if (strcmp(lxcname
, msg
.name
)) {
207 if (msg
.value
< 0 || msg
.value
>= MAX_STATE
) {
208 ERROR("Receive an invalid state number '%d'",
223 /* just ignore garbage */
229 lxc_monitor_close(fd
);