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 #define MIN(a, b) (((a) < (b))? (a) : (b))
41 
42 typedef int32_t wrl_creditt;
43 #define WRL_CREDIT_MAX (1000*1000*1000)
44 /* ^ satisfies non-overflow condition for wrl_xfer_credit */
45 
46 struct buffered_data
47 {
48 	struct list_head list;
49 
50 	/* Are we still doing the header? */
51 	bool inhdr;
52 
53 	/* How far are we? */
54 	unsigned int used;
55 
56 	union {
57 		struct xsd_sockmsg msg;
58 		char raw[sizeof(struct xsd_sockmsg)];
59 	} hdr;
60 
61 	/* The actual data. */
62 	char *buffer;
63 	char default_buffer[DEFAULT_BUFFER_SIZE];
64 };
65 
66 struct connection;
67 typedef int connwritefn_t(struct connection *, const void *, unsigned int);
68 typedef int connreadfn_t(struct connection *, void *, unsigned int);
69 
70 struct connection
71 {
72 	struct list_head list;
73 
74 	/* The file descriptor we came in on. */
75 	int fd;
76 	/* The index of pollfd in global pollfd array */
77 	int pollfd_idx;
78 
79 	/* Who am I? 0 for socket connections. */
80 	unsigned int id;
81 
82 	/* Is this a read-only connection? */
83 	bool can_write;
84 
85 	/* Buffered incoming data. */
86 	struct buffered_data *in;
87 
88 	/* Buffered output data */
89 	struct list_head out_list;
90 
91 	/* Transaction context for current request (NULL if none). */
92 	struct transaction *transaction;
93 
94 	/* List of in-progress transactions. */
95 	struct list_head transaction_list;
96 	uint32_t next_transaction_id;
97 	unsigned int transaction_started;
98 
99 	/* The domain I'm associated with, if any. */
100 	struct domain *domain;
101 
102         /* The target of the domain I'm associated with. */
103         struct connection *target;
104 
105 	/* My watches. */
106 	struct list_head watches;
107 
108 	/* Methods for communicating over this connection: write can be NULL */
109 	connwritefn_t *write;
110 	connreadfn_t *read;
111 };
112 extern struct list_head connections;
113 
114 struct node {
115 	const char *name;
116 
117 	/* Parent (optional) */
118 	struct node *parent;
119 
120 	/* Generation count. */
121 	uint64_t generation;
122 #define NO_GENERATION ~((uint64_t)0)
123 
124 	/* Permissions. */
125 	unsigned int num_perms;
126 	struct xs_permissions *perms;
127 
128 	/* Contents. */
129 	unsigned int datalen;
130 	void *data;
131 
132 	/* Children, each nul-terminated. */
133 	unsigned int childlen;
134 	char *children;
135 };
136 
137 /* Return the only argument in the input. */
138 const char *onearg(struct buffered_data *in);
139 
140 /* Break input into vectors, return the number, fill in up to num of them. */
141 unsigned int get_strings(struct buffered_data *data,
142 			 char *vec[], unsigned int num);
143 
144 void send_reply(struct connection *conn, enum xsd_sockmsg_type type,
145 		const void *data, unsigned int len);
146 
147 /* Some routines (write, mkdir, etc) just need a non-error return */
148 void send_ack(struct connection *conn, enum xsd_sockmsg_type type);
149 
150 /* Canonicalize this path if possible. */
151 char *canonicalize(struct connection *conn, const void *ctx, const char *node);
152 
153 /* Write a node to the tdb data base. */
154 int write_node_raw(struct connection *conn, TDB_DATA *key, struct node *node);
155 
156 /* Get this node, checking we have permissions. */
157 struct node *get_node(struct connection *conn,
158 		      const void *ctx,
159 		      const char *name,
160 		      enum xs_perm_type perm);
161 
162 struct connection *new_connection(connwritefn_t *write, connreadfn_t *read);
163 void check_store(void);
164 void corrupt(struct connection *conn, const char *fmt, ...);
165 
166 /* Is this a valid node name? */
167 bool is_valid_nodename(const char *node);
168 
169 /* Tracing infrastructure. */
170 void trace_create(const void *data, const char *type);
171 void trace_destroy(const void *data, const char *type);
172 void trace(const char *fmt, ...);
173 void dtrace_io(const struct connection *conn, const struct buffered_data *data, int out);
174 void reopen_log(void);
175 void close_log(void);
176 
177 extern char *tracefile;
178 extern int tracefd;
179 
180 extern TDB_CONTEXT *tdb_ctx;
181 extern int dom0_domid;
182 extern int dom0_event;
183 extern int priv_domid;
184 extern int quota_nb_entry_per_domain;
185 
186 /* Map the kernel's xenstore page. */
187 void *xenbus_map(void);
188 void unmap_xenbus(void *interface);
189 
xenbus_master_domid(void)190 static inline int xenbus_master_domid(void) { return dom0_domid; }
191 
192 /* Return the event channel used by xenbus. */
193 evtchn_port_t xenbus_evtchn(void);
194 
195 /* Tell the kernel xenstored is running. */
196 void xenbus_notify_running(void);
197 
198 /* Write out the pidfile */
199 void write_pidfile(const char *pidfile);
200 
201 /* Fork but do not close terminal FDs */
202 void daemonize(void);
203 /* Close stdin/stdout/stderr to complete daemonize */
204 void finish_daemonize(void);
205 
206 /* Open a pipe for signal handling */
207 void init_pipe(int reopen_log_pipe[2]);
208 
209 xengnttab_handle **xgt_handle;
210 
211 int remember_string(struct hashtable *hash, const char *str);
212 
213 #endif /* _XENSTORED_CORE_H */
214 
215 /*
216  * Local variables:
217  *  c-file-style: "linux"
218  *  indent-tabs-mode: t
219  *  c-indent-level: 8
220  *  c-basic-offset: 8
221  *  tab-width: 8
222  * End:
223  */
224