system.pp 7.6 KB

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