system.pp 9.7 KB

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