system.pp 9.7 KB

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