system.pp 9.5 KB

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