Statistics
| Branch: | Revision:

root / os-win32.c @ feature-archipelago

History | View | Annotate | Download (3.8 kB)

1 19113504 Jes Sorensen
/*
2 19113504 Jes Sorensen
 * os-win32.c
3 19113504 Jes Sorensen
 *
4 19113504 Jes Sorensen
 * Copyright (c) 2003-2008 Fabrice Bellard
5 19113504 Jes Sorensen
 * Copyright (c) 2010 Red Hat, Inc.
6 19113504 Jes Sorensen
 *
7 19113504 Jes Sorensen
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 19113504 Jes Sorensen
 * of this software and associated documentation files (the "Software"), to deal
9 19113504 Jes Sorensen
 * in the Software without restriction, including without limitation the rights
10 19113504 Jes Sorensen
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 19113504 Jes Sorensen
 * copies of the Software, and to permit persons to whom the Software is
12 19113504 Jes Sorensen
 * furnished to do so, subject to the following conditions:
13 19113504 Jes Sorensen
 *
14 19113504 Jes Sorensen
 * The above copyright notice and this permission notice shall be included in
15 19113504 Jes Sorensen
 * all copies or substantial portions of the Software.
16 19113504 Jes Sorensen
 *
17 19113504 Jes Sorensen
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 19113504 Jes Sorensen
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 19113504 Jes Sorensen
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 19113504 Jes Sorensen
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 19113504 Jes Sorensen
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 19113504 Jes Sorensen
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 19113504 Jes Sorensen
 * THE SOFTWARE.
24 19113504 Jes Sorensen
 */
25 19113504 Jes Sorensen
#include <windows.h>
26 0727b867 Paolo Bonzini
#include <mmsystem.h>
27 19113504 Jes Sorensen
#include <unistd.h>
28 19113504 Jes Sorensen
#include <fcntl.h>
29 19113504 Jes Sorensen
#include <signal.h>
30 19113504 Jes Sorensen
#include <time.h>
31 19113504 Jes Sorensen
#include <errno.h>
32 19113504 Jes Sorensen
#include <sys/time.h>
33 19113504 Jes Sorensen
#include "config-host.h"
34 9c17d615 Paolo Bonzini
#include "sysemu/sysemu.h"
35 59a5264b Jes Sorensen
#include "qemu-options.h"
36 19113504 Jes Sorensen
37 19113504 Jes Sorensen
/***********************************************************/
38 0a1574bb Stefan Weil
/* Functions missing in mingw */
39 0a1574bb Stefan Weil
40 0a1574bb Stefan Weil
int setenv(const char *name, const char *value, int overwrite)
41 0a1574bb Stefan Weil
{
42 0a1574bb Stefan Weil
    int result = 0;
43 0a1574bb Stefan Weil
    if (overwrite || !getenv(name)) {
44 0a1574bb Stefan Weil
        size_t length = strlen(name) + strlen(value) + 2;
45 7267c094 Anthony Liguori
        char *string = g_malloc(length);
46 0a1574bb Stefan Weil
        snprintf(string, length, "%s=%s", name, value);
47 0a1574bb Stefan Weil
        result = putenv(string);
48 91a9ecef Zhi Hui Li
49 91a9ecef Zhi Hui Li
        /* Windows takes a copy and does not continue to use our string.
50 91a9ecef Zhi Hui Li
         * Therefore it can be safely freed on this platform.  POSIX code
51 91a9ecef Zhi Hui Li
         * typically has to leak the string because according to the spec it
52 91a9ecef Zhi Hui Li
         * becomes part of the environment.
53 91a9ecef Zhi Hui Li
         */
54 91a9ecef Zhi Hui Li
        g_free(string);
55 0a1574bb Stefan Weil
    }
56 0a1574bb Stefan Weil
    return result;
57 0a1574bb Stefan Weil
}
58 0a1574bb Stefan Weil
59 69bd73b1 Jes Sorensen
static BOOL WINAPI qemu_ctrl_handler(DWORD type)
60 69bd73b1 Jes Sorensen
{
61 b75a0282 Pavel Dovgaluk
    qemu_system_shutdown_request();
62 b75a0282 Pavel Dovgaluk
    /* Windows 7 kills application when the function returns.
63 b75a0282 Pavel Dovgaluk
       Sleep here to give QEMU a try for closing.
64 b75a0282 Pavel Dovgaluk
       Sleep period is 10000ms because Windows kills the program
65 b75a0282 Pavel Dovgaluk
       after 10 seconds anyway. */
66 b75a0282 Pavel Dovgaluk
    Sleep(10000);
67 b75a0282 Pavel Dovgaluk
68 69bd73b1 Jes Sorensen
    return TRUE;
69 69bd73b1 Jes Sorensen
}
70 69bd73b1 Jes Sorensen
71 0727b867 Paolo Bonzini
static TIMECAPS mm_tc;
72 0727b867 Paolo Bonzini
73 0727b867 Paolo Bonzini
static void os_undo_timer_resolution(void)
74 0727b867 Paolo Bonzini
{
75 0727b867 Paolo Bonzini
    timeEndPeriod(mm_tc.wPeriodMin);
76 0727b867 Paolo Bonzini
}
77 0727b867 Paolo Bonzini
78 fe98ac14 Jes Sorensen
void os_setup_early_signal_handling(void)
79 69bd73b1 Jes Sorensen
{
80 69bd73b1 Jes Sorensen
    SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
81 0727b867 Paolo Bonzini
    timeGetDevCaps(&mm_tc, sizeof(mm_tc));
82 0727b867 Paolo Bonzini
    timeBeginPeriod(mm_tc.wPeriodMin);
83 0727b867 Paolo Bonzini
    atexit(os_undo_timer_resolution);
84 69bd73b1 Jes Sorensen
}
85 6170540b Jes Sorensen
86 6170540b Jes Sorensen
/* Look for support files in the same directory as the executable.  */
87 10f5bff6 Fam Zheng
char *os_find_datadir(void)
88 6170540b Jes Sorensen
{
89 10f5bff6 Fam Zheng
    return qemu_get_exec_dir();
90 6170540b Jes Sorensen
}
91 59a5264b Jes Sorensen
92 6650b710 Stefan Weil
void os_set_line_buffering(void)
93 6650b710 Stefan Weil
{
94 6650b710 Stefan Weil
    setbuf(stdout, NULL);
95 6650b710 Stefan Weil
    setbuf(stderr, NULL);
96 6650b710 Stefan Weil
}
97 6650b710 Stefan Weil
98 59a5264b Jes Sorensen
/*
99 59a5264b Jes Sorensen
 * Parse OS specific command line options.
100 59a5264b Jes Sorensen
 * return 0 if option handled, -1 otherwise
101 59a5264b Jes Sorensen
 */
102 59a5264b Jes Sorensen
void os_parse_cmd_args(int index, const char *optarg)
103 59a5264b Jes Sorensen
{
104 59a5264b Jes Sorensen
    return;
105 59a5264b Jes Sorensen
}
106 eb505be1 Jes Sorensen
107 eb505be1 Jes Sorensen
void os_pidfile_error(void)
108 eb505be1 Jes Sorensen
{
109 eb505be1 Jes Sorensen
    fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
110 eb505be1 Jes Sorensen
}
111 bc4a957c Jes Sorensen
112 bc4a957c Jes Sorensen
int qemu_create_pidfile(const char *filename)
113 bc4a957c Jes Sorensen
{
114 bc4a957c Jes Sorensen
    char buffer[128];
115 bc4a957c Jes Sorensen
    int len;
116 bc4a957c Jes Sorensen
    HANDLE file;
117 bc4a957c Jes Sorensen
    OVERLAPPED overlap;
118 bc4a957c Jes Sorensen
    BOOL ret;
119 bc4a957c Jes Sorensen
    memset(&overlap, 0, sizeof(overlap));
120 bc4a957c Jes Sorensen
121 bc4a957c Jes Sorensen
    file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
122 bfc763fc Fabien Chouteau
                      OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
123 bc4a957c Jes Sorensen
124 bc4a957c Jes Sorensen
    if (file == INVALID_HANDLE_VALUE) {
125 bc4a957c Jes Sorensen
        return -1;
126 bc4a957c Jes Sorensen
    }
127 59ad3403 Stefan Weil
    len = snprintf(buffer, sizeof(buffer), "%d\n", getpid());
128 bfc763fc Fabien Chouteau
    ret = WriteFile(file, (LPCVOID)buffer, (DWORD)len,
129 bfc763fc Fabien Chouteau
                    NULL, &overlap);
130 bfc763fc Fabien Chouteau
    CloseHandle(file);
131 bc4a957c Jes Sorensen
    if (ret == 0) {
132 bc4a957c Jes Sorensen
        return -1;
133 bc4a957c Jes Sorensen
    }
134 bc4a957c Jes Sorensen
    return 0;
135 bc4a957c Jes Sorensen
}