system.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. {$ifdef SYSTEMDEBUG}
  22. {$define SYSTEMEXCEPTIONDEBUG}
  23. {$endif SYSTEMDEBUG}
  24. {$ifdef cpui386}
  25. {$define Set_i386_Exception_handler}
  26. {$endif cpui386}
  27. { include system-independent routine headers }
  28. {$I systemh.inc}
  29. {Platform specific information}
  30. const
  31. LineEnding = #13#10;
  32. LFNSupport : boolean = false;
  33. DirectorySeparator = '/';
  34. DriveSeparator = ':';
  35. PathSeparator = ';';
  36. { FileNameCaseSensitive is defined separately below!!! }
  37. maxExitCode = $ffff;
  38. MaxPathLen = 256;
  39. CONST
  40. { Default filehandles }
  41. UnusedHandle : THandle = -1;
  42. StdInputHandle : THandle = 0;
  43. StdOutputHandle : THandle = 0;
  44. StdErrorHandle : THandle = 0;
  45. FileNameCaseSensitive : boolean = false;
  46. CtrlZMarksEOF: boolean = false; (* #26 not considered as end of file *)
  47. sLineBreak = LineEnding;
  48. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  49. type
  50. TNWCheckFunction = procedure (var code : longint);
  51. TDLL_Process_Entry_Hook = function (dllparam : longint) : longbool;
  52. TDLL_Entry_Hook = procedure (dllparam : longint);
  53. VAR
  54. ArgC : INTEGER;
  55. ArgV : ppchar;
  56. NetwareCheckFunction: TNWCheckFunction;
  57. NWLoggerScreen : pointer = nil;
  58. const
  59. Dll_Process_Attach_Hook : TDLL_Process_Entry_Hook = nil;
  60. Dll_Process_Detach_Hook : TDLL_Entry_Hook = nil;
  61. Dll_Thread_Attach_Hook : TDLL_Entry_Hook = nil;
  62. Dll_Thread_Detach_Hook : TDLL_Entry_Hook = nil;
  63. NetwareUnloadProc : pointer = nil; {like exitProc but for nlm unload only}
  64. envp : ppchar = nil;
  65. type
  66. //TSysCloseAllRemainingSemaphores = procedure;
  67. TSysReleaseThreadVars = procedure;
  68. TSysSetThreadDataAreaPtr = function (newPtr:pointer):pointer;
  69. procedure NWSysSetThreadFunctions (atv:TSysReleaseThreadVars;
  70. rtv:TSysReleaseThreadVars;
  71. stdata:TSysSetThreadDataAreaPtr);
  72. procedure _ConsolePrintf (s :shortstring);
  73. procedure _ConsolePrintf (FormatStr : PCHAR; Param : LONGINT);
  74. procedure _ConsolePrintf (FormatStr : PCHAR; Param : pchar);
  75. procedure _ConsolePrintf (FormatStr : PCHAR; P1,P2 : LONGINT);
  76. procedure _ConsolePrintf (FormatStr : PCHAR; P1,P2,P3 : LONGINT);
  77. procedure _ConsolePrintf (FormatStr : PCHAR);
  78. procedure __EnterDebugger;cdecl;external '!netware' name 'EnterDebugger';
  79. function NWGetCodeStart : pointer; // needed for Lineinfo
  80. function NWGetCodeLength : dword;
  81. function NWGetDataStart : pointer;
  82. function NWGetDataLength : dword;
  83. implementation
  84. { Indicate that stack checking is taken care by OS}
  85. {$DEFINE NO_GENERIC_STACK_CHECK}
  86. { include system independent routines }
  87. {$I system.inc}
  88. procedure PASCALMAIN;external name 'PASCALMAIN';
  89. procedure fpc_do_exit;external name 'FPC_DO_EXIT';
  90. {*****************************************************************************
  91. System Dependent Exit code
  92. *****************************************************************************}
  93. var SigTermHandlerActive : boolean;
  94. Procedure system_exit;
  95. begin
  96. if TerminatingThreadID <> 0 then
  97. if TerminatingThreadID <> ThreadId then
  98. if TerminatingThreadID <> dword(pthread_self) then
  99. begin
  100. {$ifdef DEBUG_MT}
  101. _ConsolePrintf ('Terminating Thread %x because halt was called while Thread %x terminates nlm'#13#10,dword(pthread_self),TerminatingThreadId);
  102. {$endif}
  103. pthread_exit (nil);
  104. // only for the case ExitThread fails
  105. while true do
  106. NXThreadYield;
  107. end;
  108. if assigned (ReleaseThreadVars) then ReleaseThreadVars;
  109. {$ifdef autoHeapRelease}
  110. FreeSbrkMem; { free memory allocated by heapmanager }
  111. {$endif}
  112. if not SigTermHandlerActive then
  113. begin
  114. if Erroraddr <> nil then { otherwise we dont see runtime-errors }
  115. SetScreenMode (0);
  116. _exit (ExitCode);
  117. end;
  118. end;
  119. {*****************************************************************************
  120. Stack check code
  121. *****************************************************************************}
  122. const StackErr : boolean = false;
  123. procedure int_stackcheck(stack_size:Cardinal);[public,alias:'FPC_STACKCHECK'];
  124. {
  125. called when trying to get local stack if the compiler directive $S
  126. is set this function must preserve all registers
  127. With a 5k byte safe area used to write to StdIo and some libc
  128. functions without crossing the stack boundary
  129. }
  130. begin
  131. if StackErr then exit; // avoid recursive calls
  132. asm
  133. pusha
  134. end;
  135. stackerr := (stackavail < stack_size + 5120); // we really need that much, at least on nw6.5
  136. asm
  137. popa
  138. end;
  139. if not StackErr then exit;
  140. StackErr := true;
  141. HandleError (202);
  142. end;
  143. {*****************************************************************************
  144. ParamStr/Randomize
  145. *****************************************************************************}
  146. { number of args }
  147. function paramcount : longint;
  148. begin
  149. paramcount := argc - 1;
  150. end;
  151. { argument number l }
  152. function paramstr(l : longint) : string;
  153. begin
  154. if (l>=0) and (l+1<=argc) then
  155. begin
  156. paramstr:=strpas(argv[l]);
  157. if l = 0 then // fix nlm path
  158. begin
  159. for l := 1 to length (paramstr) do
  160. if paramstr[l] = '\' then paramstr[l] := '/';
  161. end;
  162. end else
  163. paramstr:='';
  164. end;
  165. { set randseed to a new pseudo random value }
  166. procedure randomize;
  167. begin
  168. randseed := time (NIL);
  169. end;
  170. {*****************************************************************************
  171. Thread Handling
  172. *****************************************************************************}
  173. procedure InitFPU;assembler;
  174. asm
  175. fninit
  176. fldcw fpucw
  177. end;
  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. {*****************************************************************************
  420. SystemUnit Initialization
  421. *****************************************************************************}
  422. Begin
  423. getCodeAddresses;
  424. StackBottom := SPtr - StackLength;
  425. SigTermHandlerActive := false;
  426. NetwareCheckFunction := nil;
  427. {$ifdef StdErrToConsole}
  428. NWLoggerScreen := getnetwarelogger;
  429. {$endif}
  430. CheckFunction; // avoid check function to be removed by the linker
  431. envp := ____environ^;
  432. NLMHandle := getnlmhandle;
  433. { allocate resource tags to see what kind of memory i forgot to release }
  434. HeapAllocResourceTag :=
  435. AllocateResourceTag(NLMHandle,'Heap Memory',AllocSignature);
  436. {$ifdef autoHeapRelease}
  437. HeapListAllocResourceTag :=
  438. AllocateResourceTag(NLMHandle,'Heap Memory List',AllocSignature);
  439. {$endif}
  440. FpSignal (SIGTERM, @TermSigHandler);
  441. { Setup heap }
  442. InitHeap;
  443. SysInitExceptions;
  444. { Reset IO Error }
  445. InOutRes:=0;
  446. ThreadID := dword(pthread_self);
  447. SysInitStdIO;
  448. {Delphi Compatible}
  449. IsConsole := TRUE;
  450. ExitCode := 0;
  451. InitSystemThreads;
  452. initvariantmanager;
  453. initwidestringmanager;
  454. End.