system.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2000 by Marco van de Voort
  4. member of the Free Pascal development team.
  5. System unit for Linux.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. { These things are set in the makefile, }
  13. { But you can override them here.}
  14. { If you use an aout system, set the conditional AOUT}
  15. { $Define AOUT}
  16. Unit System;
  17. {*****************************************************************************}
  18. interface
  19. {*****************************************************************************}
  20. {$define FPC_IS_SYSTEM}
  21. {$define HAS_CMDLINE}
  22. {$define USE_NOTHREADMANAGER}
  23. {$i osdefs.inc}
  24. {$I sysunixh.inc}
  25. function get_cmdline:Pchar;
  26. property cmdline:Pchar read get_cmdline;
  27. {$if defined(CPUARM) or defined(CPUM68K) or (defined(CPUSPARC) and defined(VER2_6))}
  28. {$define fpc_softfpu_interface}
  29. {$i softfpu.pp}
  30. {$undef fpc_softfpu_interface}
  31. {$endif defined(CPUARM) or defined(CPUM68K) or (defined(CPUSPARC) and defined(VER2_6))}
  32. {*****************************************************************************}
  33. implementation
  34. {*****************************************************************************}
  35. {$if defined(CPUI386) and not defined(FPC_USE_LIBC)}
  36. var
  37. sysenter_supported: LongInt = 0;
  38. {$endif}
  39. const calculated_cmdline:Pchar=nil;
  40. {$if defined(CPUARM) or defined(CPUM68K) or (defined(CPUSPARC) and defined(VER2_6))}
  41. {$define fpc_softfpu_implementation}
  42. {$i softfpu.pp}
  43. {$undef fpc_softfpu_implementation}
  44. { we get these functions and types from the softfpu code }
  45. {$define FPC_SYSTEM_HAS_float64}
  46. {$define FPC_SYSTEM_HAS_float32}
  47. {$define FPC_SYSTEM_HAS_flag}
  48. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  49. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  50. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  51. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  52. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  53. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  54. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  55. {$endif defined(CPUARM) or defined(CPUM68K) or (defined(CPUSPARC) and defined(VER2_6))}
  56. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  57. var
  58. EntryInformation : TEntryInformation;
  59. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  60. {$I system.inc}
  61. {*****************************************************************************
  62. Indirect Entry Point
  63. *****************************************************************************}
  64. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  65. var
  66. FPCResStrInitTables : Pointer;public name '_FPC_ResStrInitTables';
  67. FPCResourceStringTables : Pointer;public name '_FPC_ResourceStringTables';
  68. initialstkptr : Pointer;
  69. procedure SysEntry(constref info: TEntryInformation);[public,alias:'FPC_SysEntry'];
  70. begin
  71. EntryInformation := info;
  72. argc := EntryInformation.Platform.argc;
  73. argv := EntryInformation.Platform.argv;
  74. envp := EntryInformation.Platform.envp;
  75. initialstkptr := EntryInformation.Platform.stkptr;
  76. FPCResStrInitTables := EntryInformation.ResStrInitTables;
  77. FPCResourceStringTables := EntryInformation.ResourceStringTables;
  78. {$ifdef cpui386}
  79. Set8087CW(Default8087CW);
  80. {$endif cpui386}
  81. EntryInformation.PascalMain();
  82. end;
  83. {$else FPC_HAS_INDIRECT_MAIN_INFORMATION}
  84. var
  85. initialstkptr : Pointer;external name '__stkptr';
  86. {$if defined(CPUARM) and defined(FPC_ABI_EABI)}
  87. procedure haltproc(e:longint);cdecl;external name '_haltproc_eabi';
  88. {$else}
  89. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  90. {$endif}
  91. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  92. {*****************************************************************************
  93. Misc. System Dependent Functions
  94. *****************************************************************************}
  95. {$ifdef FPC_USE_LIBC}
  96. function FpPrCtl(options : cInt; const args : ptruint) : cint; cdecl; external clib name 'prctl';
  97. {$endif}
  98. procedure System_exit;
  99. begin
  100. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  101. EntryInformation.Platform.haltproc(ExitCode);
  102. {$else FPC_HAS_INDIRECT_MAIN_INFORMATION}
  103. haltproc(ExitCode);
  104. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  105. End;
  106. Function ParamCount: Longint;
  107. Begin
  108. Paramcount:=argc-1
  109. End;
  110. {function BackPos(c:char; const s: shortstring): integer;
  111. var
  112. i: integer;
  113. Begin
  114. for i:=length(s) downto 0 do
  115. if s[i] = c then break;
  116. if i=0 then
  117. BackPos := 0
  118. else
  119. BackPos := i;
  120. end;}
  121. { variable where full path and filename and executable is stored }
  122. { is setup by the startup of the system unit. }
  123. var
  124. execpathstr : shortstring;
  125. function paramstr(l: longint) : string;
  126. begin
  127. { stricly conforming POSIX applications }
  128. { have the executing filename as argv[0] }
  129. if l=0 then
  130. begin
  131. paramstr := execpathstr;
  132. end
  133. else if (l < argc) then
  134. paramstr:=strpas(argv[l])
  135. else
  136. paramstr:='';
  137. end;
  138. Procedure Randomize;
  139. Begin
  140. randseed:=longint(Fptime(nil));
  141. End;
  142. {*****************************************************************************
  143. cmdline
  144. *****************************************************************************}
  145. procedure SetupCmdLine;
  146. var
  147. bufsize,
  148. len,j,
  149. size,i : longint;
  150. found : boolean;
  151. buf : pchar;
  152. procedure AddBuf;
  153. begin
  154. reallocmem(calculated_cmdline,size+bufsize);
  155. move(buf^,calculated_cmdline[size],bufsize);
  156. inc(size,bufsize);
  157. bufsize:=0;
  158. end;
  159. begin
  160. if argc<=0 then
  161. exit;
  162. GetMem(buf,ARG_MAX);
  163. size:=0;
  164. bufsize:=0;
  165. i:=0;
  166. while (i<argc) do
  167. begin
  168. len:=strlen(argv[i]);
  169. if len>ARG_MAX-2 then
  170. len:=ARG_MAX-2;
  171. found:=false;
  172. for j:=1 to len do
  173. if argv[i][j]=' ' then
  174. begin
  175. found:=true;
  176. break;
  177. end;
  178. found:=found or (len=0); // also quote if len=0, bug 19114
  179. if bufsize+len>=ARG_MAX-2 then
  180. AddBuf;
  181. if found then
  182. begin
  183. buf[bufsize]:='"';
  184. inc(bufsize);
  185. end;
  186. if len>0 then
  187. begin
  188. move(argv[i]^,buf[bufsize],len);
  189. inc(bufsize,len);
  190. end;
  191. if found then
  192. begin
  193. buf[bufsize]:='"';
  194. inc(bufsize);
  195. end;
  196. if i<argc-1 then
  197. buf[bufsize]:=' '
  198. else
  199. buf[bufsize]:=#0;
  200. inc(bufsize);
  201. inc(i);
  202. end;
  203. AddBuf;
  204. FreeMem(buf,ARG_MAX);
  205. end;
  206. function get_cmdline:Pchar;
  207. begin
  208. if calculated_cmdline=nil then
  209. setupcmdline;
  210. get_cmdline:=calculated_cmdline;
  211. end;
  212. {*****************************************************************************
  213. SystemUnit Initialization
  214. *****************************************************************************}
  215. function reenable_signal(sig : longint) : boolean;
  216. var
  217. e : TSigSet;
  218. i,j : byte;
  219. olderrno: cint;
  220. begin
  221. fillchar(e,sizeof(e),#0);
  222. { set is 1 based PM }
  223. dec(sig);
  224. i:=sig mod (sizeof(cuLong) * 8);
  225. j:=sig div (sizeof(cuLong) * 8);
  226. e[j]:=1 shl i;
  227. { this routine is called from a signal handler, so must not change errno }
  228. olderrno:=geterrno;
  229. fpsigprocmask(SIG_UNBLOCK,@e,nil);
  230. reenable_signal:=geterrno=0;
  231. seterrno(olderrno);
  232. end;
  233. // signal handler is arch dependant due to processorexception to language
  234. // exception translation
  235. {$i sighnd.inc}
  236. procedure InstallDefaultSignalHandler(signum: longint; out oldact: SigActionRec); public name '_FPC_INSTALLDEFAULTSIGHANDLER';
  237. var
  238. act: SigActionRec;
  239. begin
  240. { Initialize the sigaction structure }
  241. { all flags and information set to zero }
  242. FillChar(act, sizeof(SigActionRec),0);
  243. { initialize handler }
  244. act.sa_handler := SigActionHandler(@SignalToRunError);
  245. act.sa_flags:=SA_SIGINFO;
  246. FpSigAction(signum,@act,@oldact);
  247. end;
  248. var
  249. oldsigfpe: SigActionRec; public name '_FPC_OLDSIGFPE';
  250. oldsigsegv: SigActionRec; public name '_FPC_OLDSIGSEGV';
  251. oldsigbus: SigActionRec; public name '_FPC_OLDSIGBUS';
  252. oldsigill: SigActionRec; public name '_FPC_OLDSIGILL';
  253. Procedure InstallSignals;
  254. begin
  255. InstallDefaultSignalHandler(SIGFPE,oldsigfpe);
  256. InstallDefaultSignalHandler(SIGSEGV,oldsigsegv);
  257. InstallDefaultSignalHandler(SIGBUS,oldsigbus);
  258. InstallDefaultSignalHandler(SIGILL,oldsigill);
  259. end;
  260. procedure SysInitStdIO;
  261. begin
  262. OpenStdIO(Input,fmInput,StdInputHandle);
  263. OpenStdIO(Output,fmOutput,StdOutputHandle);
  264. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  265. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  266. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  267. end;
  268. Procedure RestoreOldSignalHandlers;
  269. begin
  270. FpSigAction(SIGFPE,@oldsigfpe,nil);
  271. FpSigAction(SIGSEGV,@oldsigsegv,nil);
  272. FpSigAction(SIGBUS,@oldsigbus,nil);
  273. FpSigAction(SIGILL,@oldsigill,nil);
  274. end;
  275. procedure SysInitExecPath;
  276. var
  277. i : longint;
  278. begin
  279. execpathstr[0]:=#0;
  280. i:=Fpreadlink('/proc/self/exe',@execpathstr[1],high(execpathstr));
  281. { it must also be an absolute filename, linux 2.0 points to a memory
  282. location so this will skip that }
  283. if (i>0) and (execpathstr[1]='/') then
  284. execpathstr[0]:=char(i);
  285. end;
  286. function GetProcessID: SizeUInt;
  287. begin
  288. GetProcessID := SizeUInt (fpGetPID);
  289. end;
  290. {$ifdef FPC_USE_LIBC}
  291. {$ifdef HAS_UGETRLIMIT}
  292. { there is no ugetrlimit libc call, just map it to the getrlimit call in these cases }
  293. function FpUGetRLimit(resource : cInt; rlim : PRLimit) : cInt; cdecl; external clib name 'getrlimit';
  294. {$endif}
  295. {$endif}
  296. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  297. var
  298. limits : TRLimit;
  299. success : boolean;
  300. begin
  301. success := false;
  302. fillchar(limits, sizeof(limits), 0);
  303. {$ifdef has_ugetrlimit}
  304. success := fpugetrlimit(RLIMIT_STACK, @limits)=0;
  305. {$endif}
  306. {$ifndef NO_SYSCALL_GETRLIMIT}
  307. if (not success) then
  308. success := fpgetrlimit(RLIMIT_STACK, @limits)=0;
  309. {$endif}
  310. if (success) and (limits.rlim_cur < stklen) then
  311. result := limits.rlim_cur
  312. else
  313. result := stklen;
  314. end;
  315. begin
  316. {$if defined(i386) and not defined(FPC_USE_LIBC)}
  317. InitSyscallIntf;
  318. {$endif}
  319. {$ifndef FPUNONE}
  320. {$if defined(cpupowerpc)}
  321. // some PPC kernels set the exception bits FE0/FE1 in the MSR to zero,
  322. // disabling all FPU exceptions. Enable them again.
  323. fpprctl(PR_SET_FPEXC, PR_FP_EXC_PRECISE);
  324. {$endif}
  325. {$endif}
  326. IsConsole := TRUE;
  327. StackLength := CheckInitialStkLen(initialStkLen);
  328. StackBottom := initialstkptr - StackLength;
  329. { Set up signals handlers (may be needed by init code to test cpu features) }
  330. InstallSignals;
  331. {$if defined(cpui386) or defined(cpuarm)}
  332. fpc_cpucodeinit;
  333. {$endif cpui386}
  334. { Setup heap }
  335. InitHeap;
  336. SysInitExceptions;
  337. initunicodestringmanager;
  338. { Setup stdin, stdout and stderr }
  339. SysInitStdIO;
  340. { Arguments }
  341. SysInitExecPath;
  342. { Reset IO Error }
  343. InOutRes:=0;
  344. { threading }
  345. InitSystemThreads;
  346. initvariantmanager;
  347. { restore original signal handlers in case this is a library }
  348. if IsLibrary then
  349. RestoreOldSignalHandlers;
  350. end.