system.pp 6.6 KB

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