1
0
mirror of https://github.com/lxc/go-lxc.git synced 2026-02-05 15:47:17 +01:00

container: add ConsoleLog() API extension

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
This commit is contained in:
Christian Brauner
2017-11-09 10:24:11 +01:00
parent 74b61dd0a9
commit 00e1448836
4 changed files with 69 additions and 0 deletions

View File

@@ -22,6 +22,7 @@ import (
"strconv"
"strings"
"sync"
"syscall"
"time"
"unsafe"
)
@@ -1866,3 +1867,43 @@ func (c *Container) SetRunningConfigItem(key string, value string) error {
}
return nil
}
// ConsoleLog allows to perform operations on the container's in-memory console
// buffer.
func (c *Container) ConsoleLog(opt ConsoleLogOptions) ([]byte, error) {
c.mu.Lock()
defer c.mu.Unlock()
cl := C.struct_lxc_console_log{
clear: C.bool(opt.ClearLog),
read: C.bool(opt.ReadLog),
data: nil,
write_logfile: C.bool(opt.WriteToLogFile),
}
// CGO is a fickle little beast:
// We need to manually allocate memory here that we pass to C. If we
// were to pass a GO pointer by passing a C.uint64_t pointer we'd end in
// the situation where we have a GO pointer that points to a GO pointer.
// Go will freak out when this happens. So give C its own memory.
var buf unsafe.Pointer
buf = C.malloc(C.sizeof_uint64_t)
if buf == nil {
return nil, syscall.ENOMEM
}
defer C.free(buf)
cl.read_max = (*C.uint64_t)(buf)
*cl.read_max = C.uint64_t(opt.ReadMax)
ret := C.go_lxc_console_log(c.container, &cl)
if ret < 0 {
return nil, syscall.Errno(-ret)
}
numBytes := C.int(*cl.read_max)
if C.uint64_t(numBytes) != *cl.read_max {
return nil, syscall.ERANGE
}
return C.GoBytes(unsafe.Pointer(cl.data), numBytes), nil
}

View File

@@ -458,3 +458,11 @@ bool go_lxc_set_running_config_item(struct lxc_container *c, const char *key, co
return false;
#endif
}
int go_lxc_console_log(struct lxc_container *c, struct lxc_console_log *log) {
#if VERSION_AT_LEAST(3, 0, 0)
return c->console_log(c, log);
#else
return false;
#endif
}

View File

@@ -113,3 +113,15 @@ int go_lxc_migrate(struct lxc_container *c, unsigned int cmd, struct migrate_opt
extern bool go_lxc_attach_interface(struct lxc_container *c, const char *dev, const char *dst_dev);
extern bool go_lxc_detach_interface(struct lxc_container *c, const char *dev, const char *dst_dev);
#if !VERSION_AT_LEAST(3, 0, 0)
struct lxc_console_log {
bool clear;
bool read;
uint64_t *read_max;
char *data;
bool write_logfile;
};
#endif
extern int go_lxc_console_log(struct lxc_container *c, struct lxc_console_log *log);

View File

@@ -198,3 +198,11 @@ type MigrateOptions struct {
PreservesInodes bool
GhostLimit uint64
}
// ConsoleLogOptioins type is used for defining console log options.
type ConsoleLogOptions struct {
ClearLog bool
ReadLog bool
ReadMax uint64
WriteToLogFile bool
}