system.pp 7.7 KB

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