system.pp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. FpSigAction(SIGFPE,@act,nil);
  104. FpSigAction(SIGSEGV,@act,nil);
  105. FpSigAction(SIGBUS,@act,nil);
  106. FpSigAction(SIGILL,@act,nil);
  107. end;
  108. procedure SetupCmdLine;
  109. var
  110. bufsize,
  111. len,j,
  112. size,i : longint;
  113. found : boolean;
  114. buf : pchar;
  115. procedure AddBuf;
  116. begin
  117. reallocmem(cmdline,size+bufsize);
  118. move(buf^,cmdline[size],bufsize);
  119. inc(size,bufsize);
  120. bufsize:=0;
  121. end;
  122. begin
  123. cmdline:=nil;
  124. if i>=argc then
  125. exit;
  126. GetMem(buf,ARG_MAX);
  127. size:=0;
  128. bufsize:=0;
  129. i:=0;
  130. while (i<argc) do
  131. begin
  132. len:=strlen(argv[i]);
  133. if len>ARG_MAX-2 then
  134. len:=ARG_MAX-2;
  135. found:=false;
  136. for j:=1 to len do
  137. if argv[i][j]=' ' then
  138. begin
  139. found:=true;
  140. break;
  141. end;
  142. if bufsize+len>=ARG_MAX-2 then
  143. AddBuf;
  144. if found then
  145. begin
  146. buf[bufsize]:='"';
  147. inc(bufsize);
  148. end;
  149. move(argv[i]^,buf[bufsize],len);
  150. inc(bufsize,len);
  151. if found then
  152. begin
  153. buf[bufsize]:='"';
  154. inc(bufsize);
  155. end;
  156. if i<argc then
  157. buf[bufsize]:=' '
  158. else
  159. buf[bufsize]:=#0;
  160. inc(bufsize);
  161. inc(i);
  162. end;
  163. AddBuf;
  164. FreeMem(buf,ARG_MAX);
  165. end;
  166. procedure SysInitStdIO;
  167. begin
  168. OpenStdIO(Input,fmInput,StdInputHandle);
  169. OpenStdIO(Output,fmOutput,StdOutputHandle);
  170. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  171. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  172. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  173. end;
  174. procedure SysInitExecPath;
  175. var
  176. i : longint;
  177. begin
  178. execpathstr[0]:=#0;
  179. i:=Fpreadlink('/proc/self/exe',@execpathstr[1],high(execpathstr));
  180. { it must also be an absolute filename, linux 2.0 points to a memory
  181. location so this will skip that }
  182. if (i>0) and (execpathstr[1]='/') then
  183. execpathstr[0]:=char(i);
  184. end;
  185. function GetProcessID: SizeUInt;
  186. begin
  187. GetProcessID := SizeUInt (fpGetPID);
  188. end;
  189. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  190. var
  191. limits : TRLimit;
  192. success : boolean;
  193. begin
  194. success := false;
  195. fillchar(limits, sizeof(limits), 0);
  196. {$ifdef has_ugetrlimit}
  197. success := fpugetrlimit(RLIMIT_STACK, @limits)=0;
  198. {$endif}
  199. if (not success) then
  200. success := fpgetrlimit(RLIMIT_STACK, @limits)=0;
  201. if (success) and (limits.rlim_cur < stklen) then
  202. result := limits.rlim_cur
  203. else
  204. result := stklen;
  205. end;
  206. var
  207. initialstkptr : Pointer;external name '__stkptr';
  208. Begin
  209. SysResetFPU;
  210. IsConsole := TRUE;
  211. StackLength := CheckInitialStkLen(initialStkLen);
  212. StackBottom := initialstkptr - StackLength;
  213. { Set up signals handlers }
  214. InstallSignals;
  215. { Setup heap }
  216. InitHeap;
  217. SysInitExceptions;
  218. { Setup stdin, stdout and stderr }
  219. SysInitStdIO;
  220. { Arguments }
  221. SetupCmdLine;
  222. SysInitExecPath;
  223. { Reset IO Error }
  224. InOutRes:=0;
  225. { threading }
  226. InitSystemThreads;
  227. initvariantmanager;
  228. initwidestringmanager;
  229. End.