1#!/bin/sh
2#         $NetBSD: checkver,v 1.18 2021/12/05 08:09:30 msaitoh Exp $
3#
4# Copyright (c) 1998 The NetBSD Foundation, Inc.
5# All rights reserved.
6#
7# This code is derived from software contributed to The NetBSD Foundation
8# by Eric Haszlakiewicz.
9#
10# Redistribution and use in source and binary forms, with or without
11# modification, are permitted provided that the following conditions
12# are met:
13# 1. Redistributions of source code must retain the above copyright
14#    notice, this list of conditions and the following disclaimer.
15# 2. Redistributions in binary form must reproduce the above copyright
16#    notice, this list of conditions and the following disclaimer in the
17#    documentation and/or other materials provided with the distribution.
18#
19# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29# POSSIBILITY OF SUCH DAMAGE.
30#
31
32#--------------------------------------------------------------------#
33# checkver -
34#         Check for libraries that appear to be newer than the
35#         one we're about to install.
36#
37# checkver [-q] [-v shlib_version_file] -d [installedlibdir [library name]]"
38# checkver [-q] [-v shlib_version_file] -s [setlistdir [library name]]"
39# checkver [-q] [-v shlib_version_file] -f liblistfile [library name]"
40#
41# One of -d, -s or -f must be specified.
42#
43# all: If library name is not specified it is assumed to
44#         be the name of the current directory.
45#
46# -d: Checks the version against the installed libraries.
47#         If no further arguments are given "/usr/lib" is
48#         used as the location of installed libraries.
49#
50# -s: Checks the version against the sets.  If no argument
51#         follows the sets directory defaults to "/usr/src/distrib/sets/lists".
52#         The directory may be specified as either the top of the
53#         source tree or as the lists directory.
54#
55# -f: Checks the version against the provided list.  A filename
56#         must be supplied.
57#
58# -v: Specify the filename of the shlib_version file.  Defaults
59#     to "./shlib_version".
60#
61# This script produces no output if all library version are not
62# large than the source version.  If an installed library with a
63# version greater than the source is found, checkver prints a
64# header and a list of the names of the offending installed libraries.
65#
66# The header may be suppressed by passing "-q" as the first argument.
67#
68
69TMP=/tmp/checkver.$$
70PROG="$(basename "$0")"
71# Can't trap 11 (SEGV) in the Real Bourne Shell, since it uses it for
72# internal malloc!
73trap "exit 2" 1 2 3 4 5 6 7 8 10 12 13 14 15
74trap "[ -d $TMP ] && rm -rf $TMP" 0
75
76Usage() {
77cat << EOF 1>&2
78Usage: $PROG [-q] [-v version_file] -d [installedlibdir [library name]]
79       $PROG [-q] [-v version_file] -s [setlistdir [library name]]
80       $PROG [-q] [-v version_file] -f liblistfile [library name]
81       $PROG is a script that looks for installed libraries with
82       versions greater than that in the version file.
83       For more information, read the comments.
84EOF
85          exit 1
86}
87
88basedir=/usr/src
89setsdir=$basedir/distrib/sets/lists
90libdir=/usr/lib
91shlib_version=./shlib_version
92
93error=0
94quiet=0
95usedir=0
96usefile=0
97usesets=0
98CWD=$(pwd)
99: ${AWK:=awk}
100
101fixone() {
102          eval $(${AWK} -v 'LIB=$1' '
103BEGIN {
104          gsub(".*\.so\.", "", LIB);
105          split(LIB, VER, ".");
106          printf("local instmajor=%d\n", V[1] + 0);
107          printf("local instminor=%d\n", V[2] + 0);
108          printf("local instteeny=%d\n", V[3] + 0);
109}')
110          local ms="The following libraries have versions greater than the source"
111
112          # If they're greater than the source, complain.
113          if [ "0$major" -eq "0$instmajor" ]; then
114                    if [ "0$minor" -eq "0$instminor" ]; then
115                              if [ "0$teeny" -lt "0$instteeny" ]; then
116                                        if [ $error -eq 0 -a $quiet -eq 0 ]; then
117                                                  echo "$ms" 1>&2
118                                        fi
119                                        echo $1 1>&2
120                                        error=1
121                              fi
122                    elif [ "0$minor" -lt "0$instminor" ]; then
123                              if [ $error -eq 0 -a $quiet -eq 0 ]; then
124                                        echo "$ms" 1>&2
125                              fi
126                              echo $1 1>&2
127                              error=1
128                    fi
129          elif [ "0$major" -lt "0$instmajor" ]; then
130                    if [ $error -eq 0 -a $quiet -eq 0 ]; then
131                              echo "$ms" 1>&2
132                    fi
133                    echo $1 1>&2
134                    error=1
135          fi
136}
137
138while getopts df:shqv: f; do
139          case $f in
140          d)        usedir=1
141                    if [ $usefile -eq 1 -o $usesets -eq 1 ]; then
142                              Usage
143                    fi;;
144          f)        usefile=1; arg1=$OPTARG
145                    if [ $usedir -eq 1 -o $usesets -eq 1 ]; then
146                              Usage
147                    fi;;
148          s)        usesets=1
149                    if [ $usedir -eq 1 -o $usefile -eq 1 ]; then
150                              Usage
151                    fi;;
152          v)        shlib_version=$OPTARG;;
153          q)        quiet=1;;
154          *)        Usage;;
155          esac
156done
157
158shift $(($OPTIND - 1))
159
160if [ $usedir -eq 0 -a $usefile -eq 0 -a $usesets -eq 0 ]; then
161          Usage
162fi
163
164if [ $usefile -eq 1 ]; then
165          LIBLIST="$arg1"
166else
167          if ! mkdir -m 0700 $TMP; then
168                    echo "$PROG: Unable to create temp directory." 1>&2
169                    exit 2
170          fi
171          LIBLIST=$TMP/libs.lst
172fi
173
174# Build list from the installed libraries.
175if [ $usedir -eq 1 ]; then
176          if [ -n "$1" ]; then
177                    libdir="$1"
178          fi
179          for f in $libdir; do
180                    ls $f/lib*.so.*.*
181          done > $LIBLIST 2> /dev/null
182fi
183
184# Build list from set lists.  Parameter may be either
185# the "lists" directory or the top of the source tree.
186if [ $usesets -eq 1 ]; then
187          if [ -n "$1" ]; then
188                    setsdir="$1"
189                    if [ -d "$setsdir/distrib/sets/lists" ]; then
190                              setsdir="$setsdir/distrib/sets/lists"
191                    fi
192          fi
193          (cd $setsdir;
194           cat */[a-z]* |
195           grep '^./usr/lib/lib.*\.so\.[0-9][0-9]*\.[0-9][0-9]*' |
196           sort -u > $LIBLIST)
197fi
198
199#
200# The file $LIBLIST now contains a list of libraries.
201#
202if [ -z "$2" ]; then
203          makefile=$CWD/Makefile
204          libname=$(grep '^LIB=' $makefile | sed -e 's/^LIB=[[:space:]]*//')
205
206          # Assume the library name is the name of the current directory.
207          if [ -z "$libname" ]; then
208                    libname=$(basename $CWD)
209          fi
210else
211              libname="$2"
212fi
213echo $libname | grep "^lib" 1>&2 2> /dev/null
214if [ $? != 0 ]; then
215          libname="lib$libname"
216fi
217
218
219if [ ! -f $shlib_version ]; then
220          echo "$PROG: unable to find $shlib_version" 1>&2
221          exit 2
222fi
223
224# Grab major and minor numbers from the source.
225. $shlib_version
226
227if [ -z "$minor" -o -z "$major" ]; then
228          echo "$PROG: $shlib_version doesn't contain the version." 1>&2
229          exit 2
230fi
231
232# Find every shared object library with the same base name.
233for instlib in $(grep $libname.so "$LIBLIST"); do
234          # Grab the major and minor from the installed library.
235          fixone "$instlib"
236done
237
238exit $error
239