Home | History | Annotate | Line # | Download | only in sys_info
sys_info.sh revision 1.16
      1 #! /bin/sh
      2 
      3 # $NetBSD: sys_info.sh,v 1.16 2017/09/21 07:07:28 wiz Exp $
      4 
      5 # Copyright (c) 2016 Alistair Crooks <agc (at] NetBSD.org>
      6 # All rights reserved.
      7 #
      8 # Redistribution and use in source and binary forms, with or without
      9 # modification, are permitted provided that the following conditions
     10 # are met:
     11 # 1. Redistributions of source code must retain the above copyright
     12 #    notice, this list of conditions and the following disclaimer.
     13 # 2. Redistributions in binary form must reproduce the above copyright
     14 #    notice, this list of conditions and the following disclaimer in the
     15 #    documentation and/or other materials provided with the distribution.
     16 #
     17 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 #
     28 
     29 SYS_INFO_VERSION=20170920
     30 
     31 PATH=$(sysctl -n user.cs_path)
     32 export PATH
     33 
     34 LIBRARY_PATH=${LD_LIBRARY_PATH:-/usr/lib:/usr/X11R7/lib}
     35 
     36 # default libraries when no args are given (sorted...)
     37 LIBS=
     38 LIBS="${LIBS} libc"
     39 LIBS="${LIBS} libcurses"
     40 LIBS="${LIBS} libdrm"
     41 LIBS="${LIBS} libm"
     42 LIBS="${LIBS} libresolv"
     43 LIBS="${LIBS} libsqlite"
     44 LIBS="${LIBS} libssh"
     45 LIBS="${LIBS} libstdc++"
     46 LIBS="${LIBS} libterminfo"
     47 LIBS="${LIBS} libutil"
     48 LIBS="${LIBS} libX11"
     49 LIBS="${LIBS} libXaw7"
     50 LIBS="${LIBS} libXcb"
     51 LIBS="${LIBS} libXfont"
     52 LIBS="${LIBS} libXft"
     53 LIBS="${LIBS} libXrandr"
     54 LIBS="${LIBS} libXt"
     55 
     56 # short script to look for an executable $2, and if found, to place
     57 # path in $1
     58 # taken from pkgsrc bootstrap
     59 which_prog()
     60 {
     61 	local IFS _var _name _d -
     62 	set -f
     63 
     64 	_var="$1"; _name="$2"
     65 
     66 	eval _d=\"\$$_var\"
     67 	if [ -n "$_d" ]; then
     68 		# Variable is already set (by the user, for example)
     69 		return 0
     70 	fi
     71 
     72 	IFS=:
     73 	for _d in $PATH ; do
     74 		if [ -f "$_d/$_name" ] && [ -x "$_d/$_name" ]; then
     75 			# Program found
     76 			eval $_var=\""$_d/$_name"\"
     77 			return 0
     78 		fi
     79 	done
     80 
     81 	return 1
     82 }
     83 
     84 savedIFS=unset
     85 saveIFS() { savedIFS="${IFS-unset}"; IFS="$1"; }
     86 restIFS() { test "${savedIFS}" = unset && unset IFS || IFS="${savedIFS}"; }
     87 
     88 run() {
     89 	 # must send to stderr, as run is used in $() sometimes.
     90 	 $verbose && printf >&2 '%s\n' "${PS4:-...: }${1}"
     91 	 eval "$1"
     92 }
     93 
     94 # print out the version for the given argument (or everything)
     95 
     96 # case patterns are sorted by output order so
     97 #	sys_info
     98 # and
     99 #	sys_info | sort -f
    100 # generate identical output
    101 
    102 getversion() {
    103 	case "$1" in
    104 	'')
    105 		$all || return 0 ;&
    106 	awk)
    107 		run "awk --version | awk '{ print \$1 \"-\" \$3 }'"
    108 		$all || return 0 ;&
    109 	[Bb][Ii][Nn][Dd]|named)
    110 		run "named -v | awk '{ gsub(\"-\", \"\", \$2); gsub(\"P\", \"pl\", \$2); print tolower(\$1) \"-\" \$2 }'"
    111 		$all || return 0 ;&
    112 	bozohttpd|httpd)
    113 		v=$(run "${destdir}/usr/libexec/httpd -G" 2>/dev/null)
    114 		case "${v}" in
    115 		"")
    116 			run  "strings -a ${destdir}/usr/libexec/httpd | awk -F/ '\$1 == \"bozohttpd\" && NF == 2 { print \$1 \"-\" \$2; exit }'"
    117 			;;
    118 		*)
    119 			printf '%s\n' "bozohttpd-${v##*/}"
    120 			;;
    121 		esac
    122 		$all || return 0 ;&
    123 	bzip2)
    124 		run  "bzip2 --help 2>&1 | awk '{ sub(\",\", \"\", \$7); print \"bzip2-\" \$7; exit }'"
    125 		$all || return 0 ;&
    126 	calendar)
    127 		v=$(run "calendar -v" 2>/dev/null || true)
    128 		case "${v}" in
    129 		"")	printf '%s\n' "calendar-20150701" ;;
    130 		*)	printf '%s\n' "${v}" ;;
    131 		esac
    132 		$all || return 0 ;&
    133 	dhcpcd)
    134 		run  "dhcpcd --version | sed -e 's/ /-/g' -e 1q"
    135 		$all || return 0 ;&
    136 	dtc)
    137 		run "dtc --version | sed 's/Version: DTC /dtc-/'"
    138 		$all || return 0 ;&
    139 	ftpd)
    140 		run "strings -a /usr/libexec/ftpd | awk '\$1 == \"NetBSD-ftpd\" { print \"ftpd-\" \$2 }'"
    141 		$all || return 0 ;&
    142 	g++|c++)
    143 		run "g++ --version | awk '{ print \$1 \"-\" \$4; exit }'"
    144 		$all || return 0 ;&
    145 	gcc|cc)
    146 		run "gcc --version | awk '{ print \$1 \"-\" \$4; exit }'"
    147 		$all || return 0 ;&
    148 	grep)
    149 		run "grep --version | awk '{ print \$1 \"-\" \$4 \$5; exit }'"
    150 		$all || return 0 ;&
    151 	gzip)
    152 		run "gzip --version 2>&1 | awk '{ print \$2 \"-\" \$3 }'"
    153 		$all || return 0 ;&
    154 	lib*)
    155 		for L in ${1:-$LIBS}; do
    156 			saveIFS :
    157 			for d in ${LIBRARY_PATH} nowhere; do
    158 				restIFS
    159 				if [ -e ${d}/$L.so ]; then
    160 					run "ls -al \"${d}/$L.so\" | sed -e 's/^.*-> //' -e 's;^.*/;;' -e 's/\\.so\\./-/'"
    161 					break
    162 				fi
    163 			done
    164 			restIFS
    165 			test "$d" = nowhere && test -n "$1" &&
    166 				printf 2>&1 '%s\n' "$0: library $1 not found"
    167 		done
    168 		$all || return 0 ;&
    169 	[Nn]et[Bb][Ss][Dd]|kernel)
    170 		run "uname -sr | awk '{ print \$1 \"-\" \$2 }'"
    171 		$all || return 0 ;&
    172 	netpgp)
    173 		run "netpgp -V | awk '{ sub(\"/.*\", \"\", \$3); print \"netpgp-\" \$3; exit }'"
    174 		$all || return 0 ;&
    175 	netpgpverify)
    176 		run "netpgpverify -v | awk '{ print \$1 \"-\" \$3 }'"
    177 		$all || return 0 ;&
    178 	ntp)
    179 		run "ntpq --version | awk '{ sub(\"-.\", \"\"); sub(\"p\", \"pl\", \$2); print \"ntp-\" \$2 }'"
    180 		$all || return 0 ;&
    181 	openssh|ssh)
    182 		run "ssh -V 2>&1 | awk '{ sub(\"_\", \"-\", \$1); print tolower(\$1) }'"
    183 		$all || return 0 ;&
    184 	opensshd|sshd)
    185 		run "sshd -V 2>&1 | awk '/OpenSSH/ { sub(\"_\", \"D-\", \$1); print tolower(\$1) }'"
    186 		$all || return 0 ;&
    187 	openssl)
    188 		run "openssl version 2>/dev/null | awk '{ print tolower(\$1) \"-\" \$2 }'"
    189 		$all || return 0 ;&
    190 	pkg_info|pkg_install)
    191 		if which_prog infopath pkg_info; then
    192 			run "printf 'pkg_install-%s\n' \$(${infopath} -V)"
    193 		fi
    194 		$all || return 0 ;&
    195 	sh)
    196 		run "set -- \$NETBSD_SHELL; case \"\$1+\$2\" in *+BUILD*) ;; +) set -- ancient;; *) set -- \"\$1\";;esac; printf 'sh-%s\\n' \$1\${2:+-\${2#BUILD:}}"
    197 		$all || return 0 ;&
    198 	sqlite|sqlite3)
    199 		run "sqlite3 --version | awk '{ print \"sqlite3-\" \$1 }'"
    200 		$all || return 0 ;&
    201 	sys_info)
    202 		run "printf '%s\n' sys_info-${SYS_INFO_VERSION}"
    203 		$all || return 0 ;&
    204 	tcsh)
    205 		if which_prog tcshpath tcsh; then
    206 			run "${tcshpath} --version | awk '{ print \$1 \"-\" \$2 }'"
    207 		fi
    208 		$all || return 0 ;&
    209 	tzdata)
    210 		if [ -f ${destdir}/usr/share/zoneinfo/TZDATA_VERSION ]; then
    211 			run "cat ${destdir}/usr/share/zoneinfo/TZDATA_VERSION"
    212 		else
    213 			run "printf '%s\n' tzdata-too-old-to-matter"
    214 		fi
    215 		$all || return 0 ;&
    216 	unbound)
    217 		if which_prog unboundpath unbound-control; then
    218 			run "${unboundpath} -h | awk '/^Version/ { print \"unbound-\" \$2 }'"
    219 		else
    220 			$all || printf >&2 '%s\n' "unbound: not found"
    221 		fi
    222 		$all || return 0 ;&
    223 	[uU]ser[lL]and|release)
    224 		run "sed <${destdir}/etc/release -e 's/ /-/g' -e 's/^/userland-/' -e 1q"
    225 		$all || return 0 ;&
    226 	xz)
    227 		run "xz --version | awk '{ print \$1 \"-\" \$4; exit }'"
    228 		$all || return 0 ;&
    229 	yacc)
    230 		run "yacc -V | sed -e 's| ||g'"
    231 		$all || return 0 ;&
    232 
    233 	'')			# never matches
    234 		;;		# but terminates ;& sequence
    235 
    236 	*)	printf >&2 '%s\n' "Unrecognised subsystem: $1"
    237 		ERRS=1
    238 		;;
    239 	esac
    240 }
    241 
    242 verbose=false
    243 destdir=""
    244 # check if we have our only option
    245 while getopts "L:P:d:v" a; do
    246 	case "$a" in
    247 	L)	LIBRARY_PATH=${OPTARG};;
    248 	P)	PATH=${OPTARG};;
    249 	d)	PATH=${OPTARG}/bin:${OPTARG}/sbin:${OPTARG}/usr/bin:${OPTARG}/usr/sbin
    250 		LIBRARY_PATH=${OPTARG}/usr/lib:${OPTARG}/usr.X11R7/lib
    251 		destdir=${OPTARG};;
    252 	v)	verbose=true;;
    253 	\?)	printf >&2 '%s\n' \
    254 		    "Usage: $0 [-v] [-d destdir] [-L libdirs] [-P path] [system...]"
    255 		exit 2
    256 	esac
    257 done
    258 shift $((OPTIND - 1))
    259 
    260 if [ $# -eq 0 ]; then
    261 	set -- ''
    262 	all=true
    263 else
    264 	all=false
    265 fi
    266 
    267 ERRS=0
    268 while [ $# -gt 0 ]; do
    269 	getversion "$1"
    270 	shift
    271 done
    272 exit $ERRS
    273