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