mksysctl_openbsd.pl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #!/usr/bin/env perl
  2. # Copyright 2011 The Go Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style
  4. # license that can be found in the LICENSE file.
  5. #
  6. # Parse the header files for OpenBSD and generate a Go usable sysctl MIB.
  7. #
  8. # Build a MIB with each entry being an array containing the level, type and
  9. # a hash that will contain additional entries if the current entry is a node.
  10. # We then walk this MIB and create a flattened sysctl name to OID hash.
  11. #
  12. use strict;
  13. if($ENV{'GOARCH'} eq "" || $ENV{'GOOS'} eq "") {
  14. print STDERR "GOARCH or GOOS not defined in environment\n";
  15. exit 1;
  16. }
  17. my $debug = 0;
  18. my %ctls = ();
  19. my @headers = qw (
  20. sys/sysctl.h
  21. sys/socket.h
  22. sys/tty.h
  23. sys/malloc.h
  24. sys/mount.h
  25. sys/namei.h
  26. sys/sem.h
  27. sys/shm.h
  28. sys/vmmeter.h
  29. uvm/uvmexp.h
  30. uvm/uvm_param.h
  31. uvm/uvm_swap_encrypt.h
  32. ddb/db_var.h
  33. net/if.h
  34. net/if_pfsync.h
  35. net/pipex.h
  36. netinet/in.h
  37. netinet/icmp_var.h
  38. netinet/igmp_var.h
  39. netinet/ip_ah.h
  40. netinet/ip_carp.h
  41. netinet/ip_divert.h
  42. netinet/ip_esp.h
  43. netinet/ip_ether.h
  44. netinet/ip_gre.h
  45. netinet/ip_ipcomp.h
  46. netinet/ip_ipip.h
  47. netinet/pim_var.h
  48. netinet/tcp_var.h
  49. netinet/udp_var.h
  50. netinet6/in6.h
  51. netinet6/ip6_divert.h
  52. netinet6/pim6_var.h
  53. netinet/icmp6.h
  54. netmpls/mpls.h
  55. );
  56. my @ctls = qw (
  57. kern
  58. vm
  59. fs
  60. net
  61. #debug # Special handling required
  62. hw
  63. #machdep # Arch specific
  64. user
  65. ddb
  66. #vfs # Special handling required
  67. fs.posix
  68. kern.forkstat
  69. kern.intrcnt
  70. kern.malloc
  71. kern.nchstats
  72. kern.seminfo
  73. kern.shminfo
  74. kern.timecounter
  75. kern.tty
  76. kern.watchdog
  77. net.bpf
  78. net.ifq
  79. net.inet
  80. net.inet.ah
  81. net.inet.carp
  82. net.inet.divert
  83. net.inet.esp
  84. net.inet.etherip
  85. net.inet.gre
  86. net.inet.icmp
  87. net.inet.igmp
  88. net.inet.ip
  89. net.inet.ip.ifq
  90. net.inet.ipcomp
  91. net.inet.ipip
  92. net.inet.mobileip
  93. net.inet.pfsync
  94. net.inet.pim
  95. net.inet.tcp
  96. net.inet.udp
  97. net.inet6
  98. net.inet6.divert
  99. net.inet6.ip6
  100. net.inet6.icmp6
  101. net.inet6.pim6
  102. net.inet6.tcp6
  103. net.inet6.udp6
  104. net.mpls
  105. net.mpls.ifq
  106. net.key
  107. net.pflow
  108. net.pfsync
  109. net.pipex
  110. net.rt
  111. vm.swapencrypt
  112. #vfsgenctl # Special handling required
  113. );
  114. # Node name "fixups"
  115. my %ctl_map = (
  116. "ipproto" => "net.inet",
  117. "net.inet.ipproto" => "net.inet",
  118. "net.inet6.ipv6proto" => "net.inet6",
  119. "net.inet6.ipv6" => "net.inet6.ip6",
  120. "net.inet.icmpv6" => "net.inet6.icmp6",
  121. "net.inet6.divert6" => "net.inet6.divert",
  122. "net.inet6.tcp6" => "net.inet.tcp",
  123. "net.inet6.udp6" => "net.inet.udp",
  124. "mpls" => "net.mpls",
  125. "swpenc" => "vm.swapencrypt"
  126. );
  127. # Node mappings
  128. my %node_map = (
  129. "net.inet.ip.ifq" => "net.ifq",
  130. "net.inet.pfsync" => "net.pfsync",
  131. "net.mpls.ifq" => "net.ifq"
  132. );
  133. my $ctlname;
  134. my %mib = ();
  135. my %sysctl = ();
  136. my $node;
  137. sub debug() {
  138. print STDERR "$_[0]\n" if $debug;
  139. }
  140. # Walk the MIB and build a sysctl name to OID mapping.
  141. sub build_sysctl() {
  142. my ($node, $name, $oid) = @_;
  143. my %node = %{$node};
  144. my @oid = @{$oid};
  145. foreach my $key (sort keys %node) {
  146. my @node = @{$node{$key}};
  147. my $nodename = $name.($name ne '' ? '.' : '').$key;
  148. my @nodeoid = (@oid, $node[0]);
  149. if ($node[1] eq 'CTLTYPE_NODE') {
  150. if (exists $node_map{$nodename}) {
  151. $node = \%mib;
  152. $ctlname = $node_map{$nodename};
  153. foreach my $part (split /\./, $ctlname) {
  154. $node = \%{@{$$node{$part}}[2]};
  155. }
  156. } else {
  157. $node = $node[2];
  158. }
  159. &build_sysctl($node, $nodename, \@nodeoid);
  160. } elsif ($node[1] ne '') {
  161. $sysctl{$nodename} = \@nodeoid;
  162. }
  163. }
  164. }
  165. foreach my $ctl (@ctls) {
  166. $ctls{$ctl} = $ctl;
  167. }
  168. # Build MIB
  169. foreach my $header (@headers) {
  170. &debug("Processing $header...");
  171. open HEADER, "/usr/include/$header" ||
  172. print STDERR "Failed to open $header\n";
  173. while (<HEADER>) {
  174. if ($_ =~ /^#define\s+(CTL_NAMES)\s+{/ ||
  175. $_ =~ /^#define\s+(CTL_(.*)_NAMES)\s+{/ ||
  176. $_ =~ /^#define\s+((.*)CTL_NAMES)\s+{/) {
  177. if ($1 eq 'CTL_NAMES') {
  178. # Top level.
  179. $node = \%mib;
  180. } else {
  181. # Node.
  182. my $nodename = lc($2);
  183. if ($header =~ /^netinet\//) {
  184. $ctlname = "net.inet.$nodename";
  185. } elsif ($header =~ /^netinet6\//) {
  186. $ctlname = "net.inet6.$nodename";
  187. } elsif ($header =~ /^net\//) {
  188. $ctlname = "net.$nodename";
  189. } else {
  190. $ctlname = "$nodename";
  191. $ctlname =~ s/^(fs|net|kern)_/$1\./;
  192. }
  193. if (exists $ctl_map{$ctlname}) {
  194. $ctlname = $ctl_map{$ctlname};
  195. }
  196. if (not exists $ctls{$ctlname}) {
  197. &debug("Ignoring $ctlname...");
  198. next;
  199. }
  200. # Walk down from the top of the MIB.
  201. $node = \%mib;
  202. foreach my $part (split /\./, $ctlname) {
  203. if (not exists $$node{$part}) {
  204. &debug("Missing node $part");
  205. $$node{$part} = [ 0, '', {} ];
  206. }
  207. $node = \%{@{$$node{$part}}[2]};
  208. }
  209. }
  210. # Populate current node with entries.
  211. my $i = -1;
  212. while (defined($_) && $_ !~ /^}/) {
  213. $_ = <HEADER>;
  214. $i++ if $_ =~ /{.*}/;
  215. next if $_ !~ /{\s+"(\w+)",\s+(CTLTYPE_[A-Z]+)\s+}/;
  216. $$node{$1} = [ $i, $2, {} ];
  217. }
  218. }
  219. }
  220. close HEADER;
  221. }
  222. &build_sysctl(\%mib, "", []);
  223. print <<EOF;
  224. // mksysctl_openbsd.pl
  225. // Code generated by the command above; DO NOT EDIT.
  226. // +build $ENV{'GOARCH'},$ENV{'GOOS'}
  227. package unix;
  228. type mibentry struct {
  229. ctlname string
  230. ctloid []_C_int
  231. }
  232. var sysctlMib = []mibentry {
  233. EOF
  234. foreach my $name (sort keys %sysctl) {
  235. my @oid = @{$sysctl{$name}};
  236. print "\t{ \"$name\", []_C_int{ ", join(', ', @oid), " } }, \n";
  237. }
  238. print <<EOF;
  239. }
  240. EOF