2
0

os-win32.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * os-win32.c
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. * Copyright (c) 2010 Red Hat, Inc.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include <windows.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include <signal.h>
  29. #include <time.h>
  30. #include <errno.h>
  31. #include <sys/time.h>
  32. #include "config-host.h"
  33. #include "sysemu.h"
  34. #include "qemu-options.h"
  35. /***********************************************************/
  36. /* Functions missing in mingw */
  37. int setenv(const char *name, const char *value, int overwrite)
  38. {
  39. int result = 0;
  40. if (overwrite || !getenv(name)) {
  41. size_t length = strlen(name) + strlen(value) + 2;
  42. char *string = g_malloc(length);
  43. snprintf(string, length, "%s=%s", name, value);
  44. result = putenv(string);
  45. }
  46. return result;
  47. }
  48. static BOOL WINAPI qemu_ctrl_handler(DWORD type)
  49. {
  50. exit(STATUS_CONTROL_C_EXIT);
  51. return TRUE;
  52. }
  53. void os_setup_early_signal_handling(void)
  54. {
  55. /* Note: cpu_interrupt() is currently not SMP safe, so we force
  56. QEMU to run on a single CPU */
  57. HANDLE h;
  58. DWORD_PTR mask, smask;
  59. int i;
  60. SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
  61. h = GetCurrentProcess();
  62. if (GetProcessAffinityMask(h, &mask, &smask)) {
  63. for(i = 0; i < 32; i++) {
  64. if (mask & (1 << i))
  65. break;
  66. }
  67. if (i != 32) {
  68. mask = 1 << i;
  69. SetProcessAffinityMask(h, mask);
  70. }
  71. }
  72. }
  73. /* Look for support files in the same directory as the executable. */
  74. char *os_find_datadir(const char *argv0)
  75. {
  76. char *p;
  77. char buf[MAX_PATH];
  78. DWORD len;
  79. len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
  80. if (len == 0) {
  81. return NULL;
  82. }
  83. buf[len] = 0;
  84. p = buf + len - 1;
  85. while (p != buf && *p != '\\')
  86. p--;
  87. *p = 0;
  88. if (access(buf, R_OK) == 0) {
  89. return g_strdup(buf);
  90. }
  91. return NULL;
  92. }
  93. void os_set_line_buffering(void)
  94. {
  95. setbuf(stdout, NULL);
  96. setbuf(stderr, NULL);
  97. }
  98. /*
  99. * Parse OS specific command line options.
  100. * return 0 if option handled, -1 otherwise
  101. */
  102. void os_parse_cmd_args(int index, const char *optarg)
  103. {
  104. return;
  105. }
  106. void os_pidfile_error(void)
  107. {
  108. fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
  109. }
  110. int qemu_create_pidfile(const char *filename)
  111. {
  112. char buffer[128];
  113. int len;
  114. HANDLE file;
  115. OVERLAPPED overlap;
  116. BOOL ret;
  117. memset(&overlap, 0, sizeof(overlap));
  118. file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
  119. OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  120. if (file == INVALID_HANDLE_VALUE) {
  121. return -1;
  122. }
  123. len = snprintf(buffer, sizeof(buffer), "%d\n", getpid());
  124. ret = WriteFile(file, (LPCVOID)buffer, (DWORD)len,
  125. NULL, &overlap);
  126. CloseHandle(file);
  127. if (ret == 0) {
  128. return -1;
  129. }
  130. return 0;
  131. }
  132. int qemu_get_thread_id(void)
  133. {
  134. return GetCurrentThreadId();
  135. }