[CRIU] [PATCH] app-emu: Add job test
Andrew Vagin
avagin at parallels.com
Tue Oct 23 03:35:21 EDT 2012
What do you think about test/zdtm/live/static/jobctl00.c
Why cannot it be a part of zdtm?
On Mon, Oct 22, 2012 at 08:18:34PM +0400, Cyrill Gorcunov wrote:
>
> For tty migration testing.
>
> Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
> ---
> test/app-emu/job/Makefile | 10 +++++
> test/app-emu/job/job.c | 90 +++++++++++++++++++++++++++++++++++++++++++++
> test/app-emu/job/job.exp | 51 +++++++++++++++++++++++++
> test/app-emu/job/run.sh | 3 +
> 4 files changed, 154 insertions(+), 0 deletions(-)
> create mode 100644 test/app-emu/job/Makefile
> create mode 100644 test/app-emu/job/job.c
> create mode 100755 test/app-emu/job/job.exp
> create mode 100644 test/app-emu/job/run.sh
>
> diff --git a/test/app-emu/job/Makefile b/test/app-emu/job/Makefile
> new file mode 100644
> index 0000000..e8d572b
> --- /dev/null
> +++ b/test/app-emu/job/Makefile
> @@ -0,0 +1,10 @@
> +all: job
> +
> +%.o: %.c
> + gcc -c $< -o $@
> +
> +job: job.o
> + gcc -o $@ job.o
> +
> +clean:
> + rm -f *.o job
> diff --git a/test/app-emu/job/job.c b/test/app-emu/job/job.c
> new file mode 100644
> index 0000000..e01480a
> --- /dev/null
> +++ b/test/app-emu/job/job.c
> @@ -0,0 +1,90 @@
> +#include <stdlib.h>
> +#include <stdio.h>
> +#include <unistd.h>
> +
> +#include <sys/types.h>
> +#include <sys/stat.h>
> +#include <sys/ioctl.h>
> +
> +#include <fcntl.h>
> +#include <string.h>
> +#include <termios.h>
> +
> +#include <signal.h>
> +
> +#include <dirent.h>
> +
> +int main(int argc, char *argv[])
> +{
> + int pid, gid, sid;
> + int tty_sid, tty_gid;
> + int fd = fileno(stdout);
> + char buf[32];
> + int c = 0;
> + struct dirent *de;
> + DIR *fd_dir;
> +
> + if (!isatty(fd)) {
> + printf("stdout is not tty\n");
> + return -1;
> + }
> +
> + pid = getpid();
> + gid = getgid();
> + sid = getsid(pid);
> +
> + printf("pid %d gid %d sid %d\n",
> + pid, gid, sid);
> +
> + snprintf(buf, sizeof(buf), "/proc/%d/fd", pid);
> + fd_dir = opendir(buf);
> + if (!fd_dir) {
> + printf("cant open %s\n", buf);
> + return -1;
> + }
> +
> + while ((de = readdir(fd_dir))) {
> + int _fd;
> + if (!strcmp(de->d_name, "."))
> + continue;
> + if (!strcmp(de->d_name, ".."))
> + continue;
> +
> + _fd = atoi(de->d_name);
> + if (_fd > 2 && _fd != fd && isatty(_fd)) {
> + close(_fd);
> + printf("Closed %d\n", _fd);
> + }
> + }
> + closedir(fd_dir);
> +
> + if (ioctl(fd, TIOCGSID, &tty_sid) < 0) {
> + printf("cant obtain sid on stdout\n");
> + return -1;
> + }
> + printf("stdout sid = %d\n", tty_sid);
> +
> + if (ioctl(fd, TIOCGPGRP, &tty_gid) < 0) {
> + printf("cant obtain gid on stdout\n");
> + return -1;
> + }
> + printf("stdout gid = %d\n", tty_gid);
> +
> + printf("READY\n");
> +
> + c = 0;
> + while (1) {
> + sleep(1);
> + if (c++ > 10) {
> + printf("Too long for restore\n");
> + exit(-1);
> + }
> +
> + if (getsid(pid) != sid) {
> + printf("ALIVE\n");
> + break;
> + }
> + }
> +
> + return 0;
> +}
> diff --git a/test/app-emu/job/job.exp b/test/app-emu/job/job.exp
> new file mode 100755
> index 0000000..cf743e8
> --- /dev/null
> +++ b/test/app-emu/job/job.exp
> @@ -0,0 +1,51 @@
> +#!/usr/bin/expect
> +
> +exec rm -rf ./dump
> +exec mkdir ./dump
> +
> +system echo "-1" > ./dump/pid.pid
> +
> +set current [fork]
> +switch $current {
> + -1 {
> + puts "Fork failed."
> + exit -1
> + }
> + 0 {
> + set timeout 5
> + spawn ./job
> + set pid [exp_pid]
> + expect "READY" {
> + puts "READY"
> + } timeout {
> + puts "FAIL: Timed out on ready"
> + exit -1
> + }
> + system ../../../crtools dump -v 4 -D ./dump -o dump.log -j -t $pid
> + system echo "$pid" > ./dump/pid.pid
> + exit 0
> + }
> + default {
> + sleep 2
> + set timeout 5
> +
> + set ::pidfile [open ./dump/pid.pid r]
> + set pid [gets $::pidfile]
> +
> + if {$pid == -1} {
> + puts "FAIL: Invalid pid read"
> + exit -1
> + }
> +
> + spawn ../../../crtools restore -v 4 -D ./dump -o restore.log -j -t $pid
> +
> + expect "ALIVE" {
> + puts "PASS"
> + } timeout {
> + puts "FAIL: Timed out"
> + exit -1
> + }
> +
> + exit 0
> + }
> +}
> diff --git a/test/app-emu/job/run.sh b/test/app-emu/job/run.sh
> new file mode 100644
> index 0000000..1d1e0ea
> --- /dev/null
> +++ b/test/app-emu/job/run.sh
> @@ -0,0 +1,3 @@
> +#!/bin/sh
> +
> +exec expect ./job.exp
> _______________________________________________
> CRIU mailing list
> CRIU at openvz.org
> https://openvz.org/mailman/listinfo/criu
More information about the CRIU
mailing list