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
25 #include "qemu/osdep.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
)
39 if (clock_id
== CLOCK_REALTIME
&& pTimespec
!= 0) {
40 DWORD t
= GetTickCount();
41 const unsigned cps
= 1000;
44 ts
.tv_nsec
= (t
% cps
) * (1000000000UL / cps
);
53 int pthread_sigmask(int how
, const sigset_t
*set
, sigset_t
*oldset
)
55 /* Dummy, do nothing. */
59 int sigfillset(sigset_t
*set
)
63 *(set
) = (sigset_t
)(-1);
71 #endif /* CONFIG_THREAD */
73 int setenv(const char *name
, const char *value
, int overwrite
)
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.
92 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
94 qemu_system_shutdown_request();
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. */
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 void os_parse_cmd_args(int index
, const char *optarg
)
140 int qemu_create_pidfile(const char *filename
)
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
) {
155 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", getpid());
156 ret
= WriteFile(file
, (LPCVOID
)buffer
, (DWORD
)len
,