sysctl.pp 5.4 KB

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