| 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 <sys/types.h> |
| 20 |
#include <sys/stat.h> |
| 21 |
#include <unistd.h> |
| 22 |
#include <fcntl.h> |
| 23 |
#include <signal.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <syslog.h> |
| 26 |
#include <sys/mman.h> |
| 27 |
#include <sys/socket.h> |
| 28 |
#include <sys/un.h> |
| 29 |
#if defined(HAVE_SYSTEMD) |
| 30 |
#include <xen-sd-notify.h> |
| 31 |
#endif |
| 32 |
#include <xen-tools/xenstore-common.h> |
| 33 |
|
| 34 |
#include "utils.h" |
| 35 |
#include "core.h" |
| 36 |
#include "osdep.h" |
| 37 |
#include "talloc.h" |
| 38 |
|
| 39 |
static int reopen_log_pipe0_pollfd_idx = -1; |
| 40 |
static int reopen_log_pipe[2]; |
| 41 |
|
| 42 |
static int sock_pollfd_idx = -1; |
| 43 |
static int sock = -1; |
| 44 |
|
| 45 |
static void write_pidfile(const char *pidfile) |
| 46 |
{ |
| 47 |
char buf[100]; |
| 48 |
int len; |
| 49 |
int fd; |
| 50 |
|
| 51 |
fd = open(pidfile, O_RDWR | O_CREAT, 0600); |
| 52 |
if (fd == -1) |
| 53 |
barf_perror("Opening pid file %s", pidfile); |
| 54 |
|
| 55 |
/* We exit silently if daemon already running. */ |
| 56 |
if (lockf(fd, F_TLOCK, 0) == -1) |
| 57 |
exit(0); |
| 58 |
|
| 59 |
len = snprintf(buf, sizeof(buf), "%ld\n", (long)getpid()); |
| 60 |
if (write(fd, buf, len) != len) |
| 61 |
barf_perror("Writing pid file %s", pidfile); |
| 62 |
|
| 63 |
close(fd); |
| 64 |
} |
| 65 |
|
| 66 |
/* Stevens. */ |
| 67 |
static void daemonize(void) |
| 68 |
{ |
| 69 |
pid_t pid; |
| 70 |
|
| 71 |
/* Separate from our parent via fork, so init inherits us. */ |
| 72 |
if ((pid = fork()) < 0) |
| 73 |
barf_perror("Failed to fork daemon"); |
| 74 |
if (pid != 0) |
| 75 |
exit(0); |
| 76 |
|
| 77 |
/* Session leader so ^C doesn't whack us. */ |
| 78 |
setsid(); |
| 79 |
|
| 80 |
/* Let session leader exit so child cannot regain CTTY */ |
| 81 |
if ((pid = fork()) < 0) |
| 82 |
barf_perror("Failed to fork daemon"); |
| 83 |
if (pid != 0) |
| 84 |
exit(0); |
| 85 |
|
| 86 |
/* Move off any mount points we might be in. */ |
| 87 |
if (chdir("/") == -1) |
| 88 |
barf_perror("Failed to chdir"); |
| 89 |
|
| 90 |
/* Discard our parent's old-fashioned umask prejudices. */ |
| 91 |
umask(0); |
| 92 |
} |
| 93 |
|
| 94 |
/* |
| 95 |
* Signal handler for SIGHUP, which requests that the trace log is reopened |
| 96 |
* (in the main loop). A single byte is written to reopen_log_pipe, to awaken |
| 97 |
* the poll() in the main loop. |
| 98 |
*/ |
| 99 |
static void trigger_reopen_log(int signal __attribute__((unused))) |
| 100 |
{ |
| 101 |
char c = 'A'; |
| 102 |
int dummy; |
| 103 |
|
| 104 |
dummy = write(reopen_log_pipe[1], &c, 1); |
| 105 |
} |
| 106 |
|
| 107 |
void finish_daemonize(void) |
| 108 |
{ |
| 109 |
int devnull = open("/dev/null", O_RDWR); |
| 110 |
if (devnull == -1) |
| 111 |
barf_perror("Could not open /dev/null\n"); |
| 112 |
dup2(devnull, STDIN_FILENO); |
| 113 |
dup2(devnull, STDOUT_FILENO); |
| 114 |
dup2(devnull, STDERR_FILENO); |
| 115 |
close(devnull); |
| 116 |
} |
| 117 |
|
| 118 |
static void init_pipe(void) |
| 119 |
{ |
| 120 |
int flags; |
| 121 |
unsigned int i; |
| 122 |
|
| 123 |
if (pipe(reopen_log_pipe)) { |
| 124 |
barf_perror("pipe"); |
| 125 |
} |
| 126 |
|
| 127 |
for (i = 0; i < 2; i++) { |
| 128 |
flags = fcntl(reopen_log_pipe[i], F_GETFD); |
| 129 |
if (flags < 0) |
| 130 |
barf_perror("pipe get flags"); |
| 131 |
flags |= FD_CLOEXEC; |
| 132 |
if (fcntl(reopen_log_pipe[i], F_SETFD, flags) < 0) |
| 133 |
barf_perror("pipe set flags"); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
void unmap_xenbus(void *interface) |
| 138 |
{ |
| 139 |
munmap(interface, getpagesize()); |
| 140 |
} |
| 141 |
|
| 142 |
evtchn_port_t get_xenbus_evtchn(void) |
| 143 |
{ |
| 144 |
int fd; |
| 145 |
int rc; |
| 146 |
evtchn_port_t port; |
| 147 |
char str[20]; |
| 148 |
|
| 149 |
fd = open(XENSTORED_PORT_DEV, O_RDONLY); |
| 150 |
if (fd == -1) |
| 151 |
return -1; |
| 152 |
|
| 153 |
rc = read(fd, str, sizeof(str) - 1); |
| 154 |
if (rc == -1) |
| 155 |
{ |
| 156 |
int err = errno; |
| 157 |
close(fd); |
| 158 |
errno = err; |
| 159 |
return -1; |
| 160 |
} |
| 161 |
|
| 162 |
str[rc] = '\0'; |
| 163 |
port = strtoul(str, NULL, 0); |
| 164 |
|
| 165 |
close(fd); |
| 166 |
return port; |
| 167 |
} |
| 168 |
|
| 169 |
void *xenbus_map(void) |
| 170 |
{ |
| 171 |
int fd; |
| 172 |
void *addr; |
| 173 |
|
| 174 |
fd = open(XENSTORED_KVA_DEV, O_RDWR); |
| 175 |
if (fd == -1) |
| 176 |
return NULL; |
| 177 |
|
| 178 |
addr = mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE, |
| 179 |
MAP_SHARED, fd, 0); |
| 180 |
|
| 181 |
if (addr == MAP_FAILED) |
| 182 |
addr = NULL; |
| 183 |
|
| 184 |
close(fd); |
| 185 |
|
| 186 |
return addr; |
| 187 |
} |
| 188 |
|
| 189 |
static int writefd(struct connection *conn, const void *data, unsigned int len) |
| 190 |
{ |
| 191 |
int rc; |
| 192 |
|
| 193 |
while ((rc = write(conn->fd, data, len)) < 0) { |
| 194 |
if (errno == EAGAIN) { |
| 195 |
rc = 0; |
| 196 |
break; |
| 197 |
} |
| 198 |
if (errno != EINTR) |
| 199 |
break; |
| 200 |
} |
| 201 |
|
| 202 |
return rc; |
| 203 |
} |
| 204 |
|
| 205 |
static int readfd(struct connection *conn, void *data, unsigned int len) |
| 206 |
{ |
| 207 |
int rc; |
| 208 |
|
| 209 |
while ((rc = read(conn->fd, data, len)) < 0) { |
| 210 |
if (errno == EAGAIN) { |
| 211 |
rc = 0; |
| 212 |
break; |
| 213 |
} |
| 214 |
if (errno != EINTR) |
| 215 |
break; |
| 216 |
} |
| 217 |
|
| 218 |
/* Reading zero length means we're done with this connection. */ |
| 219 |
if ((rc == 0) && (len != 0)) { |
| 220 |
errno = EBADF; |
| 221 |
rc = -1; |
| 222 |
} |
| 223 |
|
| 224 |
return rc; |
| 225 |
} |
| 226 |
|
| 227 |
static bool socket_can_process(struct connection *conn, int mask) |
| 228 |
{ |
| 229 |
if (conn->pollfd_idx == -1) |
| 230 |
return false; |
| 231 |
|
| 232 |
if (poll_fds[conn->pollfd_idx].revents & ~(POLLIN | POLLOUT)) { |
| 233 |
talloc_free(conn); |
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
return (poll_fds[conn->pollfd_idx].revents & mask); |
| 238 |
} |
| 239 |
|
| 240 |
static bool socket_can_write(struct connection *conn) |
| 241 |
{ |
| 242 |
return socket_can_process(conn, POLLOUT); |
| 243 |
} |
| 244 |
|
| 245 |
static bool socket_can_read(struct connection *conn) |
| 246 |
{ |
| 247 |
return socket_can_process(conn, POLLIN); |
| 248 |
} |
| 249 |
|
| 250 |
static const struct interface_funcs socket_funcs = { |
| 251 |
.write = writefd, |
| 252 |
.read = readfd, |
| 253 |
.can_write = socket_can_write, |
| 254 |
.can_read = socket_can_read, |
| 255 |
}; |
| 256 |
|
| 257 |
static void accept_connection(int sock) |
| 258 |
{ |
| 259 |
int fd; |
| 260 |
struct connection *conn; |
| 261 |
|
| 262 |
fd = accept(sock, NULL, NULL); |
| 263 |
if (fd < 0) |
| 264 |
return; |
| 265 |
|
| 266 |
conn = new_connection(&socket_funcs); |
| 267 |
if (conn) { |
| 268 |
conn->fd = fd; |
| 269 |
conn->id = dom0_domid; |
| 270 |
} else |
| 271 |
close(fd); |
| 272 |
} |
| 273 |
|
| 274 |
static void destroy_fds(void) |
| 275 |
{ |
| 276 |
if (sock >= 0) |
| 277 |
close(sock); |
| 278 |
} |
| 279 |
|
| 280 |
static void init_sockets(void) |
| 281 |
{ |
| 282 |
struct sockaddr_un addr; |
| 283 |
const char *soc_str = xenstore_daemon_path(); |
| 284 |
|
| 285 |
if (!soc_str) |
| 286 |
barf_perror("Failed to obtain xs domain socket"); |
| 287 |
|
| 288 |
/* Create sockets for them to listen to. */ |
| 289 |
atexit(destroy_fds); |
| 290 |
sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 291 |
if (sock < 0) |
| 292 |
barf_perror("Could not create socket"); |
| 293 |
|
| 294 |
/* FIXME: Be more sophisticated, don't mug running daemon. */ |
| 295 |
unlink(soc_str); |
| 296 |
|
| 297 |
addr.sun_family = AF_UNIX; |
| 298 |
|
| 299 |
if (strlen(soc_str) >= sizeof(addr.sun_path)) |
| 300 |
barf_perror("socket string '%s' too long", soc_str); |
| 301 |
strcpy(addr.sun_path, soc_str); |
| 302 |
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) != 0) |
| 303 |
barf_perror("Could not bind socket to %s", soc_str); |
| 304 |
|
| 305 |
if (chmod(soc_str, 0600) != 0) |
| 306 |
barf_perror("Could not chmod sockets"); |
| 307 |
|
| 308 |
if (listen(sock, 1) != 0) |
| 309 |
barf_perror("Could not listen on sockets"); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
struct connection *add_socket_connection(int fd) |
| 314 |
{ |
| 315 |
struct connection *conn; |
| 316 |
|
| 317 |
conn = new_connection(&socket_funcs); |
| 318 |
if (!conn) |
| 319 |
barf("error restoring connection"); |
| 320 |
conn->fd = fd; |
| 321 |
|
| 322 |
return conn; |
| 323 |
} |
| 324 |
|
| 325 |
void early_init(bool live_update, bool dofork, const char *pidfile) |
| 326 |
{ |
| 327 |
reopen_log(); |
| 328 |
|
| 329 |
/* Make sure xenstored directories exist. */ |
| 330 |
/* Errors ignored here, will be reported when we open files */ |
| 331 |
mkdir(xenstore_daemon_rundir(), 0755); |
| 332 |
mkdir(XENSTORE_LIB_DIR, 0755); |
| 333 |
|
| 334 |
if (dofork) { |
| 335 |
openlog("xenstored", 0, LOG_DAEMON); |
| 336 |
if (!live_update) |
| 337 |
daemonize(); |
| 338 |
} |
| 339 |
|
| 340 |
if (pidfile) |
| 341 |
write_pidfile(pidfile); |
| 342 |
|
| 343 |
/* Don't kill us with SIGPIPE. */ |
| 344 |
signal(SIGPIPE, SIG_IGN); |
| 345 |
signal(SIGHUP, trigger_reopen_log); |
| 346 |
|
| 347 |
if (!live_update) |
| 348 |
init_sockets(); |
| 349 |
|
| 350 |
init_pipe(); |
| 351 |
} |
| 352 |
|
| 353 |
void set_special_fds(void) |
| 354 |
{ |
| 355 |
if (reopen_log_pipe[0] != -1) |
| 356 |
reopen_log_pipe0_pollfd_idx = |
| 357 |
set_fd(reopen_log_pipe[0], POLLIN|POLLPRI); |
| 358 |
|
| 359 |
if (sock != -1) |
| 360 |
sock_pollfd_idx = set_fd(sock, POLLIN|POLLPRI); |
| 361 |
} |
| 362 |
|
| 363 |
void handle_special_fds(void) |
| 364 |
{ |
| 365 |
if (reopen_log_pipe0_pollfd_idx != -1) { |
| 366 |
if (poll_fds[reopen_log_pipe0_pollfd_idx].revents & ~POLLIN) { |
| 367 |
close(reopen_log_pipe[0]); |
| 368 |
close(reopen_log_pipe[1]); |
| 369 |
init_pipe(); |
| 370 |
} else if (poll_fds[reopen_log_pipe0_pollfd_idx].revents & |
| 371 |
POLLIN) { |
| 372 |
char c; |
| 373 |
|
| 374 |
if (read(reopen_log_pipe[0], &c, 1) != 1) |
| 375 |
barf_perror("read failed"); |
| 376 |
reopen_log(); |
| 377 |
} |
| 378 |
reopen_log_pipe0_pollfd_idx = -1; |
| 379 |
} |
| 380 |
|
| 381 |
if (sock_pollfd_idx != -1) { |
| 382 |
if (poll_fds[sock_pollfd_idx].revents & ~POLLIN) { |
| 383 |
barf_perror("sock poll failed"); |
| 384 |
} else if (poll_fds[sock_pollfd_idx].revents & POLLIN) { |
| 385 |
accept_connection(sock); |
| 386 |
sock_pollfd_idx = -1; |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
void late_init(bool live_update) |
| 392 |
{ |
| 393 |
#if defined(HAVE_SYSTEMD) |
| 394 |
if (!live_update) { |
| 395 |
sd_notify(1, "READY=1"); |
| 396 |
fprintf(stderr, "xenstored is ready\n"); |
| 397 |
} |
| 398 |
#endif |
| 399 |
} |
| 400 |
|
| 401 |
int get_socket_fd(void) |
| 402 |
{ |
| 403 |
return sock; |
| 404 |
} |
| 405 |
|
| 406 |
void set_socket_fd(int fd) |
| 407 |
{ |
| 408 |
sock = fd; |
| 409 |
} |
| 410 |
|
| 411 |
const char *xenstore_rundir(void) |
| 412 |
{ |
| 413 |
return xenstore_daemon_rundir(); |
| 414 |
} |