Commit Graph

835 Commits

Author SHA1 Message Date
Karel Zak 558880dd35 Merge branch 'frkaya/loopconfig' of https://github.com/franksinankaya/util-linux
* 'frkaya/loopconfig' of https://github.com/franksinankaya/util-linux:
  loopdev: use LOOP_CONFIG ioctl
2020-10-05 14:51:45 +02:00
Sinan Kaya d5fd456c88 loopdev: use LOOP_CONFIG ioctl
Prefer to use the new LOOP_CONFIG ioctl.
https://lwn.net/Articles/820408/

Signed-off-by: Sinan Kaya <sinan.kaya@microsoft.com>
2020-09-30 20:04:21 -04:00
Karel Zak 1918b4dd9d build-sys: add missing header file
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-09-30 14:03:15 +02:00
Karel Zak 0a9939816c lib/buffer: add simple grow-able buffer
The goal is to use it in libmount when generate options strings
and in libsmartcols to replace libscols_buffer.

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-09-29 12:06:11 +02:00
nl6720 5362bfa3ea
libfdisk: add "Linux /usr" and "Linux /usr verity" GPT partition types
See https://systemd.io/DISCOVERABLE_PARTITIONS/ and https://github.com/systemd/systemd/pull/17101 .

Move ARM after x86-64, so that x86 and x86-64 are next to one another.

Signed-off-by: nl6720 <nl6720@gmail.com>
2020-09-21 18:26:53 +03:00
Karel Zak 14bb8e3ca6 lsblk: ignore only loopdevs without backing file
* do not ignore all empty devices, we need more smart solution

* ignore only loop devices without backing file, for example:
 # touch img
 # losetup -f img
 losetup: img: Warning: file is smaller than 512 bytes; the loop device may be useless or invisible for system tools.

 - old version display nothing
 - new version:

 # lsblk /dev/loop0
 NAME  MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
 loop0   7:0    0   0B  0 loop

Addresses: https://github.com/karelzak/util-linux/issues/1118
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-09-09 12:23:33 +02:00
Soumendra Ganguly 75ccd75a2f script: cleanup --echo
Permanently turn off current stdin ECHO when it is a terminal and enable setting slave ECHO instead.
Fix other minor typos, update documentation.

[kzak@redhat.com: - remove irrelevant changes
                  - keep --echo argument unchanged]

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-08-27 12:56:46 +02:00
Karel Zak e113093ce9 lib/env: add function to save and restore unwanted variables
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-08-25 10:43:07 +02:00
nl6720 80a54e2b3e
libfdisk: add systemd-homed user's home GPT partition type
See https://systemd.io/HOME_DIRECTORY/ .
Additionally update the URL of the Boot Loader Specification.

Signed-off-by: nl6720 <nl6720@gmail.com>
2020-08-22 15:40:06 +03:00
Karel Zak c87cce57d8 Merge branch 'fuzz' of https://github.com/evverx/util-linux
* 'fuzz' of https://github.com/evverx/util-linux:
  tests: pack testcases into zip archives
  tests: integrate test_last_fuzz into the testsuite
  tests: add a fuzzer for process_wtmp_file
  docs: mention OSS-Fuzz and CIFuzz and how to build fuzz targets locally
  tools: make it possible to set all the fuzzing flags with config-gen
  build-system: make "make distcheck" work
  travis: set CXX correctly
  travis: turn on --enable-fuzzing-engine
  build-sys: add support for --enable-fuzzing-engine
  tests: integrate test_mount_fuzz into the testsuite
  tests: take exit codes into account
  tests: add a fuzzer for mnt_table_parse_stream
2020-08-10 14:40:44 +02:00
Evgeny Vereshchagin 0be452a82a build-system: stop looking for %ms and %as
Looks like those specifiers haven't been used since 6c9ab254ae
(where sscanf was removed) was merged. This should help to get
util-linux to compile with MSan. Currently it's failing with
```
...
configure: error: libmount selected, but required scanf string alloc modifier not available
...
configure:20240: ./conftest
==116617==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x496fd6 in main /home/vagrant/util-linux/conftest.c:171:6
    #1 0x7f5eb85ea1a2 in __libc_start_main (/lib64/libc.so.6+0x271a2)
    #2 0x41c2cd in _start (/home/vagrant/util-linux/conftest+0x41c2cd)

SUMMARY: MemorySanitizer: use-of-uninitialized-value /home/vagrant/util-linux/conftest.c:171:6 in main
Exiting
configure:20240: $? = 77
...
configure:20265: $? = 0
configure:20265: ./conftest
MemorySanitizer: bad pointer 0x000000496e60
==116627==MemorySanitizer CHECK failed: /builddir/build/BUILD/compiler-rt-9.0.1.src/lib/msan/../sanitizer_common/sanitizer_allocator_secondary.h:177 "((IsAligned(reinterpret_cast<uptr>(p), page_size_))) != (0)" (0x0, 0x0)
    #0 0x41d1d8 in MsanCheckFailed(char const*, int, char const*, unsigned long long, unsigned long long) (/home/vagrant/util-linux/conftest+0x41d1d8)
    #1 0x484e1e in __sanitizer::CheckFailed(char const*, int, char const*, unsigned long long, unsigned long long) (/home/vagrant/util-linux/conftest+0x484e1e)
    #2 0x42066c in __msan::MsanDeallocate(__sanitizer::StackTrace*, void*) (/home/vagrant/util-linux/conftest+0x42066c)
    #3 0x424bc9 in free (/home/vagrant/util-linux/conftest+0x424bc9)
    #4 0x496fae in main /home/vagrant/util-linux/conftest.c:173:2
    #5 0x7f2245f311a2 in __libc_start_main (/lib64/libc.so.6+0x271a2)
    #6 0x41c2cd in _start (/home/vagrant/util-linux/conftest+0x41c2cd)

configure:20265: $? = 77
configure: program exited with status 77
...
| }
configure:22568: result: no
configure:22926: error: libmount selected, but required scanf string alloc modifier not available
```

Signed-off-by: Evgeny Vereshchagin <evvers@ya.ru>
2020-08-06 14:27:20 +00:00
Evgeny Vereshchagin 681d3a9586 build-system: make "make distcheck" work
It failed with:
```
make[5]: Entering directory '/home/travis/build/karelzak/util-linux/util-linux-2.36.32-0fea/_build/sub/po'
make[5]: *** No rule to make target '../../../include/fuzz.h', needed by 'util-linux.pot-update'.  Stop.
make[5]: Leaving directory '/home/travis/build/karelzak/util-linux/util-linux-2.36.32-0fea/_build/sub/po'
Makefile:896: recipe for target 'update-po' failed
make[4]: *** [update-po] Error 2
```

Signed-off-by: Evgeny Vereshchagin <evvers@ya.ru>
2020-08-06 12:33:11 +00:00
Evgeny Vereshchagin 7b4ab119f4 tests: integrate test_mount_fuzz into the testsuite
Signed-off-by: Evgeny Vereshchagin <evvers@ya.ru>
2020-08-06 12:33:11 +00:00
zhenwei pi b6ce063b04 irqtop/lsirq: support softirq
Add '-S' or '--softirq' for irqtop/lsirq, instead of interrupts, show
softirqs infomation. Because there is no more description of softirq,
do not show 'NAME' column by default.

Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2020-07-23 12:23:56 +02:00
Karel Zak 655d736da3 lib/path: add ul_path_is_accessible()
This function allow to check that path_cxt is usable. Note that
ul_new_path() does not open the path.

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-07-08 08:50:07 +02:00
Konstantin Khlebnikov 9bf622dad9 dmesg: adjust timestamps according to suspended time
Timestamps in kernel log comes from monotonic clocksource which does not
tick when system suspended. Suspended time easily sums into hours and days
rendering human readable timestamps in dmesg useless.

Adjusting timestamps accouring to current delta between boottime and
monotonic clocksources produces accurate timestamps for messages printed
since last resume. Which are supposed to be most interesting.

Signed-off-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
2020-06-04 10:57:36 +02:00
Karel Zak 37b302046a lib/blkdev: add support for --lock and LOCK_BLOCK_DEVICE
All simple function to parse --lock <mode> and $LOCK_BLOCK_DEVICE,
and to flock the fd.

The supported <mode> is:

	"1" or "yes"	- LOCK_EX
	"0" or "no"	- do nothing
	"nonblock"	- LOCK_EX | LOCK_NB

The function tries LOCK_NB before the solo LOCK_EX and prints
inform user that it will wait, for example:

session A:
	# sfdisk --lock /dev/sdc

session B:
	# sfdisk --lock /dev/sdc
	sfdisk: /dev/sdc: device already locked, waiting to get lock ...
	^C

	# sfdisk --lock=nonblock /dev/sdc
	sfdisk: /dev/sdc: device already locked

Addresses: https://github.com/karelzak/util-linux/issues/921
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-05-26 17:30:39 +02:00
Ritika Srivastava c8487d854b lsblk: Ignore hidden devices
Lsblk throws the following error for nvmeNcXnY devices.

lsblk: nvme1c1n1: unknown device name

This is because nvmeNcXnY devices are hidden and do not have
the file /sys/block/<nvmeNcXnY>/dev.

Following patch was added
https://git.kernel.org/pub/scm/utils/util-linux/util-linux.git/commit/?id=d51f05bfecb299a830897106460bf395be440c0a
Which made lsblk read from /sys/block/<nvmeNcXnY>/device/dev
which do exist for nvmeNcXnY devices.

After the above patch, the unknown error goes away.
However, another error is encountered in the very next step.

nvme1c1n1: failed to initialize sysfs handler

This is because lsblk looks for /sys/dev/block/242:1
(nvmeNcXnY major:minor) pathname which usually exists for other
block devices but not for the nvmeNcXnY devices as they are hidden.

Below patch does not even print this error for hidden devices
and exits silently.

[kzak@redhat.com: - add prefix to make sysfs_devname_is_hidden()
                    usable for  /sys dumps
                  - use the function in initialize_device() more early]

Signed-off-by: Ritika Srivastava <ritika.srivastava@oracle.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-05-14 15:48:26 +02:00
Karel Zak 72b155ea6e login: keep default MOTD_FILE= backwardly compatible
It seems that directories like /etc/motd.d are already used by PAM or
by another stuff. Let's keep it in admin/distro hands and do not
change the current default.

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-05-12 15:37:39 +02:00
Karel Zak 5a528e2c6f login: add support for directories in MOTD_FILE=
The current standard is to use directories to make it easy for
distributions to share resources. This patch also add /etc/motd.d
and /run/motd.d to the default MOTD_FILE=.

Addresses: https://github.com/karelzak/util-linux/issues/10341
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-05-12 14:20:54 +02:00
Karel Zak 0957fdca6a lib/mbsalign: add function to calculate width
We already have mbs_safe_nwidth() but it assumes that all "bad" chars
will be encoded by \x<hex>. Now we need also function that do not care
about encoding.

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-04-22 12:22:29 +02:00
Karel Zak 9faba2114a include/nls: remove unnecessary declaration
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-04-22 10:27:12 +02:00
Rosen Penev 13ae035211
[clang-tidy] fix mismatching declarations
Found with readability-inconsistent-declaration-parameter-name

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2020-04-19 14:03:21 -07:00
Karel Zak 75c7684724 Merge branch 'map-user' of https://github.com/mat8913/util-linux
* 'map-user' of https://github.com/mat8913/util-linux:
  unshare: Support names for map-user/group options
  lib/pwdutils: add xgetgrnam
  unshare: allow custom uid/gid mappings in userns
2020-04-17 11:02:29 +02:00
Matthew Harm Bekkema dc96ca29d8 lib/pwdutils: add xgetgrnam
Signed-off-by: Matthew Harm Bekkema <id@mbekkema.name>
2020-04-15 22:50:59 +10:00
Sami Kerola ed292a08dd
include: add remove_entry() to env.h
A function to remove an command-line option argument, or environment
variable.

Requested-by: Karel Zak <kzak@redhat.com>
Reference: https://github.com/karelzak/util-linux/pull/1003#discussion_r403988092
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-04-13 12:14:08 +01:00
Gaël PORTAY e368d0ad5d lib/mangle: check for the NULL string argument
This patch prevents to call the function strlen() with a NULL string
argument that leads to a segmentation fault.

Signed-off-by: Gaël PORTAY <gael.portay@collabora.com>
2020-03-20 16:28:49 -04:00
Karel Zak 0a4035ff2e include: cleanup pidfd inckudes
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-03-10 12:32:36 +01:00
Karel Zak ef0341c9be Merge branch 'time-namespace' of https://github.com/adrianreber/util-linux
* 'time-namespace' of https://github.com/adrianreber/util-linux:
  unshare: support the time namespace
  unshare: fix help message indentation
2020-03-09 10:31:45 +01:00
Karel Zak a8fe698c45 Merge branch 'topic/irq' 2020-03-09 10:25:48 +01:00
Adrian Reber be7df01a62
unshare: support the time namespace
This adds support to unshare for time namespaces. With the newly added
options '-t, --time' and '--monotonic' and '--boottime' it is now
possible to change CLOCK_MONOTONIC and CLOCK_BOOTTIME in a new time
namespace.

The time namespace has been merged in kernel version 5.6 and an easy way
to test it is using CLOCK_BOOTTIME and the uptime command:

 # uptime
 11:08:26 up 20:28,  1 user,  load average: 0.00, 0.00, 0.00
 # ./unshare --fork --time --boottime 100000000 uptime
 11:08:29 up 1158 days,  6:15,  1 user,  load average: 0.00, 0.00, 0.00

Signed-off-by: Adrian Reber <areber@redhat.com>
2020-03-08 19:02:00 +01:00
Karel Zak 35c6ed615f libblkid: move UTF encoding function to lib/
Let's consolidate the code, we need to use it in libfdisk too. It
seems better to keep it generic and libblkid independent.

This patch also removes blkid_encode_alloc(), this function is overkill.

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-02-25 12:00:27 +01:00
Sami Kerola 57e3dc061b
irqtop: use util-linux libcommon facilities
Including NLS support.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-02-21 21:09:14 +00:00
Karel Zak 88dbec9833 Merge branch 'various-fixes' of https://github.com/kerolasa/util-linux
* 'various-fixes' of https://github.com/kerolasa/util-linux:
  kill: include sys/types.h before checking SYS_pidfd_send_signal
  libfdisk: fix pointer wraparound warning
  hwclock: update yacc file
  script: fix minor warning
  getopt: use examples installation directory in man page
  fstrim: randomize timer start time across 100 minutes
  various: use threadsafe versions of time functions [lgtm scan]
  write: fix potential string overflow
2020-02-18 14:02:41 +01:00
Sami Kerola 3cfde0370d
kill: include sys/types.h before checking SYS_pidfd_send_signal
Including sys/types.h must happen before SYS_pidfd_send_signal is checked,
because that header defines variable in normal conditions.  When sys/types.h
does not have SYS_pidfd_send_signal then fallback is defined in config.h
that is included by default, and has therefore worked fine before and after
this change.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-02-15 21:12:50 +00:00
Sami Kerola 3160589d86
various: use threadsafe versions of time functions [lgtm scan]
Deprecating calls to not-thread safe asctime(), ctime(), and localtime()
calls is pretty close to pointless change.  Lets do it to reduce lgtm scan
warnings with justification it's nicer to use static analysis tools when
they have very few positives.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-02-15 15:23:44 +00:00
nl6720 bc46ec1947 libfdisk: add Linux /var, /var/tmp and root verity GPT partition types
See https://github.com/systemd/systemd/pull/14390 and https://systemd.io/DISCOVERABLE_PARTITIONS/ .

Also replace a stray tab with a space.
2020-02-11 12:23:04 +02:00
Karel Zak f1970cc557 docs: improve size arguments description in --help output
Let's add "Arguments:" section to the --help output and describe
{K,M,G...}iB suffixes there.

Addresses: https://github.com/karelzak/util-linux/pull/917
Co-Author: ed <ed@s5h.net>
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-02-05 11:01:36 +01:00
Karel Zak 67f938bfba include/c: add USAGE_ARGUMENT
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-02-05 10:17:18 +01:00
Karel Zak 01aedbecfe strutils: fix double free in strrealloc() [coverity scan]
* fix double free
* keep strrealloc() semantic compatible with classic realloc() (do not
  free original pointer on failed memory allocation).
* remove unnecessary goto

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-01-15 13:00:43 +01:00
Karel Zak b82237dfb2 lib: add _PATH_TMP fallback
- cleanup header of the file
- fix fileutils.c warnings
- add _PATH_TMP fallback

Addresses: https://github.com/karelzak/util-linux/issues/931
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-01-13 10:49:44 +01:00
Karel Zak 40f8c5e47e include: add some missing licence stuff to header files
It's mostly wrappers for compatibility and another trivial stuff etc.
Let's keep it as public domain to make it more portable to LGPL, GPL
and BSD code.

Signed-off-by: Karel Zak <kzak@redhat.com>
CC: Sami Kerola <kerolasa@iki.fi>
CC: Ruediger Meier <ruediger.meier@ga-group.nl>
2020-01-13 10:39:52 +01:00
Karel Zak d0c4300682 libfdisk: (MBR) use 0xEA partition type by BootLoaderSpecification
Let's use 0xEA for "Linux extended boot" rather than for "Rufus alignment".

References: https://www.freedesktop.org/wiki/Specifications/BootLoaderSpec/
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-01-02 15:16:54 +01:00
Daniel Drake 1f10f4afec isosize: move ISO size functions into a shared header
Move the helper functions that parse ISO data & sector size info into
a separate, shared header.

These will addtionally be used by libblkid's iso9660 parser.

Signed-off-by: Daniel Drake <drake@endlessm.com>
2019-12-16 14:29:24 +08:00
Karel Zak ac17a625ce build-sys: add missing header
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-12-10 12:31:59 +01:00
Karel Zak 7727be1af1 script: listen to SIGUSR1, flush logs on the signal
Based on pull request: https://github.com/karelzak/util-linux/pull/815

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-12-09 16:04:18 +01:00
Karel Zak b6c3de881c include/pidfd-utils: small cleanup
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-12-09 12:22:31 +01:00
Karel Zak e601521259 Merge branch 'kill-pidfd' of https://github.com/kerolasa/util-linux
* 'kill-pidfd' of https://github.com/kerolasa/util-linux:
  kill: use pidfd system calls to implement --timeout option
  build-sys: add missing NR underscore to UL_CHECK_SYSCALL()
2019-12-09 12:20:22 +01:00
Karel Zak 96c2b09fcb libfdisk: move GPT partition types to include/
We need the array use in another tools too. Let's follow the way we
use for MBR and keep it in include/.

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-12-04 12:37:22 +01:00
Sami Kerola 6e6b9a1d24
kill: use pidfd system calls to implement --timeout option
At times there is need in scripts to send multiple signals to a process.
Often these cases require some amount of waiting before follow-up signal
should be sent.

One common case is process termination, where first script tries to kill
process gracefully but if that does not work SIGKILL is sent.  Functionality
like that is commonly done by periodically checking if signalled pid exist
or not, and if it does another signal is sent possibly to an unrelated
process that reused pid number.  That means polling a pid is prone to a data
race.  Also if the first signal immediately kills the process one polling
interval is lost in sleep.

Another example when multiple signal need to be sent is various daemon
process control situations, such as Upgrading Executable on the Fly (see
reference).  This happens to be the case that inspired change author to make
sequential signaling a little bit easier.

Reference: http://nginx.org/en/docs/control.html#upgrade
Pull-request: https://github.com/karelzak/util-linux/pull/902
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-11-25 21:25:50 +00:00
Karel Zak 1eee1acb24 script: add --echo
It some cases it makes sense to disable ECHO flag also when script
used in pipe. This new option allows to keep full control in user's
hands.

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-11-25 12:39:52 +01:00
Karel Zak 95d255a819 scriptlive: terminate session at end of the log
We need a proper way how to inform child (shell) that the game is
over. It seems the best is to send EOF to child rather than
immediately break PTY mainloop where we have poll(), because shell can
still produce data etc.

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-11-22 13:03:35 +01:00
Karel Zak 4169bcb766 script: fix ECHO use, improve shell exec
For tools like su(1) is ECHO flag unexpected for use-case like

	echo 'date' | su - user

but script(1) need the echo to keep input recorded.

The patch also return execlp() use to script(1) code.

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-11-21 12:28:51 +01:00
Karel Zak 456bcbca6b agetty: add support for /run/issue and /usr/lib/issue
Addresses: https://github.com/karelzak/util-linux/issues/828
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-11-12 10:26:35 +01:00
Karel Zak 8e1b131e0a build-sys: use parse-date() only for hwclock
The parse-date.y is used only for hwclock, let's keep it together.
Note that the file (originally from gnulib) has GPLv3 license, so it's
better to make it obvious that we use it really only for hwclock (also
GPL).

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-11-07 09:02:23 +01:00
Karel Zak 787226dc28 include/strutils: add strdup_between_structs()
* improve strdup_to_offset() readability

* add strdup_between_offsets() and strdup_between_structs() to have
  better support for use-cases when we copy structs

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-10-31 12:25:08 +01:00
Patrick Steinhardt b2a1a69155 lib/path: fix missing header for `struct stat`
In commit b1418ed14 (lib/path: add ul_path_stat(), fix absolute paths,
2019-10-15), a new function `ul_path_stat()` was added to "path.h". This
new function prototype causes a compiler warning on musl libc based
systems due to one of the parameters having the unknown type `struct
stat` due to the <sys/stat.h> header not being included.

Fix the warning by including the header.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
2019-10-17 12:46:33 +02:00
Karel Zak 488f65fcb8 lib/fileutils: add close_all_fds()
The classic way which is based on file-descriptors table size is
pretty expensive (due to table size) and forces code to do many
unnecessary close() calls. It seems better to use /proc/self/fds and
close used descriptors only.

Addresses: https://github.com/karelzak/util-linux/issues/883
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-10-17 10:44:57 +02:00
Karel Zak b1418ed146 lib/path: add ul_path_stat(), fix absolute paths
* add ul_path_stat()

* make sure all paths for ul_path_..() functions are always
interpreted relatively to the context directory and prefix. This is
difference between ul_path_ API and standard libc "at" functions. We
do not use any exception for absolute paths. The reason is that we
need to read from prefixed paths although application assume absolute
path (/dev/sda1 means /prefix/dev/sda1 if a /prefix is defined).

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-10-15 11:59:16 +02:00
Karel Zak bdd4335706 lib/pty-session: make wait_child callback optional
Now the code is duplicate on many places, but all we usually need is to
remember child status. It seems good enough to have very simple
callback child_die() to inform application about a change.

The patch also add PID to all signal related callbacks.

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-10-08 13:11:54 +02:00
Karel Zak 4f7f723b31 lib/pty-session: add loggin callback to code, follow return codes
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-10-08 13:11:54 +02:00
Karel Zak 04f0c95f93 lib/pty-session: add log callbacks
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-10-08 13:11:54 +02:00
Karel Zak ab61a03881 lib/pty: save sigmask, add API to free all resources
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-10-08 13:11:54 +02:00
Karel Zak 4d5b2fed8f lib/pty: allow use callback from mainloop
This allows to control mainloop behavior from PTY applications. For
example you can write to child (shell) process independently on the
current stdin.

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-10-08 13:11:54 +02:00
Karel Zak 6954895cae lib/pty-session: add generic PTY container code
The idea is to consolidate script(1), scriptlive(1) and su(1) --pty
and use the same code everywhere.

TODO: add callbacks for stdin/out logging (necessary for script(1)).

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-10-08 13:11:54 +02:00
Karel Zak 23afadca57 include/strutils: add strrealloc() 2019-10-08 13:11:53 +02:00
Elliott Mitchell 2bb3aa36b2 cleanup: Remove some spurious spaces
Sorry detail-oriented people tend to wipe these out if they notice them.
Add in automated tools and lots of excess end-of-line spaces get wiped
out.

Addresses: https://github.com/karelzak/util-linux/pull/849
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-10-01 13:01:43 +02:00
Quentin Rameau cd083615b4 lib/pwdutils: add xgetpwuid 2019-09-26 15:55:39 +02:00
Karel Zak b2ce7d5827 include/all-io: remove unnecessary condition [lgtm scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-09-20 16:00:13 +02:00
Karel Zak 7618b3be4b build-sys: fix UTIL_LINUX_PT_SGI_H macro [lgtm scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-09-20 15:31:31 +02:00
Sami Kerola 218b1dd6f9
misc: fix typos [codespell]
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-09-12 19:41:46 +01:00
James Peach cef4decf04 unshare: add --keep-caps option
Add the --keep-caps option to unshare to preserve capabilities that
are granted when creating a new user namespace. This allows the child
process to retain privilege within the new user namespace without also
being UID 0.
2019-09-09 20:23:08 -07:00
Patrick Steinhardt 530220b6bf include/closestream: fix assignment to read-only standard streams
In order to avoid closing standard streams multiple times, commit
52aa1a661 (include/closestream: avoid close more than once, 2019-06-13)
introduced code to set the standard output and error streams to `NULL`.
As musl libc defines standard streams as constant pointers, the change
causes compiler errors on systems with that libc. According to ISO C89,
being able to assign to the standard text streams is not a requirement
for any C implementation, see footnote 238 in chapter §7.19.5.6:

    The primary use of the freopen function is to change the file
    associated with a standard text stream (stderr, stdin, or stdout),
    as those identifiers need not be modifiable lvalues to which the
    value returned by the fopen function may be assigned.

This commit implements a new function `flush_standard_stream` that tries
to reliably flush standard streams without actually closing them. By not
calling fclose(3P), we can neatly avoid the issue of accessing standard
streams in an unspecified state and thus remove the infringing `NULL`
assignments.

Properly flushing standard streams without fclose(3P) proves to be more
intricate than one may expect, though, as some filesystems like NFS may
defer flushing until they see a close(3P) of the underlying descriptor.
One may call fsync(3P) to remedy that, but this may incur a heavy
performance penalty in some scenarios. To work around the issue and
still get proper errors, we duplicate the stream's file descriptor and
close that one instead, which is sufficient to cause a flush.

Note that both `close_stdout` and `close_stdout_atexit` are misnamed
after this change, as we do not actually close the streams now. In order
to avoid unnecessary code churn, we still retain their current names.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
2019-09-02 11:55:56 +02:00
Sami Kerola 273ebba11d include/xalloc: reindent function bodies to unify indentation
The file used unusual 7 spaces indent step in some of the functions.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-08-21 15:00:16 +02:00
Sami Kerola aba9e76e25 include/xalloc: use multiline function declarations
There are two things happening in this commit.  1) Move each function
attribute to it's own line 2) move function return value after attributes so
that function inputs and outputs are on same line.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-08-21 15:00:16 +02:00
Rosen Penev 76b680b1a9
Remove isascii usage
There is a c_isascii function that can be used.

isascii is deprecated and not available with some libcs like uClibc-ng
where it can be compile time disabled.
2019-08-08 12:12:55 -07:00
Sami Kerola f3aded3fdb
include/xalloc: ensure xstrdup() and xstrndup() returns nonnull attribute
Turned out lsblk is passing null as argument to xstrdup(), so fix that and
add assert() to make sure promise of not returning null is kept in future.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-07-14 11:52:13 +01:00
Karel Zak bddcf5b816 Merge branch 'topic/wdctl' 2019-06-17 15:48:28 +02:00
Karel Zak b3dd29d1b8 wdctl; read from /sys if necessary
The device can be inaccessible for non-root user or busy (already used
by another process). In this case it seems better to read information
from /sys.

Note that /sys does not provide struct watchdog_info.options, so we
cannot print list of supported watchdog features.

Addresses: https://github.com/karelzak/util-linux/issues/804
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-06-17 15:47:39 +02:00
Karel Zak 8c8df42146 wdctl: default to /dev/watchdog0
Let's use miscdev /dev/watchdog as fallback only. We need (if possible)
cdev /dev/watchdog0 as this device has entry in /sys/class/watchdog.

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-06-17 15:47:39 +02:00
Karel Zak 52aa1a6611 include/closestream: avoid close more than once
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-06-17 15:46:10 +02:00
Karel Zak a6d9d23b5f lib/ttyutils: introduce get_terminal_stdfd()
Let's use separate function to detect what is the current terminal.

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-27 12:49:12 +02:00
Karel Zak 1a56f15b0f libfdisk: (sgi) improve checksum calculation [-Waddress-of-packed-member]
Let's keep compilers and static analyzers happy. The idea is to use
memcpy() to copy from buffer to variable and use all label as unsigned
char rather than vectorize by uint32_t.

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-22 17:42:48 +02:00
Karel Zak 34b0a305f6 include/c: use __has_attribute
The __has macros are more portable (supported by gcc as well as
clang).

The old __GNUC_PREREQ is deprecated and it should be removed in
future. (Well, the __has macros are supported since gcc 5, so we
should be patient as some old stable distros (e.g. RHEL 7) use gcc
4.x).

This patch helps clang to correctly analyze our xalloc.h stuff.

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-21 16:27:17 +02:00
Sami Kerola d2a1ee4e56
include/strutils: fix potential null pointer dereference
Recent lscpu fix caused gcc -Wnull-dereference to go off that this change
addresses.

Reference: b94acada9e
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-05-16 22:05:07 +01:00
Sami Kerola b94acada9e
lscpu: move trailing null after removing characters from a string
From the test input string ':' characters are removed:

    cat x86_64-epyc_7451/sys/devices/system/cpu/vulnerabilities/spectre_v2
    Mitigation: Full AMD retpoline, IBPB: conditional, STIBP: disabled, RSB filling

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-05-16 11:45:47 +01:00
Karel Zak 084365f1a1 build-sys: use __SANITIZE_ADDRESS__ rather than custom USE_CLOSE_ATEXIT
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-07 12:59:52 +02:00
Karel Zak 2c308875a7 misc: consolidate version printing and close_stdout()
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-04-16 15:14:13 +02:00
Karel Zak 31c66833cb include/closestream: add close_stdout_atexit()
It seems better to have a way to control when atexit(close_stdout()) is
used, because close stdout means that for example ASAN (or another
into binary integrated tool) is not able to print the final summary.

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-04-16 13:42:34 +02:00
Karel Zak 3c4ff2dc9d include/strutils: add functions to replace and remove chars from string
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-04-11 13:13:06 +02:00
Karel Zak 7761bd3bb6 lib/fileutils: add xreaddir()
Remove duplicate code and keep only one implementation in
include/fileutils.h.

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-04-11 13:11:53 +02:00
Karel Zak 6c9ab254ae libmount: don't use sscanf() for swaps parsing
Addresses: https://github.com/karelzak/util-linux/issues/780
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-04-08 15:16:23 +02:00
Karel Zak 86673b3a46 libmount: don't use sscanf() for mountinfo parsing
Addresses: https://github.com/karelzak/util-linux/issues/780
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-04-08 14:45:56 +02:00
Sami Kerola fcb89ba7bb include/path.h: remove duplicate header inclusion
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-02-18 13:21:46 +01:00
Sami Kerola e73456dfbb include: add no return function attribute
This warning is repeated 112 times when compiling with all warnings.

xalloc.h:23:1: warning: function '__err_oom' could be declared with
attribute 'noreturn' [-Wmissing-noreturn]

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-02-18 13:21:23 +01:00
Karel Zak 07b94c9f32 lib/strutils: support two decimal places in size_to_human_string() output
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-02-12 14:20:55 +01:00
Karel Zak 5db5962339 lib/colors: force to "never" mode on non-terminal output
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-02-04 14:14:37 +01:00
Karel Zak 75d239ffd8 lib/loopdev: set blocksize when create a new device
The partition scanner in kernel depends on blocksize. We need to set
the blocksize before we call LOOP_SET_STATUS64 (this ioctl triggers
the scanner).

This patch extends the internal API to save blocksize into loopdev
context to be usable later for loopcxt_setup_device().

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-01-22 11:37:35 +01:00
Karel Zak 9fcc893624 lib/loopdev: differentiate between setter()s and ioctl calls
Now the internal API uses loopcxt_set_...() to set context variables
as well as to call ioctls. This patch introduces loopcxt_ioctl_...()
to makes things more obvious to readers.

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-01-22 11:12:42 +01:00
Sami Kerola ee6e39305c
include: add indirect monotonic clock id specifier
Avoid repeated ifdef checks in code by adding a project specific
preprocessor definition.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-01-12 08:46:53 +00:00