system.pp 15 KB

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