Debian Codegraph @

debian/clean-up-unmanaged-libraries

raw
1 #!/bin/dash
2 # (This script requires either dash or bash due to its use of test -ef.)
3
4 # Copyright 2020 Collabora Ltd.
5 # SPDX-License-Identifier: MIT
6 # (see "Expat" paragraph in debian/copyright)
7
8 set -eu
9
10 # global
11 bug_ref=911225
12 force=
13 me="$0"
14 need_ldconfig=
15 really=yes
16 verbose=
17
18 debug () {
19 [ -z "$verbose" ] || echo "DEBUG: $me: $*" >&2
20 }
21
22 warning () {
23 echo "WARNING: $me: $*" >&2
24 }
25
26 usage () {
27 local status="${1:-2}"
28
29 if [ "$status" -gt 0 ]; then
30 exec >&2
31 fi
32
33 cat <<EOF
34 Usage: $me [OPTIONS] MULTIARCH SONAME...
35
36 Clean up unmanaged copies of SONAME that might still exist
37 in /lib/MULTIARCH after upgrading to a version that is in
38 /usr/lib/MULTIARCH. On non-merged-/usr systems, because /lib/MULTIARCH
39 is higher-precedence than /usr/lib/MULTIARCH, that can result in the
40 old version continuing to be used, breaking versioned dependencies.
41
42 See #911225, #949395 and related bugs.
43
44 Options:
45 --bug-ref=BUG Mention BUG in log messages
46 --dry-run Don't remove the files, just report
47 --force Run even if the system is merged-/usr
48 --verbose Be more verbose
49 EOF
50 exit "$status"
51 }
52
53 do_soname () {
54 local multiarch="$1"
55 local soname="$2"
56 local impl
57 local owner
58 local removal="/lib/$multiarch/removed-by-upgrade-bug$bug_ref"
59 local found_one=
60
61 # We had better not remove the only copy of $soname. This script is
62 # for the situation where it moved from /lib/MULTIARCH/SONAME to
63 # /usr/lib/MULTIARCH/SONAME, so fail in all other cases.
64 if ! [ -e "/usr/lib/$multiarch/$soname" ]; then
65 warning "/usr/lib/$multiarch/$soname does not exist"
66 return 1
67 fi
68
69 # Safety-catch against problems with dpkg-query: if no package is said
70 # to own the new version of the library, fail early.
71 if owner=$(dpkg-query -S "/usr/lib/$multiarch/$soname"); then
72 owner="${owner%%:*}"
73 debug "/usr/lib/$multiarch/$soname is owned by $owner"
74 else
75 warning "/usr/lib/$multiarch/$soname is not owned by a package?"
76 return 1
77 fi
78
79 for impl in "/lib/$multiarch/$soname".*; do
80 if ! [ -e "$impl" ]; then
81 continue
82 fi
83
84 found_one=yes
85
86 if owner=$(dpkg-query -S "$impl"); then
87 owner="${owner%%:*}"
88 warning "$impl is owned by $owner, not deleting"
89 continue
90 fi
91
92 if [ "/usr$impl" -ef "$impl" ] && owner=$(dpkg-query -S "/usr$impl"); then
93 owner="${owner%%:*}"
94 warning "/usr$impl is owned by $owner, not deleting"
95 continue
96 fi
97
98 warning "$impl is not owned by any package"
99 warning "Related files:"
100 ls -il \
101 "/lib/$multiarch/$soname" \
102 "/lib/$multiarch/$soname".* \
103 "/usr/lib/$multiarch/$soname" \
104 "/usr/lib/$multiarch/$soname".* \
105 >&2 || :
106
107 if [ -n "$really" ]; then
108 warning "Moving $impl into $removal"
109 install -d "$removal"
110 rm -f "$removal/${impl##*/}"
111 mv "$impl" "$removal/."
112 else
113 warning "Not moving $impl into $removal (--dry-run)"
114 fi
115
116 echo >&2
117 need_ldconfig=yes
118 done
119
120 if [ -z "$found_one" ]; then
121 debug "No stray files found at /lib/$multiarch/$soname.*"
122 fi
123 }
124
125 main () {
126 local getopt_temp
127 local multiarch
128 local soname
129
130 getopt_temp="help"
131 getopt_temp="$getopt_temp,bug-ref:"
132 getopt_temp="$getopt_temp,dry-run"
133 getopt_temp="$getopt_temp,force"
134 getopt_temp="$getopt_temp,verbose"
135
136 getopt_temp="$(getopt -o '' --long "$getopt_temp" -n "$me" -- "$@")"
137 eval "set -- $getopt_temp"
138
139 while [ "$#" -gt 0 ]
140 do
141 case "$1" in
142 (--dry-run)
143 really=
144 verbose=yes
145 shift
146 ;;
147
148 (--bug-ref)
149 bug_ref="$2"
150 shift 2
151 ;;
152
153 (--force)
154 force=yes
155 shift
156 ;;
157
158 (--help)
159 usage 0
160 ;;
161
162 (--verbose)
163 verbose=yes
164 shift
165 ;;
166
167 (--)
168 shift
169 break
170 ;;
171
172 (-*)
173 warning "Unknown option: $1"
174 usage 2
175 ;;
176
177 (*)
178 break
179 ;;
180 esac
181 done
182
183 if [ "$#" -lt 2 ]; then
184 warning "A multiarch tuple and at least one SONAME are required"
185 usage 2
186 fi
187
188 multiarch="$1"
189 shift
190
191 if [ -n "$force" ]; then
192 debug "Using force"
193 elif [ "/usr/lib/$multiarch" -ef "/lib/$multiarch" ]; then
194 # On a merged-/usr system, a new library like libglib-2.0.so.0.5000.0
195 # will take precedence over a stale library like
196 # libglib-2.0.so.0.4200.0 in the same directory without us needing
197 # to do anything, so the safe route is to avoid doing anything.
198 debug "Merged-/usr system, no need to do anything without --force"
199 return 0
200 fi
201
202 for soname in "$@"; do
203 do_soname "$multiarch" "$soname"
204 done
205
206 if [ -n "$need_ldconfig" ] && [ -n "$really" ]; then
207 warning "Changes were made, running ldconfig..."
208 ldconfig || ldconfig --verbose
209 elif [ -n "$need_ldconfig" ]; then
210 debug "Would run ldconfig, but skipped due to --dry-run"
211 fi
212 }
213
214 main "$@"
215
216 # vim:set sw=4 sts=4 et: