| 1 |
/* |
| 2 |
* This library is free software; you can redistribute it and/or |
| 3 |
* modify it under the terms of the GNU Lesser General Public |
| 4 |
* License as published by the Free Software Foundation; |
| 5 |
* version 2.1 of the License. |
| 6 |
* |
| 7 |
* This library is distributed in the hope that it will be useful, |
| 8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 10 |
* Lesser General Public License for more details. |
| 11 |
* |
| 12 |
* You should have received a copy of the GNU Lesser General Public |
| 13 |
* License along with this library; If not, see <http://www.gnu.org/licenses/>. |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <errno.h> |
| 17 |
#include <unistd.h> |
| 18 |
#include <stdlib.h> |
| 19 |
|
| 20 |
#include "private.h" |
| 21 |
|
| 22 |
static int all_restrict_cb(Xentoolcore__Active_Handle *ah, domid_t domid) |
| 23 |
{ |
| 24 |
xenevtchn_handle *xce = CONTAINER_OF(ah, *xce, tc_ah); |
| 25 |
|
| 26 |
if ( xce->fd < 0 ) |
| 27 |
/* just in case */ |
| 28 |
return 0; |
| 29 |
|
| 30 |
return xenevtchn_restrict(xce, domid); |
| 31 |
} |
| 32 |
|
| 33 |
static xenevtchn_handle *xenevtchn_alloc_handle(xentoollog_logger *logger) |
| 34 |
{ |
| 35 |
xenevtchn_handle *xce = malloc(sizeof(*xce)); |
| 36 |
|
| 37 |
if ( !xce ) |
| 38 |
return NULL; |
| 39 |
|
| 40 |
xce->fd = -1; |
| 41 |
xce->logger = logger; |
| 42 |
xce->logger_tofree = NULL; |
| 43 |
|
| 44 |
xce->tc_ah.restrict_callback = all_restrict_cb; |
| 45 |
xentoolcore__register_active_handle(&xce->tc_ah); |
| 46 |
|
| 47 |
if ( !xce->logger ) |
| 48 |
{ |
| 49 |
xce->logger = xce->logger_tofree = (xentoollog_logger *) |
| 50 |
xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0); |
| 51 |
if ( !xce->logger ) |
| 52 |
goto err; |
| 53 |
} |
| 54 |
|
| 55 |
return xce; |
| 56 |
|
| 57 |
err: |
| 58 |
xenevtchn_close(xce); |
| 59 |
return NULL; |
| 60 |
} |
| 61 |
|
| 62 |
xenevtchn_handle *xenevtchn_open(xentoollog_logger *logger, unsigned int flags) |
| 63 |
{ |
| 64 |
xenevtchn_handle *xce; |
| 65 |
int rc; |
| 66 |
|
| 67 |
if ( flags & ~XENEVTCHN_NO_CLOEXEC ) |
| 68 |
{ |
| 69 |
errno = EINVAL; |
| 70 |
return NULL; |
| 71 |
} |
| 72 |
|
| 73 |
xce = xenevtchn_alloc_handle(logger); |
| 74 |
if ( !xce ) |
| 75 |
return NULL; |
| 76 |
|
| 77 |
rc = osdep_evtchn_open(xce, flags); |
| 78 |
if ( rc < 0 ) |
| 79 |
goto err; |
| 80 |
|
| 81 |
return xce; |
| 82 |
|
| 83 |
err: |
| 84 |
xenevtchn_close(xce); |
| 85 |
return NULL; |
| 86 |
} |
| 87 |
|
| 88 |
xenevtchn_handle *xenevtchn_fdopen(struct xentoollog_logger *logger, |
| 89 |
int fd, unsigned int flags) |
| 90 |
{ |
| 91 |
xenevtchn_handle *xce; |
| 92 |
|
| 93 |
if ( flags ) |
| 94 |
{ |
| 95 |
errno = EINVAL; |
| 96 |
return NULL; |
| 97 |
} |
| 98 |
|
| 99 |
xce = xenevtchn_alloc_handle(logger); |
| 100 |
if ( !xce ) |
| 101 |
return NULL; |
| 102 |
|
| 103 |
xce->fd = fd; |
| 104 |
|
| 105 |
return xce; |
| 106 |
} |
| 107 |
|
| 108 |
int xenevtchn_close(xenevtchn_handle *xce) |
| 109 |
{ |
| 110 |
int rc; |
| 111 |
|
| 112 |
if ( !xce ) |
| 113 |
return 0; |
| 114 |
|
| 115 |
xentoolcore__deregister_active_handle(&xce->tc_ah); |
| 116 |
rc = osdep_evtchn_close(xce); |
| 117 |
xtl_logger_destroy(xce->logger_tofree); |
| 118 |
free(xce); |
| 119 |
|
| 120 |
return rc; |
| 121 |
} |
| 122 |
|
| 123 |
int xenevtchn_fd(xenevtchn_handle *xce) |
| 124 |
{ |
| 125 |
return xce->fd; |
| 126 |
} |
| 127 |
|
| 128 |
int xenevtchn_restrict(xenevtchn_handle *xce, domid_t domid) |
| 129 |
{ |
| 130 |
return osdep_evtchn_restrict(xce, domid); |
| 131 |
} |
| 132 |
|
| 133 |
/* |
| 134 |
* Local variables: |
| 135 |
* mode: C |
| 136 |
* c-file-style: "BSD" |
| 137 |
* c-basic-offset: 4 |
| 138 |
* tab-width: 4 |
| 139 |
* indent-tabs-mode: nil |
| 140 |
* End: |
| 141 |
*/ |