xref: /qemu/os-win32.c (revision 0727b867)
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>
26*0727b867SPaolo Bonzini #include <mmsystem.h>
2719113504SJes Sorensen #include <unistd.h>
2819113504SJes Sorensen #include <fcntl.h>
2919113504SJes Sorensen #include <signal.h>
3019113504SJes Sorensen #include <time.h>
3119113504SJes Sorensen #include <errno.h>
3219113504SJes Sorensen #include <sys/time.h>
3319113504SJes Sorensen #include "config-host.h"
349c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
3559a5264bSJes Sorensen #include "qemu-options.h"
3619113504SJes Sorensen 
3719113504SJes Sorensen /***********************************************************/
380a1574bbSStefan Weil /* Functions missing in mingw */
390a1574bbSStefan Weil 
400a1574bbSStefan Weil int setenv(const char *name, const char *value, int overwrite)
410a1574bbSStefan Weil {
420a1574bbSStefan Weil     int result = 0;
430a1574bbSStefan Weil     if (overwrite || !getenv(name)) {
440a1574bbSStefan Weil         size_t length = strlen(name) + strlen(value) + 2;
457267c094SAnthony Liguori         char *string = g_malloc(length);
460a1574bbSStefan Weil         snprintf(string, length, "%s=%s", name, value);
470a1574bbSStefan Weil         result = putenv(string);
4891a9ecefSZhi Hui Li 
4991a9ecefSZhi Hui Li         /* Windows takes a copy and does not continue to use our string.
5091a9ecefSZhi Hui Li          * Therefore it can be safely freed on this platform.  POSIX code
5191a9ecefSZhi Hui Li          * typically has to leak the string because according to the spec it
5291a9ecefSZhi Hui Li          * becomes part of the environment.
5391a9ecefSZhi Hui Li          */
5491a9ecefSZhi Hui Li         g_free(string);
550a1574bbSStefan Weil     }
560a1574bbSStefan Weil     return result;
570a1574bbSStefan Weil }
580a1574bbSStefan Weil 
5969bd73b1SJes Sorensen static BOOL WINAPI qemu_ctrl_handler(DWORD type)
6069bd73b1SJes Sorensen {
61b75a0282SPavel Dovgaluk     qemu_system_shutdown_request();
62b75a0282SPavel Dovgaluk     /* Windows 7 kills application when the function returns.
63b75a0282SPavel Dovgaluk        Sleep here to give QEMU a try for closing.
64b75a0282SPavel Dovgaluk        Sleep period is 10000ms because Windows kills the program
65b75a0282SPavel Dovgaluk        after 10 seconds anyway. */
66b75a0282SPavel Dovgaluk     Sleep(10000);
67b75a0282SPavel Dovgaluk 
6869bd73b1SJes Sorensen     return TRUE;
6969bd73b1SJes Sorensen }
7069bd73b1SJes Sorensen 
71*0727b867SPaolo Bonzini static TIMECAPS mm_tc;
72*0727b867SPaolo Bonzini 
73*0727b867SPaolo Bonzini static void os_undo_timer_resolution(void)
74*0727b867SPaolo Bonzini {
75*0727b867SPaolo Bonzini     timeEndPeriod(mm_tc.wPeriodMin);
76*0727b867SPaolo Bonzini }
77*0727b867SPaolo Bonzini 
78fe98ac14SJes Sorensen void os_setup_early_signal_handling(void)
7969bd73b1SJes Sorensen {
8069bd73b1SJes Sorensen     SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
81*0727b867SPaolo Bonzini     timeGetDevCaps(&mm_tc, sizeof(mm_tc));
82*0727b867SPaolo Bonzini     timeBeginPeriod(mm_tc.wPeriodMin);
83*0727b867SPaolo Bonzini     atexit(os_undo_timer_resolution);
8469bd73b1SJes Sorensen }
856170540bSJes Sorensen 
866170540bSJes Sorensen /* Look for support files in the same directory as the executable.  */
876170540bSJes Sorensen char *os_find_datadir(const char *argv0)
886170540bSJes Sorensen {
896170540bSJes Sorensen     char *p;
906170540bSJes Sorensen     char buf[MAX_PATH];
916170540bSJes Sorensen     DWORD len;
926170540bSJes Sorensen 
936170540bSJes Sorensen     len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
946170540bSJes Sorensen     if (len == 0) {
956170540bSJes Sorensen         return NULL;
966170540bSJes Sorensen     }
976170540bSJes Sorensen 
986170540bSJes Sorensen     buf[len] = 0;
996170540bSJes Sorensen     p = buf + len - 1;
1006170540bSJes Sorensen     while (p != buf && *p != '\\')
1016170540bSJes Sorensen         p--;
1026170540bSJes Sorensen     *p = 0;
1036170540bSJes Sorensen     if (access(buf, R_OK) == 0) {
1047267c094SAnthony Liguori         return g_strdup(buf);
1056170540bSJes Sorensen     }
1066170540bSJes Sorensen     return NULL;
1076170540bSJes Sorensen }
10859a5264bSJes Sorensen 
1096650b710SStefan Weil void os_set_line_buffering(void)
1106650b710SStefan Weil {
1116650b710SStefan Weil     setbuf(stdout, NULL);
1126650b710SStefan Weil     setbuf(stderr, NULL);
1136650b710SStefan Weil }
1146650b710SStefan Weil 
11559a5264bSJes Sorensen /*
11659a5264bSJes Sorensen  * Parse OS specific command line options.
11759a5264bSJes Sorensen  * return 0 if option handled, -1 otherwise
11859a5264bSJes Sorensen  */
11959a5264bSJes Sorensen void os_parse_cmd_args(int index, const char *optarg)
12059a5264bSJes Sorensen {
12159a5264bSJes Sorensen     return;
12259a5264bSJes Sorensen }
123eb505be1SJes Sorensen 
124eb505be1SJes Sorensen void os_pidfile_error(void)
125eb505be1SJes Sorensen {
126eb505be1SJes Sorensen     fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
127eb505be1SJes Sorensen }
128bc4a957cSJes Sorensen 
129bc4a957cSJes Sorensen int qemu_create_pidfile(const char *filename)
130bc4a957cSJes Sorensen {
131bc4a957cSJes Sorensen     char buffer[128];
132bc4a957cSJes Sorensen     int len;
133bc4a957cSJes Sorensen     HANDLE file;
134bc4a957cSJes Sorensen     OVERLAPPED overlap;
135bc4a957cSJes Sorensen     BOOL ret;
136bc4a957cSJes Sorensen     memset(&overlap, 0, sizeof(overlap));
137bc4a957cSJes Sorensen 
138bc4a957cSJes Sorensen     file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
139bc4a957cSJes Sorensen                       OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
140bc4a957cSJes Sorensen 
141bc4a957cSJes Sorensen     if (file == INVALID_HANDLE_VALUE) {
142bc4a957cSJes Sorensen         return -1;
143bc4a957cSJes Sorensen     }
14459ad3403SStefan Weil     len = snprintf(buffer, sizeof(buffer), "%d\n", getpid());
145bfc763fcSFabien Chouteau     ret = WriteFile(file, (LPCVOID)buffer, (DWORD)len,
146bfc763fcSFabien Chouteau                     NULL, &overlap);
147bfc763fcSFabien Chouteau     CloseHandle(file);
148bc4a957cSJes Sorensen     if (ret == 0) {
149bc4a957cSJes Sorensen         return -1;
150bc4a957cSJes Sorensen     }
151bc4a957cSJes Sorensen     return 0;
152bc4a957cSJes Sorensen }
153