3 # Copyright 2009 The Go Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style
5 # license that can be found in the LICENSE file.
9 # This shell script creates the sysinfo.go file which holds types and
10 # constants extracted from the system header files. This relies on a
11 # hook in gcc: the -fdump-go-spec option will generate debugging
12 # information in Go syntax.
14 # We currently #include all the files at once, which works, but leads
15 # to exposing some names which ideally should not be exposed, as they
16 # match grep patterns. E.g., WCHAR_MIN gets exposed because it starts
17 # with W, like the wait flags.
29 #define _LARGEFILE_SOURCE
30 #define _FILE_OFFSET_BITS 64
32 #include <sys/types.h>
36 #include <netinet/in.h>
37 /* <netinet/tcp.h> needs u_char/u_short, but <sys/bsd_types> is only
38 included by <netinet/in.h> if _SGIAPI (i.e. _SGI_SOURCE
41 #include <sys/bsd_types.h>
43 #include <netinet/tcp.h>
45 #if defined(HAVE_SYSCALL_H)
48 #if defined(HAVE_SYS_SYSCALL_H)
49 #include <sys/syscall.h>
51 #if defined(HAVE_SYS_EPOLL_H)
52 #include <sys/epoll.h>
54 #if defined(HAVE_SYS_MMAN_H)
57 #if defined(HAVE_SYS_PTRACE_H)
58 #include <sys/ptrace.h>
60 #include <sys/resource.h>
62 #include <sys/socket.h>
67 #if defined(HAVE_SYS_USER_H)
70 #if defined(HAVE_SYS_UTSNAME_H)
71 #include <sys/utsname.h>
73 #if defined(HAVE_SYS_SELECT_H)
74 #include <sys/select.h>
79 ${CC} -fdump-go-spec=gen-sysinfo.go
-std=gnu99
-S -o sysinfo.s sysinfo.c
81 echo 'package syscall' > ${OUT}
83 # Get all the consts and types, skipping ones which could not be
84 # represented in Go and ones which we need to rewrite. We also skip
85 # function declarations, as we don't need them here. All the symbols
86 # will all have a leading underscore.
87 grep -v '^// ' gen-sysinfo.go | \
89 grep -v '^type _timeval ' | \
90 grep -v '^type _timespec_t ' | \
91 grep -v '^type _timespec ' | \
92 grep -v '^type _timestruc_t ' | \
93 grep -v '^type _epoll_' | \
94 grep -v 'in6_addr' | \
95 grep -v 'sockaddr_in6' | \
96 sed -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
97 -e 's/\([^a-zA-Z0-9_]\)_timespec_t\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
98 -e 's/\([^a-zA-Z0-9_]\)_timespec\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
99 -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
102 # The errno constants.
103 grep '^const _E' gen-sysinfo.go | \
104 sed -e 's/^\(const \)_\(E[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
107 egrep '^const _(O|F|FD)_' gen-sysinfo.go | \
108 sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
109 if ! grep '^const O_ASYNC' ${OUT} >/dev
/null
2>&1; then
110 echo "const O_ASYNC = 0" >> ${OUT}
112 if ! grep '^const O_CLOEXEC' ${OUT} >/dev
/null
2>&1; then
113 echo "const O_CLOEXEC = 0" >> ${OUT}
116 # The signal numbers.
117 grep '^const _SIG[^_]' gen-sysinfo.go | \
118 grep -v '^const _SIGEV_' | \
119 sed -e 's/^\(const \)_\(SIG[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
121 # The syscall numbers. We force the names to upper case.
122 grep '^const _SYS_' gen-sysinfo.go | \
123 sed -e 's/const _\(SYS_[^= ]*\).*$/\1/' | \
125 sup
=`echo $sys | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
126 echo "const $sup = _$sys" >> ${OUT}
130 grep '^const _S_' gen-sysinfo.go | \
131 sed -e 's/^\(const \)_\(S_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
134 grep '^const _PROT_' gen-sysinfo.go | \
135 sed -e 's/^\(const \)_\(PROT_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
136 grep '^const _MAP_' gen-sysinfo.go | \
137 sed -e 's/^\(const \)_\(MAP_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
139 # Process status constants.
140 grep '^const _W' gen-sysinfo.go |
141 sed -e 's/^\(const \)_\(W[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
142 # WSTOPPED was introduced in glibc 2.3.4.
143 if ! grep '^const _WSTOPPED = ' gen-sysinfo.go
>/dev
/null
2>&1; then
144 if grep '^const _WUNTRACED = ' gen-sysinfo.go
> /dev
/null
2>&1; then
145 echo 'const WSTOPPED = _WUNTRACED' >> ${OUT}
147 echo 'const WSTOPPED = 2' >> ${OUT}
150 if grep '^const ___WALL = ' gen-sysinfo.go
>/dev
/null
2>&1 \
151 && ! grep '^const _WALL = ' gen-sysinfo.go
>/dev
/null
2>&1; then
152 echo 'const WALL = ___WALL' >> ${OUT}
155 # Networking constants.
156 egrep '^const _(AF|SOCK|SOL|SO|IPPROTO|TCP|IP|IPV6)_' gen-sysinfo.go |
157 sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
158 grep '^const _SOMAXCONN' gen-sysinfo.go |
159 sed -e 's/^\(const \)_\(SOMAXCONN[^= ]*\)\(.*\)$/\1\2 = _\2/' \
161 grep '^const _SHUT_' gen-sysinfo.go |
162 sed -e 's/^\(const \)_\(SHUT[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
164 # The net package requires a definition for IPV6ONLY.
165 if ! grep '^const IPV6_V6ONLY ' ${OUT} >/dev
/null
2>&1; then
166 echo "const IPV6_V6ONLY = 0" >> ${OUT}
169 # pathconf constants.
170 grep '^const __PC' gen-sysinfo.go |
171 sed -e 's/^\(const \)__\(PC[^= ]*\)\(.*\)$/\1\2 = __\2/' >> ${OUT}
173 # The epoll constants were picked up by the errno constants, but we
174 # need to be sure the EPOLLRDHUP is defined.
175 if ! grep '^const EPOLLRDHUP' ${OUT} >/dev
/null
2>&1; then
176 echo "const EPOLLRDHUP = 0x2000" >> ${OUT}
179 # Ptrace constants. We don't expose all the PTRACE flags, just the
180 # PTRACE_O_xxx and PTRACE_EVENT_xxx ones.
181 grep '^const _PTRACE_O' gen-sysinfo.go |
182 sed -e 's/^\(const \)_\(PTRACE_O[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
183 grep '^const _PTRACE_EVENT' gen-sysinfo.go |
184 sed -e 's/^\(const \)_\(PTRACE_EVENT[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
185 # We need PTRACE_SETOPTIONS and PTRACE_GETEVENTMSG, but they are not
186 # defined in older versions of glibc.
187 if ! grep '^const _PTRACE_SETOPTIONS' ${OUT} > /dev
/null
2>&1; then
188 echo "const _PTRACE_SETOPTIONS = 0x4200" >> ${OUT}
190 if ! grep '^const PTRACE_O_TRACESYSGOOD' ${OUT} > /dev
/null
2>&1; then
191 echo "const PTRACE_O_TRACESYSGOOD = 0x1" >> ${OUT}
193 if ! grep '^const PTRACE_O_TRACEFORK' ${OUT} > /dev
/null
2>&1; then
194 echo "const PTRACE_O_TRACEFORK = 0x2" >> ${OUT}
196 if ! grep '^const PTRACE_O_TRACEVFORK' ${OUT} > /dev
/null
2>&1; then
197 echo "const PTRACE_O_TRACEVFORK = 0x4" >> ${OUT}
199 if ! grep '^const PTRACE_O_TRACECLONE' ${OUT} > /dev
/null
2>&1; then
200 echo "const PTRACE_O_TRACECLONE = 0x8" >> ${OUT}
202 if ! grep '^const PTRACE_O_TRACEEXEC' ${OUT} > /dev
/null
2>&1; then
203 echo "const PTRACE_O_TRACEEXEC = 0x10" >> ${OUT}
205 if ! grep '^const PTRACE_O_TRACEVFORKDONE' ${OUT} > /dev
/null
2>&1; then
206 echo "const PTRACE_O_TRACEVFORKDONE = 0x20" >> ${OUT}
208 if ! grep '^const PTRACE_O_TRACEEXIT' ${OUT} > /dev
/null
2>&1; then
209 echo "const PTRACE_O_TRACEEXIT = 0x40" >> ${OUT}
211 if ! grep '^const PTRACE_O_MASK' ${OUT} > /dev
/null
2>&1; then
212 echo "const PTRACE_O_MASK = 0x7f" >> ${OUT}
214 if ! grep '^const _PTRACE_GETEVENTMSG' ${OUT} > /dev
/null
2>&1; then
215 echo "const _PTRACE_GETEVENTMSG = 0x4201" >> ${OUT}
217 if ! grep '^const PTRACE_EVENT_FORK' ${OUT} > /dev
/null
2>&1; then
218 echo "const PTRACE_EVENT_FORK = 1" >> ${OUT}
220 if ! grep '^const PTRACE_EVENT_VFORK' ${OUT} > /dev
/null
2>&1; then
221 echo "const PTRACE_EVENT_VFORK = 2" >> ${OUT}
223 if ! grep '^const PTRACE_EVENT_CLONE' ${OUT} > /dev
/null
2>&1; then
224 echo "const PTRACE_EVENT_CLONE = 3" >> ${OUT}
226 if ! grep '^const PTRACE_EVENT_EXEC' ${OUT} > /dev
/null
2>&1; then
227 echo "const PTRACE_EVENT_EXEC = 4" >> ${OUT}
229 if ! grep '^const PTRACE_EVENT_VFORK_DONE' ${OUT} > /dev
/null
2>&1; then
230 echo "const PTRACE_EVENT_VFORK_DONE = 5" >> ${OUT}
232 if ! grep '^const PTRACE_EVENT_EXIT' ${OUT} > /dev
/null
2>&1; then
233 echo "const PTRACE_EVENT_EXIT = 6" >> ${OUT}
235 if ! grep '^const _PTRACE_TRACEME' ${OUT} > /dev
/null
2>&1; then
236 echo "const _PTRACE_TRACEME = 0" >> ${OUT}
239 # The registers returned by PTRACE_GETREGS. This is probably
240 # GNU/Linux specific; it should do no harm if there is no
242 regs
=`grep '^type _user_regs_struct struct' gen-sysinfo.go || true`
243 if test "$regs" != ""; then
244 regs
=`echo $regs | sed -e 's/type _user_regs_struct struct //' -e 's/[{}]//g'`
245 regs
=`echo $regs | sed -e s'/^ *//'`
247 while test -n "$regs"; do
248 field
=`echo $regs | sed -e 's/^\([^;]*\);.*$/\1/'`
249 regs
=`echo $regs | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
250 # Capitalize the first character of the field.
251 f
=`echo $field | sed -e 's/^\(.\).*$/\1/'`
252 r
=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
253 f
=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
255 nregs
="$nregs $field;"
257 echo "type PtraceRegs struct {$nregs }" >> ${OUT}
261 echo 'type Size_t _size_t' >> ${OUT}
262 echo "type Ssize_t _ssize_t" >> ${OUT}
263 if grep '^const _HAVE_OFF64_T = ' gen-sysinfo.go
> /dev
/null
2>&1; then
264 echo "type Offset_t _off64_t" >> ${OUT}
266 echo "type Offset_t _off_t" >> ${OUT}
268 echo "type Mode_t _mode_t" >> ${OUT}
269 echo "type Pid_t _pid_t" >> ${OUT}
270 echo "type Uid_t _uid_t" >> ${OUT}
271 echo "type Gid_t _gid_t" >> ${OUT}
272 echo "type Socklen_t _socklen_t" >> ${OUT}
274 # The long type, needed because that is the type that ptrace returns.
275 sizeof_long
=`grep '^const ___SIZEOF_LONG__ = ' gen-sysinfo.go | sed -e 's/.*= //'`
276 if test "$sizeof_long" = "4"; then
277 echo "type _C_long int32" >> ${OUT}
278 elif test "$sizeof_long" = "8"; then
279 echo "type _C_long int64" >> ${OUT}
281 echo 1>&2 "mksysinfo.sh: could not determine size of long (got $sizeof_long)"
285 # Solaris 2 needs _u?pad128_t, but its default definition in terms of long
286 # double is commented by -fdump-go-spec.
287 if grep "^// type _pad128_t" gen-sysinfo.go
> /dev
/null
2>&1; then
288 echo "type _pad128_t struct { _l [4]int32; }" >> ${OUT}
290 if grep "^// type _upad128_t" gen-sysinfo.go
> /dev
/null
2>&1; then
291 echo "type _upad128_t struct { _l [4]uint32; }" >> ${OUT}
294 # The time structures need special handling: we need to name the
295 # types, so that we can cast integers to the right types when
296 # assigning to the structures.
297 timeval
=`grep '^type _timeval ' gen-sysinfo.go`
298 timeval_sec
=`echo $timeval | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
299 timeval_usec
=`echo $timeval | sed -n -e 's/^.*tv_usec \([^ ]*\);.*$/\1/p'`
300 echo "type Timeval_sec_t $timeval_sec" >> ${OUT}
301 echo "type Timeval_usec_t $timeval_usec" >> ${OUT}
303 sed -e 's/type _timeval /type Timeval /' \
304 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timeval_sec_t/' \
305 -e 's/tv_usec *[a-zA-Z0-9_]*/Usec Timeval_usec_t/' >> ${OUT}
306 timespec
=`grep '^type _timespec ' gen-sysinfo.go || true`
307 if test "$timespec" = ""; then
308 # IRIX 6.5 has __timespec instead.
309 timespec
=`grep '^type ___timespec ' gen-sysinfo.go || true`
311 timespec_sec
=`echo $timespec | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
312 timespec_nsec
=`echo $timespec | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
313 echo "type Timespec_sec_t $timespec_sec" >> ${OUT}
314 echo "type Timespec_nsec_t $timespec_nsec" >> ${OUT}
316 sed -e 's/^type ___timespec /type Timespec /' \
317 -e 's/^type _timespec /type Timespec /' \
318 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timespec_sec_t/' \
319 -e 's/tv_nsec *[a-zA-Z0-9_]*/Nsec Timespec_nsec_t/' >> ${OUT}
321 timestruc
=`grep '^type _timestruc_t ' gen-sysinfo.go || true`
322 if test "$timestruc" != ""; then
323 timestruc_sec
=`echo $timestruc | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
324 timestruc_nsec
=`echo $timestruc | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
325 echo "type Timestruc_sec_t $timestruc_sec" >> ${OUT}
326 echo "type Timestruc_nsec_t $timestruc_nsec" >> ${OUT}
328 sed -e 's/^type _timestruc_t /type Timestruc /' \
329 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timestruc_sec_t/' \
330 -e 's/tv_nsec *[a-zA-Z0-9_]*/Nsec Timestruc_nsec_t/' >> ${OUT}
334 # Prefer largefile variant if available.
335 stat
=`grep '^type _stat64 ' gen-sysinfo.go || true`
336 if test "$stat" != ""; then
337 grep '^type _stat64 ' gen-sysinfo.go
339 grep '^type _stat ' gen-sysinfo.go
340 fi |
sed -e 's/type _stat64/type Stat_t/' \
341 -e 's/type _stat/type Stat_t/' \
343 -e 's/st_ino/Ino/g' \
344 -e 's/st_nlink/Nlink/' \
345 -e 's/st_mode/Mode/' \
348 -e 's/st_rdev/Rdev/' \
349 -e 's/st_size/Size/' \
350 -e 's/st_blksize/Blksize/' \
351 -e 's/st_blocks/Blocks/' \
352 -e 's/st_atim/Atime/' \
353 -e 's/st_mtim/Mtime/' \
354 -e 's/st_ctim/Ctime/' \
355 -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
356 -e 's/\([^a-zA-Z0-9_]\)_timespec_t\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
357 -e 's/\([^a-zA-Z0-9_]\)_timespec\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
358 -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
361 # The directory searching types.
362 # Prefer largefile variant if available.
363 dirent
=`grep '^type _dirent64 ' gen-sysinfo.go || true`
364 if test "$dirent" != ""; then
365 grep '^type _dirent64 ' gen-sysinfo.go
367 grep '^type _dirent ' gen-sysinfo.go
368 fi |
sed -e 's/type _dirent64/type Dirent/' \
369 -e 's/type _dirent/type Dirent/' \
370 -e 's/d_name \[0+1\]/d_name [0+256]/' \
371 -e 's/d_name/Name/' \
372 -e 's/]int8/]byte/' \
375 -e 's/d_reclen/Reclen/' \
376 -e 's/d_type/Type/' \
378 echo "type DIR _DIR" >> ${OUT}
381 rusage
=`grep '^type _rusage struct' gen-sysinfo.go`
382 if test "$rusage" != ""; then
383 rusage
=`echo $rusage | sed -e 's/type _rusage struct //' -e 's/[{}]//g'`
384 rusage
=`echo $rusage | sed -e 's/^ *//'`
386 while test -n "$rusage"; do
387 field
=`echo $rusage | sed -e 's/^\([^;]*\);.*$/\1/'`
388 rusage
=`echo $rusage | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
389 # Drop the leading ru_, capitalize the next character.
390 field
=`echo $field | sed -e 's/^ru_//'`
391 f
=`echo $field | sed -e 's/^\(.\).*$/\1/'`
392 r
=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
393 f
=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
394 # Fix _timeval _timespec, and _timestruc_t.
395 r
=`echo $r | sed -e s'/ _timeval$/ Timeval/'`
396 r
=`echo $r | sed -e s'/ _timespec$/ Timespec/'`
397 r
=`echo $r | sed -e s'/ _timestruc_t$/ Timestruc/'`
399 nrusage
="$nrusage $field;"
401 echo "type Rusage struct {$nrusage }" >> ${OUT}
403 echo "type Rusage struct {}" >> ${OUT}
406 # The utsname struct.
407 grep '^type _utsname ' gen-sysinfo.go | \
408 sed -e 's/_utsname/Utsname/' \
409 -e 's/sysname/Sysname/' \
410 -e 's/nodename/Nodename/' \
411 -e 's/release/Release/' \
412 -e 's/version/Version/' \
413 -e 's/machine/Machine/' \
414 -e 's/domainname/Domainname/' \
418 iovec
=`grep '^type _iovec ' gen-sysinfo.go`
419 iovec_len
=`echo $iovec | sed -n -e 's/^.*iov_len \([^ ]*\);.*$/\1/p'`
420 echo "type Iovec_len_t $iovec_len" >> ${OUT}
422 sed -e 's/_iovec/Iovec/' \
423 -e 's/iov_base/Base/' \
424 -e 's/iov_len *[a-zA-Z0-9_]*/Len Iovec_len_t/' \
428 msghdr
=`grep '^type _msghdr ' gen-sysinfo.go`
429 msghdr_controllen
=`echo $msghdr | sed -n -e 's/^.*msg_controllen \([^ ]*\);.*$/\1/p'`
430 echo "type Msghdr_controllen_t $msghdr_controllen" >> ${OUT}
432 sed -e 's/_msghdr/Msghdr/' \
433 -e 's/msg_name/Name/' \
434 -e 's/msg_namelen/Namelen/' \
435 -e 's/msg_iov/Iov/' \
436 -e 's/msg_iovlen/Iovlen/' \
437 -e 's/_iovec/Iovec/' \
438 -e 's/msg_control/Control/' \
439 -e 's/msg_controllen *[a-zA-Z0-9_]*/Controllen Msghdr_controllen_t/' \
440 -e 's/msg_flags/Flags/' \
444 grep '^type _ip_mreq ' gen-sysinfo.go | \
445 sed -e 's/_ip_mreq/IpMreq/' \
446 -e 's/imr_multiaddr/Multiaddr/' \
447 -e 's/imr_interface/Interface/' \
448 -e 's/_in_addr/[4]byte/g' \
451 # Try to guess the type to use for fd_set.
452 fd_set
=`grep '^type _fd_set ' gen-sysinfo.go || true`
453 fds_bits_type
="_C_long"
454 if test "$fd_set" != ""; then
455 fds_bits_type
=`echo $fd_set | sed -e 's/.*[]]\([^;]*\); }$/\1/'`
457 echo "type fds_bits_type $fds_bits_type" >> ${OUT}