loading ruby scripts specified at command line
[strongswan.git] / src / dumm / guest.h
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #ifndef GUEST_H
17 #define GUEST_H
18
19 #include <library.h>
20 #include <utils/enumerator.h>
21
22 typedef enum guest_state_t guest_state_t;
23 typedef struct guest_t guest_t;
24
25 #include "iface.h"
26
27 /**
28 * @brief State of a guest (started, stopped, ...)
29 */
30 enum guest_state_t {
31 /** guest kernel not running at all */
32 GUEST_STOPPED,
33 /** kernel started, but not yet available */
34 GUEST_STARTING,
35 /** guest is up and running */
36 GUEST_RUNNING,
37 /** guest has been paused */
38 GUEST_PAUSED,
39 /** guest is stopping (shutting down) */
40 GUEST_STOPPING,
41 };
42
43 /**
44 * string mappings for guest_state_t
45 */
46 extern enum_name_t *guest_state_names;
47
48 /**
49 * Invoke function which lauches the UML guest.
50 *
51 * Consoles are all set to NULL, you may change them by adding additional UML
52 * options to args before invocation.
53 *
54 * @param data callback data
55 * @param guest guest to start
56 * @param args args to use for guest invocation, args[0] is kernel
57 * @param argc number of elements in args
58 * @param idle
59 * @return PID of child, 0 if failed
60 */
61 typedef pid_t (*invoke_function_t)(void *data, guest_t *guest,
62 char *args[], int argc);
63
64 /**
65 * Idle function to pass to start().
66 */
67 typedef void (*idle_function_t)(void);
68
69 /**
70 * @brief A guest is a UML instance running on the host.
71 **/
72 struct guest_t {
73
74 /**
75 * @brief Get the name of this guest.
76 *
77 * @return name of the guest
78 */
79 char* (*get_name) (guest_t *this);
80
81 /**
82 * @brief Get the process ID of the guest child process.
83 *
84 * @return name of the guest
85 */
86 pid_t (*get_pid) (guest_t *this);
87
88 /**
89 * @brief Get the state of the guest (stopped, started, etc.).
90 *
91 * @return guests state
92 */
93 guest_state_t (*get_state)(guest_t *this);
94
95 /**
96 * @brief Start the guest.
97 *
98 * @param invoke UML guest invocation function
99 * @param data data to pass back to invoke function
100 * @param idle idle function to call while waiting on child
101 * @return TRUE if guest successfully started
102 */
103 bool (*start) (guest_t *this, invoke_function_t invoke, void *data,
104 idle_function_t idle);
105
106 /**
107 * @brief Kill the guest.
108 *
109 * @param idle idle function to call while waiting to termination
110 */
111 void (*stop) (guest_t *this, idle_function_t idle);
112
113 /**
114 * @brief Create a new interface in the current scenario.
115 *
116 * @param name name of the interface in the guest
117 * @return created interface, or NULL if failed
118 */
119 iface_t* (*create_iface)(guest_t *this, char *name);
120
121 /**
122 * @brief Destroy an interface on guest.
123 *
124 * @param iface interface to destroy
125 */
126 void (*destroy_iface)(guest_t *this, iface_t *iface);
127
128 /**
129 * @brief Create an enumerator over all guest interfaces.
130 *
131 * @return enumerator over iface_t's
132 */
133 enumerator_t* (*create_iface_enumerator)(guest_t *this);
134
135 /**
136 * @brief Set the template COWFS overlay to use.
137 *
138 * @param parent parent directory where template diff should point to
139 * @return FALSE if failed
140 */
141 bool (*load_template)(guest_t *this, char *parent);
142
143 /**
144 * Execute a command in the guest.
145 *
146 * @param cb callback to call for each read block
147 * @param data data to pass to callback
148 * @param cmd command to execute
149 * @param ... printf style argument list for cmd
150 * @return return value
151 */
152 int (*exec)(guest_t *this, void(*cb)(void*,char*,size_t), void *data,
153 char *cmd, ...);
154
155 /**
156 * @brief Called whenever a SIGCHILD for the guests PID is received.
157 */
158 void (*sigchild)(guest_t *this);
159
160 /**
161 * @brief Close and destroy a guest with all interfaces
162 */
163 void (*destroy) (guest_t *this);
164 };
165
166 /**
167 * @brief Create a new, unstarted guest.
168 *
169 * @param parent parent directory to create the guest in
170 * @param name name of the guest to create
171 * @param kernel kernel this guest uses
172 * @param master read-only master filesystem for guest
173 * @param mem amount of memory to give the guest
174 */
175 guest_t *guest_create(char *parent, char *name, char *kernel,
176 char *master, int mem);
177
178 /**
179 * @brief Load a guest created with guest_create().
180 *
181 * @param parent parent directory to look for a guest
182 * @param name name of the guest directory
183 */
184 guest_t *guest_load(char *parent, char *name);
185
186 #endif /* GUEST_H */
187