1 /* kernel32.cc: Win32 replacement functions.
3 This file is part of Cygwin.
5 This software is a copyrighted work licensed under the terms of the
6 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
10 #include "shared_info.h"
20 #include "sys/cygwin.h"
22 /* Implement CreateEvent/OpenEvent so that named objects are always created in
23 Cygwin shared object namespace. */
26 CreateEventW (LPSECURITY_ATTRIBUTES lpEventAttributes
, BOOL bManualReset
,
27 BOOL bInitialState
, LPCWSTR lpName
)
31 OBJECT_ATTRIBUTES attr
;
35 if (lpEventAttributes
&& lpEventAttributes
->bInheritHandle
)
39 RtlInitUnicodeString (&uname
, lpName
);
40 flags
|= OBJ_OPENIF
| OBJ_CASE_INSENSITIVE
;
42 InitializeObjectAttributes (&attr
, lpName
? &uname
: NULL
, flags
,
43 lpName
? get_shared_parent_dir () : NULL
,
45 ? lpEventAttributes
->lpSecurityDescriptor
: NULL
);
46 status
= NtCreateEvent (&evt
, EVENT_ALL_ACCESS
, &attr
,
47 bManualReset
? NotificationEvent
48 : SynchronizationEvent
,
50 if (!NT_SUCCESS (status
))
52 SetLastError (RtlNtStatusToDosError (status
));
55 SetLastError (status
== STATUS_OBJECT_NAME_EXISTS
56 ? ERROR_ALREADY_EXISTS
: ERROR_SUCCESS
);
61 CreateEventA (LPSECURITY_ATTRIBUTES lpEventAttributes
, BOOL bManualReset
,
62 BOOL bInitialState
, LPCSTR lpName
)
66 if (lpName
&& !sys_mbstowcs (name
, MAX_PATH
, lpName
))
68 SetLastError (ERROR_FILENAME_EXCED_RANGE
);
71 return CreateEventW (lpEventAttributes
, bManualReset
, bInitialState
,
72 lpName
? name
: NULL
);
76 OpenEventW (DWORD dwDesiredAccess
, BOOL bInheritHandle
, LPCWSTR lpName
)
80 OBJECT_ATTRIBUTES attr
;
88 RtlInitUnicodeString (&uname
, lpName
);
89 flags
|= OBJ_CASE_INSENSITIVE
;
91 InitializeObjectAttributes (&attr
, lpName
? &uname
: NULL
, flags
,
92 lpName
? get_shared_parent_dir () : NULL
,
94 status
= NtOpenEvent (&evt
, dwDesiredAccess
, &attr
);
95 if (!NT_SUCCESS (status
))
97 SetLastError (RtlNtStatusToDosError (status
));
104 OpenEventA (DWORD dwDesiredAccess
, BOOL bInheritHandle
, LPCSTR lpName
)
106 WCHAR name
[MAX_PATH
];
108 if (lpName
&& !sys_mbstowcs (name
, MAX_PATH
, lpName
))
110 SetLastError (ERROR_FILENAME_EXCED_RANGE
);
113 return OpenEventW (dwDesiredAccess
, bInheritHandle
, lpName
? name
: NULL
);
116 /* Implement CreateMutex/OpenMutex so that named objects are always created in
117 Cygwin shared object namespace. */
120 CreateMutexW (LPSECURITY_ATTRIBUTES lpMutexAttributes
, BOOL bInitialOwner
,
124 UNICODE_STRING uname
;
125 OBJECT_ATTRIBUTES attr
;
129 if (lpMutexAttributes
&& lpMutexAttributes
->bInheritHandle
)
130 flags
|= OBJ_INHERIT
;
133 RtlInitUnicodeString (&uname
, lpName
);
134 flags
|= OBJ_OPENIF
| OBJ_CASE_INSENSITIVE
;
136 InitializeObjectAttributes (&attr
, lpName
? &uname
: NULL
, flags
,
137 lpName
? get_shared_parent_dir () : NULL
,
139 ? lpMutexAttributes
->lpSecurityDescriptor
: NULL
);
140 status
= NtCreateMutant (&mtx
, MUTEX_ALL_ACCESS
, &attr
, bInitialOwner
);
141 if (!NT_SUCCESS (status
))
143 SetLastError (RtlNtStatusToDosError (status
));
146 SetLastError (status
== STATUS_OBJECT_NAME_EXISTS
147 ? ERROR_ALREADY_EXISTS
: ERROR_SUCCESS
);
152 CreateMutexA (LPSECURITY_ATTRIBUTES lpMutexAttributes
, BOOL bInitialOwner
,
155 WCHAR name
[MAX_PATH
];
157 if (lpName
&& !sys_mbstowcs (name
, MAX_PATH
, lpName
))
159 SetLastError (ERROR_FILENAME_EXCED_RANGE
);
162 return CreateMutexW (lpMutexAttributes
, bInitialOwner
, lpName
? name
: NULL
);
166 OpenMutexW (DWORD dwDesiredAccess
, BOOL bInheritHandle
, LPCWSTR lpName
)
169 UNICODE_STRING uname
;
170 OBJECT_ATTRIBUTES attr
;
175 flags
|= OBJ_INHERIT
;
178 RtlInitUnicodeString (&uname
, lpName
);
179 flags
|= OBJ_CASE_INSENSITIVE
;
181 InitializeObjectAttributes (&attr
, lpName
? &uname
: NULL
, flags
,
182 lpName
? get_shared_parent_dir () : NULL
,
184 status
= NtOpenMutant (&mtx
, dwDesiredAccess
, &attr
);
185 if (!NT_SUCCESS (status
))
187 SetLastError (RtlNtStatusToDosError (status
));
194 OpenMutexA (DWORD dwDesiredAccess
, BOOL bInheritHandle
, LPCSTR lpName
)
196 WCHAR name
[MAX_PATH
];
198 if (lpName
&& !sys_mbstowcs (name
, MAX_PATH
, lpName
))
200 SetLastError (ERROR_FILENAME_EXCED_RANGE
);
203 return OpenMutexW (dwDesiredAccess
, bInheritHandle
, lpName
? name
: NULL
);
206 /* Implement CreateSemaphore/OpenSemaphore so that named objects are always
207 created in Cygwin shared object namespace. */
210 CreateSemaphoreW (LPSECURITY_ATTRIBUTES lpSemaphoreAttributes
,
211 LONG lInitialCount
, LONG lMaximumCount
, LPCWSTR lpName
)
214 UNICODE_STRING uname
;
215 OBJECT_ATTRIBUTES attr
;
219 if (lpSemaphoreAttributes
&& lpSemaphoreAttributes
->bInheritHandle
)
220 flags
|= OBJ_INHERIT
;
223 RtlInitUnicodeString (&uname
, lpName
);
224 flags
|= OBJ_OPENIF
| OBJ_CASE_INSENSITIVE
;
226 InitializeObjectAttributes (&attr
, lpName
? &uname
: NULL
, flags
,
227 lpName
? get_shared_parent_dir () : NULL
,
228 lpSemaphoreAttributes
229 ? lpSemaphoreAttributes
->lpSecurityDescriptor
231 status
= NtCreateSemaphore (&sem
, SEMAPHORE_ALL_ACCESS
, &attr
,
232 lInitialCount
, lMaximumCount
);
233 if (!NT_SUCCESS (status
))
235 SetLastError (RtlNtStatusToDosError (status
));
238 SetLastError (status
== STATUS_OBJECT_NAME_EXISTS
239 ? ERROR_ALREADY_EXISTS
: ERROR_SUCCESS
);
244 CreateSemaphoreA (LPSECURITY_ATTRIBUTES lpSemaphoreAttributes
,
245 LONG lInitialCount
, LONG lMaximumCount
, LPCSTR lpName
)
247 WCHAR name
[MAX_PATH
];
249 if (lpName
&& !sys_mbstowcs (name
, MAX_PATH
, lpName
))
251 SetLastError (ERROR_FILENAME_EXCED_RANGE
);
254 return CreateSemaphoreW (lpSemaphoreAttributes
, lInitialCount
, lMaximumCount
,
255 lpName
? name
: NULL
);
259 OpenSemaphoreW (DWORD dwDesiredAccess
, BOOL bInheritHandle
, LPCWSTR lpName
)
262 UNICODE_STRING uname
;
263 OBJECT_ATTRIBUTES attr
;
268 flags
|= OBJ_INHERIT
;
271 RtlInitUnicodeString (&uname
, lpName
);
272 flags
|= OBJ_CASE_INSENSITIVE
;
274 InitializeObjectAttributes (&attr
, lpName
? &uname
: NULL
, flags
,
275 lpName
? get_shared_parent_dir () : NULL
,
277 status
= NtOpenSemaphore (&sem
, dwDesiredAccess
, &attr
);
278 if (!NT_SUCCESS (status
))
280 SetLastError (RtlNtStatusToDosError (status
));
287 OpenSemaphoreA (DWORD dwDesiredAccess
, BOOL bInheritHandle
, LPCSTR lpName
)
289 WCHAR name
[MAX_PATH
];
291 if (lpName
&& !sys_mbstowcs (name
, MAX_PATH
, lpName
))
293 SetLastError (ERROR_FILENAME_EXCED_RANGE
);
296 return OpenSemaphoreW (dwDesiredAccess
, bInheritHandle
, lpName
? name
: NULL
);
299 /* Implement CreateFileMapping/OpenFileMapping so that named objects are always
300 created in Cygwin shared object namespace. */
303 CreateFileMappingW (HANDLE hFile
, LPSECURITY_ATTRIBUTES lpAttributes
,
304 DWORD flProtect
, DWORD dwMaximumSizeHigh
,
305 DWORD dwMaximumSizeLow
, LPCWSTR lpName
)
308 UNICODE_STRING uname
;
309 OBJECT_ATTRIBUTES attr
;
312 ACCESS_MASK access
= STANDARD_RIGHTS_REQUIRED
313 | SECTION_QUERY
| SECTION_MAP_READ
;
314 ULONG prot
= flProtect
& (PAGE_NOACCESS
| PAGE_READONLY
| PAGE_READWRITE
315 | PAGE_WRITECOPY
| PAGE_EXECUTE
316 | PAGE_EXECUTE_READ
| PAGE_EXECUTE_READWRITE
317 | PAGE_EXECUTE_WRITECOPY
);
318 ULONG attribs
= flProtect
& (SEC_COMMIT
| SEC_IMAGE
| SEC_NOCACHE
320 LARGE_INTEGER size
= {{ LowPart
: dwMaximumSizeLow
,
321 HighPart
: (LONG
) dwMaximumSizeHigh
}};
322 PLARGE_INTEGER psize
= size
.QuadPart
? &size
: NULL
;
324 if (prot
& (PAGE_READWRITE
| PAGE_WRITECOPY
325 | PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_WRITECOPY
))
326 access
|= SECTION_MAP_WRITE
;
327 if (prot
& (PAGE_EXECUTE
| PAGE_EXECUTE_READ
328 | PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_WRITECOPY
))
329 access
|= SECTION_MAP_EXECUTE
;
330 if (lpAttributes
&& lpAttributes
->bInheritHandle
)
331 flags
|= OBJ_INHERIT
;
334 RtlInitUnicodeString (&uname
, lpName
);
335 flags
|= OBJ_OPENIF
| OBJ_CASE_INSENSITIVE
;
337 InitializeObjectAttributes (&attr
, lpName
? &uname
: NULL
, flags
,
338 lpName
? get_shared_parent_dir () : NULL
,
340 ? lpAttributes
->lpSecurityDescriptor
342 if (!(attribs
& (SEC_RESERVE
| SEC_COMMIT
)))
343 attribs
|= SEC_COMMIT
;
344 if (hFile
== INVALID_HANDLE_VALUE
)
346 status
= NtCreateSection (§
, access
, &attr
, psize
, prot
, attribs
, hFile
);
347 if (!NT_SUCCESS (status
))
349 SetLastError (RtlNtStatusToDosError (status
));
352 SetLastError (status
== STATUS_OBJECT_NAME_EXISTS
353 ? ERROR_ALREADY_EXISTS
: ERROR_SUCCESS
);
358 CreateFileMappingA (HANDLE hFile
, LPSECURITY_ATTRIBUTES lpAttributes
,
359 DWORD flProtect
, DWORD dwMaximumSizeHigh
,
360 DWORD dwMaximumSizeLow
, LPCSTR lpName
)
362 WCHAR name
[MAX_PATH
];
364 if (lpName
&& !sys_mbstowcs (name
, MAX_PATH
, lpName
))
366 SetLastError (ERROR_FILENAME_EXCED_RANGE
);
369 return CreateFileMappingW (hFile
, lpAttributes
, flProtect
, dwMaximumSizeHigh
,
370 dwMaximumSizeLow
, lpName
? name
: NULL
);
374 OpenFileMappingW (DWORD dwDesiredAccess
, BOOL bInheritHandle
, LPCWSTR lpName
)
377 UNICODE_STRING uname
;
378 OBJECT_ATTRIBUTES attr
;
383 flags
|= OBJ_INHERIT
;
386 RtlInitUnicodeString (&uname
, lpName
);
387 flags
|= OBJ_CASE_INSENSITIVE
;
389 InitializeObjectAttributes (&attr
, lpName
? &uname
: NULL
, flags
,
390 lpName
? get_shared_parent_dir () : NULL
,
392 status
= NtOpenSection (§
, dwDesiredAccess
, &attr
);
393 if (!NT_SUCCESS (status
))
395 SetLastError (RtlNtStatusToDosError (status
));
402 OpenFileMappingA (DWORD dwDesiredAccess
, BOOL bInheritHandle
, LPCSTR lpName
)
404 WCHAR name
[MAX_PATH
];
406 if (lpName
&& !sys_mbstowcs (name
, MAX_PATH
, lpName
))
408 SetLastError (ERROR_FILENAME_EXCED_RANGE
);
411 return OpenFileMappingW (dwDesiredAccess
, bInheritHandle
, lpName
? name
: NULL
);
414 /* The external functions below wrap Windows functions of the same name
415 and provide a Windows interface to Cygwin functionality. */
417 /* Construct a unicode version of the Cygwin command line from __argv) */
418 static UNICODE_STRING
*
421 static UNICODE_STRING wcmd
;
427 char *win_progname
= tp
.c_get ();
428 sys_wcstombs (win_progname
, NT_MAX_PATH
, global_progname
);
429 av
newargv (__argc
, __argv
);
432 cmd
.fromargv (newargv
, win_progname
, true);
433 RtlInitUnicodeString (&wcmd
, cmd
);
439 /* Cygwin replacement for GetCommandLineW. Returns a concatenated wide string
440 representing the argv list, constructed using roughly the same mechanism as
441 child_info_spawn::worker */
443 cygwin_GetCommandLineW (void)
445 return ucmd ()->Buffer
;
448 /* Cygwin replacement for GetCommandLineA. Returns a concatenated string
449 representing the argv list, constructed using roughly the same mechanism
450 as child_info_spawn::worker */
452 cygwin_GetCommandLineA (void)
454 static ANSI_STRING cmd
;
456 RtlUnicodeStringToAnsiString (&cmd
, ucmd (), TRUE
);