xref: /qemu/os-win32.c (revision d38ea87a)
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  */
25*d38ea87aSPeter Maydell #include "qemu/osdep.h"
2619113504SJes Sorensen #include <windows.h>
270727b867SPaolo Bonzini #include <mmsystem.h>
289c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
2959a5264bSJes Sorensen #include "qemu-options.h"
3019113504SJes Sorensen 
3119113504SJes Sorensen /***********************************************************/
320a1574bbSStefan Weil /* Functions missing in mingw */
330a1574bbSStefan Weil 
340a1574bbSStefan Weil int setenv(const char *name, const char *value, int overwrite)
350a1574bbSStefan Weil {
360a1574bbSStefan Weil     int result = 0;
370a1574bbSStefan Weil     if (overwrite || !getenv(name)) {
380a1574bbSStefan Weil         size_t length = strlen(name) + strlen(value) + 2;
397267c094SAnthony Liguori         char *string = g_malloc(length);
400a1574bbSStefan Weil         snprintf(string, length, "%s=%s", name, value);
410a1574bbSStefan Weil         result = putenv(string);
4291a9ecefSZhi Hui Li 
4391a9ecefSZhi Hui Li         /* Windows takes a copy and does not continue to use our string.
4491a9ecefSZhi Hui Li          * Therefore it can be safely freed on this platform.  POSIX code
4591a9ecefSZhi Hui Li          * typically has to leak the string because according to the spec it
4691a9ecefSZhi Hui Li          * becomes part of the environment.
4791a9ecefSZhi Hui Li          */
4891a9ecefSZhi Hui Li         g_free(string);
490a1574bbSStefan Weil     }
500a1574bbSStefan Weil     return result;
510a1574bbSStefan Weil }
520a1574bbSStefan Weil 
5369bd73b1SJes Sorensen static BOOL WINAPI qemu_ctrl_handler(DWORD type)
5469bd73b1SJes Sorensen {
55b75a0282SPavel Dovgaluk     qemu_system_shutdown_request();
56b75a0282SPavel Dovgaluk     /* Windows 7 kills application when the function returns.
57b75a0282SPavel Dovgaluk        Sleep here to give QEMU a try for closing.
58b75a0282SPavel Dovgaluk        Sleep period is 10000ms because Windows kills the program
59b75a0282SPavel Dovgaluk        after 10 seconds anyway. */
60b75a0282SPavel Dovgaluk     Sleep(10000);
61b75a0282SPavel Dovgaluk 
6269bd73b1SJes Sorensen     return TRUE;
6369bd73b1SJes Sorensen }
6469bd73b1SJes Sorensen 
650727b867SPaolo Bonzini static TIMECAPS mm_tc;
660727b867SPaolo Bonzini 
670727b867SPaolo Bonzini static void os_undo_timer_resolution(void)
680727b867SPaolo Bonzini {
690727b867SPaolo Bonzini     timeEndPeriod(mm_tc.wPeriodMin);
700727b867SPaolo Bonzini }
710727b867SPaolo Bonzini 
72fe98ac14SJes Sorensen void os_setup_early_signal_handling(void)
7369bd73b1SJes Sorensen {
7469bd73b1SJes Sorensen     SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
750727b867SPaolo Bonzini     timeGetDevCaps(&mm_tc, sizeof(mm_tc));
760727b867SPaolo Bonzini     timeBeginPeriod(mm_tc.wPeriodMin);
770727b867SPaolo Bonzini     atexit(os_undo_timer_resolution);
7869bd73b1SJes Sorensen }
796170540bSJes Sorensen 
806170540bSJes Sorensen /* Look for support files in the same directory as the executable.  */
8110f5bff6SFam Zheng char *os_find_datadir(void)
826170540bSJes Sorensen {
8310f5bff6SFam Zheng     return qemu_get_exec_dir();
846170540bSJes Sorensen }
8559a5264bSJes Sorensen 
866650b710SStefan Weil void os_set_line_buffering(void)
876650b710SStefan Weil {
886650b710SStefan Weil     setbuf(stdout, NULL);
896650b710SStefan Weil     setbuf(stderr, NULL);
906650b710SStefan Weil }
916650b710SStefan Weil 
9259a5264bSJes Sorensen /*
9359a5264bSJes Sorensen  * Parse OS specific command line options.
9459a5264bSJes Sorensen  * return 0 if option handled, -1 otherwise
9559a5264bSJes Sorensen  */
9659a5264bSJes Sorensen void os_parse_cmd_args(int index, const char *optarg)
9759a5264bSJes Sorensen {
9859a5264bSJes Sorensen     return;
9959a5264bSJes Sorensen }
100eb505be1SJes Sorensen 
101bc4a957cSJes Sorensen int qemu_create_pidfile(const char *filename)
102bc4a957cSJes Sorensen {
103bc4a957cSJes Sorensen     char buffer[128];
104bc4a957cSJes Sorensen     int len;
105bc4a957cSJes Sorensen     HANDLE file;
106bc4a957cSJes Sorensen     OVERLAPPED overlap;
107bc4a957cSJes Sorensen     BOOL ret;
108bc4a957cSJes Sorensen     memset(&overlap, 0, sizeof(overlap));
109bc4a957cSJes Sorensen 
110bc4a957cSJes Sorensen     file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
111bc4a957cSJes Sorensen                       OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
112bc4a957cSJes Sorensen 
113bc4a957cSJes Sorensen     if (file == INVALID_HANDLE_VALUE) {
114bc4a957cSJes Sorensen         return -1;
115bc4a957cSJes Sorensen     }
11659ad3403SStefan Weil     len = snprintf(buffer, sizeof(buffer), "%d\n", getpid());
117bfc763fcSFabien Chouteau     ret = WriteFile(file, (LPCVOID)buffer, (DWORD)len,
118bfc763fcSFabien Chouteau                     NULL, &overlap);
119bfc763fcSFabien Chouteau     CloseHandle(file);
120bc4a957cSJes Sorensen     if (ret == 0) {
121bc4a957cSJes Sorensen         return -1;
122bc4a957cSJes Sorensen     }
123bc4a957cSJes Sorensen     return 0;
124bc4a957cSJes Sorensen }
125