From 4f00cb634653a64077acfc3e90f93cc7f69b6410 Mon Sep 17 00:00:00 2001 From: Thomas Richter Date: Thu, 22 Feb 2018 14:47:44 +0000 Subject: [PATCH] vmur: Use libvmcp and remove local vmcp code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use the new libvmcp.a to send commands to z/VM CP. The error messages remain unchanged. Signed-off-by: Thomas Richter Reviewed-by: Hendrik Brueckner Reviewed-by: Jan Höppner Signed-off-by: Jan Höppner --- vmur/Makefile | 2 +- vmur/vmur.cpp | 102 +++++++++++++++++++++----------------------------- vmur/vmur.h | 8 ---- 3 files changed, 44 insertions(+), 68 deletions(-) diff --git a/vmur/Makefile b/vmur/Makefile index dd6799dd..1a6bddc0 100644 --- a/vmur/Makefile +++ b/vmur/Makefile @@ -6,7 +6,7 @@ LDLIBS += -lz all: vmur libs = $(rootdir)/libvmdump/libvmdump.a \ - $(rootdir)/libutil/libutil.a + $(rootdir)/libvmcp/libvmcp.a $(rootdir)/libutil/libutil.a objects = vmur.o diff --git a/vmur/vmur.cpp b/vmur/vmur.cpp index eb77e6c7..7de6fc4a 100644 --- a/vmur/vmur.cpp +++ b/vmur/vmur.cpp @@ -32,9 +32,12 @@ #include "lib/vmdump.h" #include "lib/zt_common.h" #include "lib/util_libc.h" +#include "lib/vmcp.h" #include "vmur.h" +#define CP_PREFIX_LEN 11 + /* Program name */ static char *prog_name; @@ -244,24 +247,6 @@ fail: ERR_EXIT("Could not initialize signal handler (errno = %i)\n", errno); } -/* - * Read at most COUNT bytes from FD into memory at location BUF. - * Return number of bytes read on success, -1 on error. - */ -static ssize_t read_buf(int fd, char *buf, ssize_t count) -{ - ssize_t rc, len; - - for (len = 0; len < count; len += rc) { - rc = read(fd, &buf[len], count - len); - if (rc == -1) - return -1; - if (rc == 0) - break; - } - return len; -} - /* * Strip leading CP header from error message */ @@ -294,64 +279,63 @@ static void cperr_exit(char *cpcmd, int cprc, char *buf) static void _cpcmd(char *cpcmd, char **resp, int *rc, int retry, int upper) { - int fd, len, cprc, bufsize = VMCP_BUFSIZE; - char *buf; + struct vmcp_parm cp; char cmd[MAXCMDLEN]; + int ret; strcpy(cmd, cpcmd); - if (upper) - to_upper(cmd); + cp.cpcmd = cmd; + cp.do_upper = upper; + cp.buffer_size = VMCP_DEFAULT_BUFSZ; - fd = open(VMCP_DEVICE_NODE, O_RDWR); - if (fd == -1) +retry: + ret = vmcp(&cp); + + switch (ret) { + case VMCP_ERR_OPEN: ERR_EXIT("Could not issue CP command: \"%s\"\n" "Ensure that vmcp kernel module is loaded!\n", cmd); - do { - if (ioctl(fd, VMCP_SETBUF, &bufsize) == -1) - goto fail; - - if (write(fd, cmd, strlen(cmd)) == -1) - goto fail; - - if (ioctl(fd, VMCP_GETCODE, &cprc) == -1) - goto fail; - - if (ioctl(fd, VMCP_GETSIZE, &len) == -1) - goto fail; - - if (len <= bufsize) - break; - else if (retry) - bufsize = len; - else - ERR_EXIT("Not enough buffer space (%i/%i) for CP " - "command '%s'.\nSorry, please issue command " - "on your 3270 console!\n", len, bufsize, cmd); - } while (1); - - buf = (char *) malloc(len + 1); - if (!buf) - ERR_EXIT("Out of memory for CP command '%s'\n", cmd); - - memset(buf, 0, len + 1); - if (read_buf(fd, buf, len) == -1) + case VMCP_ERR_SETBUF: goto fail; + case VMCP_ERR_WRITE: + goto fail; + + case VMCP_ERR_GETCODE: + goto fail; + + case VMCP_ERR_GETSIZE: + goto fail; + + case VMCP_ERR_READ: + goto fail; + + case VMCP_ERR_TOOSMALL: + if (retry) { + cp.buffer_size = cp.response_size; + free(cp.response); + goto retry; + } + ERR_EXIT("Not enough buffer space (%u/%u) for CP " + "command '%s'.\nSorry, please issue command " + " on your 3270 console!\n", cp.response_size, + cp.buffer_size, cmd); + } + if (rc == NULL) { - if (cprc != 0) { + if (cp.cprc != 0) { /* caller wants us to handle the error */ - cperr_exit(cmd, cprc, buf); + cperr_exit(cmd, cp.cprc, cp.response); } } else { - *rc = cprc; + *rc = cp.cprc; } if (resp) - *resp = buf; + *resp = cp.response; else - free(buf); - close(fd); + free(cp.response); return; fail: diff --git a/vmur/vmur.h b/vmur/vmur.h index 581d9a1c..254135f8 100644 --- a/vmur/vmur.h +++ b/vmur/vmur.h @@ -31,14 +31,6 @@ do { \ ERR_EXIT(str " can only be specified once.\n"); \ } while (0) -#define VMCP_DEVICE_NODE "/dev/vmcp" -#define VMCP_BUFSIZE 0x4000 -#define VMCP_GETSIZE _IOR(0x10, 3, int) -#define VMCP_SETBUF _IOW(0x10, 2, int) -#define VMCP_GETCODE _IOR(0x10, 1, int) - -#define CP_PREFIX_LEN 11 - #define VMRDR_DEVICE_NODE "/dev/vmrdr-0.0.000c" #define VMPUN_DEVICE_NODE "/dev/vmpun-0.0.000d" #define VMPRT_DEVICE_NODE "/dev/vmprt-0.0.000e"