system.pp 7.9 KB

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