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 #if defined(__sun__) && defined(__svr4__)
33 /* Needed by Solaris header files. */
34 #define _XOPEN_SOURCE 600
35 #define __EXTENSIONS__
38 #include <sys/types.h>
42 #include <netinet/in.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_PTRACE_H)
55 #include <sys/ptrace.h>
57 #include <sys/resource.h>
59 #include <sys/socket.h>
64 #if defined(HAVE_SYS_USER_H)
67 #if defined(HAVE_SYS_UTSNAME_H)
68 #include <sys/utsname.h>
73 ${CC} -fdump-go-spec=gen-sysinfo.go
-std=gnu99
-S -o sysinfo.s sysinfo.c
75 echo 'package syscall' > ${OUT}
77 # Get all the consts and types, skipping ones which could not be
78 # represented in Go and ones which we need to rewrite. We also skip
79 # function declarations, as we don't need them here. All the symbols
80 # will all have a leading underscore.
81 grep -v '^// ' gen-sysinfo.go | \
83 grep -v '^type _timeval ' | \
84 grep -v '^type _timespec ' | \
85 grep -v '^type _timestruc_t ' | \
86 grep -v '^type _epoll_' | \
87 grep -v 'in6_addr' | \
88 grep -v 'sockaddr_in6' | \
89 sed -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
90 -e 's/\([^a-zA-Z0-9_]\)_timespec\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
91 -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
94 # The errno constants.
95 grep '^const _E' gen-sysinfo.go | \
96 sed -e 's/^\(const \)_\(E[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
99 grep '^const _\(O\|F\|FD\)_' gen-sysinfo.go | \
100 sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
101 if ! grep '^const O_ASYNC' ${OUT} >/dev
/null
2>&1; then
102 echo "const O_ASYNC = 0" >> ${OUT}
104 if ! grep '^const O_CLOEXEC' ${OUT} >/dev
/null
2>&1; then
105 echo "const O_CLOEXEC = 0" >> ${OUT}
108 # The signal numbers.
109 grep '^const _SIG[^_]' gen-sysinfo.go | \
110 grep -v '^const _SIGEV_' | \
111 sed -e 's/^\(const \)_\(SIG[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
113 # The syscall numbers. We force the names to upper case.
114 grep '^const _SYS_' gen-sysinfo.go | \
115 sed -e 's/const _\(SYS_[^= ]*\).*$/\1/' | \
117 sup
=`echo $sys | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
118 echo "const $sup = _$sys" >> ${OUT}
122 grep '^const _S_' gen-sysinfo.go | \
123 sed -e 's/^\(const \)_\(S_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
125 # Process status constants.
126 grep '^const _W' gen-sysinfo.go |
127 sed -e 's/^\(const \)_\(W[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
128 # WSTOPPED was introduced in glibc 2.3.4.
129 if ! grep '^const _WSTOPPED = ' gen-sysinfo.go
>/dev
/null
2>&1; then
130 if grep '^const _WUNTRACED = ' gen-sysinfo.go
> /dev
/null
2>&1; then
131 echo 'const WSTOPPED = _WUNTRACED' >> ${OUT}
133 echo 'const WSTOPPED = 2' >> ${OUT}
136 if grep '^const ___WALL = ' gen-sysinfo.go
>/dev
/null
2>&1 \
137 && ! grep '^const _WALL = ' gen-sysinfo.go
>/dev
/null
2>&1; then
138 echo 'const WALL = ___WALL' >> ${OUT}
141 # Networking constants.
142 grep '^const _\(AF\|SOCK\|SOL\|SO\|IPPROTO\|TCP\|IP\|IPV6\)_' gen-sysinfo.go |
143 sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
144 grep '^const _SOMAXCONN' gen-sysinfo.go |
145 sed -e 's/^\(const \)_\(SOMAXCONN[^= ]*\)\(.*\)$/\1\2 = _\2/' \
147 grep '^const _SHUT_' gen-sysinfo.go |
148 sed -e 's/^\(const \)_\(SHUT[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
150 # The net package requires a definition for IPV6ONLY.
151 if ! grep '^const IPV6_V6ONLY ' ${OUT} >/dev
/null
2>&1; then
152 echo "const IPV6_V6ONLY = 0" >> ${OUT}
155 # pathconf constants.
156 grep '^const __PC' gen-sysinfo.go |
157 sed -e 's/^\(const \)__\(PC[^= ]*\)\(.*\)$/\1\2 = __\2/' >> ${OUT}
159 # The epoll constants were picked up by the errno constants, but we
160 # need to be sure the EPOLLRDHUP is defined.
161 if ! grep '^const EPOLLRDHUP' ${OUT} >/dev
/null
2>&1; then
162 echo "const EPOLLRDHUP = 0x2000" >> ${OUT}
165 # Ptrace constants. We don't expose all the PTRACE flags, just the
166 # PTRACE_O_xxx and PTRACE_EVENT_xxx ones.
167 grep '^const _PTRACE_O' gen-sysinfo.go |
168 sed -e 's/^\(const \)_\(PTRACE_O[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
169 grep '^const _PTRACE_EVENT' gen-sysinfo.go |
170 sed -e 's/^\(const \)_\(PTRACE_EVENT[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
171 # We need PTRACE_SETOPTIONS and PTRACE_GETEVENTMSG, but they are not
172 # defined in older versions of glibc.
173 if ! grep '^const _PTRACE_SETOPTIONS' ${OUT} > /dev
/null
2>&1; then
174 echo "const _PTRACE_SETOPTIONS = 0x4200" >> ${OUT}
176 if ! grep '^const PTRACE_O_TRACESYSGOOD' ${OUT} > /dev
/null
2>&1; then
177 echo "const PTRACE_O_TRACESYSGOOD = 0x1" >> ${OUT}
179 if ! grep '^const PTRACE_O_TRACEFORK' ${OUT} > /dev
/null
2>&1; then
180 echo "const PTRACE_O_TRACEFORK = 0x2" >> ${OUT}
182 if ! grep '^const PTRACE_O_TRACEVFORK' ${OUT} > /dev
/null
2>&1; then
183 echo "const PTRACE_O_TRACEVFORK = 0x4" >> ${OUT}
185 if ! grep '^const PTRACE_O_TRACECLONE' ${OUT} > /dev
/null
2>&1; then
186 echo "const PTRACE_O_TRACECLONE = 0x8" >> ${OUT}
188 if ! grep '^const PTRACE_O_TRACEEXEC' ${OUT} > /dev
/null
2>&1; then
189 echo "const PTRACE_O_TRACEEXEC = 0x10" >> ${OUT}
191 if ! grep '^const PTRACE_O_TRACEVFORKDONE' ${OUT} > /dev
/null
2>&1; then
192 echo "const PTRACE_O_TRACEVFORKDONE = 0x20" >> ${OUT}
194 if ! grep '^const PTRACE_O_TRACEEXIT' ${OUT} > /dev
/null
2>&1; then
195 echo "const PTRACE_O_TRACEEXIT = 0x40" >> ${OUT}
197 if ! grep '^const PTRACE_O_MASK' ${OUT} > /dev
/null
2>&1; then
198 echo "const PTRACE_O_MASK = 0x7f" >> ${OUT}
200 if ! grep '^const _PTRACE_GETEVENTMSG' ${OUT} > /dev
/null
2>&1; then
201 echo "const _PTRACE_GETEVENTMSG = 0x4201" >> ${OUT}
203 if ! grep '^const PTRACE_EVENT_FORK' ${OUT} > /dev
/null
2>&1; then
204 echo "const PTRACE_EVENT_FORK = 1" >> ${OUT}
206 if ! grep '^const PTRACE_EVENT_VFORK' ${OUT} > /dev
/null
2>&1; then
207 echo "const PTRACE_EVENT_VFORK = 2" >> ${OUT}
209 if ! grep '^const PTRACE_EVENT_CLONE' ${OUT} > /dev
/null
2>&1; then
210 echo "const PTRACE_EVENT_CLONE = 3" >> ${OUT}
212 if ! grep '^const PTRACE_EVENT_EXEC' ${OUT} > /dev
/null
2>&1; then
213 echo "const PTRACE_EVENT_EXEC = 4" >> ${OUT}
215 if ! grep '^const PTRACE_EVENT_VFORK_DONE' ${OUT} > /dev
/null
2>&1; then
216 echo "const PTRACE_EVENT_VFORK_DONE = 5" >> ${OUT}
218 if ! grep '^const PTRACE_EVENT_EXIT' ${OUT} > /dev
/null
2>&1; then
219 echo "const PTRACE_EVENT_EXIT = 6" >> ${OUT}
221 if ! grep '^const _PTRACE_TRACEME' ${OUT} > /dev
/null
2>&1; then
222 echo "const _PTRACE_TRACEME = 0" >> ${OUT}
225 # The registers returned by PTRACE_GETREGS. This is probably
226 # GNU/Linux specific; it should do no harm if there is no
228 regs
=`grep '^type _user_regs_struct struct' gen-sysinfo.go || true`
229 if test "$regs" != ""; then
230 regs
=`echo $regs | sed -e 's/type _user_regs_struct struct //' -e 's/[{}]//g'`
231 regs
=`echo $regs | sed -e s'/^ *//'`
233 while test -n "$regs"; do
234 field
=`echo $regs | sed -e 's/^\([^;]*\);.*$/\1/'`
235 regs
=`echo $regs | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
236 # Capitalize the first character of the field.
237 f
=`echo $field | sed -e 's/^\(.\).*$/\1/'`
238 r
=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
239 f
=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
241 nregs
="$nregs $field;"
243 echo "type PtraceRegs struct {$nregs }" >> ${OUT}
247 echo 'type Size_t _size_t' >> ${OUT}
248 echo "type Ssize_t _ssize_t" >> ${OUT}
249 if grep '^const _HAVE_OFF64_T = ' gen-sysinfo.go
> /dev
/null
2>&1; then
250 echo "type Offset_t _off64_t" >> ${OUT}
252 echo "type Offset_t _off_t" >> ${OUT}
254 echo "type Mode_t _mode_t" >> ${OUT}
255 echo "type Pid_t _pid_t" >> ${OUT}
256 echo "type Uid_t _uid_t" >> ${OUT}
257 echo "type Gid_t _gid_t" >> ${OUT}
258 echo "type Socklen_t _socklen_t" >> ${OUT}
260 # The long type, needed because that is the type that ptrace returns.
261 sizeof_long
=`grep '^const ___SIZEOF_LONG__ = ' gen-sysinfo.go | sed -e 's/.*= //'`
262 if test "$sizeof_long" = "4"; then
263 echo "type _C_long int32" >> ${OUT}
264 elif test "$sizeof_long" = "8"; then
265 echo "type _C_long int64" >> ${OUT}
267 echo 1>&2 "mksysinfo.sh: could not determine size of long (got $sizeof_long)"
271 # Solaris 2 needs _u?pad128_t, but its default definition in terms of long
272 # double is commented by -fdump-go-spec.
273 if grep "^// type _pad128_t" gen-sysinfo.go
> /dev
/null
2>&1; then
274 echo "type _pad128_t struct { _l [4]int32; }" >> ${OUT}
276 if grep "^// type _upad128_t" gen-sysinfo.go
> /dev
/null
2>&1; then
277 echo "type _upad128_t struct { _l [4]uint32; }" >> ${OUT}
280 # The time structures need special handling: we need to name the
281 # types, so that we can cast integers to the right types when
282 # assigning to the structures.
283 timeval
=`grep '^type _timeval ' gen-sysinfo.go`
284 timeval_sec
=`echo $timeval | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
285 timeval_usec
=`echo $timeval | sed -n -e 's/^.*tv_usec \([^ ]*\);.*$/\1/p'`
286 echo "type Timeval_sec_t $timeval_sec" >> ${OUT}
287 echo "type Timeval_usec_t $timeval_usec" >> ${OUT}
289 sed -e 's/type _timeval /type Timeval /' \
290 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timeval_sec_t/' \
291 -e 's/tv_usec *[a-zA-Z0-9_]*/Usec Timeval_usec_t/' >> ${OUT}
292 timespec
=`grep '^type _timespec ' gen-sysinfo.go`
293 timespec_sec
=`echo $timespec | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
294 timespec_nsec
=`echo $timespec | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
295 echo "type Timespec_sec_t $timespec_sec" >> ${OUT}
296 echo "type Timespec_nsec_t $timespec_nsec" >> ${OUT}
298 sed -e 's/^type _timespec /type Timespec /' \
299 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timespec_sec_t/' \
300 -e 's/tv_nsec *[a-zA-Z0-9_]*/Nsec Timespec_nsec_t/' >> ${OUT}
302 timestruc
=`grep '^type _timestruc_t ' gen-sysinfo.go || true`
303 if test "$timestruc" != ""; then
304 timestruc_sec
=`echo $timestruc | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
305 timestruc_nsec
=`echo $timestruc | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
306 echo "type Timestruc_sec_t $timestruc_sec" >> ${OUT}
307 echo "type Timestruc_nsec_t $timestruc_nsec" >> ${OUT}
309 sed -e 's/^type _timestruc_t /type Timestruc /' \
310 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timestruc_sec_t/' \
311 -e 's/tv_nsec *[a-zA-Z0-9_]*/Nsec Timestruc_nsec_t/' >> ${OUT}
315 # Prefer largefile variant if available.
316 stat
=`grep '^type _stat64 ' gen-sysinfo.go || true`
317 if test "$stat" != ""; then
318 grep '^type _stat64 ' gen-sysinfo.go
320 grep '^type _stat ' gen-sysinfo.go
321 fi |
sed -e 's/type _stat\(64\)\?/type Stat_t/' \
323 -e 's/st_ino/Ino/g' \
324 -e 's/st_nlink/Nlink/' \
325 -e 's/st_mode/Mode/' \
328 -e 's/st_rdev/Rdev/' \
329 -e 's/st_size/Size/' \
330 -e 's/st_blksize/Blksize/' \
331 -e 's/st_blocks/Blocks/' \
332 -e 's/st_atim/Atime/' \
333 -e 's/st_mtim/Mtime/' \
334 -e 's/st_ctim/Ctime/' \
335 -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
336 -e 's/\([^a-zA-Z0-9_]\)_timespec\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
337 -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
340 # The directory searching types.
341 # Prefer largefile variant if available.
342 dirent
=`grep '^type _dirent64 ' gen-sysinfo.go || true`
343 if test "$dirent" != ""; then
344 grep '^type _dirent64 ' gen-sysinfo.go
346 grep '^type _dirent ' gen-sysinfo.go
347 fi |
sed -e 's/type _dirent\(64\)\?/type Dirent/' \
348 -e 's/d_name \[0+1\]/d_name [0+256]/' \
349 -e 's/d_name/Name/' \
350 -e 's/]int8/]byte/' \
353 -e 's/d_reclen/Reclen/' \
354 -e 's/d_type/Type/' \
356 echo "type DIR _DIR" >> ${OUT}
359 rusage
=`grep '^type _rusage struct' gen-sysinfo.go`
360 if test "$rusage" != ""; then
361 rusage
=`echo $rusage | sed -e 's/type _rusage struct //' -e 's/[{}]//g'`
362 rusage
=`echo $rusage | sed -e 's/^ *//'`
364 while test -n "$rusage"; do
365 field
=`echo $rusage | sed -e 's/^\([^;]*\);.*$/\1/'`
366 rusage
=`echo $rusage | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
367 # Drop the leading ru_, capitalize the next character.
368 field
=`echo $field | sed -e 's/^ru_//'`
369 f
=`echo $field | sed -e 's/^\(.\).*$/\1/'`
370 r
=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
371 f
=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
372 # Fix _timeval _timespec, and _timestruc_t.
373 r
=`echo $r | sed -e s'/ _timeval$/ Timeval/'`
374 r
=`echo $r | sed -e s'/ _timespec$/ Timespec/'`
375 r
=`echo $r | sed -e s'/ _timestruc_t$/ Timestruc/'`
377 nrusage
="$nrusage $field;"
379 echo "type Rusage struct {$nrusage }" >> ${OUT}
382 # The utsname struct.
383 grep '^type _utsname ' gen-sysinfo.go | \
384 sed -e 's/_utsname/Utsname/' \
385 -e 's/sysname/Sysname/' \
386 -e 's/nodename/Nodename/' \
387 -e 's/release/Release/' \
388 -e 's/version/Version/' \
389 -e 's/machine/Machine/' \
390 -e 's/domainname/Domainname/' \
394 iovec
=`grep '^type _iovec ' gen-sysinfo.go`
395 iovec_len
=`echo $iovec | sed -n -e 's/^.*iov_len \([^ ]*\);.*$/\1/p'`
396 echo "type Iovec_len_t $iovec_len" >> ${OUT}
398 sed -e 's/_iovec/Iovec/' \
399 -e 's/iov_base/Base/' \
400 -e 's/iov_len *[a-zA-Z0-9_]*/Len Iovec_len_t/' \
404 msghdr
=`grep '^type _msghdr ' gen-sysinfo.go`
405 msghdr_controllen
=`echo $msghdr | sed -n -e 's/^.*msg_controllen \([^ ]*\);.*$/\1/p'`
406 echo "type Msghdr_controllen_t $msghdr_controllen" >> ${OUT}
408 sed -e 's/_msghdr/Msghdr/' \
409 -e 's/msg_name/Name/' \
410 -e 's/msg_namelen/Namelen/' \
411 -e 's/msg_iov/Iov/' \
412 -e 's/msg_iovlen/Iovlen/' \
413 -e 's/_iovec/Iovec/' \
414 -e 's/msg_control/Control/' \
415 -e 's/msg_controllen *[a-zA-Z0-9_]*/Controllen Msghdr_controllen_t/' \
416 -e 's/msg_flags/Flags/' \