system.pp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. 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. procedure SysInitStdIO;
  164. begin
  165. OpenStdIO(Input,fmInput,StdInputHandle);
  166. OpenStdIO(Output,fmOutput,StdOutputHandle);
  167. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  168. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  169. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  170. end;
  171. procedure SysInitExecPath;
  172. var
  173. i : longint;
  174. begin
  175. execpathstr[0]:=#0;
  176. i:=Fpreadlink('/proc/self/exe',@execpathstr[1],high(execpathstr));
  177. { it must also be an absolute filename, linux 2.0 points to a memory
  178. location so this will skip that }
  179. if (i>0) and (execpathstr[1]='/') then
  180. execpathstr[0]:=char(i);
  181. end;
  182. function GetProcessID: SizeUInt;
  183. begin
  184. GetProcessID := SizeUInt (fpGetPID);
  185. end;
  186. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  187. var
  188. limits : TRLimit;
  189. success : boolean;
  190. begin
  191. success := false;
  192. fillchar(limits, sizeof(limits), 0);
  193. {$ifdef has_ugetrlimit}
  194. success := fpugetrlimit(RLIMIT_STACK, @limits)=0;
  195. {$endif}
  196. if (not success) then
  197. success := fpgetrlimit(RLIMIT_STACK, @limits)=0;
  198. if (success) and (limits.rlim_cur < stklen) then
  199. result := limits.rlim_cur
  200. else
  201. result := stklen;
  202. end;
  203. var
  204. initialstkptr : Pointer;external name '__stkptr';
  205. Begin
  206. SysResetFPU;
  207. IsConsole := TRUE;
  208. IsLibrary := FALSE;
  209. StackLength := CheckInitialStkLen(initialStkLen);
  210. StackBottom := initialstkptr - StackLength;
  211. { Set up signals handlers }
  212. InstallSignals;
  213. { Setup heap }
  214. InitHeap;
  215. SysInitExceptions;
  216. { Setup stdin, stdout and stderr }
  217. SysInitStdIO;
  218. { Arguments }
  219. SetupCmdLine;
  220. SysInitExecPath;
  221. { Reset IO Error }
  222. InOutRes:=0;
  223. { threading }
  224. InitSystemThreads;
  225. initvariantmanager;
  226. initwidestringmanager;
  227. End.