system.pp 9.9 KB

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