Statistics
| Branch: | Revision:

root / os-win32.c @ 6650b710

History | View | Annotate | Download (6.5 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 0a1574bb Stefan Weil
        char *string = qemu_malloc(length);
45 0a1574bb Stefan Weil
        snprintf(string, length, "%s=%s", name, value);
46 0a1574bb Stefan Weil
        result = putenv(string);
47 0a1574bb Stefan Weil
    }
48 0a1574bb Stefan Weil
    return result;
49 0a1574bb Stefan Weil
}
50 0a1574bb Stefan Weil
51 0a1574bb Stefan Weil
/***********************************************************/
52 19113504 Jes Sorensen
/* Polling handling */
53 19113504 Jes Sorensen
54 19113504 Jes Sorensen
typedef struct PollingEntry {
55 19113504 Jes Sorensen
    PollingFunc *func;
56 19113504 Jes Sorensen
    void *opaque;
57 19113504 Jes Sorensen
    struct PollingEntry *next;
58 19113504 Jes Sorensen
} PollingEntry;
59 19113504 Jes Sorensen
60 19113504 Jes Sorensen
static PollingEntry *first_polling_entry;
61 19113504 Jes Sorensen
62 19113504 Jes Sorensen
int qemu_add_polling_cb(PollingFunc *func, void *opaque)
63 19113504 Jes Sorensen
{
64 19113504 Jes Sorensen
    PollingEntry **ppe, *pe;
65 19113504 Jes Sorensen
    pe = qemu_mallocz(sizeof(PollingEntry));
66 19113504 Jes Sorensen
    pe->func = func;
67 19113504 Jes Sorensen
    pe->opaque = opaque;
68 19113504 Jes Sorensen
    for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
69 19113504 Jes Sorensen
    *ppe = pe;
70 19113504 Jes Sorensen
    return 0;
71 19113504 Jes Sorensen
}
72 19113504 Jes Sorensen
73 19113504 Jes Sorensen
void qemu_del_polling_cb(PollingFunc *func, void *opaque)
74 19113504 Jes Sorensen
{
75 19113504 Jes Sorensen
    PollingEntry **ppe, *pe;
76 19113504 Jes Sorensen
    for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
77 19113504 Jes Sorensen
        pe = *ppe;
78 19113504 Jes Sorensen
        if (pe->func == func && pe->opaque == opaque) {
79 19113504 Jes Sorensen
            *ppe = pe->next;
80 19113504 Jes Sorensen
            qemu_free(pe);
81 19113504 Jes Sorensen
            break;
82 19113504 Jes Sorensen
        }
83 19113504 Jes Sorensen
    }
84 19113504 Jes Sorensen
}
85 19113504 Jes Sorensen
86 19113504 Jes Sorensen
/***********************************************************/
87 19113504 Jes Sorensen
/* Wait objects support */
88 19113504 Jes Sorensen
typedef struct WaitObjects {
89 19113504 Jes Sorensen
    int num;
90 19113504 Jes Sorensen
    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
91 19113504 Jes Sorensen
    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
92 19113504 Jes Sorensen
    void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
93 19113504 Jes Sorensen
} WaitObjects;
94 19113504 Jes Sorensen
95 19113504 Jes Sorensen
static WaitObjects wait_objects = {0};
96 19113504 Jes Sorensen
97 19113504 Jes Sorensen
int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
98 19113504 Jes Sorensen
{
99 19113504 Jes Sorensen
    WaitObjects *w = &wait_objects;
100 19113504 Jes Sorensen
101 19113504 Jes Sorensen
    if (w->num >= MAXIMUM_WAIT_OBJECTS)
102 19113504 Jes Sorensen
        return -1;
103 19113504 Jes Sorensen
    w->events[w->num] = handle;
104 19113504 Jes Sorensen
    w->func[w->num] = func;
105 19113504 Jes Sorensen
    w->opaque[w->num] = opaque;
106 19113504 Jes Sorensen
    w->num++;
107 19113504 Jes Sorensen
    return 0;
108 19113504 Jes Sorensen
}
109 19113504 Jes Sorensen
110 19113504 Jes Sorensen
void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
111 19113504 Jes Sorensen
{
112 19113504 Jes Sorensen
    int i, found;
113 19113504 Jes Sorensen
    WaitObjects *w = &wait_objects;
114 19113504 Jes Sorensen
115 19113504 Jes Sorensen
    found = 0;
116 19113504 Jes Sorensen
    for (i = 0; i < w->num; i++) {
117 19113504 Jes Sorensen
        if (w->events[i] == handle)
118 19113504 Jes Sorensen
            found = 1;
119 19113504 Jes Sorensen
        if (found) {
120 19113504 Jes Sorensen
            w->events[i] = w->events[i + 1];
121 19113504 Jes Sorensen
            w->func[i] = w->func[i + 1];
122 19113504 Jes Sorensen
            w->opaque[i] = w->opaque[i + 1];
123 19113504 Jes Sorensen
        }
124 19113504 Jes Sorensen
    }
125 19113504 Jes Sorensen
    if (found)
126 19113504 Jes Sorensen
        w->num--;
127 19113504 Jes Sorensen
}
128 0d93ca7c Jes Sorensen
129 0d93ca7c Jes Sorensen
void os_host_main_loop_wait(int *timeout)
130 0d93ca7c Jes Sorensen
{
131 0d93ca7c Jes Sorensen
    int ret, ret2, i;
132 0d93ca7c Jes Sorensen
    PollingEntry *pe;
133 0d93ca7c Jes Sorensen
134 0d93ca7c Jes Sorensen
    /* XXX: need to suppress polling by better using win32 events */
135 0d93ca7c Jes Sorensen
    ret = 0;
136 0d93ca7c Jes Sorensen
    for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
137 0d93ca7c Jes Sorensen
        ret |= pe->func(pe->opaque);
138 0d93ca7c Jes Sorensen
    }
139 0d93ca7c Jes Sorensen
    if (ret == 0) {
140 0d93ca7c Jes Sorensen
        int err;
141 0d93ca7c Jes Sorensen
        WaitObjects *w = &wait_objects;
142 0d93ca7c Jes Sorensen
143 0d93ca7c Jes Sorensen
        ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
144 0d93ca7c Jes Sorensen
        if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
145 0d93ca7c Jes Sorensen
            if (w->func[ret - WAIT_OBJECT_0])
146 0d93ca7c Jes Sorensen
                w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
147 0d93ca7c Jes Sorensen
148 0d93ca7c Jes Sorensen
            /* Check for additional signaled events */
149 0d93ca7c Jes Sorensen
            for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
150 0d93ca7c Jes Sorensen
151 0d93ca7c Jes Sorensen
                /* Check if event is signaled */
152 0d93ca7c Jes Sorensen
                ret2 = WaitForSingleObject(w->events[i], 0);
153 0d93ca7c Jes Sorensen
                if(ret2 == WAIT_OBJECT_0) {
154 0d93ca7c Jes Sorensen
                    if (w->func[i])
155 0d93ca7c Jes Sorensen
                        w->func[i](w->opaque[i]);
156 0d93ca7c Jes Sorensen
                } else if (ret2 == WAIT_TIMEOUT) {
157 0d93ca7c Jes Sorensen
                } else {
158 0d93ca7c Jes Sorensen
                    err = GetLastError();
159 0d93ca7c Jes Sorensen
                    fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
160 0d93ca7c Jes Sorensen
                }
161 0d93ca7c Jes Sorensen
            }
162 0d93ca7c Jes Sorensen
        } else if (ret == WAIT_TIMEOUT) {
163 0d93ca7c Jes Sorensen
        } else {
164 0d93ca7c Jes Sorensen
            err = GetLastError();
165 0d93ca7c Jes Sorensen
            fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
166 0d93ca7c Jes Sorensen
        }
167 0d93ca7c Jes Sorensen
    }
168 0d93ca7c Jes Sorensen
169 0d93ca7c Jes Sorensen
    *timeout = 0;
170 0d93ca7c Jes Sorensen
}
171 69bd73b1 Jes Sorensen
172 69bd73b1 Jes Sorensen
static BOOL WINAPI qemu_ctrl_handler(DWORD type)
173 69bd73b1 Jes Sorensen
{
174 69bd73b1 Jes Sorensen
    exit(STATUS_CONTROL_C_EXIT);
175 69bd73b1 Jes Sorensen
    return TRUE;
176 69bd73b1 Jes Sorensen
}
177 69bd73b1 Jes Sorensen
178 fe98ac14 Jes Sorensen
void os_setup_early_signal_handling(void)
179 69bd73b1 Jes Sorensen
{
180 69bd73b1 Jes Sorensen
    /* Note: cpu_interrupt() is currently not SMP safe, so we force
181 69bd73b1 Jes Sorensen
       QEMU to run on a single CPU */
182 69bd73b1 Jes Sorensen
    HANDLE h;
183 69bd73b1 Jes Sorensen
    DWORD mask, smask;
184 69bd73b1 Jes Sorensen
    int i;
185 69bd73b1 Jes Sorensen
186 69bd73b1 Jes Sorensen
    SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
187 69bd73b1 Jes Sorensen
188 69bd73b1 Jes Sorensen
    h = GetCurrentProcess();
189 69bd73b1 Jes Sorensen
    if (GetProcessAffinityMask(h, &mask, &smask)) {
190 69bd73b1 Jes Sorensen
        for(i = 0; i < 32; i++) {
191 69bd73b1 Jes Sorensen
            if (mask & (1 << i))
192 69bd73b1 Jes Sorensen
                break;
193 69bd73b1 Jes Sorensen
        }
194 69bd73b1 Jes Sorensen
        if (i != 32) {
195 69bd73b1 Jes Sorensen
            mask = 1 << i;
196 69bd73b1 Jes Sorensen
            SetProcessAffinityMask(h, mask);
197 69bd73b1 Jes Sorensen
        }
198 69bd73b1 Jes Sorensen
    }
199 69bd73b1 Jes Sorensen
}
200 6170540b Jes Sorensen
201 6170540b Jes Sorensen
/* Look for support files in the same directory as the executable.  */
202 6170540b Jes Sorensen
char *os_find_datadir(const char *argv0)
203 6170540b Jes Sorensen
{
204 6170540b Jes Sorensen
    char *p;
205 6170540b Jes Sorensen
    char buf[MAX_PATH];
206 6170540b Jes Sorensen
    DWORD len;
207 6170540b Jes Sorensen
208 6170540b Jes Sorensen
    len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
209 6170540b Jes Sorensen
    if (len == 0) {
210 6170540b Jes Sorensen
        return NULL;
211 6170540b Jes Sorensen
    }
212 6170540b Jes Sorensen
213 6170540b Jes Sorensen
    buf[len] = 0;
214 6170540b Jes Sorensen
    p = buf + len - 1;
215 6170540b Jes Sorensen
    while (p != buf && *p != '\\')
216 6170540b Jes Sorensen
        p--;
217 6170540b Jes Sorensen
    *p = 0;
218 6170540b Jes Sorensen
    if (access(buf, R_OK) == 0) {
219 6170540b Jes Sorensen
        return qemu_strdup(buf);
220 6170540b Jes Sorensen
    }
221 6170540b Jes Sorensen
    return NULL;
222 6170540b Jes Sorensen
}
223 59a5264b Jes Sorensen
224 6650b710 Stefan Weil
void os_set_line_buffering(void)
225 6650b710 Stefan Weil
{
226 6650b710 Stefan Weil
    setbuf(stdout, NULL);
227 6650b710 Stefan Weil
    setbuf(stderr, NULL);
228 6650b710 Stefan Weil
}
229 6650b710 Stefan Weil
230 59a5264b Jes Sorensen
/*
231 59a5264b Jes Sorensen
 * Parse OS specific command line options.
232 59a5264b Jes Sorensen
 * return 0 if option handled, -1 otherwise
233 59a5264b Jes Sorensen
 */
234 59a5264b Jes Sorensen
void os_parse_cmd_args(int index, const char *optarg)
235 59a5264b Jes Sorensen
{
236 59a5264b Jes Sorensen
    return;
237 59a5264b Jes Sorensen
}
238 eb505be1 Jes Sorensen
239 eb505be1 Jes Sorensen
void os_pidfile_error(void)
240 eb505be1 Jes Sorensen
{
241 eb505be1 Jes Sorensen
    fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
242 eb505be1 Jes Sorensen
}