Cross Reference: /lxc/src/lua-lxc/core.c
core.c revision 67f69573777d35b291c853733752c2c079bf14e8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano/*
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano * lua-lxc: lua bindings for lxc
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano *
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano * Copyright © 2012 Oracle.
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano *
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano * Authors:
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano * Dwight Engen <dwight.engen@oracle.com>
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano *
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano * This library is free software; you can redistribute it and/or
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano * modify it under the terms of the GNU Lesser General Public
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano * License as published by the Free Software Foundation; either
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano * version 2.1 of the License, or (at your option) any later version.
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano * This library is distributed in the hope that it will be useful,
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano * but WITHOUT ANY WARRANTY; without even the implied warranty of
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano * Lesser General Public License for more details.
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano * You should have received a copy of the GNU Lesser General Public
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano * License along with this library; if not, write to the Free Software
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano */
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano#define LUA_LIB
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano#define _GNU_SOURCE
05cda563bff2433c21acf5d13c364d581c34efd6Daniel Lezcano#include <lua.h>
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano#include <lauxlib.h>
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano#include <assert.h>
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano#include <string.h>
4a2ca8b2ba4b9820a3e3e26e2cf081c17d8737f9Serge Hallyn#include <unistd.h>
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano#include <libgen.h>
20d816599f954e7e5864d39884cc0de56f9358fdChristian Seiler#include <lxc/lxccontainer.h>
20d816599f954e7e5864d39884cc0de56f9358fdChristian Seiler#include "lxc/commands.h"
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano#if LUA_VERSION_NUM < 502
596a818d4b8b55586d36af518b745cd96b24c67aDwight Engen#define luaL_newlib(L,l) (lua_newtable(L), luaL_register(L,NULL,l))
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano#define luaL_setfuncs(L,l,n) (assert(n==0), luaL_register(L,NULL,l))
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano#define luaL_checkunsigned(L,n) luaL_checknumber(L,n)
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano#endif
596a818d4b8b55586d36af518b745cd96b24c67aDwight Engen
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano#if LUA_VERSION_NUM >= 503
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano#define luaL_checkunsigned(L,n) ((lua_Unsigned)luaL_checkinteger(L,n))
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano#endif
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano
596a818d4b8b55586d36af518b745cd96b24c67aDwight Engen#ifdef NO_CHECK_UDATA
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano#define checkudata(L,i,tname) lua_touserdata(L, i)
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano#else
596a818d4b8b55586d36af518b745cd96b24c67aDwight Engen#define checkudata(L,i,tname) luaL_checkudata(L, i, tname)
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano#endif
596a818d4b8b55586d36af518b745cd96b24c67aDwight Engen
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano#define lua_boxpointer(L,u) \
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano (*(void **) (lua_newuserdata(L, sizeof(void *))) = (u))
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano
596a818d4b8b55586d36af518b745cd96b24c67aDwight Engen#define lua_unboxpointer(L,i,tname) \
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano (*(void **) (checkudata(L, i, tname)))
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano#define CONTAINER_TYPENAME "lxc.container"
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano
596a818d4b8b55586d36af518b745cd96b24c67aDwight Engenstatic int container_new(lua_State *L)
7d40e69bd7fd3e9eaf120be9f749245e7f48f997Daniel Lezcano{
b3357a6f5b90f1e342c270de66491afc412c1cf7Daniel Lezcano struct lxc_container *c;
const char *name = luaL_checkstring(L, 1);
const char *configpath = NULL;
int argc = lua_gettop(L);
if (argc > 1)
configpath = luaL_checkstring(L, 2);
c = lxc_container_new(name, configpath);
if (c) {
lua_boxpointer(L, c);
luaL_getmetatable(L, CONTAINER_TYPENAME);
lua_setmetatable(L, -2);
} else {
lua_pushnil(L);
}
return 1;
}
static int container_gc(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
/* XXX what to do if this fails? */
lxc_container_put(c);
return 0;
}
static int container_config_file_name(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
char *config_file_name;
config_file_name = c->config_file_name(c);
lua_pushstring(L, config_file_name);
free(config_file_name);
return 1;
}
static int container_defined(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
lua_pushboolean(L, !!c->is_defined(c));
return 1;
}
static int container_name(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
lua_pushstring(L, c->name);
return 1;
}
static int container_create(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
char *template_name = strdupa(luaL_checkstring(L, 2));
int argc = lua_gettop(L);
char **argv;
int i;
argv = alloca((argc+1) * sizeof(char *));
for (i = 0; i < argc-2; i++)
argv[i] = strdupa(luaL_checkstring(L, i+3));
argv[i] = NULL;
lua_pushboolean(L, !!c->create(c, template_name, NULL, NULL, 0, argv));
return 1;
}
static int container_destroy(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
lua_pushboolean(L, !!c->destroy(c));
return 1;
}
/* container state */
static int container_start(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
int argc = lua_gettop(L);
char **argv = NULL;
int i,j;
int useinit = 0;
if (argc > 1) {
argv = alloca((argc+1) * sizeof(char *));
for (i = 0, j = 0; i < argc-1; i++) {
const char *arg = luaL_checkstring(L, i+2);
if (!strcmp(arg, "useinit"))
useinit = 1;
else
argv[j++] = strdupa(arg);
}
argv[j] = NULL;
}
c->want_daemonize(c, true);
lua_pushboolean(L, !!c->start(c, useinit, argv));
return 1;
}
static int container_stop(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
lua_pushboolean(L, !!c->stop(c));
return 1;
}
static int container_shutdown(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
int timeout = luaL_checkinteger(L, 2);
lua_pushboolean(L, !!c->shutdown(c, timeout));
return 1;
}
static int container_wait(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
const char *state = luaL_checkstring(L, 2);
int timeout = luaL_checkinteger(L, 3);
lua_pushboolean(L, !!c->wait(c, state, timeout));
return 1;
}
static int container_freeze(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
lua_pushboolean(L, !!c->freeze(c));
return 1;
}
static int container_unfreeze(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
lua_pushboolean(L, !!c->unfreeze(c));
return 1;
}
static int container_running(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
lua_pushboolean(L, !!c->is_running(c));
return 1;
}
static int container_state(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
lua_pushstring(L, c->state(c));
return 1;
}
static int container_init_pid(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
lua_pushinteger(L, c->init_pid(c));
return 1;
}
/* configuration file methods */
static int container_load_config(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
int arg_cnt = lua_gettop(L);
const char *alt_path = NULL;
if (arg_cnt > 1)
alt_path = luaL_checkstring(L, 2);
lua_pushboolean(L, !!c->load_config(c, alt_path));
return 1;
}
static int container_save_config(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
int arg_cnt = lua_gettop(L);
const char *alt_path = NULL;
if (arg_cnt > 1)
alt_path = luaL_checkstring(L, 2);
lua_pushboolean(L, !!c->save_config(c, alt_path));
return 1;
}
static int container_get_config_path(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
const char *config_path;
config_path = c->get_config_path(c);
lua_pushstring(L, config_path);
return 1;
}
static int container_set_config_path(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
const char *config_path = luaL_checkstring(L, 2);
lua_pushboolean(L, !!c->set_config_path(c, config_path));
return 1;
}
static int container_clear_config_item(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
const char *key = luaL_checkstring(L, 2);
lua_pushboolean(L, !!c->clear_config_item(c, key));
return 1;
}
static int container_get_cgroup_item(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
const char *key = luaL_checkstring(L, 2);
int len;
char *value;
len = c->get_cgroup_item(c, key, NULL, 0);
if (len <= 0)
goto not_found;
value = alloca(sizeof(char)*len + 1);
if (c->get_cgroup_item(c, key, value, len + 1) != len)
goto not_found;
lua_pushstring(L, value);
return 1;
not_found:
lua_pushnil(L);
return 1;
}
static int container_get_config_item(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
const char *key = luaL_checkstring(L, 2);
int len;
char *value;
len = c->get_config_item(c, key, NULL, 0);
if (len <= 0)
goto not_found;
value = alloca(sizeof(char)*len + 1);
if (c->get_config_item(c, key, value, len + 1) != len)
goto not_found;
lua_pushstring(L, value);
return 1;
not_found:
lua_pushnil(L);
return 1;
}
static int container_set_cgroup_item(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
const char *key = luaL_checkstring(L, 2);
const char *value = luaL_checkstring(L, 3);
lua_pushboolean(L, !!c->set_cgroup_item(c, key, value));
return 1;
}
static int container_set_config_item(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
const char *key = luaL_checkstring(L, 2);
const char *value = luaL_checkstring(L, 3);
lua_pushboolean(L, !!c->set_config_item(c, key, value));
return 1;
}
static int container_get_keys(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
const char *key = NULL;
int len;
char *value;
int arg_cnt = lua_gettop(L);
if (arg_cnt > 1)
key = luaL_checkstring(L, 2);
len = c->get_keys(c, key, NULL, 0);
if (len <= 0)
goto not_found;
value = alloca(sizeof(char)*len + 1);
if (c->get_keys(c, key, value, len + 1) != len)
goto not_found;
lua_pushstring(L, value);
return 1;
not_found:
lua_pushnil(L);
return 1;
}
static int container_attach(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
int argc = lua_gettop(L);
char **argv = NULL;
int i;
if (argc > 1) {
argv = alloca((argc+1) * sizeof(char *));
for (i = 0; i < argc-1; i++) {
const char *arg = luaL_checkstring(L, i+2);
argv[i] = strdupa(arg);
}
argv[i] = NULL;
}
else
{
lua_pushnil(L);
return 1;
}
lua_pushboolean(L, !(c->attach_run_wait(c, NULL, argv[0], (const char**)argv)));
return 1;
}
static luaL_Reg lxc_container_methods[] =
{
{"attach", container_attach},
{"create", container_create},
{"defined", container_defined},
{"destroy", container_destroy},
{"init_pid", container_init_pid},
{"name", container_name},
{"running", container_running},
{"state", container_state},
{"freeze", container_freeze},
{"unfreeze", container_unfreeze},
{"start", container_start},
{"stop", container_stop},
{"shutdown", container_shutdown},
{"wait", container_wait},
{"config_file_name", container_config_file_name},
{"load_config", container_load_config},
{"save_config", container_save_config},
{"get_cgroup_item", container_get_cgroup_item},
{"set_cgroup_item", container_set_cgroup_item},
{"get_config_path", container_get_config_path},
{"set_config_path", container_set_config_path},
{"get_config_item", container_get_config_item},
{"set_config_item", container_set_config_item},
{"clear_config_item", container_clear_config_item},
{"get_keys", container_get_keys},
{NULL, NULL}
};
static int lxc_version_get(lua_State *L) {
lua_pushstring(L, VERSION);
return 1;
}
static int lxc_default_config_path_get(lua_State *L) {
const char *lxcpath = lxc_get_global_config_item("lxc.lxcpath");
lua_pushstring(L, lxcpath);
return 1;
}
static int cmd_get_config_item(lua_State *L)
{
int arg_cnt = lua_gettop(L);
const char *name = luaL_checkstring(L, 1);
const char *key = luaL_checkstring(L, 2);
const char *lxcpath = NULL;
char *value;
if (arg_cnt > 2)
lxcpath = luaL_checkstring(L, 3);
value = lxc_cmd_get_config_item(name, key, lxcpath);
if (!value)
goto not_found;
lua_pushstring(L, value);
return 1;
not_found:
lua_pushnil(L);
return 1;
}
/* utility functions */
static int lxc_util_usleep(lua_State *L) {
usleep((useconds_t)luaL_checkunsigned(L, 1));
return 0;
}
static int lxc_util_dirname(lua_State *L) {
char *path = strdupa(luaL_checkstring(L, 1));
lua_pushstring(L, dirname(path));
return 1;
}
static luaL_Reg lxc_lib_methods[] = {
{"version_get", lxc_version_get},
{"default_config_path_get", lxc_default_config_path_get},
{"cmd_get_config_item", cmd_get_config_item},
{"container_new", container_new},
{"usleep", lxc_util_usleep},
{"dirname", lxc_util_dirname},
{NULL, NULL}
};
static int lxc_lib_uninit(lua_State *L) {
(void) L;
/* this is where we would fini liblxc.so if we needed to */
return 0;
}
LUALIB_API int luaopen_lxc_core(lua_State *L) {
/* this is where we would initialize liblxc.so if we needed to */
luaL_newlib(L, lxc_lib_methods);
lua_newuserdata(L, 0);
lua_newtable(L); /* metatable */
lua_pushvalue(L, -1);
lua_pushliteral(L, "__gc");
lua_pushcfunction(L, lxc_lib_uninit);
lua_rawset(L, -3);
lua_setmetatable(L, -3);
lua_rawset(L, -3);
luaL_newmetatable(L, CONTAINER_TYPENAME);
luaL_setfuncs(L, lxc_container_methods, 0);
lua_pushvalue(L, -1); /* push metatable */
lua_pushstring(L, "__gc");
lua_pushcfunction(L, container_gc);
lua_settable(L, -3);
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
lua_pop(L, 1);
return 1;
}