sysctl.pp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. {$IFNDEF FPC_DOTTEDUNITS}
  2. Unit sysctl;
  3. {$ENDIF FPC_DOTTEDUNITS}
  4. {
  5. This file is part of the Free Pascal run time library.
  6. (c) 2002 by Marco van de Voort
  7. member of the Free Pascal development team.
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. sysctl.h header conversion, taken from FreeBSD 4.6, mainly as base
  11. to implement UNAME on.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY;without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. }
  16. Interface
  17. {$IFDEF FPC_DOTTEDUNITS}
  18. uses
  19. UnixApi.Types;
  20. {$ELSE FPC_DOTTEDUNITS}
  21. uses
  22. unixtype;
  23. {$ENDIF FPC_DOTTEDUNITS}
  24. {$ifndef FPC_USE_LIBC}
  25. {$define FPC_USE_SYSCALL}
  26. {$endif}
  27. {$Packrecords C}
  28. {
  29. * Copyright (c) 1989, 1993
  30. * The Regents of the University of California. All rights reserved.
  31. *
  32. * This code is derived from software contributed to Berkeley by
  33. * Mike Karels at Berkeley Software Design, Inc.
  34. *
  35. * Redistribution and use in source and binary forms, with or without
  36. * modification, are permitted provided that the following conditions
  37. * are met:
  38. * 1. Redistributions of source code must retain the above copyright
  39. * notice, this list of conditions and the following disclaimer.
  40. * 2. Redistributions in binary form must reproduce the above copyright
  41. * notice, this list of conditions and the following disclaimer in the
  42. * documentation and/or other materials provided with the distribution.
  43. * 3. All advertising materials mentioning features or use of this software
  44. * must display the following acknowledgement:
  45. * This product includes software developed by the University of
  46. * California, Berkeley and its contributors.
  47. * 4. Neither the name of the University nor the names of its contributors
  48. * may be used to endorse or promote products derived from this software
  49. * without specific prior written permission.
  50. *
  51. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  52. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  53. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  54. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  55. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  56. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  57. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  58. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  59. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  60. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  61. * SUCH DAMAGE.
  62. *
  63. * @(#)sysctl.h 8.1 (Berkeley) 6/2/93
  64. * $FreeBSD: src/sys/sys/sysctl.h,v 1.81.2.8 2002/03/17 11:08:38 alfred Exp $
  65. }
  66. TYPE CtlNameRec = Record
  67. Name: ansistring; {String[LongestStringInCtlNames]}
  68. CtlType:cint;
  69. end;
  70. {$I sysctlh.inc}
  71. // sysctl only one that is tested. user_definable part of the sysctl
  72. // function is not implemented
  73. //
  74. {$ifdef FPC_USE_LIBC}
  75. {$if defined(VER3_0_0) or defined(VER3_0_2)}
  76. function FPsysctl (Name: PAnsiChar; namelen:cuint; oldp:pointer;oldlenp:psize_t; newp:pointer;newlen:size_t):cint; cdecl; external name 'sysctl';
  77. {$else}
  78. function FPsysctl (Name: pcint; namelen:cuint; oldp:pointer;oldlenp:psize_t; newp:pointer;newlen:size_t):cint; cdecl; external name 'sysctl';
  79. {$endif}
  80. function FPsysctlbyname (Name: PAnsiChar; oldp:pointer;oldlenp:psize_t; newp:pointer;newlen:size_t):cint; cdecl; external name 'sysctlbyname';
  81. function FPsysctlnametomib (Name: PAnsiChar;mibp:pcint;sizep:psize_t):cint; cdecl; external name 'sysctlnametomib';
  82. {$else}
  83. {$if defined(VER3_0_0) or defined(VER3_0_2)}
  84. function FPsysctl (Name: PAnsiChar; namelen:cuint; oldp:pointer;oldlenp:psize_t; newp:pointer;newlen:size_t):cint;
  85. {$else}
  86. function FPsysctl (Name: pcint; namelen:cuint; oldp:pointer;oldlenp:psize_t; newp:pointer;newlen:size_t):cint;
  87. {$endif}
  88. function FPsysctlbyname (Name: PAnsiChar; oldp:pointer;oldlenp:psize_t; newp:pointer;newlen:size_t):cint;
  89. function FPsysctlnametomib (Name: PAnsiChar; mibp:pcint;sizep:psize_t):cint;
  90. {$endif}
  91. Implementation
  92. {$ifndef FPC_USE_LIBC}
  93. {$IFDEF FPC_DOTTEDUNITS}
  94. Uses UnixApi.SysCall;
  95. {$ELSE FPC_DOTTEDUNITS}
  96. Uses Syscall;
  97. {$ENDIF FPC_DOTTEDUNITS}
  98. {$ENDIF}
  99. {$ifndef FPC_USE_LIBC}
  100. {$if defined(FreeBSD) or defined(DragonFly)}
  101. CONST syscall_nr___sysctl = 202;
  102. {$endif}
  103. {$if defined(VER3_0_0) or defined(VER3_0_2)}
  104. function FPsysctl (Name: PAnsiChar; namelen:cuint; oldp:pointer;oldlenp:psize_t; newp:pointer;newlen:size_t):cint;
  105. {$else}
  106. function FPsysctl (Name: pcint; namelen:cuint; oldp:pointer;oldlenp:psize_t; newp:pointer;newlen:size_t):cint;
  107. {$endif}
  108. {$ifdef OpenBSD}
  109. const
  110. syscall_nr___sysctl = syscall_nr_sysctl;
  111. {$endif OpenBSD}
  112. Begin
  113. if (pcint(name)[0] <> CTL_USER) Then
  114. exit(do_syscall(syscall_nr___sysctl,TSysParam(name), namelen, TSysParam(oldp), TSysParam(oldlenp), TSysParam(newp), TSysParam(newlen)))
  115. else
  116. Exit(0);
  117. End;
  118. function FPsysctlbyname (Name: PAnsiChar; oldp:pointer;oldlenp:psize_t; newp:pointer;newlen:size_t):cint;
  119. Var
  120. name2oid_oid : array[0..1] of cint;
  121. real_oid : array[0..CTL_MAXNAME+1] of cint;
  122. error : cint;
  123. oidlen : size_t;
  124. Begin
  125. name2oid_oid[0] := 0; {This is magic & undocumented! }
  126. name2oid_oid[1] := 3;
  127. oidlen := sizeof(real_oid);
  128. error := FPsysctl(@name2oid_oid, 2, @real_oid, @oidlen, name,
  129. strlen(name));
  130. if (error < 0) Then
  131. Exit(error);
  132. oidlen := Oidlen DIV sizeof (cint);
  133. error := FPsysctl(@real_oid, oidlen, oldp, oldlenp, newp, newlen);
  134. exit(error);
  135. End;
  136. function FPsysctlnametomib (Name: PAnsiChar; mibp:pcint;sizep:psize_t):cint;
  137. Var oid : array[0..1] OF cint;
  138. error : cint;
  139. Begin
  140. oid[0] := 0;
  141. oid[1] := 3;
  142. sizep^:=sizep^*sizeof(cint);
  143. error := FPsysctl(@oid, 2, mibp, sizep, name, strlen(name));
  144. sizep^ := sizep^ div sizeof (cint);
  145. if (error < 0) Then
  146. Exit (error);
  147. FPsysctlnametomib:=0;
  148. End;
  149. {$endif}
  150. end.