root / os-win32.c @ e3e87df4
History | View | Annotate | Download (4.1 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 | 69bd73b1 | Jes Sorensen | exit(STATUS_CONTROL_C_EXIT); |
61 | 69bd73b1 | Jes Sorensen | return TRUE;
|
62 | 69bd73b1 | Jes Sorensen | } |
63 | 69bd73b1 | Jes Sorensen | |
64 | fe98ac14 | Jes Sorensen | void os_setup_early_signal_handling(void) |
65 | 69bd73b1 | Jes Sorensen | { |
66 | 69bd73b1 | Jes Sorensen | /* Note: cpu_interrupt() is currently not SMP safe, so we force
|
67 | 69bd73b1 | Jes Sorensen | QEMU to run on a single CPU */
|
68 | 69bd73b1 | Jes Sorensen | HANDLE h; |
69 | f45a1108 | Stefan Weil | DWORD_PTR mask, smask; |
70 | 69bd73b1 | Jes Sorensen | int i;
|
71 | 69bd73b1 | Jes Sorensen | |
72 | 69bd73b1 | Jes Sorensen | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); |
73 | 69bd73b1 | Jes Sorensen | |
74 | 69bd73b1 | Jes Sorensen | h = GetCurrentProcess(); |
75 | 69bd73b1 | Jes Sorensen | if (GetProcessAffinityMask(h, &mask, &smask)) {
|
76 | 69bd73b1 | Jes Sorensen | for(i = 0; i < 32; i++) { |
77 | 69bd73b1 | Jes Sorensen | if (mask & (1 << i)) |
78 | 69bd73b1 | Jes Sorensen | break;
|
79 | 69bd73b1 | Jes Sorensen | } |
80 | 69bd73b1 | Jes Sorensen | if (i != 32) { |
81 | 69bd73b1 | Jes Sorensen | mask = 1 << i;
|
82 | 69bd73b1 | Jes Sorensen | SetProcessAffinityMask(h, mask); |
83 | 69bd73b1 | Jes Sorensen | } |
84 | 69bd73b1 | Jes Sorensen | } |
85 | 69bd73b1 | Jes Sorensen | } |
86 | 6170540b | Jes Sorensen | |
87 | 6170540b | Jes Sorensen | /* Look for support files in the same directory as the executable. */
|
88 | 6170540b | Jes Sorensen | char *os_find_datadir(const char *argv0) |
89 | 6170540b | Jes Sorensen | { |
90 | 6170540b | Jes Sorensen | char *p;
|
91 | 6170540b | Jes Sorensen | char buf[MAX_PATH];
|
92 | 6170540b | Jes Sorensen | DWORD len; |
93 | 6170540b | Jes Sorensen | |
94 | 6170540b | Jes Sorensen | len = GetModuleFileName(NULL, buf, sizeof(buf) - 1); |
95 | 6170540b | Jes Sorensen | if (len == 0) { |
96 | 6170540b | Jes Sorensen | return NULL; |
97 | 6170540b | Jes Sorensen | } |
98 | 6170540b | Jes Sorensen | |
99 | 6170540b | Jes Sorensen | buf[len] = 0;
|
100 | 6170540b | Jes Sorensen | p = buf + len - 1;
|
101 | 6170540b | Jes Sorensen | while (p != buf && *p != '\\') |
102 | 6170540b | Jes Sorensen | p--; |
103 | 6170540b | Jes Sorensen | *p = 0;
|
104 | 6170540b | Jes Sorensen | if (access(buf, R_OK) == 0) { |
105 | 7267c094 | Anthony Liguori | return g_strdup(buf);
|
106 | 6170540b | Jes Sorensen | } |
107 | 6170540b | Jes Sorensen | return NULL; |
108 | 6170540b | Jes Sorensen | } |
109 | 59a5264b | Jes Sorensen | |
110 | 6650b710 | Stefan Weil | void os_set_line_buffering(void) |
111 | 6650b710 | Stefan Weil | { |
112 | 6650b710 | Stefan Weil | setbuf(stdout, NULL);
|
113 | 6650b710 | Stefan Weil | setbuf(stderr, NULL);
|
114 | 6650b710 | Stefan Weil | } |
115 | 6650b710 | Stefan Weil | |
116 | 59a5264b | Jes Sorensen | /*
|
117 | 59a5264b | Jes Sorensen | * Parse OS specific command line options.
|
118 | 59a5264b | Jes Sorensen | * return 0 if option handled, -1 otherwise
|
119 | 59a5264b | Jes Sorensen | */
|
120 | 59a5264b | Jes Sorensen | void os_parse_cmd_args(int index, const char *optarg) |
121 | 59a5264b | Jes Sorensen | { |
122 | 59a5264b | Jes Sorensen | return;
|
123 | 59a5264b | Jes Sorensen | } |
124 | eb505be1 | Jes Sorensen | |
125 | eb505be1 | Jes Sorensen | void os_pidfile_error(void) |
126 | eb505be1 | Jes Sorensen | { |
127 | eb505be1 | Jes Sorensen | fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
|
128 | eb505be1 | Jes Sorensen | } |
129 | bc4a957c | Jes Sorensen | |
130 | bc4a957c | Jes Sorensen | int qemu_create_pidfile(const char *filename) |
131 | bc4a957c | Jes Sorensen | { |
132 | bc4a957c | Jes Sorensen | char buffer[128]; |
133 | bc4a957c | Jes Sorensen | int len;
|
134 | bc4a957c | Jes Sorensen | HANDLE file; |
135 | bc4a957c | Jes Sorensen | OVERLAPPED overlap; |
136 | bc4a957c | Jes Sorensen | BOOL ret; |
137 | bc4a957c | Jes Sorensen | memset(&overlap, 0, sizeof(overlap)); |
138 | bc4a957c | Jes Sorensen | |
139 | bc4a957c | Jes Sorensen | file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
|
140 | bfc763fc | Fabien Chouteau | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
141 | bc4a957c | Jes Sorensen | |
142 | bc4a957c | Jes Sorensen | if (file == INVALID_HANDLE_VALUE) {
|
143 | bc4a957c | Jes Sorensen | return -1; |
144 | bc4a957c | Jes Sorensen | } |
145 | 59ad3403 | Stefan Weil | len = snprintf(buffer, sizeof(buffer), "%d\n", getpid()); |
146 | bfc763fc | Fabien Chouteau | ret = WriteFile(file, (LPCVOID)buffer, (DWORD)len, |
147 | bfc763fc | Fabien Chouteau | NULL, &overlap);
|
148 | bfc763fc | Fabien Chouteau | CloseHandle(file); |
149 | bc4a957c | Jes Sorensen | if (ret == 0) { |
150 | bc4a957c | Jes Sorensen | return -1; |
151 | bc4a957c | Jes Sorensen | } |
152 | bc4a957c | Jes Sorensen | return 0; |
153 | bc4a957c | Jes Sorensen | } |