2 * Copyright (C) 2009 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <sys/types.h>
32 #include <sys/socket.h>
35 #define UNIX_PATH_MAX 108
37 #include <apr_general.h>
38 #include <apr_network_io.h>
39 #include <apr_getopt.h>
40 #include <apr_strings.h>
41 #include <apr_thread_proc.h>
44 #include <apr_portable.h>
46 #include "hostinfod.h"
48 static void main_loop (void);
49 static void set_reread_socket_dir (const apr_pollfd_t *, void *);
50 static void do_reread_socket_dir (void);
51 static struct guest_description *guest_added (const char *sock_path, const char *name);
52 static void guest_removed (struct guest_description *);
53 static void guest_event (const apr_pollfd_t *, void *);
54 static void modify_pollfd_reqevents (struct guest_description *, apr_int16_t);
56 const char *conf_file = DEFAULT_CONF_FILE;
57 char *socket_dir = NULL;
58 char *guests_file = NULL;
60 char *libvirt_uri = NULL;
61 int libvirt_uri_set_on_cmdline = 0;
64 int verbose_set_on_cmdline = 0;
66 int foreground_set_on_cmdline = 0;
68 int messages_to_stderr = 1;
70 static int reread_socket_dir = 1;
73 apr_pool_t *pool = NULL;
74 static apr_pollset_t *set = NULL;
76 static apr_hash_t *guests = NULL; /* Hash "driver-name" -> guest_description */
78 typedef void (*poll_callback) (const apr_pollfd_t *, void *data);
79 struct callback_data {
80 poll_callback callback;
87 printf ("hostinfod (virt-hostinfo daemon)\n"
88 "Copyright (C) 2009 Red Hat Inc.\n"
91 " hostinfod [--options]\n"
94 " --help Display full usage\n"
95 " -c file | --config file\n"
96 " Configuration file (default: %s)\n"
97 " -C uri | --connect uri\n"
98 " Set libvirt connection URI (default: NULL)\n"
99 " -f | --foreground\n"
100 " Run in the foreground (don't fork)\n"
101 " -v Enable verbose messages (sent to syslog)\n",
109 apr_pool_create (&pool, NULL);
114 main (int argc, char *argv[])
116 static const apr_getopt_option_t options[] = {
117 { "config", 'c', TRUE, "configuration file" },
118 { "connect", 'C', TRUE, "libvirt connection URI" },
119 { "foreground", 'f', FALSE, "run in foreground (don't fork)" },
120 { "verbose", 'v', FALSE, "enable verbose messages" },
121 { "help", '?', FALSE, "display help" },
122 { NULL, 0, 0, NULL },
129 /* REGISTER_COMMAND macro should have caused this to be
130 * initialized. If it's not, then something is badly wrong ...
133 error ("internal error: daemon not initialized - no commands registered");
137 apr_getopt_init (&opt, pool, argc, argv);
139 socket_dir = apr_pstrdup (pool, DEFAULT_SOCKET_DIR);
140 guests_file = apr_pstrdup (pool, DEFAULT_GUESTS_FILE);
143 while ((r = apr_getopt_long (opt, options, &c, &optarg)) == APR_SUCCESS) {
147 /* If the user is specifying this on the command line, then
148 * it should exist. They may have typo'd the name.
150 if (access (conf_file, R_OK) == -1) {
151 perrorf ("%s", conf_file);
156 libvirt_uri = optarg;
157 libvirt_uri_set_on_cmdline = 1;
161 foreground_set_on_cmdline = 1;
165 verbose_set_on_cmdline = 1;
175 fprintf (stderr, "%s: unknown command line option\n", argv[0]);
179 /* Read the config file. */
180 read_main_conf_file ();
182 /* Connect to libvirt. */
185 /* Monitor the socket directory. */
186 monitor_socket_dir ();
188 /* Create the guests hash. */
189 guests = apr_hash_make (pool);
191 /* Create the initial pollset, just containing inotify socket. */
192 r = apr_pollset_create (&set, 1024 /* ? */, pool, 0);
193 if (r != APR_SUCCESS) {
194 paprerror (r, "apr_pollset_create");
197 apr_socket_t *tsock = NULL;
198 r = apr_os_sock_put (&tsock, &sockets_inotify_fd, pool);
199 if (r != APR_SUCCESS) {
200 paprerror (r, "apr_os_sock_put");
203 apr_pollfd_t *tpollfd = apr_palloc (pool, sizeof *tpollfd);
205 tpollfd->desc_type = APR_POLL_SOCKET;
206 tpollfd->reqevents = APR_POLLIN;
207 tpollfd->rtnevents = 0;
208 tpollfd->desc.s = tsock;
210 struct callback_data *callback_data =
211 apr_palloc (pool, sizeof *callback_data);
212 callback_data->callback = set_reread_socket_dir;
213 callback_data->data = NULL;
214 tpollfd->client_data = callback_data;
216 r = apr_pollset_add (set, tpollfd);
217 if (r != APR_SUCCESS) {
218 paprerror (r, "apr_pollset_add");
227 /* After we detach from the terminal, all further messages
228 * should just go to syslog.
230 messages_to_stderr = 0;
233 message ("%s started", PACKAGE_STRING);
235 message ("%s exiting", PACKAGE_STRING);
245 apr_int32_t numdescs;
246 const apr_pollfd_t *descs;
250 /* A socket has appeared or disappeared from the socket directory. */
251 if (reread_socket_dir) {
252 do_reread_socket_dir ();
253 reread_socket_dir = 0;
259 r = apr_pollset_poll (set, -1, &numdescs, &descs);
260 if (r != APR_SUCCESS) {
261 paprerror (r, "apr_pollset_poll");
265 /* Perform the callbacks. */
266 for (i = 0; i < numdescs; ++i) {
267 struct callback_data *callback_data;
269 callback_data = descs[i].client_data;
270 callback_data->callback (&descs[i], callback_data->data);
276 set_reread_socket_dir (const apr_pollfd_t *ignored1, void *ignored2)
278 reread_socket_dir = 1;
282 do_reread_socket_dir (void)
284 static int count = 0;
285 int added = 0, removed = 0;
291 struct guest_description *hval;
292 apr_hash_index_t *hi;
295 debug ("reading socket directory (counter = %d)", count);
297 /* Discard anything which appears on the inotify socket. We will
298 * reread the whole directory each time.
301 r = read (sockets_inotify_fd, buf, sizeof buf);
303 if (errno != EAGAIN && errno != EWOULDBLOCK) {
304 perrorf ("inotify socket: read");
310 dir = opendir (socket_dir);
312 perrorf ("%s: failed to open socket directory", socket_dir);
316 while (errno = 0, (d = readdir (dir)) != NULL) {
317 /* We expect the name to be "<driver>-<name>" (where <driver>
318 * is the libvirt driver name, and <name> is the name of the
319 * domain). Skip any dot-entries and anything that doesn't have
322 if (d->d_name[0] == '.')
324 if (strlen (d->d_name) < 3 || strchr (&d->d_name[1], '-') == NULL)
327 /* It must be a Unix domain socket - skip anything else. */
328 snprintf (buf, sizeof buf, "%s/%s", socket_dir, d->d_name);
329 if (stat (buf, &statbuf) == -1) {
330 perrorf ("stat: %s", buf);
333 if (!S_ISSOCK (statbuf.st_mode))
336 /* See if we have an entry matching this already. */
337 hval = (struct guest_description *)
338 apr_hash_get (guests, d->d_name, APR_HASH_KEY_STRING);
340 hval = guest_added (buf, d->d_name);
344 /* NB. It's not well documented, but the hash table
345 * implementation DOES NOT copy the key internally. Therefore
346 * we have to use hval->name (ie. our copy) as the key, NOT
347 * d->d_name, even though they are the same string.
349 apr_hash_set (guests, hval->name, APR_HASH_KEY_STRING, hval);
353 hval->counter = count;
356 perrorf ("%s: error reading socket directory", socket_dir);
360 if (closedir (dir) == -1) {
361 perrorf ("%s: error closing socket directory", socket_dir);
365 /* Iterate over the hash and look for any guests which have
366 * gone away. The guest_description.counter field won't have
369 for (hi = apr_hash_first (pool, guests); hi; hi = apr_hash_next (hi)) {
370 apr_hash_this(hi, NULL, NULL, (void **) &hval);
371 if (hval->counter != count) {
372 /* This hash table implementation allows you to delete the
373 * current entry safely.
375 apr_hash_set (guests, hval->name, APR_HASH_KEY_STRING, NULL);
377 /* guest_removed frees hval but does not unregister it from the
380 guest_removed (hval);
385 debug ("finished reading socket directory, added %d, removed %d, guests %d",
386 added, removed, apr_hash_count (guests));
389 /* This is called whenever we detect that a guest socket has been
390 * created in the socket directory.
392 static struct guest_description *
393 guest_added (const char *sock_path, const char *name)
395 struct guest_description *hval = NULL;
398 unsigned retries = 0, tns;
399 enum guest_state state;
400 apr_pool_t *guest_pool = NULL;
401 struct sockaddr_un addr;
404 sock = socket (AF_UNIX, SOCK_STREAM, 0);
410 if (fcntl (sock, F_SETFL, O_NONBLOCK) == -1) {
411 perrorf ("fcntl: O_NONBLOCK");
415 if (fcntl (sock, F_SETFD, FD_CLOEXEC) == -1) {
416 perrorf ("fcntl: FD_CLOEXEC");
421 addr.sun_family = AF_UNIX;
422 strncpy (addr.sun_path, sock_path, UNIX_PATH_MAX);
423 addr.sun_path[UNIX_PATH_MAX-1] = '\0';
426 r = connect (sock, (struct sockaddr *) &addr, sizeof addr);
428 /* Nasty race condition: The moment the listener binds the socket,
429 * we see it in the directory and can try to connect to it.
430 * However the listener might not have called listen(2) yet, which
431 * means if we are faster than the other end, we will get
432 * ECONNREFUSED. If this happens, sleep a bit and try again a few
435 if (errno == ECONNREFUSED) {
438 ts.tv_sec = tns / 1000000000;
439 ts.tv_nsec = tns % 1000000000;
440 nanosleep (&ts, NULL);
446 if (errno != EINPROGRESS) {
447 /* Dead socket - cull these dead sockets from the directory. */
448 perrorf ("connect: %s", sock_path);
453 state = guest_state_connecting;
456 state = guest_state_request;
458 /* Create a pool which can be used for allocations
459 * during the lifetime of this guest connection.
461 apr_pool_create (&guest_pool, pool);
463 hval = apr_pcalloc (guest_pool, sizeof *hval);
464 hval->pool = guest_pool;
466 /* Create the remaining hash fields. */
468 hval->name = apr_pstrdup (hval->pool, name);
469 hval->sock_path = apr_pstrdup (hval->pool, sock_path);
471 hval->request_max = 4096;
472 hval->request = apr_palloc (hval->pool, hval->request_max);
473 hval->lasttime = apr_hash_make (hval->pool);
475 /* Convert Unix fd into APR socket type. */
476 r = apr_os_sock_put (&hval->aprsock, &sock, hval->pool);
477 if (r != APR_SUCCESS) {
478 paprerror (r, "apr_os_sock_put: %s", sock_path);
482 /* Register the socket in the pollset. */
483 hval->pollfd.p = hval->pool;
484 hval->pollfd.desc_type = APR_POLL_SOCKET;
485 if (hval->state == guest_state_connecting)
486 hval->pollfd.reqevents = APR_POLLOUT;
488 hval->pollfd.reqevents = APR_POLLIN;
489 hval->pollfd.rtnevents = 0;
490 hval->pollfd.desc.s = hval->aprsock;
492 struct callback_data *callback_data =
493 apr_palloc (hval->pool, sizeof *callback_data);
494 callback_data->callback = guest_event;
495 callback_data->data = hval;
496 hval->pollfd.client_data = callback_data;
498 r = apr_pollset_add (set, &hval->pollfd);
499 if (r != APR_SUCCESS) {
500 paprerror (r, "apr_pollset_add: %s", sock_path);
504 message ("new guest added: %s", hval->name);
508 /* This is called whenever we detect that a guest socket has been
509 * removed from the socket directory. The guest_description parameter
510 * is freed after this call and must not be used again.
513 guest_removed (struct guest_description *hval)
517 message ("guest removed: %s", hval->name);
519 /* Unregister the socket from the pollset. */
520 r = apr_pollset_remove (set, &hval->pollfd);
521 if (r != APR_SUCCESS)
522 paprerror (r, "%s: apr_pollset_remove", hval->name);
524 if (close (hval->sock) == -1)
525 pwarningf ("close: %s", hval->sock_path);
527 /* This also frees hval and all related data. */
528 apr_pool_destroy (hval->pool);
531 /* Forcibly remove a guest, removing the socket from the
532 * socket directory and cleaning up any resources used in
533 * the daemon. The guest_description parameter is freed
534 * after this call and must not be used again.
537 guest_force_close (struct guest_description *hval)
539 debug ("forcibly closing guest: %s", hval->name);
541 apr_hash_set (guests, hval->name, APR_HASH_KEY_STRING, NULL);
542 unlink (hval->sock_path);
543 guest_removed (hval);
546 /* Difference between two timespec structures (r = a - b) */
548 diff_timespec (struct timespec *r,
549 const struct timespec *a, const struct timespec *b)
551 if (a->tv_nsec - b->tv_nsec < 0) {
552 r->tv_sec = a->tv_sec - b->tv_sec - 1;
553 r->tv_nsec = 1000000000 + a->tv_nsec - b->tv_nsec;
555 r->tv_sec = a->tv_sec - b->tv_sec;
556 r->tv_nsec = a->tv_nsec - b->tv_nsec;
562 /* This is called when there is some event from the guest, eg.
563 * connection finished, read, write or closed.
566 guest_event (const apr_pollfd_t *pollfd, void *hvalv)
568 struct guest_description *hval = hvalv;
569 int err, max, r, extra;
574 #ifdef HAVE_CLOCK_GETTIME
575 clock_gettime (CLOCK_MONOTONIC, &now);
578 gettimeofday (&tv, NULL);
579 now.tv_sec = tv.tv_sec;
580 now.tv_nsec = tv.tv_usec * 1000;
583 /* If the guest keeps doing bad stuff, eventually lose patience with it. */
584 if (hval->penalty >= 100) {
585 error ("%s: guest did too much bad stuff, so we stopped talking to it",
587 guest_force_close (hval);
591 /* Decrement the penalty once a minute, so the guest can recover. */
592 if (hval->penalty > 0) {
593 struct timespec diff;
595 diff_timespec (&diff, &now, &hval->last_penalty_decr);
597 if (diff.tv_sec >= 60) {
599 hval->last_penalty_decr = now;
603 switch (hval->state) {
604 case guest_state_connecting:
605 /* Once we get a write event, we know the socket has
606 * connected, or there is an error.
610 getsockopt (hval->sock, SOL_SOCKET, SO_ERROR, &err, &len);
612 hval->state = guest_state_request;
615 perrorf ("connect: %s", hval->sock_path);
616 guest_force_close (hval);
621 case guest_state_request:
622 /* Reading the guest's request, a single line terminated by \r?\n */
623 max = hval->request_max - hval->request_posn;
624 if (max <= 0) { /* Request too long w/o termination. */
626 hval->request_posn = 0;
629 r = read (hval->sock, &hval->request[hval->request_posn], max);
630 if (r == 0) { /* Socket closed. */
631 guest_force_close (hval);
635 if (errno != EAGAIN && errno != EWOULDBLOCK) {
636 perrorf ("read: %s", hval->sock_path);
637 guest_force_close (hval);
643 hval->request_posn += r;
645 /* Have we got a terminating \n character in the buffer yet? Note
646 * the buffer is not NUL-terminated which is why we use memchr.
649 p = memchr (hval->request, '\n', hval->request_posn);
653 /* Is there more after the \n char? Normal guests shouldn't do
654 * this, but it can be an attempt to reestablish synchronization.
655 * It's documented that we throw away all but the last command sent,
658 extra = &hval->request[hval->request_posn]-(p+1);
661 memmove (hval->request, p+1, extra);
662 hval->request_posn = extra;
666 /* Looks like we've got ourselves a command. Remove trailing
667 * \r?\n char(s) and NUL-terminate the command string.
670 assert (hval->request_posn >= 1);
671 assert (p == &hval->request[hval->request_posn-1]);
672 hval->request_posn--;
675 if (hval->request_posn > 0 && *p == '\r') {
676 hval->request_posn--;
682 execute_command (&now, hval, hval->request);
684 hval->request_posn = 0;
687 case guest_state_reply:
688 /* Keep writing out the reply buffer until we've sent
691 max = hval->reply_size - hval->reply_posn;
693 hval->state = guest_state_request;
697 r = write (hval->sock, &hval->reply[hval->reply_posn], max);
699 if (errno != EAGAIN && errno != EWOULDBLOCK) {
700 perrorf ("write: %s", hval->sock_path);
701 guest_force_close (hval);
707 hval->reply_posn += r;
708 if (hval->reply_posn >= hval->reply_size)
709 hval->state = guest_state_request;
713 case guest_state_dead:
714 /* We shouldn't get an event here. */
718 /* Depending on the (new) state we want to set the
719 * events that we would like poll to give us next time.
721 switch (hval->state) {
722 case guest_state_connecting:
723 modify_pollfd_reqevents (hval, APR_POLLOUT);
725 case guest_state_request:
726 modify_pollfd_reqevents (hval, APR_POLLIN);
728 case guest_state_reply:
729 modify_pollfd_reqevents (hval, APR_POLLOUT);
731 case guest_state_dead:
732 modify_pollfd_reqevents (hval, 0);
737 /* It turns out you can't just update the pollfd->reqevents
738 * field. Instead you have to remove the pollfd and reregister
742 modify_pollfd_reqevents (struct guest_description *hval,
743 apr_int16_t new_reqevents)
747 if (hval->pollfd.reqevents != new_reqevents) {
748 r = apr_pollset_remove (set, &hval->pollfd);
749 if (r != APR_SUCCESS) {
750 paprerror (r, "%s: apr_pollset_remove", hval->name);
754 hval->pollfd.reqevents = new_reqevents;
755 r = apr_pollset_add (set, &hval->pollfd);
756 if (r != APR_SUCCESS)
757 paprerror (r, "%s: apr_pollset_add", hval->name);