char-file.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qapi/error.h"
  26. #include "qemu/module.h"
  27. #include "qemu/option.h"
  28. #include "chardev/char.h"
  29. #ifdef _WIN32
  30. #include "chardev/char-win.h"
  31. #else
  32. #include "chardev/char-fd.h"
  33. #endif
  34. static void qmp_chardev_open_file(Chardev *chr,
  35. ChardevBackend *backend,
  36. bool *be_opened,
  37. Error **errp)
  38. {
  39. ChardevFile *file = backend->u.file.data;
  40. #ifdef _WIN32
  41. HANDLE out;
  42. DWORD accessmode;
  43. DWORD flags;
  44. if (file->in) {
  45. error_setg(errp, "input file not supported");
  46. return;
  47. }
  48. if (file->has_append && file->append) {
  49. /* Append to file if it already exists. */
  50. accessmode = FILE_GENERIC_WRITE & ~FILE_WRITE_DATA;
  51. flags = OPEN_ALWAYS;
  52. } else {
  53. /* Truncate file if it already exists. */
  54. accessmode = GENERIC_WRITE;
  55. flags = CREATE_ALWAYS;
  56. }
  57. out = CreateFile(file->out, accessmode, FILE_SHARE_READ, NULL, flags,
  58. FILE_ATTRIBUTE_NORMAL, NULL);
  59. if (out == INVALID_HANDLE_VALUE) {
  60. error_setg(errp, "open %s failed", file->out);
  61. return;
  62. }
  63. win_chr_set_file(chr, out, false);
  64. #else
  65. int flags, in = -1, out;
  66. flags = O_WRONLY | O_CREAT | O_BINARY;
  67. if (file->has_append && file->append) {
  68. flags |= O_APPEND;
  69. } else {
  70. flags |= O_TRUNC;
  71. }
  72. out = qmp_chardev_open_file_source(file->out, flags, errp);
  73. if (out < 0) {
  74. return;
  75. }
  76. if (file->in) {
  77. flags = O_RDONLY;
  78. in = qmp_chardev_open_file_source(file->in, flags, errp);
  79. if (in < 0) {
  80. qemu_close(out);
  81. return;
  82. }
  83. }
  84. qemu_chr_open_fd(chr, in, out);
  85. #endif
  86. }
  87. static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
  88. Error **errp)
  89. {
  90. const char *path = qemu_opt_get(opts, "path");
  91. ChardevFile *file;
  92. backend->type = CHARDEV_BACKEND_KIND_FILE;
  93. if (path == NULL) {
  94. error_setg(errp, "chardev: file: no filename given");
  95. return;
  96. }
  97. file = backend->u.file.data = g_new0(ChardevFile, 1);
  98. qemu_chr_parse_common(opts, qapi_ChardevFile_base(file));
  99. file->out = g_strdup(path);
  100. file->has_append = true;
  101. file->append = qemu_opt_get_bool(opts, "append", false);
  102. }
  103. static void char_file_class_init(ObjectClass *oc, void *data)
  104. {
  105. ChardevClass *cc = CHARDEV_CLASS(oc);
  106. cc->parse = qemu_chr_parse_file_out;
  107. cc->open = qmp_chardev_open_file;
  108. }
  109. static const TypeInfo char_file_type_info = {
  110. .name = TYPE_CHARDEV_FILE,
  111. #ifdef _WIN32
  112. .parent = TYPE_CHARDEV_WIN,
  113. #else
  114. .parent = TYPE_CHARDEV_FD,
  115. #endif
  116. .class_init = char_file_class_init,
  117. };
  118. static void register_types(void)
  119. {
  120. type_register_static(&char_file_type_info);
  121. }
  122. type_init(register_types);