Statistics
| Branch: | Revision:

root / os-win32.c @ 76b523db

History | View | Annotate | Download (4.4 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 19113504 Jes Sorensen
#include <unistd.h>
27 19113504 Jes Sorensen
#include <fcntl.h>
28 19113504 Jes Sorensen
#include <signal.h>
29 19113504 Jes Sorensen
#include <time.h>
30 19113504 Jes Sorensen
#include <errno.h>
31 19113504 Jes Sorensen
#include <sys/time.h>
32 19113504 Jes Sorensen
#include "config-host.h"
33 19113504 Jes Sorensen
#include "sysemu.h"
34 59a5264b Jes Sorensen
#include "qemu-options.h"
35 19113504 Jes Sorensen
36 19113504 Jes Sorensen
/***********************************************************/
37 0a1574bb Stefan Weil
/* Functions missing in mingw */
38 0a1574bb Stefan Weil
39 0a1574bb Stefan Weil
int setenv(const char *name, const char *value, int overwrite)
40 0a1574bb Stefan Weil
{
41 0a1574bb Stefan Weil
    int result = 0;
42 0a1574bb Stefan Weil
    if (overwrite || !getenv(name)) {
43 0a1574bb Stefan Weil
        size_t length = strlen(name) + strlen(value) + 2;
44 7267c094 Anthony Liguori
        char *string = g_malloc(length);
45 0a1574bb Stefan Weil
        snprintf(string, length, "%s=%s", name, value);
46 0a1574bb Stefan Weil
        result = putenv(string);
47 91a9ecef Zhi Hui Li
48 91a9ecef Zhi Hui Li
        /* Windows takes a copy and does not continue to use our string.
49 91a9ecef Zhi Hui Li
         * Therefore it can be safely freed on this platform.  POSIX code
50 91a9ecef Zhi Hui Li
         * typically has to leak the string because according to the spec it
51 91a9ecef Zhi Hui Li
         * becomes part of the environment.
52 91a9ecef Zhi Hui Li
         */
53 91a9ecef Zhi Hui Li
        g_free(string);
54 0a1574bb Stefan Weil
    }
55 0a1574bb Stefan Weil
    return result;
56 0a1574bb Stefan Weil
}
57 0a1574bb Stefan Weil
58 69bd73b1 Jes Sorensen
static BOOL WINAPI qemu_ctrl_handler(DWORD type)
59 69bd73b1 Jes Sorensen
{
60 b75a0282 Pavel Dovgaluk
    qemu_system_shutdown_request();
61 b75a0282 Pavel Dovgaluk
    /* Windows 7 kills application when the function returns.
62 b75a0282 Pavel Dovgaluk
       Sleep here to give QEMU a try for closing.
63 b75a0282 Pavel Dovgaluk
       Sleep period is 10000ms because Windows kills the program
64 b75a0282 Pavel Dovgaluk
       after 10 seconds anyway. */
65 b75a0282 Pavel Dovgaluk
    Sleep(10000);
66 b75a0282 Pavel Dovgaluk
67 69bd73b1 Jes Sorensen
    return TRUE;
68 69bd73b1 Jes Sorensen
}
69 69bd73b1 Jes Sorensen
70 fe98ac14 Jes Sorensen
void os_setup_early_signal_handling(void)
71 69bd73b1 Jes Sorensen
{
72 69bd73b1 Jes Sorensen
    /* Note: cpu_interrupt() is currently not SMP safe, so we force
73 69bd73b1 Jes Sorensen
       QEMU to run on a single CPU */
74 69bd73b1 Jes Sorensen
    HANDLE h;
75 f45a1108 Stefan Weil
    DWORD_PTR mask, smask;
76 69bd73b1 Jes Sorensen
    int i;
77 69bd73b1 Jes Sorensen
78 69bd73b1 Jes Sorensen
    SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
79 69bd73b1 Jes Sorensen
80 69bd73b1 Jes Sorensen
    h = GetCurrentProcess();
81 69bd73b1 Jes Sorensen
    if (GetProcessAffinityMask(h, &mask, &smask)) {
82 69bd73b1 Jes Sorensen
        for(i = 0; i < 32; i++) {
83 69bd73b1 Jes Sorensen
            if (mask & (1 << i))
84 69bd73b1 Jes Sorensen
                break;
85 69bd73b1 Jes Sorensen
        }
86 69bd73b1 Jes Sorensen
        if (i != 32) {
87 69bd73b1 Jes Sorensen
            mask = 1 << i;
88 69bd73b1 Jes Sorensen
            SetProcessAffinityMask(h, mask);
89 69bd73b1 Jes Sorensen
        }
90 69bd73b1 Jes Sorensen
    }
91 69bd73b1 Jes Sorensen
}
92 6170540b Jes Sorensen
93 6170540b Jes Sorensen
/* Look for support files in the same directory as the executable.  */
94 6170540b Jes Sorensen
char *os_find_datadir(const char *argv0)
95 6170540b Jes Sorensen
{
96 6170540b Jes Sorensen
    char *p;
97 6170540b Jes Sorensen
    char buf[MAX_PATH];
98 6170540b Jes Sorensen
    DWORD len;
99 6170540b Jes Sorensen
100 6170540b Jes Sorensen
    len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
101 6170540b Jes Sorensen
    if (len == 0) {
102 6170540b Jes Sorensen
        return NULL;
103 6170540b Jes Sorensen
    }
104 6170540b Jes Sorensen
105 6170540b Jes Sorensen
    buf[len] = 0;
106 6170540b Jes Sorensen
    p = buf + len - 1;
107 6170540b Jes Sorensen
    while (p != buf && *p != '\\')
108 6170540b Jes Sorensen
        p--;
109 6170540b Jes Sorensen
    *p = 0;
110 6170540b Jes Sorensen
    if (access(buf, R_OK) == 0) {
111 7267c094 Anthony Liguori
        return g_strdup(buf);
112 6170540b Jes Sorensen
    }
113 6170540b Jes Sorensen
    return NULL;
114 6170540b Jes Sorensen
}
115 59a5264b Jes Sorensen
116 6650b710 Stefan Weil
void os_set_line_buffering(void)
117 6650b710 Stefan Weil
{
118 6650b710 Stefan Weil
    setbuf(stdout, NULL);
119 6650b710 Stefan Weil
    setbuf(stderr, NULL);
120 6650b710 Stefan Weil
}
121 6650b710 Stefan Weil
122 59a5264b Jes Sorensen
/*
123 59a5264b Jes Sorensen
 * Parse OS specific command line options.
124 59a5264b Jes Sorensen
 * return 0 if option handled, -1 otherwise
125 59a5264b Jes Sorensen
 */
126 59a5264b Jes Sorensen
void os_parse_cmd_args(int index, const char *optarg)
127 59a5264b Jes Sorensen
{
128 59a5264b Jes Sorensen
    return;
129 59a5264b Jes Sorensen
}
130 eb505be1 Jes Sorensen
131 eb505be1 Jes Sorensen
void os_pidfile_error(void)
132 eb505be1 Jes Sorensen
{
133 eb505be1 Jes Sorensen
    fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
134 eb505be1 Jes Sorensen
}
135 bc4a957c Jes Sorensen
136 bc4a957c Jes Sorensen
int qemu_create_pidfile(const char *filename)
137 bc4a957c Jes Sorensen
{
138 bc4a957c Jes Sorensen
    char buffer[128];
139 bc4a957c Jes Sorensen
    int len;
140 bc4a957c Jes Sorensen
    HANDLE file;
141 bc4a957c Jes Sorensen
    OVERLAPPED overlap;
142 bc4a957c Jes Sorensen
    BOOL ret;
143 bc4a957c Jes Sorensen
    memset(&overlap, 0, sizeof(overlap));
144 bc4a957c Jes Sorensen
145 bc4a957c Jes Sorensen
    file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
146 bfc763fc Fabien Chouteau
                      OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
147 bc4a957c Jes Sorensen
148 bc4a957c Jes Sorensen
    if (file == INVALID_HANDLE_VALUE) {
149 bc4a957c Jes Sorensen
        return -1;
150 bc4a957c Jes Sorensen
    }
151 59ad3403 Stefan Weil
    len = snprintf(buffer, sizeof(buffer), "%d\n", getpid());
152 bfc763fc Fabien Chouteau
    ret = WriteFile(file, (LPCVOID)buffer, (DWORD)len,
153 bfc763fc Fabien Chouteau
                    NULL, &overlap);
154 bfc763fc Fabien Chouteau
    CloseHandle(file);
155 bc4a957c Jes Sorensen
    if (ret == 0) {
156 bc4a957c Jes Sorensen
        return -1;
157 bc4a957c Jes Sorensen
    }
158 bc4a957c Jes Sorensen
    return 0;
159 bc4a957c Jes Sorensen
}