/** Allocate and initialize CT handler. * * @param veid CT ID. * @return handler or NULL on error. */ vps_handler *vz_open(envid_t veid) { int vzfd, stdfd; vps_handler *h = NULL; stdfd = reset_std(); if ((vzfd = open(VZCTLDEV, O_RDWR)) < 0) { logger(-1, errno, "Unable to open %s", VZCTLDEV); logger(-1, 0, "Please check that vzdev kernel module is loaded" " and you have sufficient permissions" " to access the file."); goto err; } h = calloc(1, sizeof(*h)); if (h == NULL) goto err; h->vzfd = vzfd; h->stdfd = stdfd; if (vz_env_create_ioctl(h, 0, 0) < 0 && (errno == ENOSYS || errno == EPERM)) { logger(-1, 0, "Your kernel lacks support for virtual" " environments or modules not loaded"); goto err; } return h; err: free(h); if (vzfd != -1) close(vzfd); if (stdfd != -1) close(stdfd); return NULL; }
/** Get CT status. * * @param h CT handler. * @param veid CT ID. * @return 1 - CT is running * 0 - CT is stopped. */ int vps_is_run(vps_handler *h, envid_t veid) { int ret; int errno; if (veid == 0) return 1; ret = vz_env_create_ioctl(h, veid, VE_TEST); if (ret < 0 && (errno == ESRCH || errno == ENOTTY)) return 0; else if (ret < 0) logger(-1, errno, "error on vz_env_create_ioctl(VE_TEST)"); return 1; }
static int real_env_stop(vps_handler *h, envid_t veid, const char *vps_root, int stop_mode) { int ret; if ((ret = vz_chroot(vps_root))) return ret; if ((ret = vz_setluid(veid))) return ret; close_fds(1, h->vzfd, -1); if ((ret = vz_env_create_ioctl(h, veid, VE_ENTER)) < 0) { if (errno == ESRCH) return 0; logger(-1, errno, "Container enter failed"); return ret; } close(h->vzfd); switch (stop_mode) { case M_REBOOT: { char *argv[] = {"reboot", NULL}; execvep(argv[0], argv, NULL); break; } case M_HALT: { char *argv[] = {"halt", NULL}; execvep(argv[0], argv, NULL); break; } case M_KILL: { syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_POWER_OFF, NULL); break; } } return 0; }
int main(int argc, char **argv) { int veid, vzfd, ret; int qwe = 10; int cld_pid; if (argc == 1) { printf("Specify container ID\n"); return -1; } veid = atoi(argv[1]); if ((vzfd = open("/dev/vzctl", O_RDWR)) < 0) { perror("open vzctl"); return -1; } // printf("Press any to create child and proceed...\n"); if (cld_pid = fork()) { int status; printf("Waiting for child with pid %d.\n", cld_pid); wait(&status); printf("Child terminated.\n"); } else { int cld_cnt_pid, cnt_status; printf("Entering container %d...\n", veid); if ((ret = vz_setluid(veid))) return ret; ret = vz_env_create_ioctl(vzfd, veid, VE_ENTER); if (ret < 0) { printf("Failed to enter container %d\n", veid); if (errno == ESRCH) ret = VZ_VE_NOT_RUNNING; else ret = VZ_ENVCREATE_ERROR; goto env_err; } printf("Successfully entered container %d.\n", veid); printf("Creating child inside container...\n"); if (cld_cnt_pid = fork()) { /* while(qwe--) { printf("Sleeping for 1 second...\n"); sleep(1); printf("%d seconds left...\n", qwe); } */ printf("Child: Waiting for child with pid %d created inside container...\n", cld_cnt_pid); wait(&cnt_status); printf("Child: Child created inside container - terminated.\n"); } else { int i = 0; printf("Child inside continer created successfully. Sleep for 1 sec.\n"); if (daemon(0, 0) == -1) { perror("daemonize failed:"); return -1; } while(i++ < 10) { printf("Sleeping for 1 second...\n"); sleep(1); } return 0; } } return 0; env_err: return -1; }