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 <stdlib.h>
17 
18 #include "private.h"
19 
all_restrict_cb(Xentoolcore__Active_Handle * ah,domid_t domid)20 static int all_restrict_cb(Xentoolcore__Active_Handle *ah, domid_t domid) {
21     xencall_handle *xcall = CONTAINER_OF(ah, *xcall, tc_ah);
22     int rc;
23 
24     rc = xentoolcore__restrict_by_dup2_null(xcall->buf_fd);
25     if ( rc )
26         goto out;
27 
28     rc = xentoolcore__restrict_by_dup2_null(xcall->fd);
29 
30 out:
31     return rc;
32 }
33 
xencall_open(xentoollog_logger * logger,unsigned open_flags)34 xencall_handle *xencall_open(xentoollog_logger *logger, unsigned open_flags)
35 {
36     xencall_handle *xcall = malloc(sizeof(*xcall));
37     int rc;
38 
39     if (!xcall) return NULL;
40 
41     xcall->fd = -1;
42     xcall->buf_fd = -1;
43     xcall->tc_ah.restrict_callback = all_restrict_cb;
44     xentoolcore__register_active_handle(&xcall->tc_ah);
45 
46     xcall->flags = open_flags;
47     xcall->buffer_cache_nr = 0;
48 
49     xcall->buffer_total_allocations = 0;
50     xcall->buffer_total_releases = 0;
51     xcall->buffer_current_allocations = 0;
52     xcall->buffer_maximum_allocations = 0;
53     xcall->buffer_cache_hits = 0;
54     xcall->buffer_cache_misses = 0;
55     xcall->buffer_cache_toobig = 0;
56     xcall->logger = logger;
57     xcall->logger_tofree = NULL;
58 
59     if (!xcall->logger) {
60         xcall->logger = xcall->logger_tofree =
61             (xentoollog_logger*)
62             xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
63         if (!xcall->logger) goto err;
64     }
65 
66     rc = osdep_xencall_open(xcall);
67     if ( rc  < 0 ) goto err;
68 
69     return xcall;
70 
71 err:
72     xentoolcore__deregister_active_handle(&xcall->tc_ah);
73     osdep_xencall_close(xcall);
74     xtl_logger_destroy(xcall->logger_tofree);
75     free(xcall);
76     return NULL;
77 }
78 
xencall_close(xencall_handle * xcall)79 int xencall_close(xencall_handle *xcall)
80 {
81     int rc;
82 
83     if ( !xcall )
84         return 0;
85 
86     xentoolcore__deregister_active_handle(&xcall->tc_ah);
87     rc = osdep_xencall_close(xcall);
88     buffer_release_cache(xcall);
89     xtl_logger_destroy(xcall->logger_tofree);
90     free(xcall);
91     return rc;
92 }
93 
xencall_fd(xencall_handle * xcall)94 int xencall_fd(xencall_handle *xcall)
95 {
96     return xcall->fd;
97 }
98 
xencall0(xencall_handle * xcall,unsigned int op)99 int xencall0(xencall_handle *xcall, unsigned int op)
100 {
101     privcmd_hypercall_t call = {
102         .op = op,
103     };
104 
105     return osdep_hypercall(xcall, &call);
106 }
107 
xencall1(xencall_handle * xcall,unsigned int op,uint64_t arg1)108 int xencall1(xencall_handle *xcall, unsigned int op,
109              uint64_t arg1)
110 {
111     privcmd_hypercall_t call = {
112         .op = op,
113         .arg = { arg1 },
114     };
115 
116     return osdep_hypercall(xcall, &call);
117 }
118 
xencall2(xencall_handle * xcall,unsigned int op,uint64_t arg1,uint64_t arg2)119 int xencall2(xencall_handle *xcall, unsigned int op,
120              uint64_t arg1, uint64_t arg2)
121 {
122     privcmd_hypercall_t call = {
123         .op = op,
124         .arg = { arg1, arg2 },
125     };
126 
127     return osdep_hypercall(xcall, &call);
128 }
129 
xencall3(xencall_handle * xcall,unsigned int op,uint64_t arg1,uint64_t arg2,uint64_t arg3)130 int xencall3(xencall_handle *xcall, unsigned int op,
131              uint64_t arg1, uint64_t arg2, uint64_t arg3)
132 {
133     privcmd_hypercall_t call = {
134         .op = op,
135         .arg = { arg1, arg2, arg3},
136     };
137 
138     return osdep_hypercall(xcall, &call);
139 }
140 
xencall4(xencall_handle * xcall,unsigned int op,uint64_t arg1,uint64_t arg2,uint64_t arg3,uint64_t arg4)141 int xencall4(xencall_handle *xcall, unsigned int op,
142              uint64_t arg1, uint64_t arg2, uint64_t arg3,
143              uint64_t arg4)
144 {
145     privcmd_hypercall_t call = {
146         .op = op,
147         .arg = { arg1, arg2, arg3, arg4 },
148     };
149 
150     return osdep_hypercall(xcall, &call);
151 }
152 
xencall5(xencall_handle * xcall,unsigned int op,uint64_t arg1,uint64_t arg2,uint64_t arg3,uint64_t arg4,uint64_t arg5)153 int xencall5(xencall_handle *xcall, unsigned int op,
154              uint64_t arg1, uint64_t arg2, uint64_t arg3,
155              uint64_t arg4, uint64_t arg5)
156 {
157     privcmd_hypercall_t call = {
158         .op = op,
159         .arg = { arg1, arg2, arg3, arg4, arg5 },
160     };
161 
162     return osdep_hypercall(xcall, &call);
163 }
164 
165 /*
166  * Local variables:
167  * mode: C
168  * c-file-style: "BSD"
169  * c-basic-offset: 4
170  * tab-width: 4
171  * indent-tabs-mode: nil
172  * End:
173  */
174