119113504SJes Sorensen /* 219113504SJes Sorensen * os-win32.c 319113504SJes Sorensen * 419113504SJes Sorensen * Copyright (c) 2003-2008 Fabrice Bellard 519113504SJes Sorensen * Copyright (c) 2010 Red Hat, Inc. 619113504SJes Sorensen * 719113504SJes Sorensen * Permission is hereby granted, free of charge, to any person obtaining a copy 819113504SJes Sorensen * of this software and associated documentation files (the "Software"), to deal 919113504SJes Sorensen * in the Software without restriction, including without limitation the rights 1019113504SJes Sorensen * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 1119113504SJes Sorensen * copies of the Software, and to permit persons to whom the Software is 1219113504SJes Sorensen * furnished to do so, subject to the following conditions: 1319113504SJes Sorensen * 1419113504SJes Sorensen * The above copyright notice and this permission notice shall be included in 1519113504SJes Sorensen * all copies or substantial portions of the Software. 1619113504SJes Sorensen * 1719113504SJes Sorensen * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1819113504SJes Sorensen * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1919113504SJes Sorensen * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 2019113504SJes Sorensen * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 2119113504SJes Sorensen * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 2219113504SJes Sorensen * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 2319113504SJes Sorensen * THE SOFTWARE. 2419113504SJes Sorensen */ 2519113504SJes Sorensen #include <windows.h> 2619113504SJes Sorensen #include <unistd.h> 2719113504SJes Sorensen #include <fcntl.h> 2819113504SJes Sorensen #include <signal.h> 2919113504SJes Sorensen #include <time.h> 3019113504SJes Sorensen #include <errno.h> 3119113504SJes Sorensen #include <sys/time.h> 3219113504SJes Sorensen #include "config-host.h" 3319113504SJes Sorensen #include "sysemu.h" 3459a5264bSJes Sorensen #include "qemu-options.h" 3519113504SJes Sorensen 3619113504SJes Sorensen /***********************************************************/ 370a1574bbSStefan Weil /* Functions missing in mingw */ 380a1574bbSStefan Weil 390a1574bbSStefan Weil int setenv(const char *name, const char *value, int overwrite) 400a1574bbSStefan Weil { 410a1574bbSStefan Weil int result = 0; 420a1574bbSStefan Weil if (overwrite || !getenv(name)) { 430a1574bbSStefan Weil size_t length = strlen(name) + strlen(value) + 2; 447267c094SAnthony Liguori char *string = g_malloc(length); 450a1574bbSStefan Weil snprintf(string, length, "%s=%s", name, value); 460a1574bbSStefan Weil result = putenv(string); 4791a9ecefSZhi Hui Li 4891a9ecefSZhi Hui Li /* Windows takes a copy and does not continue to use our string. 4991a9ecefSZhi Hui Li * Therefore it can be safely freed on this platform. POSIX code 5091a9ecefSZhi Hui Li * typically has to leak the string because according to the spec it 5191a9ecefSZhi Hui Li * becomes part of the environment. 5291a9ecefSZhi Hui Li */ 5391a9ecefSZhi Hui Li g_free(string); 540a1574bbSStefan Weil } 550a1574bbSStefan Weil return result; 560a1574bbSStefan Weil } 570a1574bbSStefan Weil 5869bd73b1SJes Sorensen static BOOL WINAPI qemu_ctrl_handler(DWORD type) 5969bd73b1SJes Sorensen { 60*b75a0282SPavel Dovgaluk qemu_system_shutdown_request(); 61*b75a0282SPavel Dovgaluk /* Windows 7 kills application when the function returns. 62*b75a0282SPavel Dovgaluk Sleep here to give QEMU a try for closing. 63*b75a0282SPavel Dovgaluk Sleep period is 10000ms because Windows kills the program 64*b75a0282SPavel Dovgaluk after 10 seconds anyway. */ 65*b75a0282SPavel Dovgaluk Sleep(10000); 66*b75a0282SPavel Dovgaluk 6769bd73b1SJes Sorensen return TRUE; 6869bd73b1SJes Sorensen } 6969bd73b1SJes Sorensen 70fe98ac14SJes Sorensen void os_setup_early_signal_handling(void) 7169bd73b1SJes Sorensen { 7269bd73b1SJes Sorensen /* Note: cpu_interrupt() is currently not SMP safe, so we force 7369bd73b1SJes Sorensen QEMU to run on a single CPU */ 7469bd73b1SJes Sorensen HANDLE h; 75f45a1108SStefan Weil DWORD_PTR mask, smask; 7669bd73b1SJes Sorensen int i; 7769bd73b1SJes Sorensen 7869bd73b1SJes Sorensen SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); 7969bd73b1SJes Sorensen 8069bd73b1SJes Sorensen h = GetCurrentProcess(); 8169bd73b1SJes Sorensen if (GetProcessAffinityMask(h, &mask, &smask)) { 8269bd73b1SJes Sorensen for(i = 0; i < 32; i++) { 8369bd73b1SJes Sorensen if (mask & (1 << i)) 8469bd73b1SJes Sorensen break; 8569bd73b1SJes Sorensen } 8669bd73b1SJes Sorensen if (i != 32) { 8769bd73b1SJes Sorensen mask = 1 << i; 8869bd73b1SJes Sorensen SetProcessAffinityMask(h, mask); 8969bd73b1SJes Sorensen } 9069bd73b1SJes Sorensen } 9169bd73b1SJes Sorensen } 926170540bSJes Sorensen 936170540bSJes Sorensen /* Look for support files in the same directory as the executable. */ 946170540bSJes Sorensen char *os_find_datadir(const char *argv0) 956170540bSJes Sorensen { 966170540bSJes Sorensen char *p; 976170540bSJes Sorensen char buf[MAX_PATH]; 986170540bSJes Sorensen DWORD len; 996170540bSJes Sorensen 1006170540bSJes Sorensen len = GetModuleFileName(NULL, buf, sizeof(buf) - 1); 1016170540bSJes Sorensen if (len == 0) { 1026170540bSJes Sorensen return NULL; 1036170540bSJes Sorensen } 1046170540bSJes Sorensen 1056170540bSJes Sorensen buf[len] = 0; 1066170540bSJes Sorensen p = buf + len - 1; 1076170540bSJes Sorensen while (p != buf && *p != '\\') 1086170540bSJes Sorensen p--; 1096170540bSJes Sorensen *p = 0; 1106170540bSJes Sorensen if (access(buf, R_OK) == 0) { 1117267c094SAnthony Liguori return g_strdup(buf); 1126170540bSJes Sorensen } 1136170540bSJes Sorensen return NULL; 1146170540bSJes Sorensen } 11559a5264bSJes Sorensen 1166650b710SStefan Weil void os_set_line_buffering(void) 1176650b710SStefan Weil { 1186650b710SStefan Weil setbuf(stdout, NULL); 1196650b710SStefan Weil setbuf(stderr, NULL); 1206650b710SStefan Weil } 1216650b710SStefan Weil 12259a5264bSJes Sorensen /* 12359a5264bSJes Sorensen * Parse OS specific command line options. 12459a5264bSJes Sorensen * return 0 if option handled, -1 otherwise 12559a5264bSJes Sorensen */ 12659a5264bSJes Sorensen void os_parse_cmd_args(int index, const char *optarg) 12759a5264bSJes Sorensen { 12859a5264bSJes Sorensen return; 12959a5264bSJes Sorensen } 130eb505be1SJes Sorensen 131eb505be1SJes Sorensen void os_pidfile_error(void) 132eb505be1SJes Sorensen { 133eb505be1SJes Sorensen fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno)); 134eb505be1SJes Sorensen } 135bc4a957cSJes Sorensen 136bc4a957cSJes Sorensen int qemu_create_pidfile(const char *filename) 137bc4a957cSJes Sorensen { 138bc4a957cSJes Sorensen char buffer[128]; 139bc4a957cSJes Sorensen int len; 140bc4a957cSJes Sorensen HANDLE file; 141bc4a957cSJes Sorensen OVERLAPPED overlap; 142bc4a957cSJes Sorensen BOOL ret; 143bc4a957cSJes Sorensen memset(&overlap, 0, sizeof(overlap)); 144bc4a957cSJes Sorensen 145bc4a957cSJes Sorensen file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL, 146bc4a957cSJes Sorensen OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 147bc4a957cSJes Sorensen 148bc4a957cSJes Sorensen if (file == INVALID_HANDLE_VALUE) { 149bc4a957cSJes Sorensen return -1; 150bc4a957cSJes Sorensen } 15159ad3403SStefan Weil len = snprintf(buffer, sizeof(buffer), "%d\n", getpid()); 152bfc763fcSFabien Chouteau ret = WriteFile(file, (LPCVOID)buffer, (DWORD)len, 153bfc763fcSFabien Chouteau NULL, &overlap); 154bfc763fcSFabien Chouteau CloseHandle(file); 155bc4a957cSJes Sorensen if (ret == 0) { 156bc4a957cSJes Sorensen return -1; 157bc4a957cSJes Sorensen } 158bc4a957cSJes Sorensen return 0; 159bc4a957cSJes Sorensen } 160