| 1 |
/* |
| 2 |
Watch code 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 |
#include <stdio.h> |
| 20 |
#include <sys/types.h> |
| 21 |
#include <stdarg.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <sys/time.h> |
| 24 |
#include <time.h> |
| 25 |
#include <assert.h> |
| 26 |
#include "talloc.h" |
| 27 |
#include "list.h" |
| 28 |
#include "watch.h" |
| 29 |
#include "xenstore_lib.h" |
| 30 |
#include "utils.h" |
| 31 |
#include "domain.h" |
| 32 |
#include "transaction.h" |
| 33 |
|
| 34 |
struct watch |
| 35 |
{ |
| 36 |
/* Watches on this connection */ |
| 37 |
struct list_head list; |
| 38 |
|
| 39 |
/* Offset into path for skipping prefix (used for relative paths). */ |
| 40 |
unsigned int prefix_len; |
| 41 |
|
| 42 |
char *token; |
| 43 |
char *node; |
| 44 |
}; |
| 45 |
|
| 46 |
/* Is child a subnode of parent, or equal? */ |
| 47 |
static bool is_child(const char *child, const char *parent) |
| 48 |
{ |
| 49 |
unsigned int len = strlen(parent); |
| 50 |
|
| 51 |
/* |
| 52 |
* / should really be "" for this algorithm to work, but that's a |
| 53 |
* usability nightmare. |
| 54 |
*/ |
| 55 |
if (streq(parent, "/")) |
| 56 |
return true; |
| 57 |
|
| 58 |
if (strncmp(child, parent, len) != 0) |
| 59 |
return false; |
| 60 |
|
| 61 |
return child[len] == '/' || child[len] == '\0'; |
| 62 |
} |
| 63 |
|
| 64 |
static const char *get_watch_path(const struct watch *watch, const char *name) |
| 65 |
{ |
| 66 |
return name + watch->prefix_len; |
| 67 |
} |
| 68 |
|
| 69 |
/* |
| 70 |
* Check permissions of a specific watch to fire: |
| 71 |
* Either the node itself or its parent have to be readable by the connection |
| 72 |
* the watch has been setup for. In case a watch event is created due to |
| 73 |
* changed permissions we need to take the old permissions into account, too. |
| 74 |
*/ |
| 75 |
static bool watch_permitted(struct connection *conn, const void *ctx, |
| 76 |
const char *name, const struct node *node, |
| 77 |
struct node_perms *perms) |
| 78 |
{ |
| 79 |
unsigned int perm; |
| 80 |
const struct node *parent; |
| 81 |
char *parent_name; |
| 82 |
|
| 83 |
if (perms) { |
| 84 |
perm = perm_for_conn(conn, perms); |
| 85 |
if (perm & XS_PERM_READ) |
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
if (!node) { |
| 90 |
node = read_node_const(conn, ctx, name); |
| 91 |
if (!node) |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
perm = perm_for_conn_from_node(conn, node); |
| 96 |
if (perm & XS_PERM_READ) |
| 97 |
return true; |
| 98 |
|
| 99 |
parent = node->parent; |
| 100 |
if (!parent) { |
| 101 |
parent_name = get_parent(ctx, node->name); |
| 102 |
if (!parent_name) |
| 103 |
return false; |
| 104 |
parent = read_node_const(conn, ctx, parent_name); |
| 105 |
if (!parent) |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
perm = perm_for_conn_from_node(conn, parent); |
| 110 |
|
| 111 |
return perm & XS_PERM_READ; |
| 112 |
} |
| 113 |
|
| 114 |
/* |
| 115 |
* Check whether any watch events are to be sent. |
| 116 |
* Temporary memory allocations are done with ctx. |
| 117 |
* We need to take the (potential) old permissions of the node into account |
| 118 |
* as a watcher losing permissions to access a node should receive the |
| 119 |
* watch event, too. |
| 120 |
*/ |
| 121 |
void fire_watches(struct connection *conn, const void *ctx, const char *name, |
| 122 |
const struct node *node, bool exact, struct node_perms *perms) |
| 123 |
{ |
| 124 |
struct connection *i; |
| 125 |
struct buffered_data *req; |
| 126 |
struct watch *watch; |
| 127 |
|
| 128 |
/* During transactions, don't fire watches, but queue them. */ |
| 129 |
if (conn && conn->transaction) { |
| 130 |
queue_watches(conn, name, exact); |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
req = domain_is_unprivileged(conn) ? conn->in : NULL; |
| 135 |
|
| 136 |
/* Create an event for each watch. */ |
| 137 |
list_for_each_entry(i, &connections, list) { |
| 138 |
if (!watch_permitted(i, ctx, name, node, perms)) |
| 139 |
continue; |
| 140 |
|
| 141 |
list_for_each_entry(watch, &i->watches, list) { |
| 142 |
if (exact) { |
| 143 |
if (streq(name, watch->node)) |
| 144 |
send_event(req, i, |
| 145 |
get_watch_path(watch, name), |
| 146 |
watch->token); |
| 147 |
} else { |
| 148 |
if (is_child(name, watch->node)) |
| 149 |
send_event(req, i, |
| 150 |
get_watch_path(watch, name), |
| 151 |
watch->token); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
static int destroy_watch(void *_watch) |
| 158 |
{ |
| 159 |
trace_destroy(_watch, "watch"); |
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
static int check_watch_path(struct connection *conn, const void *ctx, |
| 164 |
const char **path, bool *relative) |
| 165 |
{ |
| 166 |
*relative = !strstarts(*path, "/") && !strstarts(*path, "@"); |
| 167 |
*path = canonicalize(conn, ctx, *path, true); |
| 168 |
|
| 169 |
return *path ? 0 : errno; |
| 170 |
} |
| 171 |
|
| 172 |
static struct watch *add_watch(struct connection *conn, const char *path, |
| 173 |
const char *token, bool relative, |
| 174 |
bool no_quota_check) |
| 175 |
{ |
| 176 |
struct watch *watch; |
| 177 |
|
| 178 |
watch = talloc(conn, struct watch); |
| 179 |
if (!watch) |
| 180 |
goto nomem; |
| 181 |
watch->node = talloc_strdup(watch, path); |
| 182 |
watch->token = talloc_strdup(watch, token); |
| 183 |
if (!watch->node || !watch->token) |
| 184 |
goto nomem; |
| 185 |
if (domain_memory_add(conn, conn->id, strlen(path) + strlen(token), |
| 186 |
no_quota_check)) |
| 187 |
goto nomem; |
| 188 |
|
| 189 |
watch->prefix_len = relative ? strlen(get_implicit_path(conn)) + 1 : 0; |
| 190 |
|
| 191 |
domain_watch_inc(conn); |
| 192 |
list_add_tail(&watch->list, &conn->watches); |
| 193 |
talloc_set_destructor(watch, destroy_watch); |
| 194 |
|
| 195 |
return watch; |
| 196 |
|
| 197 |
nomem: |
| 198 |
talloc_free(watch); |
| 199 |
errno = ENOMEM; |
| 200 |
return NULL; |
| 201 |
} |
| 202 |
|
| 203 |
int do_watch(const void *ctx, struct connection *conn, struct buffered_data *in) |
| 204 |
{ |
| 205 |
struct watch *watch; |
| 206 |
const char *vec[2]; |
| 207 |
bool relative; |
| 208 |
|
| 209 |
if (get_strings(in, vec, ARRAY_SIZE(vec)) != ARRAY_SIZE(vec)) |
| 210 |
return EINVAL; |
| 211 |
|
| 212 |
errno = check_watch_path(conn, ctx, &(vec[0]), &relative); |
| 213 |
if (errno) |
| 214 |
return errno; |
| 215 |
|
| 216 |
/* Check for duplicates. */ |
| 217 |
list_for_each_entry(watch, &conn->watches, list) { |
| 218 |
if (streq(watch->node, vec[0]) && |
| 219 |
streq(watch->token, vec[1])) |
| 220 |
return EEXIST; |
| 221 |
} |
| 222 |
|
| 223 |
if (domain_watch(conn) > hard_quotas[ACC_WATCH].val) |
| 224 |
return E2BIG; |
| 225 |
|
| 226 |
watch = add_watch(conn, vec[0], vec[1], relative, false); |
| 227 |
if (!watch) |
| 228 |
return errno; |
| 229 |
|
| 230 |
trace_create(watch, "watch"); |
| 231 |
send_ack(conn, XS_WATCH); |
| 232 |
|
| 233 |
/* |
| 234 |
* We fire once up front: simplifies clients and restart. |
| 235 |
* This event will not be linked to the XS_WATCH request. |
| 236 |
*/ |
| 237 |
send_event(NULL, conn, get_watch_path(watch, watch->node), |
| 238 |
watch->token); |
| 239 |
|
| 240 |
return 0; |
| 241 |
} |
| 242 |
|
| 243 |
int do_unwatch(const void *ctx, struct connection *conn, |
| 244 |
struct buffered_data *in) |
| 245 |
{ |
| 246 |
struct watch *watch; |
| 247 |
const char *node; |
| 248 |
const char *vec[2]; |
| 249 |
|
| 250 |
if (get_strings(in, vec, ARRAY_SIZE(vec)) != ARRAY_SIZE(vec)) |
| 251 |
return EINVAL; |
| 252 |
|
| 253 |
node = canonicalize(conn, ctx, vec[0], true); |
| 254 |
if (!node) |
| 255 |
return errno; |
| 256 |
list_for_each_entry(watch, &conn->watches, list) { |
| 257 |
if (streq(watch->node, node) && streq(watch->token, vec[1])) { |
| 258 |
list_del(&watch->list); |
| 259 |
domain_memory_add_nochk(conn, conn->id, |
| 260 |
-strlen(watch->node) - |
| 261 |
strlen(watch->token)); |
| 262 |
talloc_free(watch); |
| 263 |
domain_watch_dec(conn); |
| 264 |
send_ack(conn, XS_UNWATCH); |
| 265 |
return 0; |
| 266 |
} |
| 267 |
} |
| 268 |
return ENOENT; |
| 269 |
} |
| 270 |
|
| 271 |
void conn_delete_all_watches(struct connection *conn) |
| 272 |
{ |
| 273 |
struct watch *watch; |
| 274 |
|
| 275 |
while ((watch = list_top(&conn->watches, struct watch, list))) { |
| 276 |
list_del(&watch->list); |
| 277 |
domain_memory_add_nochk(conn, conn->id, -strlen(watch->node) - |
| 278 |
strlen(watch->token)); |
| 279 |
talloc_free(watch); |
| 280 |
domain_watch_dec(conn); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
const char *dump_state_watches(FILE *fp, struct connection *conn, |
| 285 |
unsigned int conn_id) |
| 286 |
{ |
| 287 |
const char *ret = NULL; |
| 288 |
struct watch *watch; |
| 289 |
struct xs_state_watch sw; |
| 290 |
struct xs_state_record_header head; |
| 291 |
const char *path; |
| 292 |
|
| 293 |
head.type = XS_STATE_TYPE_WATCH; |
| 294 |
|
| 295 |
list_for_each_entry(watch, &conn->watches, list) { |
| 296 |
head.length = sizeof(sw); |
| 297 |
|
| 298 |
sw.conn_id = conn_id; |
| 299 |
path = get_watch_path(watch, watch->node); |
| 300 |
sw.path_length = strlen(path) + 1; |
| 301 |
sw.token_length = strlen(watch->token) + 1; |
| 302 |
head.length += sw.path_length + sw.token_length; |
| 303 |
head.length = ROUNDUP(head.length, 3); |
| 304 |
if (fwrite(&head, sizeof(head), 1, fp) != 1) |
| 305 |
return "Dump watch state error"; |
| 306 |
if (fwrite(&sw, sizeof(sw), 1, fp) != 1) |
| 307 |
return "Dump watch state error"; |
| 308 |
|
| 309 |
if (fwrite(path, sw.path_length, 1, fp) != 1) |
| 310 |
return "Dump watch path error"; |
| 311 |
if (fwrite(watch->token, sw.token_length, 1, fp) != 1) |
| 312 |
return "Dump watch token error"; |
| 313 |
|
| 314 |
ret = dump_state_align(fp); |
| 315 |
if (ret) |
| 316 |
return ret; |
| 317 |
} |
| 318 |
|
| 319 |
return ret; |
| 320 |
} |
| 321 |
|
| 322 |
void read_state_watch(const void *ctx, const void *state) |
| 323 |
{ |
| 324 |
const struct xs_state_watch *sw = state; |
| 325 |
struct connection *conn; |
| 326 |
const char *path, *token; |
| 327 |
bool relative; |
| 328 |
|
| 329 |
conn = get_connection_by_id(sw->conn_id); |
| 330 |
if (!conn) |
| 331 |
barf("connection not found for read watch"); |
| 332 |
|
| 333 |
path = (char *)sw->data; |
| 334 |
token = path + sw->path_length; |
| 335 |
|
| 336 |
/* Don't check success, we want the relative information only. */ |
| 337 |
check_watch_path(conn, ctx, &path, &relative); |
| 338 |
if (!path) |
| 339 |
barf("allocation error for read watch"); |
| 340 |
|
| 341 |
if (!add_watch(conn, path, token, relative, true)) |
| 342 |
barf("error adding watch"); |
| 343 |
} |
| 344 |
|
| 345 |
/* |
| 346 |
* Local variables: |
| 347 |
* mode: C |
| 348 |
* c-file-style: "linux" |
| 349 |
* indent-tabs-mode: t |
| 350 |
* c-basic-offset: 8 |
| 351 |
* tab-width: 8 |
| 352 |
* End: |
| 353 |
*/ |