criu/test/zdtm/lib/msg.c
Andrey Vagin 5a49b9e216 zdtm: Zero Downtime Migration Test Suite
This test suite contains many small test cases for different subsystems.

Example of execution:
 # make busyloop00.pid
 # ../../../../crtools -d -t `cat busyloop00.pid`
 # kill -9 `cat busyloop00.pid`
 # ../../../../crtools -r -t `cat busyloop00.pid`
 # cat busyloop00.out
PASS

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Acked-by: Pavel Emelyanov <xemul@parallels.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
2011-12-02 17:49:08 +04:00

62 lines
1.3 KiB
C

#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>
#include "zdtmtst.h"
static struct {
char buffer[LOG_BUF_SIZE];
char *ptr;
int left;
} msg_buf = {
.buffer = {},
.ptr = msg_buf.buffer,
.left = sizeof(msg_buf.buffer),
};
void test_msg(const char *format, ...)
{
va_list arg;
int len;
va_start(arg, format);
len = vsnprintf(msg_buf.ptr, msg_buf.left, format, arg);
va_end(arg);
if (len >= msg_buf.left) { /* indicate message buffer overflow */
const static char overflow_mark[] = "\n.@.\n";
msg_buf.left = 0;
msg_buf.ptr = msg_buf.buffer + sizeof(msg_buf.buffer);
strcpy(msg_buf.ptr - sizeof(overflow_mark), overflow_mark);
msg_buf.ptr--; /* correct for terminating '\0' */
return;
}
msg_buf.ptr += len;
msg_buf.left -= len;
}
extern int proc_id;
void dump_msg(const char *fname)
{
if (msg_buf.ptr != msg_buf.buffer) {
int fd;
if (proc_id == 0) {
fd = open(fname, O_WRONLY | O_CREAT | O_EXCL, 0644);
} else {
char fname_child[1000];
snprintf(fname_child,1000,"%s.%d",fname,proc_id);
fd = open(fname_child, O_WRONLY | O_CREAT, 0644);
}
if (fd < 0)
return;
/* ignore errors as there's no way to report them */
write(fd, msg_buf.buffer, msg_buf.ptr - msg_buf.buffer);
out:
close(fd);
}
}