hw/arm: Restore local modifications
[qemu/ar7.git] / os-win32.c
blob590517e220794dde63c4edbee44a79b9e5388138
1 /*
2 * os-win32.c
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2010 Red Hat, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include <windows.h>
27 #include <mmsystem.h>
28 #include "sysemu/sysemu.h"
29 #include "qemu-options.h"
31 /***********************************************************/
32 /* Functions missing in mingw */
34 #if defined(CONFIG_THREAD)
36 int clock_gettime(clockid_t clock_id, struct timespec *pTimespec)
38 int result = 0;
39 if (clock_id == CLOCK_REALTIME && pTimespec != 0) {
40 DWORD t = GetTickCount();
41 const unsigned cps = 1000;
42 struct timespec ts;
43 ts.tv_sec = t / cps;
44 ts.tv_nsec = (t % cps) * (1000000000UL / cps);
45 *pTimespec = ts;
46 } else {
47 errno = EINVAL;
48 result = -1;
50 return result;
53 int pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset)
55 /* Dummy, do nothing. */
56 return EINVAL;
59 int sigfillset(sigset_t *set)
61 int result = 0;
62 if (set) {
63 *(set) = (sigset_t)(-1);
64 } else {
65 errno = EINVAL;
66 result = -1;
68 return result;
71 #endif /* CONFIG_THREAD */
73 int setenv(const char *name, const char *value, int overwrite)
75 int result = 0;
76 if (overwrite || !getenv(name)) {
77 size_t length = strlen(name) + strlen(value) + 2;
78 char *string = g_malloc(length);
79 snprintf(string, length, "%s=%s", name, value);
80 result = putenv(string);
82 /* Windows takes a copy and does not continue to use our string.
83 * Therefore it can be safely freed on this platform. POSIX code
84 * typically has to leak the string because according to the spec it
85 * becomes part of the environment.
87 g_free(string);
89 return result;
92 static BOOL WINAPI qemu_ctrl_handler(DWORD type)
94 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_SIGNAL);
95 /* Windows 7 kills application when the function returns.
96 Sleep here to give QEMU a try for closing.
97 Sleep period is 10000ms because Windows kills the program
98 after 10 seconds anyway. */
99 Sleep(10000);
101 return TRUE;
104 static TIMECAPS mm_tc;
106 static void os_undo_timer_resolution(void)
108 timeEndPeriod(mm_tc.wPeriodMin);
111 void os_setup_early_signal_handling(void)
113 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
114 timeGetDevCaps(&mm_tc, sizeof(mm_tc));
115 timeBeginPeriod(mm_tc.wPeriodMin);
116 atexit(os_undo_timer_resolution);
119 /* Look for support files in the same directory as the executable. */
120 char *os_find_datadir(void)
122 return qemu_get_exec_dir();
125 void os_set_line_buffering(void)
127 setbuf(stdout, NULL);
128 setbuf(stderr, NULL);
132 * Parse OS specific command line options.
133 * return 0 if option handled, -1 otherwise
135 int os_parse_cmd_args(int index, const char *optarg)
137 return -1;
140 int qemu_create_pidfile(const char *filename)
142 char buffer[128];
143 int len;
144 HANDLE file;
145 OVERLAPPED overlap;
146 BOOL ret;
147 memset(&overlap, 0, sizeof(overlap));
149 file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
150 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
152 if (file == INVALID_HANDLE_VALUE) {
153 return -1;
155 len = snprintf(buffer, sizeof(buffer), "%d\n", getpid());
156 ret = WriteFile(file, (LPCVOID)buffer, (DWORD)len,
157 NULL, &overlap);
158 CloseHandle(file);
159 if (ret == 0) {
160 return -1;
162 return 0;