qubes_penctl.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * The Qubes OS Project, http://www.qubes-os.org
  3. *
  4. * Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. *
  20. */
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <xs.h>
  26. void check_name(unsigned char *s)
  27. {
  28. int c;
  29. for (; *s; s++) {
  30. c = *s;
  31. if (c >= 'a' && c <= 'z')
  32. continue;
  33. if (c >= 'A' && c <= 'Z')
  34. continue;
  35. if (c == '_' || c == '-')
  36. continue;
  37. if (c >= '0' && c <= '9')
  38. continue;
  39. fprintf(stderr, "invalid string %s\n", s);
  40. exit(1);
  41. }
  42. }
  43. /*
  44. A tool to request action from qfileexchgd by writing to device/qpen xenstore key.
  45. new - please attach a vfat-formatted block device at /dev/xvdg; I will either write some files to it and
  46. then request sending it to other AppVM, or I will place dvm_header+some file on it and send it to DVM
  47. send vmname - detach my /dev/xvdg and attach it to vmname at /dev/xvdh
  48. umount - I am done with my /dev/xvdh, please detach it
  49. */
  50. void usage(char *argv0)
  51. {
  52. fprintf(stderr, "usage: %s [new|umount]\n"
  53. "%s send vmname [seq]\n", argv0, argv0);
  54. exit(1);
  55. }
  56. int main(int argc, char **argv)
  57. {
  58. char buf[256];
  59. struct xs_handle *xs;
  60. xs = xs_domain_open();
  61. setuid(getuid());
  62. if (!xs) {
  63. perror("xs_domain_open");
  64. exit(1);
  65. }
  66. switch (argc) {
  67. case 2:
  68. if (!strcmp(argv[1], "umount"))
  69. strcpy(buf, "umount");
  70. else
  71. strcpy(buf, "new");
  72. break;
  73. case 3:
  74. check_name((unsigned char *) argv[2]);
  75. snprintf(buf, sizeof(buf), "send %s", argv[2]);
  76. break;
  77. case 4:
  78. check_name((unsigned char *) argv[2]);
  79. check_name((unsigned char *) argv[3]);
  80. snprintf(buf, sizeof(buf), "send %s %s", argv[2], argv[3]);
  81. default:
  82. usage(argv[0]);
  83. }
  84. if (!xs_write(xs, 0, "device/qpen", buf, strlen(buf))) {
  85. perror("xs_write");
  86. exit(1);
  87. }
  88. xs_daemon_close(xs);
  89. return 0;
  90. }