2013-03-20 05:27:32 +00:00
|
|
|
/*
|
|
|
|
* The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
|
|
|
|
*
|
|
|
|
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
2013-04-22 03:08:09 +00:00
|
|
|
#include <errno.h>
|
2013-03-20 05:27:32 +00:00
|
|
|
#include <libvchan.h>
|
|
|
|
|
2013-04-22 03:08:09 +00:00
|
|
|
int wait_for_vchan_or_argfd_once(libvchan_t *ctrl, int max, fd_set * rdset, fd_set * wrset)
|
2013-03-20 05:27:32 +00:00
|
|
|
{
|
|
|
|
int vfd, ret;
|
|
|
|
struct timespec tv = { 1, 100000000 };
|
|
|
|
sigset_t empty_set;
|
|
|
|
|
|
|
|
sigemptyset(&empty_set);
|
|
|
|
|
|
|
|
vfd = libvchan_fd_for_select(ctrl);
|
|
|
|
FD_SET(vfd, rdset);
|
|
|
|
if (vfd > max)
|
|
|
|
max = vfd;
|
|
|
|
max++;
|
|
|
|
ret = pselect(max, rdset, wrset, NULL, &tv, &empty_set);
|
|
|
|
if (ret < 0) {
|
|
|
|
if (errno != EINTR) {
|
|
|
|
perror("select");
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
FD_ZERO(rdset);
|
|
|
|
FD_ZERO(wrset);
|
|
|
|
fprintf(stderr, "eintr\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-04-22 03:08:09 +00:00
|
|
|
if (!libvchan_is_open(ctrl)) {
|
2013-03-20 05:27:32 +00:00
|
|
|
fprintf(stderr, "libvchan_is_eof\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if (FD_ISSET(vfd, rdset))
|
|
|
|
// the following will never block; we need to do this to
|
|
|
|
// clear libvchan_fd pending state
|
|
|
|
libvchan_wait(ctrl);
|
2013-06-07 03:22:13 +00:00
|
|
|
if (libvchan_data_ready(ctrl))
|
|
|
|
return 1;
|
2013-03-20 05:27:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-22 03:08:09 +00:00
|
|
|
void wait_for_vchan_or_argfd(libvchan_t *ctrl, int max, fd_set * rdset, fd_set * wrset)
|
2013-03-20 05:27:32 +00:00
|
|
|
{
|
|
|
|
fd_set r = *rdset, w = *wrset;
|
|
|
|
do {
|
|
|
|
*rdset = r;
|
|
|
|
*wrset = w;
|
|
|
|
}
|
2013-04-22 03:08:09 +00:00
|
|
|
while (wait_for_vchan_or_argfd_once(ctrl, max, rdset, wrset) == 0);
|
2013-03-20 05:27:32 +00:00
|
|
|
}
|
2013-12-27 04:53:19 +00:00
|
|
|
|
2014-06-26 01:10:25 +00:00
|
|
|
int write_vchan_all(libvchan_t *vchan, const void *data, size_t size) {
|
|
|
|
size_t pos;
|
|
|
|
int ret;
|
2013-12-27 04:53:19 +00:00
|
|
|
|
|
|
|
pos = 0;
|
|
|
|
while (pos < size) {
|
|
|
|
ret = libvchan_write(vchan, data+pos, size-pos);
|
|
|
|
if (ret < 0)
|
|
|
|
return 0;
|
|
|
|
pos += ret;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int read_vchan_all(libvchan_t *vchan, void *data, size_t size) {
|
2014-06-26 01:10:25 +00:00
|
|
|
size_t pos;
|
|
|
|
int ret;
|
2013-12-27 04:53:19 +00:00
|
|
|
|
|
|
|
pos = 0;
|
|
|
|
while (pos < size) {
|
|
|
|
ret = libvchan_read(vchan, data+pos, size-pos);
|
|
|
|
if (ret < 0)
|
|
|
|
return 0;
|
|
|
|
pos += ret;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|