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
33 /* IRIX 6 needs _XOPEN_SOURCE=500 for the XPG5 version of struct msghdr in
35 #define _XOPEN_SOURCE 500
38 #include <sys/types.h>
42 #include <netinet/in.h>
43 /* <netinet/tcp.h> needs u_char/u_short, but <sys/bsd_types> is only
44 included by <netinet/in.h> if _SGIAPI (i.e. _SGI_SOURCE
47 #include <sys/bsd_types.h>
49 #include <netinet/tcp.h>
51 #if defined(HAVE_SYSCALL_H)
54 #if defined(HAVE_SYS_SYSCALL_H)
55 #include <sys/syscall.h>
57 #if defined(HAVE_SYS_EPOLL_H)
58 #include <sys/epoll.h>
60 #if defined(HAVE_SYS_PTRACE_H)
61 #include <sys/ptrace.h>
63 #include <sys/resource.h>
65 #include <sys/socket.h>
70 #if defined(HAVE_SYS_USER_H)
73 #if defined(HAVE_SYS_UTSNAME_H)
74 #include <sys/utsname.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 _timestruc_t ' | \
92 grep -v '^type _epoll_' | \
93 grep -v 'in6_addr' | \
94 grep -v 'sockaddr_in6' | \
95 sed -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
96 -e 's/\([^a-zA-Z0-9_]\)_timespec\(_t\)\?\([^a-zA-Z0-9_]\)/\1Timespec\3/g' \
97 -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
100 # The errno constants.
101 grep '^const _E' gen-sysinfo.go | \
102 sed -e 's/^\(const \)_\(E[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
105 egrep '^const _(O|F|FD)_' gen-sysinfo.go | \
106 sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
107 if ! grep '^const O_ASYNC' ${OUT} >/dev/null 2>&1; then
108 echo "const O_ASYNC = 0" >> ${OUT}
110 if ! grep '^const O_CLOEXEC' ${OUT} >/dev/null 2>&1; then
111 echo "const O_CLOEXEC = 0" >> ${OUT}
114 # The signal numbers.
115 grep '^const _SIG[^_]' gen-sysinfo.go | \
116 grep -v '^const _SIGEV_' | \
117 sed -e 's/^\(const \)_\(SIG[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
119 # The syscall numbers. We force the names to upper case.
120 grep '^const _SYS_' gen-sysinfo.go | \
121 sed -e 's/const _\(SYS_[^= ]*\).*$/\1/' | \
123 sup=`echo $sys | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
124 echo "const $sup = _$sys" >> ${OUT}
128 grep '^const _S_' gen-sysinfo.go | \
129 sed -e 's/^\(const \)_\(S_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
131 # Process status constants.
132 grep '^const _W' gen-sysinfo.go |
133 sed -e 's/^\(const \)_\(W[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
134 # WSTOPPED was introduced in glibc 2.3.4.
135 if ! grep '^const _WSTOPPED = ' gen-sysinfo.go >/dev/null 2>&1; then
136 if grep '^const _WUNTRACED = ' gen-sysinfo.go > /dev/null 2>&1; then
137 echo 'const WSTOPPED = _WUNTRACED' >> ${OUT}
139 echo 'const WSTOPPED = 2' >> ${OUT}
142 if grep '^const ___WALL = ' gen-sysinfo.go >/dev/null 2>&1 \
143 && ! grep '^const _WALL = ' gen-sysinfo.go >/dev/null 2>&1; then
144 echo 'const WALL = ___WALL' >> ${OUT}
147 # Networking constants.
148 egrep '^const _(AF|SOCK|SOL|SO|IPPROTO|TCP|IP|IPV6)_' gen-sysinfo.go |
149 sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
150 grep '^const _SOMAXCONN' gen-sysinfo.go |
151 sed -e 's/^\(const \)_\(SOMAXCONN[^= ]*\)\(.*\)$/\1\2 = _\2/' \
153 grep '^const _SHUT_' gen-sysinfo.go |
154 sed -e 's/^\(const \)_\(SHUT[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
156 # The net package requires a definition for IPV6ONLY.
157 if ! grep '^const IPV6_V6ONLY ' ${OUT} >/dev/null 2>&1; then
158 echo "const IPV6_V6ONLY = 0" >> ${OUT}
161 # pathconf constants.
162 grep '^const __PC' gen-sysinfo.go |
163 sed -e 's/^\(const \)__\(PC[^= ]*\)\(.*\)$/\1\2 = __\2/' >> ${OUT}
165 # The epoll constants were picked up by the errno constants, but we
166 # need to be sure the EPOLLRDHUP is defined.
167 if ! grep '^const EPOLLRDHUP' ${OUT} >/dev/null 2>&1; then
168 echo "const EPOLLRDHUP = 0x2000" >> ${OUT}
171 # Ptrace constants. We don't expose all the PTRACE flags, just the
172 # PTRACE_O_xxx and PTRACE_EVENT_xxx ones.
173 grep '^const _PTRACE_O' gen-sysinfo.go |
174 sed -e 's/^\(const \)_\(PTRACE_O[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
175 grep '^const _PTRACE_EVENT' gen-sysinfo.go |
176 sed -e 's/^\(const \)_\(PTRACE_EVENT[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
177 # We need PTRACE_SETOPTIONS and PTRACE_GETEVENTMSG, but they are not
178 # defined in older versions of glibc.
179 if ! grep '^const _PTRACE_SETOPTIONS' ${OUT} > /dev/null 2>&1; then
180 echo "const _PTRACE_SETOPTIONS = 0x4200" >> ${OUT}
182 if ! grep '^const PTRACE_O_TRACESYSGOOD' ${OUT} > /dev/null 2>&1; then
183 echo "const PTRACE_O_TRACESYSGOOD = 0x1" >> ${OUT}
185 if ! grep '^const PTRACE_O_TRACEFORK' ${OUT} > /dev/null 2>&1; then
186 echo "const PTRACE_O_TRACEFORK = 0x2" >> ${OUT}
188 if ! grep '^const PTRACE_O_TRACEVFORK' ${OUT} > /dev/null 2>&1; then
189 echo "const PTRACE_O_TRACEVFORK = 0x4" >> ${OUT}
191 if ! grep '^const PTRACE_O_TRACECLONE' ${OUT} > /dev/null 2>&1; then
192 echo "const PTRACE_O_TRACECLONE = 0x8" >> ${OUT}
194 if ! grep '^const PTRACE_O_TRACEEXEC' ${OUT} > /dev/null 2>&1; then
195 echo "const PTRACE_O_TRACEEXEC = 0x10" >> ${OUT}
197 if ! grep '^const PTRACE_O_TRACEVFORKDONE' ${OUT} > /dev/null 2>&1; then
198 echo "const PTRACE_O_TRACEVFORKDONE = 0x20" >> ${OUT}
200 if ! grep '^const PTRACE_O_TRACEEXIT' ${OUT} > /dev/null 2>&1; then
201 echo "const PTRACE_O_TRACEEXIT = 0x40" >> ${OUT}
203 if ! grep '^const PTRACE_O_MASK' ${OUT} > /dev/null 2>&1; then
204 echo "const PTRACE_O_MASK = 0x7f" >> ${OUT}
206 if ! grep '^const _PTRACE_GETEVENTMSG' ${OUT} > /dev/null 2>&1; then
207 echo "const _PTRACE_GETEVENTMSG = 0x4201" >> ${OUT}
209 if ! grep '^const PTRACE_EVENT_FORK' ${OUT} > /dev/null 2>&1; then
210 echo "const PTRACE_EVENT_FORK = 1" >> ${OUT}
212 if ! grep '^const PTRACE_EVENT_VFORK' ${OUT} > /dev/null 2>&1; then
213 echo "const PTRACE_EVENT_VFORK = 2" >> ${OUT}
215 if ! grep '^const PTRACE_EVENT_CLONE' ${OUT} > /dev/null 2>&1; then
216 echo "const PTRACE_EVENT_CLONE = 3" >> ${OUT}
218 if ! grep '^const PTRACE_EVENT_EXEC' ${OUT} > /dev/null 2>&1; then
219 echo "const PTRACE_EVENT_EXEC = 4" >> ${OUT}
221 if ! grep '^const PTRACE_EVENT_VFORK_DONE' ${OUT} > /dev/null 2>&1; then
222 echo "const PTRACE_EVENT_VFORK_DONE = 5" >> ${OUT}
224 if ! grep '^const PTRACE_EVENT_EXIT' ${OUT} > /dev/null 2>&1; then
225 echo "const PTRACE_EVENT_EXIT = 6" >> ${OUT}
227 if ! grep '^const _PTRACE_TRACEME' ${OUT} > /dev/null 2>&1; then
228 echo "const _PTRACE_TRACEME = 0" >> ${OUT}
231 # The registers returned by PTRACE_GETREGS. This is probably
232 # GNU/Linux specific; it should do no harm if there is no
234 regs=`grep '^type _user_regs_struct struct' gen-sysinfo.go || true`
235 if test "$regs" != ""; then
236 regs=`echo $regs | sed -e 's/type _user_regs_struct struct //' -e 's/[{}]//g'`
237 regs=`echo $regs | sed -e s'/^ *//'`
239 while test -n "$regs"; do
240 field=`echo $regs | sed -e 's/^\([^;]*\);.*$/\1/'`
241 regs=`echo $regs | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
242 # Capitalize the first character of the field.
243 f=`echo $field | sed -e 's/^\(.\).*$/\1/'`
244 r=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
245 f=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
247 nregs="$nregs $field;"
249 echo "type PtraceRegs struct {$nregs }" >> ${OUT}
253 echo 'type Size_t _size_t' >> ${OUT}
254 echo "type Ssize_t _ssize_t" >> ${OUT}
255 if grep '^const _HAVE_OFF64_T = ' gen-sysinfo.go > /dev/null 2>&1; then
256 echo "type Offset_t _off64_t" >> ${OUT}
258 echo "type Offset_t _off_t" >> ${OUT}
260 echo "type Mode_t _mode_t" >> ${OUT}
261 echo "type Pid_t _pid_t" >> ${OUT}
262 echo "type Uid_t _uid_t" >> ${OUT}
263 echo "type Gid_t _gid_t" >> ${OUT}
264 echo "type Socklen_t _socklen_t" >> ${OUT}
266 # The long type, needed because that is the type that ptrace returns.
267 sizeof_long=`grep '^const ___SIZEOF_LONG__ = ' gen-sysinfo.go | sed -e 's/.*= //'`
268 if test "$sizeof_long" = "4"; then
269 echo "type _C_long int32" >> ${OUT}
270 elif test "$sizeof_long" = "8"; then
271 echo "type _C_long int64" >> ${OUT}
273 echo 1>&2 "mksysinfo.sh: could not determine size of long (got $sizeof_long)"
277 # Solaris 2 needs _u?pad128_t, but its default definition in terms of long
278 # double is commented by -fdump-go-spec.
279 if grep "^// type _pad128_t" gen-sysinfo.go > /dev/null 2>&1; then
280 echo "type _pad128_t struct { _l [4]int32; }" >> ${OUT}
282 if grep "^// type _upad128_t" gen-sysinfo.go > /dev/null 2>&1; then
283 echo "type _upad128_t struct { _l [4]uint32; }" >> ${OUT}
286 # The time structures need special handling: we need to name the
287 # types, so that we can cast integers to the right types when
288 # assigning to the structures.
289 timeval=`grep '^type _timeval ' gen-sysinfo.go`
290 timeval_sec=`echo $timeval | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
291 timeval_usec=`echo $timeval | sed -n -e 's/^.*tv_usec \([^ ]*\);.*$/\1/p'`
292 echo "type Timeval_sec_t $timeval_sec" >> ${OUT}
293 echo "type Timeval_usec_t $timeval_usec" >> ${OUT}
295 sed -e 's/type _timeval /type Timeval /' \
296 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timeval_sec_t/' \
297 -e 's/tv_usec *[a-zA-Z0-9_]*/Usec Timeval_usec_t/' >> ${OUT}
298 timespec=`grep '^type _timespec ' gen-sysinfo.go || true`
299 if test "$timespec" = ""; then
300 # IRIX 6.5 has __timespec instead.
301 timespec=`grep '^type ___timespec ' gen-sysinfo.go || true`
303 timespec_sec=`echo $timespec | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
304 timespec_nsec=`echo $timespec | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
305 echo "type Timespec_sec_t $timespec_sec" >> ${OUT}
306 echo "type Timespec_nsec_t $timespec_nsec" >> ${OUT}
308 sed -e 's/^type \(__\)\?_timespec /type Timespec /' \
309 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timespec_sec_t/' \
310 -e 's/tv_nsec *[a-zA-Z0-9_]*/Nsec Timespec_nsec_t/' >> ${OUT}
312 timestruc=`grep '^type _timestruc_t ' gen-sysinfo.go || true`
313 if test "$timestruc" != ""; then
314 timestruc_sec=`echo $timestruc | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
315 timestruc_nsec=`echo $timestruc | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
316 echo "type Timestruc_sec_t $timestruc_sec" >> ${OUT}
317 echo "type Timestruc_nsec_t $timestruc_nsec" >> ${OUT}
319 sed -e 's/^type _timestruc_t /type Timestruc /' \
320 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timestruc_sec_t/' \
321 -e 's/tv_nsec *[a-zA-Z0-9_]*/Nsec Timestruc_nsec_t/' >> ${OUT}
325 # Prefer largefile variant if available.
326 stat=`grep '^type _stat64 ' gen-sysinfo.go || true`
327 if test "$stat" != ""; then
328 grep '^type _stat64 ' gen-sysinfo.go
330 grep '^type _stat ' gen-sysinfo.go
331 fi | sed -e 's/type _stat64/type Stat_t/' \
332 -e 's/type _stat/type Stat_t/' \
334 -e 's/st_ino/Ino/g' \
335 -e 's/st_nlink/Nlink/' \
336 -e 's/st_mode/Mode/' \
339 -e 's/st_rdev/Rdev/' \
340 -e 's/st_size/Size/' \
341 -e 's/st_blksize/Blksize/' \
342 -e 's/st_blocks/Blocks/' \
343 -e 's/st_atim/Atime/' \
344 -e 's/st_mtim/Mtime/' \
345 -e 's/st_ctim/Ctime/' \
346 -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
347 -e 's/\([^a-zA-Z0-9_]\)_timespec\(_t\)\?\([^a-zA-Z0-9_]\)/\1Timespec\3/g' \
348 -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
351 # The directory searching types.
352 # Prefer largefile variant if available.
353 dirent=`grep '^type _dirent64 ' gen-sysinfo.go || true`
354 if test "$dirent" != ""; then
355 grep '^type _dirent64 ' gen-sysinfo.go
357 grep '^type _dirent ' gen-sysinfo.go
358 fi | sed -e 's/type _dirent64/type Dirent/' \
359 -e 's/type _dirent/type Dirent/' \
360 -e 's/d_name \[0+1\]/d_name [0+256]/' \
361 -e 's/d_name/Name/' \
362 -e 's/]int8/]byte/' \
365 -e 's/d_reclen/Reclen/' \
366 -e 's/d_type/Type/' \
368 echo "type DIR _DIR" >> ${OUT}
371 rusage=`grep '^type _rusage struct' gen-sysinfo.go`
372 if test "$rusage" != ""; then
373 rusage=`echo $rusage | sed -e 's/type _rusage struct //' -e 's/[{}]//g'`
374 rusage=`echo $rusage | sed -e 's/^ *//'`
376 while test -n "$rusage"; do
377 field=`echo $rusage | sed -e 's/^\([^;]*\);.*$/\1/'`
378 rusage=`echo $rusage | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
379 # Drop the leading ru_, capitalize the next character.
380 field=`echo $field | sed -e 's/^ru_//'`
381 f=`echo $field | sed -e 's/^\(.\).*$/\1/'`
382 r=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
383 f=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
384 # Fix _timeval _timespec, and _timestruc_t.
385 r=`echo $r | sed -e s'/ _timeval$/ Timeval/'`
386 r=`echo $r | sed -e s'/ _timespec$/ Timespec/'`
387 r=`echo $r | sed -e s'/ _timestruc_t$/ Timestruc/'`
389 nrusage="$nrusage $field;"
391 echo "type Rusage struct {$nrusage }" >> ${OUT}
393 echo "type Rusage struct {}" >> ${OUT}
396 # The utsname struct.
397 grep '^type _utsname ' gen-sysinfo.go | \
398 sed -e 's/_utsname/Utsname/' \
399 -e 's/sysname/Sysname/' \
400 -e 's/nodename/Nodename/' \
401 -e 's/release/Release/' \
402 -e 's/version/Version/' \
403 -e 's/machine/Machine/' \
404 -e 's/domainname/Domainname/' \
408 iovec=`grep '^type _iovec ' gen-sysinfo.go`
409 iovec_len=`echo $iovec | sed -n -e 's/^.*iov_len \([^ ]*\);.*$/\1/p'`
410 echo "type Iovec_len_t $iovec_len" >> ${OUT}
412 sed -e 's/_iovec/Iovec/' \
413 -e 's/iov_base/Base/' \
414 -e 's/iov_len *[a-zA-Z0-9_]*/Len Iovec_len_t/' \
418 msghdr=`grep '^type _msghdr ' gen-sysinfo.go`
419 msghdr_controllen=`echo $msghdr | sed -n -e 's/^.*msg_controllen \([^ ]*\);.*$/\1/p'`
420 echo "type Msghdr_controllen_t $msghdr_controllen" >> ${OUT}
422 sed -e 's/_msghdr/Msghdr/' \
423 -e 's/msg_name/Name/' \
424 -e 's/msg_namelen/Namelen/' \
425 -e 's/msg_iov/Iov/' \
426 -e 's/msg_iovlen/Iovlen/' \
427 -e 's/_iovec/Iovec/' \
428 -e 's/msg_control/Control/' \
429 -e 's/msg_controllen *[a-zA-Z0-9_]*/Controllen Msghdr_controllen_t/' \
430 -e 's/msg_flags/Flags/' \
434 grep '^type _ip_mreq ' gen-sysinfo.go | \
435 sed -e 's/_ip_mreq/IpMreq/' \
436 -e 's/imr_multiaddr/Multiaddr/' \
437 -e 's/imr_interface/Interface/' \
438 -e 's/_in_addr/[4]byte/g' \