Commit Graph

14972 Commits

Author SHA1 Message Date
Karel Zak 618a6ccbfd libsmartcols: use lib/jsonwrt.c for JSON
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-11-12 11:21:50 +01:00
Karel Zak 37bcd05602 lib/jsonwrt: add new functions to write in JSON
We need JSON formatting stuff also outside libsmartcols.

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-11-12 11:19:13 +01:00
Pali Rohár 8e3c0176bd libblkid: udf: add support for unclosed sequential Write-Once media 2020-11-10 23:40:53 +01:00
Pali Rohár 6408acd9e0 libblkid: udf: add support for multisession via session_offset hint
To read multisession UDF disc it is required to probe VSD and AVDP from the
selected session (specified by session_offset) and then read other volume
descriptors from absolute location specified in AVDP, which is in most
cases in previous session.

So it is required for udf detector to be able to read data from any
location of disc and therefore blkid's --offset argument cannot be used to
access multisession optical disc.

Only blkid's --hint session_offset argument can be used to instruct blkid
to read multisession optical disc or disc images correctly.

Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
2020-11-10 23:40:53 +01:00
Pali Rohár cf2f10bb59 libblkid: iso9660: add support for multisession via session_offset hint
The only required change is to read disc from offset specified by
session_offset instead of beginning of disc (offset 0).

So either blkid's --offset or --hint session_offset argument would work for
correct detection of multisession ISO9660 optical disc or disc image.

Although the ISO9660 specification allows also non-2kB sector sizes,
current blkid implementation has hardcoded ISO9660 sector size to 2kB.
Therefore there is check that session_offset is multiple of 2048.

Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
2020-11-10 23:40:53 +01:00
Pali Rohár b5c5b42014 libblkid: fix blkid_probe_get_sb() to use hint offset calculation 2020-11-10 23:40:53 +01:00
Pali Rohár 3e31657db5 libblkid: allow to specify offset defined by hint for blkid_probe_get_idmag()
Extends struct blkid_idmag with a new member hoff which specify name of the
hint used for blkid_probe_get_hint() function to retrieve offset to kboff.

blkid_probe_get_idmag() will use supplied hint offset in bytes as a start
position from which the final offsets for magic strings are calculated.

Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
2020-11-10 23:40:53 +01:00
Pali Rohár 427ea35512 libblkid: detect session_offset hint for optical discs
When offset and session_offset hint are not specified then sets
session_offset hint from CDROMMULTISESSION ioctl which returns start
position of the last session of multisession optical disc.

It is expected that session_offset hint contains position offset in bytes
where starts session of multisession optical disc which should be read.

Filesystems designed for optical discs are expected to use this value when
dealing with multisession optical disc to know where they should start
reading optical disc or image of optical disc.

Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
2020-11-10 23:40:52 +01:00
Pali Rohár bfd4da5628 libblkid: do size correction of optical discs also by last written sector
Everything after last written sector is undefined.

Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
2020-11-10 23:21:14 +01:00
Pali Rohár 6b88a410ae libblkid: detect CD/DVD discs in packet writing mode
Packet writing mode for CD and DVD discs is provided by kernel module
pktcdvd.ko which exports block devices /dev/pktcdvd/pktcdvd*.

These block devices do not accept CDROM_DRIVE_STATUS ioctl, therefore
current blkid code does not detect them as BLKID_FL_CDROM_DEV and also
does not do necessary size correction.

Extend detection of CD/DVD discs by additional CDROM_LAST_WRITTEN ioctl
which is accepted also by pktcdvd.ko.

Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
2020-11-10 23:21:13 +01:00
Karel Zak 32d81aff5b Merge branch 'libblkid' of https://github.com/ferivoz/util-linux 2020-11-10 13:37:30 +01:00
Karel Zak ab5d18e973 tests: (ul) remove another 'dim' input
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-11-10 13:36:32 +01:00
Samanta Navarro c70b4f2a5b libblkid: limit amount of parsed partitions
The linux kernel does not support more than 256 partitions
(DISK_MAX_PARTS). The atari and mac block devices have no such limits.

Use dos logical partition limit for atari as well (100).
Use the kernel limit for mac (256).

Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
2020-11-10 11:53:16 +00:00
Samanta Navarro 3fbeb9eeb8 libblkid: fix time_t handling
The time_t data type is a signed integer. A signed integer overflow is
not defined in C programming language.

A signed overflow occurs on 32 bit systems with 32 time_t for loop back
devices, e.g. when calling "blkid /dev/loop0". This happens because
bid_time is set to INT_MIN and the diff calculation cannot store the
result in time_t (positive int - INT_MIN > INT_MAX).

This fix changes the code to use an unsigned integer calculation. It
pretty much means that the code works as before, but well defined in C.
Checking diff to be positive protects the code against system setups
with dates before 1970 as well.

The time_t data type on modern Linux systems is 64 bit even for 32 bit
systems. Since long is 32 bit on these systems, long long is a better
data type for 64 bit output.

Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
2020-11-10 11:52:45 +00:00
Karel Zak b7bca24476 libblkid: overwrite existing hint
Addresses: https://github.com/karelzak/util-linux/issues/1161
Suggested-by:  Pali Rohár <pali.rohar@gmail.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-11-09 17:11:51 +01:00
Karel Zak e34abedb6e tests: remove ul(1) 'dim' input
It's not supported by all terminals.

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-11-09 16:41:46 +01:00
Sami Kerola bd66eb2e43 tests: move misc/ul to ul/ directory
Reference: https://github.com/karelzak/util-linux/pull/1165#issuecomment-715173662
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-11-09 16:31:59 +01:00
Karel Zak 782cc33a02 build-sys: fix sendfile use
* OSX uses different prototype
* include file is not covered by HAVE_SYS_SENDIFLE_H

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-11-09 15:30:06 +01:00
Karel Zak e577bea10e wipefs: (man) add hint to erase on partitions and disk
Addresses: https://github.com/karelzak/util-linux/issues/1177
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-11-09 12:54:18 +01:00
Karel Zak df8a8d1373 Merge branch 'libuuid' of https://github.com/ferivoz/util-linux
* 'libuuid' of https://github.com/ferivoz/util-linux:
  libuuid: check quality of random bytes
2020-11-09 12:31:17 +01:00
Karel Zak 248c239b27 libblkid: export blkid_probe_reset_hints()
* make hints persistent and do not call blkid_probe_reset_hints() in
  blkid_probe_set_device()
* export blkid_probe_reset_hints() by API to keep it completely in
  user's hands

Reported-by: Pali Rohár <pali.rohar@gmail.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-11-09 11:42:58 +01:00
Karel Zak bb123ad503 Merge branch 'vipw-shortwrite' of https://github.com/DankRank/util-linux
* 'vipw-shortwrite' of https://github.com/DankRank/util-linux:
  ul_copy_file: make defines for return values
  read_all: return 0 when EOF occurs after 0 bytes
  ul_copy_file: add test program
  ul_copy_file: handle EAGAIN and EINTR
  ul_copy_file: use all_read/all_write
  ul_copy_file: use BUFSSIZ for buffer size
  nologin: use ul_copy_file
  login: use ul_copy_file
  configure.ac: check for sendfile
  ul_copy_file: use sendfile
  vipw: move copyfile to the lib
  vipw: fix short write handling in copyfile
2020-11-09 11:06:27 +01:00
Karel Zak e5781b3d3b flock: keep -E exit status more restrictive
Addresses: https://github.com/karelzak/util-linux/issues/1180
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-11-09 10:53:23 +01:00
Egor Chelak cabbf61fab ul_copy_file: make defines for return values
Suggested-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Egor Chelak <egor.chelak@gmail.com>
2020-11-09 07:20:14 +02:00
Egor Chelak 418eb09482 read_all: return 0 when EOF occurs after 0 bytes
Originally it would return -1 (without setting errno) if the fd was
already at EOF when you called read_all.

This is already fixed in sendfile_all.

Reviewed-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Egor Chelak <egor.chelak@gmail.com>
2020-11-09 07:19:51 +02:00
Egor Chelak 7951164c1f ul_copy_file: add test program
Suggested-by: Karel Zak <kzak@redhat.com>
Reviewed-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Egor Chelak <egor.chelak@gmail.com>
2020-11-09 07:19:43 +02:00
Egor Chelak 212bde6cf7 ul_copy_file: handle EAGAIN and EINTR
I did this by implementing a function called sendfile_all() similar to
read_all()/write_all().

The manpage for sendfile doesn't mention EINTR, but I decided to check
it anyway, just in case.

Suggested-by: Karel Zak <kzak@redhat.com>
Reviewed-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Egor Chelak <egor.chelak@gmail.com>
2020-11-09 07:19:39 +02:00
Egor Chelak f19a16550a ul_copy_file: use all_read/all_write
Suggested-by: Karel Zak <kzak@redhat.com>
Reviewed-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Egor Chelak <egor.chelak@gmail.com>
2020-11-09 07:19:36 +02:00
Egor Chelak e453099640 ul_copy_file: use BUFSSIZ for buffer size
Suggested-by: Karel Zak <kzak@redhat.com>
Reviewed-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Egor Chelak <egor.chelak@gmail.com>
2020-11-09 07:19:30 +02:00
Egor Chelak 9203b41a4f nologin: use ul_copy_file
Suggested-by: Sami Kerola <kerolasa@iki.fi>
Reviewed-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Egor Chelak <egor.chelak@gmail.com>
2020-11-09 07:19:24 +02:00
Egor Chelak 8fcdbefb7b login: use ul_copy_file
Suggested-by: Karel Zak <kzak@redhat.com>
Reviewed-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Egor Chelak <egor.chelak@gmail.com>
2020-11-09 07:19:15 +02:00
Egor Chelak 360cdaa6c7 configure.ac: check for sendfile
Do note that according to man sendfile, "Other UNIX systems implement
sendfile() with different semantics and prototypes."
If this is something we care about, a better check is needed.

Suggested-by: Karel Zak <kzak@redhat.com>
Reviewed-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Egor Chelak <egor.chelak@gmail.com>
2020-11-09 07:19:00 +02:00
Egor Chelak a8b4e7cad1 ul_copy_file: use sendfile
Suggested-by: Karel Zak <kzak@redhat.com>
Reviewed-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Egor Chelak <egor.chelak@gmail.com>
2020-11-09 07:18:49 +02:00
Egor Chelak b9dcd38462 vipw: move copyfile to the lib
Also, a bug in pw_tmpfile was fixed: copyfile used tmp_file to report
errors, but pw_tmpfile only assigned that variable _after_ calling
copyfile.

Suggested-by: Sami Kerola <kerolasa@iki.fi>
Reviewed-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Egor Chelak <egor.chelak@gmail.com>
2020-11-09 07:18:42 +02:00
Samanta Navarro e4be3ee01d libuuid: check quality of random bytes
If a libuuid application is unable to access /dev/random or /dev/urandom
then uuid generation by uuid_generate falls back to uuid_generate_time.
This could happen in chroot or container environments.

The function ul_random_get_bytes from lib/randutils.c uses getrandom if
it is available. This could either mean that the libuuid application
skips good random bytes because the character special files do not exist
or the application trusts in good random bytes just because these files
are accessible but not necessarily usable, e.g. limit of open file
descriptors reached, lack of data, kernel without getrandom, etc.

This commit modifies ul_random_get_bytes to return an integer which
indicates if random bytes are of good quality (0) or not (1). Callers
can decide based on this information if they want to discard the random
bytes. Only libuuid checks the return value. I decided to return 1
instead of -1 because -1 feels more like an error, but weak random bytes
can be totally fine.

Another issue is that getrandom sets errno to specific values only in
case of an error, i.e. with return value -1. Set errno to 0 explicitly
if getrandom succeeds so we do not enter the fallback routine for
ENOSYS by mistake. I do not think that this is likely to happen, but it
really depends on possible wrapper function supplied by a C library.

Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
2020-11-08 11:48:23 +00:00
Samanta Navarro 8f22adaaf3 libblkid: allow a lot of mac partitions
If the map count is set to INT_MAX then the for loop does not stop
because its check is never false.

I have not found a correct upper limit. The other partition logics have
a maximum amount (exception is atari.c).

The loop itself wouldn't be endless. If the iteration reaches block 0
then the signature will be wrong. This means that map count = INT_MAX
case would fail even if such a setup would be correct on disk.

Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
2020-11-08 11:47:33 +00:00
Samanta Navarro 02162731c1 libblkid: fix memory leak in config parser
Multiple occurrences of CACHE_FILE lead to memory leaks.
Also if last occurrence of CACHE_FILE is empty then cache file is not
set to NULL again.

An example /etc/blkid.conf could be:

CACHE_FILE=/tmp/cache1
CACHE_FILE=/tmp/cache2
CACHE_FILE=

I would expect that CACHE_FILE is empty but actually it is still
/tmp/cache2.

Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
2020-11-08 11:47:33 +00:00
Karel Zak ee7f4bee9b sfdisk: fix backward --move-data
* fix final message where number of moved sectors overflow number of
  all moved sectors

* align last step size before we use it for backward mode source and
  destination offsets calculation

Addresses: https://github.com/karelzak/util-linux/issues/1176
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-11-06 11:38:51 +01:00
Karel Zak de9cbcdcef Merge branch 'whereis' of https://github.com/ferivoz/util-linux
* 'whereis' of https://github.com/ferivoz/util-linux:
  whereis: extend test case
  whereis: filter bin, man and src differently
  whereis: do not strip suffixes
  whereis: do not ignore trailing numbers
  whereis: add --disable-whereis to configure
  whereis: add lib32 directories
  whereis: support zst compressed man pages
  whereis: fix out of boundary read
2020-11-06 09:44:52 +01:00
Karel Zak 028043c2d5 Merge branch 'sfdisk-json-dump' of https://github.com/xnox/util-linux 2020-11-05 11:37:51 +01:00
Dimitri John Ledkov d7101e5911
sfdisk: correct --json --dump false exclusive
`--json` implies `--dump`, thus `--json --dump` must be
allowed. `--list-free` is incompatible with `--dump`, and thus also
with `--json`. Currently `--json --dump` is prohibited, even though
`--list-free` is not specified at all.

Regression introduced in 03154d2cf2.

Signed-off-by: Dimitri John Ledkov <xnox@ubuntu.com>
2020-11-04 12:47:55 +00:00
Samanta Navarro 062e6a3945 whereis: extend test case
Previous commits are covered with these test cases.

Removed dependency on system layout.

Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
2020-11-04 11:43:09 +00:00
Samanta Navarro 653f672ab6 whereis: filter bin, man and src differently
Consider "s." prefixes for source code files only (even though I do not
know which VCS does that), compression suffixes for manual pages and
strict matching for executables.

Calling "whereis python3" is kind of okay to return python3.8 next to
python3, but python3.8-config is not the same tool as python3.

Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
2020-11-04 11:43:09 +00:00
Samanta Navarro 117ddbeedc whereis: do not strip suffixes
The whereis implementations of FreeBSD, macOS, NetBSD, and OpenBSD do
not strip suffixes. Although whereis is not a POSIX tool and has no
shared standard, even its manual page indicates that the supplied names
are command names.

Commands do not have a suffix on Linux systems.

Stripping suffixes actually leads to issues with tools like fsck.ext4,
since fsck.ext4 is not the same tool as fsck and definitely not the same
tool as fsck.minix.

Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
2020-11-04 11:43:09 +00:00
Samanta Navarro d95ee9fd75 whereis: do not ignore trailing numbers
The commands diff and diff3 are so distinct that their manual pages
should not be mixed in whereis output.

Theoretically this works for commands and binaries with links to each
other, e.g. gpg and gpg2, but if gpg is version 1 and gpg2 is version 2
then manual pages do not match either.

Also the while loop does not decrement "i" while incrementing "dp". The
effect of this is that the output of whereis depends on manual pages
being compressed or not.

The easiest solution is to not ignore trailing numbers.

Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
2020-11-04 11:43:09 +00:00
Samanta Navarro b7fc158a2e whereis: add --disable-whereis to configure
Allow a build of util-linux without whereis.

Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
2020-11-04 11:43:09 +00:00
Samanta Navarro 74b7c01fa6 whereis: add lib32 directories
These directories are sometimes used by Linux distributions.

Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
2020-11-04 11:43:09 +00:00
Samanta Navarro 6065250a4d whereis: support zst compressed man pages
Add zst as extension for manual pages. Current version of man-db
supports zst extension out of the box.

Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
2020-11-04 11:43:09 +00:00
Samanta Navarro cd4142f7c3 whereis: fix out of boundary read
If whereis encounters a short file name then an out of boundary
read can occur.

Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
2020-11-04 11:43:09 +00:00
Samanta Navarro bd0f347f86 misc: fix typos
Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
2020-11-04 11:42:28 +00:00