mirrored from git://git.sv.gnu.org/emacs.git
/
w32.c
11198 lines (10004 loc) · 304 KB
/
w32.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Utility and Unix shadow routines for GNU Emacs on the Microsoft Windows API.
Copyright (C) 1994-1995, 2000-2024 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
/*
Geoff Voelker (voelker@cs.washington.edu) 7-29-94
*/
#define DEFER_MS_W32_H
#include <config.h>
#include <mingw_time.h>
#include <stddef.h> /* for offsetof */
#include <stdlib.h>
#include <stdio.h>
#include <float.h> /* for DBL_EPSILON */
#include <io.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include <signal.h>
#include <sys/file.h>
#include <time.h> /* must be before nt/inc/sys/time.h, for MinGW64 */
#include <sys/time.h>
#include <sys/utime.h>
#include <math.h>
#include <nproc.h>
/* Include (most) CRT headers *before* ms-w32.h. */
#include <ms-w32.h>
#include <string.h> /* for strerror, needed by sys_strerror */
#include <mbstring.h> /* for _mbspbrk, _mbslwr, _mbsrchr, ... */
#undef access
#undef chdir
#undef chmod
#undef creat
#undef ctime
#undef fopen
#undef link
#undef mkdir
#undef open
#undef rename
#undef rmdir
#undef unlink
#undef close
#undef dup
#undef dup2
#undef pipe
#undef read
#undef write
#undef strerror
#undef localtime
#undef clock
char *sys_ctime (const time_t *);
int sys_chdir (const char *);
int sys_creat (const char *, int);
FILE *sys_fopen (const char *, const char *);
int sys_open (const char *, int, int);
int sys_rename (char const *, char const *);
int sys_rmdir (const char *);
int sys_close (int);
int sys_dup2 (int, int);
int sys_read (int, char *, unsigned int);
int sys_write (int, const void *, unsigned int);
struct tm *sys_localtime (const time_t *);
/* MinGW64 system headers include string.h too early, causing the
compiler to emit a warning about sys_strerror having no
prototype. */
char *sys_strerror (int);
clock_t sys_clock (void);
#ifdef HAVE_MODULES
extern void dynlib_reset_last_error (void);
#endif
#include "lisp.h"
#include "epaths.h" /* for PATH_EXEC */
#include <pwd.h>
#include <grp.h>
#include <windows.h>
/* Some versions of compiler define MEMORYSTATUSEX, some don't, so we
use a different name to avoid compilation problems. */
typedef struct _MEMORY_STATUS_EX {
DWORD dwLength;
DWORD dwMemoryLoad;
DWORDLONG ullTotalPhys;
DWORDLONG ullAvailPhys;
DWORDLONG ullTotalPageFile;
DWORDLONG ullAvailPageFile;
DWORDLONG ullTotalVirtual;
DWORDLONG ullAvailVirtual;
DWORDLONG ullAvailExtendedVirtual;
} MEMORY_STATUS_EX,*LPMEMORY_STATUS_EX;
/* These are here so that GDB would know about these data types. This
allows attaching GDB to Emacs when a fatal exception is triggered
and Windows pops up the "application needs to be closed" dialog.
At that point, _gnu_exception_handler, the top-level exception
handler installed by the MinGW startup code, is somewhere on the
call-stack of the main thread, so going to that call frame and
looking at the argument to _gnu_exception_handler, which is a
PEXCEPTION_POINTERS pointer, can reveal the exception code
(excptr->ExceptionRecord->ExceptionCode) and the address where the
exception happened (excptr->ExceptionRecord->ExceptionAddress), as
well as some additional information specific to the exception. */
PEXCEPTION_POINTERS excptr;
PEXCEPTION_RECORD excprec;
PCONTEXT ctxrec;
#include <lmcons.h>
#include <shlobj.h>
#include <tlhelp32.h>
#include <psapi.h>
#ifndef _MSC_VER
#include <w32api.h>
#endif
#if _WIN32_WINNT < 0x0500
#if !defined (__MINGW32__) || __W32API_MAJOR_VERSION < 3 || (__W32API_MAJOR_VERSION == 3 && __W32API_MINOR_VERSION < 15)
/* This either is not in psapi.h or guarded by higher value of
_WIN32_WINNT than what we use. w32api supplied with MinGW 3.15
defines it in psapi.h */
typedef struct _PROCESS_MEMORY_COUNTERS_EX {
DWORD cb;
DWORD PageFaultCount;
SIZE_T PeakWorkingSetSize;
SIZE_T WorkingSetSize;
SIZE_T QuotaPeakPagedPoolUsage;
SIZE_T QuotaPagedPoolUsage;
SIZE_T QuotaPeakNonPagedPoolUsage;
SIZE_T QuotaNonPagedPoolUsage;
SIZE_T PagefileUsage;
SIZE_T PeakPagefileUsage;
SIZE_T PrivateUsage;
} PROCESS_MEMORY_COUNTERS_EX,*PPROCESS_MEMORY_COUNTERS_EX;
#endif
#endif
#include <winioctl.h>
#include <aclapi.h>
#include <sddl.h>
#include <sys/acl.h>
#include <acl.h>
/* This is not in MinGW's sddl.h (but they are in MSVC headers), so we
define them by hand if not already defined. */
#ifndef SDDL_REVISION_1
#define SDDL_REVISION_1 1
#endif /* SDDL_REVISION_1 */
#if defined(_MSC_VER) || defined(MINGW_W64)
/* MSVC and MinGW64 don't provide the definition of
REPARSE_DATA_BUFFER and the associated macros, except on ntifs.h,
which cannot be included because it triggers conflicts with other
Windows API headers. So we define it here by hand. */
typedef struct _REPARSE_DATA_BUFFER {
ULONG ReparseTag;
USHORT ReparseDataLength;
USHORT Reserved;
union {
struct {
USHORT SubstituteNameOffset;
USHORT SubstituteNameLength;
USHORT PrintNameOffset;
USHORT PrintNameLength;
ULONG Flags;
WCHAR PathBuffer[1];
} SymbolicLinkReparseBuffer;
struct {
USHORT SubstituteNameOffset;
USHORT SubstituteNameLength;
USHORT PrintNameOffset;
USHORT PrintNameLength;
WCHAR PathBuffer[1];
} MountPointReparseBuffer;
struct {
UCHAR DataBuffer[1];
} GenericReparseBuffer;
} DUMMYUNIONNAME;
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
#ifndef FILE_DEVICE_FILE_SYSTEM
#define FILE_DEVICE_FILE_SYSTEM 9
#endif
#ifndef METHOD_BUFFERED
#define METHOD_BUFFERED 0
#endif
#ifndef FILE_ANY_ACCESS
#define FILE_ANY_ACCESS 0x00000000
#endif
#ifndef CTL_CODE
#define CTL_CODE(t,f,m,a) (((t)<<16)|((a)<<14)|((f)<<2)|(m))
#endif
/* MinGW64 defines FSCTL_GET_REPARSE_POINT on winioctl.h. */
#ifndef FSCTL_GET_REPARSE_POINT
#define FSCTL_GET_REPARSE_POINT \
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 42, METHOD_BUFFERED, FILE_ANY_ACCESS)
#endif
#endif
/* TCP connection support. */
#include <sys/socket.h>
#undef socket
#undef bind
#undef connect
#undef htons
#undef ntohs
#undef htonl
#undef ntohl
#undef inet_addr
#undef gethostname
#undef gethostbyname
#undef getservbyname
#undef getpeername
#undef shutdown
#undef setsockopt
#undef listen
#undef getsockname
#undef accept
#undef recvfrom
#undef sendto
/* We need at least XP level for GetAdaptersAddresses stuff. */
#if _WIN32_WINNT < 0x0501
# undef ORIG_WIN32_WINNT
# define ORIG_WIN32_WINNT _WIN32_WINNT
# undef _WIN32_WINNT
# define _WIN32_WINNT 0x0501
#endif
#include <iphlpapi.h> /* should be after winsock2.h */
#ifdef ORIG_WIN32_WINNT
# undef _WIN32_WINNT
# define _WIN32_WINNT ORIG_WIN32_WINNT
# undef ORIG_WIN32_WINNT
#endif
#include <wincrypt.h>
#include <c-strcase.h>
#include <utimens.h> /* for fdutimens */
#include "w32.h"
#include <dirent.h>
#include "w32common.h"
#include "w32select.h"
#include "systime.h" /* for current_timespec, struct timespec */
#include "dispextern.h" /* for xstrcasecmp */
#include "coding.h" /* for Vlocale_coding_system */
#include "careadlinkat.h"
#include "allocator.h"
/* For Lisp_Process, serial_configure and serial_open. */
#include "process.h"
#include "systty.h"
typedef HRESULT (WINAPI * ShGetFolderPath_fn)
(IN HWND, IN int, IN HANDLE, IN DWORD, OUT char *);
static DWORD get_rid (PSID);
static int is_symlink (const char *);
static char * chase_symlinks (const char *);
static int enable_privilege (LPCTSTR, BOOL, TOKEN_PRIVILEGES *);
static int restore_privilege (TOKEN_PRIVILEGES *);
static BOOL WINAPI revert_to_self (void);
static int sys_access (const char *, int);
extern void *e_malloc (size_t);
extern int sys_select (int, SELECT_TYPE *, SELECT_TYPE *, SELECT_TYPE *,
const struct timespec *, const sigset_t *);
extern int sys_dup (int);
/* Initialization states.
WARNING: If you add any more such variables for additional APIs,
you MUST add initialization for them to globals_of_w32
below. This is because these variables might get set
to non-NULL values during dumping, but the dumped Emacs
cannot reuse those values, because it could be run on a
different version of the OS, where API addresses are
different. */
static BOOL g_b_init_is_windows_9x;
static BOOL g_b_init_open_process_token;
static BOOL g_b_init_get_token_information;
static BOOL g_b_init_lookup_account_sid;
static BOOL g_b_init_get_sid_sub_authority;
static BOOL g_b_init_get_sid_sub_authority_count;
static BOOL g_b_init_get_security_info;
static BOOL g_b_init_get_file_security_w;
static BOOL g_b_init_get_file_security_a;
static BOOL g_b_init_get_security_descriptor_owner;
static BOOL g_b_init_get_security_descriptor_group;
static BOOL g_b_init_is_valid_sid;
static BOOL g_b_init_create_toolhelp32_snapshot;
static BOOL g_b_init_process32_first;
static BOOL g_b_init_process32_next;
static BOOL g_b_init_open_thread_token;
static BOOL g_b_init_impersonate_self;
static BOOL g_b_init_revert_to_self;
static BOOL g_b_init_get_process_memory_info;
static BOOL g_b_init_get_process_working_set_size;
static BOOL g_b_init_global_memory_status;
static BOOL g_b_init_global_memory_status_ex;
static BOOL g_b_init_get_length_sid;
static BOOL g_b_init_equal_sid;
static BOOL g_b_init_copy_sid;
static BOOL g_b_init_get_native_system_info;
static BOOL g_b_init_get_system_times;
static BOOL g_b_init_create_symbolic_link_w;
static BOOL g_b_init_create_symbolic_link_a;
static BOOL g_b_init_get_security_descriptor_dacl;
static BOOL g_b_init_convert_sd_to_sddl;
static BOOL g_b_init_convert_sddl_to_sd;
static BOOL g_b_init_is_valid_security_descriptor;
static BOOL g_b_init_set_file_security_w;
static BOOL g_b_init_set_file_security_a;
static BOOL g_b_init_set_named_security_info_w;
static BOOL g_b_init_set_named_security_info_a;
static BOOL g_b_init_get_adapters_info;
static BOOL g_b_init_get_adapters_addresses;
static BOOL g_b_init_reg_open_key_ex_w;
static BOOL g_b_init_reg_query_value_ex_w;
static BOOL g_b_init_expand_environment_strings_w;
static BOOL g_b_init_get_user_default_ui_language;
static BOOL g_b_init_get_console_font_size;
BOOL g_b_init_compare_string_w;
BOOL g_b_init_debug_break_process;
/*
BEGIN: Wrapper functions around OpenProcessToken
and other functions in advapi32.dll that are only
supported in Windows NT / 2k / XP
*/
/* ** Function pointer typedefs ** */
typedef BOOL (WINAPI * OpenProcessToken_Proc) (
HANDLE ProcessHandle,
DWORD DesiredAccess,
PHANDLE TokenHandle);
typedef BOOL (WINAPI * GetTokenInformation_Proc) (
HANDLE TokenHandle,
TOKEN_INFORMATION_CLASS TokenInformationClass,
LPVOID TokenInformation,
DWORD TokenInformationLength,
PDWORD ReturnLength);
typedef BOOL (WINAPI * GetProcessTimes_Proc) (
HANDLE process_handle,
LPFILETIME creation_time,
LPFILETIME exit_time,
LPFILETIME kernel_time,
LPFILETIME user_time);
GetProcessTimes_Proc get_process_times_fn = NULL;
#ifdef _UNICODE
const char * const LookupAccountSid_Name = "LookupAccountSidW";
#else
const char * const LookupAccountSid_Name = "LookupAccountSidA";
#endif
typedef BOOL (WINAPI * LookupAccountSid_Proc) (
LPCTSTR lpSystemName,
PSID Sid,
LPTSTR Name,
LPDWORD cbName,
LPTSTR DomainName,
LPDWORD cbDomainName,
PSID_NAME_USE peUse);
typedef PDWORD (WINAPI * GetSidSubAuthority_Proc) (
PSID pSid,
DWORD n);
typedef PUCHAR (WINAPI * GetSidSubAuthorityCount_Proc) (
PSID pSid);
typedef DWORD (WINAPI * GetSecurityInfo_Proc) (
HANDLE handle,
SE_OBJECT_TYPE ObjectType,
SECURITY_INFORMATION SecurityInfo,
PSID *ppsidOwner,
PSID *ppsidGroup,
PACL *ppDacl,
PACL *ppSacl,
PSECURITY_DESCRIPTOR *ppSecurityDescriptor);
typedef BOOL (WINAPI * GetFileSecurityW_Proc) (
LPCWSTR lpFileName,
SECURITY_INFORMATION RequestedInformation,
PSECURITY_DESCRIPTOR pSecurityDescriptor,
DWORD nLength,
LPDWORD lpnLengthNeeded);
typedef BOOL (WINAPI * GetFileSecurityA_Proc) (
LPCSTR lpFileName,
SECURITY_INFORMATION RequestedInformation,
PSECURITY_DESCRIPTOR pSecurityDescriptor,
DWORD nLength,
LPDWORD lpnLengthNeeded);
typedef BOOL (WINAPI *SetFileSecurityW_Proc) (
LPCWSTR lpFileName,
SECURITY_INFORMATION SecurityInformation,
PSECURITY_DESCRIPTOR pSecurityDescriptor);
typedef BOOL (WINAPI *SetFileSecurityA_Proc) (
LPCSTR lpFileName,
SECURITY_INFORMATION SecurityInformation,
PSECURITY_DESCRIPTOR pSecurityDescriptor);
typedef DWORD (WINAPI *SetNamedSecurityInfoW_Proc) (
LPCWSTR lpObjectName,
SE_OBJECT_TYPE ObjectType,
SECURITY_INFORMATION SecurityInformation,
PSID psidOwner,
PSID psidGroup,
PACL pDacl,
PACL pSacl);
typedef DWORD (WINAPI *SetNamedSecurityInfoA_Proc) (
LPCSTR lpObjectName,
SE_OBJECT_TYPE ObjectType,
SECURITY_INFORMATION SecurityInformation,
PSID psidOwner,
PSID psidGroup,
PACL pDacl,
PACL pSacl);
typedef BOOL (WINAPI * GetSecurityDescriptorOwner_Proc) (
PSECURITY_DESCRIPTOR pSecurityDescriptor,
PSID *pOwner,
LPBOOL lpbOwnerDefaulted);
typedef BOOL (WINAPI * GetSecurityDescriptorGroup_Proc) (
PSECURITY_DESCRIPTOR pSecurityDescriptor,
PSID *pGroup,
LPBOOL lpbGroupDefaulted);
typedef BOOL (WINAPI *GetSecurityDescriptorDacl_Proc) (
PSECURITY_DESCRIPTOR pSecurityDescriptor,
LPBOOL lpbDaclPresent,
PACL *pDacl,
LPBOOL lpbDaclDefaulted);
typedef BOOL (WINAPI * IsValidSid_Proc) (
PSID sid);
typedef HANDLE (WINAPI * CreateToolhelp32Snapshot_Proc) (
DWORD dwFlags,
DWORD th32ProcessID);
typedef BOOL (WINAPI * Process32First_Proc) (
HANDLE hSnapshot,
LPPROCESSENTRY32 lppe);
typedef BOOL (WINAPI * Process32Next_Proc) (
HANDLE hSnapshot,
LPPROCESSENTRY32 lppe);
typedef BOOL (WINAPI * OpenThreadToken_Proc) (
HANDLE ThreadHandle,
DWORD DesiredAccess,
BOOL OpenAsSelf,
PHANDLE TokenHandle);
typedef BOOL (WINAPI * ImpersonateSelf_Proc) (
SECURITY_IMPERSONATION_LEVEL ImpersonationLevel);
typedef BOOL (WINAPI * RevertToSelf_Proc) (void);
typedef BOOL (WINAPI * GetProcessMemoryInfo_Proc) (
HANDLE Process,
PPROCESS_MEMORY_COUNTERS ppsmemCounters,
DWORD cb);
typedef BOOL (WINAPI * GetProcessWorkingSetSize_Proc) (
HANDLE hProcess,
PSIZE_T lpMinimumWorkingSetSize,
PSIZE_T lpMaximumWorkingSetSize);
typedef BOOL (WINAPI * GlobalMemoryStatus_Proc) (
LPMEMORYSTATUS lpBuffer);
typedef BOOL (WINAPI * GlobalMemoryStatusEx_Proc) (
LPMEMORY_STATUS_EX lpBuffer);
typedef BOOL (WINAPI * CopySid_Proc) (
DWORD nDestinationSidLength,
PSID pDestinationSid,
PSID pSourceSid);
typedef BOOL (WINAPI * EqualSid_Proc) (
PSID pSid1,
PSID pSid2);
typedef DWORD (WINAPI * GetLengthSid_Proc) (
PSID pSid);
typedef void (WINAPI * GetNativeSystemInfo_Proc) (
LPSYSTEM_INFO lpSystemInfo);
typedef BOOL (WINAPI * GetSystemTimes_Proc) (
LPFILETIME lpIdleTime,
LPFILETIME lpKernelTime,
LPFILETIME lpUserTime);
typedef BOOLEAN (WINAPI *CreateSymbolicLinkW_Proc) (
LPCWSTR lpSymlinkFileName,
LPCWSTR lpTargetFileName,
DWORD dwFlags);
typedef BOOLEAN (WINAPI *CreateSymbolicLinkA_Proc) (
LPCSTR lpSymlinkFileName,
LPCSTR lpTargetFileName,
DWORD dwFlags);
typedef BOOL (WINAPI *ConvertStringSecurityDescriptorToSecurityDescriptor_Proc) (
LPCTSTR StringSecurityDescriptor,
DWORD StringSDRevision,
PSECURITY_DESCRIPTOR *SecurityDescriptor,
PULONG SecurityDescriptorSize);
typedef BOOL (WINAPI *ConvertSecurityDescriptorToStringSecurityDescriptor_Proc) (
PSECURITY_DESCRIPTOR SecurityDescriptor,
DWORD RequestedStringSDRevision,
SECURITY_INFORMATION SecurityInformation,
LPTSTR *StringSecurityDescriptor,
PULONG StringSecurityDescriptorLen);
typedef BOOL (WINAPI *IsValidSecurityDescriptor_Proc) (PSECURITY_DESCRIPTOR);
typedef DWORD (WINAPI *GetAdaptersInfo_Proc) (
PIP_ADAPTER_INFO pAdapterInfo,
PULONG pOutBufLen);
typedef DWORD (WINAPI *GetAdaptersAddresses_Proc) (
ULONG,
ULONG,
PVOID,
PIP_ADAPTER_ADDRESSES,
PULONG);
int (WINAPI *pMultiByteToWideChar)(UINT,DWORD,LPCSTR,int,LPWSTR,int);
int (WINAPI *pWideCharToMultiByte)(UINT,DWORD,LPCWSTR,int,LPSTR,int,LPCSTR,LPBOOL);
DWORD multiByteToWideCharFlags;
typedef LONG (WINAPI *RegOpenKeyExW_Proc) (HKEY,LPCWSTR,DWORD,REGSAM,PHKEY);
typedef LONG (WINAPI *RegQueryValueExW_Proc) (HKEY,LPCWSTR,LPDWORD,LPDWORD,LPBYTE,LPDWORD);
typedef DWORD (WINAPI *ExpandEnvironmentStringsW_Proc) (LPCWSTR,LPWSTR,DWORD);
typedef LANGID (WINAPI *GetUserDefaultUILanguage_Proc) (void);
typedef COORD (WINAPI *GetConsoleFontSize_Proc) (HANDLE, DWORD);
/* Old versions of mingw.org's MinGW, before v5.2.0, don't have a
_WIN32_WINNT guard for CONSOLE_FONT_INFO in wincon.h, and so don't
need the conditional definition below, which causes compilation
errors. Note: MinGW64 sets _WIN32_WINNT to a higher version, and
its w32api.h version stays fixed at 3.14. */
#if _WIN32_WINNT < 0x0501 \
&& (__W32API_MAJOR_VERSION > 5 \
|| (__W32API_MAJOR_VERSION == 5 && __W32API_MINOR_VERSION >= 2))
typedef struct
{
DWORD nFont;
COORD dwFontSize;
} CONSOLE_FONT_INFO;
#endif
typedef BOOL (WINAPI *GetCurrentConsoleFont_Proc) (
HANDLE,
BOOL,
CONSOLE_FONT_INFO *);
/* ** A utility function ** */
static BOOL
is_windows_9x (void)
{
static BOOL s_b_ret = 0;
OSVERSIONINFO os_ver;
if (g_b_init_is_windows_9x == 0)
{
g_b_init_is_windows_9x = 1;
ZeroMemory (&os_ver, sizeof (OSVERSIONINFO));
os_ver.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
if (GetVersionEx (&os_ver))
{
s_b_ret = (os_ver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
}
}
return s_b_ret;
}
static Lisp_Object ltime (ULONGLONG);
/* Get total user and system times for get-internal-run-time.
Returns a list of integers if the times are provided by the OS
(NT derivatives), otherwise it returns the result of current-time. */
Lisp_Object
w32_get_internal_run_time (void)
{
if (get_process_times_fn)
{
FILETIME create, exit, kernel, user;
HANDLE proc = GetCurrentProcess ();
if ((*get_process_times_fn) (proc, &create, &exit, &kernel, &user))
{
LARGE_INTEGER user_int, kernel_int, total;
user_int.LowPart = user.dwLowDateTime;
user_int.HighPart = user.dwHighDateTime;
kernel_int.LowPart = kernel.dwLowDateTime;
kernel_int.HighPart = kernel.dwHighDateTime;
total.QuadPart = user_int.QuadPart + kernel_int.QuadPart;
return ltime (total.QuadPart);
}
}
return Fcurrent_time ();
}
/* ** The wrapper functions ** */
static BOOL WINAPI
open_process_token (HANDLE ProcessHandle,
DWORD DesiredAccess,
PHANDLE TokenHandle)
{
static OpenProcessToken_Proc s_pfn_Open_Process_Token = NULL;
HMODULE hm_advapi32 = NULL;
if (is_windows_9x () == TRUE)
{
return FALSE;
}
if (g_b_init_open_process_token == 0)
{
g_b_init_open_process_token = 1;
hm_advapi32 = LoadLibrary ("Advapi32.dll");
s_pfn_Open_Process_Token = (OpenProcessToken_Proc)
get_proc_addr (hm_advapi32, "OpenProcessToken");
}
if (s_pfn_Open_Process_Token == NULL)
{
return FALSE;
}
return (
s_pfn_Open_Process_Token (
ProcessHandle,
DesiredAccess,
TokenHandle)
);
}
static BOOL WINAPI
get_token_information (HANDLE TokenHandle,
TOKEN_INFORMATION_CLASS TokenInformationClass,
LPVOID TokenInformation,
DWORD TokenInformationLength,
PDWORD ReturnLength)
{
static GetTokenInformation_Proc s_pfn_Get_Token_Information = NULL;
HMODULE hm_advapi32 = NULL;
if (is_windows_9x () == TRUE)
{
return FALSE;
}
if (g_b_init_get_token_information == 0)
{
g_b_init_get_token_information = 1;
hm_advapi32 = LoadLibrary ("Advapi32.dll");
s_pfn_Get_Token_Information = (GetTokenInformation_Proc)
get_proc_addr (hm_advapi32, "GetTokenInformation");
}
if (s_pfn_Get_Token_Information == NULL)
{
return FALSE;
}
return (
s_pfn_Get_Token_Information (
TokenHandle,
TokenInformationClass,
TokenInformation,
TokenInformationLength,
ReturnLength)
);
}
static BOOL WINAPI
lookup_account_sid (LPCTSTR lpSystemName,
PSID Sid,
LPTSTR Name,
LPDWORD cbName,
LPTSTR DomainName,
LPDWORD cbDomainName,
PSID_NAME_USE peUse)
{
static LookupAccountSid_Proc s_pfn_Lookup_Account_Sid = NULL;
HMODULE hm_advapi32 = NULL;
if (is_windows_9x () == TRUE)
{
return FALSE;
}
if (g_b_init_lookup_account_sid == 0)
{
g_b_init_lookup_account_sid = 1;
hm_advapi32 = LoadLibrary ("Advapi32.dll");
s_pfn_Lookup_Account_Sid = (LookupAccountSid_Proc)
get_proc_addr (hm_advapi32, LookupAccountSid_Name);
}
if (s_pfn_Lookup_Account_Sid == NULL)
{
return FALSE;
}
return (
s_pfn_Lookup_Account_Sid (
lpSystemName,
Sid,
Name,
cbName,
DomainName,
cbDomainName,
peUse)
);
}
static PDWORD WINAPI
get_sid_sub_authority (PSID pSid, DWORD n)
{
static GetSidSubAuthority_Proc s_pfn_Get_Sid_Sub_Authority = NULL;
static DWORD zero = 0U;
HMODULE hm_advapi32 = NULL;
if (is_windows_9x () == TRUE)
{
return &zero;
}
if (g_b_init_get_sid_sub_authority == 0)
{
g_b_init_get_sid_sub_authority = 1;
hm_advapi32 = LoadLibrary ("Advapi32.dll");
s_pfn_Get_Sid_Sub_Authority = (GetSidSubAuthority_Proc)
get_proc_addr (hm_advapi32, "GetSidSubAuthority");
}
if (s_pfn_Get_Sid_Sub_Authority == NULL)
{
return &zero;
}
return (s_pfn_Get_Sid_Sub_Authority (pSid, n));
}
static PUCHAR WINAPI
get_sid_sub_authority_count (PSID pSid)
{
static GetSidSubAuthorityCount_Proc s_pfn_Get_Sid_Sub_Authority_Count = NULL;
static UCHAR zero = 0U;
HMODULE hm_advapi32 = NULL;
if (is_windows_9x () == TRUE)
{
return &zero;
}
if (g_b_init_get_sid_sub_authority_count == 0)
{
g_b_init_get_sid_sub_authority_count = 1;
hm_advapi32 = LoadLibrary ("Advapi32.dll");
s_pfn_Get_Sid_Sub_Authority_Count = (GetSidSubAuthorityCount_Proc)
get_proc_addr (hm_advapi32, "GetSidSubAuthorityCount");
}
if (s_pfn_Get_Sid_Sub_Authority_Count == NULL)
{
return &zero;
}
return (s_pfn_Get_Sid_Sub_Authority_Count (pSid));
}
static DWORD WINAPI
get_security_info (HANDLE handle,
SE_OBJECT_TYPE ObjectType,
SECURITY_INFORMATION SecurityInfo,
PSID *ppsidOwner,
PSID *ppsidGroup,
PACL *ppDacl,
PACL *ppSacl,
PSECURITY_DESCRIPTOR *ppSecurityDescriptor)
{
static GetSecurityInfo_Proc s_pfn_Get_Security_Info = NULL;
HMODULE hm_advapi32 = NULL;
if (is_windows_9x () == TRUE)
{
return FALSE;
}
if (g_b_init_get_security_info == 0)
{
g_b_init_get_security_info = 1;
hm_advapi32 = LoadLibrary ("Advapi32.dll");
s_pfn_Get_Security_Info = (GetSecurityInfo_Proc)
get_proc_addr (hm_advapi32, "GetSecurityInfo");
}
if (s_pfn_Get_Security_Info == NULL)
{
return FALSE;
}
return (s_pfn_Get_Security_Info (handle, ObjectType, SecurityInfo,
ppsidOwner, ppsidGroup, ppDacl, ppSacl,
ppSecurityDescriptor));
}
static BOOL WINAPI
get_file_security (const char *lpFileName,
SECURITY_INFORMATION RequestedInformation,
PSECURITY_DESCRIPTOR pSecurityDescriptor,
DWORD nLength,
LPDWORD lpnLengthNeeded)
{
static GetFileSecurityA_Proc s_pfn_Get_File_SecurityA = NULL;
static GetFileSecurityW_Proc s_pfn_Get_File_SecurityW = NULL;
HMODULE hm_advapi32 = NULL;
if (is_windows_9x () == TRUE)
{
errno = ENOTSUP;
return FALSE;
}
if (w32_unicode_filenames)
{
wchar_t filename_w[MAX_PATH];
if (g_b_init_get_file_security_w == 0)
{
g_b_init_get_file_security_w = 1;
hm_advapi32 = LoadLibrary ("Advapi32.dll");
s_pfn_Get_File_SecurityW = (GetFileSecurityW_Proc)
get_proc_addr (hm_advapi32, "GetFileSecurityW");
}
if (s_pfn_Get_File_SecurityW == NULL)
{
errno = ENOTSUP;
return FALSE;
}
filename_to_utf16 (lpFileName, filename_w);
return (s_pfn_Get_File_SecurityW (filename_w, RequestedInformation,
pSecurityDescriptor, nLength,
lpnLengthNeeded));
}
else
{
char filename_a[MAX_PATH];
if (g_b_init_get_file_security_a == 0)
{
g_b_init_get_file_security_a = 1;
hm_advapi32 = LoadLibrary ("Advapi32.dll");
s_pfn_Get_File_SecurityA = (GetFileSecurityA_Proc)
get_proc_addr (hm_advapi32, "GetFileSecurityA");
}
if (s_pfn_Get_File_SecurityA == NULL)
{
errno = ENOTSUP;
return FALSE;
}
filename_to_ansi (lpFileName, filename_a);
return (s_pfn_Get_File_SecurityA (filename_a, RequestedInformation,
pSecurityDescriptor, nLength,
lpnLengthNeeded));
}
}
static BOOL WINAPI
set_file_security (const char *lpFileName,
SECURITY_INFORMATION SecurityInformation,
PSECURITY_DESCRIPTOR pSecurityDescriptor)
{
static SetFileSecurityW_Proc s_pfn_Set_File_SecurityW = NULL;
static SetFileSecurityA_Proc s_pfn_Set_File_SecurityA = NULL;
HMODULE hm_advapi32 = NULL;
if (is_windows_9x () == TRUE)
{
errno = ENOTSUP;
return FALSE;
}
if (w32_unicode_filenames)
{
wchar_t filename_w[MAX_PATH];
if (g_b_init_set_file_security_w == 0)
{
g_b_init_set_file_security_w = 1;
hm_advapi32 = LoadLibrary ("Advapi32.dll");
s_pfn_Set_File_SecurityW = (SetFileSecurityW_Proc)
get_proc_addr (hm_advapi32, "SetFileSecurityW");
}
if (s_pfn_Set_File_SecurityW == NULL)
{
errno = ENOTSUP;
return FALSE;
}
filename_to_utf16 (lpFileName, filename_w);
return (s_pfn_Set_File_SecurityW (filename_w, SecurityInformation,
pSecurityDescriptor));
}
else
{
char filename_a[MAX_PATH];
if (g_b_init_set_file_security_a == 0)
{
g_b_init_set_file_security_a = 1;
hm_advapi32 = LoadLibrary ("Advapi32.dll");
s_pfn_Set_File_SecurityA = (SetFileSecurityA_Proc)
get_proc_addr (hm_advapi32, "SetFileSecurityA");
}
if (s_pfn_Set_File_SecurityA == NULL)
{
errno = ENOTSUP;
return FALSE;
}
filename_to_ansi (lpFileName, filename_a);
return (s_pfn_Set_File_SecurityA (filename_a, SecurityInformation,
pSecurityDescriptor));
}
}
static DWORD WINAPI
set_named_security_info (LPCTSTR lpObjectName,
SE_OBJECT_TYPE ObjectType,
SECURITY_INFORMATION SecurityInformation,
PSID psidOwner,
PSID psidGroup,
PACL pDacl,
PACL pSacl)
{
static SetNamedSecurityInfoW_Proc s_pfn_Set_Named_Security_InfoW = NULL;
static SetNamedSecurityInfoA_Proc s_pfn_Set_Named_Security_InfoA = NULL;
HMODULE hm_advapi32 = NULL;
if (is_windows_9x () == TRUE)
{
errno = ENOTSUP;
return ENOTSUP;
}
if (w32_unicode_filenames)
{
wchar_t filename_w[MAX_PATH];
if (g_b_init_set_named_security_info_w == 0)
{
g_b_init_set_named_security_info_w = 1;
hm_advapi32 = LoadLibrary ("Advapi32.dll");
s_pfn_Set_Named_Security_InfoW = (SetNamedSecurityInfoW_Proc)
get_proc_addr (hm_advapi32, "SetNamedSecurityInfoW");
}
if (s_pfn_Set_Named_Security_InfoW == NULL)
{
errno = ENOTSUP;
return ENOTSUP;
}
filename_to_utf16 (lpObjectName, filename_w);
return (s_pfn_Set_Named_Security_InfoW (filename_w, ObjectType,
SecurityInformation, psidOwner,
psidGroup, pDacl, pSacl));
}
else
{
char filename_a[MAX_PATH];
if (g_b_init_set_named_security_info_a == 0)
{
g_b_init_set_named_security_info_a = 1;
hm_advapi32 = LoadLibrary ("Advapi32.dll");
s_pfn_Set_Named_Security_InfoA = (SetNamedSecurityInfoA_Proc)
get_proc_addr (hm_advapi32, "SetNamedSecurityInfoA");
}
if (s_pfn_Set_Named_Security_InfoA == NULL)
{
errno = ENOTSUP;
return ENOTSUP;
}
filename_to_ansi (lpObjectName, filename_a);
return (s_pfn_Set_Named_Security_InfoA (filename_a, ObjectType,
SecurityInformation, psidOwner,
psidGroup, pDacl, pSacl));
}
}
static BOOL WINAPI
get_security_descriptor_owner (PSECURITY_DESCRIPTOR pSecurityDescriptor,
PSID *pOwner,
LPBOOL lpbOwnerDefaulted)
{
static GetSecurityDescriptorOwner_Proc s_pfn_Get_Security_Descriptor_Owner = NULL;
HMODULE hm_advapi32 = NULL;
if (is_windows_9x () == TRUE)
{
errno = ENOTSUP;
return FALSE;
}
if (g_b_init_get_security_descriptor_owner == 0)
{
g_b_init_get_security_descriptor_owner = 1;
hm_advapi32 = LoadLibrary ("Advapi32.dll");
s_pfn_Get_Security_Descriptor_Owner = (GetSecurityDescriptorOwner_Proc)
get_proc_addr (hm_advapi32, "GetSecurityDescriptorOwner");
}
if (s_pfn_Get_Security_Descriptor_Owner == NULL)
{
errno = ENOTSUP;
return FALSE;
}
return (s_pfn_Get_Security_Descriptor_Owner (pSecurityDescriptor, pOwner,
lpbOwnerDefaulted));
}
static BOOL WINAPI