system.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2004 by the Free Pascal development team.
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. System.pp for Netware libc environment
  10. **********************************************************************}
  11. { no stack check in system }
  12. {$S-}
  13. unit System;
  14. interface
  15. {$define netware}
  16. {$define netware_libc}
  17. {$define StdErrToConsole}
  18. {$define autoHeapRelease}
  19. {$define IOpossix}
  20. {$define DisableArrayOfConst}
  21. {$define DISABLE_NO_THREAD_MANAGER}
  22. {$ifdef SYSTEMDEBUG}
  23. {$define SYSTEMEXCEPTIONDEBUG}
  24. {$endif SYSTEMDEBUG}
  25. {$ifdef cpui386}
  26. {$define Set_i386_Exception_handler}
  27. {$endif cpui386}
  28. { include system-independent routine headers }
  29. {$I systemh.inc}
  30. {Platform specific information}
  31. const
  32. LineEnding = #13#10;
  33. LFNSupport : boolean = false;
  34. DirectorySeparator = '/';
  35. DriveSeparator = ':';
  36. ExtensionSeparator = '.';
  37. PathSeparator = ';';
  38. AllowDirectorySeparators : set of char = ['\','/'];
  39. AllowDriveSeparators : set of char = [':'];
  40. { FileNameCaseSensitive and FileNameCasePreserving are defined separately below!!! }
  41. maxExitCode = $ffff;
  42. MaxPathLen = 256;
  43. AllFilesMask = '*';
  44. CONST
  45. { Default filehandles }
  46. UnusedHandle : THandle = -1;
  47. StdInputHandle : THandle = 0;
  48. StdOutputHandle : THandle = 0;
  49. StdErrorHandle : THandle = 0;
  50. FileNameCaseSensitive : boolean = false;
  51. FileNameCasePreserving: boolean = true;
  52. CtrlZMarksEOF: boolean = false; (* #26 not considered as end of file *)
  53. sLineBreak = LineEnding;
  54. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  55. type
  56. TNWCheckFunction = procedure (var code : longint);
  57. TDLL_Process_Entry_Hook = function (dllparam : longint) : longbool;
  58. TDLL_Entry_Hook = procedure (dllparam : longint);
  59. VAR
  60. ArgC : INTEGER;
  61. ArgV : ppchar;
  62. NetwareCheckFunction: TNWCheckFunction;
  63. NWLoggerScreen : pointer = nil;
  64. const
  65. Dll_Process_Attach_Hook : TDLL_Process_Entry_Hook = nil;
  66. Dll_Process_Detach_Hook : TDLL_Entry_Hook = nil;
  67. Dll_Thread_Attach_Hook : TDLL_Entry_Hook = nil;
  68. Dll_Thread_Detach_Hook : TDLL_Entry_Hook = nil;
  69. NetwareUnloadProc : pointer = nil; {like exitProc but for nlm unload only}
  70. envp : ppchar = nil;
  71. type
  72. //TSysCloseAllRemainingSemaphores = procedure;
  73. TSysReleaseThreadVars = procedure;
  74. TSysSetThreadDataAreaPtr = function (newPtr:pointer):pointer;
  75. procedure NWSysSetThreadFunctions (atv:TSysReleaseThreadVars;
  76. rtv:TSysReleaseThreadVars;
  77. stdata:TSysSetThreadDataAreaPtr);
  78. procedure _ConsolePrintf (s :shortstring);
  79. procedure _ConsolePrintf (FormatStr : PCHAR; Param : LONGINT);
  80. procedure _ConsolePrintf (FormatStr : PCHAR; Param : pchar);
  81. procedure _ConsolePrintf (FormatStr : PCHAR; P1,P2 : LONGINT);
  82. procedure _ConsolePrintf (FormatStr : PCHAR; P1,P2,P3 : LONGINT);
  83. procedure _ConsolePrintf (FormatStr : PCHAR);
  84. procedure __EnterDebugger;cdecl;external '!netware' name 'EnterDebugger';
  85. function NWGetCodeStart : pointer; // needed for Lineinfo
  86. function NWGetCodeLength : dword;
  87. function NWGetDataStart : pointer;
  88. function NWGetDataLength : dword;
  89. implementation
  90. { Indicate that stack checking is taken care by OS}
  91. {$DEFINE NO_GENERIC_STACK_CHECK}
  92. { include system independent routines }
  93. {$I system.inc}
  94. procedure PASCALMAIN;external name 'PASCALMAIN';
  95. procedure fpc_do_exit;external name 'FPC_DO_EXIT';
  96. {*****************************************************************************
  97. System Dependent Exit code
  98. *****************************************************************************}
  99. var SigTermHandlerActive : boolean;
  100. Procedure system_exit;
  101. begin
  102. if TerminatingThreadID <> 0 then
  103. if TerminatingThreadID <> ThreadId then
  104. if TerminatingThreadID <> dword(pthread_self) then
  105. begin
  106. {$ifdef DEBUG_MT}
  107. _ConsolePrintf ('Terminating Thread %x because halt was called while Thread %x terminates nlm'#13#10,dword(pthread_self),TerminatingThreadId);
  108. {$endif}
  109. pthread_exit (nil);
  110. // only for the case ExitThread fails
  111. while true do
  112. NXThreadYield;
  113. end;
  114. if assigned (ReleaseThreadVars) then ReleaseThreadVars;
  115. {$ifdef autoHeapRelease}
  116. FreeSbrkMem; { free memory allocated by heapmanager }
  117. {$endif}
  118. if not SigTermHandlerActive then
  119. begin
  120. if Erroraddr <> nil then { otherwise we dont see runtime-errors }
  121. SetScreenMode (0);
  122. _exit (ExitCode);
  123. end;
  124. end;
  125. {*****************************************************************************
  126. Stack check code
  127. *****************************************************************************}
  128. const StackErr : boolean = false;
  129. procedure fpc_stackcheck(stack_size:SizeUInt);[public,alias:'FPC_STACKCHECK']; compilerproc;
  130. {
  131. called when trying to get local stack if the compiler directive $S
  132. is set this function must preserve all registers
  133. With a 5k byte safe area used to write to StdIo and some libc
  134. functions without crossing the stack boundary
  135. }
  136. begin
  137. if StackErr then exit; // avoid recursive calls
  138. asm
  139. pusha
  140. end;
  141. stackerr := (stackavail < stack_size + 5120); // we really need that much, at least on nw6.5
  142. asm
  143. popa
  144. end;
  145. if not StackErr then exit;
  146. StackErr := true;
  147. HandleError (202);
  148. end;
  149. {*****************************************************************************
  150. ParamStr/Randomize
  151. *****************************************************************************}
  152. { number of args }
  153. function paramcount : longint;
  154. begin
  155. paramcount := argc - 1;
  156. end;
  157. { argument number l }
  158. function paramstr(l : longint) : string;
  159. begin
  160. if (l>=0) and (l+1<=argc) then
  161. begin
  162. paramstr:=strpas(argv[l]);
  163. if l = 0 then // fix nlm path
  164. begin
  165. DoDirSeparators(paramstr);
  166. end;
  167. end else
  168. paramstr:='';
  169. end;
  170. { set randseed to a new pseudo random value }
  171. procedure randomize;
  172. begin
  173. randseed := time (NIL);
  174. end;
  175. {*****************************************************************************
  176. Thread Handling
  177. *****************************************************************************}
  178. { if return-value is <> 0, netware shows the message
  179. Unload Anyway ?
  180. To Disable unload at all, SetNLMDontUnloadFlag can be used on
  181. Netware >= 4.0 }
  182. function CheckFunction : longint; CDECL; [public,alias: '_NonAppCheckUnload'];
  183. var oldPtr : pointer;
  184. begin
  185. //_ConsolePrintf ('CheckFunction'#13#10);
  186. if assigned (NetwareCheckFunction) then
  187. begin
  188. if assigned (SetThreadDataAreaPtr) then
  189. oldPtr := SetThreadDataAreaPtr (NIL); { nil means main thread }
  190. result := 0;
  191. NetwareCheckFunction (result);
  192. if assigned (SetThreadDataAreaPtr) then
  193. SetThreadDataAreaPtr (oldPtr);
  194. end else
  195. result := 0;
  196. end;
  197. procedure _ConsolePrintf (s : shortstring);
  198. begin
  199. if length(s) > 254 then
  200. byte(s[0]) := 254;
  201. s := s + #0;
  202. _ConsolePrintf (@s[1]);
  203. end;
  204. procedure _ConsolePrintf (FormatStr : PCHAR);
  205. begin
  206. if NWLoggerScreen = nil then
  207. NWLoggerScreen := getnetwarelogger;
  208. if NWLoggerScreen <> nil then
  209. screenprintf (NWLoggerScreen,FormatStr);
  210. end;
  211. procedure _ConsolePrintf (FormatStr : PCHAR; Param : LONGINT);
  212. begin
  213. if NWLoggerScreen = nil then
  214. NWLoggerScreen := getnetwarelogger;
  215. if NWLoggerScreen <> nil then
  216. screenprintf (NWLoggerScreen,FormatStr,Param);
  217. end;
  218. procedure _ConsolePrintf (FormatStr : PCHAR; Param : pchar);
  219. begin
  220. _ConsolePrintf (FormatStr,longint(Param));
  221. end;
  222. procedure _ConsolePrintf (FormatStr : PCHAR; P1,P2 : LONGINT);
  223. begin
  224. if NWLoggerScreen = nil then
  225. NWLoggerScreen := getnetwarelogger;
  226. if NWLoggerScreen <> nil then
  227. screenprintf (NWLoggerScreen,FormatStr,P1,P2);
  228. end;
  229. procedure _ConsolePrintf (FormatStr : PCHAR; P1,P2,P3 : LONGINT);
  230. begin
  231. if NWLoggerScreen = nil then
  232. NWLoggerScreen := getnetwarelogger;
  233. if NWLoggerScreen <> nil then
  234. screenprintf (NWLoggerScreen,FormatStr,P1,P2,P3);
  235. end;
  236. var NWUts : Tutsname;
  237. procedure getCodeAddresses;
  238. begin
  239. if Fpuname(NWUts) < 0 then
  240. FillChar(NWuts,sizeof(NWUts),0);
  241. end;
  242. function NWGetCodeStart : pointer;
  243. begin
  244. NWGetCodeStart := NWUts.codeoffset;
  245. NXThreadYield;
  246. end;
  247. function NWGetCodeLength : dword;
  248. begin
  249. NWGetCodeLength := NWUts.codelength;
  250. NXThreadYield;
  251. end;
  252. function NWGetDataStart : pointer;
  253. begin
  254. NWGetDataStart := NWUts.dataoffset;
  255. NXThreadYield;
  256. end;
  257. function NWGetDataLength : dword;
  258. begin
  259. NWGetDataLength := NWUts.datalength;
  260. NXThreadYield;
  261. end;
  262. {$ifdef StdErrToConsole}
  263. var ConsoleBuff : array [0..512] of char;
  264. Function ConsoleWrite(Var F: TextRec): Integer;
  265. var
  266. i : longint;
  267. Begin
  268. if F.BufPos>0 then
  269. begin
  270. if F.BufPos>sizeof(ConsoleBuff)-1 then
  271. i:=sizeof(ConsoleBuff)-1
  272. else
  273. i:=F.BufPos;
  274. Move(F.BufPtr^,ConsoleBuff,i);
  275. ConsoleBuff[i] := #0;
  276. screenprintf (NWLoggerScreen,@ConsoleBuff);
  277. end;
  278. F.BufPos:=0;
  279. ConsoleWrite := 0;
  280. NXThreadYield;
  281. End;
  282. Function ConsoleClose(Var F: TextRec): Integer;
  283. begin
  284. ConsoleClose:=0;
  285. end;
  286. Function ConsoleOpen(Var F: TextRec): Integer;
  287. Begin
  288. TextRec(F).InOutFunc:=@ConsoleWrite;
  289. TextRec(F).FlushFunc:=@ConsoleWrite;
  290. TextRec(F).CloseFunc:=@ConsoleClose;
  291. ConsoleOpen:=0;
  292. End;
  293. procedure AssignStdErrConsole(Var T: Text);
  294. begin
  295. Assign(T,'');
  296. TextRec(T).OpenFunc:=@ConsoleOpen;
  297. Rewrite(T);
  298. end;
  299. {$endif}
  300. function GetProcessID: SizeUInt;
  301. begin
  302. GetProcessID := SizeUInt (getnlmhandle);
  303. end;
  304. { this will be called if the nlm is unloaded. It will NOT be
  305. called if the program exits i.e. with halt.
  306. Halt (or _exit) can not be called from this callback procedure }
  307. procedure TermSigHandler (Sig:longint); CDecl;
  308. var oldPtr : pointer;
  309. current_exit : procedure;
  310. begin
  311. { Threadvar Pointer will not be valid because the signal
  312. handler is called by netware with a differnt thread. To avoid
  313. problems in the exit routines, we set the data of the main thread
  314. here }
  315. if assigned (SetThreadDataAreaPtr) then
  316. oldPtr := SetThreadDataAreaPtr (NIL); { nil means main thread }
  317. TerminatingThreadID := dword(pthread_self);
  318. {we need to finalize winock to release threads
  319. waiting on a blocking socket call. If that thread
  320. calls halt, we have to avoid that unit finalization
  321. is called by that thread because we are doing it
  322. here
  323. like the old exitProc, mainly to allow winsock to release threads
  324. blocking in a winsock calls }
  325. while NetwareUnloadProc<>nil Do
  326. Begin
  327. InOutRes:=0;
  328. current_exit:=tProcedure(NetwareUnloadProc);
  329. NetwareUnloadProc:=nil;
  330. current_exit();
  331. NXThreadYield;
  332. //hadExitProc := true;
  333. End;
  334. SigTermHandlerActive := true; { to avoid that system_exit calls _exit }
  335. do_exit; { calls finalize units }
  336. if assigned (SetThreadDataAreaPtr) then
  337. SetThreadDataAreaPtr (oldPtr);
  338. end;
  339. procedure SysInitStdIO;
  340. begin
  341. { Setup stdin, stdout and stderr }
  342. {$ifdef IOpossix}
  343. StdInputHandle := THandle (fileno (___stdin^)); // GetStd** returns **FILE !
  344. StdOutputHandle:= THandle (fileno (___stdout^));
  345. StdErrorHandle := THandle (fileno (___stderr^));
  346. {$else}
  347. StdInputHandle := THandle (___stdin^); // GetStd** returns **FILE !
  348. StdOutputHandle:= THandle (___stdout^);
  349. StdErrorHandle := THandle (___stderr^);
  350. {$endif}
  351. OpenStdIO(Input,fmInput,StdInputHandle);
  352. OpenStdIO(Output,fmOutput,StdOutputHandle);
  353. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  354. {$ifdef StdErrToConsole}
  355. AssignStdErrConsole(StdErr);
  356. AssignStdErrConsole(ErrOutput);
  357. {$else}
  358. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  359. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  360. {$endif}
  361. end;
  362. // this is called by main.as, setup args and call PASCALMAIN
  363. procedure nlm_main (_ArgC : LONGINT; _ArgV : ppchar); cdecl; [public,alias: '_FPC_NLM_Entry'];
  364. BEGIN
  365. ArgC := _ArgC;
  366. ArgV := _ArgV;
  367. isLibrary := false;
  368. PASCALMAIN;
  369. do_exit; // currently not needed
  370. END;
  371. function _DLLMain (hInstDLL:pointer; fdwReason:dword; DLLParam:longint):longbool; cdecl;
  372. [public, alias : '_FPC_DLL_Entry'];
  373. var res : longbool;
  374. begin
  375. {$ifdef DEBUG_MT}
  376. _ConsolePrintf ('_FPC_DLL_Entry called');
  377. {$endif}
  378. _DLLMain := false;
  379. isLibrary := true;
  380. case fdwReason of
  381. DLL_ACTUAL_DLLMAIN : _DLLMain := true;
  382. DLL_NLM_STARTUP : begin
  383. //_ConsolePrintf ('DLL_NLM_STARTUP');
  384. if assigned(Dll_Process_Attach_Hook) then
  385. begin
  386. res:=Dll_Process_Attach_Hook(DllParam);
  387. if not res then
  388. exit(false);
  389. end;
  390. PASCALMAIN;
  391. _DLLMain := true;
  392. end;
  393. DLL_NLM_SHUTDOWN : begin
  394. //_ConsolePrintf ('DLL_NLM_SHUTDOWN');
  395. TermSigHandler(0);
  396. _DLLMain := true;
  397. end;
  398. { standard DllMain() messages... }
  399. DLL_THREAD_ATTACH,
  400. DLL_PROCESS_ATTACH : begin
  401. //__ConsolePrintf ('DLL_PROCESS/THREAD_ATTACH');
  402. if assigned(AllocateThreadVars) then
  403. AllocateThreadVars;
  404. if assigned(Dll_Thread_Attach_Hook) then
  405. Dll_Thread_Attach_Hook(DllParam);
  406. _DLLMain := true;
  407. end;
  408. DLL_THREAD_DETACH,
  409. DLL_PROCESS_DETACH : begin
  410. //__ConsolePrintf ('DLL_PROCESS/THREAD_DETACH');
  411. if assigned(Dll_Thread_Detach_Hook) then
  412. Dll_Thread_Detach_Hook(DllParam);
  413. if assigned(ReleaseThreadVars) then
  414. ReleaseThreadVars;
  415. _DLLMain := true;
  416. end;
  417. end;
  418. end;
  419. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  420. begin
  421. result := stklen;
  422. end;
  423. {*****************************************************************************
  424. SystemUnit Initialization
  425. *****************************************************************************}
  426. Begin
  427. getCodeAddresses;
  428. StackLength := CheckInitialStkLen(initialStkLen);
  429. StackBottom := SPtr - StackLength;
  430. SigTermHandlerActive := false;
  431. NetwareCheckFunction := nil;
  432. {$ifdef StdErrToConsole}
  433. NWLoggerScreen := getnetwarelogger;
  434. {$endif}
  435. CheckFunction; // avoid check function to be removed by the linker
  436. envp := ____environ^;
  437. NLMHandle := getnlmhandle;
  438. { allocate resource tags to see what kind of memory i forgot to release }
  439. HeapAllocResourceTag :=
  440. AllocateResourceTag(NLMHandle,'Heap Memory',AllocSignature);
  441. {$ifdef autoHeapRelease}
  442. HeapListAllocResourceTag :=
  443. AllocateResourceTag(NLMHandle,'Heap Memory List',AllocSignature);
  444. {$endif}
  445. FpSignal (SIGTERM, @TermSigHandler);
  446. { Setup heap }
  447. InitHeap;
  448. SysInitExceptions;
  449. { Reset IO Error }
  450. InOutRes:=0;
  451. ThreadID := dword(pthread_self);
  452. initunicodestringmanager;
  453. SysInitStdIO;
  454. {Delphi Compatible}
  455. IsConsole := TRUE;
  456. ExitCode := 0;
  457. InitSystemThreads;
  458. InitSystemDynLibs;
  459. End.