ia64/xen-unstable

view tools/fs-back/fs-xenbus.c @ 17838:e5c9c8e6e726

tools: replace sprintf with snprintf where applicable

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Jun 12 15:41:15 2008 +0100 (2008-06-12)
parents a1a3fe600ef3
children 7a35ab9dba8a
line source
1 #undef NDEBUG
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <stdarg.h>
5 #include <string.h>
6 #include <assert.h>
7 #include <xenctrl.h>
8 #include <xs.h>
9 #include <xen/io/fsif.h>
10 #include "fs-backend.h"
13 static bool xenbus_printf(struct xs_handle *xsh,
14 xs_transaction_t xbt,
15 char* node,
16 char* path,
17 char* fmt,
18 ...)
19 {
20 char fullpath[1024];
21 char val[1024];
22 va_list args;
24 va_start(args, fmt);
25 snprintf(fullpath, sizeof(fullpath), "%s/%s", node, path);
26 vsnprintf(val, sizeof(val), fmt, args);
27 va_end(args);
28 printf("xenbus_printf (%s) <= %s.\n", fullpath, val);
30 return xs_write(xsh, xbt, fullpath, val, strlen(val));
31 }
33 bool xenbus_create_request_node(void)
34 {
35 bool ret;
36 struct xs_permissions perms;
38 assert(xsh != NULL);
39 xs_rm(xsh, XBT_NULL, WATCH_NODE);
40 ret = xs_mkdir(xsh, XBT_NULL, WATCH_NODE);
41 if (!ret)
42 return false;
44 perms.id = 0;
45 perms.perms = XS_PERM_WRITE;
46 ret = xs_set_permissions(xsh, XBT_NULL, WATCH_NODE, &perms, 1);
48 return ret;
49 }
51 int xenbus_register_export(struct fs_export *export)
52 {
53 xs_transaction_t xst = 0;
54 char node[1024];
55 struct xs_permissions perms;
57 assert(xsh != NULL);
58 if(xsh == NULL)
59 {
60 printf("Could not open connection to xenbus deamon.\n");
61 goto error_exit;
62 }
63 printf("Connection to the xenbus deamon opened successfully.\n");
65 /* Start transaction */
66 xst = xs_transaction_start(xsh);
67 if(xst == 0)
68 {
69 printf("Could not start a transaction.\n");
70 goto error_exit;
71 }
72 printf("XS transaction is %d\n", xst);
74 /* Create node string */
75 snprintf(node, sizeof(node), "%s/%d", EXPORTS_NODE, export->export_id);
76 /* Remove old export (if exists) */
77 xs_rm(xsh, xst, node);
79 if(!xenbus_printf(xsh, xst, node, "name", "%s", export->name))
80 {
81 printf("Could not write the export node.\n");
82 goto error_exit;
83 }
85 /* People need to be able to read our export */
86 perms.id = 0;
87 perms.perms = XS_PERM_READ;
88 if(!xs_set_permissions(xsh, xst, EXPORTS_NODE, &perms, 1))
89 {
90 printf("Could not set permissions on the export node.\n");
91 goto error_exit;
92 }
94 xs_transaction_end(xsh, xst, 0);
95 return 0;
97 error_exit:
98 if(xst != 0)
99 xs_transaction_end(xsh, xst, 1);
100 return -1;
101 }
103 int xenbus_get_watch_fd(void)
104 {
105 int res;
106 assert(xsh != NULL);
107 res = xs_watch(xsh, WATCH_NODE, "conn-watch");
108 assert(res);
109 return xs_fileno(xsh);
110 }
112 void xenbus_read_mount_request(struct mount *mount, char *frontend)
113 {
114 char node[1024];
115 char *s;
117 assert(xsh != NULL);
118 #if 0
119 snprintf(node, sizeof(node), WATCH_NODE"/%d/%d/frontend",
120 mount->dom_id, mount->export->export_id);
121 frontend = xs_read(xsh, XBT_NULL, node, NULL);
122 #endif
123 mount->frontend = frontend;
124 snprintf(node, sizeof(node), "%s/state", frontend);
125 s = xs_read(xsh, XBT_NULL, node, NULL);
126 assert(strcmp(s, STATE_READY) == 0);
127 free(s);
128 snprintf(node, sizeof(node), "%s/ring-ref", frontend);
129 s = xs_read(xsh, XBT_NULL, node, NULL);
130 mount->gref = atoi(s);
131 free(s);
132 snprintf(node, sizeof(node), "%s/event-channel", frontend);
133 s = xs_read(xsh, XBT_NULL, node, NULL);
134 mount->remote_evtchn = atoi(s);
135 free(s);
136 }
138 /* Small utility function to figure out our domain id */
139 static int get_self_id(void)
140 {
141 char *dom_id;
142 int ret;
144 assert(xsh != NULL);
145 dom_id = xs_read(xsh, XBT_NULL, "domid", NULL);
146 sscanf(dom_id, "%d", &ret);
147 free(dom_id);
149 return ret;
150 }
153 void xenbus_write_backend_node(struct mount *mount)
154 {
155 char node[1024], backend_node[1024];
156 int self_id;
158 assert(xsh != NULL);
159 self_id = get_self_id();
160 printf("Our own dom_id=%d\n", self_id);
161 snprintf(node, sizeof(node), "%s/backend", mount->frontend);
162 snprintf(backend_node, sizeof(backend_node), "/local/domain/%d/"ROOT_NODE"/%d",
163 self_id, mount->mount_id);
164 xs_write(xsh, XBT_NULL, node, backend_node, strlen(backend_node));
166 snprintf(node, sizeof(node), ROOT_NODE"/%d/state", mount->mount_id);
167 xs_write(xsh, XBT_NULL, node, STATE_INITIALISED, strlen(STATE_INITIALISED));
168 }
170 void xenbus_write_backend_ready(struct mount *mount)
171 {
172 char node[1024];
173 int self_id;
175 assert(xsh != NULL);
176 self_id = get_self_id();
177 snprintf(node, sizeof(node), ROOT_NODE"/%d/state", mount->mount_id);
178 xs_write(xsh, XBT_NULL, node, STATE_READY, strlen(STATE_READY));
179 }