system.pp 9.6 KB

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