system.pp 9.8 KB

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