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 <unistd.h>
17 #include <stdlib.h>
18 
19 #include "private.h"
20 
all_restrict_cb(Xentoolcore__Active_Handle * ah,domid_t domid)21 static int all_restrict_cb(Xentoolcore__Active_Handle *ah, domid_t domid) {
22     xenevtchn_handle *xce = CONTAINER_OF(ah, *xce, tc_ah);
23 
24     if (xce->fd < 0)
25         /* just in case */
26         return 0;
27 
28     return xenevtchn_restrict(xce, domid);
29 }
30 
xenevtchn_open(xentoollog_logger * logger,unsigned open_flags)31 xenevtchn_handle *xenevtchn_open(xentoollog_logger *logger, unsigned open_flags)
32 {
33     xenevtchn_handle *xce = malloc(sizeof(*xce));
34     int rc;
35 
36     if (!xce) return NULL;
37 
38     xce->fd = -1;
39     xce->logger = logger;
40     xce->logger_tofree  = NULL;
41 
42     xce->tc_ah.restrict_callback = all_restrict_cb;
43     xentoolcore__register_active_handle(&xce->tc_ah);
44 
45     if (!xce->logger) {
46         xce->logger = xce->logger_tofree =
47             (xentoollog_logger*)
48             xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
49         if (!xce->logger) goto err;
50     }
51 
52     rc = osdep_evtchn_open(xce);
53     if ( rc  < 0 ) goto err;
54 
55     return xce;
56 
57 err:
58     xentoolcore__deregister_active_handle(&xce->tc_ah);
59     osdep_evtchn_close(xce);
60     xtl_logger_destroy(xce->logger_tofree);
61     free(xce);
62     return NULL;
63 }
64 
xenevtchn_close(xenevtchn_handle * xce)65 int xenevtchn_close(xenevtchn_handle *xce)
66 {
67     int rc;
68 
69     if ( !xce )
70         return 0;
71 
72     xentoolcore__deregister_active_handle(&xce->tc_ah);
73     rc = osdep_evtchn_close(xce);
74     xtl_logger_destroy(xce->logger_tofree);
75     free(xce);
76     return rc;
77 }
78 
xenevtchn_restrict(xenevtchn_handle * xce,domid_t domid)79 int xenevtchn_restrict(xenevtchn_handle *xce, domid_t domid)
80 {
81     return osdep_evtchn_restrict(xce, domid);
82 }
83 
84 /*
85  * Local variables:
86  * mode: C
87  * c-file-style: "BSD"
88  * c-basic-offset: 4
89  * tab-width: 4
90  * indent-tabs-mode: nil
91  * End:
92  */
93