1 /*
2     Common routines between Xen store user library and daemon.
3     Copyright (C) 2005 Rusty Russell IBM Corporation
4 
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9 
10     This library 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 GNU
13     Lesser General Public License for more details.
14 
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef XENSTORE_LIB_H
20 #define XENSTORE_LIB_H
21 
22 #include <stddef.h>
23 #include <stdbool.h>
24 #include <limits.h>
25 #include <errno.h>
26 #include <stdint.h>
27 #include <xen/io/xs_wire.h>
28 
29 /* Bitmask of permissions. */
30 enum xs_perm_type {
31 	XS_PERM_NONE = 0,
32 	XS_PERM_READ = 1,
33 	XS_PERM_WRITE = 2,
34 	/* Internal use. */
35 	XS_PERM_ENOENT_OK = 4,
36 	XS_PERM_OWNER = 8,
37 	XS_PERM_IGNORE = 16,
38 };
39 
40 struct xs_permissions
41 {
42 	unsigned int id;
43 	enum xs_perm_type perms;
44 };
45 
46 /* Header of the node record in tdb. */
47 struct xs_tdb_record_hdr {
48 	uint64_t generation;
49 	uint32_t num_perms;
50 	uint32_t datalen;
51 	uint32_t childlen;
52 	struct xs_permissions perms[0];
53 };
54 
55 /* Each 10 bits takes ~ 3 digits, plus one, plus one for nul terminator. */
56 #define MAX_STRLEN(x) ((sizeof(x) * CHAR_BIT + CHAR_BIT-1) / 10 * 3 + 2)
57 
58 /* Path for various daemon things: env vars can override. */
59 const char *xs_daemon_rootdir(void);
60 const char *xs_daemon_rundir(void);
61 const char *xs_daemon_socket(void);
62 const char *xs_daemon_socket_ro(void);
63 const char *xs_domain_dev(void);
64 const char *xs_daemon_tdb(void);
65 
66 /* Simple write function: loops for you. */
67 bool xs_write_all(int fd, const void *data, unsigned int len);
68 
69 /* Convert strings to permissions.  False if a problem. */
70 bool xs_strings_to_perms(struct xs_permissions *perms, unsigned int num,
71 			 const char *strings);
72 
73 /* Convert permissions to a string (up to len MAX_STRLEN(unsigned int)+1). */
74 bool xs_perm_to_string(const struct xs_permissions *perm,
75                        char *buffer, size_t buf_len);
76 
77 /* Given a string and a length, count how many strings (nul terms). */
78 unsigned int xs_count_strings(const char *strings, unsigned int len);
79 
80 /* Sanitising (quoting) possibly-binary strings. */
81 struct expanding_buffer {
82 	char *buf;
83 	int avail;
84 };
85 
86 /* Ensure that given expanding buffer has at least min_avail characters. */
87 char *expanding_buffer_ensure(struct expanding_buffer *, int min_avail);
88 
89 /* sanitise_value() may return NULL if malloc fails. */
90 char *sanitise_value(struct expanding_buffer *, const char *val, unsigned len);
91 
92 /* *out_len_r on entry is ignored; out must be at least strlen(in)+1 bytes. */
93 void unsanitise_value(char *out, unsigned *out_len_r, const char *in);
94 
95 #endif /* XENSTORE_LIB_H */
96