xref: /netbsd-src/lib/checkver (revision 73b5f8505bd95e1f3ddcb8a105c21486501ec05e)
18e3f60f3Schristos#!/bin/sh
2*73b5f850Smsaitoh#	$NetBSD: checkver,v 1.18 2021/12/05 08:09:30 msaitoh Exp $
37c323c9fSerh#
47c323c9fSerh# Copyright (c) 1998 The NetBSD Foundation, Inc.
57c323c9fSerh# All rights reserved.
67c323c9fSerh#
77c323c9fSerh# This code is derived from software contributed to The NetBSD Foundation
87c323c9fSerh# by Eric Haszlakiewicz.
97c323c9fSerh#
107c323c9fSerh# Redistribution and use in source and binary forms, with or without
117c323c9fSerh# modification, are permitted provided that the following conditions
127c323c9fSerh# are met:
137c323c9fSerh# 1. Redistributions of source code must retain the above copyright
147c323c9fSerh#    notice, this list of conditions and the following disclaimer.
157c323c9fSerh# 2. Redistributions in binary form must reproduce the above copyright
167c323c9fSerh#    notice, this list of conditions and the following disclaimer in the
177c323c9fSerh#    documentation and/or other materials provided with the distribution.
187c323c9fSerh#
197c323c9fSerh# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
207c323c9fSerh# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
217c323c9fSerh# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
227c323c9fSerh# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
237c323c9fSerh# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
247c323c9fSerh# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
257c323c9fSerh# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
267c323c9fSerh# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
277c323c9fSerh# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
287c323c9fSerh# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
297c323c9fSerh# POSSIBILITY OF SUCH DAMAGE.
307c323c9fSerh#
317c323c9fSerh
327c323c9fSerh#--------------------------------------------------------------------#
339aa5adaeSerh# checkver -
349aa5adaeSerh#	Check for libraries that appear to be newer than the
359aa5adaeSerh#	one we're about to install.
369aa5adaeSerh#
375b354a58Ssimonb# checkver [-q] [-v shlib_version_file] -d [installedlibdir [library name]]"
385b354a58Ssimonb# checkver [-q] [-v shlib_version_file] -s [setlistdir [library name]]"
395b354a58Ssimonb# checkver [-q] [-v shlib_version_file] -f liblistfile [library name]"
407c323c9fSerh#
41792cde2dSerh# One of -d, -s or -f must be specified.
427c323c9fSerh#
43792cde2dSerh# all: If library name is not specified it is assumed to
44792cde2dSerh#	be the name of the current directory.
457c323c9fSerh#
46792cde2dSerh# -d: Checks the version against the installed libraries.
47792cde2dSerh#	If no further arguments are given "/usr/lib" is
48792cde2dSerh#	used as the location of installed libraries.
49792cde2dSerh#
50792cde2dSerh# -s: Checks the version against the sets.  If no argument
51792cde2dSerh#	follows the sets directory defaults to "/usr/src/distrib/sets/lists".
52792cde2dSerh#	The directory may be specified as either the top of the
53792cde2dSerh#	source tree or as the lists directory.
54792cde2dSerh#
55792cde2dSerh# -f: Checks the version against the provided list.  A filename
56792cde2dSerh#	must be supplied.
577c323c9fSerh#
585b354a58Ssimonb# -v: Specify the filename of the shlib_version file.  Defaults
595b354a58Ssimonb#     to "./shlib_version".
605b354a58Ssimonb#
617c323c9fSerh# This script produces no output if all library version are not
627c323c9fSerh# large than the source version.  If an installed library with a
637c323c9fSerh# version greater than the source is found, checkver prints a
647c323c9fSerh# header and a list of the names of the offending installed libraries.
657c323c9fSerh#
66*73b5f850Smsaitoh# The header may be suppressed by passing "-q" as the first argument.
677c323c9fSerh#
687c323c9fSerh
69792cde2dSerhTMP=/tmp/checkver.$$
70e2c5bb78SchristosPROG="$(basename "$0")"
718e3f60f3Schristos# Can't trap 11 (SEGV) in the Real Bourne Shell, since it uses it for
728e3f60f3Schristos# internal malloc!
738e3f60f3Schristostrap "exit 2" 1 2 3 4 5 6 7 8 10 12 13 14 15
74792cde2dSerhtrap "[ -d $TMP ] && rm -rf $TMP" 0
757c323c9fSerh
767c323c9fSerhUsage() {
7733b31c0dSchristoscat << EOF 1>&2
7833b31c0dSchristosUsage: $PROG [-q] [-v version_file] -d [installedlibdir [library name]]
7933b31c0dSchristos       $PROG [-q] [-v version_file] -s [setlistdir [library name]]
8033b31c0dSchristos       $PROG [-q] [-v version_file] -f liblistfile [library name]
8133b31c0dSchristos       $PROG is a script that looks for installed libraries with
8233b31c0dSchristos       versions greater than that in the version file.
8333b31c0dSchristos       For more information, read the comments.
8433b31c0dSchristosEOF
8533b31c0dSchristos	exit 1
867c323c9fSerh}
877c323c9fSerh
88792cde2dSerhbasedir=/usr/src
89792cde2dSerhsetsdir=$basedir/distrib/sets/lists
90792cde2dSerhlibdir=/usr/lib
915b354a58Ssimonbshlib_version=./shlib_version
927c323c9fSerh
93792cde2dSerherror=0
94792cde2dSerhquiet=0
95792cde2dSerhusedir=0
96792cde2dSerhusefile=0
97792cde2dSerhusesets=0
9833b31c0dSchristosCWD=$(pwd)
99fa075811Schristos: ${AWK:=awk}
10033b31c0dSchristos
10133b31c0dSchristosfixone() {
102fa075811Schristos	eval $(${AWK} -v 'LIB=$1' '
103fa075811SchristosBEGIN {
104fa075811Schristos	gsub(".*\.so\.", "", LIB);
105fa075811Schristos	split(LIB, VER, ".");
106fa075811Schristos	printf("local instmajor=%d\n", V[1] + 0);
107fa075811Schristos	printf("local instminor=%d\n", V[2] + 0);
108fa075811Schristos	printf("local instteeny=%d\n", V[3] + 0);
109fa075811Schristos}')
11033b31c0dSchristos	local ms="The following libraries have versions greater than the source"
11133b31c0dSchristos
11233b31c0dSchristos	# If they're greater than the source, complain.
11333b31c0dSchristos	if [ "0$major" -eq "0$instmajor" ]; then
11433b31c0dSchristos		if [ "0$minor" -eq "0$instminor" ]; then
11533b31c0dSchristos			if [ "0$teeny" -lt "0$instteeny" ]; then
11633b31c0dSchristos				if [ $error -eq 0 -a $quiet -eq 0 ]; then
11733b31c0dSchristos					echo "$ms" 1>&2
1187c323c9fSerh				fi
11933b31c0dSchristos				echo $1 1>&2
12033b31c0dSchristos				error=1
12133b31c0dSchristos			fi
12233b31c0dSchristos		elif [ "0$minor" -lt "0$instminor" ]; then
12333b31c0dSchristos			if [ $error -eq 0 -a $quiet -eq 0 ]; then
12433b31c0dSchristos				echo "$ms" 1>&2
12533b31c0dSchristos			fi
12633b31c0dSchristos			echo $1 1>&2
12733b31c0dSchristos			error=1
12833b31c0dSchristos		fi
12933b31c0dSchristos	elif [ "0$major" -lt "0$instmajor" ]; then
13033b31c0dSchristos		if [ $error -eq 0 -a $quiet -eq 0 ]; then
13133b31c0dSchristos			echo "$ms" 1>&2
13233b31c0dSchristos		fi
13333b31c0dSchristos		echo $1 1>&2
13433b31c0dSchristos		error=1
13533b31c0dSchristos	fi
13633b31c0dSchristos}
1377c323c9fSerh
13833b31c0dSchristoswhile getopts df:shqv: f; do
13933b31c0dSchristos	case $f in
14033b31c0dSchristos	d)	usedir=1
141792cde2dSerh		if [ $usefile -eq 1 -o $usesets -eq 1 ]; then
14233b31c0dSchristos			Usage
143792cde2dSerh		fi;;
14433b31c0dSchristos	f)	usefile=1; arg1=$OPTARG
145792cde2dSerh		if [ $usedir -eq 1 -o $usesets -eq 1 ]; then
14633b31c0dSchristos			Usage
147792cde2dSerh		fi;;
14833b31c0dSchristos	s)	usesets=1
149792cde2dSerh		if [ $usedir -eq 1 -o $usefile -eq 1 ]; then
15033b31c0dSchristos			Usage
151792cde2dSerh		fi;;
1526619cb95Schristos	v)	shlib_version=$OPTARG;;
1536619cb95Schristos	q)	quiet=1;;
15433b31c0dSchristos	*)	Usage;;
155792cde2dSerh	esac
156792cde2dSerhdone
157792cde2dSerh
15833b31c0dSchristosshift $(($OPTIND - 1))
15933b31c0dSchristos
160792cde2dSerhif [ $usedir -eq 0 -a $usefile -eq 0 -a $usesets -eq 0 ]; then
16133b31c0dSchristos	Usage
1627c323c9fSerhfi
1637c323c9fSerh
164792cde2dSerhif [ $usefile -eq 1 ]; then
165792cde2dSerh	LIBLIST="$arg1"
1667c323c9fSerhelse
16733b31c0dSchristos	if ! mkdir -m 0700 $TMP; then
16833b31c0dSchristos		echo "$PROG: Unable to create temp directory." 1>&2
169792cde2dSerh		exit 2
1707c323c9fSerh	fi
171792cde2dSerh	LIBLIST=$TMP/libs.lst
172792cde2dSerhfi
173792cde2dSerh
174792cde2dSerh# Build list from the installed libraries.
175792cde2dSerhif [ $usedir -eq 1 ]; then
17633b31c0dSchristos	if [ -n "$1" ]; then
177792cde2dSerh		libdir="$1"
178792cde2dSerh	fi
179006c21d4Serh	for f in $libdir; do
180006c21d4Serh		ls $f/lib*.so.*.*
1818b8a7d2cSerh	done > $LIBLIST 2> /dev/null
182792cde2dSerhfi
183792cde2dSerh
184792cde2dSerh# Build list from set lists.  Parameter may be either
185792cde2dSerh# the "lists" directory or the top of the source tree.
186792cde2dSerhif [ $usesets -eq 1 ]; then
18733b31c0dSchristos	if [ -n "$1" ]; then
188792cde2dSerh		setsdir="$1"
189792cde2dSerh		if [ -d "$setsdir/distrib/sets/lists" ]; then
190792cde2dSerh			setsdir="$setsdir/distrib/sets/lists"
191792cde2dSerh		fi
192792cde2dSerh	fi
193792cde2dSerh	(cd $setsdir;
19433b31c0dSchristos	 cat */[a-z]* |
19533b31c0dSchristos	 grep '^./usr/lib/lib.*\.so\.[0-9][0-9]*\.[0-9][0-9]*' |
19633b31c0dSchristos	 sort -u > $LIBLIST)
197792cde2dSerhfi
198792cde2dSerh
199792cde2dSerh#
200792cde2dSerh# The file $LIBLIST now contains a list of libraries.
201792cde2dSerh#
20233b31c0dSchristosif [ -z "$2" ]; then
203792cde2dSerh	makefile=$CWD/Makefile
20433b31c0dSchristos	libname=$(grep '^LIB=' $makefile | sed -e 's/^LIB=[[:space:]]*//')
205792cde2dSerh
206792cde2dSerh	# Assume the library name is the name of the current directory.
20733b31c0dSchristos	if [ -z "$libname" ]; then
20833b31c0dSchristos		libname=$(basename $CWD)
209792cde2dSerh	fi
2107c323c9fSerhelse
2117c323c9fSerh	    libname="$2"
2127c323c9fSerhfi
2138e3f60f3Schristosecho $libname | grep "^lib" 1>&2 2> /dev/null
2148e3f60f3Schristosif [ $? != 0 ]; then
2157c323c9fSerh	libname="lib$libname"
2167c323c9fSerhfi
2177c323c9fSerh
2187c323c9fSerh
2195b354a58Ssimonbif [ ! -f $shlib_version ]; then
22033b31c0dSchristos	echo "$PROG: unable to find $shlib_version" 1>&2
221792cde2dSerh	exit 2
2227c323c9fSerhfi
2237c323c9fSerh
2247c323c9fSerh# Grab major and minor numbers from the source.
2255b354a58Ssimonb. $shlib_version
2267c323c9fSerh
22733b31c0dSchristosif [ -z "$minor" -o -z "$major" ]; then
22833b31c0dSchristos	echo "$PROG: $shlib_version doesn't contain the version." 1>&2
229792cde2dSerh	exit 2
2307c323c9fSerhfi
2317c323c9fSerh
2327c323c9fSerh# Find every shared object library with the same base name.
23333b31c0dSchristosfor instlib in $(grep $libname.so "$LIBLIST"); do
2347c323c9fSerh	# Grab the major and minor from the installed library.
23533b31c0dSchristos	fixone "$instlib"
2367c323c9fSerhdone
2377c323c9fSerh
2387c323c9fSerhexit $error
239