465
* lxcCheckContainerProcess:
466
* @def: Ptr to VM definition
468
* Checks if the container process (stored at def->id is running
470
* Returns on success or -1 in case of error
471
* 0 - no process with id vm->def->id
472
* 1 - container process exists
475
int lxcCheckContainerProcess(lxc_vm_def_t *def)
480
if (-1 == kill(def->id, 0)) {
481
if (ESRCH == errno) {
483
DEBUG("pid %d no longer exists", def->id);
487
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
488
_("error checking container process: %d %s"),
489
def->id, strerror(errno));
493
DEBUG("pid %d still exists", def->id);
461
504
void lxcRemoveInactiveVM(lxc_driver_t *driver,
689
738
lxc_vm_def_t *def)
691
virBufferPtr buf = 0;
740
virBuffer buf = VIR_BUFFER_INITIALIZER;
692
741
unsigned char *uuid;
693
742
char uuidstr[VIR_UUID_STRING_BUFLEN];
694
743
lxc_mount_t *mount;
696
buf = virBufferNew(LXC_MAX_XML_LENGTH);
701
if (lxcIsActiveVM(vm)) {
702
if (virBufferVSprintf(buf, "<domain type='%s' id='%d'>\n",
703
LXC_DOMAIN_TYPE, vm->def->id) < 0) {
707
if (virBufferVSprintf(buf, "<domain type='%s'>\n",
708
LXC_DOMAIN_TYPE) < 0) {
713
if (virBufferVSprintf(buf, " <name>%s</name>\n", def->name) < 0) {
745
if (lxcIsActiveVM(vm))
746
virBufferVSprintf(&buf, "<domain type='%s' id='%d'>\n",
747
LXC_DOMAIN_TYPE, vm->def->id);
749
virBufferVSprintf(&buf, "<domain type='%s'>\n",
752
virBufferVSprintf(&buf, " <name>%s</name>\n", def->name);
717
754
uuid = def->uuid;
718
755
virUUIDFormat(uuid, uuidstr);
719
if (virBufferVSprintf(buf, " <uuid>%s</uuid>\n", uuidstr) < 0) {
723
if (virBufferAddLit(buf, " <os>\n") < 0) {
727
if (virBufferVSprintf(buf, " <init>%s</init>\n", def->init) < 0) {
731
if (virBufferAddLit(buf, " </os>\n") < 0) {
735
if (virBufferVSprintf(buf, " <memory>%d</memory>\n", def->maxMemory) < 0) {
739
if (virBufferAddLit(buf, " <devices>\n") < 0) {
756
virBufferVSprintf(&buf, " <uuid>%s</uuid>\n", uuidstr);
757
virBufferAddLit(&buf, " <os>\n");
758
virBufferVSprintf(&buf, " <init>%s</init>\n", def->init);
759
virBufferAddLit(&buf, " </os>\n");
760
virBufferVSprintf(&buf, " <memory>%d</memory>\n", def->maxMemory);
761
virBufferAddLit(&buf, " <devices>\n");
743
763
/* loop adding mounts */
744
764
for (mount = def->mounts; mount; mount = mount->next) {
745
if (virBufferAddLit(buf, " <filesystem type='mount'>\n") < 0) {
749
if (virBufferVSprintf(buf, " <source dir='%s'/>\n",
750
mount->source) < 0) {
754
if (virBufferVSprintf(buf, " <target dir='%s'/>\n",
755
mount->target) < 0) {
759
if (virBufferAddLit(buf, " </filesystem>\n") < 0) {
765
if (virBufferVSprintf(buf, " <console tty='%s'/>\n", def->tty) < 0) {
769
if (virBufferAddLit(buf, " </devices>\n") < 0) {
773
if (virBufferAddLit(buf, "</domain>\n") < 0) {
777
return virBufferContentAndFree(buf);
780
lxcError(conn, NULL, VIR_ERR_NO_MEMORY, "generateXml");
765
virBufferAddLit(&buf, " <filesystem type='mount'>\n");
766
virBufferVSprintf(&buf, " <source dir='%s'/>\n",
768
virBufferVSprintf(&buf, " <target dir='%s'/>\n",
770
virBufferAddLit(&buf, " </filesystem>\n");
773
virBufferVSprintf(&buf, " <console tty='%s'/>\n", def->tty);
774
virBufferAddLit(&buf, " </devices>\n");
775
virBufferAddLit(&buf, "</domain>\n");
777
if (virBufferError(&buf)) {
778
lxcError(conn, NULL, VIR_ERR_NO_MEMORY,_("allocate buffer"));
782
return virBufferContentAndReset(&buf);
786
785
void lxcFreeVMDef(lxc_vm_def_t *vmdef)
889
* @driver: pointer to driver
892
* Stores the pid of the tty forward process contained in vm->pid
893
* LOCAL_STATE_DIR/run/libvirt/lxc/{container_name}.pid
895
* Returns 0 on success or -1 in case of error
897
int lxcStoreTtyPid(const lxc_driver_t *driver, lxc_vm_t *vm)
903
if (vm->ttyPidFile[0] == 0x00) {
904
if ((rc = virFileMakePath(driver->stateDir))) {
905
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
906
_("cannot create lxc state directory %s: %s"),
907
driver->stateDir, strerror(rc));
911
if (virFileBuildPath(driver->stateDir, vm->def->name, ".pid",
912
vm->ttyPidFile, PATH_MAX) < 0) {
913
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
914
_("cannot construct tty pid file path"));
919
if ((fd = open(vm->ttyPidFile,
920
O_WRONLY | O_CREAT | O_TRUNC,
921
S_IRUSR | S_IWUSR)) < 0) {
922
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
923
_("cannot create tty pid file %s: %s"),
924
vm->ttyPidFile, strerror(errno));
928
if (!(file = fdopen(fd, "w"))) {
929
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
930
_("cannot fdopen tty pid file %s: %s"),
931
vm->ttyPidFile, strerror(errno));
934
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
935
_("failed to close tty pid file %s: %s"),
936
vm->ttyPidFile, strerror(errno));
942
if (fprintf(file, "%d", vm->pid) < 0) {
943
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
944
_("cannot write tty pid file %s: %s"),
945
vm->ttyPidFile, strerror(errno));
947
goto fclose_error_out;
953
if (fclose(file) < 0) {
954
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
955
_("failed to close tty pid file %s: %s"),
956
vm->ttyPidFile, strerror(errno));
965
* @driver: pointer to driver
968
* Loads the pid of the tty forward process from the pid file.
969
* LOCAL_STATE_DIR/run/libvirt/lxc/{container_name}.pid
972
* > 0 - pid of tty process
973
* 0 - no tty pid file
976
int lxcLoadTtyPid(const lxc_driver_t *driver, lxc_vm_t *vm)
981
if (vm->ttyPidFile[0] == 0x00) {
982
if ((rc = virFileMakePath(driver->stateDir))) {
983
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
984
_("cannot create lxc state directory %s: %s"),
985
driver->stateDir, strerror(rc));
989
if (virFileBuildPath(driver->stateDir, vm->def->name, ".pid",
990
vm->ttyPidFile, PATH_MAX) < 0) {
991
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
992
_("cannot construct tty pid file path"));
997
if (!(file = fopen(vm->ttyPidFile, "r"))) {
998
if (ENOENT == errno) {
1003
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
1004
_("cannot open tty pid file %s: %s"),
1005
vm->ttyPidFile, strerror(errno));
1009
if (fscanf(file, "%d", &(vm->pid)) < 0) {
1010
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
1011
_("cannot read tty pid file %s: %s"),
1012
vm->ttyPidFile, strerror(errno));
1016
if (fclose(file) < 0) {
1017
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
1018
_("failed to close tty pid file %s: %s"),
1019
vm->ttyPidFile, strerror(errno));
1033
* Unlinks the tty pid file for the vm
1034
* LOCAL_STATE_DIR/run/libvirt/lxc/{container_name}.pid
1036
* Returns on 0 success or -1 in case of error
1038
int lxcDeleteTtyPidFile(const lxc_vm_t *vm)
1040
if (vm->ttyPidFile[0] == 0x00) {
1044
if (unlink(vm->ttyPidFile) < 0) {
1045
if (errno == ENOENT) {
1049
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
1050
_("cannot remove ttyPidFile %s: %s"), vm->ttyPidFile,
887
1059
#endif /* WITH_LXC */
892
* indent-tabs-mode: nil