/
mormot.core.os.pas
6484 lines (5850 loc) · 224 KB
/
mormot.core.os.pas
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
/// Framework Core Low-Level Wrappers to the Operating-System API
// - this unit is a part of the Open Source Synopse mORMot framework 2,
// licensed under a MPL/GPL/LGPL three license - see LICENSE.md
unit mormot.core.os;
{
*****************************************************************************
Cross-platform functions shared by all framework units
- Some Cross-System Type and Constant Definitions
- Gather Operating System Information
- Operating System Specific Types (e.g. TWinRegistry)
- Unicode, Time, File, Console, Library process
- Per Class Properties O(1) Lookup via vmtAutoTable Slot (e.g. for RTTI cache)
- TSynLocker/TSynLocked and Low-Level Threading Features
- Unix Daemon and Windows Service Support
Aim of this unit is to centralize most used OS-specific API calls, like a
SysUtils unit on steroids, to avoid $ifdef/$endif in "uses" clauses.
In practice, no "Windows", nor "Linux/Posix" reference should be needed in
regular units, once mormot.core.os is included. :)
This unit only refers to mormot.core.base so can be used almost stand-alone.
*****************************************************************************
}
interface
{$I ..\mormot.defines.inc}
uses
{$ifdef OSWINDOWS}
Windows, // needed here e.g. for redefinition of standard types
Messages,
{$endif OSWINDOWS}
classes,
contnrs,
syncobjs,
types,
sysutils,
mormot.core.base;
{ ****************** Some Cross-System Type and Constant Definitions }
const
/// void HTTP Status Code (not a standard value, for internal use only)
HTTP_NONE = 0;
/// HTTP Status Code for "Continue"
HTTP_CONTINUE = 100;
/// HTTP Status Code for "Switching Protocols"
HTTP_SWITCHINGPROTOCOLS = 101;
/// HTTP Status Code for "Success"
HTTP_SUCCESS = 200;
/// HTTP Status Code for "Created"
HTTP_CREATED = 201;
/// HTTP Status Code for "Accepted"
HTTP_ACCEPTED = 202;
/// HTTP Status Code for "Non-Authoritative Information"
HTTP_NONAUTHORIZEDINFO = 203;
/// HTTP Status Code for "No Content"
HTTP_NOCONTENT = 204;
/// HTTP Status Code for "Reset Content"
HTTP_RESETCONTENT = 205;
/// HTTP Status Code for "Partial Content"
HTTP_PARTIALCONTENT = 206;
/// HTTP Status Code for "Multiple Choices"
HTTP_MULTIPLECHOICES = 300;
/// HTTP Status Code for "Moved Permanently"
HTTP_MOVEDPERMANENTLY = 301;
/// HTTP Status Code for "Found"
HTTP_FOUND = 302;
/// HTTP Status Code for "See Other"
HTTP_SEEOTHER = 303;
/// HTTP Status Code for "Not Modified"
HTTP_NOTMODIFIED = 304;
/// HTTP Status Code for "Use Proxy"
HTTP_USEPROXY = 305;
/// HTTP Status Code for "Temporary Redirect"
HTTP_TEMPORARYREDIRECT = 307;
/// HTTP Status Code for "Bad Request"
HTTP_BADREQUEST = 400;
/// HTTP Status Code for "Unauthorized"
HTTP_UNAUTHORIZED = 401;
/// HTTP Status Code for "Forbidden"
HTTP_FORBIDDEN = 403;
/// HTTP Status Code for "Not Found"
HTTP_NOTFOUND = 404;
// HTTP Status Code for "Method Not Allowed"
HTTP_NOTALLOWED = 405;
// HTTP Status Code for "Not Acceptable"
HTTP_NOTACCEPTABLE = 406;
// HTTP Status Code for "Proxy Authentication Required"
HTTP_PROXYAUTHREQUIRED = 407;
/// HTTP Status Code for "Request Time-out"
HTTP_TIMEOUT = 408;
/// HTTP Status Code for "Conflict"
HTTP_CONFLICT = 409;
/// HTTP Status Code for "Payload Too Large"
HTTP_PAYLOADTOOLARGE = 413;
/// HTTP Status Code for "Range Not Satisfiable"
HTTP_RANGENOTSATISFIABLE = 416;
/// HTTP Status Code for "Internal Server Error"
HTTP_SERVERERROR = 500;
/// HTTP Status Code for "Not Implemented"
HTTP_NOTIMPLEMENTED = 501;
/// HTTP Status Code for "Bad Gateway"
HTTP_BADGATEWAY = 502;
/// HTTP Status Code for "Service Unavailable"
HTTP_UNAVAILABLE = 503;
/// HTTP Status Code for "Gateway Timeout"
HTTP_GATEWAYTIMEOUT = 504;
/// HTTP Status Code for "HTTP Version Not Supported"
HTTP_HTTPVERSIONNONSUPPORTED = 505;
/// retrieve the HTTP reason text from its integer code
// - e.g. StatusCodeToReason(200)='OK'
// - as defined in http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
// - returns the generic 'Invalid Request' for unknown Code
// - use an internal cache for efficiency
// - see also StatusCodeToErrorMsg() from mormot.core.text if you need
// the HTTP error as both integer and text, returned as ShortString
function StatusCodeToReason(Code: cardinal): RawUtf8; overload;
{$ifdef HASINLINE}inline;{$endif}
/// retrieve the HTTP reason text from its integer code
// - as defined in http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
procedure StatusCodeToReason(Code: cardinal; var Reason: RawUtf8); overload;
/// returns true for successful HTTP status codes, i.e. in 200..399 range
// - will map mainly SUCCESS (200), CREATED (201), NOCONTENT (204),
// PARTIALCONTENT (206), NOTMODIFIED (304) or TEMPORARYREDIRECT (307) codes
// - any HTTP status not part of this range will be identified as erronous
// request in the internal server statistics
function StatusCodeIsSuccess(Code: integer): boolean;
{$ifdef HASINLINE}inline;{$endif}
/// check the supplied HTTP header to not contain more than one EOL
// - to avoid unexpected HTTP body injection, e.g. from unsafe business code
function IsInvalidHttpHeader(head: PUtf8Char; headlen: PtrInt): boolean;
const
/// HTTP header name for the content type, as defined in the corresponding RFC
HEADER_CONTENT_TYPE = 'Content-Type: ';
/// HTTP header name for the content type, in upper case
// - as defined in the corresponding RFC
// - could be used e.g. with IdemPChar() to retrieve the Content-Type value
HEADER_CONTENT_TYPE_UPPER = 'CONTENT-TYPE: ';
/// HTTP header name for the client IP, in upper case
// - as defined in our HTTP server classes
// - could be used e.g. with IdemPChar() to retrieve the remote IP address
HEADER_REMOTEIP_UPPER = 'REMOTEIP: ';
/// HTTP header name for the authorization token, in upper case
// - could be used e.g. with IdemPChar() to retrieve a JWT value
// - will detect header computed e.g. by motmot.net.http's
// AuthorizationBearer()
HEADER_BEARER_UPPER = 'AUTHORIZATION: BEARER ';
/// MIME content type used for JSON communication (as used by the Microsoft
// WCF framework and the YUI framework)
JSON_CONTENT_TYPE = 'application/json; charset=UTF-8';
/// HTTP header for MIME content type used for plain JSON
// - i.e. 'Content-Type: application/json; charset=UTF-8'
JSON_CONTENT_TYPE_HEADER = HEADER_CONTENT_TYPE + JSON_CONTENT_TYPE;
/// MIME content type used for plain JSON, in upper case
// - could be used e.g. with IdemPChar() to retrieve the Content-Type value
JSON_CONTENT_TYPE_UPPER = 'APPLICATION/JSON';
/// HTTP header for MIME content type used for plain JSON, in upper case
// - could be used e.g. with IdemPChar() to retrieve the Content-Type value
JSON_CONTENT_TYPE_HEADER_UPPER =
HEADER_CONTENT_TYPE_UPPER + JSON_CONTENT_TYPE_UPPER;
/// MIME content type used for plain UTF-8 text
TEXT_CONTENT_TYPE = 'text/plain; charset=UTF-8';
/// HTTP header for MIME content type used for plain UTF-8 text
TEXT_CONTENT_TYPE_HEADER = HEADER_CONTENT_TYPE + TEXT_CONTENT_TYPE;
/// MIME content type used for UTF-8 encoded HTML
HTML_CONTENT_TYPE = 'text/html; charset=UTF-8';
/// HTTP header for MIME content type used for UTF-8 encoded HTML
HTML_CONTENT_TYPE_HEADER = HEADER_CONTENT_TYPE + HTML_CONTENT_TYPE;
/// MIME content type used for UTF-8 encoded XML
XML_CONTENT_TYPE = 'text/xml; charset=UTF-8';
/// HTTP header for MIME content type used for UTF-8 encoded XML
XML_CONTENT_TYPE_HEADER = HEADER_CONTENT_TYPE + XML_CONTENT_TYPE;
/// MIME content type used for raw binary data
BINARY_CONTENT_TYPE = 'application/octet-stream';
/// MIME content type used for raw binary data, in upper case
BINARY_CONTENT_TYPE_UPPER = 'APPLICATION/OCTET-STREAM';
/// HTTP header for MIME content type used for raw binary data
BINARY_CONTENT_TYPE_HEADER = HEADER_CONTENT_TYPE + BINARY_CONTENT_TYPE;
/// MIME content type used for a JPEG picture
JPEG_CONTENT_TYPE = 'image/jpeg';
/// internal HTTP content-type for efficient static file sending
// - detected e.g. by http.sys' THttpApiServer.Request or via the NGINX
// X-Accel-Redirect header's THttpServer.Process (see
// THttpServer.NginxSendFileFrom) for direct sending with no local bufferring
// - the OutCustomHeader should contain the proper 'Content-type: ....'
// corresponding to the file (e.g. by calling GetMimeContentType() function)
STATICFILE_CONTENT_TYPE = '!STATICFILE';
/// internal HTTP content-type Header for efficient static file sending
STATICFILE_CONTENT_TYPE_HEADER =
HEADER_CONTENT_TYPE + STATICFILE_CONTENT_TYPE;
/// uppercase version of HTTP header for static file content serving
STATICFILE_CONTENT_TYPE_HEADER_UPPPER =
HEADER_CONTENT_TYPE_UPPER + STATICFILE_CONTENT_TYPE;
/// used to notify e.g. the THttpServerRequest not to wait for any response
// from the client
// - is not to be used in normal HTTP process, but may be used e.g. by
// TWebSocketProtocolRest.ProcessFrame() to avoid to wait for an incoming
// response from the other endpoint
NORESPONSE_CONTENT_TYPE = '!NORESPONSE';
/// JSON compatible representation of a boolean value, i.e. 'false' and 'true'
// - can be used e.g. in logs, or anything accepting a ShortString
BOOL_STR: array[boolean] of string[7] = (
'false', 'true');
/// the JavaScript-like values of non-number IEEE constants
// - as recognized by FloatToShortNan, and used by TTextWriter.Add()
// when serializing such single/double/extended floating-point values
JSON_NAN: array[TFloatNan] of string[11] = (
'0', '"NaN"', '"Infinity"', '"-Infinity"');
var
/// MIME content type used for JSON communication
// - i.e. 'application/json; charset=UTF-8'
// - this global will be initialized with JSON_CONTENT_TYPE constant, to
// avoid a memory allocation each time it is assigned to a variable
JSON_CONTENT_TYPE_VAR: RawUtf8;
/// HTTP header for MIME content type used for plain JSON
// - this global will be initialized with JSON_CONTENT_TYPE_HEADER constant,
// to avoid a memory allocation each time it is assigned to a variable
JSON_CONTENT_TYPE_HEADER_VAR: RawUtf8;
/// can be used to avoid a memory allocation for res := 'null'
// - this global will be initialized with 'null' constant, to
// avoid a memory allocation each time it is assigned to a variable
NULL_STR_VAR: RawUtf8;
/// JSON compatible representation of a boolean value, i.e. 'false' and 'true'
// - can be used when a RawUtf8 string is expected
// - this global will be initialized with 'false' and 'true' constants, to
// avoid a memory allocation each time it is assigned to a variable
BOOL_UTF8: array[boolean] of RawUtf8;
{ ****************** Gather Operating System Information }
type
/// Exception types raised by this mormot.core.os unit
EOSException = class(Exception);
/// the recognized operating systems
// - it will also recognize most Linux distributions
TOperatingSystem = (
osUnknown,
osWindows,
osLinux,
osOSX,
osBSD,
osPOSIX,
osArch,
osAurox,
osDebian,
osFedora,
osGentoo,
osKnoppix,
osMint,
osMandrake,
osMandriva,
osNovell,
osUbuntu,
osSlackware,
osSolaris,
osSuse,
osSynology,
osTrustix,
osClear,
osUnited,
osRedHat,
osLFS,
osOracle,
osMageia,
osCentOS,
osCloud,
osXen,
osAmazon,
osCoreOS,
osAlpine,
osAndroid);
/// the recognized Windows versions
// - defined even outside OSWINDOWS to access e.g. from monitoring tools
TWindowsVersion = (
wUnknown,
w2000,
wXP,
wXP_64,
wServer2003,
wServer2003_R2,
wVista,
wVista_64,
wServer2008,
wServer2008_64,
wSeven,
wSeven_64,
wServer2008_R2,
wServer2008_R2_64,
wEight,
wEight_64,
wServer2012,
wServer2012_64,
wEightOne,
wEightOne_64,
wServer2012R2,
wServer2012R2_64,
wTen,
wTen_64,
wServer2016,
wServer2016_64,
wEleven,
wEleven_64,
wServer2019_64,
wServer2022_64);
/// the running Operating System, encoded as a 32-bit integer
TOperatingSystemVersion = packed record
case os: TOperatingSystem of
osUnknown: (
b: array[0..2] of byte);
osWindows: (
win: TWindowsVersion;
winbuild: word);
osLinux: (
utsrelease: array[0..2] of byte);
end;
const
/// the recognized Windows versions, as plain text
// - defined even outside OSWINDOWS to allow process e.g. from monitoring tools
WINDOWS_NAME: array[TWindowsVersion] of RawUtf8 = (
'',
'2000',
'XP',
'XP 64bit',
'Server 2003',
'Server 2003 R2',
'Vista',
'Vista 64bit',
'Server 2008',
'Server 2008 64bit',
'7',
'7 64bit',
'Server 2008 R2',
'Server 2008 R2 64bit',
'8',
'8 64bit',
'Server 2012',
'Server 2012 64bit',
'8.1',
'8.1 64bit',
'Server 2012 R2',
'Server 2012 R2 64bit',
'10',
'10 64bit',
'Server 2016',
'Server 2016 64bit',
'11',
'11 64bit',
'Server 2019 64bit',
'Server 2022 64bit');
/// the recognized Windows versions which are 32-bit
WINDOWS_32 = [
w2000,
wXP,
wServer2003,
wServer2003_R2,
wVista,
wServer2008,
wSeven,
wServer2008_R2,
wEight,
wServer2012,
wEightOne,
wServer2012R2,
wTen,
wEleven,
wServer2016];
/// translate one operating system (and distribution) into a its common name
OS_NAME: array[TOperatingSystem] of RawUtf8 = (
'Unknown',
'Windows',
'Linux',
'OSX',
'BSD',
'POSIX',
'Arch',
'Aurox',
'Debian',
'Fedora',
'Gentoo',
'Knoppix',
'Mint',
'Mandrake',
'Mandriva',
'Novell',
'Ubuntu',
'Slackware',
'Solaris',
'Suse',
'Synology',
'Trustix',
'Clear',
'United',
'RedHat',
'LFS',
'Oracle',
'Mageia',
'CentOS',
'Cloud',
'Xen',
'Amazon',
'CoreOS',
'Alpine',
'Android');
/// translate one operating system (and distribution) into a single character
// - may be used internally e.g. for a HTTP User-Agent header, as with
// TFileVersion.UserAgent
OS_INITIAL: array[TOperatingSystem] of AnsiChar = (
'?', // Unknown
'W', // Windows
'L', // Linux
'X', // OSX
'B', // BSD
'P', // POSIX
'A', // Arch
'a', // Aurox
'D', // Debian
'F', // Fedora
'G', // Gentoo
'K', // Knoppix
'M', // Mint
'm', // Mandrake
'n', // Mandriva
'N', // Novell
'U', // Ubuntu
'S', // Slackware
's', // Solaris
'u', // Suse
'Y', // Synology
'T', // Trustix
'C', // Clear
't', // United
'R', // RedHat
'l', // LFS
'O', // Oracle
'G', // Mageia
'c', // CentOS
'd', // Cloud
'x', // Xen
'Z', // Amazon
'r', // CoreOS
'p', // Alpine
'J' // Android (J=JVM)
);
/// the operating systems items which actually have a Linux kernel
OS_LINUX = [
osLinux,
osArch .. osAndroid];
/// the compiler family used
COMP_TEXT = {$ifdef FPC}'Fpc'{$else}'Delphi'{$endif};
/// the target Operating System used for compilation, as short text
OS_TEXT =
{$ifdef OSWINDOWS}
'Win';
{$else} {$ifdef OSDARWIN}
'OSX';
{$else}{$ifdef OSBSD}
'BSD';
{$else} {$ifdef OSANDROID}
'Android';
{$else} {$ifdef OSLINUX}
'Linux';
{$else}
'Posix';
{$endif OSLINUX}
{$endif OSANDROID}
{$endif OSBSD}
{$endif OSDARWIN}
{$endif OSWINDOWS}
/// the CPU architecture used for compilation
CPU_ARCH_TEXT =
{$ifdef CPUX86}
'x86'
{$else} {$ifdef CPUX64}
'x64'
{$else} {$ifdef CPUARM}
'arm' +
{$else} {$ifdef CPUAARCH64}
'aarch' +
{$ifdef CPUPOWERPC}
'ppc' +
{$else} {$ifdef CPUSPARC}
'sparc' +
{$endif CPUSPARC}
{$endif CPUPOWERPC}
{$endif CPUARM}
{$endif CPUAARCH64}
{$ifdef CPU32}
'32'
{$else}
'64'
{$endif CPU32}
{$endif CPUX64}
{$endif CPUX86};
var
/// the target Operating System used for compilation, as TOperatingSystem
// - a specific Linux distribution may be detected instead of plain osLinux
OS_KIND: TOperatingSystem =
{$ifdef OSWINDOWS}
osWindows
{$else} {$ifdef OSDARWIN}
osOSX
{$else} {$ifdef OSBSD}
osBSD
{$else} {$ifdef OSANDROID}
osAndroid
{$else} {$ifdef OSLINUX}
osLinux
{$else}
osPOSIX
{$endif OSLINUX}
{$endif OSANDROID}
{$endif OSBSD}
{$endif OSDARWIN}
{$endif OSWINDOWS};
/// the current Operating System version, as retrieved for the current process
// - contains e.g. 'Windows Seven 64 SP1 (6.1.7601)' or
// 'Ubuntu 16.04.5 LTS - Linux 3.13.0 110 generic#157 Ubuntu SMP Mon Feb 20 11:55:25 UTC 2017'
OSVersionText: RawUtf8;
/// some addition system information as text, e.g. 'Wine 1.1.5'
// - also always appended to OSVersionText high-level description
// - use if PosEx('Wine', OSVersionInfoEx) > 0 then to check for Wine presence
OSVersionInfoEx: RawUtf8;
/// the current Operating System version, as retrieved for the current process
// and computed by ToTextOS(OSVersionInt32)
// - contains e.g. 'Windows Vista' or 'Ubuntu 5.4.0'
OSVersionShort: RawUtf8;
/// some textual information about the current CPU
// - contains e.g. '4 x Intel(R) Core(TM) i5-7300U CPU @ 2.60GHz 3MB cache'
CpuInfoText: RawUtf8;
/// the on-chip cache size, in bytes, as returned by the OS
// - retrieved from /proc/cpuinfo "cache size" entry (L3 cache) on Linux or
// CpuCache[3/4].Size (from GetLogicalProcessorInformation) on Windows
CpuCacheSize: cardinal;
/// the available cache information as returned by the OS
// - e.g. 'L1=2*32KB L2=256KB L3=3MB' on Windows or '3072 KB' on Linux
CpuCacheText: RawUtf8;
/// some textual information about the current computer hardware, from BIOS
// - contains e.g. 'LENOVO 20HES23B0U ThinkPad T470'
BiosInfoText: RawUtf8;
/// Level 1 to 4 CPU caches as returned by GetLogicalProcessorInformation
// - yes, Intel introduced a Level 4 cache (eDRAM) with some Haswell/Iris CPUs
// - this information is not retrieved on all Linux / POSIX systems yet
// - only Unified or Data caches are include (not Instruction or Trace)
// - note: some CPU - like the Apple M1 - have 128 bytes of LineSize
CpuCache: array[1..4] of record
Count, Size, LineSize: cardinal;
end;
{$ifdef OSLINUXANDROID}
/// contains the Flags: or Features: value of Linux /proc/cpuinfo
CpuInfoFeatures: RawUtf8;
{$endif OSLINUXANDROID}
/// the running Operating System
OSVersion32: TOperatingSystemVersion;
/// the running Operating System, encoded as a 32-bit integer
OSVersionInt32: integer absolute OSVersion32;
/// convert an Operating System type into its text representation
// - returns e.g. 'Windows Vista' or 'Ubuntu'
function ToText(const osv: TOperatingSystemVersion): RawUtf8; overload;
/// convert an Operating System type into its one-word text representation
// - returns e.g. 'Vista' or 'Ubuntu'
function ToTextShort(const osv: TOperatingSystemVersion): RawUtf8;
/// convert a 32-bit Operating System type into its full text representation
// including the kernel revision (not the distribution version) on POSIX systems
// - returns e.g. 'Windows Vista', 'Windows 11 64-bit 22000' or 'Ubuntu 5.4.0'
function ToTextOS(osint32: integer): RawUtf8;
type
/// the recognized ARM/AARCH64 CPU types
// - https://github.com/karelzak/util-linux/blob/master/sys-utils/lscpu-arm.c
// - is defined on all platforms for cross-system use
TArmCpuType = (
actUnknown,
actARM810,
actARM920,
actARM922,
actARM926,
actARM940,
actARM946,
actARM966,
actARM1020,
actARM1022,
actARM1026,
actARM11MPCore,
actARM1136,
actARM1156,
actARM1176,
actCortexA5,
actCortexA7,
actCortexA8,
actCortexA9,
actCortexA12,
actCortexA15,
actCortexA17,
actCortexR4,
actCortexR5,
actCortexR7,
actCortexR8,
actCortexM0,
actCortexM1,
actCortexM3,
actCortexM4,
actCortexM7,
actCortexM0P,
actCortexA32,
actCortexA53,
actCortexA35,
actCortexA55,
actCortexA65,
actCortexA57,
actCortexA72,
actCortexA73,
actCortexA75,
actCortexA76,
actNeoverseN1,
actCortexA77,
actCortexA76AE,
actCortexR52,
actCortexM23,
actCortexM33,
actCortexA78,
actCortexA78AE,
actNeoverseE1,
actCortexA78C);
/// a set of recognized ARM/AARCH64 CPU types
TArmCpuTypes = set of TArmCpuType;
/// the recognized ARM/AARCH64 CPU hardware implementers
// - https://github.com/karelzak/util-linux/blob/master/sys-utils/lscpu-arm.c
TArmCpuImplementer = (
aciUnknown,
aciARM,
aciBroadcom,
aciCavium,
aciDEC,
aciFUJITSU,
aciHiSilicon,
aciInfineon,
aciMotorola,
aciNVIDIA,
aciAPM,
aciQualcomm,
aciSamsung,
aciMarvell,
aciApple,
aciFaraday,
aciIntel,
aciAmpere);
/// a set of recognized ARM/AARCH64 CPU hardware implementers
TArmCpuImplementers = set of TArmCpuImplementer;
/// recognize a given ARM/AARCH64 CPU from its 12-bit hardware ID
function ArmCpuType(id: word): TArmCpuType;
/// recognize a given ARM/AARCH64 CPU type name from its 12-bit hardware ID
function ArmCpuTypeName(act: TArmCpuType; id: word): RawUtf8;
/// recognize a given ARM/AARCH64 CPU implementer from its 8-bit hardware ID
function ArmCpuImplementer(id: byte): TArmCpuImplementer;
/// recognize a given ARM/AARCH64 CPU implementer name from its 8-bit hardware ID
function ArmCpuImplementerName(aci: TArmCpuImplementer; id: word): RawUtf8;
const
/// contains the Delphi/FPC Compiler Version as text
// - e.g. 'Delphi 10.3 Rio', 'Delphi 2010' or 'Free Pascal 3.3.1'
COMPILER_VERSION: RawUtf8 =
{$ifdef FPC}
'Free Pascal'
{$ifdef VER2_6_4} + ' 2.6.4'{$endif}
{$ifdef VER3_0} + ' 3.0'
{$ifdef VER3_0_4} + '.4' {$else}
{$ifdef VER3_0_2} + '.2' {$endif}
{$endif VER3_0_4}
{$endif VER3_0}
{$ifdef VER3_1} + ' 3.1'
{$ifdef VER3_1_1} + '.1' {$endif}
{$endif VER3_1}
{$ifdef VER3_2} + ' 3.2'
{$ifdef VER3_2_4} + '.4' {$else}
{$ifdef VER3_2_3} + '.3' {$else}
{$ifdef VER3_2_2} + '.2' {$endif}
{$endif VER3_2_3}
{$endif VER3_2_4}
{$endif VER3_2}
{$ifdef VER3_3} + ' 3.3'
{$ifdef VER3_3_1} + '.1' {$endif}
{$endif VER3_3}
{$ifdef VER3_4} + ' 3.4' {$endif}
{$else}
'Delphi'
{$ifdef CONDITIONALEXPRESSIONS} // Delphi 6 or newer
{$if defined(VER140)} + ' 6'
{$elseif defined(VER150)} + ' 7'
{$elseif defined(VER160)} + ' 8'
{$elseif defined(VER170)} + ' 2005'
{$elseif defined(VER185)} + ' 2007'
{$elseif defined(VER180)} + ' 2006'
{$elseif defined(VER200)} + ' 2009'
{$elseif defined(VER210)} + ' 2010'
{$elseif defined(VER220)} + ' XE'
{$elseif defined(VER230)} + ' XE2'
{$elseif defined(VER240)} + ' XE3'
{$elseif defined(VER250)} + ' XE4'
{$elseif defined(VER260)} + ' XE5'
{$elseif defined(VER265)} + ' AppMethod 1'
{$elseif defined(VER270)} + ' XE6'
{$elseif defined(VER280)} + ' XE7'
{$elseif defined(VER290)} + ' XE8'
{$elseif defined(VER300)} + ' 10 Seattle'
{$elseif defined(VER310)} + ' 10.1 Berlin'
{$elseif defined(VER320)} + ' 10.2 Tokyo'
{$elseif defined(VER330)} + ' 10.3 Rio'
{$elseif defined(VER340)} + ' 10.4 Sydney'
{$elseif defined(VER350)} + ' 11 Alexandria'
{$elseif defined(VER360)} + ' 11.1 Next'
{$ifend}
{$endif CONDITIONALEXPRESSIONS}
{$endif FPC}
{$ifdef CPU64} + ' 64 bit' {$else} + ' 32 bit' {$endif};
{$ifndef PUREMORMOT2}
/// deprecated function: use COMPILER_VERSION constant instead
function GetDelphiCompilerVersion: RawUtf8; deprecated;
{$endif PUREMORMOT2}
{$ifdef OSWINDOWS}
{$ifdef UNICODE}
const
/// a global constant to be appended for Windows Ansi or wide API names
_AW = 'W';
{$else}
const
/// a global constant to be appended for Windows Ansi or wide API names
_AW = 'A';
type
/// low-level API structure, not defined in old Delphi versions
TOSVersionInfoEx = record
dwOSVersionInfoSize: DWORD;
dwMajorVersion: DWORD;
dwMinorVersion: DWORD;
dwBuildNumber: DWORD;
dwPlatformId: DWORD;
szCSDVersion: array[0..127] of char;
wServicePackMajor: WORD;
wServicePackMinor: WORD;
wSuiteMask: WORD;
wProductType: BYTE;
wReserved: BYTE;
end;
{$endif UNICODE}
var
/// is set to TRUE if the current process is a 32-bit image running under WOW64
// - WOW64 is the x86 emulator that allows 32-bit Windows-based applications
// to run seamlessly on 64-bit Windows
// - equals always FALSE if the current executable is a 64-bit image
IsWow64: boolean;
/// the current System information, as retrieved for the current process
// - under a WOW64 process, it will use the GetNativeSystemInfo() new API
// to retrieve the real top-most system information
// - note that the lpMinimumApplicationAddress field is replaced by a
// more optimistic/realistic value ($100000 instead of default $10000)
// - under BSD/Linux, only contain dwPageSize and dwNumberOfProcessors fields
SystemInfo: TSystemInfo;
/// low-level Operating System information, as retrieved for the current process
OSVersionInfo: TOSVersionInfoEx;
/// the current Windows edition, as retrieved for the current process
OSVersion: TWindowsVersion;
{$else OSWINDOWS}
var
/// emulate only some used fields of Windows' TSystemInfo
SystemInfo: record
// retrieved from libc's getpagesize() - is expected to not be 0
dwPageSize: cardinal;
// retrieved from HW_NCPU (BSD) or /proc/cpuinfo (Linux)
dwNumberOfProcessors: cardinal;
// meaningful system information, as returned by fpuname()
uts: record
sysname, release, version: RawUtf8;
end;
/// Linux Distribution release name, retrieved from /etc/*-release
release: RawUtf8;
end;
{$endif OSWINDOWS}
{$M+} // to have existing RTTI for published properties
type
/// used to retrieve version information from any EXE
// - under Linux, all version numbers are set to 0 by default, unless
// you define the FPCUSEVERSIONINFO conditional and information is
// extracted from executable resources
// - you should not have to use this class directly, but via the
// Executable global variable
TFileVersion = class
protected
fDetailed: string;
fFileName: TFileName;
fBuildDateTime: TDateTime;
fVersionInfo, fUserAgent: RawUtf8;
/// change the version (not to be used in most cases)
procedure SetVersion(aMajor, aMinor, aRelease, aBuild: integer);
public
/// executable major version number
Major: integer;
/// executable minor version number
Minor: integer;
/// executable release version number
Release: integer;
/// executable release build number
Build: integer;
/// build year of this exe file
BuildYear: word;
/// version info of the exe file as '3.1'
// - return "string" type, i.e. UnicodeString for Delphi 2009+
Main: string;
/// associated CompanyName string version resource
CompanyName: RawUtf8;
/// associated FileDescription string version resource
FileDescription: RawUtf8;
/// associated FileVersion string version resource
FileVersion: RawUtf8;
/// associated InternalName string version resource
InternalName: RawUtf8;
/// associated LegalCopyright string version resource
LegalCopyright: RawUtf8;
/// associated OriginalFileName string version resource
OriginalFilename: RawUtf8;
/// associated ProductName string version resource
ProductName: RawUtf8;
/// associated ProductVersion string version resource
ProductVersion: RawUtf8;
/// associated Comments string version resource
Comments: RawUtf8;
/// associated Language Translation string version resource
LanguageInfo: RawUtf8;
/// retrieve application version from exe file name
// - DefaultVersion32 is used if no information Version was included into
// the executable resources (on compilation time)
// - you should not have to use this constructor, but rather access the
// Executable global variable
constructor Create(const aFileName: TFileName; aMajor: integer = 0;
aMinor: integer = 0; aRelease: integer = 0; aBuild: integer = 0);
/// retrieve the version as a 32-bit integer with Major.Minor.Release
// - following Major shl 16+Minor shl 8+Release bit pattern
function Version32: integer;
/// build date and time of this exe file, as plain text
function BuildDateTimeString: string;
/// version info of the exe file as '3.1.0.123' or ''
// - this method returns '' if Detailed is '0.0.0.0'
function DetailedOrVoid: string;
/// returns the version information of this exe file as text
// - includes FileName (without path), Detailed and BuildDateTime properties
// - e.g. 'myprogram.exe 3.1.0.123 (2016-06-14 19:07:55)'
function VersionInfo: RawUtf8;
/// returns a ready-to-use User-Agent header with exe name, version and OS
// - e.g. 'myprogram/3.1.0.123W32' for myprogram running on Win32
// - here OS_INITIAL[] character is used to identify the OS, with '32'
// appended on Win32 only (e.g. 'myprogram/3.1.0.2W', is for Win64)
function UserAgent: RawUtf8;
/// returns the version information of a specified exe file as text
// - includes FileName (without path), Detailed and BuildDateTime properties
// - e.g. 'myprogram.exe 3.1.0.123 2016-06-14 19:07:55'
class function GetVersionInfo(const aFileName: TFileName): RawUtf8;
published
/// version info of the exe file as '3.1.0.123'
// - return "string" type, i.e. UnicodeString for Delphi 2009+
// - under Linux, always return '0.0.0.0' if no custom version number
// has been defined
// - consider using DetailedOrVoid method if '0.0.0.0' is not expected
property Detailed: string
read fDetailed write fDetailed;
/// build date and time of this exe file
property BuildDateTime: TDateTime
read fBuildDateTime write fBuildDateTime;
end;
{$M-}
/// quickly parse the TFileVersion.UserAgent content
// - identify e.g. 'myprogram/3.1.0.2W' or 'myprogram/3.1.0.2W32' text
function UserAgentParse(const UserAgent: RawUtf8;
out ProgramName, ProgramVersion: RawUtf8;
out OS: TOperatingSystem): boolean;
type
/// stores some global information about the current executable and computer
TExecutable = record
/// the main executable name, without any path nor extension
// - e.g. 'Test' for 'c:\pathto\Test.exe'
ProgramName: RawUtf8;
/// the main executable details, as used e.g. by TSynLog
// - e.g. 'C:\Dev\lib\SQLite3\exe\TestSQL3.exe 1.2.3.123 (2011-03-29 11:09:06)'
ProgramFullSpec: RawUtf8;
/// the main executable file name (including full path)
// - same as paramstr(0)
ProgramFileName: TFileName;
/// the main executable full path (excluding .exe file name)
// - same as ExtractFilePath(paramstr(0))
ProgramFilePath: TFileName;
/// the full path of the running executable or library
// - for an executable, same as paramstr(0)
// - for a library, will contain the whole .dll file name
InstanceFileName: TFileName;
/// the current executable version
Version: TFileVersion;
/// the current computer host name
Host: RawUtf8;
/// the current computer user name
User: RawUtf8;
/// some hash representation of this information
// - the very same executable on the very same computer run by the very
// same user will always have the same Hash value
// - is computed from the crc32c of this TExecutable fields: c0 from
// Version32, CpuFeatures and Host, c1 from User, c2 from ProgramFullSpec
// and c3 from InstanceFileName
// - may be used as an entropy seed, or to identify a process execution
Hash: THash128Rec;
end;
var
/// global information about the current executable and computer
// - this structure is initialized in this unit's initialization block below
// - you can call SetExecutableVersion() with a custom version, if needed
Executable: TExecutable;
{$ifndef PUREMORMOT2}
/// deprecated global: use Executable variable instead
ExeVersion: TExecutable absolute Executable;
{$endif PUREMORMOT2}