aboutsummaryrefslogtreecommitdiff
path: root/sbopkglint
blob: 5fd9419264e0bd917fba41442674ea3f775320ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
#!/bin/bash

# Don't edit the next line; use "make version" instead.
VER=0.9.3

: <<EOF
=pod

=head1 NAME

sbopkglint - check Slackware binary packages for common errrors.

=head1 SYNOPSIS

B<sbopkglint> [-k] [-i] [-s] [-c | --color] [-m | --mono] [I<package.t?z> I<...>]

=head1 DESCRIPTION

B<sbopkglint> installs a Slackware package to a temporary directory, then
examines the contents. It finds lots of common problems that aren't
always noticed by SBo script maintainers or the admins.

This is for built packages. If you want to lint your build scripts,
use B<sbolint>(1) instead.

With no package arguments, it looks for a SlackBuild in the current
directory, extracts the PRINT_PACKAGE_NAME information, and tries
to find a package in $OUTPUT (/tmp by default). If found, it checks
that package. It's up to you to know whether the package needs to be
rebuilt (e.g. if you've edited the SlackBuild since the package was
built).

With arguments, it checks the given packages. These must be
supported Slackware package files (.tgz, .txz, .tlz, etc). There's no
requirement that these have to be SBo packages, but a couple of the
tests (e.g. the check for $PRGNAM.SlackBuild in the doc dir) might not
apply to non-SBo builds.

Diagnostics will be logged to B<stdout> and B<stderr>. Exit status will
be 0 if all tests passed, non-zero otherwise.

This script must run as root. If you run it as a normal user, it tries
to re-execute itself via sudo(8) before installing any packages. If
you dislike sudo, you can always run it as root.

Note that B<sbopkglint> is intended to help you, not control you. If
your build fails the tests, and you're 100% sure it's correct, please
contact the maintainer (B<urchlay@slackware.uk>) and explain the
situation. Most likely, B<sbopkglint> will be modified to accomodate your
code, unless something really is wrong with it.

=head1 OPTIONS

Bundling options is supported, e.g. B<-ki> is the same as B<-k -i>.

=over 4

=item B<-k>, B<--keep>

Keep the temporary package install directory instead of deleting it on exit.

=item B<-i>, B<--install-ok>

Disable the "useless-looking install instructions" test. This is
intended for SBo admins mass-linting a ton of packages. INSTALL in
the doc dir is something that exists in thousands of existing builds,
and it's not a major problem. New builds and updates should be linted
without this option, however.

=item B<-s>, B<--slackbuild-missing-ok>

Disable the check for /usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild.
For use with non-SBo packages (including core Slackware packages).

=item B<-c>, B<--color>

Colorize the output (green I<OK>, red I<FAILED>, etc). Default is to use
color only when B<stdout> is a terminal.

=item B<-m>, B<--mono>

Do not colorize the output, even if B<stdout> is a terminal.

=item B<--doc>, B<--help>

View this documentation using perldoc(1), which generally uses your
pager (e.g. less(1) or more(1)) to display it.

=item B<--man>

Convert this documentation to a man page, on B<stdout>.

=item B<-v>, B<--version>

Print the B<sbopkglint> version number on B<stdout> and exit.

=back

=head1 EXIT STATUS

0 (success) if all tests passed for all packages, non-zero if there
were any test failures (or if installpkg failed for some reason).

=head1 FILES

=over 4

=item B<sbolint.d/*.t.sh>

These are the actual tests. They're installed to
I<PREFIX>/share/sbo-maintainer-tools, and they're sourced by
B<sbopkglint> at runtime. Each test script begins with (hopefully)
useful comments that go into more detail than the documentation here.

=back

=head1 TESTS

=head2 pre-doinst

This test is done before B<doinst.sh> is run, unlike the rest of
the tests. It checks for the existence of various files that should
not be in the package, but are OK if generated by B<doinst.sh>,
e.g. I</usr/info/dir>. It also checks for the existence of symlinks
in the package (these also must be created by B<doinst.sh>, not
included in the package archive).

=head2 basic-sanity

=over 4

=item B<->

Top-level directories inside the package must be recognized ones,
such as /bin /etc /usr /opt. Packages shouldn't be installing files
in /tmp, /dev, or /home... and they really shouldn't be inventing
new top-level directories.

=item B<->

The documentation directory must exist and be correctly named, as
/usr/doc/$PRGNAM-$VERSION.

=item B<->

The documentation directory must contain $PRGNAM.SlackBuild. This
check can be disabled with the B<-s> option.

=item B<->

The directories /usr/local, /usr/share/doc, /usr/share/man, /usr/etc
are not allowed in SBo packages.

=item B<->

Some directories (e.g. /usr/bin) may not contain subdirectories.

=item B<->

Some directories (e.g. /usr/share) must *only* contain subdirectories.

=item B<->

All files in /bin, /usr/bin, /sbin, /usr/sbin must be executable (have
at least one +x bit set).

=item B<->

Some directories (e.g. /usr/man, /usr/share/applications) must not
contain files with executable permissions. /usr/doc is not in this
list; neither is /etc (too many existing packages install +x files
there).

=item B<->

Broken symlinks may not exist.

=item B<->

Absolute symlinks may not exist (they should be converted to
relative symlinks). This may seem like nitpicking, but packages
may be installed somewhere besides / (the root dir) with the
-root option to installpkg. If /usr/bin/foo is a link to /usr/bin/bar,
it should be a link to just bar.

=back

=head2 docs

=over 4

=item B<->

Documentation must be installed to /usr/doc/$PRGNAM-$VERSION. If
there's any other directory under /usr/doc, it's incorrect. Some
builds use mis-named doc directories (if it hasn't been fixed by
now, an example is gcc5, which installs docs to /usr/doc/gcc-$VERSION
when it should be gcc5-$VERSION).

=item B<->

Documentation files must be readable by everyone, and owned by root:root.

=item B<->

Doc dir shouldn't contain empty files (0 bytes in length).

=item B<->

Doc dir shouldn't contain install instructions. Specifically, files
named INSTALL, INSTALL.*, or install.txt are flagged (it's impossible
to make this test 100% perfect). This check can be disabled with
the B<-i> option.

=item B<->

Any *.pdf files found in the doc dir are checked with B<pdfinfo> to
make sure they really are PDF files, and not corrupt/damaged.

=back

=head2 noarch

=over 4

=item B<->

If a package has its architecture set to "noarch", it must not contain
any ELF binaries/libraries.

=back

=head2 arch

=over 4

=item B<->

If a package has its architecture set to i?86 or x86_64, all ELF
binaries/libraries must be for the correct arch (no 32-bit code in
64-bit packages, and vice versa).

=item B<->

If a package is i?86, it must not contain /usr/lib64.

=item B<->

If a package is x86_64 and contains shared libraries, they must be
in /lib64 or /usr/lib64 (not /lib or /usr/lib).

=back

=head2 lafiles

=over 4

=item B<->

Packages are no longer allowed to contain libtool archive files (.la)
in /lib, /lib64, /usr/lib, or /usr/lib64. However, subdirectories
such as /usr/lib64/someprogram/ are not checked, since some applications
which use plugins actually use the .la files.

=back

=head2 manpages

=over 4

=item B<->

All man pages must be readable by everyone, and owned by root:root.

=item B<->

All man pages must be gzipped.

=item B<->

All man pages must be in /usr/man/man[1-9n] or /usr/man/<lang>/man[1-9n].

=item B<->

Man page directories must be mode 755, owned by root:root.

=item B<->

The section numbers in man page filenames must match the section number
in the directory name (e.g. /usr/man/man1/ls.1.gz is OK,
/usr/man/man1/tetris.6.gz is an error).

=item B<->

Man pages must actually be man pages (troff markup).

=back

=head2 desktop

=over 4

=item B<->

If there are .desktop files, doinst.sh must run update-desktop-database.

=item B<->

.desktop files must be mode 644, owned by root:root. Slackware's KDE
packages actually break this rule (they install executable .desktop
files), but SBo packages are not allowed to.

=item B<->

Only .desktop files are allowed in /usr/share/applications.

=item B<->

.desktop files must be valid, according to the desktop-file-validate
command. Only actual errors count; warnings don't cause this test to
fail.

=back

=head2 newconfig

=over 4

=item B<->

Any files (outside of /usr/doc) with names ending in .new are flagged.
This might be a bit too restrictive (possibly only check /etc and
/usr/share?)

=back

=head2 doinst

=over 4

=item B<->

If there are icons in /usr/share/icons, .desktop files in /usr/share/applications,
or glib2 schemas in /usr/share/glib-2.0/schemas, there must be a doinst.sh
with appropriate command(s), e.g. update-desktop-database, gtk-update-icon-cache,
glib-compile-schemas.

=back

=head2 icons

=over 4

=item B<->

Files in /usr/share/pixmaps and /usr/share/icons/hicolor must be known
image file types (PNG, SVG, JPEG, etc). Their filename extensions must
match the image type (e.g. file.png must actually be a PNG and not
some other MIME type).

=item B<->

Image files in /usr/share/icons/hicolor/<size>x<size>/* must actually be
the correct pixel size.

=back

=head2 usr_info

=over 4

=item B<->

Files in /usr/info must be valid GNU TexInfo files. Normally these are
gzipped; non-gzipped files will be noted, but some packages require
.info files to be uncompressed because they e.g. display them in a GUI
(so this is not an error).

=item B<->

If there are info files, there must be a doinst.sh which uses
install-info to add them to B</usr/info/dir>.

=item B<->

Ideally, there should also be a douninst.sh which recreates
B</usr/info/dir> after the package's .info files are removed. Since
douninst.sh is still pretty new (first introduced with Slackware
15.0), and since we have thousands of builds with .info files that
don't use it, this isn't an error. Lack of douninst.sh will be noted.

=back

=head2 python

=over 4

=item B<->

If a Python module is installed under the name UNKNOWN (e.g.
/usr/lib64/python3.9/site-packages/UNKNOWN-1.2.3/), that module was
built incorrectly and is non-functional, so it'll be flagged as an
error.

=back

=head2 tmp_path

=over 4

=item B<->

No file in the package may have B<$TMP/package-*> included in the
file. This includes both text and binary files.

For this check to work properly, the B<TMP> environment variable used
to build the package should also be set when running B<sbopkglint>. If
you didn't set B<TMP>, the default is I</tmp/SBo>, which is the same
as the default B<TMP> in all SBo SlackBuild scripts.

=back

=head2 static_libs

If there are static libraries in /usr/lib or /usr/lib64:

=over 4

=item B<->

B<*.a> files are checked to make sure they really are static libraries.

=item B<->

Permissions must be 0644 or 044, owner root:root.

=item B<->

If a libI<foo>.a file has a corresponding libI<foo>.so, a note is printed,
suggesting removal of the B<.a> file. This is not an error, just food for thought.

=back

=head1 BUGS

Probably many. This is still a work in progress.

One known problem is that the same file can fail multiple tests. E.g.
if you have a man page that's installed executable, it will fail both
the basic-sanity test and the manpages test. This isn't really a huge
problem, so it might not be fixed any time soon.

=head1 AUTHOR

B. Watson <urchlay@slackware.uk>, aka Urchlay on Libera IRC.

=head1 SEE ALSO

B<sbolint>(1), B<sbodl>, B<sbofixinfo>(1), B<sbopkg>(8), B<sboinstall>(1)

=cut
EOF

SELF="$( basename $0 )"

COLOR=${COLOR:-auto}

# Note: list long options with the -- but short ones without the - here.
parse_option() {
	case "$1" in
		--version|v) echo $VER ; exit 0 ;;
		--h*|--doc|h) exec perldoc "$0" ;;
		--man) exec pod2man --stderr -s1 -csbo-maintainer-tools -r$VER "$0" ;;
		--keep|k) KEEP=1 ;;
		--install-ok|i) INSTALL_DOCS_OK=1 ;;
		--slackbuild-missing-ok|s) SLACKBUILD_MISSING_OK=1 ;;
		--color|c) COLOR=yes ;;
		--mono*|m) COLOR=no  ;;
		*) echo "$SELF: invalid option '$1', try '$SELF --help'" ; exit 1 ;;
	esac
}

while true; do
	case "$1" in
		--) shift ; break ;;
		--*) parse_option "$1" ; shift ;;
		-*)
			opt="$( printf "%s" "$1" | sed -e 's,^-,,' -e 's,\(.\),\1 ,g' )"
			for i in $opt; do
				parse_option "$i"
			done
			shift ;;
		*) break ;;
	esac
done

# where the test scripts live, space-separated list. to allow for
# running sbopkglint from within its own directory, without installing it,
# check for a sbopkglint.d in the script's dir.
script_dir="$( dirname $( realpath $0 ) )"

# this line gets modified by 'make install'.
prefix_dir="@PREFIX@"

case "$prefix_dir" in
  @*) ;; # unset, don't use.
  *)  prefix_script_dir+=" $prefix_dir/share/sbo-maintainer-tools/sbopkglint.d"
esac

SBOPKGLINT_PATH=${SBOPKGLINT_PATH:-"$script_dir/sbopkglint.d $prefix_script_dir"}

if [ "$(id -u)" != "0" ]; then
	exec sudo \
		TMP="$TMP" \
		OUTPUT="$OUTPUT" \
		INSTALL_DOCS_OK="$INSTALL_DOCS_OK" \
		KEEP="$KEEP" \
		SBOPKGLINT_PATH="$SBOPKGLINT_PATH" \
		SLACKBUILD_MISSING_OK="$SLACKBUILD_MISSING_OK" \
		COLOR="$COLOR" \
		"$0" -- "$@"
fi

if [ "$COLOR" = "auto" ]; then
	if [ -t 1 ]; then
		COLOR=yes
	else
		COLOR=no
	fi
fi

if [ "$COLOR" = "yes" ]; then
	RED="\033[1;31m"
	GREEN="\033[1;32m"
	COLOR_OFF="\033[0m"
fi

echo_OK() {
	echo -e "${GREEN}OK${COLOR_OFF}"
}

echo_FAILED() {
	echo -e "${RED}FAILED${COLOR_OFF}"
}

warn() {
	[ "$warncount" = "0" ] && echo
	: $(( warncount ++ ))
	echo -e "${RED}---${COLOR_OFF} $@" 1>&2
}

note() {
	[ "$warncount" = "0" ] && echo
   echo -e "${GREEN}___ note:${COLOR_OFF} $@" 1>&2
}

die() {
	warn "$@"
	exit 1
}

find_warnfiles() {
	local msg
	local output=.files.$RANDOM
	local note=0

	if [ "$1" = "--note" ]; then
		note=1
		shift
	fi

	msg="$1"
	shift

	find "$@" -print0 | xargs -r0 ls -bld &> $output
	if [ -s $output ]; then
		if [ "$note" = "0" ]; then
			warn "$msg"
		else
			note "$msg"
		fi
		cat $output 1>&2
	fi
	rm -f $output
}

# return true if the file is gzipped, otherwise false.
# faster than using file(1).
is_gzipped() {
	[ "$( hexdump -n 2 -e '"%2x"' "$1" )" = '8b1f' ]
}

# N.B. these need to match the template (and they do)
TMP=${TMP:-/tmp/SBo}
OUTPUT=${OUTPUT:-/tmp}

exit_status=0

if [ -n "$1" ]; then
	packages="$@"
else
	cnt="$( /bin/ls *.SlackBuild | wc -l )"
	case "$cnt" in
		0) die "No argument given and no SlackBuild script in current dir" ;;
		1) ;; # OK
		*) die "Multiple SlackBuild scripts in current dir" ;;
	esac
	script="$( /bin/ls *.SlackBuild )"
	if ! grep -q PRINT_PACKAGE_NAME $script; then
		die "$script doesn't support PRINT_PACKAGE_NAME"
	fi
	filename="$( PRINT_PACKAGE_NAME=1 sh $script )"
	packages="$OUTPUT/$filename"
	if [ ! -e "$packages" ]; then
		die "Can't find $packages"
	fi
fi

for testdir in $SBOPKGLINT_PATH; do
	[ -d $testdir ] || continue
	testdir="$( realpath $testdir )"
	break
done
[ -z "$testdir" -o "$testdir/*.t.sh" = '*.t.sh' ] && \
	die "Can't find any tests to run, looked in: $SBOPKGLINT_PATH"

echo "Using tests from $testdir"

if echo "$packages" | grep -q ' '; then
	want_summary=1
fi

faillist=""
passcount=0
failcount=0

# 20231204 bkw: Bad Things Happen if $TMP doesn't exist.
mkdir -p "$TMP"

for package in $packages; do
	if [ ! -e "$package" ]; then
		warn "$package does not exist"
		echo_FAILED
		continue
	fi

	filename="$( basename $package )"

	ARCH="$(    echo $filename | rev | cut -d- -f2  | rev )"
	PRGNAM="$(  echo $filename | rev | cut -d- -f4- | rev )"
	VERSION="$( echo $filename | rev | cut -d- -f3  | rev )"

	PKG="$( mktemp -d $TMP/sbopkglint.XXXXXX )"
	if [ "$PKG" = "" ]; then
	  warn "Can't create temp directory in $TMP, bailing!"
	  exit 1
	fi

	totalwarns=0
	foundtests=0

	# pre-doinst test requires extracting the package *without*
	# running its doinst.sh. this is so we can check for e.g.
	# /usr/info/dir existing in the package (rather than being
	# created by doinst, which would be OK).
	echo -n "Exploding $package to $PKG ..."
	olddir="$( pwd )"
	package_fullpath="$( realpath $package )"
	cd "$PKG"
	/sbin/explodepkg "$package_fullpath" &> $PKG/.tmp.$$

	# 20230319 bkw: explodepkg *never* exits with non-zero status, not
	# even if the package file doesn't exist. don't bother checking.
	# check for install/ dir instead: every valid slackware package has
	# to have this dir.
	if [ ! -d install ]; then
		cat $PKG/.tmp.$$
		warn "not a valid Slackware package"
		echo_FAILED
		rm -rf $PKG
		exit_status=1
		continue
	fi
	echo_OK

	echo -n "Running pre-doinst test..."
	warncount=0
	source $testdir/pre-doinst.sh
	if [ "$warncount" = "0" ]; then
		echo_OK
	else
		echo_FAILED
		: $(( totalwarns += warncount ))
	fi
	cd "$olddir"
	rm -rf $PKG
	mkdir -p $PKG

	# now the "real" tests
	echo -n "Installing $package to $PKG ..."
	/sbin/installpkg -root "$PKG" "$package" &> $PKG/.tmp.$$
	S="$?"

	if [ "$S" != "0" ]; then
		echo_FAILED
		cat $PKG/.tmp.$$
		echo "installpkg exited with status $S"
		[ "$KEEP" = "" ] && rm -rf $PKG
		exit_status=1
		continue
	fi

	echo_OK
	rm -f $PKG/.tmp.$$

	olddir="$( pwd )"
	cd "$PKG"

	for testscript in $testdir/*.t.sh; do
		foundtests=1
		(
			warncount=0
			echo -n "Running test: $( basename $testscript .t.sh )..."
			source "$testscript"
			if [ "$warncount" = "0" ]; then
				echo_OK
			else
				echo_FAILED
				echo "$warncount" > .tmp.warncount
			fi
		)
		if [ -e .tmp.warncount ]; then
			warns="$( cat .tmp.warncount )"
			: $(( totalwarns += warns ))
		fi
		rm -f .tmp.warncount
	done

	cd "$olddir"

	[ "$KEEP" = "" ] && rm -rf "$PKG"

	if [ "$foundtests" = "0" ]; then
		die "!!! can't find any tests to run in $testdir."
	fi

	if [ "$totalwarns" = "0" ]; then
		echo -e "=== $filename: ${GREEN}All tests passed${COLOR_OFF}"
		: $(( passcount ++ ))
	else
		exit_status=1
		echo -e "!!! $filename: ${RED}$totalwarns failures${COLOR_OFF}"
		: $(( failcount ++ ))
		faillist+="$filename "
	fi
	[ "$want_summary" = "1" ] && echo
done

if [ "$want_summary" = "1" ]; then
	count=$(( failcount + passcount ))
	echo "=== $count packages linted, $passcount passed, $failcount failed"
	if [ "$failcount" = "0" ]; then
		echo -e "=== ${GREEN}All packages check out OK${COLOR_OFF}"
	else
		echo -e "!!! ${RED}Failed packages:${COLOR_OFF}"
		for i in $faillist; do
			echo $i
		done
	fi
fi

exit $exit_status