]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/state.c
remove dead state code
[mirror_lxc.git] / src / lxc / state.c
CommitLineData
5e97c3fc 1/*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
8 *
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.
13 *
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.
18 *
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
22 */
23#include <stdio.h>
0ad19a3f 24#include <string.h>
25#include <fcntl.h>
26#include <errno.h>
5e97c3fc 27#include <unistd.h>
5e97c3fc 28#include <sys/types.h>
0ad19a3f 29#include <sys/types.h>
30#include <sys/param.h>
31#include <sys/stat.h>
32#include <sys/file.h>
5e97c3fc 33
b113348e 34#include <lxc/lxc.h>
36eb9bde 35#include <lxc/log.h>
e98fe68b 36#include "commands.h"
36eb9bde
CLG
37
38lxc_log_define(lxc_state, lxc);
0ad19a3f 39
40static char *strstate[] = {
41 "STOPPED", "STARTING", "RUNNING", "STOPPING",
42 "ABORTING", "FREEZING", "FROZEN",
43};
44
45const char *lxc_state2str(lxc_state_t state)
46{
47 if (state < STOPPED || state > MAX_STATE - 1)
48 return NULL;
49 return strstate[state];
50}
5e97c3fc 51
0ad19a3f 52lxc_state_t lxc_str2state(const char *state)
5e97c3fc 53{
0ad19a3f 54 int i, len;
55 len = sizeof(strstate)/sizeof(strstate[0]);
56 for (i = 0; i < len; i++)
57 if (!strcmp(strstate[i], state))
58 return i;
3ab87b66 59
439358bf 60 ERROR("invalid state '%s'", state);
0ad19a3f 61 return -1;
5e97c3fc 62}
63
0ad19a3f 64int lxc_rmstate(const char *name)
65{
66 char file[MAXPATHLEN];
67 snprintf(file, MAXPATHLEN, LXCPATH "/%s/state", name);
68 unlink(file);
5e97c3fc 69 return 0;
70}
71
0ad19a3f 72static int freezer_state(const char *name)
73{
74 char freezer[MAXPATHLEN];
75 char status[MAXPATHLEN];
76 FILE *file;
77 int err;
35d2c3e7 78
0ad19a3f 79 snprintf(freezer, MAXPATHLEN,
70a59143 80 LXCPATH "/%s/freezer.state", name);
0ad19a3f 81
82 file = fopen(freezer, "r");
83 if (!file)
84 return -1;
85
86 err = fscanf(file, "%s", status);
87 fclose(file);
88
89 if (err == EOF) {
36eb9bde 90 SYSERROR("failed to read %s", freezer);
0ad19a3f 91 return -1;
92 }
93
94 return lxc_str2state(status);
95}
96
e98fe68b
DL
97lxc_state_t lxc_getstate(const char *name)
98{
99 struct lxc_command command = {
100 .request = { .type = LXC_COMMAND_STATE },
101 };
102
103 int ret;
104
105 ret = lxc_command(name, &command);
106 if (ret < 0) {
107 ERROR("failed to send command");
108 return -1;
109 }
110
111 if (!ret) {
112 WARN("'%s' has stopped before sending its state", name);
113 return -1;
114 }
115
116 if (command.answer.ret < 0) {
117 ERROR("failed to get state for '%s': %s",
118 name, strerror(-command.answer.ret));
119 return -1;
120 }
121
122 DEBUG("'%s' is in '%s' state", name, lxc_state2str(command.answer.ret));
123
124 return command.answer.ret;
125}
126
0ad19a3f 127lxc_state_t lxc_state(const char *name)
128{
129 int state = freezer_state(name);
130 if (state != FROZEN && state != FREEZING)
131 state = lxc_getstate(name);
132 return state;
133}
e98fe68b
DL
134
135/*----------------------------------------------------------------------------
136 * functions used by lxc-start mainloop
137 * to handle above command request.
138 *--------------------------------------------------------------------------*/
139extern int lxc_state_callback(int fd, struct lxc_request *request,
140 struct lxc_handler *handler)
141{
142 struct lxc_answer answer;
143 int ret;
144
145 answer.ret = handler->state;
146
147 ret = send(fd, &answer, sizeof(answer), 0);
148 if (ret < 0) {
149 WARN("failed to send answer to the peer");
150 goto out;
151 }
152
153 if (ret != sizeof(answer)) {
154 ERROR("partial answer sent");
155 goto out;
156 }
157
158out:
159 return ret;
160}
161