1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
/* cvm/client_xfer_command.c - CVM client command transmission library
* Copyright (C)2008 Bruce Guenter <bruce@untroubled.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sys/types.h>
#include <netdb.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sysdeps.h>
#include "v1client.h"
static pid_t pid;
/* Command module execution **************************************************/
static int pipefork(const char* cmd, int pipes[2])
{
int pipe1[2];
int pipe2[2];
if (pipe(pipe1) == -1 || pipe(pipe2) == -2) return 0;
pid = fork();
switch (pid) {
case -1:
return 0;
case 0:
close(0);
close(pipe1[1]);
dup2(pipe1[0], 0);
close(pipe1[0]);
close(1);
close(pipe2[0]);
dup2(pipe2[1], 1);
close(pipe2[1]);
execlp(cmd, cmd, 0);
exit(1);
default:
close(pipe1[0]);
pipes[0] = pipe1[1];
close(pipe2[1]);
pipes[1] = pipe2[0];
return 1;
}
}
static void killit(void)
{
if (pid != -1)
kill(pid, SIGTERM);
}
static int waitforit(void)
{
int status;
pid_t tmp;
while ((tmp = wait(&status)) != -1) {
if (tmp == pid)
return WIFEXITED(status) ? WEXITSTATUS(status) : -CVME_IO;
}
return -CVME_IO;
}
static int write_buffer(int fd, const unsigned char* buffer, unsigned buflen)
{
size_t wr;
while (buflen > 0) {
wr = write(fd, buffer, buflen);
if (wr == 0 || wr == (unsigned)-1) return 0;
buflen -= wr;
buffer += wr;
}
return 1;
}
static unsigned read_buffer(int fd, unsigned char* buffer)
{
size_t rd;
unsigned buflen;
for (buflen = 0; buflen < CVM_BUFSIZE; buffer += rd, buflen += rd) {
rd = read(fd, buffer, CVM_BUFSIZE-buflen);
if (rd == (unsigned)-1) return 0;
if (rd == 0) break;
}
return buflen;
}
unsigned cvm_xfer_command_packets(const char* module,
const struct cvm_packet* request,
struct cvm_packet* response)
{
int pipes[2];
int result;
if (!pipefork(module, pipes)) return CVME_IO;
if (!write_buffer(pipes[0], request->data, request->length) ||
close(pipes[0]) == -1 ||
(response->length = read_buffer(pipes[1], response->data)) == 0 ||
close(pipes[1]) == -1) {
killit();
if ((result = waitforit()) < 0)
return -result;
return CVME_IO;
}
if ((result = waitforit()) < 0)
return -result;
response->data[0] = result;
return 0;
}
|