| 1 |
/* |
| 2 |
Simple prototype Xen Store Daemon providing simple tree-like database. |
| 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 <inttypes.h> |
| 20 |
#include <sys/types.h> |
| 21 |
#include <sys/stat.h> |
| 22 |
#include <poll.h> |
| 23 |
#include <sys/time.h> |
| 24 |
#include <time.h> |
| 25 |
#include <unistd.h> |
| 26 |
#include <fcntl.h> |
| 27 |
#include <stdbool.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <stdarg.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <syslog.h> |
| 32 |
#include <string.h> |
| 33 |
#include <errno.h> |
| 34 |
#include <dirent.h> |
| 35 |
#include <getopt.h> |
| 36 |
#include <signal.h> |
| 37 |
#include <assert.h> |
| 38 |
#include <setjmp.h> |
| 39 |
|
| 40 |
#include <xenevtchn.h> |
| 41 |
#include <xen-tools/xenstore-common.h> |
| 42 |
|
| 43 |
#include "utils.h" |
| 44 |
#include "list.h" |
| 45 |
#include "talloc.h" |
| 46 |
#include "core.h" |
| 47 |
#include "watch.h" |
| 48 |
#include "transaction.h" |
| 49 |
#include "domain.h" |
| 50 |
#include "control.h" |
| 51 |
#include "lu.h" |
| 52 |
|
| 53 |
extern xenevtchn_handle *xce_handle; /* in domain.c */ |
| 54 |
static int xce_pollfd_idx = -1; |
| 55 |
struct pollfd *poll_fds; |
| 56 |
static unsigned int current_array_size; |
| 57 |
static unsigned int nr_fds; |
| 58 |
static unsigned int delayed_requests; |
| 59 |
|
| 60 |
int orig_argc; |
| 61 |
char **orig_argv; |
| 62 |
|
| 63 |
LIST_HEAD(connections); |
| 64 |
int tracefd = -1; |
| 65 |
bool keep_orphans = false; |
| 66 |
const char *tracefile = NULL; |
| 67 |
static struct hashtable *nodes; |
| 68 |
unsigned int trace_flags = TRACE_OBJ | TRACE_IO; |
| 69 |
|
| 70 |
static const char *sockmsg_string(enum xsd_sockmsg_type type); |
| 71 |
|
| 72 |
unsigned int timeout_watch_event_msec = 20000; |
| 73 |
|
| 74 |
void trace(const char *fmt, ...) |
| 75 |
{ |
| 76 |
va_list arglist; |
| 77 |
char *str; |
| 78 |
char sbuf[1024]; |
| 79 |
int ret, dummy; |
| 80 |
|
| 81 |
if (tracefd < 0) |
| 82 |
return; |
| 83 |
|
| 84 |
/* try to use a static buffer */ |
| 85 |
va_start(arglist, fmt); |
| 86 |
ret = vsnprintf(sbuf, 1024, fmt, arglist); |
| 87 |
va_end(arglist); |
| 88 |
|
| 89 |
if (ret <= 1024) { |
| 90 |
dummy = write(tracefd, sbuf, ret); |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
/* fail back to dynamic allocation */ |
| 95 |
va_start(arglist, fmt); |
| 96 |
str = talloc_vasprintf(NULL, fmt, arglist); |
| 97 |
va_end(arglist); |
| 98 |
if (str) { |
| 99 |
dummy = write(tracefd, str, strlen(str)); |
| 100 |
talloc_free(str); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
static void trace_io(const struct connection *conn, |
| 105 |
const struct buffered_data *data, |
| 106 |
const char *type) |
| 107 |
{ |
| 108 |
unsigned int i; |
| 109 |
time_t now; |
| 110 |
struct tm *tm; |
| 111 |
|
| 112 |
if (tracefd < 0 || !(trace_flags & TRACE_IO)) |
| 113 |
return; |
| 114 |
|
| 115 |
now = time(NULL); |
| 116 |
tm = localtime(&now); |
| 117 |
|
| 118 |
trace("io: %s %p (d%u) %04d%02d%02d %02d:%02d:%02d %s (", |
| 119 |
type, conn, conn->id, tm->tm_year + 1900, tm->tm_mon + 1, |
| 120 |
tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 121 |
sockmsg_string(data->hdr.msg.type)); |
| 122 |
|
| 123 |
for (i = 0; i < data->hdr.msg.len; i++) |
| 124 |
trace("%c", (data->buffer[i] != '\0') ? data->buffer[i] : ' '); |
| 125 |
trace(")\n"); |
| 126 |
} |
| 127 |
|
| 128 |
void trace_create(const void *data, const char *type) |
| 129 |
{ |
| 130 |
if (trace_flags & TRACE_OBJ) |
| 131 |
trace("obj: CREATE %s %p\n", type, data); |
| 132 |
} |
| 133 |
|
| 134 |
void trace_destroy(const void *data, const char *type) |
| 135 |
{ |
| 136 |
if (trace_flags & TRACE_OBJ) |
| 137 |
trace("obj: DESTROY %s %p\n", type, data); |
| 138 |
} |
| 139 |
|
| 140 |
/* |
| 141 |
* Return an absolute filename. |
| 142 |
* In case of a relative filename given as input, prepend XENSTORE_LIB_DIR. |
| 143 |
*/ |
| 144 |
const char *absolute_filename(const void *ctx, const char *filename) |
| 145 |
{ |
| 146 |
if (filename[0] != '/') |
| 147 |
return talloc_asprintf(ctx, XENSTORE_LIB_DIR "/%s", filename); |
| 148 |
return talloc_strdup(ctx, filename); |
| 149 |
} |
| 150 |
|
| 151 |
void close_log(void) |
| 152 |
{ |
| 153 |
if (tracefd >= 0) |
| 154 |
close(tracefd); |
| 155 |
tracefd = -1; |
| 156 |
} |
| 157 |
|
| 158 |
void reopen_log(void) |
| 159 |
{ |
| 160 |
if (tracefile) { |
| 161 |
close_log(); |
| 162 |
|
| 163 |
tracefd = open(tracefile, |
| 164 |
O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0600); |
| 165 |
|
| 166 |
if (tracefd < 0) |
| 167 |
perror("Could not open tracefile"); |
| 168 |
else |
| 169 |
trace("\n***\n"); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
uint64_t get_now_msec(void) |
| 174 |
{ |
| 175 |
struct timespec now_ts; |
| 176 |
|
| 177 |
if (clock_gettime(CLOCK_MONOTONIC, &now_ts)) |
| 178 |
barf_perror("Could not find time (clock_gettime failed)"); |
| 179 |
|
| 180 |
return now_ts.tv_sec * 1000 + now_ts.tv_nsec / 1000000; |
| 181 |
} |
| 182 |
|
| 183 |
/* |
| 184 |
* Remove a struct buffered_data from the list of outgoing data. |
| 185 |
* A struct buffered_data related to a request having caused watch events to be |
| 186 |
* sent is kept until all those events have been written out. |
| 187 |
* Each watch event is referencing the related request via pend.req, while the |
| 188 |
* number of watch events caused by a request is kept in pend.ref.event_cnt |
| 189 |
* (those two cases are mutually exclusive, so the two fields can share memory |
| 190 |
* via a union). |
| 191 |
* The struct buffered_data is freed only if no related watch event is |
| 192 |
* referencing it. The related return data can be freed right away. |
| 193 |
*/ |
| 194 |
static void free_buffered_data(struct buffered_data *out, |
| 195 |
struct connection *conn) |
| 196 |
{ |
| 197 |
struct buffered_data *req; |
| 198 |
|
| 199 |
list_del(&out->list); |
| 200 |
out->on_out_list = false; |
| 201 |
|
| 202 |
/* |
| 203 |
* Update conn->timeout_msec with the next found timeout value in the |
| 204 |
* queued pending requests. |
| 205 |
*/ |
| 206 |
if (out->timeout_msec) { |
| 207 |
conn->timeout_msec = 0; |
| 208 |
list_for_each_entry(req, &conn->out_list, list) { |
| 209 |
if (req->timeout_msec) { |
| 210 |
conn->timeout_msec = req->timeout_msec; |
| 211 |
break; |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
domain_memory_add_nochk(conn, conn->id, |
| 217 |
-out->hdr.msg.len - sizeof(out->hdr)); |
| 218 |
|
| 219 |
if (out->hdr.msg.type == XS_WATCH_EVENT) { |
| 220 |
req = out->pend.req; |
| 221 |
if (req) { |
| 222 |
req->pend.ref.event_cnt--; |
| 223 |
if (!req->pend.ref.event_cnt && !req->on_out_list) { |
| 224 |
if (req->on_ref_list) { |
| 225 |
domain_outstanding_dec(conn, |
| 226 |
req->pend.ref.domid); |
| 227 |
list_del(&req->list); |
| 228 |
} |
| 229 |
talloc_free(req); |
| 230 |
} |
| 231 |
} |
| 232 |
} else if (out->pend.ref.event_cnt) { |
| 233 |
/* Hang out off from conn. */ |
| 234 |
talloc_steal(NULL, out); |
| 235 |
if (out->buffer != out->default_buffer) |
| 236 |
talloc_free(out->buffer); |
| 237 |
list_add(&out->list, &conn->ref_list); |
| 238 |
out->on_ref_list = true; |
| 239 |
return; |
| 240 |
} else |
| 241 |
domain_outstanding_dec(conn, conn->id); |
| 242 |
|
| 243 |
talloc_free(out); |
| 244 |
} |
| 245 |
|
| 246 |
static void check_event_timeout(struct connection *conn, uint64_t msecs, |
| 247 |
int *ptimeout) |
| 248 |
{ |
| 249 |
uint64_t delta; |
| 250 |
struct buffered_data *out, *tmp; |
| 251 |
|
| 252 |
if (!conn->timeout_msec) |
| 253 |
return; |
| 254 |
|
| 255 |
delta = conn->timeout_msec - msecs; |
| 256 |
if (conn->timeout_msec <= msecs) { |
| 257 |
delta = 0; |
| 258 |
list_for_each_entry_safe(out, tmp, &conn->out_list, list) { |
| 259 |
/* |
| 260 |
* Only look at buffers with timeout and no data |
| 261 |
* already written to the ring. |
| 262 |
*/ |
| 263 |
if (out->timeout_msec && out->inhdr && !out->used) { |
| 264 |
if (out->timeout_msec > msecs) { |
| 265 |
conn->timeout_msec = out->timeout_msec; |
| 266 |
delta = conn->timeout_msec - msecs; |
| 267 |
break; |
| 268 |
} |
| 269 |
|
| 270 |
/* |
| 271 |
* Free out without updating conn->timeout_msec, |
| 272 |
* as the update is done in this loop already. |
| 273 |
*/ |
| 274 |
out->timeout_msec = 0; |
| 275 |
trace("watch event path %s for domain %u timed out\n", |
| 276 |
out->buffer, conn->id); |
| 277 |
free_buffered_data(out, conn); |
| 278 |
} |
| 279 |
} |
| 280 |
if (!delta) { |
| 281 |
conn->timeout_msec = 0; |
| 282 |
return; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
if (*ptimeout == -1 || *ptimeout > delta) |
| 287 |
*ptimeout = delta; |
| 288 |
} |
| 289 |
|
| 290 |
void conn_free_buffered_data(struct connection *conn) |
| 291 |
{ |
| 292 |
struct buffered_data *out; |
| 293 |
|
| 294 |
while ((out = list_top(&conn->out_list, struct buffered_data, list))) |
| 295 |
free_buffered_data(out, conn); |
| 296 |
|
| 297 |
conn->timeout_msec = 0; |
| 298 |
} |
| 299 |
|
| 300 |
static bool write_messages(struct connection *conn) |
| 301 |
{ |
| 302 |
int ret; |
| 303 |
struct buffered_data *out; |
| 304 |
bool started = false; |
| 305 |
|
| 306 |
out = list_top(&conn->out_list, struct buffered_data, list); |
| 307 |
if (out == NULL) |
| 308 |
return true; |
| 309 |
|
| 310 |
if (out->inhdr) { |
| 311 |
started = !out->used; |
| 312 |
ret = conn->funcs->write(conn, out->hdr.raw + out->used, |
| 313 |
sizeof(out->hdr) - out->used); |
| 314 |
if (ret < 0) |
| 315 |
goto err; |
| 316 |
|
| 317 |
out->used += ret; |
| 318 |
if (out->used < sizeof(out->hdr)) |
| 319 |
goto start; |
| 320 |
|
| 321 |
out->inhdr = false; |
| 322 |
out->used = 0; |
| 323 |
|
| 324 |
/* Second write might block if non-zero. */ |
| 325 |
if (out->hdr.msg.len && !conn->domain) |
| 326 |
goto start; |
| 327 |
} |
| 328 |
|
| 329 |
ret = conn->funcs->write(conn, out->buffer + out->used, |
| 330 |
out->hdr.msg.len - out->used); |
| 331 |
if (ret < 0) |
| 332 |
goto err; |
| 333 |
|
| 334 |
out->used += ret; |
| 335 |
if (out->used != out->hdr.msg.len) |
| 336 |
goto start; |
| 337 |
|
| 338 |
trace_io(conn, out, started ? "OUT" : "OUT(END)"); |
| 339 |
|
| 340 |
free_buffered_data(out, conn); |
| 341 |
|
| 342 |
return true; |
| 343 |
|
| 344 |
err: |
| 345 |
trace_io(conn, out, "OUT(ERR)"); |
| 346 |
return false; |
| 347 |
|
| 348 |
start: |
| 349 |
if (started) |
| 350 |
trace_io(conn, out, "OUT(START)"); |
| 351 |
return true; |
| 352 |
} |
| 353 |
|
| 354 |
static int undelay_request(void *_req) |
| 355 |
{ |
| 356 |
struct delayed_request *req = _req; |
| 357 |
|
| 358 |
list_del(&req->list); |
| 359 |
delayed_requests--; |
| 360 |
|
| 361 |
return 0; |
| 362 |
} |
| 363 |
|
| 364 |
static void call_delayed(struct delayed_request *req) |
| 365 |
{ |
| 366 |
if (req->func(req)) { |
| 367 |
undelay_request(req); |
| 368 |
talloc_set_destructor(req, NULL); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
int delay_request(struct connection *conn, struct buffered_data *in, |
| 373 |
bool (*func)(struct delayed_request *), void *data, |
| 374 |
bool no_quota_check) |
| 375 |
{ |
| 376 |
struct delayed_request *req; |
| 377 |
|
| 378 |
/* |
| 379 |
* Only allow one request can be delayed for an unprivileged |
| 380 |
* connection. |
| 381 |
*/ |
| 382 |
if (!no_quota_check && domain_is_unprivileged(conn) && |
| 383 |
!list_empty(&conn->delayed)) |
| 384 |
return ENOSPC; |
| 385 |
|
| 386 |
req = talloc(in, struct delayed_request); |
| 387 |
if (!req) |
| 388 |
return ENOMEM; |
| 389 |
|
| 390 |
/* For the case of connection being closed. */ |
| 391 |
talloc_set_destructor(req, undelay_request); |
| 392 |
|
| 393 |
req->in = in; |
| 394 |
req->func = func; |
| 395 |
req->data = data; |
| 396 |
|
| 397 |
delayed_requests++; |
| 398 |
list_add(&req->list, &conn->delayed); |
| 399 |
|
| 400 |
/* Unlink the request from conn if this is the current one */ |
| 401 |
if (conn->in == in) |
| 402 |
conn->in = NULL; |
| 403 |
|
| 404 |
return 0; |
| 405 |
} |
| 406 |
|
| 407 |
static int destroy_conn(void *_conn) |
| 408 |
{ |
| 409 |
struct connection *conn = _conn; |
| 410 |
struct buffered_data *req; |
| 411 |
|
| 412 |
/* Flush outgoing if possible, but don't block. */ |
| 413 |
if (!conn->domain) { |
| 414 |
struct pollfd pfd; |
| 415 |
pfd.fd = conn->fd; |
| 416 |
pfd.events = POLLOUT; |
| 417 |
|
| 418 |
while (!list_empty(&conn->out_list) |
| 419 |
&& poll(&pfd, 1, 0) == 1) |
| 420 |
if (!write_messages(conn)) |
| 421 |
break; |
| 422 |
close(conn->fd); |
| 423 |
} |
| 424 |
|
| 425 |
conn_free_buffered_data(conn); |
| 426 |
conn_delete_all_watches(conn); |
| 427 |
list_for_each_entry(req, &conn->ref_list, list) |
| 428 |
req->on_ref_list = false; |
| 429 |
|
| 430 |
if (conn->target) |
| 431 |
talloc_unlink(conn, conn->target); |
| 432 |
list_del(&conn->list); |
| 433 |
trace_destroy(conn, "connection"); |
| 434 |
return 0; |
| 435 |
} |
| 436 |
|
| 437 |
static bool conn_can_read(struct connection *conn) |
| 438 |
{ |
| 439 |
if (conn->is_ignored) |
| 440 |
return false; |
| 441 |
|
| 442 |
if (!conn->funcs->can_read(conn)) |
| 443 |
return false; |
| 444 |
|
| 445 |
/* |
| 446 |
* For stalled connection, we want to process the pending |
| 447 |
* command as soon as live-update has aborted. |
| 448 |
*/ |
| 449 |
if (conn->is_stalled) |
| 450 |
return !lu_is_pending(); |
| 451 |
|
| 452 |
return true; |
| 453 |
} |
| 454 |
|
| 455 |
static bool conn_can_write(struct connection *conn) |
| 456 |
{ |
| 457 |
return !conn->is_ignored && conn->funcs->can_write(conn); |
| 458 |
} |
| 459 |
|
| 460 |
/* This function returns index inside the array if succeed, -1 if fail */ |
| 461 |
int set_fd(int fd, short events) |
| 462 |
{ |
| 463 |
int ret; |
| 464 |
if (current_array_size < nr_fds + 1) { |
| 465 |
struct pollfd *new_fds = NULL; |
| 466 |
unsigned long newsize; |
| 467 |
|
| 468 |
/* Round up to 2^8 boundary, in practice this just |
| 469 |
* make newsize larger than current_array_size. |
| 470 |
*/ |
| 471 |
newsize = ROUNDUP(nr_fds + 1, 8); |
| 472 |
|
| 473 |
new_fds = realloc(poll_fds, sizeof(struct pollfd)*newsize); |
| 474 |
if (!new_fds) |
| 475 |
goto fail; |
| 476 |
poll_fds = new_fds; |
| 477 |
|
| 478 |
memset(&poll_fds[0] + current_array_size, 0, |
| 479 |
sizeof(struct pollfd ) * (newsize-current_array_size)); |
| 480 |
current_array_size = newsize; |
| 481 |
} |
| 482 |
|
| 483 |
poll_fds[nr_fds].fd = fd; |
| 484 |
poll_fds[nr_fds].events = events; |
| 485 |
ret = nr_fds; |
| 486 |
nr_fds++; |
| 487 |
|
| 488 |
return ret; |
| 489 |
fail: |
| 490 |
syslog(LOG_ERR, "realloc failed, ignoring fd %d\n", fd); |
| 491 |
return -1; |
| 492 |
} |
| 493 |
|
| 494 |
static void initialize_fds(int *ptimeout) |
| 495 |
{ |
| 496 |
struct connection *conn; |
| 497 |
uint64_t msecs; |
| 498 |
|
| 499 |
if (poll_fds) |
| 500 |
memset(poll_fds, 0, sizeof(struct pollfd) * current_array_size); |
| 501 |
nr_fds = 0; |
| 502 |
|
| 503 |
/* In case of delayed requests pause for max 1 second. */ |
| 504 |
*ptimeout = delayed_requests ? 1000 : -1; |
| 505 |
|
| 506 |
set_special_fds(); |
| 507 |
|
| 508 |
if (xce_handle != NULL) |
| 509 |
xce_pollfd_idx = set_fd(xenevtchn_fd(xce_handle), |
| 510 |
POLLIN|POLLPRI); |
| 511 |
|
| 512 |
msecs = get_now_msec(); |
| 513 |
wrl_log_periodic(msecs); |
| 514 |
|
| 515 |
list_for_each_entry(conn, &connections, list) { |
| 516 |
if (conn->domain) { |
| 517 |
wrl_check_timeout(conn->domain, msecs, ptimeout); |
| 518 |
check_event_timeout(conn, msecs, ptimeout); |
| 519 |
if (conn_can_read(conn) || |
| 520 |
(conn_can_write(conn) && |
| 521 |
!list_empty(&conn->out_list))) |
| 522 |
*ptimeout = 0; |
| 523 |
} else { |
| 524 |
short events = POLLIN|POLLPRI; |
| 525 |
if (!list_empty(&conn->out_list)) |
| 526 |
events |= POLLOUT; |
| 527 |
conn->pollfd_idx = set_fd(conn->fd, events); |
| 528 |
/* |
| 529 |
* For stalled connection, we want to process the |
| 530 |
* pending command as soon as live-update has aborted. |
| 531 |
*/ |
| 532 |
if (conn->is_stalled && !lu_is_pending()) |
| 533 |
*ptimeout = 0; |
| 534 |
} |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
static size_t calc_node_acc_size(const struct node_hdr *hdr) |
| 539 |
{ |
| 540 |
return sizeof(*hdr) + hdr->num_perms * sizeof(struct xs_permissions) + |
| 541 |
hdr->datalen + hdr->childlen; |
| 542 |
} |
| 543 |
|
| 544 |
const struct node_hdr *db_fetch(const char *db_name, size_t *size) |
| 545 |
{ |
| 546 |
const struct node_hdr *hdr; |
| 547 |
|
| 548 |
hdr = hashtable_search(nodes, db_name); |
| 549 |
if (!hdr) { |
| 550 |
errno = ENOENT; |
| 551 |
return NULL; |
| 552 |
} |
| 553 |
|
| 554 |
*size = calc_node_acc_size(hdr); |
| 555 |
|
| 556 |
trace_tdb("read %s size %zu\n", db_name, *size + strlen(db_name)); |
| 557 |
|
| 558 |
return hdr; |
| 559 |
} |
| 560 |
|
| 561 |
static const struct xs_permissions *perms_from_node_hdr( |
| 562 |
const struct node_hdr *hdr) |
| 563 |
{ |
| 564 |
return (const struct xs_permissions *)(hdr + 1); |
| 565 |
} |
| 566 |
|
| 567 |
static void get_acc_data(const char *name, struct node_account_data *acc) |
| 568 |
{ |
| 569 |
size_t size; |
| 570 |
const struct node_hdr *hdr; |
| 571 |
|
| 572 |
if (acc->memory < 0) { |
| 573 |
hdr = db_fetch(name, &size); |
| 574 |
/* No check for error, as the node might not exist. */ |
| 575 |
if (hdr == NULL) { |
| 576 |
acc->memory = 0; |
| 577 |
} else { |
| 578 |
acc->memory = size; |
| 579 |
acc->domid = perms_from_node_hdr(hdr)->id; |
| 580 |
} |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
/* |
| 585 |
* Per-transaction nodes need to be accounted for the transaction owner. |
| 586 |
* Those nodes are stored in the data base with the transaction generation |
| 587 |
* count prepended (e.g. 123/local/domain/...). So testing for the node's |
| 588 |
* key not to start with "/" or "@" is sufficient. |
| 589 |
*/ |
| 590 |
static unsigned int get_acc_domid(struct connection *conn, const char *name, |
| 591 |
unsigned int domid) |
| 592 |
{ |
| 593 |
return (!conn || name[0] == '/' || name[0] == '@') ? domid : conn->id; |
| 594 |
} |
| 595 |
|
| 596 |
int db_write(struct connection *conn, const char *db_name, void *data, |
| 597 |
size_t size, struct node_account_data *acc, |
| 598 |
enum write_node_mode mode, bool no_quota_check) |
| 599 |
{ |
| 600 |
const struct node_hdr *hdr = data; |
| 601 |
struct node_account_data old_acc = {}; |
| 602 |
unsigned int old_domid, new_domid; |
| 603 |
size_t name_len = strlen(db_name); |
| 604 |
const char *name; |
| 605 |
int ret; |
| 606 |
|
| 607 |
if (!acc) |
| 608 |
old_acc.memory = -1; |
| 609 |
else |
| 610 |
old_acc = *acc; |
| 611 |
|
| 612 |
get_acc_data(db_name, &old_acc); |
| 613 |
old_domid = get_acc_domid(conn, db_name, old_acc.domid); |
| 614 |
new_domid = get_acc_domid(conn, db_name, perms_from_node_hdr(hdr)->id); |
| 615 |
|
| 616 |
/* |
| 617 |
* Don't check for ENOENT, as we want to be able to switch orphaned |
| 618 |
* nodes to new owners. |
| 619 |
*/ |
| 620 |
if (old_acc.memory) |
| 621 |
domain_memory_add_nochk(conn, old_domid, |
| 622 |
-old_acc.memory - name_len); |
| 623 |
ret = domain_memory_add(conn, new_domid, size + name_len, |
| 624 |
no_quota_check); |
| 625 |
if (ret) { |
| 626 |
/* Error path, so no quota check. */ |
| 627 |
if (old_acc.memory) |
| 628 |
domain_memory_add_nochk(conn, old_domid, |
| 629 |
old_acc.memory + name_len); |
| 630 |
return ret; |
| 631 |
} |
| 632 |
|
| 633 |
if (mode == NODE_CREATE) { |
| 634 |
/* db_name could be modified later, so allocate a copy. */ |
| 635 |
name = talloc_strdup(data, db_name); |
| 636 |
ret = name ? hashtable_add(nodes, name, data) : ENOMEM; |
| 637 |
} else |
| 638 |
ret = hashtable_replace(nodes, db_name, data); |
| 639 |
|
| 640 |
if (ret) { |
| 641 |
/* Free data, as it isn't owned by hashtable now. */ |
| 642 |
talloc_free(data); |
| 643 |
domain_memory_add_nochk(conn, new_domid, -size - name_len); |
| 644 |
/* Error path, so no quota check. */ |
| 645 |
if (old_acc.memory) |
| 646 |
domain_memory_add_nochk(conn, old_domid, |
| 647 |
old_acc.memory + name_len); |
| 648 |
errno = ret; |
| 649 |
return errno; |
| 650 |
} |
| 651 |
trace_tdb("store %s size %zu\n", db_name, size + name_len); |
| 652 |
|
| 653 |
if (acc) { |
| 654 |
/* Don't use new_domid, as it might be a transaction node. */ |
| 655 |
acc->domid = perms_from_node_hdr(hdr)->id; |
| 656 |
acc->memory = size; |
| 657 |
} |
| 658 |
|
| 659 |
return 0; |
| 660 |
} |
| 661 |
|
| 662 |
void db_delete(struct connection *conn, const char *name, |
| 663 |
struct node_account_data *acc) |
| 664 |
{ |
| 665 |
struct node_account_data tmp_acc; |
| 666 |
unsigned int domid; |
| 667 |
|
| 668 |
if (!acc) { |
| 669 |
acc = &tmp_acc; |
| 670 |
acc->memory = -1; |
| 671 |
} |
| 672 |
|
| 673 |
get_acc_data(name, acc); |
| 674 |
|
| 675 |
hashtable_remove(nodes, name); |
| 676 |
trace_tdb("delete %s\n", name); |
| 677 |
|
| 678 |
if (acc->memory) { |
| 679 |
domid = get_acc_domid(conn, name, acc->domid); |
| 680 |
domain_memory_add_nochk(conn, domid, |
| 681 |
-acc->memory - strlen(name)); |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
/* |
| 686 |
* If it fails, returns NULL and sets errno. |
| 687 |
* Temporary memory allocations will be done with ctx. |
| 688 |
*/ |
| 689 |
static struct node *read_node_alloc(struct connection *conn, const void *ctx, |
| 690 |
const char *name, |
| 691 |
const struct node_hdr **hdr) |
| 692 |
{ |
| 693 |
size_t size; |
| 694 |
struct node *node; |
| 695 |
const char *db_name; |
| 696 |
int err; |
| 697 |
|
| 698 |
node = talloc(ctx, struct node); |
| 699 |
if (!node) { |
| 700 |
errno = ENOMEM; |
| 701 |
return NULL; |
| 702 |
} |
| 703 |
|
| 704 |
node->name = talloc_strdup(node, name); |
| 705 |
if (!node->name) { |
| 706 |
errno = ENOMEM; |
| 707 |
goto error; |
| 708 |
} |
| 709 |
|
| 710 |
db_name = transaction_prepend(conn, name); |
| 711 |
*hdr = db_fetch(db_name, &size); |
| 712 |
if (*hdr == NULL) { |
| 713 |
node->hdr.generation = NO_GENERATION; |
| 714 |
err = access_node(conn, node, NODE_ACCESS_READ, NULL); |
| 715 |
errno = err ? : ENOENT; |
| 716 |
goto error; |
| 717 |
} |
| 718 |
|
| 719 |
node->parent = NULL; |
| 720 |
|
| 721 |
/* Datalen, childlen, number of permissions */ |
| 722 |
node->hdr = **hdr; |
| 723 |
node->acc.domid = perms_from_node_hdr(*hdr)->id; |
| 724 |
node->acc.memory = size; |
| 725 |
|
| 726 |
return node; |
| 727 |
|
| 728 |
error: |
| 729 |
talloc_free(node); |
| 730 |
return NULL; |
| 731 |
} |
| 732 |
|
| 733 |
static bool read_node_helper(struct connection *conn, struct node *node) |
| 734 |
{ |
| 735 |
/* Data is binary blob (usually ascii, no nul). */ |
| 736 |
node->data = node->perms + node->hdr.num_perms; |
| 737 |
/* Children is strings, nul separated. */ |
| 738 |
node->children = node->data + node->hdr.datalen; |
| 739 |
|
| 740 |
if (domain_adjust_node_perms(node)) |
| 741 |
return false; |
| 742 |
|
| 743 |
/* If owner is gone reset currently accounted memory size. */ |
| 744 |
if (node->acc.domid != get_node_owner(node)) |
| 745 |
node->acc.memory = 0; |
| 746 |
|
| 747 |
if (access_node(conn, node, NODE_ACCESS_READ, NULL)) |
| 748 |
return false; |
| 749 |
|
| 750 |
return true; |
| 751 |
} |
| 752 |
|
| 753 |
struct node *read_node(struct connection *conn, const void *ctx, |
| 754 |
const char *name) |
| 755 |
{ |
| 756 |
size_t size; |
| 757 |
const struct node_hdr *hdr; |
| 758 |
struct node *node; |
| 759 |
|
| 760 |
node = read_node_alloc(conn, ctx, name, &hdr); |
| 761 |
if (!node) |
| 762 |
return NULL; |
| 763 |
|
| 764 |
/* Copy node data to new memory area, starting with permissions. */ |
| 765 |
size = node->acc.memory - sizeof(*hdr); |
| 766 |
node->perms = talloc_memdup(node, perms_from_node_hdr(hdr), size); |
| 767 |
if (node->perms == NULL) { |
| 768 |
errno = ENOMEM; |
| 769 |
goto error; |
| 770 |
} |
| 771 |
|
| 772 |
if (!read_node_helper(conn, node)) |
| 773 |
goto error; |
| 774 |
|
| 775 |
return node; |
| 776 |
|
| 777 |
error: |
| 778 |
talloc_free(node); |
| 779 |
return NULL; |
| 780 |
} |
| 781 |
|
| 782 |
const struct node *read_node_const(struct connection *conn, const void *ctx, |
| 783 |
const char *name) |
| 784 |
{ |
| 785 |
const struct node_hdr *hdr; |
| 786 |
struct node *node; |
| 787 |
|
| 788 |
node = read_node_alloc(conn, ctx, name, &hdr); |
| 789 |
if (!node) |
| 790 |
return NULL; |
| 791 |
|
| 792 |
/* Unfortunately node->perms isn't const. */ |
| 793 |
node->perms = (void *)perms_from_node_hdr(hdr); |
| 794 |
|
| 795 |
if (!read_node_helper(conn, node)) |
| 796 |
goto error; |
| 797 |
|
| 798 |
return node; |
| 799 |
|
| 800 |
error: |
| 801 |
talloc_free(node); |
| 802 |
return NULL; |
| 803 |
} |
| 804 |
|
| 805 |
static bool read_node_can_propagate_errno(void) |
| 806 |
{ |
| 807 |
/* |
| 808 |
* 2 error cases for read_node() can always be propagated up: |
| 809 |
* ENOMEM, because this has nothing to do with the node being in the |
| 810 |
* data base or not, but is caused by a general lack of memory. |
| 811 |
* ENOSPC, because this is related to hitting quota limits which need |
| 812 |
* to be respected. |
| 813 |
*/ |
| 814 |
return errno == ENOMEM || errno == ENOSPC; |
| 815 |
} |
| 816 |
|
| 817 |
int write_node_raw(struct connection *conn, const char *db_name, |
| 818 |
struct node *node, enum write_node_mode mode, |
| 819 |
bool no_quota_check) |
| 820 |
{ |
| 821 |
void *data; |
| 822 |
size_t size; |
| 823 |
void *p; |
| 824 |
struct node_hdr *hdr; |
| 825 |
|
| 826 |
if (domain_adjust_node_perms(node)) |
| 827 |
return errno; |
| 828 |
|
| 829 |
size = calc_node_acc_size(&node->hdr); |
| 830 |
|
| 831 |
/* Call domain_max_chk() in any case in order to record max values. */ |
| 832 |
if (domain_max_chk(conn, ACC_NODESZ, size) && !no_quota_check) { |
| 833 |
errno = ENOSPC; |
| 834 |
return errno; |
| 835 |
} |
| 836 |
|
| 837 |
data = talloc_size(node, size); |
| 838 |
if (!data) { |
| 839 |
errno = ENOMEM; |
| 840 |
return errno; |
| 841 |
} |
| 842 |
|
| 843 |
BUILD_BUG_ON(XENSTORE_PAYLOAD_MAX >= (typeof(hdr->datalen))(-1)); |
| 844 |
|
| 845 |
hdr = data; |
| 846 |
*hdr = node->hdr; |
| 847 |
|
| 848 |
/* Open code perms_from_node_hdr() for the non-const case. */ |
| 849 |
p = hdr + 1; |
| 850 |
memcpy(p, node->perms, node->hdr.num_perms * sizeof(*node->perms)); |
| 851 |
p += node->hdr.num_perms * sizeof(*node->perms); |
| 852 |
memcpy(p, node->data, node->hdr.datalen); |
| 853 |
p += node->hdr.datalen; |
| 854 |
memcpy(p, node->children, node->hdr.childlen); |
| 855 |
|
| 856 |
if (db_write(conn, db_name, data, size, &node->acc, mode, |
| 857 |
no_quota_check)) |
| 858 |
return EIO; |
| 859 |
|
| 860 |
return 0; |
| 861 |
} |
| 862 |
|
| 863 |
/* |
| 864 |
* Write the node. If the node is written, caller can find the DB name used in |
| 865 |
* node->db_name. This can later be used if the change needs to be reverted. |
| 866 |
*/ |
| 867 |
static int write_node(struct connection *conn, struct node *node, |
| 868 |
enum write_node_mode mode, bool no_quota_check) |
| 869 |
{ |
| 870 |
int ret; |
| 871 |
|
| 872 |
if (access_node(conn, node, NODE_ACCESS_WRITE, &node->db_name)) |
| 873 |
return errno; |
| 874 |
|
| 875 |
ret = write_node_raw(conn, node->db_name, node, mode, no_quota_check); |
| 876 |
if (ret && conn && conn->transaction) { |
| 877 |
/* |
| 878 |
* Reverting access_node() is hard, so just fail the |
| 879 |
* transaction. |
| 880 |
*/ |
| 881 |
fail_transaction(conn->transaction); |
| 882 |
} |
| 883 |
|
| 884 |
return ret; |
| 885 |
} |
| 886 |
|
| 887 |
unsigned int perm_for_conn(struct connection *conn, |
| 888 |
const struct node_perms *perms) |
| 889 |
{ |
| 890 |
unsigned int i; |
| 891 |
unsigned int mask = XS_PERM_READ|XS_PERM_WRITE|XS_PERM_OWNER; |
| 892 |
|
| 893 |
/* Owners and tools get it all... */ |
| 894 |
if (!domain_is_unprivileged(conn) || perms->p[0].id == conn->id |
| 895 |
|| (conn->target && perms->p[0].id == conn->target->id)) |
| 896 |
return (XS_PERM_READ|XS_PERM_WRITE|XS_PERM_OWNER) & mask; |
| 897 |
|
| 898 |
for (i = 1; i < perms->num; i++) |
| 899 |
if (!(perms->p[i].perms & XS_PERM_IGNORE) && |
| 900 |
(perms->p[i].id == conn->id || |
| 901 |
(conn->target && perms->p[i].id == conn->target->id))) |
| 902 |
return perms->p[i].perms & mask; |
| 903 |
|
| 904 |
return perms->p[0].perms & mask; |
| 905 |
} |
| 906 |
|
| 907 |
/* |
| 908 |
* Get name of node parent. |
| 909 |
* Temporary memory allocations are done with ctx. |
| 910 |
*/ |
| 911 |
char *get_parent(const void *ctx, const char *node) |
| 912 |
{ |
| 913 |
char *parent; |
| 914 |
char *slash = strrchr(node + 1, '/'); |
| 915 |
|
| 916 |
parent = slash ? talloc_asprintf(ctx, "%.*s", (int)(slash - node), node) |
| 917 |
: talloc_strdup(ctx, "/"); |
| 918 |
if (!parent) |
| 919 |
errno = ENOMEM; |
| 920 |
|
| 921 |
return parent; |
| 922 |
} |
| 923 |
|
| 924 |
/* |
| 925 |
* What do parents say? |
| 926 |
* Temporary memory allocations are done with ctx. |
| 927 |
*/ |
| 928 |
static int ask_parents(struct connection *conn, const void *ctx, |
| 929 |
const char *name, unsigned int *perm) |
| 930 |
{ |
| 931 |
const struct node *node; |
| 932 |
|
| 933 |
do { |
| 934 |
name = get_parent(ctx, name); |
| 935 |
if (!name) |
| 936 |
return errno; |
| 937 |
node = read_node_const(conn, ctx, name); |
| 938 |
if (node) |
| 939 |
break; |
| 940 |
if (read_node_can_propagate_errno()) |
| 941 |
return errno; |
| 942 |
} while (!streq(name, "/")); |
| 943 |
|
| 944 |
/* No permission at root? We're in trouble. */ |
| 945 |
if (!node) { |
| 946 |
corrupt(conn, "No permissions file at root"); |
| 947 |
*perm = XS_PERM_NONE; |
| 948 |
return 0; |
| 949 |
} |
| 950 |
|
| 951 |
*perm = perm_for_conn_from_node(conn, node); |
| 952 |
|
| 953 |
return 0; |
| 954 |
} |
| 955 |
|
| 956 |
/* |
| 957 |
* We have a weird permissions system. You can allow someone into a |
| 958 |
* specific node without allowing it in the parents. If it's going to |
| 959 |
* fail, however, we don't want the errno to indicate any information |
| 960 |
* about the node. |
| 961 |
* Temporary memory allocations are done with ctx. |
| 962 |
*/ |
| 963 |
static int errno_from_parents(struct connection *conn, const void *ctx, |
| 964 |
const char *node, int errnum, unsigned int perm) |
| 965 |
{ |
| 966 |
unsigned int parent_perm = XS_PERM_NONE; |
| 967 |
|
| 968 |
/* We always tell them about memory failures. */ |
| 969 |
if (errnum == ENOMEM) |
| 970 |
return errnum; |
| 971 |
|
| 972 |
if (ask_parents(conn, ctx, node, &parent_perm)) |
| 973 |
return errno; |
| 974 |
if (parent_perm & perm) |
| 975 |
return errnum; |
| 976 |
return EACCES; |
| 977 |
} |
| 978 |
|
| 979 |
/* |
| 980 |
* If it fails, returns NULL and sets errno. |
| 981 |
* Temporary memory allocations are done with ctx. |
| 982 |
*/ |
| 983 |
static bool get_node_chk_perm(struct connection *conn, const void *ctx, |
| 984 |
const struct node *node, const char *name, |
| 985 |
unsigned int perm) |
| 986 |
{ |
| 987 |
bool success = node; |
| 988 |
|
| 989 |
/* If we don't have permission, we don't have node. */ |
| 990 |
if (node && (perm_for_conn_from_node(conn, node) & perm) != perm) { |
| 991 |
errno = EACCES; |
| 992 |
success = false; |
| 993 |
} |
| 994 |
/* Clean up errno if they weren't supposed to know. */ |
| 995 |
if (!success && !read_node_can_propagate_errno()) |
| 996 |
errno = errno_from_parents(conn, ctx, name, errno, perm); |
| 997 |
|
| 998 |
return success; |
| 999 |
} |
| 1000 |
|
| 1001 |
static struct buffered_data *new_buffer(void *ctx) |
| 1002 |
{ |
| 1003 |
struct buffered_data *data; |
| 1004 |
|
| 1005 |
data = talloc_zero(ctx, struct buffered_data); |
| 1006 |
if (data == NULL) |
| 1007 |
return NULL; |
| 1008 |
|
| 1009 |
data->inhdr = true; |
| 1010 |
return data; |
| 1011 |
} |
| 1012 |
|
| 1013 |
/* Return length of string (including nul) at this offset. |
| 1014 |
* If there is no nul, returns 0 for failure. |
| 1015 |
*/ |
| 1016 |
unsigned int get_string(const struct buffered_data *data, unsigned int offset) |
| 1017 |
{ |
| 1018 |
const char *nul; |
| 1019 |
|
| 1020 |
if (offset >= data->used) |
| 1021 |
return 0; |
| 1022 |
|
| 1023 |
nul = memchr(data->buffer + offset, 0, data->used - offset); |
| 1024 |
if (!nul) |
| 1025 |
return 0; |
| 1026 |
|
| 1027 |
return nul - (data->buffer + offset) + 1; |
| 1028 |
} |
| 1029 |
|
| 1030 |
/* Break input into vectors, return the number, fill in up to num of them. |
| 1031 |
* Always returns the actual number of nuls in the input. Stores the |
| 1032 |
* positions of the starts of the nul-terminated strings in vec. |
| 1033 |
* Callers who use this and then rely only on vec[] will |
| 1034 |
* ignore any data after the final nul. |
| 1035 |
*/ |
| 1036 |
unsigned int get_strings(struct buffered_data *data, |
| 1037 |
const char *vec[], unsigned int num) |
| 1038 |
{ |
| 1039 |
unsigned int off, i, len; |
| 1040 |
|
| 1041 |
off = i = 0; |
| 1042 |
while ((len = get_string(data, off)) != 0) { |
| 1043 |
if (i < num) |
| 1044 |
vec[i] = data->buffer + off; |
| 1045 |
i++; |
| 1046 |
off += len; |
| 1047 |
} |
| 1048 |
return i; |
| 1049 |
} |
| 1050 |
|
| 1051 |
static void send_error(struct connection *conn, int error) |
| 1052 |
{ |
| 1053 |
unsigned int i; |
| 1054 |
|
| 1055 |
for (i = 0; error != xsd_errors[i].errnum; i++) { |
| 1056 |
if (i == ARRAY_SIZE(xsd_errors) - 1) { |
| 1057 |
eprintf("xenstored: error %i untranslatable", error); |
| 1058 |
i = 0; /* EINVAL */ |
| 1059 |
break; |
| 1060 |
} |
| 1061 |
} |
| 1062 |
|
| 1063 |
acc_drop(conn); |
| 1064 |
|
| 1065 |
send_reply(conn, XS_ERROR, xsd_errors[i].errstring, |
| 1066 |
strlen(xsd_errors[i].errstring) + 1); |
| 1067 |
} |
| 1068 |
|
| 1069 |
void send_reply(struct connection *conn, enum xsd_sockmsg_type type, |
| 1070 |
const void *data, unsigned int len) |
| 1071 |
{ |
| 1072 |
struct buffered_data *bdata = conn->in; |
| 1073 |
|
| 1074 |
assert(type != XS_WATCH_EVENT); |
| 1075 |
|
| 1076 |
/* Commit accounting now, as later errors won't undo any changes. */ |
| 1077 |
acc_commit(conn); |
| 1078 |
|
| 1079 |
if ( len > XENSTORE_PAYLOAD_MAX ) { |
| 1080 |
send_error(conn, E2BIG); |
| 1081 |
return; |
| 1082 |
} |
| 1083 |
|
| 1084 |
if (!bdata) |
| 1085 |
return; |
| 1086 |
bdata->inhdr = true; |
| 1087 |
bdata->used = 0; |
| 1088 |
bdata->timeout_msec = 0; |
| 1089 |
bdata->watch_event = false; |
| 1090 |
|
| 1091 |
if (len <= DEFAULT_BUFFER_SIZE) { |
| 1092 |
bdata->buffer = bdata->default_buffer; |
| 1093 |
/* Don't check quota, path might be used for returning error. */ |
| 1094 |
domain_memory_add_nochk(conn, conn->id, |
| 1095 |
len + sizeof(bdata->hdr)); |
| 1096 |
} else { |
| 1097 |
bdata->buffer = talloc_array(bdata, char, len); |
| 1098 |
if (!bdata->buffer || |
| 1099 |
domain_memory_add_chk(conn, conn->id, |
| 1100 |
len + sizeof(bdata->hdr))) { |
| 1101 |
send_error(conn, ENOMEM); |
| 1102 |
return; |
| 1103 |
} |
| 1104 |
} |
| 1105 |
|
| 1106 |
conn->in = NULL; |
| 1107 |
|
| 1108 |
/* Update relevant header fields and fill in the message body. */ |
| 1109 |
bdata->hdr.msg.type = type; |
| 1110 |
bdata->hdr.msg.len = len; |
| 1111 |
memcpy(bdata->buffer, data, len); |
| 1112 |
|
| 1113 |
/* Queue for later transmission. */ |
| 1114 |
list_add_tail(&bdata->list, &conn->out_list); |
| 1115 |
bdata->on_out_list = true; |
| 1116 |
domain_outstanding_inc(conn); |
| 1117 |
} |
| 1118 |
|
| 1119 |
/* |
| 1120 |
* Send a watch event. |
| 1121 |
* As this is not directly related to the current command, errors can't be |
| 1122 |
* reported. |
| 1123 |
*/ |
| 1124 |
void send_event(struct buffered_data *req, struct connection *conn, |
| 1125 |
const char *path, const char *token) |
| 1126 |
{ |
| 1127 |
struct buffered_data *bdata, *bd; |
| 1128 |
unsigned int len; |
| 1129 |
|
| 1130 |
len = strlen(path) + 1 + strlen(token) + 1; |
| 1131 |
/* Don't try to send over-long events. */ |
| 1132 |
if (len > XENSTORE_PAYLOAD_MAX) |
| 1133 |
return; |
| 1134 |
|
| 1135 |
bdata = new_buffer(conn); |
| 1136 |
if (!bdata) |
| 1137 |
return; |
| 1138 |
|
| 1139 |
bdata->buffer = talloc_array(bdata, char, len); |
| 1140 |
if (!bdata->buffer) { |
| 1141 |
talloc_free(bdata); |
| 1142 |
return; |
| 1143 |
} |
| 1144 |
strcpy(bdata->buffer, path); |
| 1145 |
strcpy(bdata->buffer + strlen(path) + 1, token); |
| 1146 |
bdata->hdr.msg.type = XS_WATCH_EVENT; |
| 1147 |
bdata->hdr.msg.len = len; |
| 1148 |
|
| 1149 |
/* |
| 1150 |
* Check whether an identical event is pending already. |
| 1151 |
* Special events are excluded from that check. |
| 1152 |
*/ |
| 1153 |
if (path[0] != '@') { |
| 1154 |
list_for_each_entry(bd, &conn->out_list, list) { |
| 1155 |
if (bd->watch_event && bd->hdr.msg.len == len && |
| 1156 |
!memcmp(bdata->buffer, bd->buffer, len)) { |
| 1157 |
trace("dropping duplicate watch %s %s for domain %u\n", |
| 1158 |
path, token, conn->id); |
| 1159 |
talloc_free(bdata); |
| 1160 |
return; |
| 1161 |
} |
| 1162 |
} |
| 1163 |
} |
| 1164 |
|
| 1165 |
if (domain_memory_add_chk(conn, conn->id, len + sizeof(bdata->hdr))) { |
| 1166 |
talloc_free(bdata); |
| 1167 |
return; |
| 1168 |
} |
| 1169 |
|
| 1170 |
if (timeout_watch_event_msec && domain_is_unprivileged(conn)) { |
| 1171 |
bdata->timeout_msec = get_now_msec() + timeout_watch_event_msec; |
| 1172 |
if (!conn->timeout_msec) |
| 1173 |
conn->timeout_msec = bdata->timeout_msec; |
| 1174 |
} |
| 1175 |
|
| 1176 |
bdata->watch_event = true; |
| 1177 |
bdata->pend.req = req; |
| 1178 |
if (req) |
| 1179 |
req->pend.ref.event_cnt++; |
| 1180 |
|
| 1181 |
/* Queue for later transmission. */ |
| 1182 |
list_add_tail(&bdata->list, &conn->out_list); |
| 1183 |
bdata->on_out_list = true; |
| 1184 |
} |
| 1185 |
|
| 1186 |
/* Some routines (write, mkdir, etc) just need a non-error return */ |
| 1187 |
void send_ack(struct connection *conn, enum xsd_sockmsg_type type) |
| 1188 |
{ |
| 1189 |
send_reply(conn, type, "OK", sizeof("OK")); |
| 1190 |
} |
| 1191 |
|
| 1192 |
static bool valid_chars(const char *node) |
| 1193 |
{ |
| 1194 |
/* Nodes can have lots of crap. */ |
| 1195 |
return (strspn(node, |
| 1196 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 1197 |
"abcdefghijklmnopqrstuvwxyz" |
| 1198 |
"0123456789-/_@") == strlen(node)); |
| 1199 |
} |
| 1200 |
|
| 1201 |
/* We expect one arg in the input: return NULL otherwise. |
| 1202 |
* The payload must contain exactly one nul, at the end. |
| 1203 |
*/ |
| 1204 |
const char *onearg(struct buffered_data *in) |
| 1205 |
{ |
| 1206 |
if (!in->used || get_string(in, 0) != in->used) |
| 1207 |
return NULL; |
| 1208 |
return in->buffer; |
| 1209 |
} |
| 1210 |
|
| 1211 |
static char *node_perms_to_strings(const struct node *node, unsigned int *len) |
| 1212 |
{ |
| 1213 |
unsigned int i; |
| 1214 |
char *strings = NULL; |
| 1215 |
char buffer[MAX_STRLEN(unsigned int) + 1]; |
| 1216 |
|
| 1217 |
for (*len = 0, i = 0; i < node->hdr.num_perms; i++) { |
| 1218 |
if (!xenstore_perm_to_string(&node->perms[i], buffer, |
| 1219 |
sizeof(buffer))) |
| 1220 |
return NULL; |
| 1221 |
|
| 1222 |
strings = talloc_realloc(node, strings, char, |
| 1223 |
*len + strlen(buffer) + 1); |
| 1224 |
if (!strings) |
| 1225 |
return NULL; |
| 1226 |
strcpy(strings + *len, buffer); |
| 1227 |
*len += strlen(buffer) + 1; |
| 1228 |
} |
| 1229 |
return strings; |
| 1230 |
} |
| 1231 |
|
| 1232 |
const char *canonicalize(struct connection *conn, const void *ctx, |
| 1233 |
const char *node, bool allow_special) |
| 1234 |
{ |
| 1235 |
const char *name; |
| 1236 |
int local_off = 0; |
| 1237 |
unsigned int domid; |
| 1238 |
|
| 1239 |
/* |
| 1240 |
* Invalid if any of: |
| 1241 |
* - no node at all |
| 1242 |
* - illegal character in node |
| 1243 |
* - starts with '@' but no special node allowed |
| 1244 |
*/ |
| 1245 |
errno = EINVAL; |
| 1246 |
if (!node || |
| 1247 |
!valid_chars(node) || |
| 1248 |
(node[0] == '@' && !allow_special)) |
| 1249 |
return NULL; |
| 1250 |
|
| 1251 |
if (node[0] != '/' && node[0] != '@') { |
| 1252 |
name = talloc_asprintf(ctx, "%s/%s", get_implicit_path(conn), |
| 1253 |
node); |
| 1254 |
if (!name) |
| 1255 |
return NULL; |
| 1256 |
} else |
| 1257 |
name = node; |
| 1258 |
|
| 1259 |
if (sscanf(name, "/local/domain/%5u/%n", &domid, &local_off) != 1) |
| 1260 |
local_off = 0; |
| 1261 |
|
| 1262 |
/* |
| 1263 |
* Only valid if: |
| 1264 |
* - doesn't end in / (unless it's just "/") |
| 1265 |
* - no double // |
| 1266 |
* - not violating max allowed path length |
| 1267 |
*/ |
| 1268 |
if (!(strends(name, "/") && !streq(name, "/")) && |
| 1269 |
!strstr(name, "//") && |
| 1270 |
!domain_max_chk(conn, ACC_PATHLEN, strlen(name) - local_off)) |
| 1271 |
return name; |
| 1272 |
|
| 1273 |
/* Release the memory if 'name' was allocated by us. */ |
| 1274 |
if (name != node) |
| 1275 |
talloc_free(name); |
| 1276 |
|
| 1277 |
return NULL; |
| 1278 |
} |
| 1279 |
|
| 1280 |
static struct node *get_node(struct connection *conn, const void *ctx, |
| 1281 |
const char *name, const char **canonical_name, |
| 1282 |
unsigned int perm, bool allow_special) |
| 1283 |
{ |
| 1284 |
struct node *node; |
| 1285 |
|
| 1286 |
*canonical_name = canonicalize(conn, ctx, name, allow_special); |
| 1287 |
if (!*canonical_name) |
| 1288 |
return NULL; |
| 1289 |
|
| 1290 |
node = read_node(conn, ctx, *canonical_name); |
| 1291 |
|
| 1292 |
return get_node_chk_perm(conn, ctx, node, *canonical_name, perm) |
| 1293 |
? node : NULL; |
| 1294 |
} |
| 1295 |
|
| 1296 |
static const struct node *get_node_const(struct connection *conn, |
| 1297 |
const void *ctx, const char *name, |
| 1298 |
unsigned int perm, bool allow_special) |
| 1299 |
{ |
| 1300 |
const char *tmp_name; |
| 1301 |
const struct node *node; |
| 1302 |
|
| 1303 |
tmp_name = canonicalize(conn, ctx, name, allow_special); |
| 1304 |
if (!tmp_name) |
| 1305 |
return NULL; |
| 1306 |
|
| 1307 |
node = read_node_const(conn, ctx, tmp_name); |
| 1308 |
|
| 1309 |
return get_node_chk_perm(conn, ctx, node, tmp_name, perm) ? node : NULL; |
| 1310 |
} |
| 1311 |
|
| 1312 |
static int send_directory(const void *ctx, struct connection *conn, |
| 1313 |
struct buffered_data *in) |
| 1314 |
{ |
| 1315 |
const struct node *node; |
| 1316 |
|
| 1317 |
node = get_node_const(conn, ctx, onearg(in), XS_PERM_READ, false); |
| 1318 |
if (!node) |
| 1319 |
return errno; |
| 1320 |
|
| 1321 |
send_reply(conn, XS_DIRECTORY, node->children, node->hdr.childlen); |
| 1322 |
|
| 1323 |
return 0; |
| 1324 |
} |
| 1325 |
|
| 1326 |
static int send_directory_part(const void *ctx, struct connection *conn, |
| 1327 |
struct buffered_data *in) |
| 1328 |
{ |
| 1329 |
unsigned int off, len, maxlen, genlen; |
| 1330 |
char *child, *data; |
| 1331 |
const struct node *node; |
| 1332 |
char gen[24]; |
| 1333 |
|
| 1334 |
if (xenstore_count_strings(in->buffer, in->used) != 2) |
| 1335 |
return EINVAL; |
| 1336 |
|
| 1337 |
/* First arg is node name. */ |
| 1338 |
node = get_node_const(conn, ctx, in->buffer, XS_PERM_READ, false); |
| 1339 |
if (!node) |
| 1340 |
return errno; |
| 1341 |
|
| 1342 |
/* Second arg is childlist offset. */ |
| 1343 |
off = atoi(in->buffer + strlen(in->buffer) + 1); |
| 1344 |
|
| 1345 |
genlen = snprintf(gen, sizeof(gen), "%"PRIu64, node->hdr.generation) + |
| 1346 |
1; |
| 1347 |
|
| 1348 |
/* Offset behind list: just return a list with an empty string. */ |
| 1349 |
if (off >= node->hdr.childlen) { |
| 1350 |
gen[genlen] = 0; |
| 1351 |
send_reply(conn, XS_DIRECTORY_PART, gen, genlen + 1); |
| 1352 |
return 0; |
| 1353 |
} |
| 1354 |
|
| 1355 |
len = 0; |
| 1356 |
maxlen = XENSTORE_PAYLOAD_MAX - genlen - 1; |
| 1357 |
child = node->children + off; |
| 1358 |
|
| 1359 |
while (len + strlen(child) < maxlen) { |
| 1360 |
len += strlen(child) + 1; |
| 1361 |
child += strlen(child) + 1; |
| 1362 |
if (off + len == node->hdr.childlen) |
| 1363 |
break; |
| 1364 |
} |
| 1365 |
|
| 1366 |
data = talloc_array(ctx, char, genlen + len + 1); |
| 1367 |
if (!data) |
| 1368 |
return ENOMEM; |
| 1369 |
|
| 1370 |
memcpy(data, gen, genlen); |
| 1371 |
memcpy(data + genlen, node->children + off, len); |
| 1372 |
if (off + len == node->hdr.childlen) { |
| 1373 |
data[genlen + len] = 0; |
| 1374 |
len++; |
| 1375 |
} |
| 1376 |
|
| 1377 |
send_reply(conn, XS_DIRECTORY_PART, data, genlen + len); |
| 1378 |
|
| 1379 |
return 0; |
| 1380 |
} |
| 1381 |
|
| 1382 |
static int do_read(const void *ctx, struct connection *conn, |
| 1383 |
struct buffered_data *in) |
| 1384 |
{ |
| 1385 |
const struct node *node; |
| 1386 |
|
| 1387 |
node = get_node_const(conn, ctx, onearg(in), XS_PERM_READ, false); |
| 1388 |
if (!node) |
| 1389 |
return errno; |
| 1390 |
|
| 1391 |
send_reply(conn, XS_READ, node->data, node->hdr.datalen); |
| 1392 |
|
| 1393 |
return 0; |
| 1394 |
} |
| 1395 |
|
| 1396 |
/* Must not be / */ |
| 1397 |
static char *basename(const char *name) |
| 1398 |
{ |
| 1399 |
return strrchr(name, '/') + 1; |
| 1400 |
} |
| 1401 |
|
| 1402 |
static int add_child(const void *ctx, struct node *parent, const char *name) |
| 1403 |
{ |
| 1404 |
const char *base; |
| 1405 |
unsigned int baselen; |
| 1406 |
char *children; |
| 1407 |
|
| 1408 |
base = basename(name); |
| 1409 |
baselen = strlen(base) + 1; |
| 1410 |
children = talloc_array(ctx, char, parent->hdr.childlen + baselen); |
| 1411 |
if (!children) |
| 1412 |
return ENOMEM; |
| 1413 |
memcpy(children, parent->children, parent->hdr.childlen); |
| 1414 |
memcpy(children + parent->hdr.childlen, base, baselen); |
| 1415 |
parent->children = children; |
| 1416 |
parent->hdr.childlen += baselen; |
| 1417 |
|
| 1418 |
return 0; |
| 1419 |
} |
| 1420 |
|
| 1421 |
static struct node *construct_node(struct connection *conn, const void *ctx, |
| 1422 |
const char *name) |
| 1423 |
{ |
| 1424 |
const char **names = NULL; |
| 1425 |
unsigned int levels = 0; |
| 1426 |
struct node *node = NULL; |
| 1427 |
struct node *parent = NULL; |
| 1428 |
const char *parentname = talloc_strdup(ctx, name); |
| 1429 |
|
| 1430 |
if (!parentname) |
| 1431 |
return NULL; |
| 1432 |
|
| 1433 |
/* Walk the path up until an existing node is found. */ |
| 1434 |
while (!parent) { |
| 1435 |
names = talloc_realloc(ctx, names, const char *, levels + 1); |
| 1436 |
if (!names) |
| 1437 |
goto nomem; |
| 1438 |
|
| 1439 |
/* |
| 1440 |
* names[0] is the name of the node to construct initially, |
| 1441 |
* names[1] is its parent, and so on. |
| 1442 |
*/ |
| 1443 |
names[levels] = parentname; |
| 1444 |
parentname = get_parent(ctx, parentname); |
| 1445 |
if (!parentname) |
| 1446 |
return NULL; |
| 1447 |
|
| 1448 |
/* Try to read parent node until we found an existing one. */ |
| 1449 |
parent = read_node(conn, ctx, parentname); |
| 1450 |
if (!parent && (errno != ENOENT || !strcmp(parentname, "/"))) |
| 1451 |
return NULL; |
| 1452 |
|
| 1453 |
levels++; |
| 1454 |
} |
| 1455 |
|
| 1456 |
/* Walk the path down again constructing the missing nodes. */ |
| 1457 |
for (; levels > 0; levels--) { |
| 1458 |
/* Add child to parent. */ |
| 1459 |
if (add_child(ctx, parent, names[levels - 1])) |
| 1460 |
goto nomem; |
| 1461 |
|
| 1462 |
/* Allocate node */ |
| 1463 |
node = talloc(ctx, struct node); |
| 1464 |
if (!node) |
| 1465 |
goto nomem; |
| 1466 |
node->name = talloc_steal(node, names[levels - 1]); |
| 1467 |
|
| 1468 |
/* Inherit permissions, unpriv domains own what they create. */ |
| 1469 |
node->hdr.num_perms = parent->hdr.num_perms; |
| 1470 |
node->perms = talloc_memdup(node, parent->perms, |
| 1471 |
node->hdr.num_perms * |
| 1472 |
sizeof(*node->perms)); |
| 1473 |
if (!node->perms) |
| 1474 |
goto nomem; |
| 1475 |
if (domain_is_unprivileged(conn)) |
| 1476 |
node->perms[0].id = conn->id; |
| 1477 |
|
| 1478 |
/* No children, no data */ |
| 1479 |
node->children = node->data = NULL; |
| 1480 |
node->hdr.childlen = node->hdr.datalen = 0; |
| 1481 |
node->acc.memory = 0; |
| 1482 |
node->parent = parent; |
| 1483 |
|
| 1484 |
parent = node; |
| 1485 |
} |
| 1486 |
|
| 1487 |
return node; |
| 1488 |
|
| 1489 |
nomem: |
| 1490 |
errno = ENOMEM; |
| 1491 |
return NULL; |
| 1492 |
} |
| 1493 |
|
| 1494 |
static void destroy_node_rm(struct connection *conn, struct node *node) |
| 1495 |
{ |
| 1496 |
if (streq(node->name, "/")) |
| 1497 |
corrupt(NULL, "Destroying root node!"); |
| 1498 |
|
| 1499 |
db_delete(conn, node->db_name, &node->acc); |
| 1500 |
} |
| 1501 |
|
| 1502 |
static int destroy_node(struct connection *conn, struct node *node) |
| 1503 |
{ |
| 1504 |
destroy_node_rm(conn, node); |
| 1505 |
|
| 1506 |
/* |
| 1507 |
* It is not possible to easily revert the changes in a transaction. |
| 1508 |
* So if the failure happens in a transaction, mark it as fail to |
| 1509 |
* prevent any commit. |
| 1510 |
*/ |
| 1511 |
if ( conn->transaction ) |
| 1512 |
fail_transaction(conn->transaction); |
| 1513 |
|
| 1514 |
return 0; |
| 1515 |
} |
| 1516 |
|
| 1517 |
static struct node *create_node(struct connection *conn, const void *ctx, |
| 1518 |
const char *name, |
| 1519 |
void *data, unsigned int datalen) |
| 1520 |
{ |
| 1521 |
struct node *node, *i, *j; |
| 1522 |
int ret; |
| 1523 |
|
| 1524 |
node = construct_node(conn, ctx, name); |
| 1525 |
if (!node) |
| 1526 |
return NULL; |
| 1527 |
|
| 1528 |
if (conn && conn->transaction) |
| 1529 |
ta_node_created(conn->transaction); |
| 1530 |
|
| 1531 |
node->data = data; |
| 1532 |
node->hdr.datalen = datalen; |
| 1533 |
|
| 1534 |
/* |
| 1535 |
* We write out the nodes bottom up. |
| 1536 |
* All new created nodes will have i->parent set, while the final |
| 1537 |
* node will be already existing and won't have i->parent set. |
| 1538 |
* New nodes are subject to quota handling. |
| 1539 |
*/ |
| 1540 |
for (i = node; i; i = i->parent) { |
| 1541 |
/* i->parent is set for each new node, so check quota. */ |
| 1542 |
if (i->parent && |
| 1543 |
domain_nbentry(conn) >= hard_quotas[ACC_NODES].val) { |
| 1544 |
ret = ENOSPC; |
| 1545 |
goto err; |
| 1546 |
} |
| 1547 |
|
| 1548 |
ret = write_node(conn, i, i->parent ? NODE_CREATE : NODE_MODIFY, |
| 1549 |
false); |
| 1550 |
if (ret) |
| 1551 |
goto err; |
| 1552 |
|
| 1553 |
/* Account for new node */ |
| 1554 |
if (i->parent) { |
| 1555 |
if (domain_nbentry_inc(conn, get_node_owner(i))) { |
| 1556 |
destroy_node_rm(conn, i); |
| 1557 |
return NULL; |
| 1558 |
} |
| 1559 |
} |
| 1560 |
} |
| 1561 |
|
| 1562 |
return node; |
| 1563 |
|
| 1564 |
err: |
| 1565 |
/* |
| 1566 |
* We failed to update TDB for some of the nodes. Undo any work that |
| 1567 |
* have already been done. |
| 1568 |
*/ |
| 1569 |
for (j = node; j != i; j = j->parent) |
| 1570 |
destroy_node(conn, j); |
| 1571 |
|
| 1572 |
/* We don't need to keep the nodes around, so free them. */ |
| 1573 |
i = node; |
| 1574 |
while (i) { |
| 1575 |
j = i; |
| 1576 |
i = i->parent; |
| 1577 |
talloc_free(j); |
| 1578 |
} |
| 1579 |
|
| 1580 |
errno = ret; |
| 1581 |
|
| 1582 |
return NULL; |
| 1583 |
} |
| 1584 |
|
| 1585 |
/* path, data... */ |
| 1586 |
static int do_write(const void *ctx, struct connection *conn, |
| 1587 |
struct buffered_data *in) |
| 1588 |
{ |
| 1589 |
unsigned int offset, datalen; |
| 1590 |
struct node *node; |
| 1591 |
const char *vec[1] = { NULL }; /* gcc4 + -W + -Werror fucks code. */ |
| 1592 |
const char *name; |
| 1593 |
|
| 1594 |
/* Extra "strings" can be created by binary data. */ |
| 1595 |
if (get_strings(in, vec, ARRAY_SIZE(vec)) < ARRAY_SIZE(vec)) |
| 1596 |
return EINVAL; |
| 1597 |
|
| 1598 |
offset = strlen(vec[0]) + 1; |
| 1599 |
datalen = in->used - offset; |
| 1600 |
|
| 1601 |
node = get_node(conn, ctx, vec[0], &name, XS_PERM_WRITE, false); |
| 1602 |
if (!node) { |
| 1603 |
/* No permissions, invalid input? */ |
| 1604 |
if (errno != ENOENT) |
| 1605 |
return errno; |
| 1606 |
node = create_node(conn, ctx, name, in->buffer + offset, |
| 1607 |
datalen); |
| 1608 |
if (!node) |
| 1609 |
return errno; |
| 1610 |
} else { |
| 1611 |
node->data = in->buffer + offset; |
| 1612 |
node->hdr.datalen = datalen; |
| 1613 |
if (write_node(conn, node, NODE_MODIFY, false)) |
| 1614 |
return errno; |
| 1615 |
} |
| 1616 |
|
| 1617 |
fire_watches(conn, ctx, name, node, false, NULL); |
| 1618 |
send_ack(conn, XS_WRITE); |
| 1619 |
|
| 1620 |
return 0; |
| 1621 |
} |
| 1622 |
|
| 1623 |
static int do_mkdir(const void *ctx, struct connection *conn, |
| 1624 |
struct buffered_data *in) |
| 1625 |
{ |
| 1626 |
struct node *node; |
| 1627 |
const char *name; |
| 1628 |
|
| 1629 |
node = get_node(conn, ctx, onearg(in), &name, XS_PERM_WRITE, false); |
| 1630 |
|
| 1631 |
/* If it already exists, fine. */ |
| 1632 |
if (!node) { |
| 1633 |
/* No permissions? */ |
| 1634 |
if (errno != ENOENT) |
| 1635 |
return errno; |
| 1636 |
if (!name) |
| 1637 |
return ENOMEM; |
| 1638 |
node = create_node(conn, ctx, name, NULL, 0); |
| 1639 |
if (!node) |
| 1640 |
return errno; |
| 1641 |
fire_watches(conn, ctx, name, node, false, NULL); |
| 1642 |
} |
| 1643 |
send_ack(conn, XS_MKDIR); |
| 1644 |
|
| 1645 |
return 0; |
| 1646 |
} |
| 1647 |
|
| 1648 |
/* Delete memory using memmove. */ |
| 1649 |
static void memdel(void *mem, unsigned off, unsigned len, unsigned total) |
| 1650 |
{ |
| 1651 |
memmove(mem + off, mem + off + len, total - off - len); |
| 1652 |
} |
| 1653 |
|
| 1654 |
static int remove_child_entry(struct connection *conn, struct node *node, |
| 1655 |
size_t offset) |
| 1656 |
{ |
| 1657 |
size_t childlen = strlen(node->children + offset); |
| 1658 |
|
| 1659 |
memdel(node->children, offset, childlen + 1, node->hdr.childlen); |
| 1660 |
node->hdr.childlen -= childlen + 1; |
| 1661 |
|
| 1662 |
return write_node(conn, node, NODE_MODIFY, true); |
| 1663 |
} |
| 1664 |
|
| 1665 |
static int delete_child(struct connection *conn, |
| 1666 |
struct node *node, const char *childname) |
| 1667 |
{ |
| 1668 |
unsigned int i; |
| 1669 |
|
| 1670 |
for (i = 0; i < node->hdr.childlen; |
| 1671 |
i += strlen(node->children + i) + 1) { |
| 1672 |
if (streq(node->children + i, childname)) { |
| 1673 |
errno = remove_child_entry(conn, node, i) ? EIO : 0; |
| 1674 |
return errno; |
| 1675 |
} |
| 1676 |
} |
| 1677 |
corrupt(conn, "Can't find child '%s' in %s", childname, node->name); |
| 1678 |
|
| 1679 |
errno = EIO; |
| 1680 |
return errno; |
| 1681 |
} |
| 1682 |
|
| 1683 |
static int delnode_sub(const void *ctx, struct connection *conn, |
| 1684 |
struct node *node, void *arg) |
| 1685 |
{ |
| 1686 |
const char *root = arg; |
| 1687 |
bool watch_exact; |
| 1688 |
int ret; |
| 1689 |
const char *db_name; |
| 1690 |
|
| 1691 |
/* Any error here will probably be repeated for all following calls. */ |
| 1692 |
ret = access_node(conn, node, NODE_ACCESS_DELETE, &db_name); |
| 1693 |
if (ret > 0) |
| 1694 |
return WALK_TREE_SUCCESS_STOP; |
| 1695 |
|
| 1696 |
if (domain_nbentry_dec(conn, get_node_owner(node))) |
| 1697 |
return WALK_TREE_ERROR_STOP; |
| 1698 |
|
| 1699 |
if (!ret) |
| 1700 |
db_delete(conn, db_name, &node->acc); |
| 1701 |
|
| 1702 |
/* |
| 1703 |
* Fire the watches now, when we can still see the node permissions. |
| 1704 |
* This fine as we are single threaded and the next possible read will |
| 1705 |
* be handled only after the node has been really removed. |
| 1706 |
*/ |
| 1707 |
watch_exact = strcmp(root, node->name); |
| 1708 |
fire_watches(conn, ctx, node->name, node, watch_exact, NULL); |
| 1709 |
|
| 1710 |
return WALK_TREE_RM_CHILDENTRY; |
| 1711 |
} |
| 1712 |
|
| 1713 |
int rm_node(struct connection *conn, const void *ctx, const char *name) |
| 1714 |
{ |
| 1715 |
struct node *parent; |
| 1716 |
char *parentname = get_parent(ctx, name); |
| 1717 |
struct walk_funcs walkfuncs = { .exit = delnode_sub }; |
| 1718 |
int ret; |
| 1719 |
|
| 1720 |
if (!parentname) |
| 1721 |
return errno; |
| 1722 |
|
| 1723 |
parent = read_node(conn, ctx, parentname); |
| 1724 |
if (!parent) |
| 1725 |
return read_node_can_propagate_errno() ? errno : EINVAL; |
| 1726 |
|
| 1727 |
ret = walk_node_tree(ctx, conn, name, &walkfuncs, (void *)name); |
| 1728 |
if (ret < 0) { |
| 1729 |
if (ret == WALK_TREE_ERROR_STOP) { |
| 1730 |
/* |
| 1731 |
* This can't be triggered by an unprivileged guest, |
| 1732 |
* so calling corrupt() is fine here. |
| 1733 |
* In fact it is needed in order to fix a potential |
| 1734 |
* accounting inconsistency. |
| 1735 |
*/ |
| 1736 |
corrupt(conn, "error when deleting sub-nodes of %s\n", |
| 1737 |
name); |
| 1738 |
errno = EIO; |
| 1739 |
} |
| 1740 |
return errno; |
| 1741 |
} |
| 1742 |
|
| 1743 |
if (delete_child(conn, parent, basename(name))) |
| 1744 |
return errno; |
| 1745 |
|
| 1746 |
return 0; |
| 1747 |
} |
| 1748 |
|
| 1749 |
|
| 1750 |
static int do_rm(const void *ctx, struct connection *conn, |
| 1751 |
struct buffered_data *in) |
| 1752 |
{ |
| 1753 |
struct node *node; |
| 1754 |
int ret; |
| 1755 |
const char *name; |
| 1756 |
char *parentname; |
| 1757 |
|
| 1758 |
node = get_node(conn, ctx, onearg(in), &name, XS_PERM_WRITE, false); |
| 1759 |
if (!node) { |
| 1760 |
/* Didn't exist already? Fine, if parent exists. */ |
| 1761 |
if (errno == ENOENT) { |
| 1762 |
if (!name) |
| 1763 |
return ENOMEM; |
| 1764 |
parentname = get_parent(ctx, name); |
| 1765 |
if (!parentname) |
| 1766 |
return errno; |
| 1767 |
node = read_node(conn, ctx, parentname); |
| 1768 |
if (node) { |
| 1769 |
send_ack(conn, XS_RM); |
| 1770 |
return 0; |
| 1771 |
} |
| 1772 |
/* Restore errno, just in case. */ |
| 1773 |
if (!read_node_can_propagate_errno()) |
| 1774 |
errno = ENOENT; |
| 1775 |
} |
| 1776 |
return errno; |
| 1777 |
} |
| 1778 |
|
| 1779 |
if (streq(name, "/")) |
| 1780 |
return EINVAL; |
| 1781 |
|
| 1782 |
ret = rm_node(conn, ctx, name); |
| 1783 |
if (ret) |
| 1784 |
return ret; |
| 1785 |
|
| 1786 |
send_ack(conn, XS_RM); |
| 1787 |
|
| 1788 |
return 0; |
| 1789 |
} |
| 1790 |
|
| 1791 |
|
| 1792 |
static int do_get_perms(const void *ctx, struct connection *conn, |
| 1793 |
struct buffered_data *in) |
| 1794 |
{ |
| 1795 |
const struct node *node; |
| 1796 |
char *strings; |
| 1797 |
unsigned int len; |
| 1798 |
|
| 1799 |
node = get_node_const(conn, ctx, onearg(in), XS_PERM_READ, true); |
| 1800 |
if (!node) |
| 1801 |
return errno; |
| 1802 |
|
| 1803 |
strings = node_perms_to_strings(node, &len); |
| 1804 |
if (!strings) |
| 1805 |
return errno; |
| 1806 |
|
| 1807 |
send_reply(conn, XS_GET_PERMS, strings, len); |
| 1808 |
|
| 1809 |
return 0; |
| 1810 |
} |
| 1811 |
|
| 1812 |
static int do_set_perms(const void *ctx, struct connection *conn, |
| 1813 |
struct buffered_data *in) |
| 1814 |
{ |
| 1815 |
struct node_perms perms, old_perms; |
| 1816 |
const char *name; |
| 1817 |
char *permstr; |
| 1818 |
struct node *node; |
| 1819 |
|
| 1820 |
perms.num = xenstore_count_strings(in->buffer, in->used); |
| 1821 |
if (perms.num < 2) |
| 1822 |
return EINVAL; |
| 1823 |
|
| 1824 |
perms.num--; |
| 1825 |
if (domain_max_chk(conn, ACC_NPERM, perms.num)) |
| 1826 |
return ENOSPC; |
| 1827 |
|
| 1828 |
permstr = in->buffer + strlen(in->buffer) + 1; |
| 1829 |
|
| 1830 |
perms.p = talloc_array(ctx, struct xs_permissions, perms.num); |
| 1831 |
if (!perms.p) |
| 1832 |
return ENOMEM; |
| 1833 |
if (!xenstore_strings_to_perms(perms.p, perms.num, permstr)) |
| 1834 |
return errno; |
| 1835 |
|
| 1836 |
if (domain_alloc_permrefs(&perms)) |
| 1837 |
return ENOMEM; |
| 1838 |
if (perms.p[0].perms & XS_PERM_IGNORE) |
| 1839 |
return ENOENT; |
| 1840 |
|
| 1841 |
/* We must own node to do this (tools can do this too). */ |
| 1842 |
node = get_node(conn, ctx, in->buffer, &name, |
| 1843 |
XS_PERM_WRITE | XS_PERM_OWNER, true); |
| 1844 |
if (!node) |
| 1845 |
return errno; |
| 1846 |
|
| 1847 |
/* Unprivileged domains may not change the owner. */ |
| 1848 |
if (domain_is_unprivileged(conn) && |
| 1849 |
perms.p[0].id != get_node_owner(node)) |
| 1850 |
return EPERM; |
| 1851 |
|
| 1852 |
node_to_node_perms(node, &old_perms); |
| 1853 |
if (domain_nbentry_dec(conn, get_node_owner(node))) |
| 1854 |
return ENOMEM; |
| 1855 |
node_perms_to_node(&perms, node); |
| 1856 |
if (domain_nbentry_inc(conn, get_node_owner(node))) |
| 1857 |
return ENOMEM; |
| 1858 |
|
| 1859 |
if (write_node(conn, node, NODE_MODIFY, false)) |
| 1860 |
return errno; |
| 1861 |
|
| 1862 |
fire_watches(conn, ctx, name, node, false, &old_perms); |
| 1863 |
send_ack(conn, XS_SET_PERMS); |
| 1864 |
|
| 1865 |
return 0; |
| 1866 |
} |
| 1867 |
|
| 1868 |
static char *child_name(const void *ctx, const char *s1, const char *s2) |
| 1869 |
{ |
| 1870 |
if (strcmp(s1, "/")) |
| 1871 |
return talloc_asprintf(ctx, "%s/%s", s1, s2); |
| 1872 |
return talloc_asprintf(ctx, "/%s", s2); |
| 1873 |
} |
| 1874 |
|
| 1875 |
static int rm_from_parent(struct connection *conn, struct node *parent, |
| 1876 |
const char *name) |
| 1877 |
{ |
| 1878 |
size_t off; |
| 1879 |
|
| 1880 |
if (!parent) |
| 1881 |
return WALK_TREE_ERROR_STOP; |
| 1882 |
|
| 1883 |
for (off = parent->childoff - 1; off && parent->children[off - 1]; |
| 1884 |
off--); |
| 1885 |
if (remove_child_entry(conn, parent, off)) { |
| 1886 |
log("treewalk: child entry could not be removed from '%s'", |
| 1887 |
parent->name); |
| 1888 |
return WALK_TREE_ERROR_STOP; |
| 1889 |
} |
| 1890 |
parent->childoff = off; |
| 1891 |
|
| 1892 |
return WALK_TREE_OK; |
| 1893 |
} |
| 1894 |
|
| 1895 |
static int walk_call_func(const void *ctx, struct connection *conn, |
| 1896 |
struct node *node, struct node *parent, void *arg, |
| 1897 |
int (*func)(const void *ctx, struct connection *conn, |
| 1898 |
struct node *node, void *arg)) |
| 1899 |
{ |
| 1900 |
int ret; |
| 1901 |
|
| 1902 |
if (!func) |
| 1903 |
return WALK_TREE_OK; |
| 1904 |
|
| 1905 |
ret = func(ctx, conn, node, arg); |
| 1906 |
if (ret == WALK_TREE_RM_CHILDENTRY && parent) |
| 1907 |
ret = rm_from_parent(conn, parent, node->name); |
| 1908 |
|
| 1909 |
return ret; |
| 1910 |
} |
| 1911 |
|
| 1912 |
int walk_node_tree(const void *ctx, struct connection *conn, const char *root, |
| 1913 |
struct walk_funcs *funcs, void *arg) |
| 1914 |
{ |
| 1915 |
int ret = 0; |
| 1916 |
void *tmpctx; |
| 1917 |
char *name; |
| 1918 |
struct node *node = NULL; |
| 1919 |
struct node *parent = NULL; |
| 1920 |
|
| 1921 |
tmpctx = talloc_new(ctx); |
| 1922 |
if (!tmpctx) { |
| 1923 |
errno = ENOMEM; |
| 1924 |
return WALK_TREE_ERROR_STOP; |
| 1925 |
} |
| 1926 |
name = talloc_strdup(tmpctx, root); |
| 1927 |
if (!name) { |
| 1928 |
errno = ENOMEM; |
| 1929 |
talloc_free(tmpctx); |
| 1930 |
return WALK_TREE_ERROR_STOP; |
| 1931 |
} |
| 1932 |
|
| 1933 |
/* Continue the walk until an error is returned. */ |
| 1934 |
while (ret >= 0) { |
| 1935 |
/* node == NULL possible only for the initial loop iteration. */ |
| 1936 |
if (node) { |
| 1937 |
/* Go one step up if ret or if last child finished. */ |
| 1938 |
if (ret || node->childoff >= node->hdr.childlen) { |
| 1939 |
parent = node->parent; |
| 1940 |
/* Call function AFTER processing a node. */ |
| 1941 |
ret = walk_call_func(ctx, conn, node, parent, |
| 1942 |
arg, funcs->exit); |
| 1943 |
/* Last node, so exit loop. */ |
| 1944 |
if (!parent) |
| 1945 |
break; |
| 1946 |
talloc_free(node); |
| 1947 |
/* Continue with parent. */ |
| 1948 |
node = parent; |
| 1949 |
continue; |
| 1950 |
} |
| 1951 |
/* Get next child of current node. */ |
| 1952 |
name = child_name(tmpctx, node->name, |
| 1953 |
node->children + node->childoff); |
| 1954 |
if (!name) { |
| 1955 |
ret = WALK_TREE_ERROR_STOP; |
| 1956 |
break; |
| 1957 |
} |
| 1958 |
/* Point to next child. */ |
| 1959 |
node->childoff += strlen(node->children + |
| 1960 |
node->childoff) + 1; |
| 1961 |
/* Descent into children. */ |
| 1962 |
parent = node; |
| 1963 |
} |
| 1964 |
/* Read next node (root node or next child). */ |
| 1965 |
node = read_node(conn, tmpctx, name); |
| 1966 |
if (!node) { |
| 1967 |
/* Child not found - should not happen! */ |
| 1968 |
/* ENOENT case can be handled by supplied function. */ |
| 1969 |
if (errno == ENOENT && funcs->enoent) |
| 1970 |
ret = funcs->enoent(ctx, conn, parent, name, |
| 1971 |
arg); |
| 1972 |
else |
| 1973 |
ret = WALK_TREE_ERROR_STOP; |
| 1974 |
if (!parent) |
| 1975 |
break; |
| 1976 |
if (ret == WALK_TREE_RM_CHILDENTRY) |
| 1977 |
ret = rm_from_parent(conn, parent, name); |
| 1978 |
if (ret < 0) |
| 1979 |
break; |
| 1980 |
talloc_free(name); |
| 1981 |
node = parent; |
| 1982 |
continue; |
| 1983 |
} |
| 1984 |
talloc_free(name); |
| 1985 |
node->parent = parent; |
| 1986 |
node->childoff = 0; |
| 1987 |
/* Call function BEFORE processing a node. */ |
| 1988 |
ret = walk_call_func(ctx, conn, node, parent, arg, |
| 1989 |
funcs->enter); |
| 1990 |
} |
| 1991 |
|
| 1992 |
talloc_free(tmpctx); |
| 1993 |
|
| 1994 |
return ret < 0 ? ret : WALK_TREE_OK; |
| 1995 |
} |
| 1996 |
|
| 1997 |
static struct { |
| 1998 |
const char *str; |
| 1999 |
int (*func)(const void *ctx, struct connection *conn, |
| 2000 |
struct buffered_data *in); |
| 2001 |
unsigned int flags; |
| 2002 |
#define XS_FLAG_NOTID (1U << 0) /* Ignore transaction id. */ |
| 2003 |
#define XS_FLAG_PRIV (1U << 1) /* Privileged domain only. */ |
| 2004 |
} const wire_funcs[XS_TYPE_COUNT] = { |
| 2005 |
[XS_CONTROL] = |
| 2006 |
{ "CONTROL", do_control, XS_FLAG_PRIV }, |
| 2007 |
[XS_DIRECTORY] = { "DIRECTORY", send_directory }, |
| 2008 |
[XS_READ] = { "READ", do_read }, |
| 2009 |
[XS_GET_PERMS] = { "GET_PERMS", do_get_perms }, |
| 2010 |
[XS_WATCH] = |
| 2011 |
{ "WATCH", do_watch, XS_FLAG_NOTID }, |
| 2012 |
[XS_UNWATCH] = |
| 2013 |
{ "UNWATCH", do_unwatch, XS_FLAG_NOTID }, |
| 2014 |
[XS_TRANSACTION_START] = { "TRANSACTION_START", do_transaction_start }, |
| 2015 |
[XS_TRANSACTION_END] = { "TRANSACTION_END", do_transaction_end }, |
| 2016 |
[XS_INTRODUCE] = |
| 2017 |
{ "INTRODUCE", do_introduce, XS_FLAG_PRIV }, |
| 2018 |
[XS_RELEASE] = |
| 2019 |
{ "RELEASE", do_release, XS_FLAG_PRIV }, |
| 2020 |
[XS_GET_DOMAIN_PATH] = { "GET_DOMAIN_PATH", do_get_domain_path }, |
| 2021 |
[XS_WRITE] = { "WRITE", do_write }, |
| 2022 |
[XS_MKDIR] = { "MKDIR", do_mkdir }, |
| 2023 |
[XS_RM] = { "RM", do_rm }, |
| 2024 |
[XS_SET_PERMS] = { "SET_PERMS", do_set_perms }, |
| 2025 |
[XS_WATCH_EVENT] = { "WATCH_EVENT", NULL }, |
| 2026 |
[XS_ERROR] = { "ERROR", NULL }, |
| 2027 |
[XS_IS_DOMAIN_INTRODUCED] = |
| 2028 |
{ "IS_DOMAIN_INTRODUCED", do_is_domain_introduced, XS_FLAG_PRIV }, |
| 2029 |
[XS_RESUME] = |
| 2030 |
{ "RESUME", do_resume, XS_FLAG_PRIV }, |
| 2031 |
[XS_SET_TARGET] = |
| 2032 |
{ "SET_TARGET", do_set_target, XS_FLAG_PRIV }, |
| 2033 |
[XS_RESET_WATCHES] = { "RESET_WATCHES", do_reset_watches }, |
| 2034 |
[XS_DIRECTORY_PART] = { "DIRECTORY_PART", send_directory_part }, |
| 2035 |
}; |
| 2036 |
|
| 2037 |
static const char *sockmsg_string(enum xsd_sockmsg_type type) |
| 2038 |
{ |
| 2039 |
if ((unsigned int)type < ARRAY_SIZE(wire_funcs) && wire_funcs[type].str) |
| 2040 |
return wire_funcs[type].str; |
| 2041 |
|
| 2042 |
return "**UNKNOWN**"; |
| 2043 |
} |
| 2044 |
|
| 2045 |
/* Process "in" for conn: "in" will vanish after this conversation, so |
| 2046 |
* we can talloc off it for temporary variables. May free "conn". |
| 2047 |
*/ |
| 2048 |
static void process_message(struct connection *conn, struct buffered_data *in) |
| 2049 |
{ |
| 2050 |
struct transaction *trans; |
| 2051 |
enum xsd_sockmsg_type type = in->hdr.msg.type; |
| 2052 |
int ret; |
| 2053 |
void *ctx; |
| 2054 |
|
| 2055 |
/* At least send_error() and send_reply() expects conn->in == in */ |
| 2056 |
assert(conn->in == in); |
| 2057 |
trace_io(conn, in, "IN"); |
| 2058 |
|
| 2059 |
if ((unsigned int)type >= XS_TYPE_COUNT || !wire_funcs[type].func) { |
| 2060 |
eprintf("Client unknown operation %i", type); |
| 2061 |
send_error(conn, ENOSYS); |
| 2062 |
return; |
| 2063 |
} |
| 2064 |
|
| 2065 |
if ((wire_funcs[type].flags & XS_FLAG_PRIV) && |
| 2066 |
domain_is_unprivileged(conn)) { |
| 2067 |
send_error(conn, EACCES); |
| 2068 |
return; |
| 2069 |
} |
| 2070 |
|
| 2071 |
trans = (wire_funcs[type].flags & XS_FLAG_NOTID) |
| 2072 |
? NULL : transaction_lookup(conn, in->hdr.msg.tx_id); |
| 2073 |
if (IS_ERR(trans)) { |
| 2074 |
send_error(conn, -PTR_ERR(trans)); |
| 2075 |
return; |
| 2076 |
} |
| 2077 |
|
| 2078 |
ctx = talloc_new(NULL); |
| 2079 |
if (!ctx) { |
| 2080 |
send_error(conn, ENOMEM); |
| 2081 |
return; |
| 2082 |
} |
| 2083 |
|
| 2084 |
assert(conn->transaction == NULL); |
| 2085 |
conn->transaction = trans; |
| 2086 |
|
| 2087 |
ret = wire_funcs[type].func(ctx, conn, in); |
| 2088 |
talloc_free(ctx); |
| 2089 |
if (ret) |
| 2090 |
send_error(conn, ret); |
| 2091 |
|
| 2092 |
conn->transaction = NULL; |
| 2093 |
} |
| 2094 |
|
| 2095 |
static bool process_delayed_message(struct delayed_request *req) |
| 2096 |
{ |
| 2097 |
struct connection *conn = req->data; |
| 2098 |
struct buffered_data *saved_in = conn->in; |
| 2099 |
|
| 2100 |
if (lu_is_pending()) |
| 2101 |
return false; |
| 2102 |
|
| 2103 |
/* |
| 2104 |
* Part of process_message() expects conn->in to contains the |
| 2105 |
* processed response. So save the current conn->in and restore it |
| 2106 |
* afterwards. |
| 2107 |
*/ |
| 2108 |
conn->in = req->in; |
| 2109 |
process_message(req->data, req->in); |
| 2110 |
conn->in = saved_in; |
| 2111 |
|
| 2112 |
return true; |
| 2113 |
} |
| 2114 |
|
| 2115 |
static void consider_message(struct connection *conn) |
| 2116 |
{ |
| 2117 |
conn->is_stalled = false; |
| 2118 |
/* |
| 2119 |
* Currently, Live-Update is not supported if there is active |
| 2120 |
* transactions. In order to reduce the number of retry, delay |
| 2121 |
* any new request to start a transaction if Live-Update is pending |
| 2122 |
* and there are no transactions in-flight. |
| 2123 |
* |
| 2124 |
* If we can't delay the request, then mark the connection as |
| 2125 |
* stalled. This will ignore new requests until Live-Update happened |
| 2126 |
* or it was aborted. |
| 2127 |
*/ |
| 2128 |
if (lu_is_pending() && list_empty(&conn->transaction_list) && |
| 2129 |
conn->in->hdr.msg.type == XS_TRANSACTION_START) { |
| 2130 |
trace("Delaying transaction start for connection %p req_id %u\n", |
| 2131 |
conn, conn->in->hdr.msg.req_id); |
| 2132 |
|
| 2133 |
if (delay_request(conn, conn->in, process_delayed_message, |
| 2134 |
conn, false) != 0) { |
| 2135 |
trace("Stalling connection %p\n", conn); |
| 2136 |
conn->is_stalled = true; |
| 2137 |
} |
| 2138 |
return; |
| 2139 |
} |
| 2140 |
|
| 2141 |
process_message(conn, conn->in); |
| 2142 |
|
| 2143 |
assert(conn->in == NULL); |
| 2144 |
} |
| 2145 |
|
| 2146 |
/* |
| 2147 |
* Errors in reading or allocating here means we get out of sync, so we mark |
| 2148 |
* the connection as ignored. |
| 2149 |
*/ |
| 2150 |
static void handle_input(struct connection *conn) |
| 2151 |
{ |
| 2152 |
int bytes; |
| 2153 |
struct buffered_data *in; |
| 2154 |
unsigned int err; |
| 2155 |
|
| 2156 |
if (!conn->in) { |
| 2157 |
conn->in = new_buffer(conn); |
| 2158 |
/* In case of no memory just try it again next time. */ |
| 2159 |
if (!conn->in) |
| 2160 |
return; |
| 2161 |
} |
| 2162 |
in = conn->in; |
| 2163 |
in->pend.ref.domid = conn->id; |
| 2164 |
|
| 2165 |
/* Not finished header yet? */ |
| 2166 |
if (in->inhdr) { |
| 2167 |
if (in->used != sizeof(in->hdr)) { |
| 2168 |
bytes = conn->funcs->read(conn, in->hdr.raw + in->used, |
| 2169 |
sizeof(in->hdr) - in->used); |
| 2170 |
if (bytes < 0) { |
| 2171 |
err = XENSTORE_ERROR_RINGIDX; |
| 2172 |
goto bad_client; |
| 2173 |
} |
| 2174 |
in->used += bytes; |
| 2175 |
if (in->used != sizeof(in->hdr)) |
| 2176 |
return; |
| 2177 |
|
| 2178 |
/* |
| 2179 |
* The payload size is not only currently restricted by |
| 2180 |
* the protocol but also the internal implementation |
| 2181 |
* (see various BUILD_BUG_ON()). |
| 2182 |
* Any potential change of the maximum payload size |
| 2183 |
* needs to be negotiated between the involved parties. |
| 2184 |
*/ |
| 2185 |
if (in->hdr.msg.len > XENSTORE_PAYLOAD_MAX) { |
| 2186 |
syslog(LOG_ERR, "Client tried to feed us %i", |
| 2187 |
in->hdr.msg.len); |
| 2188 |
err = XENSTORE_ERROR_PROTO; |
| 2189 |
goto bad_client; |
| 2190 |
} |
| 2191 |
} |
| 2192 |
|
| 2193 |
if (in->hdr.msg.len <= DEFAULT_BUFFER_SIZE) |
| 2194 |
in->buffer = in->default_buffer; |
| 2195 |
else |
| 2196 |
in->buffer = talloc_array(in, char, in->hdr.msg.len); |
| 2197 |
/* In case of no memory just try it again next time. */ |
| 2198 |
if (!in->buffer) |
| 2199 |
return; |
| 2200 |
in->used = 0; |
| 2201 |
in->inhdr = false; |
| 2202 |
} |
| 2203 |
|
| 2204 |
bytes = conn->funcs->read(conn, in->buffer + in->used, |
| 2205 |
in->hdr.msg.len - in->used); |
| 2206 |
if (bytes < 0) { |
| 2207 |
err = XENSTORE_ERROR_RINGIDX; |
| 2208 |
goto bad_client; |
| 2209 |
} |
| 2210 |
|
| 2211 |
in->used += bytes; |
| 2212 |
if (in->used != in->hdr.msg.len) |
| 2213 |
return; |
| 2214 |
|
| 2215 |
consider_message(conn); |
| 2216 |
return; |
| 2217 |
|
| 2218 |
bad_client: |
| 2219 |
ignore_connection(conn, err); |
| 2220 |
} |
| 2221 |
|
| 2222 |
static void handle_output(struct connection *conn) |
| 2223 |
{ |
| 2224 |
/* Ignore the connection if an error occured */ |
| 2225 |
if (!write_messages(conn)) |
| 2226 |
ignore_connection(conn, XENSTORE_ERROR_RINGIDX); |
| 2227 |
} |
| 2228 |
|
| 2229 |
struct connection *new_connection(const struct interface_funcs *funcs) |
| 2230 |
{ |
| 2231 |
struct connection *new; |
| 2232 |
|
| 2233 |
new = talloc_zero(talloc_autofree_context(), struct connection); |
| 2234 |
if (!new) |
| 2235 |
return NULL; |
| 2236 |
|
| 2237 |
new->fd = -1; |
| 2238 |
new->pollfd_idx = -1; |
| 2239 |
new->funcs = funcs; |
| 2240 |
new->is_ignored = false; |
| 2241 |
new->is_stalled = false; |
| 2242 |
INIT_LIST_HEAD(&new->out_list); |
| 2243 |
INIT_LIST_HEAD(&new->acc_list); |
| 2244 |
INIT_LIST_HEAD(&new->ref_list); |
| 2245 |
INIT_LIST_HEAD(&new->watches); |
| 2246 |
INIT_LIST_HEAD(&new->transaction_list); |
| 2247 |
INIT_LIST_HEAD(&new->delayed); |
| 2248 |
|
| 2249 |
list_add_tail(&new->list, &connections); |
| 2250 |
talloc_set_destructor(new, destroy_conn); |
| 2251 |
trace_create(new, "connection"); |
| 2252 |
return new; |
| 2253 |
} |
| 2254 |
|
| 2255 |
struct connection *get_connection_by_id(unsigned int conn_id) |
| 2256 |
{ |
| 2257 |
struct connection *conn; |
| 2258 |
|
| 2259 |
list_for_each_entry(conn, &connections, list) |
| 2260 |
if (conn->conn_id == conn_id) |
| 2261 |
return conn; |
| 2262 |
|
| 2263 |
return NULL; |
| 2264 |
} |
| 2265 |
|
| 2266 |
/* We create initial nodes manually. */ |
| 2267 |
static void manual_node(const char *name, const char *child) |
| 2268 |
{ |
| 2269 |
struct node *node; |
| 2270 |
struct xs_permissions perms = { .id = dom0_domid, |
| 2271 |
.perms = XS_PERM_NONE }; |
| 2272 |
|
| 2273 |
node = talloc_zero(NULL, struct node); |
| 2274 |
if (!node) |
| 2275 |
barf_perror("Could not allocate initial node %s", name); |
| 2276 |
|
| 2277 |
node->name = name; |
| 2278 |
node->perms = &perms; |
| 2279 |
node->hdr.num_perms = 1; |
| 2280 |
node->children = (char *)child; |
| 2281 |
if (child) |
| 2282 |
node->hdr.childlen = strlen(child) + 1; |
| 2283 |
|
| 2284 |
if (write_node(NULL, node, NODE_CREATE, false)) |
| 2285 |
barf_perror("Could not create initial node %s", name); |
| 2286 |
talloc_free(node); |
| 2287 |
} |
| 2288 |
|
| 2289 |
static unsigned int hash_from_key_fn(const void *k) |
| 2290 |
{ |
| 2291 |
const char *str = k; |
| 2292 |
unsigned int hash = 5381; |
| 2293 |
char c; |
| 2294 |
|
| 2295 |
while ((c = *str++)) |
| 2296 |
hash = ((hash << 5) + hash) + (unsigned int)c; |
| 2297 |
|
| 2298 |
return hash; |
| 2299 |
} |
| 2300 |
|
| 2301 |
static int keys_equal_fn(const void *key1, const void *key2) |
| 2302 |
{ |
| 2303 |
return 0 == strcmp(key1, key2); |
| 2304 |
} |
| 2305 |
|
| 2306 |
void setup_structure(bool live_update) |
| 2307 |
{ |
| 2308 |
nodes = create_hashtable(NULL, "nodes", hash_from_key_fn, keys_equal_fn, |
| 2309 |
HASHTABLE_FREE_KEY | HASHTABLE_FREE_VALUE); |
| 2310 |
if (!nodes) |
| 2311 |
barf_perror("Could not create nodes hashtable"); |
| 2312 |
|
| 2313 |
if (live_update) |
| 2314 |
manual_node("/", NULL); |
| 2315 |
else { |
| 2316 |
manual_node("/", "tool"); |
| 2317 |
manual_node("/tool", "xenstored"); |
| 2318 |
manual_node("/tool/xenstored", NULL); |
| 2319 |
manual_node("@releaseDomain", NULL); |
| 2320 |
manual_node("@introduceDomain", NULL); |
| 2321 |
domain_nbentry_fix(dom0_domid, 5, true); |
| 2322 |
} |
| 2323 |
} |
| 2324 |
|
| 2325 |
int remember_string(struct hashtable *hash, const char *str) |
| 2326 |
{ |
| 2327 |
char *k = talloc_strdup(NULL, str); |
| 2328 |
|
| 2329 |
if (!k) |
| 2330 |
return ENOMEM; |
| 2331 |
return hashtable_add(hash, k, (void *)1); |
| 2332 |
} |
| 2333 |
|
| 2334 |
/** |
| 2335 |
* A node has a children field that names the children of the node, separated |
| 2336 |
* by NULs. We check whether there are entries in there that are duplicated |
| 2337 |
* (and if so, delete the second one), and whether there are any that do not |
| 2338 |
* have a corresponding child node (and if so, delete them). Each valid child |
| 2339 |
* is then recursively checked. |
| 2340 |
* |
| 2341 |
* As we go, we record each node in the given reachable hashtable. These |
| 2342 |
* entries will be used later in clean_store. |
| 2343 |
*/ |
| 2344 |
|
| 2345 |
struct check_store_data { |
| 2346 |
struct hashtable *reachable; |
| 2347 |
struct hashtable *domains; |
| 2348 |
}; |
| 2349 |
|
| 2350 |
static int check_store_step(const void *ctx, struct connection *conn, |
| 2351 |
struct node *node, void *arg) |
| 2352 |
{ |
| 2353 |
struct check_store_data *data = arg; |
| 2354 |
|
| 2355 |
if (hashtable_search(data->reachable, (void *)node->name)) { |
| 2356 |
log("check_store: '%s' is duplicated!", node->name); |
| 2357 |
return WALK_TREE_RM_CHILDENTRY; |
| 2358 |
} |
| 2359 |
|
| 2360 |
if (remember_string(data->reachable, node->name)) |
| 2361 |
return WALK_TREE_ERROR_STOP; |
| 2362 |
|
| 2363 |
domain_check_acc_add(node, data->domains); |
| 2364 |
|
| 2365 |
return WALK_TREE_OK; |
| 2366 |
} |
| 2367 |
|
| 2368 |
static int check_store_enoent(const void *ctx, struct connection *conn, |
| 2369 |
struct node *parent, char *name, void *arg) |
| 2370 |
{ |
| 2371 |
log("check_store: node '%s' not found", name); |
| 2372 |
|
| 2373 |
return WALK_TREE_RM_CHILDENTRY; |
| 2374 |
} |
| 2375 |
|
| 2376 |
|
| 2377 |
/** |
| 2378 |
* Helper to clean_store below. |
| 2379 |
*/ |
| 2380 |
static int clean_store_(const void *key, void *val, void *private) |
| 2381 |
{ |
| 2382 |
struct hashtable *reachable = private; |
| 2383 |
char *slash; |
| 2384 |
char *name = talloc_strdup(NULL, key); |
| 2385 |
|
| 2386 |
if (!name) { |
| 2387 |
log("clean_store: ENOMEM"); |
| 2388 |
return 1; |
| 2389 |
} |
| 2390 |
|
| 2391 |
if (name[0] != '/') { |
| 2392 |
slash = strchr(name, '/'); |
| 2393 |
if (slash) |
| 2394 |
*slash = 0; |
| 2395 |
} |
| 2396 |
if (!hashtable_search(reachable, name)) { |
| 2397 |
log("clean_store: '%s' is orphaned!", name); |
| 2398 |
db_delete(NULL, name, NULL); |
| 2399 |
} |
| 2400 |
|
| 2401 |
talloc_free(name); |
| 2402 |
|
| 2403 |
return 0; |
| 2404 |
} |
| 2405 |
|
| 2406 |
|
| 2407 |
/** |
| 2408 |
* Given the list of reachable nodes, iterate over the whole store, and |
| 2409 |
* remove any that were not reached. |
| 2410 |
*/ |
| 2411 |
static void clean_store(struct check_store_data *data) |
| 2412 |
{ |
| 2413 |
hashtable_iterate(nodes, clean_store_, data->reachable); |
| 2414 |
domain_check_acc(data->domains); |
| 2415 |
} |
| 2416 |
|
| 2417 |
int check_store_path(const void *ctx, const char *name, struct check_store_data *data) |
| 2418 |
{ |
| 2419 |
struct node *node; |
| 2420 |
|
| 2421 |
node = read_node(NULL, ctx, name); |
| 2422 |
if (!node) { |
| 2423 |
log("check_store: error %d reading special node '%s'", errno, |
| 2424 |
name); |
| 2425 |
return errno; |
| 2426 |
} |
| 2427 |
|
| 2428 |
return check_store_step(ctx, NULL, node, data); |
| 2429 |
} |
| 2430 |
|
| 2431 |
void check_store(void) |
| 2432 |
{ |
| 2433 |
struct walk_funcs walkfuncs = { |
| 2434 |
.enter = check_store_step, |
| 2435 |
.enoent = check_store_enoent, |
| 2436 |
}; |
| 2437 |
struct check_store_data data; |
| 2438 |
void *ctx; |
| 2439 |
|
| 2440 |
/* Don't free values (they are all void *1) */ |
| 2441 |
data.reachable = create_hashtable(NULL, "checkstore", hash_from_key_fn, |
| 2442 |
keys_equal_fn, HASHTABLE_FREE_KEY); |
| 2443 |
if (!data.reachable) { |
| 2444 |
log("check_store: ENOMEM"); |
| 2445 |
return; |
| 2446 |
} |
| 2447 |
|
| 2448 |
data.domains = domain_check_acc_init(); |
| 2449 |
if (!data.domains) { |
| 2450 |
log("check_store: ENOMEM"); |
| 2451 |
goto out_hash; |
| 2452 |
} |
| 2453 |
|
| 2454 |
ctx = talloc_new(NULL); |
| 2455 |
log("Checking store ..."); |
| 2456 |
if (walk_node_tree(ctx, NULL, "/", &walkfuncs, &data)) { |
| 2457 |
if (errno == ENOMEM) |
| 2458 |
log("check_store: ENOMEM"); |
| 2459 |
} else if (!check_store_path(ctx, "@introduceDomain", &data) && |
| 2460 |
!check_store_path(ctx, "@releaseDomain", &data) && |
| 2461 |
!check_transactions(data.reachable)) |
| 2462 |
clean_store(&data); |
| 2463 |
log("Checking store complete."); |
| 2464 |
|
| 2465 |
hashtable_destroy(data.domains); |
| 2466 |
talloc_free(ctx); |
| 2467 |
out_hash: |
| 2468 |
hashtable_destroy(data.reachable); |
| 2469 |
} |
| 2470 |
|
| 2471 |
|
| 2472 |
/* Something is horribly wrong: check the store. */ |
| 2473 |
void corrupt(struct connection *conn, const char *fmt, ...) |
| 2474 |
{ |
| 2475 |
va_list arglist; |
| 2476 |
char *str; |
| 2477 |
int saved_errno = errno; |
| 2478 |
|
| 2479 |
va_start(arglist, fmt); |
| 2480 |
str = talloc_vasprintf(NULL, fmt, arglist); |
| 2481 |
va_end(arglist); |
| 2482 |
|
| 2483 |
log("corruption detected by connection %i: err %s: %s", |
| 2484 |
conn ? (int)conn->id : -1, strerror(saved_errno), |
| 2485 |
str ?: "ENOMEM"); |
| 2486 |
|
| 2487 |
talloc_free(str); |
| 2488 |
|
| 2489 |
check_store(); |
| 2490 |
|
| 2491 |
errno = saved_errno; |
| 2492 |
} |
| 2493 |
|
| 2494 |
static void usage(void) |
| 2495 |
{ |
| 2496 |
fprintf(stderr, |
| 2497 |
"Usage:\n" |
| 2498 |
"\n" |
| 2499 |
" xenstored <options>\n" |
| 2500 |
"\n" |
| 2501 |
"where options may include:\n" |
| 2502 |
"\n" |
| 2503 |
" -F, --pid-file <file> giving a file for the daemon's pid to be written,\n" |
| 2504 |
" -H, --help to output this message,\n" |
| 2505 |
" -N, --no-fork to request that the daemon does not fork,\n" |
| 2506 |
" -T, --trace-file <file> giving the file for logging, and\n" |
| 2507 |
" --trace-control=+<switch> activate a specific <switch>\n" |
| 2508 |
" --trace-control=-<switch> deactivate a specific <switch>\n" |
| 2509 |
" -E, --entry-nb <nb> limit the number of entries per domain,\n" |
| 2510 |
" -S, --entry-size <size> limit the size of entry per domain, and\n" |
| 2511 |
" -W, --watch-nb <nb> limit the number of watches per domain,\n" |
| 2512 |
" -t, --transaction <nb> limit the number of transaction allowed per domain,\n" |
| 2513 |
" -A, --perm-nb <nb> limit the number of permissions per node,\n" |
| 2514 |
" -M, --path-max <chars> limit the allowed Xenstore node path length,\n" |
| 2515 |
" -Q, --quota <what>=<nb> set the quota <what> to the value <nb>, allowed\n" |
| 2516 |
" quotas are:\n" |
| 2517 |
" transaction-nodes: number of accessed node per\n" |
| 2518 |
" transaction\n" |
| 2519 |
" memory: total used memory per domain for nodes,\n" |
| 2520 |
" transactions, watches and requests, above\n" |
| 2521 |
" which Xenstore will stop talking to domain\n" |
| 2522 |
" nodes: number nodes owned by a domain\n" |
| 2523 |
" node-permissions: number of access permissions per\n" |
| 2524 |
" node\n" |
| 2525 |
" node-size: total size of a node (permissions +\n" |
| 2526 |
" children names + content)\n" |
| 2527 |
" outstanding: number of outstanding requests\n" |
| 2528 |
" path-length: length of a node path\n" |
| 2529 |
" transactions: number of concurrent transactions\n" |
| 2530 |
" per domain\n" |
| 2531 |
" watches: number of watches per domain" |
| 2532 |
" -q, --quota-soft <what>=<nb> set a soft quota <what> to the value <nb>,\n" |
| 2533 |
" causing a warning to be issued via syslog() if the\n" |
| 2534 |
" limit is violated, allowed quotas are:\n" |
| 2535 |
" memory: see above\n" |
| 2536 |
" -w, --timeout <what>=<seconds> set the timeout in seconds for <what>,\n" |
| 2537 |
" allowed timeout candidates are:\n" |
| 2538 |
" watch-event: time a watch-event is kept pending\n" |
| 2539 |
" -K, --keep-orphans don't delete nodes owned by a domain when the\n" |
| 2540 |
" domain is deleted (this is a security risk!)\n"); |
| 2541 |
} |
| 2542 |
|
| 2543 |
|
| 2544 |
static struct option options[] = { |
| 2545 |
{ "entry-nb", 1, NULL, 'E' }, |
| 2546 |
{ "pid-file", 1, NULL, 'F' }, |
| 2547 |
{ "event", 1, NULL, 'e' }, |
| 2548 |
{ "master-domid", 1, NULL, 'm' }, |
| 2549 |
{ "help", 0, NULL, 'H' }, |
| 2550 |
{ "no-fork", 0, NULL, 'N' }, |
| 2551 |
{ "priv-domid", 1, NULL, 'p' }, |
| 2552 |
{ "entry-size", 1, NULL, 'S' }, |
| 2553 |
{ "trace-file", 1, NULL, 'T' }, |
| 2554 |
{ "trace-control", 1, NULL, 1 }, |
| 2555 |
{ "transaction", 1, NULL, 't' }, |
| 2556 |
{ "perm-nb", 1, NULL, 'A' }, |
| 2557 |
{ "path-max", 1, NULL, 'M' }, |
| 2558 |
{ "quota", 1, NULL, 'Q' }, |
| 2559 |
{ "quota-soft", 1, NULL, 'q' }, |
| 2560 |
{ "timeout", 1, NULL, 'w' }, |
| 2561 |
{ "keep-orphans", 0, NULL, 'K' }, |
| 2562 |
{ "watch-nb", 1, NULL, 'W' }, |
| 2563 |
#ifndef NO_LIVE_UPDATE |
| 2564 |
{ "live-update", 0, NULL, 'U' }, |
| 2565 |
#endif |
| 2566 |
{ NULL, 0, NULL, 0 } }; |
| 2567 |
|
| 2568 |
int dom0_domid = 0; |
| 2569 |
int dom0_event = 0; |
| 2570 |
int priv_domid = 0; |
| 2571 |
domid_t stub_domid = DOMID_INVALID; |
| 2572 |
|
| 2573 |
static unsigned int get_optval_uint(const char *arg) |
| 2574 |
{ |
| 2575 |
char *end; |
| 2576 |
unsigned long val; |
| 2577 |
|
| 2578 |
val = strtoul(arg, &end, 10); |
| 2579 |
if (!*arg || *end || val > INT_MAX) |
| 2580 |
barf("invalid parameter value \"%s\"\n", arg); |
| 2581 |
|
| 2582 |
return val; |
| 2583 |
} |
| 2584 |
|
| 2585 |
static bool what_matches(const char *arg, const char *what) |
| 2586 |
{ |
| 2587 |
unsigned int what_len; |
| 2588 |
|
| 2589 |
if (!what) |
| 2590 |
return false; |
| 2591 |
|
| 2592 |
what_len = strlen(what); |
| 2593 |
|
| 2594 |
return !strncmp(arg, what, what_len) && arg[what_len] == '='; |
| 2595 |
} |
| 2596 |
|
| 2597 |
static void set_timeout(const char *arg) |
| 2598 |
{ |
| 2599 |
const char *eq = strchr(arg, '='); |
| 2600 |
unsigned int val; |
| 2601 |
|
| 2602 |
if (!eq) |
| 2603 |
barf("quotas must be specified via <what>=<seconds>\n"); |
| 2604 |
val = get_optval_uint(eq + 1); |
| 2605 |
if (what_matches(arg, "watch-event")) |
| 2606 |
timeout_watch_event_msec = val * 1000; |
| 2607 |
else |
| 2608 |
barf("unknown timeout \"%s\"\n", arg); |
| 2609 |
} |
| 2610 |
|
| 2611 |
static void set_quota(const char *arg, bool soft) |
| 2612 |
{ |
| 2613 |
const char *eq = strchr(arg, '='); |
| 2614 |
struct quota *q = soft ? soft_quotas : hard_quotas; |
| 2615 |
unsigned int val; |
| 2616 |
unsigned int i; |
| 2617 |
|
| 2618 |
if (!eq) |
| 2619 |
barf("quotas must be specified via <what>=<nb>\n"); |
| 2620 |
val = get_optval_uint(eq + 1); |
| 2621 |
|
| 2622 |
for (i = 0; i < ACC_N; i++) { |
| 2623 |
if (what_matches(arg, q[i].name)) { |
| 2624 |
q[i].val = val; |
| 2625 |
return; |
| 2626 |
} |
| 2627 |
} |
| 2628 |
|
| 2629 |
barf("unknown quota \"%s\"\n", arg); |
| 2630 |
} |
| 2631 |
|
| 2632 |
/* Sorted by bit values of TRACE_* flags. Flag is (1u << index). */ |
| 2633 |
const char *const trace_switches[] = { |
| 2634 |
"obj", "io", "wrl", "acc", "tdb", |
| 2635 |
NULL |
| 2636 |
}; |
| 2637 |
|
| 2638 |
int set_trace_switch(const char *arg) |
| 2639 |
{ |
| 2640 |
bool remove = (arg[0] == '-'); |
| 2641 |
unsigned int idx; |
| 2642 |
|
| 2643 |
switch (arg[0]) { |
| 2644 |
case '-': |
| 2645 |
remove = true; |
| 2646 |
break; |
| 2647 |
case '+': |
| 2648 |
remove = false; |
| 2649 |
break; |
| 2650 |
default: |
| 2651 |
return EINVAL; |
| 2652 |
} |
| 2653 |
|
| 2654 |
arg++; |
| 2655 |
|
| 2656 |
for (idx = 0; trace_switches[idx]; idx++) { |
| 2657 |
if (!strcmp(arg, trace_switches[idx])) { |
| 2658 |
if (remove) |
| 2659 |
trace_flags &= ~(1u << idx); |
| 2660 |
else |
| 2661 |
trace_flags |= 1u << idx; |
| 2662 |
return 0; |
| 2663 |
} |
| 2664 |
} |
| 2665 |
|
| 2666 |
return EINVAL; |
| 2667 |
} |
| 2668 |
|
| 2669 |
int main(int argc, char *argv[]) |
| 2670 |
{ |
| 2671 |
int opt; |
| 2672 |
bool dofork = true; |
| 2673 |
bool live_update = false; |
| 2674 |
const char *pidfile = NULL; |
| 2675 |
int timeout; |
| 2676 |
|
| 2677 |
orig_argc = argc; |
| 2678 |
orig_argv = argv; |
| 2679 |
|
| 2680 |
while ((opt = getopt_long(argc, argv, |
| 2681 |
"E:F:H::KNS:t:A:M:Q:q:T:W:w:U", |
| 2682 |
options, NULL)) != -1) { |
| 2683 |
switch (opt) { |
| 2684 |
case 'E': |
| 2685 |
hard_quotas[ACC_NODES].val = get_optval_uint(optarg); |
| 2686 |
break; |
| 2687 |
case 'F': |
| 2688 |
pidfile = optarg; |
| 2689 |
break; |
| 2690 |
case 'H': |
| 2691 |
usage(); |
| 2692 |
return 0; |
| 2693 |
case 'N': |
| 2694 |
dofork = false; |
| 2695 |
break; |
| 2696 |
case 'S': |
| 2697 |
hard_quotas[ACC_NODESZ].val = get_optval_uint(optarg); |
| 2698 |
break; |
| 2699 |
case 't': |
| 2700 |
hard_quotas[ACC_TRANS].val = get_optval_uint(optarg); |
| 2701 |
break; |
| 2702 |
case 'T': |
| 2703 |
tracefile = optarg; |
| 2704 |
break; |
| 2705 |
case 1: |
| 2706 |
if (set_trace_switch(optarg)) |
| 2707 |
barf("Illegal trace switch \"%s\"\n", optarg); |
| 2708 |
break; |
| 2709 |
case 'K': |
| 2710 |
keep_orphans = true; |
| 2711 |
break; |
| 2712 |
case 'W': |
| 2713 |
hard_quotas[ACC_WATCH].val = get_optval_uint(optarg); |
| 2714 |
break; |
| 2715 |
case 'A': |
| 2716 |
hard_quotas[ACC_NPERM].val = get_optval_uint(optarg); |
| 2717 |
break; |
| 2718 |
case 'M': |
| 2719 |
hard_quotas[ACC_PATHLEN].val = get_optval_uint(optarg); |
| 2720 |
hard_quotas[ACC_PATHLEN].val = |
| 2721 |
min((unsigned int)XENSTORE_REL_PATH_MAX, |
| 2722 |
hard_quotas[ACC_PATHLEN].val); |
| 2723 |
break; |
| 2724 |
case 'Q': |
| 2725 |
set_quota(optarg, false); |
| 2726 |
break; |
| 2727 |
case 'q': |
| 2728 |
set_quota(optarg, true); |
| 2729 |
break; |
| 2730 |
case 'w': |
| 2731 |
set_timeout(optarg); |
| 2732 |
break; |
| 2733 |
case 'e': |
| 2734 |
dom0_event = get_optval_uint(optarg); |
| 2735 |
break; |
| 2736 |
case 'm': |
| 2737 |
dom0_domid = get_optval_uint(optarg); |
| 2738 |
break; |
| 2739 |
case 'p': |
| 2740 |
priv_domid = get_optval_uint(optarg); |
| 2741 |
break; |
| 2742 |
#ifndef NO_LIVE_UPDATE |
| 2743 |
case 'U': |
| 2744 |
live_update = true; |
| 2745 |
break; |
| 2746 |
#endif |
| 2747 |
} |
| 2748 |
} |
| 2749 |
if (optind != argc) |
| 2750 |
barf("%s: No arguments desired", argv[0]); |
| 2751 |
|
| 2752 |
early_init(live_update, dofork, pidfile); |
| 2753 |
|
| 2754 |
talloc_enable_null_tracking(); |
| 2755 |
|
| 2756 |
domain_early_init(); |
| 2757 |
|
| 2758 |
/* Listen to hypervisor. */ |
| 2759 |
if (!live_update) { |
| 2760 |
domain_init(-1); |
| 2761 |
dom0_init(); |
| 2762 |
} |
| 2763 |
|
| 2764 |
/* redirect to /dev/null now we're ready to accept connections */ |
| 2765 |
if (dofork && !live_update) |
| 2766 |
finish_daemonize(); |
| 2767 |
#ifndef __MINIOS__ |
| 2768 |
if (dofork) |
| 2769 |
xprintf = trace; |
| 2770 |
#endif |
| 2771 |
|
| 2772 |
if (tracefile) |
| 2773 |
tracefile = absolute_filename(NULL, tracefile); |
| 2774 |
|
| 2775 |
#ifndef NO_LIVE_UPDATE |
| 2776 |
/* Read state in case of live update. */ |
| 2777 |
if (live_update) |
| 2778 |
lu_read_state(); |
| 2779 |
#endif |
| 2780 |
|
| 2781 |
stubdom_init(); |
| 2782 |
|
| 2783 |
check_store(); |
| 2784 |
|
| 2785 |
/* Get ready to listen to the tools. */ |
| 2786 |
initialize_fds(&timeout); |
| 2787 |
|
| 2788 |
late_init(live_update); |
| 2789 |
|
| 2790 |
/* Main loop. */ |
| 2791 |
for (;;) { |
| 2792 |
struct connection *conn, *next; |
| 2793 |
|
| 2794 |
if (poll(poll_fds, nr_fds, timeout) < 0) { |
| 2795 |
if (errno == EINTR) |
| 2796 |
continue; |
| 2797 |
barf_perror("Poll failed"); |
| 2798 |
} |
| 2799 |
|
| 2800 |
handle_special_fds(); |
| 2801 |
|
| 2802 |
if (xce_pollfd_idx != -1) { |
| 2803 |
if (poll_fds[xce_pollfd_idx].revents & ~POLLIN) { |
| 2804 |
barf_perror("xce_handle poll failed"); |
| 2805 |
break; |
| 2806 |
} else if (poll_fds[xce_pollfd_idx].revents & POLLIN) { |
| 2807 |
handle_event(); |
| 2808 |
xce_pollfd_idx = -1; |
| 2809 |
} |
| 2810 |
} |
| 2811 |
|
| 2812 |
/* |
| 2813 |
* list_for_each_entry_safe is not suitable here because |
| 2814 |
* handle_input may delete entries besides the current one, but |
| 2815 |
* those may be in the temporary next which would trigger a |
| 2816 |
* use-after-free. list_for_each_entry_safe is only safe for |
| 2817 |
* deleting the current entry. |
| 2818 |
*/ |
| 2819 |
next = list_entry(connections.next, typeof(*conn), list); |
| 2820 |
if (&next->list != &connections) |
| 2821 |
talloc_increase_ref_count(next); |
| 2822 |
while (&next->list != &connections) { |
| 2823 |
conn = next; |
| 2824 |
|
| 2825 |
next = list_entry(conn->list.next, |
| 2826 |
typeof(*conn), list); |
| 2827 |
if (&next->list != &connections) |
| 2828 |
talloc_increase_ref_count(next); |
| 2829 |
|
| 2830 |
if (conn_can_read(conn)) |
| 2831 |
handle_input(conn); |
| 2832 |
if (talloc_free(conn) == 0) |
| 2833 |
continue; |
| 2834 |
|
| 2835 |
talloc_increase_ref_count(conn); |
| 2836 |
|
| 2837 |
if (conn_can_write(conn)) |
| 2838 |
handle_output(conn); |
| 2839 |
if (talloc_free(conn) == 0) |
| 2840 |
continue; |
| 2841 |
|
| 2842 |
conn->pollfd_idx = -1; |
| 2843 |
} |
| 2844 |
|
| 2845 |
if (delayed_requests) { |
| 2846 |
list_for_each_entry(conn, &connections, list) { |
| 2847 |
struct delayed_request *req, *tmp; |
| 2848 |
|
| 2849 |
list_for_each_entry_safe(req, tmp, |
| 2850 |
&conn->delayed, list) |
| 2851 |
call_delayed(req); |
| 2852 |
} |
| 2853 |
} |
| 2854 |
|
| 2855 |
initialize_fds(&timeout); |
| 2856 |
} |
| 2857 |
} |
| 2858 |
|
| 2859 |
const char *dump_state_global(FILE *fp) |
| 2860 |
{ |
| 2861 |
struct xs_state_record_header head; |
| 2862 |
struct xs_state_global glb; |
| 2863 |
|
| 2864 |
head.type = XS_STATE_TYPE_GLOBAL; |
| 2865 |
head.length = sizeof(glb); |
| 2866 |
if (fwrite(&head, sizeof(head), 1, fp) != 1) |
| 2867 |
return "Dump global state error"; |
| 2868 |
glb.socket_fd = get_socket_fd(); |
| 2869 |
glb.evtchn_fd = xenevtchn_fd(xce_handle); |
| 2870 |
if (fwrite(&glb, sizeof(glb), 1, fp) != 1) |
| 2871 |
return "Dump global state error"; |
| 2872 |
|
| 2873 |
return NULL; |
| 2874 |
} |
| 2875 |
|
| 2876 |
static const char *dump_input_buffered_data(FILE *fp, |
| 2877 |
const struct buffered_data *in, |
| 2878 |
unsigned int *total_len) |
| 2879 |
{ |
| 2880 |
unsigned int hlen = in->inhdr ? in->used : sizeof(in->hdr); |
| 2881 |
|
| 2882 |
*total_len += hlen; |
| 2883 |
if (fp && fwrite(&in->hdr, hlen, 1, fp) != 1) |
| 2884 |
return "Dump read data error"; |
| 2885 |
if (!in->inhdr && in->used) { |
| 2886 |
*total_len += in->used; |
| 2887 |
if (fp && fwrite(in->buffer, in->used, 1, fp) != 1) |
| 2888 |
return "Dump read data error"; |
| 2889 |
} |
| 2890 |
|
| 2891 |
return NULL; |
| 2892 |
} |
| 2893 |
|
| 2894 |
/* Called twice: first with fp == NULL to get length, then for writing data. */ |
| 2895 |
const char *dump_state_buffered_data(FILE *fp, const struct connection *c, |
| 2896 |
struct xs_state_connection *sc) |
| 2897 |
{ |
| 2898 |
unsigned int len = 0, used; |
| 2899 |
struct buffered_data *out; |
| 2900 |
bool partial = true; |
| 2901 |
struct delayed_request *req; |
| 2902 |
const char *ret; |
| 2903 |
|
| 2904 |
/* Dump any command that was delayed */ |
| 2905 |
list_for_each_entry(req, &c->delayed, list) { |
| 2906 |
/* |
| 2907 |
* We only want to preserve commands that weren't processed at |
| 2908 |
* all. All the other delayed requests (such as do_lu_start()) |
| 2909 |
* must be processed before Live-Update. |
| 2910 |
*/ |
| 2911 |
if (req->func != process_delayed_message) |
| 2912 |
continue; |
| 2913 |
|
| 2914 |
assert(!req->in->inhdr); |
| 2915 |
if ((ret = dump_input_buffered_data(fp, req->in, &len))) |
| 2916 |
return ret; |
| 2917 |
} |
| 2918 |
|
| 2919 |
if (c->in && (ret = dump_input_buffered_data(fp, c->in, &len))) |
| 2920 |
return ret; |
| 2921 |
|
| 2922 |
if (sc) { |
| 2923 |
sc->data_in_len = len; |
| 2924 |
sc->data_resp_len = 0; |
| 2925 |
} |
| 2926 |
|
| 2927 |
len = 0; |
| 2928 |
|
| 2929 |
list_for_each_entry(out, &c->out_list, list) { |
| 2930 |
used = out->used; |
| 2931 |
if (out->inhdr) { |
| 2932 |
if (!used) |
| 2933 |
partial = false; |
| 2934 |
if (fp && fwrite(out->hdr.raw + out->used, |
| 2935 |
sizeof(out->hdr) - out->used, 1, fp) != 1) |
| 2936 |
return "Dump buffered data error"; |
| 2937 |
len += sizeof(out->hdr) - out->used; |
| 2938 |
used = 0; |
| 2939 |
} |
| 2940 |
if (fp && out->hdr.msg.len && |
| 2941 |
fwrite(out->buffer + used, out->hdr.msg.len - used, |
| 2942 |
1, fp) != 1) |
| 2943 |
return "Dump buffered data error"; |
| 2944 |
len += out->hdr.msg.len - used; |
| 2945 |
if (partial && sc) |
| 2946 |
sc->data_resp_len = len; |
| 2947 |
partial = false; |
| 2948 |
} |
| 2949 |
|
| 2950 |
/* Add "OK" for live-update command. */ |
| 2951 |
if (c == lu_get_connection()) { |
| 2952 |
unsigned int rc = lu_write_response(fp); |
| 2953 |
|
| 2954 |
if (!rc) |
| 2955 |
return "Dump buffered data error"; |
| 2956 |
|
| 2957 |
len += rc; |
| 2958 |
} |
| 2959 |
|
| 2960 |
if (sc) |
| 2961 |
sc->data_out_len = len; |
| 2962 |
|
| 2963 |
return NULL; |
| 2964 |
} |
| 2965 |
|
| 2966 |
const char *dump_state_node_perms(FILE *fp, const struct xs_permissions *perms, |
| 2967 |
unsigned int n_perms) |
| 2968 |
{ |
| 2969 |
unsigned int p; |
| 2970 |
|
| 2971 |
for (p = 0; p < n_perms; p++) { |
| 2972 |
struct xs_state_node_perm sp; |
| 2973 |
|
| 2974 |
switch ((int)perms[p].perms & ~XS_PERM_IGNORE) { |
| 2975 |
case XS_PERM_READ: |
| 2976 |
sp.access = XS_STATE_NODE_PERM_READ; |
| 2977 |
break; |
| 2978 |
case XS_PERM_WRITE: |
| 2979 |
sp.access = XS_STATE_NODE_PERM_WRITE; |
| 2980 |
break; |
| 2981 |
case XS_PERM_READ | XS_PERM_WRITE: |
| 2982 |
sp.access = XS_STATE_NODE_PERM_BOTH; |
| 2983 |
break; |
| 2984 |
default: |
| 2985 |
sp.access = XS_STATE_NODE_PERM_NONE; |
| 2986 |
break; |
| 2987 |
} |
| 2988 |
sp.flags = (perms[p].perms & XS_PERM_IGNORE) |
| 2989 |
? XS_STATE_NODE_PERM_IGNORE : 0; |
| 2990 |
sp.domid = perms[p].id; |
| 2991 |
|
| 2992 |
if (fwrite(&sp, sizeof(sp), 1, fp) != 1) |
| 2993 |
return "Dump node permission error"; |
| 2994 |
|
| 2995 |
} |
| 2996 |
|
| 2997 |
return NULL; |
| 2998 |
} |
| 2999 |
|
| 3000 |
struct dump_node_data { |
| 3001 |
FILE *fp; |
| 3002 |
const char *err; |
| 3003 |
}; |
| 3004 |
|
| 3005 |
static int dump_state_node_err(struct dump_node_data *data, const char *err) |
| 3006 |
{ |
| 3007 |
data->err = err; |
| 3008 |
return WALK_TREE_ERROR_STOP; |
| 3009 |
} |
| 3010 |
|
| 3011 |
static int dump_state_node(const void *ctx, struct connection *conn, |
| 3012 |
const struct node *node, struct dump_node_data *data) |
| 3013 |
{ |
| 3014 |
FILE *fp = data->fp; |
| 3015 |
unsigned int pathlen; |
| 3016 |
struct xs_state_record_header head; |
| 3017 |
struct xs_state_node sn; |
| 3018 |
const char *ret; |
| 3019 |
|
| 3020 |
pathlen = strlen(node->name) + 1; |
| 3021 |
|
| 3022 |
head.type = XS_STATE_TYPE_NODE; |
| 3023 |
head.length = sizeof(sn); |
| 3024 |
sn.conn_id = 0; |
| 3025 |
sn.ta_id = 0; |
| 3026 |
sn.ta_access = 0; |
| 3027 |
sn.perm_n = node->hdr.num_perms; |
| 3028 |
sn.path_len = pathlen; |
| 3029 |
sn.data_len = node->hdr.datalen; |
| 3030 |
head.length += node->hdr.num_perms * sizeof(*sn.perms); |
| 3031 |
head.length += pathlen; |
| 3032 |
head.length += node->hdr.datalen; |
| 3033 |
head.length = ROUNDUP(head.length, 3); |
| 3034 |
|
| 3035 |
if (fwrite(&head, sizeof(head), 1, fp) != 1) |
| 3036 |
return dump_state_node_err(data, "Dump node head error"); |
| 3037 |
if (fwrite(&sn, sizeof(sn), 1, fp) != 1) |
| 3038 |
return dump_state_node_err(data, "Dump node state error"); |
| 3039 |
|
| 3040 |
ret = dump_state_node_perms(fp, node->perms, node->hdr.num_perms); |
| 3041 |
if (ret) |
| 3042 |
return dump_state_node_err(data, ret); |
| 3043 |
|
| 3044 |
if (fwrite(node->name, pathlen, 1, fp) != 1) |
| 3045 |
return dump_state_node_err(data, "Dump node path error"); |
| 3046 |
|
| 3047 |
if (node->hdr.datalen && |
| 3048 |
fwrite(node->data, node->hdr.datalen, 1, fp) != 1) |
| 3049 |
return dump_state_node_err(data, "Dump node data error"); |
| 3050 |
|
| 3051 |
ret = dump_state_align(fp); |
| 3052 |
if (ret) |
| 3053 |
return dump_state_node_err(data, ret); |
| 3054 |
|
| 3055 |
return WALK_TREE_OK; |
| 3056 |
} |
| 3057 |
|
| 3058 |
static int dump_state_node_enter(const void *ctx, struct connection *conn, |
| 3059 |
struct node *node, void *arg) |
| 3060 |
{ |
| 3061 |
return dump_state_node(ctx, conn, node, arg); |
| 3062 |
} |
| 3063 |
|
| 3064 |
static int dump_state_special_node(FILE *fp, const void *ctx, |
| 3065 |
struct dump_node_data *data, |
| 3066 |
const char *name) |
| 3067 |
{ |
| 3068 |
const struct node *node; |
| 3069 |
int ret; |
| 3070 |
|
| 3071 |
node = read_node_const(NULL, ctx, name); |
| 3072 |
if (!node) |
| 3073 |
return dump_state_node_err(data, "Dump node read node error"); |
| 3074 |
|
| 3075 |
ret = dump_state_node(ctx, NULL, node, data); |
| 3076 |
talloc_free(node); |
| 3077 |
|
| 3078 |
return ret; |
| 3079 |
} |
| 3080 |
|
| 3081 |
const char *dump_state_nodes(FILE *fp, const void *ctx) |
| 3082 |
{ |
| 3083 |
struct dump_node_data data = { |
| 3084 |
.fp = fp, |
| 3085 |
.err = "Dump node walk error" |
| 3086 |
}; |
| 3087 |
struct walk_funcs walkfuncs = { .enter = dump_state_node_enter }; |
| 3088 |
|
| 3089 |
if (walk_node_tree(ctx, NULL, "/", &walkfuncs, &data)) |
| 3090 |
return data.err; |
| 3091 |
|
| 3092 |
if (dump_state_special_node(fp, ctx, &data, "@releaseDomain")) |
| 3093 |
return data.err; |
| 3094 |
if (dump_state_special_node(fp, ctx, &data, "@introduceDomain")) |
| 3095 |
return data.err; |
| 3096 |
|
| 3097 |
return NULL; |
| 3098 |
} |
| 3099 |
|
| 3100 |
void read_state_global(const void *ctx, const void *state) |
| 3101 |
{ |
| 3102 |
const struct xs_state_global *glb = state; |
| 3103 |
|
| 3104 |
set_socket_fd(glb->socket_fd); |
| 3105 |
|
| 3106 |
domain_init(glb->evtchn_fd); |
| 3107 |
} |
| 3108 |
|
| 3109 |
static void add_buffered_data(struct buffered_data *bdata, |
| 3110 |
struct connection *conn, const uint8_t *data, |
| 3111 |
unsigned int len) |
| 3112 |
{ |
| 3113 |
bdata->hdr.msg.len = len; |
| 3114 |
if (len <= DEFAULT_BUFFER_SIZE) |
| 3115 |
bdata->buffer = bdata->default_buffer; |
| 3116 |
else |
| 3117 |
bdata->buffer = talloc_array(bdata, char, len); |
| 3118 |
if (!bdata->buffer) |
| 3119 |
barf("error restoring buffered data"); |
| 3120 |
|
| 3121 |
memcpy(bdata->buffer, data, len); |
| 3122 |
if (bdata->hdr.msg.type == XS_WATCH_EVENT && timeout_watch_event_msec && |
| 3123 |
domain_is_unprivileged(conn)) { |
| 3124 |
bdata->timeout_msec = get_now_msec() + timeout_watch_event_msec; |
| 3125 |
if (!conn->timeout_msec) |
| 3126 |
conn->timeout_msec = bdata->timeout_msec; |
| 3127 |
} |
| 3128 |
|
| 3129 |
/* Queue for later transmission. */ |
| 3130 |
list_add_tail(&bdata->list, &conn->out_list); |
| 3131 |
bdata->on_out_list = true; |
| 3132 |
/* |
| 3133 |
* Watch events are never "outstanding", but the request causing them |
| 3134 |
* are instead kept "outstanding" until all watch events caused by that |
| 3135 |
* request have been delivered. |
| 3136 |
*/ |
| 3137 |
if (bdata->hdr.msg.type != XS_WATCH_EVENT) |
| 3138 |
domain_outstanding_inc(conn); |
| 3139 |
/* |
| 3140 |
* We are restoring the state after Live-Update and the new quota may |
| 3141 |
* be smaller. So ignore it. The limit will be applied for any resource |
| 3142 |
* after the state has been fully restored. |
| 3143 |
*/ |
| 3144 |
domain_memory_add_nochk(conn, conn->id, len + sizeof(bdata->hdr)); |
| 3145 |
} |
| 3146 |
|
| 3147 |
void read_state_buffered_data(const void *ctx, struct connection *conn, |
| 3148 |
const struct xs_state_connection *sc) |
| 3149 |
{ |
| 3150 |
struct buffered_data *bdata; |
| 3151 |
const uint8_t *data; |
| 3152 |
unsigned int len; |
| 3153 |
bool partial = sc->data_resp_len; |
| 3154 |
|
| 3155 |
for (data = sc->data; data < sc->data + sc->data_in_len; data += len) { |
| 3156 |
bdata = new_buffer(conn); |
| 3157 |
if (!bdata) |
| 3158 |
barf("error restoring read data"); |
| 3159 |
|
| 3160 |
/* |
| 3161 |
* We don't know yet if there is more than one message |
| 3162 |
* to process. So the len is the size of the leftover data. |
| 3163 |
*/ |
| 3164 |
len = sc->data_in_len - (data - sc->data); |
| 3165 |
if (len < sizeof(bdata->hdr)) { |
| 3166 |
bdata->inhdr = true; |
| 3167 |
memcpy(&bdata->hdr, data, len); |
| 3168 |
bdata->used = len; |
| 3169 |
} else { |
| 3170 |
bdata->inhdr = false; |
| 3171 |
memcpy(&bdata->hdr, data, sizeof(bdata->hdr)); |
| 3172 |
if (bdata->hdr.msg.len <= DEFAULT_BUFFER_SIZE) |
| 3173 |
bdata->buffer = bdata->default_buffer; |
| 3174 |
else |
| 3175 |
bdata->buffer = talloc_array(bdata, char, |
| 3176 |
bdata->hdr.msg.len); |
| 3177 |
if (!bdata->buffer) |
| 3178 |
barf("Error allocating in buffer"); |
| 3179 |
bdata->used = min_t(unsigned int, |
| 3180 |
len - sizeof(bdata->hdr), |
| 3181 |
bdata->hdr.msg.len); |
| 3182 |
memcpy(bdata->buffer, data + sizeof(bdata->hdr), |
| 3183 |
bdata->used); |
| 3184 |
/* Update len to match the size of the message. */ |
| 3185 |
len = bdata->used + sizeof(bdata->hdr); |
| 3186 |
} |
| 3187 |
|
| 3188 |
/* |
| 3189 |
* If the message is not complete, then it means this was |
| 3190 |
* the current processed message. All the other messages |
| 3191 |
* will be queued to be handled after restoring. |
| 3192 |
*/ |
| 3193 |
if (bdata->inhdr || bdata->used != bdata->hdr.msg.len) { |
| 3194 |
assert(conn->in == NULL); |
| 3195 |
conn->in = bdata; |
| 3196 |
} else if (delay_request(conn, bdata, process_delayed_message, |
| 3197 |
conn, true)) |
| 3198 |
barf("Unable to delay the request"); |
| 3199 |
} |
| 3200 |
|
| 3201 |
for (data = sc->data + sc->data_in_len; |
| 3202 |
data < sc->data + sc->data_in_len + sc->data_out_len; |
| 3203 |
data += len) { |
| 3204 |
bdata = new_buffer(conn); |
| 3205 |
if (!bdata) |
| 3206 |
barf("error restoring buffered data"); |
| 3207 |
if (partial) { |
| 3208 |
bdata->inhdr = false; |
| 3209 |
/* Make trace look nice. */ |
| 3210 |
bdata->hdr.msg.type = XS_INVALID; |
| 3211 |
len = sc->data_resp_len; |
| 3212 |
add_buffered_data(bdata, conn, data, len); |
| 3213 |
partial = false; |
| 3214 |
continue; |
| 3215 |
} |
| 3216 |
|
| 3217 |
memcpy(&bdata->hdr, data, sizeof(bdata->hdr)); |
| 3218 |
data += sizeof(bdata->hdr); |
| 3219 |
len = bdata->hdr.msg.len; |
| 3220 |
add_buffered_data(bdata, conn, data, len); |
| 3221 |
} |
| 3222 |
} |
| 3223 |
|
| 3224 |
void read_state_node(const void *ctx, const void *state) |
| 3225 |
{ |
| 3226 |
const struct xs_state_node *sn = state; |
| 3227 |
struct node *node, *parent; |
| 3228 |
char *name, *parentname; |
| 3229 |
unsigned int i; |
| 3230 |
struct connection conn = { .id = priv_domid }; |
| 3231 |
|
| 3232 |
name = (char *)(sn->perms + sn->perm_n); |
| 3233 |
node = talloc(ctx, struct node); |
| 3234 |
if (!node) |
| 3235 |
barf("allocation error restoring node"); |
| 3236 |
|
| 3237 |
node->acc.memory = 0; |
| 3238 |
node->name = name; |
| 3239 |
node->hdr.generation = ++generation; |
| 3240 |
node->hdr.datalen = sn->data_len; |
| 3241 |
node->data = name + sn->path_len; |
| 3242 |
node->hdr.childlen = 0; |
| 3243 |
node->children = NULL; |
| 3244 |
node->hdr.num_perms = sn->perm_n; |
| 3245 |
node->perms = talloc_array(node, struct xs_permissions, |
| 3246 |
node->hdr.num_perms); |
| 3247 |
if (!node->perms) |
| 3248 |
barf("allocation error restoring node"); |
| 3249 |
for (i = 0; i < node->hdr.num_perms; i++) { |
| 3250 |
switch (sn->perms[i].access) { |
| 3251 |
case 'r': |
| 3252 |
node->perms[i].perms = XS_PERM_READ; |
| 3253 |
break; |
| 3254 |
case 'w': |
| 3255 |
node->perms[i].perms = XS_PERM_WRITE; |
| 3256 |
break; |
| 3257 |
case 'b': |
| 3258 |
node->perms[i].perms = XS_PERM_READ | XS_PERM_WRITE; |
| 3259 |
break; |
| 3260 |
default: |
| 3261 |
node->perms[i].perms = XS_PERM_NONE; |
| 3262 |
break; |
| 3263 |
} |
| 3264 |
if (sn->perms[i].flags & XS_STATE_NODE_PERM_IGNORE) |
| 3265 |
node->perms[i].perms |= XS_PERM_IGNORE; |
| 3266 |
node->perms[i].id = sn->perms[i].domid; |
| 3267 |
} |
| 3268 |
|
| 3269 |
if (!strstarts(name, "@")) { |
| 3270 |
parentname = get_parent(node, name); |
| 3271 |
if (!parentname) |
| 3272 |
barf("allocation error restoring node"); |
| 3273 |
parent = read_node(NULL, node, parentname); |
| 3274 |
if (!parent) |
| 3275 |
barf("read parent error restoring node"); |
| 3276 |
|
| 3277 |
if (add_child(node, parent, name)) |
| 3278 |
barf("allocation error restoring node"); |
| 3279 |
|
| 3280 |
if (write_node_raw(NULL, parentname, parent, NODE_MODIFY, true)) |
| 3281 |
barf("write parent error restoring node"); |
| 3282 |
} |
| 3283 |
|
| 3284 |
/* The "/" node is already existing, so it can only be modified here. */ |
| 3285 |
if (write_node_raw(NULL, name, node, |
| 3286 |
strcmp(name, "/") ? NODE_CREATE : NODE_MODIFY, true)) |
| 3287 |
barf("write node error restoring node"); |
| 3288 |
|
| 3289 |
if (domain_nbentry_inc(&conn, get_node_owner(node))) |
| 3290 |
barf("node accounting error restoring node"); |
| 3291 |
|
| 3292 |
talloc_free(node); |
| 3293 |
} |
| 3294 |
|
| 3295 |
/* |
| 3296 |
* Local variables: |
| 3297 |
* mode: C |
| 3298 |
* c-file-style: "linux" |
| 3299 |
* indent-tabs-mode: t |
| 3300 |
* c-basic-offset: 8 |
| 3301 |
* tab-width: 8 |
| 3302 |
* End: |
| 3303 |
*/ |