system.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. FPCResLocation : Pointer;public name '_FPC_ResLocation';
  69. initialstkptr : Pointer;
  70. procedure SysEntry(constref info: TEntryInformation);[public,alias:'FPC_SysEntry'];
  71. begin
  72. EntryInformation := info;
  73. argc := EntryInformation.Platform.argc;
  74. argv := EntryInformation.Platform.argv;
  75. envp := EntryInformation.Platform.envp;
  76. initialstkptr := EntryInformation.Platform.stkptr;
  77. FPCResStrInitTables := EntryInformation.ResStrInitTables;
  78. FPCResourceStringTables := EntryInformation.ResourceStringTables;
  79. FPCResLocation := EntryInformation.ResLocation;
  80. {$ifdef cpui386}
  81. Set8087CW(Default8087CW);
  82. {$endif cpui386}
  83. EntryInformation.PascalMain();
  84. end;
  85. {$else FPC_HAS_INDIRECT_MAIN_INFORMATION}
  86. var
  87. initialstkptr : Pointer;external name '__stkptr';
  88. {$if defined(CPUARM) and defined(FPC_ABI_EABI)}
  89. procedure haltproc(e:longint);cdecl;external name '_haltproc_eabi';
  90. {$else}
  91. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  92. {$endif}
  93. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  94. {*****************************************************************************
  95. Misc. System Dependent Functions
  96. *****************************************************************************}
  97. {$ifdef FPC_USE_LIBC}
  98. function FpPrCtl(options : cInt; const args : ptruint) : cint; cdecl; external clib name 'prctl';
  99. {$endif}
  100. procedure System_exit;
  101. begin
  102. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  103. EntryInformation.Platform.haltproc(ExitCode);
  104. {$else FPC_HAS_INDIRECT_MAIN_INFORMATION}
  105. haltproc(ExitCode);
  106. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  107. End;
  108. Function ParamCount: Longint;
  109. Begin
  110. Paramcount:=argc-1
  111. End;
  112. {function BackPos(c:char; const s: shortstring): integer;
  113. var
  114. i: integer;
  115. Begin
  116. for i:=length(s) downto 0 do
  117. if s[i] = c then break;
  118. if i=0 then
  119. BackPos := 0
  120. else
  121. BackPos := i;
  122. end;}
  123. { variable where full path and filename and executable is stored }
  124. { is setup by the startup of the system unit. }
  125. var
  126. execpathstr : shortstring;
  127. function paramstr(l: longint) : string;
  128. begin
  129. { stricly conforming POSIX applications }
  130. { have the executing filename as argv[0] }
  131. if l=0 then
  132. begin
  133. paramstr := execpathstr;
  134. end
  135. else if (l < argc) then
  136. paramstr:=strpas(argv[l])
  137. else
  138. paramstr:='';
  139. end;
  140. Procedure Randomize;
  141. Begin
  142. randseed:=longint(Fptime(nil));
  143. End;
  144. {*****************************************************************************
  145. cmdline
  146. *****************************************************************************}
  147. procedure SetupCmdLine;
  148. var
  149. bufsize,
  150. len,j,
  151. size,i : longint;
  152. found : boolean;
  153. buf : pchar;
  154. procedure AddBuf;
  155. begin
  156. reallocmem(calculated_cmdline,size+bufsize);
  157. move(buf^,calculated_cmdline[size],bufsize);
  158. inc(size,bufsize);
  159. bufsize:=0;
  160. end;
  161. begin
  162. if argc<=0 then
  163. exit;
  164. GetMem(buf,ARG_MAX);
  165. size:=0;
  166. bufsize:=0;
  167. i:=0;
  168. while (i<argc) do
  169. begin
  170. len:=strlen(argv[i]);
  171. if len>ARG_MAX-2 then
  172. len:=ARG_MAX-2;
  173. found:=false;
  174. for j:=1 to len do
  175. if argv[i][j]=' ' then
  176. begin
  177. found:=true;
  178. break;
  179. end;
  180. found:=found or (len=0); // also quote if len=0, bug 19114
  181. if bufsize+len>=ARG_MAX-2 then
  182. AddBuf;
  183. if found then
  184. begin
  185. buf[bufsize]:='"';
  186. inc(bufsize);
  187. end;
  188. if len>0 then
  189. begin
  190. move(argv[i]^,buf[bufsize],len);
  191. inc(bufsize,len);
  192. end;
  193. if found then
  194. begin
  195. buf[bufsize]:='"';
  196. inc(bufsize);
  197. end;
  198. if i<argc-1 then
  199. buf[bufsize]:=' '
  200. else
  201. buf[bufsize]:=#0;
  202. inc(bufsize);
  203. inc(i);
  204. end;
  205. AddBuf;
  206. FreeMem(buf,ARG_MAX);
  207. end;
  208. function get_cmdline:Pchar;
  209. begin
  210. if calculated_cmdline=nil then
  211. setupcmdline;
  212. get_cmdline:=calculated_cmdline;
  213. end;
  214. {*****************************************************************************
  215. SystemUnit Initialization
  216. *****************************************************************************}
  217. function reenable_signal(sig : longint) : boolean;
  218. var
  219. e : TSigSet;
  220. i,j : byte;
  221. olderrno: cint;
  222. begin
  223. fillchar(e,sizeof(e),#0);
  224. { set is 1 based PM }
  225. dec(sig);
  226. i:=sig mod (sizeof(cuLong) * 8);
  227. j:=sig div (sizeof(cuLong) * 8);
  228. e[j]:=1 shl i;
  229. { this routine is called from a signal handler, so must not change errno }
  230. olderrno:=geterrno;
  231. fpsigprocmask(SIG_UNBLOCK,@e,nil);
  232. reenable_signal:=geterrno=0;
  233. seterrno(olderrno);
  234. end;
  235. // signal handler is arch dependant due to processorexception to language
  236. // exception translation
  237. {$i sighnd.inc}
  238. procedure InstallDefaultSignalHandler(signum: longint; out oldact: SigActionRec); public name '_FPC_INSTALLDEFAULTSIGHANDLER';
  239. var
  240. act: SigActionRec;
  241. begin
  242. { Initialize the sigaction structure }
  243. { all flags and information set to zero }
  244. FillChar(act, sizeof(SigActionRec),0);
  245. { initialize handler }
  246. act.sa_handler := SigActionHandler(@SignalToRunError);
  247. act.sa_flags:=SA_SIGINFO;
  248. FpSigAction(signum,@act,@oldact);
  249. end;
  250. var
  251. oldsigfpe: SigActionRec; public name '_FPC_OLDSIGFPE';
  252. oldsigsegv: SigActionRec; public name '_FPC_OLDSIGSEGV';
  253. oldsigbus: SigActionRec; public name '_FPC_OLDSIGBUS';
  254. oldsigill: SigActionRec; public name '_FPC_OLDSIGILL';
  255. Procedure InstallSignals;
  256. begin
  257. InstallDefaultSignalHandler(SIGFPE,oldsigfpe);
  258. InstallDefaultSignalHandler(SIGSEGV,oldsigsegv);
  259. InstallDefaultSignalHandler(SIGBUS,oldsigbus);
  260. InstallDefaultSignalHandler(SIGILL,oldsigill);
  261. end;
  262. procedure SysInitStdIO;
  263. begin
  264. OpenStdIO(Input,fmInput,StdInputHandle);
  265. OpenStdIO(Output,fmOutput,StdOutputHandle);
  266. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  267. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  268. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  269. end;
  270. Procedure RestoreOldSignalHandlers;
  271. begin
  272. FpSigAction(SIGFPE,@oldsigfpe,nil);
  273. FpSigAction(SIGSEGV,@oldsigsegv,nil);
  274. FpSigAction(SIGBUS,@oldsigbus,nil);
  275. FpSigAction(SIGILL,@oldsigill,nil);
  276. end;
  277. procedure SysInitExecPath;
  278. var
  279. i : longint;
  280. begin
  281. execpathstr[0]:=#0;
  282. i:=Fpreadlink('/proc/self/exe',@execpathstr[1],high(execpathstr));
  283. { it must also be an absolute filename, linux 2.0 points to a memory
  284. location so this will skip that }
  285. if (i>0) and (execpathstr[1]='/') then
  286. execpathstr[0]:=char(i);
  287. end;
  288. function GetProcessID: SizeUInt;
  289. begin
  290. GetProcessID := SizeUInt (fpGetPID);
  291. end;
  292. {$ifdef FPC_USE_LIBC}
  293. {$ifdef HAS_UGETRLIMIT}
  294. { there is no ugetrlimit libc call, just map it to the getrlimit call in these cases }
  295. function FpUGetRLimit(resource : cInt; rlim : PRLimit) : cInt; cdecl; external clib name 'getrlimit';
  296. {$endif}
  297. {$endif}
  298. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  299. var
  300. limits : TRLimit;
  301. success : boolean;
  302. begin
  303. success := false;
  304. fillchar(limits, sizeof(limits), 0);
  305. {$ifdef has_ugetrlimit}
  306. success := fpugetrlimit(RLIMIT_STACK, @limits)=0;
  307. {$endif}
  308. {$ifndef NO_SYSCALL_GETRLIMIT}
  309. if (not success) then
  310. success := fpgetrlimit(RLIMIT_STACK, @limits)=0;
  311. {$endif}
  312. if (success) and (limits.rlim_cur < stklen) then
  313. result := limits.rlim_cur
  314. else
  315. result := stklen;
  316. end;
  317. begin
  318. {$if defined(i386) and not defined(FPC_USE_LIBC)}
  319. InitSyscallIntf;
  320. {$endif}
  321. {$ifndef FPUNONE}
  322. {$if defined(cpupowerpc)}
  323. // some PPC kernels set the exception bits FE0/FE1 in the MSR to zero,
  324. // disabling all FPU exceptions. Enable them again.
  325. fpprctl(PR_SET_FPEXC, PR_FP_EXC_PRECISE);
  326. {$endif}
  327. {$endif}
  328. IsConsole := TRUE;
  329. StackLength := CheckInitialStkLen(initialStkLen);
  330. StackBottom := initialstkptr - StackLength;
  331. { Set up signals handlers (may be needed by init code to test cpu features) }
  332. InstallSignals;
  333. {$if defined(cpui386) or defined(cpuarm)}
  334. fpc_cpucodeinit;
  335. {$endif cpui386}
  336. { Setup heap }
  337. InitHeap;
  338. SysInitExceptions;
  339. initunicodestringmanager;
  340. { Setup stdin, stdout and stderr }
  341. SysInitStdIO;
  342. { Arguments }
  343. SysInitExecPath;
  344. { Reset IO Error }
  345. InOutRes:=0;
  346. { threading }
  347. InitSystemThreads;
  348. initvariantmanager;
  349. { restore original signal handlers in case this is a library }
  350. if IsLibrary then
  351. RestoreOldSignalHandlers;
  352. end.