Statistics
| Branch: | Revision:

root / os-win32.c @ 8cfd0495

History | View | Annotate | Download (4.2 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 6170540b Jes Sorensen
char *os_find_datadir(const char *argv0)
88 6170540b Jes Sorensen
{
89 6170540b Jes Sorensen
    char *p;
90 6170540b Jes Sorensen
    char buf[MAX_PATH];
91 6170540b Jes Sorensen
    DWORD len;
92 6170540b Jes Sorensen
93 6170540b Jes Sorensen
    len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
94 6170540b Jes Sorensen
    if (len == 0) {
95 6170540b Jes Sorensen
        return NULL;
96 6170540b Jes Sorensen
    }
97 6170540b Jes Sorensen
98 6170540b Jes Sorensen
    buf[len] = 0;
99 6170540b Jes Sorensen
    p = buf + len - 1;
100 6170540b Jes Sorensen
    while (p != buf && *p != '\\')
101 6170540b Jes Sorensen
        p--;
102 6170540b Jes Sorensen
    *p = 0;
103 6170540b Jes Sorensen
    if (access(buf, R_OK) == 0) {
104 7267c094 Anthony Liguori
        return g_strdup(buf);
105 6170540b Jes Sorensen
    }
106 6170540b Jes Sorensen
    return NULL;
107 6170540b Jes Sorensen
}
108 59a5264b Jes Sorensen
109 6650b710 Stefan Weil
void os_set_line_buffering(void)
110 6650b710 Stefan Weil
{
111 6650b710 Stefan Weil
    setbuf(stdout, NULL);
112 6650b710 Stefan Weil
    setbuf(stderr, NULL);
113 6650b710 Stefan Weil
}
114 6650b710 Stefan Weil
115 59a5264b Jes Sorensen
/*
116 59a5264b Jes Sorensen
 * Parse OS specific command line options.
117 59a5264b Jes Sorensen
 * return 0 if option handled, -1 otherwise
118 59a5264b Jes Sorensen
 */
119 59a5264b Jes Sorensen
void os_parse_cmd_args(int index, const char *optarg)
120 59a5264b Jes Sorensen
{
121 59a5264b Jes Sorensen
    return;
122 59a5264b Jes Sorensen
}
123 eb505be1 Jes Sorensen
124 eb505be1 Jes Sorensen
void os_pidfile_error(void)
125 eb505be1 Jes Sorensen
{
126 eb505be1 Jes Sorensen
    fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
127 eb505be1 Jes Sorensen
}
128 bc4a957c Jes Sorensen
129 bc4a957c Jes Sorensen
int qemu_create_pidfile(const char *filename)
130 bc4a957c Jes Sorensen
{
131 bc4a957c Jes Sorensen
    char buffer[128];
132 bc4a957c Jes Sorensen
    int len;
133 bc4a957c Jes Sorensen
    HANDLE file;
134 bc4a957c Jes Sorensen
    OVERLAPPED overlap;
135 bc4a957c Jes Sorensen
    BOOL ret;
136 bc4a957c Jes Sorensen
    memset(&overlap, 0, sizeof(overlap));
137 bc4a957c Jes Sorensen
138 bc4a957c Jes Sorensen
    file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
139 bfc763fc Fabien Chouteau
                      OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
140 bc4a957c Jes Sorensen
141 bc4a957c Jes Sorensen
    if (file == INVALID_HANDLE_VALUE) {
142 bc4a957c Jes Sorensen
        return -1;
143 bc4a957c Jes Sorensen
    }
144 59ad3403 Stefan Weil
    len = snprintf(buffer, sizeof(buffer), "%d\n", getpid());
145 bfc763fc Fabien Chouteau
    ret = WriteFile(file, (LPCVOID)buffer, (DWORD)len,
146 bfc763fc Fabien Chouteau
                    NULL, &overlap);
147 bfc763fc Fabien Chouteau
    CloseHandle(file);
148 bc4a957c Jes Sorensen
    if (ret == 0) {
149 bc4a957c Jes Sorensen
        return -1;
150 bc4a957c Jes Sorensen
    }
151 bc4a957c Jes Sorensen
    return 0;
152 bc4a957c Jes Sorensen
}