You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dear all,
I'm trying to use the attach() function to attach to a container and run a function inside it, capturing the output. My idea was to create a pipe() of two file descriptors, and set the lxc_attach_options standard output file descriptor to the write end of the pipe, and read from the read end of it. However, that doesn't work, as this code here demonstrates. Compile it with
gcc -o lxctest lxctest.c -llxc
and run it with
sudo ./lxctest running_container_name
How can I make this work and actually capture data from the running function? Any help would be appreciated.
#include<lxc/lxccontainer.h>#include<lxc/attach_options.h>#include<errno.h> [0/89]
#include<string.h>#include<assert.h>#include<unistd.h>// Run a function inside the container and capture the output.voidrun_function(structlxc_container*container, lxc_attach_exec_tfunction, void*data) {
// default options and PIDlxc_attach_options_toptions=LXC_ATTACH_OPTIONS_DEFAULT;
pid_tpid;
// create a pipe to collect stdoutintpipefd[2];
assert(0==pipe(pipefd));
options.stdout_fd=pipefd[1];
//options.attach_flags |= LXC_ATTACH_TERMINAL;// attach to container, run functionassert(0==container->attach(container, function, data, &options, &pid));
//close(pipefd[1]);// read in datasize_tbuffer_size=1024;
size_tbuffer_pos=0;
char*buffer=malloc(buffer_size);
while(true) {
if(!(buffer_size-buffer_pos)) {
buffer_size *= 2;
buffer=realloc(buffer, buffer_size);
assert(buffer);
}
ssize_tret=read(pipefd[0], buffer+buffer_pos, buffer_size-buffer_pos);
assert(ret >= 0);
printf("just read something\n");
// read end of fileif(!ret) {
break;
} else {
buffer_pos+=ret;
}
}
buffer[buffer_pos] =0;
printf("Read size: %zu\n", buffer_pos);
printf("Read: %s\n", buffer);
}
inthello_world(void*data) {
printf("Hello, World!\n");
}
intmain(intargc, char*argv[]) {
if(argc!=2) {
printf("Usage: %s <container>\n", argv[0]);
return0;
}
char**names;
structlxc_container**containers;
intamount=list_all_containers("/var/lib/lxc", &names, &containers);
assert(amount>0);
size_ti;
for(i=0; i<amount; i++) {
if(0==strcmp(argv[1], names[i])) {
break;
}
}
assert(i<amount);
run_function(containers[i], hello_world, NULL);
return0;
}
The text was updated successfully, but these errors were encountered:
Dear all,
I'm trying to use the
attach()
function to attach to a container and run a function inside it, capturing the output. My idea was to create apipe()
of two file descriptors, and set thelxc_attach_options
standard output file descriptor to the write end of the pipe, and read from the read end of it. However, that doesn't work, as this code here demonstrates. Compile it withand run it with
How can I make this work and actually capture data from the running function? Any help would be appreciated.
The text was updated successfully, but these errors were encountered: