system.pp 5.8 KB

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