1 /*
2     Internal interfaces for Xen Store Daemon.
3     Copyright (C) 2005 Rusty Russell IBM Corporation
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program; If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef _XENSTORED_CORE_H
20 #define _XENSTORED_CORE_H
21 
22 #define XC_WANT_COMPAT_MAP_FOREIGN_API
23 #include <xenctrl.h>
24 #include <xengnttab.h>
25 
26 #include <sys/types.h>
27 #include <dirent.h>
28 #include <stdbool.h>
29 #include <stdint.h>
30 #include <errno.h>
31 
32 #include "xenstore_lib.h"
33 #include "list.h"
34 #include "tdb.h"
35 #include "hashtable.h"
36 
37 /* DEFAULT_BUFFER_SIZE should be large enough for each errno string. */
38 #define DEFAULT_BUFFER_SIZE 16
39 
40 typedef int32_t wrl_creditt;
41 #define WRL_CREDIT_MAX (1000*1000*1000)
42 /* ^ satisfies non-overflow condition for wrl_xfer_credit */
43 
44 struct buffered_data
45 {
46 	struct list_head list;
47 
48 	/* Are we still doing the header? */
49 	bool inhdr;
50 
51 	/* How far are we? */
52 	unsigned int used;
53 
54 	union {
55 		struct xsd_sockmsg msg;
56 		char raw[sizeof(struct xsd_sockmsg)];
57 	} hdr;
58 
59 	/* The actual data. */
60 	char *buffer;
61 	char default_buffer[DEFAULT_BUFFER_SIZE];
62 };
63 
64 struct connection;
65 typedef int connwritefn_t(struct connection *, const void *, unsigned int);
66 typedef int connreadfn_t(struct connection *, void *, unsigned int);
67 
68 struct connection
69 {
70 	struct list_head list;
71 
72 	/* The file descriptor we came in on. */
73 	int fd;
74 	/* The index of pollfd in global pollfd array */
75 	int pollfd_idx;
76 
77 	/* Who am I? 0 for socket connections. */
78 	unsigned int id;
79 
80 	/* Is this a read-only connection? */
81 	bool can_write;
82 
83 	/* Is this connection ignored? */
84 	bool is_ignored;
85 
86 	/* Buffered incoming data. */
87 	struct buffered_data *in;
88 
89 	/* Buffered output data */
90 	struct list_head out_list;
91 
92 	/* Transaction context for current request (NULL if none). */
93 	struct transaction *transaction;
94 
95 	/* List of in-progress transactions. */
96 	struct list_head transaction_list;
97 	uint32_t next_transaction_id;
98 	unsigned int transaction_started;
99 
100 	/* The domain I'm associated with, if any. */
101 	struct domain *domain;
102 
103         /* The target of the domain I'm associated with. */
104         struct connection *target;
105 
106 	/* My watches. */
107 	struct list_head watches;
108 
109 	/* Methods for communicating over this connection: write can be NULL */
110 	connwritefn_t *write;
111 	connreadfn_t *read;
112 };
113 extern struct list_head connections;
114 
115 struct node_perms {
116 	unsigned int num;
117 	struct xs_permissions *p;
118 };
119 
120 struct node {
121 	const char *name;
122 
123 	/* Parent (optional) */
124 	struct node *parent;
125 
126 	/* Generation count. */
127 	uint64_t generation;
128 #define NO_GENERATION ~((uint64_t)0)
129 
130 	/* Permissions. */
131 	struct node_perms perms;
132 
133 	/* Contents. */
134 	unsigned int datalen;
135 	void *data;
136 
137 	/* Children, each nul-terminated. */
138 	unsigned int childlen;
139 	char *children;
140 };
141 
142 /* Return the only argument in the input. */
143 const char *onearg(struct buffered_data *in);
144 
145 /* Break input into vectors, return the number, fill in up to num of them. */
146 unsigned int get_strings(struct buffered_data *data,
147 			 char *vec[], unsigned int num);
148 
149 void send_reply(struct connection *conn, enum xsd_sockmsg_type type,
150 		const void *data, unsigned int len);
151 
152 /* Some routines (write, mkdir, etc) just need a non-error return */
153 void send_ack(struct connection *conn, enum xsd_sockmsg_type type);
154 
155 /* Canonicalize this path if possible. */
156 char *canonicalize(struct connection *conn, const void *ctx, const char *node);
157 
158 /* Get access permissions. */
159 enum xs_perm_type perm_for_conn(struct connection *conn,
160 				const struct node_perms *perms);
161 
162 /* Write a node to the tdb data base. */
163 int write_node_raw(struct connection *conn, TDB_DATA *key, struct node *node,
164 		   bool no_quota_check);
165 
166 /* Get a node from the tdb data base. */
167 struct node *read_node(struct connection *conn, const void *ctx,
168 		       const char *name);
169 
170 struct connection *new_connection(connwritefn_t *write, connreadfn_t *read);
171 void check_store(void);
172 void corrupt(struct connection *conn, const char *fmt, ...);
173 enum xs_perm_type perm_for_conn(struct connection *conn,
174 				const struct node_perms *perms);
175 
176 /* Is this a valid node name? */
177 bool is_valid_nodename(const char *node);
178 
179 /* Get name of parent node. */
180 char *get_parent(const void *ctx, const char *node);
181 
182 /* Tracing infrastructure. */
183 void trace_create(const void *data, const char *type);
184 void trace_destroy(const void *data, const char *type);
185 void trace(const char *fmt, ...);
186 void dtrace_io(const struct connection *conn, const struct buffered_data *data, int out);
187 void reopen_log(void);
188 void close_log(void);
189 
190 extern char *tracefile;
191 extern int tracefd;
192 
193 extern TDB_CONTEXT *tdb_ctx;
194 extern int dom0_domid;
195 extern int dom0_event;
196 extern int priv_domid;
197 extern int quota_nb_entry_per_domain;
198 
199 /* Map the kernel's xenstore page. */
200 void *xenbus_map(void);
201 void unmap_xenbus(void *interface);
202 
xenbus_master_domid(void)203 static inline int xenbus_master_domid(void) { return dom0_domid; }
204 
205 /* Return the event channel used by xenbus. */
206 evtchn_port_t xenbus_evtchn(void);
207 
208 /* Tell the kernel xenstored is running. */
209 void xenbus_notify_running(void);
210 
211 /* Write out the pidfile */
212 void write_pidfile(const char *pidfile);
213 
214 /* Fork but do not close terminal FDs */
215 void daemonize(void);
216 /* Close stdin/stdout/stderr to complete daemonize */
217 void finish_daemonize(void);
218 
219 /* Open a pipe for signal handling */
220 void init_pipe(int reopen_log_pipe[2]);
221 
222 extern xengnttab_handle **xgt_handle;
223 
224 int remember_string(struct hashtable *hash, const char *str);
225 
226 #endif /* _XENSTORED_CORE_H */
227 
228 /*
229  * Local variables:
230  *  mode: C
231  *  c-file-style: "linux"
232  *  indent-tabs-mode: t
233  *  c-basic-offset: 8
234  *  tab-width: 8
235  * End:
236  */
237