xen-common.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (C) 2014 Citrix Systems UK Ltd.
  3. *
  4. * This work is licensed under the terms of the GNU GPL, version 2. See
  5. * the COPYING file in the top-level directory.
  6. *
  7. * Contributions after 2012-01-13 are licensed under the terms of the
  8. * GNU GPL, version 2 or (at your option) any later version.
  9. */
  10. #include "hw/xen/xen_backend.h"
  11. #include "qmp-commands.h"
  12. #include "sysemu/char.h"
  13. //#define DEBUG_XEN
  14. #ifdef DEBUG_XEN
  15. #define DPRINTF(fmt, ...) \
  16. do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
  17. #else
  18. #define DPRINTF(fmt, ...) \
  19. do { } while (0)
  20. #endif
  21. static int store_dev_info(int domid, CharDriverState *cs, const char *string)
  22. {
  23. struct xs_handle *xs = NULL;
  24. char *path = NULL;
  25. char *newpath = NULL;
  26. char *pts = NULL;
  27. int ret = -1;
  28. /* Only continue if we're talking to a pty. */
  29. if (strncmp(cs->filename, "pty:", 4)) {
  30. return 0;
  31. }
  32. pts = cs->filename + 4;
  33. /* We now have everything we need to set the xenstore entry. */
  34. xs = xs_open(0);
  35. if (xs == NULL) {
  36. fprintf(stderr, "Could not contact XenStore\n");
  37. goto out;
  38. }
  39. path = xs_get_domain_path(xs, domid);
  40. if (path == NULL) {
  41. fprintf(stderr, "xs_get_domain_path() error\n");
  42. goto out;
  43. }
  44. newpath = realloc(path, (strlen(path) + strlen(string) +
  45. strlen("/tty") + 1));
  46. if (newpath == NULL) {
  47. fprintf(stderr, "realloc error\n");
  48. goto out;
  49. }
  50. path = newpath;
  51. strcat(path, string);
  52. strcat(path, "/tty");
  53. if (!xs_write(xs, XBT_NULL, path, pts, strlen(pts))) {
  54. fprintf(stderr, "xs_write for '%s' fail", string);
  55. goto out;
  56. }
  57. ret = 0;
  58. out:
  59. free(path);
  60. xs_close(xs);
  61. return ret;
  62. }
  63. void xenstore_store_pv_console_info(int i, CharDriverState *chr)
  64. {
  65. if (i == 0) {
  66. store_dev_info(xen_domid, chr, "/console");
  67. } else {
  68. char buf[32];
  69. snprintf(buf, sizeof(buf), "/device/console/%d", i);
  70. store_dev_info(xen_domid, chr, buf);
  71. }
  72. }
  73. static void xenstore_record_dm_state(struct xs_handle *xs, const char *state)
  74. {
  75. char path[50];
  76. if (xs == NULL) {
  77. fprintf(stderr, "xenstore connection not initialized\n");
  78. exit(1);
  79. }
  80. snprintf(path, sizeof (path), "device-model/%u/state", xen_domid);
  81. if (!xs_write(xs, XBT_NULL, path, state, strlen(state))) {
  82. fprintf(stderr, "error recording dm state\n");
  83. exit(1);
  84. }
  85. }
  86. static void xen_change_state_handler(void *opaque, int running,
  87. RunState state)
  88. {
  89. if (running) {
  90. /* record state running */
  91. xenstore_record_dm_state(xenstore, "running");
  92. }
  93. }
  94. int xen_init(MachineClass *mc)
  95. {
  96. xen_xc = xen_xc_interface_open(0, 0, 0);
  97. if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
  98. xen_be_printf(NULL, 0, "can't open xen interface\n");
  99. return -1;
  100. }
  101. qemu_add_vm_change_state_handler(xen_change_state_handler, NULL);
  102. return 0;
  103. }