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.
27 #include <sys/types.h>
31 #include <netinet/in.h>
32 #include <netinet/tcp.h>
34 #if defined(HAVE_SYSCALL_H)
37 #if defined(HAVE_SYS_EPOLL_H)
38 #include <sys/epoll.h>
40 #if defined(HAVE_SYS_PTRACE_H)
41 #include <sys/ptrace.h>
43 #include <sys/resource.h>
45 #include <sys/socket.h>
50 #if defined(HAVE_SYS_USER_H)
53 #if defined(HAVE_SYS_UTSNAME_H)
54 #include <sys/utsname.h>
59 ${CC} -D_GNU_SOURCE -fdump-go-spec=gen-sysinfo.go -S -o sysinfo.s sysinfo.c
61 echo 'package syscall' > ${OUT}
63 # Get all the consts and types, skipping ones which could not be
64 # represented in Go and ones which we need to rewrite. We also skip
65 # function declarations, as we don't need them here. All the symbols
66 # will all have a leading underscore.
67 grep -v '^// ' gen-sysinfo.go | \
69 grep -v '^type _timeval ' | \
70 grep -v '^type _timespec ' | \
71 grep -v '^type _timestruc_t ' | \
72 grep -v '^type _epoll_' | \
73 grep -v 'in6_addr' | \
74 grep -v 'sockaddr_in6' | \
75 sed -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
76 -e 's/\([^a-zA-Z0-9_]\)_timespec\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
77 -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
80 # The errno constants.
81 grep '^const _E' gen-sysinfo.go | \
82 sed -e 's/^\(const \)_\(E[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
85 grep '^const _\(O\|F\|FD\)_' gen-sysinfo.go | \
86 sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
87 if ! grep '^const O_ASYNC' ${OUT} >/dev/null 2>&1; then
88 echo "const O_ASYNC = 0" >> ${OUT}
90 if ! grep '^const O_CLOEXEC' ${OUT} >/dev/null 2>&1; then
91 echo "const O_CLOEXEC = 0" >> ${OUT}
95 grep '^const _SIG[^_]' gen-sysinfo.go | \
96 grep -v '^const _SIGEV_' | \
97 sed -e 's/^\(const \)_\(SIG[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
99 # The syscall numbers. We force the names to upper case.
100 grep '^const _SYS_' gen-sysinfo.go | \
101 sed -e 's/const _\(SYS_[^= ]*\).*$/\1/' | \
103 sup=`echo $sys | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
104 echo "const $sup = _$sys" >> ${OUT}
108 grep '^const _S_' gen-sysinfo.go | \
109 sed -e 's/^\(const \)_\(S_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
111 # Process status constants.
112 grep '^const _W' gen-sysinfo.go |
113 sed -e 's/^\(const \)_\(W[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
114 # WSTOPPED was introduced in glibc 2.3.4.
115 if ! grep '^const _WSTOPPED = ' gen-sysinfo.go >/dev/null 2>&1; then
116 if grep '^const _WUNTRACED = ' gen-sysinfo.go > /dev/null 2>&1; then
117 echo 'const WSTOPPED = _WUNTRACED' >> ${OUT}
119 echo 'const WSTOPPED = 2' >> ${OUT}
122 if grep '^const ___WALL = ' gen-sysinfo.go >/dev/null 2>&1 \
123 && ! grep '^const _WALL = ' gen-sysinfo.go >/dev/null 2>&1; then
124 echo 'const WALL = ___WALL' >> ${OUT}
127 # Networking constants.
128 grep '^const _\(AF\|SOCK\|SOL\|SO\|IPPROTO\|TCP\|IP\|IPV6\)_' gen-sysinfo.go |
129 sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
130 grep '^const _SOMAXCONN' gen-sysinfo.go |
131 sed -e 's/^\(const \)_\(SOMAXCONN[^= ]*\)\(.*\)$/\1\2 = _\2/' \
133 grep '^const _SHUT_' gen-sysinfo.go |
134 sed -e 's/^\(const \)_\(SHUT[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
136 # The net package requires a definition for IPV6ONLY.
137 if ! grep '^const IPV6_V6ONLY ' ${OUT} >/dev/null 2>&1; then
138 echo "const IPV6_V6ONLY = 0" >> ${OUT}
141 # pathconf constants.
142 grep '^const __PC' gen-sysinfo.go |
143 sed -e 's/^\(const \)__\(PC[^= ]*\)\(.*\)$/\1\2 = __\2/' >> ${OUT}
145 # The epoll constants were picked up by the errno constants, but we
146 # need to be sure the EPOLLRDHUP is defined.
147 if ! grep '^const EPOLLRDHUP' ${OUT} >/dev/null 2>&1; then
148 echo "const EPOLLRDHUP = 0x2000" >> ${OUT}
151 # Ptrace constants. We don't expose all the PTRACE flags, just the
152 # PTRACE_O_xxx and PTRACE_EVENT_xxx ones.
153 grep '^const _PTRACE_O' gen-sysinfo.go |
154 sed -e 's/^\(const \)_\(PTRACE_O[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
155 grep '^const _PTRACE_EVENT' gen-sysinfo.go |
156 sed -e 's/^\(const \)_\(PTRACE_EVENT[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
157 # We need PTRACE_SETOPTIONS and PTRACE_GETEVENTMSG, but they are not
158 # defined in older versions of glibc.
159 if ! grep '^const _PTRACE_SETOPTIONS' ${OUT} > /dev/null 2>&1; then
160 echo "const _PTRACE_SETOPTIONS = 0x4200" >> ${OUT}
162 if ! grep '^const PTRACE_O_TRACESYSGOOD' ${OUT} > /dev/null 2>&1; then
163 echo "const PTRACE_O_TRACESYSGOOD = 0x1" >> ${OUT}
165 if ! grep '^const PTRACE_O_TRACEFORK' ${OUT} > /dev/null 2>&1; then
166 echo "const PTRACE_O_TRACEFORK = 0x2" >> ${OUT}
168 if ! grep '^const PTRACE_O_TRACEVFORK' ${OUT} > /dev/null 2>&1; then
169 echo "const PTRACE_O_TRACEVFORK = 0x4" >> ${OUT}
171 if ! grep '^const PTRACE_O_TRACECLONE' ${OUT} > /dev/null 2>&1; then
172 echo "const PTRACE_O_TRACECLONE = 0x8" >> ${OUT}
174 if ! grep '^const PTRACE_O_TRACEEXEC' ${OUT} > /dev/null 2>&1; then
175 echo "const PTRACE_O_TRACEEXEC = 0x10" >> ${OUT}
177 if ! grep '^const PTRACE_O_TRACEVFORKDONE' ${OUT} > /dev/null 2>&1; then
178 echo "const PTRACE_O_TRACEVFORKDONE = 0x20" >> ${OUT}
180 if ! grep '^const PTRACE_O_TRACEEXIT' ${OUT} > /dev/null 2>&1; then
181 echo "const PTRACE_O_TRACEEXIT = 0x40" >> ${OUT}
183 if ! grep '^const PTRACE_O_MASK' ${OUT} > /dev/null 2>&1; then
184 echo "const PTRACE_O_MASK = 0x7f" >> ${OUT}
186 if ! grep '^const _PTRACE_GETEVENTMSG' ${OUT} > /dev/null 2>&1; then
187 echo "const _PTRACE_GETEVENTMSG = 0x4201" >> ${OUT}
189 if ! grep '^const PTRACE_EVENT_FORK' ${OUT} > /dev/null 2>&1; then
190 echo "const PTRACE_EVENT_FORK = 1" >> ${OUT}
192 if ! grep '^const PTRACE_EVENT_VFORK' ${OUT} > /dev/null 2>&1; then
193 echo "const PTRACE_EVENT_VFORK = 2" >> ${OUT}
195 if ! grep '^const PTRACE_EVENT_CLONE' ${OUT} > /dev/null 2>&1; then
196 echo "const PTRACE_EVENT_CLONE = 3" >> ${OUT}
198 if ! grep '^const PTRACE_EVENT_EXEC' ${OUT} > /dev/null 2>&1; then
199 echo "const PTRACE_EVENT_EXEC = 4" >> ${OUT}
201 if ! grep '^const PTRACE_EVENT_VFORK_DONE' ${OUT} > /dev/null 2>&1; then
202 echo "const PTRACE_EVENT_VFORK_DONE = 5" >> ${OUT}
204 if ! grep '^const PTRACE_EVENT_EXIT' ${OUT} > /dev/null 2>&1; then
205 echo "const PTRACE_EVENT_EXIT = 6" >> ${OUT}
207 if ! grep '^const _PTRACE_TRACEME' ${OUT} > /dev/null 2>&1; then
208 echo "const _PTRACE_TRACEME = 0" >> ${OUT}
211 # The registers returned by PTRACE_GETREGS. This is probably
212 # GNU/Linux specific; it should do no harm if there is no
214 regs=`grep '^type _user_regs_struct struct' gen-sysinfo.go || true`
215 if test "$regs" != ""; then
216 regs=`echo $regs | sed -e 's/type _user_regs_struct struct //' -e 's/[{}]//g'`
217 regs=`echo $regs | sed -e s'/^ *//'`
219 while test -n "$regs"; do
220 field=`echo $regs | sed -e 's/^\([^;]*\);.*$/\1/'`
221 regs=`echo $regs | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
222 # Capitalize the first character of the field.
223 f=`echo $field | sed -e 's/^\(.\).*$/\1/'`
224 r=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
225 f=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
227 nregs="$nregs $field;"
229 echo "type PtraceRegs struct {$nregs }" >> ${OUT}
233 echo 'type Size_t _size_t' >> ${OUT}
234 echo "type Ssize_t _ssize_t" >> ${OUT}
235 if grep '^const _HAVE_OFF64_T = ' gen-sysinfo.go > /dev/null 2>&1; then
236 echo "type Offset_t _off64_t" >> ${OUT}
238 echo "type Offset_t _off_t" >> ${OUT}
240 echo "type Mode_t _mode_t" >> ${OUT}
241 echo "type Pid_t _pid_t" >> ${OUT}
242 echo "type Uid_t _uid_t" >> ${OUT}
243 echo "type Gid_t _gid_t" >> ${OUT}
244 echo "type Socklen_t _socklen_t" >> ${OUT}
246 # The long type, needed because that is the type that ptrace returns.
247 sizeof_long=`grep '^const ___SIZEOF_LONG__ = ' gen-sysinfo.go | sed -e 's/.*= //'`
248 if test "$sizeof_long" = "4"; then
249 echo "type _C_long int32" >> ${OUT}
250 elif test "$sizeof_long" = "8"; then
251 echo "type _C_long int64" >> ${OUT}
253 echo 1>&2 "mksysinfo.sh: could not determine size of long (got $sizeof_long)"
257 # Solaris 2 needs _u?pad128_t, but its default definition in terms of long
258 # double is commented by -fdump-go-spec.
259 if grep "^// type _pad128_t" gen-sysinfo.go > /dev/null 2>&1; then
260 echo "type _pad128_t struct { _l [4]int32; }" >> ${OUT}
262 if grep "^// type _upad128_t" gen-sysinfo.go > /dev/null 2>&1; then
263 echo "type _upad128_t struct { _l [4]uint32; }" >> ${OUT}
266 # The time structures need special handling: we need to name the
267 # types, so that we can cast integers to the right types when
268 # assigning to the structures.
269 timeval=`grep '^type _timeval ' gen-sysinfo.go`
270 timeval_sec=`echo $timeval | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
271 timeval_usec=`echo $timeval | sed -n -e 's/^.*tv_usec \([^ ]*\);.*$/\1/p'`
272 echo "type Timeval_sec_t $timeval_sec" >> ${OUT}
273 echo "type Timeval_usec_t $timeval_usec" >> ${OUT}
275 sed -e 's/type _timeval /type Timeval /' \
276 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timeval_sec_t/' \
277 -e 's/tv_usec *[a-zA-Z0-9_]*/Usec Timeval_usec_t/' >> ${OUT}
278 timespec=`grep '^type _timespec ' gen-sysinfo.go`
279 timespec_sec=`echo $timespec | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
280 timespec_nsec=`echo $timespec | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
281 echo "type Timespec_sec_t $timespec_sec" >> ${OUT}
282 echo "type Timespec_nsec_t $timespec_nsec" >> ${OUT}
284 sed -e 's/^type _timespec /type Timespec /' \
285 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timespec_sec_t/' \
286 -e 's/tv_nsec *[a-zA-Z0-9_]*/Nsec Timespec_nsec_t/' >> ${OUT}
288 timestruc=`grep '^type _timestruc_t ' gen-sysinfo.go || true`
289 if test "$timestruc" != ""; then
290 timestruc_sec=`echo $timestruc | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
291 timestruc_nsec=`echo $timestruc | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
292 echo "type Timestruc_sec_t $timestruc_sec" >> ${OUT}
293 echo "type Timestruc_nsec_t $timestruc_nsec" >> ${OUT}
295 sed -e 's/^type _timestruc_t /type Timestruc /' \
296 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timestruc_sec_t/' \
297 -e 's/tv_nsec *[a-zA-Z0-9_]*/Nsec Timestruc_nsec_t/' >> ${OUT}
301 grep 'type _stat ' gen-sysinfo.go | \
302 sed -e 's/type _stat/type Stat_t/' \
305 -e 's/st_nlink/Nlink/' \
306 -e 's/st_mode/Mode/' \
309 -e 's/st_rdev/Rdev/' \
310 -e 's/st_size/Size/' \
311 -e 's/st_blksize/Blksize/' \
312 -e 's/st_blocks/Blocks/' \
313 -e 's/st_atim/Atime/' \
314 -e 's/st_mtim/Mtime/' \
315 -e 's/st_ctim/Ctime/' \
316 -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
317 -e 's/\([^a-zA-Z0-9_]\)_timespec\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
318 -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
321 # The directory searching types.
322 grep '^type _dirent ' gen-sysinfo.go | \
323 sed -e 's/type _dirent/type Dirent/' \
324 -e 's/d_name/Name/' \
325 -e 's/]int8/]byte/' \
328 -e 's/d_reclen/Reclen/' \
329 -e 's/d_type/Type/' \
331 echo "type DIR _DIR" >> ${OUT}
334 rusage=`grep '^type _rusage struct' gen-sysinfo.go`
335 if test "$rusage" != ""; then
336 rusage=`echo $rusage | sed -e 's/type _rusage struct //' -e 's/[{}]//g'`
337 rusage=`echo $rusage | sed -e 's/^ *//'`
339 while test -n "$rusage"; do
340 field=`echo $rusage | sed -e 's/^\([^;]*\);.*$/\1/'`
341 rusage=`echo $rusage | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
342 # Drop the leading ru_, capitalize the next character.
343 field=`echo $field | sed -e 's/^ru_//'`
344 f=`echo $field | sed -e 's/^\(.\).*$/\1/'`
345 r=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
346 f=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
347 # Fix _timeval _timespec, and _timestruc_t.
348 r=`echo $r | sed -e s'/ _timeval$/ Timeval/'`
349 r=`echo $r | sed -e s'/ _timespec$/ Timespec/'`
350 r=`echo $r | sed -e s'/ _timestruc_t$/ Timestruc/'`
352 nrusage="$nrusage $field;"
354 echo "type Rusage struct {$nrusage }" >> ${OUT}
357 # The utsname struct.
358 grep '^type _utsname ' gen-sysinfo.go | \
359 sed -e 's/_utsname/Utsname/' \
360 -e 's/sysname/Sysname/' \
361 -e 's/nodename/Nodename/' \
362 -e 's/release/Release/' \
363 -e 's/version/Version/' \
364 -e 's/machine/Machine/' \
365 -e 's/domainname/Domainname/' \
369 iovec=`grep '^type _iovec ' gen-sysinfo.go`
370 iovec_len=`echo $iovec | sed -n -e 's/^.*iov_len \([^ ]*\);.*$/\1/p'`
371 echo "type Iovec_len_t $iovec_len" >> ${OUT}
373 sed -e 's/_iovec/Iovec/' \
374 -e 's/iov_base/Base/' \
375 -e 's/iov_len *[a-zA-Z0-9_]*/Len Iovec_len_t/' \
379 msghdr=`grep '^type _msghdr ' gen-sysinfo.go`
380 msghdr_controllen=`echo $msghdr | sed -n -e 's/^.*msg_controllen \([^ ]*\);.*$/\1/p'`
381 echo "type Msghdr_controllen_t $msghdr_controllen" >> ${OUT}
383 sed -e 's/_msghdr/Msghdr/' \
384 -e 's/msg_name/Name/' \
385 -e 's/msg_namelen/Namelen/' \
386 -e 's/msg_iov/Iov/' \
387 -e 's/msg_iovlen/Iovlen/' \
388 -e 's/_iovec/Iovec/' \
389 -e 's/msg_control/Control/' \
390 -e 's/msg_controllen *[a-zA-Z0-9_]*/Controllen Msghdr_controllen_t/' \
391 -e 's/msg_flags/Flags/' \