Commit Graph

937 Commits

Author SHA1 Message Date
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 c4bacbd115 lib/pty-session: improve debug messages
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 984082fa85 lib/pty: reset mainloop timeout on signal
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
Quentin Rameau cd083615b4 lib/pwdutils: add xgetpwuid 2019-09-26 15:55:39 +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
Sami Kerola 5119ef0a83
lib/ttyutils: avoid checking same thing twice
Check cols and lines are not NULL only once.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-07-01 22:02:06 +01:00
Karel Zak dab1303287 Revert "lib/loopdev.c: Inline loopcxt_has_device"
... no caller in that file, this change has no effect.

This reverts commit 3bb960c7b5.
2019-06-18 22:05:16 +02:00
Karel Zak e3188dd90a lib/path: make sure ul_path_read_buffer() derminate result
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-06-17 15:47:39 +02:00
Karel Zak 4a0e70663c lib/sysfs: fix reference counting for parent
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-06-11 12:11:32 +02:00
Karel Zak 348f57d669 lib/canonicalize: fix compiler warning [-Wsign-compare]
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-06-10 11:00:38 +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 8ecc6ba807 lib/ismounted: use xstrncpy()
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-20 13:03:58 +02:00
Karel Zak e2569bcf26 lib/path: use xstrncpy()
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-20 12:57:16 +02:00
Karel Zak 94d9fd9f6a lib/sysfs: use xstrncpy()
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-20 12:57:07 +02:00
Karel Zak 91072cd480 losetup: use xstrncpy()
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-20 12:46:39 +02:00
Karel Zak 9d6852d3a2 lib/colors: remove unnecessary goto
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-20 12:41:12 +02:00
Sami Kerola cc7ffe129d
lib/colors: remove redundant if statement
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-05-18 22:56:15 +01:00
Sami Kerola 4f807791ee
lib/mangle: fix possible null pointer dereference [cppcheck]
Fix effects code that is used when testing util-linux, so quite low impact.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-05-18 22:09:25 +01:00
Karel Zak 6d92f7d7bf lib/strutils: keep static analyzer happy [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-15 16:09:16 +02:00
Karel Zak 91bf581742 lib/path: fix resource leak [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-15 16:01:32 +02:00
Karel Zak 8083862536 lib/path: fix possible NULL dereferencing [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-15 15:57:09 +02:00
Karel Zak 8e28151329 lib/path: fix ul_path_get_dirfd() usage [coverity scan]
ul_path_get_dirfd() returns negative number on error...

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-15 15:52:54 +02:00
Karel Zak 2c417af196 lib/path: consolidate ul_path_mkpath() usage
* always use errno from ul_path_mkpath()
* keep code simple and short as possible

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-15 15:47:29 +02:00
Karel Zak b24650294d lib/path: fix possible NULL pointer dereferencing [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-15 15:36:49 +02:00
Austin English 5cee62edc4 lib/colors: fix "maybe be" duplication
Signed-off-by: Austin English <austinenglish@gmail.com>
2019-05-15 13:48:34 +02:00
Karel Zak 3011b381e3 losetup: keep static analyzer happy [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-14 16:43:40 +02:00
Karel Zak dcf6f5b3d9 lib/colors: keep static analyzer happy [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-14 16:37:35 +02:00
Karel Zak 0ed52c662e lib/canonicalize: verify DM paths [coverity scan]
Now the code only checks that /sys/.../dm/name exists, but never
verify the device node in /dev (because path prefix is never NULL).

The prefix is used to redirect hardcoded paths to /sys dumps (e.g.
lsblk regression tests, etc.)

This bug has been introduced in v2.33. Fortunately, it's probably no
big issue as /dev is always in sync with /sys (thanks to udevd).

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-14 15:47:02 +02:00
Karel Zak 8c368dc6d3 lib/strutils: parse_size() fix frac digit calculation
Old code:

	./test_strutils --size 0.5MiB
		0.5MiB :               512000 :     500K :      500 KiB
	./test_strutils --size 0.50MiB
		0.50MiB :              5120000 :     4.9M :      4.9 MiB

New code:
	./test_strutils --size 0.5MiB
		0.5MiB :               524288 :     512K :      512 KiB
	./test_strutils --size 0.50MiB
	       0.50MiB :               524288 :     512K :      512 KiB

Note that the new implementation also does not use float points,
because we need to support PiB and so on... it seems good enough for
things like:

        ./test_strutils --size 7.13G
                7.13G :           7656104581 :     7.1G :      7.1 GiB
        ./test_strutils --size 7.16G
                7.16G :           7690675814 :     7.2G :      7.2 GiB

to avoid situation where cfdisk creates partition with completely
crazy numbers.

Addresses: https://github.com/karelzak/util-linux/issues/782
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-13 17:07:14 +02:00
Karel Zak 482e0a0754 lib/strutils: parse_size() fix frac with zeros
Fix 0.001G as well as accept 0.000G as valid number.

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-13 16:15:58 +02:00
Romain Izard eab90ef8d4 lib/loopdev.c: Retry LOOP_SET_STATUS64 on EAGAIN
A recent bugfix in the Linux kernel made it possible for the
LOOP_SET_STATUS64 ioctl to fail when called with a non-zero offset,
with an EAGAIN errno:

5db470e229e2 loop: drop caches if offset or block_size are changed

This fix changes a silent failure (where mount could sometimes access
the backing loop image through the cache without the specified offset)
to an explicit failure, and it has also been backported on stable
branches.

On a 5.0 kernel, other changes to the loop driver make it hard to get
generate the EAGAIN error, but this bugfix has also been backported to
stables branches, without these changes. At least with the 4.14 stable
branch, the EAGAIN error can be quickly generated with the following loop:

while mount -o loop,offset=239 disk point && umount point; do :; done

Retry the ioctl when it fails with EAGAIN, which means that mount or
losetup will eventually succeed when encountering this case.

[kzak@redhat.com: - use our local portable xusleep()]

Signed-off-by: Romain Izard <romain.izard.pro@gmail.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-07 12:36:00 +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 36fcefa651 libmount: don't use sscanf() for fstab parsing
Addresses: https://github.com/karelzak/util-linux/issues/780
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-04-08 13:34:47 +02:00
Karel Zak 65bcbf105b taskset: fix cpuset list parser
taskset hangs when executed with badly formatted cpuset list, for
example:

	 $ taskset -c 0--1 true

The current cpuset list parser is pretty weak as based on scanf()
without strings verification ("-1" as input for "%u" returns
unexpected number). It seems faster and better to use strtoul() and
isdigit().

Addresses: https://github.com/karelzak/util-linux/issues/77
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-03-20 13:12:25 +01:00
Karel Zak 357dd756d1 lib/path: remove extra semi-colons
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-03-19 13:57:05 +01:00
Marcos Paulo de Souza 3bb960c7b5 lib/loopdev.c: Inline loopcxt_has_device
loopcxt_has_device is a one liner, so adding inline statement does not
hurt.

Signed-off-by: Marcos Paulo de Souza <marcos.souza.org@gmail.com>
2019-03-03 09:15:13 -03:00
Sami Kerola 110260830d docs: fix typos [codespell]
Reference: https://github.com/codespell-project/codespell
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-02-18 13:22:43 +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
Sami Kerola d393c00c6c
timeutils: match today day and this year correctly
Assumption all years since 1970 have been exactly 365 days long has it's
problems when leap years happen.  Lets use struct tm fields that are
provided by localtime_r(), making year and day to be correctly compared even
when it's late new years eve somewhere else than UTC-0.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-01-12 08:46:29 +00:00
Karel Zak c455cdb30d choom: fix negative adjust score usage
It's really bad idea to use uint64_t (ul_path_write_u64(()) when write
signed number.

Addresses: https://github.com/karelzak/util-linux/issues/723
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-12-10 14:26:04 +01:00
Karel Zak c9ea91d41e lib/path: allow to close dirfd
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-12-07 12:32:58 +01:00
Karel Zak 93c687d895 lib/sysfs: add function to detect partitioned devices
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-12-07 12:32:57 +01:00
Karel Zak a68ed87ffe build-sys: do not require dirfd()
The dirfd() is required on many places, but it should not be required for
all utils by ./configure.ac.

Signed-off-by: Karel Zak <kzak@redhat.com>
2018-12-03 11:23:33 +01:00
Karel Zak 6df5acf970 lib/timer: add fallback if timer_create() not available
* add struct ul_timer as API abstraction to hide differences between
timer_create() and setitimer()

* add setitimer() detection to ./configure.ac

* add fallback code to use setitimer() if timer_create() not available
  (for example on OSX)

Addresses: https://github.com/karelzak/util-linux/issues/584
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-12-03 11:23:33 +01:00
Karel Zak e11d0b8c88 lib/canonicalize: fix typo
Addresses: 8b8277b7a8 (commitcomment-31491031)
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-11-30 10:25:04 +01:00
Rian Hunter e101a9eb0f lib/canonicalize: do restricted canonicalize in a subprocess
Accessing FUSE mounts require suid/sgid (saved uid) to be equal to the
owner of the mount. If mount is running as a setuid process, swapping
creds by only setting the euid/egid isn't enough to change the
suid/sgid as well. We must do a full setuid()/setgid(), but that
removes our ability to re-assume the identity of the original
euid. The solution is swap creds in a child process, preserving the
creds of the parent.

[kzak@redhat.com: - use switch() rather than if() for fork
		  - use all-io.h
		  - close unused pipe[] ends
		  - be more strict about used types]

Addresses: https://github.com/karelzak/util-linux/pull/705
Co-Author: Karel Zak <kzak@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-11-30 10:25:04 +01:00
Karel Zak 5b82289b60 lib/strutils: return from xstrmode()
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-09-14 16:26:38 +02:00
Karel Zak 892fc7d9de lib/colors:: fix compiler warnings [-Wcast-qual]
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-07-23 11:55:25 +02:00
Karel Zak 0145c00a12 lib/loopdev: fix compiler warning [-Wcast-qual]
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-07-23 11:17:28 +02:00
Karel Zak 78c66fc61d lib/mangle: const quialifier cleanup
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-07-20 15:31:42 +02:00
Karel Zak c8b237a0ed lib/color-names: const cleanup
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-07-20 13:58:10 +02:00
Karel Zak d54b83156f lib/strutils: follow const in parse_size()
* don't cast from char to const char

* don't share endptr from strtoxxx() with rest of the code
  as the end pointer is char, but code works with const chars

Signed-off-by: Karel Zak <kzak@redhat.com>
2018-07-20 12:54:58 +02:00
Karel Zak 8deb816173 lib/strutils: remove redundant condition
[util-linux-2.32.1/lib/strutils.c:122]: (style) Redundant condition: If 'EXPR == '0'', the comparison 'EXPR' is always true.

Reported-by: David Binderman <dcb314@hotmail.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-07-18 09:48:19 +02:00
Karel Zak 3b6ca044da lib/pager: fix compiler warning [-Wrestrict]
lib/pager.c: In function ‘pager_preexec’:
lib/pager.c:148:12: warning: passing argument 2 to restrict-qualified parameter aliases with argument 4 [-Wrestrict]

Signed-off-by: Karel Zak <kzak@redhat.com>
2018-07-18 09:41:46 +02:00
Karel Zak 805400c730 Merge branch 'quick-fix-20180625' of https://github.com/sjitech/util-linux
* 'quick-fix-20180625' of https://github.com/sjitech/util-linux:
  exec_shell: prevent ".: applet not found" error when SHELL env is not set.
2018-07-04 11:00:58 +02:00
Ruediger Meier 24c329bbb3 lib/path: remove unused expression
error: expression result unused [-Werror,-Wunused-value]
                        prefix = optarg, "failed to parse range start";
                                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2018-06-25 16:49:37 +02:00
osexp2000 82adb91f6e exec_shell: prevent ".: applet not found" error when SHELL env is not set.
When SHELL env is not set, it cause xstrdup(NULL) be executed, and result in weird error message ".: applet not found" instead of /bin/sh being used.
2018-06-25 10:34:31 +09:00
Karel Zak 83bbeb77c1 lib/path: allow dir-path formatting
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:49:03 +02:00
Karel Zak 905bc7817e lsblk: fix ul_path_read_string() usage
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:19:28 +02:00
Karel Zak 5a89aa99a7 lsblk: use prefix for DM name
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:19:28 +02:00
Karel Zak ab628c7c18 lib/sysfs: cleanup function names
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:19:28 +02:00
Karel Zak eb4a75afd9 lib/path: improve debug message on redirect
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:19:28 +02:00
Karel Zak 67f2d1e326 lib/sysfs: fix sysfs_blkdev_get_wholedisk()
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:07:46 +02:00
Karel Zak 53c74b700c lib/path: simplify debug message
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:07:46 +02:00
Karel Zak 9023b20123 lib/path: add ul_prefix_fopen(), improve cpuset funcs
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:07:46 +02:00
Karel Zak c87b53ca5f lib/path: don't use extra '/'
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:07:46 +02:00
Karel Zak c7df0f42b7 lib/path: add more debug messages
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:07:46 +02:00
Karel Zak fcc4517cfd lib/path: make ul_path_read_ usable with NULL handler
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:07:46 +02:00
Karel Zak 7eb8e47bcd lib/path: add ul_path_read_buffer()
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:07:46 +02:00
Karel Zak e74e5401e1 lib/path: add ul_path_get_abspath()
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:07:46 +02:00
Karel Zak 3aa4775c2f lib/sysfs: fix ul_path_read_string() usage
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:07:46 +02:00
Karel Zak 8c3d9cad1e lib/sysfs: add sysfs_blkdev_get_parent()
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:07:46 +02:00
Karel Zak 866ac74f97 lib/sysfs: make possible to call sysfs_blkdev_deinit_path() in loop
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:07:46 +02:00
Karel Zak d5878cabe6 lib/path: fix read string to be backwardly compatible
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:07:46 +02:00
Karel Zak bcf445fd68 lib/path lib/sysfs: add debug
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:07:46 +02:00
Karel Zak 2569d2ca1f lib/loopdev: remove obsolete macro
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:07:46 +02:00
Karel Zak 7604f85fb6 lib/loopdev: use new ul_path_* API
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:07:46 +02:00
Karel Zak 83029ea577 lib/sysfs: add ul_new_sysfs_path() shortcut
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:07:46 +02:00
Karel Zak 5387c01517 lib/sysfs: new implementation
* reuse ul_path_* API

* allow to use prefix for sysfs paths, so we can use snapshots from
  sysfs for regression tests

Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 12:56:16 +02:00
Karel Zak 1ed21c80ed lib/path: new implementation
The goal is to avoid duplicate code in path.c and sysfs.c and make it
possible to define prefix for paths for all sysfs and procfs based
utils. Now we have /proc snapshots (for tests) for lscpu only. It
would be nice to have the same (for sysfs) for lsblk and another tools.

* very simple API to read numbers, strings and symlinks

* based on openat()

     pc = ul_new_path("/sys/block/sda");
     ul_path_read_u64(pc, &size, "size");
     ul_path_read_u64(pc, &lsz, "queue/logical_block_size");

* printf-like API to generate paths, for example:

     ul_path_readf_u64(pc, &num, "sda%d/size", partno)

* allow to define prefix to redirect hardcoded paths to another
  location, for example:

     pc = ul_new_path("/sys/block/sda");
     ul_path_set_prefix(pc, "/my/regression/dump");
     ul_path_read_u64(pc, &num, "size");

  to read /my/regression/dump/sys/block/sda/size

* allow to extend the API by "dialects", for example for sysfs:

     pc = ul_new_path(NULL);
     sysfs_blkdev_init_path(pc, devno, NULL);

  and use ul_path_* functions to read from @pc initialized by
  sysfs_blkdev_init_path()

* add test_path binary

Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 11:58:10 +02:00
Karel Zak 64af1a2987 lib/strutils: fix strnlen() fallback
Addresses: https://github.com/karelzak/util-linux/issues/643
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-05-28 12:30:34 +02:00
Karel Zak 4d9b788d64 script: add more info to script header
This patch introduces [...] to store extra information about terminal
to the typescript header. For example:

  Script started on 2018-05-14 12:52:32+02:00 [TERM="xterm-256color" TTY="/dev/pts/3" COLS="190" LINES="53"]

or

  Script started on 2018-05-14 12:54:01+02:00 [<not executed on terminal>]

if stdout is not terminal.

Addresses: https://github.com/karelzak/util-linux/issues/583
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-05-14 12:51:50 +02:00
Sami Kerola bf99940f64 lib/canonicalize: fix truncation warning
lib/canonicalize.c: In function ‘canonicalize_dm_name’:
lib/canonicalize.c:42:45: warning: ‘%s’ directive output may be truncated
writing up to 255 bytes into a region of size 244 [-Wformat-truncation=]
   snprintf(path, sizeof(path), "/dev/mapper/%s", name);

Notice that this warnign fix does not improve code enormously.  The earlier
snprintf() truncation will not happen a bit earlier when fgets() is called.
In that sense this change merely makes one easy to silence warning to
disappear, and therefore improve change of noticing useful messaging as such
crops up.

[kzak@redhat.com: - use macro rather than hardcoded string for mapper path]

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-05-10 11:20:56 +02:00
Karel Zak edc1c90cb9 lib/randutils: don't break on EAGAIN, use usleep()
The current code uses lose_counter to make more attempts to read
random numbers. It seems better to wait a moment between attempts to
avoid busy loop (we do the same in all-io.h).

The worst case is 1 second delay for all random_get_bytes() on systems
with uninitialized entropy pool -- for example you call sfdisk (MBR Id
or GPT UUIDs) on very first boot, etc. In this case it will use libc
rand() as a fallback solution.

Note that we do not use random numbers for security sensitive things
like keys or so. It's used for random based UUIDs etc.

Addresses: https://github.com/karelzak/util-linux/pull/603
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-03-20 14:17:24 +01:00
Carlo Caione a9cf659e05 lib/randutils: Do not block on getrandom()
In Endless we have hit a problem when using 'sfdisk' on the really first
boot to automatically expand the rootfs partition. On this platform
'sfdisk' is blocking on getrandom() because not enough random bytes are
available. This is an ARM platform without a hwrng.

We fix this passing GRND_NONBLOCK to getrandom(). 'sfdisk' will use the
best entropy it has available and fallback only as necessary.

Signed-off-by: Carlo Caione <carlo@endlessm.com>
2018-03-19 14:06:09 +01:00
Tony Asleson 80ec018c6a libblkid: ignore private Stratis devices
[kzak@redhat.com: - tiny coding style changes]

References: 20e1c3dc03
Signed-off-by: Tony Asleson <tasleson@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-03-09 13:34:42 +01:00
Ruediger Meier 73afd3f8e8 misc: fix typos using codespell
Some more funny typos, please review carefully.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2018-02-16 11:12:52 +01:00
Karel Zak d51f05bfec lsblk: try device/dev to read devno
Now sysfs_devname_to_devno() reads devno from /dev or
/sys/block/<name>/dev, but it seems that NVME uses
/sys/block/<name>/device/dev.

Reported-by: Potnuri Bharat Teja <bharat@chelsio.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-02-09 14:18:18 +01:00
Karel Zak 0f6adf8673 lib/exec_shell: (and pager) use errexec()
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-02-01 15:38:24 +01:00
Karel Zak 117d079199 lib/exec_shell: cleanup function attributes
Reported-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-02-01 12:25:39 +01:00
Karel Zak a15dca2f6d include/debug: introduce __UL_INIT_DEBUG_FROM_STRING()
Let's make it possible to use debug.h without environment variables.

Suggested-by: J William Piggott <elseifthen@gmx.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-01-17 13:58:29 +01:00
Karel Zak bc9ca775a2 Merge branch 'mbsencode' of https://github.com/yontalcar/util-linux
* 'mbsencode' of https://github.com/yontalcar/util-linux:
  tests: mark mbsencode as KNOWN_FAIL
  tests: mbsencode - test for HAVE_WIDECHAR
  lib/mbsalign: Fix escaping nonprintable multibyte characters
  tests: mbsencode - removed emoji, added control unicode character
  tests: add tests for encode functions from lib/mbsalign.c
  lib/mbsalign: escape "\x" when HAVE_WIDECHAR not defined
2018-01-12 11:59:22 +01:00
Karel Zak 6d00cfb233 include/debug: don't print pointer address for SUID programs
* introduce new flag __UL_DEBUG_FL_NOADDR to suppress pointer address printing

* use __UL_DEBUG_FL_NOADDR when SUID

* move ul_debugobj() to debugobj.h, and require UL_DEBUG_CURRENT_MASK
  to provide access to the current mask from ul_debugobj(). It's better
  than modify all ul_debugobj() calls and use the global mask as
  argument.

* remove never used UL_DEBUG_DEFINE_FLAG

Reported-by: halfdog <me@halfdog.net>
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-01-12 11:01:26 +01:00
Vaclav Dolezal edf86d6bc0 lib/mbsalign: Fix escaping nonprintable multibyte characters
Signed-off-by: Vaclav Dolezal <vdolezal@redhat.com>
2018-01-11 10:42:22 +01:00
Karel Zak 4ff4b1106e lib/sha1: use proper WORDS_BIGENDIAN macro
The original code uses BYTE_ORDER, but we use WORDS_BIGENDIAN in
utl-linux.

Reported-by: Andreas Schwab <schwab@linux-m68k.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-01-02 11:08:03 +01:00
Vaclav Dolezal 2ba65f379d lib/mbsalign: escape "\x" when HAVE_WIDECHAR not defined
Signed-off-by: Vaclav Dolezal <vdolezal@redhat.com>
2017-12-22 12:32:36 +01:00
Karel Zak 43afa84581 lib/mbsalign: encode \x to \xecx
Don't encode stand alone '\', our encoding is about \x<hex>, so we
need to care about \x prefix only.

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-12-20 13:01:43 +01:00
Karel Zak 33d2e5a1ed Merge branch 'gpt-part-name' of https://github.com/yontalcar/util-linux
* 'gpt-part-name' of https://github.com/yontalcar/util-linux:
  libfdisk: return bytes processed from gpt_entry_set_name(), process rc
  libfdisk: allocate enough bytes for ucs2 to utf8 encoding
  libfdisk: gpt: properly encode string on rename
  lib/mbsalign: encode backslash to prevent ambiguity
2017-12-20 12:29:16 +01:00
Karel Zak 42dea85c5a lib/md5: use ul_/UL_ prefix
The symbols names are too generic.

Addresses: https://github.com/karelzak/util-linux/issues/548
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-12-12 12:06:15 +01:00
Karel Zak 3ae2cb49d9 lib/sha1: fix indention
* indent -linux -psl -blf  lib/sha1.c
* manually fix comments

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-12-12 11:42:28 +01:00
Karel Zak 3bd55be742 lib/sha1: use ul_/UL_prefix for symbols
Unfortunately, the symbols are visible in statically compiled libuuid
and the names are too generic.

Addresses: https://github.com/karelzak/util-linux/issues/548
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-12-12 11:29:02 +01:00
J William Piggott 6cdc7b9c02 lib/timeutils.c: warn format_iso_time() overflow
Print a message when the format_iso_time() buffer is exceeded, because
there is more than one type of failure that returns -1.

Also remove the corresponding message from hwclock.c.

Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-12-09 18:43:29 -05:00
J William Piggott 2d9a0b0adb lib/timeutils.c:strxxx_iso: do not wrap tm_year
Year can wrap when adding the tm struct epoch during iso formatting:
hwclock --utc --noadjfile --predict --date '67768034678844900 seconds'
-2147481749-12-31 23:59:59.000000-05:00

Patched:
hwclock --utc --noadjfile --predict --date '67768034678844900 seconds'
2147485547-12-31 23:59:59.000000-05:00

Comparable to date(1):
date -Ins --date '67768034678844900 seconds'
2147485547-12-31T23:59:59,547886165-0500

Note: the 'seconds' argument is relative to the current time, so
      reaching this max year output is a moving target. The values shown
      above were invalid one second later. They then overflow tm_year
      upon conversion with localtime(3) and friends, which causes them
      to return NULL indicating that an error occurred.

Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-12-09 15:05:38 -05:00
J William Piggott ee475ab23e lib/timeutils.c:strxxx_iso: test conversion errors
Test for libc time conversion errors in ISO time format functions.

hwclock --utc --noadjfile --predict --date '67768034678846520 seconds'
Segmentation fault

Patched:
hwclock --utc --noadjfile --predict --date '67768034678846520 seconds'
hwclock: time 67768036191695381 is out of range.

Comparable to date(1):
date --date '67768034678846520 seconds'
date: time 67768036191695384 is out of range

Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-12-09 14:40:05 -05:00
J William Piggott 97772cc329 lib/timeutils.c: bug fix Segmentation fault
Use reentrant time functions to avoid sending a NULL pointer to
format_iso_time() (and to be reentrant ;). Followup commits test for
errors and tm_year wrapping (illustrated below).

hwclock --utc --noadjfile --predict --date '67768034678849400 seconds'
Segmentation fault

Patched
hwclock --utc --noadjfile --predict --date '67768034678849400 seconds'
-2147481748-01-00 00:10:46.000000-05:00

Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-12-09 14:01:33 -05:00
Vaclav Dolezal ded09e8ed6 lib/mbsalign: encode backslash to prevent ambiguity
Signed-off-by: Vaclav Dolezal <vdolezal@redhat.com>
2017-11-29 16:21:07 +01:00
Sami Kerola 0b7e477188 lib/signames: remove unused function
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2017-11-28 14:39:17 +01:00
Karel Zak 365ed9f639 lib/mbsalign: add mbs_invalid_encode()
Like mbs_safe_encode(), but it does not care about control chars.

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-11-22 14:41:10 +01:00
John Groves 277e777fe8 lib: Add simple crc32c() function
Source: freebsd/sys/libkern/crc32.c
This code is an unmodified fragment from the source.  Will fixup
comments / naming in next commit

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-11-16 11:44:39 +01:00
J William Piggott 4111bb3ab5 lib/timeutils: add common ISO timestamp masks
* Start the ISO format flags at bit 0 instead of bit 1.

* Remove unnecessary _8601 from ISO format flag names to
  avoid line wrapping and to ease readability.

* ISO timestamps have date-time-timzone in common, so move
  the TIMEZONE flag to bit 2 causing all timestamp masks
  to have the first three bits set and the last four bits
  as timestamp 'options'.

* Change the 'SPACE' flag to a 'T' flag, because it makes
  the code and comments more concise.

* Add common ISO timestamp masks.

* Implement the ISO timestamp masks in all applicable code
  using the strxxx_iso() functions.

Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-11-10 16:34:55 -05:00
J William Piggott 9fd0a7a96c lib/timeutils: add get_gmtoff()
This new function returns the GMT offset relative to its
argument. It is used in this patch to fix two bugs:

1) On platforms that the tm struct excludes tm_gmtoff,
   hwclock assumes a one hour DST offset. This can cause
   an incorrect kernel timezone setting. For example:

 Master branch tested with tm_gmtoff illustrates the correct offset:
$ TZ="Australia/Lord_Howe" hwclock --hctosys --test | grep settimeofday
Calling settimeofday(1507494204.192398, -660)

 Master branch tested without tm_gmtoff has an incorrect offset:
$ TZ="Australia/Lord_Howe" hwclock --hctosys --test | grep settimeofday
Calling settimeofday(1507494249.193852, -690)

 Patched tested without tm_gmtoff has the correct offset:
$ TZ="Australia/Lord_Howe" hwclock --hctosys --test | grep settimeofday
Calling settimeofday(1507494260.194208, -660)

2) ISO 8601 'extended' format requires all time elements
   to use a colon (:).

Current invalid ISO 8601:
$ hwclock
2017-10-08 16:25:17.895462-0400

Patched:
$ hwclock
2017-10-08 16:25:34.141895-04:00

Also required by this change:
login-utils/last.c: increase ISO out_len and in_len by one to
                    accommodate the addition of the timezone colon.

Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-11-10 16:14:04 -05:00
Luca Ceresoli 424cee0f06 md5: declare byteReverse as static
byteReverse() is an internal function in md5.c, and is not exposed via
any header file, but it is not declared as static. This is a problem
with the md5.c file since it is copied more or less verbatim in other
programs (fontconfig and pjsip among others), causing a link error
when linking two of them together.

Fixes link failures such as:
  http://autobuild.buildroot.net/results/419ab2c0e034cc68991281c51caa8271b0fadbab/build-end.log

Signed-off-by: Luca Ceresoli <luca@lucaceresoli.net>
2017-11-08 11:49:59 +01:00
Karel Zak 61b9233995 lib/mangle: return size of the decoded buffer
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-11-01 15:33:04 +01:00
Karel Zak d182565be1 lib/path: make path_set_prefix() independent on cpu_set_t
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-10-23 15:45:01 +02:00
Karel Zak 7b41ad363f lib/signames: fix redefinition of 'sys_signame' on OSX
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-10-23 15:17:10 +02:00
Karel Zak c61e986fd0 lib/signames: remove signame array from header file
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-10-23 13:18:57 +02:00
Niklas Hambüchen b6e1977db4 signames: Make input char buffers const 2017-10-14 04:46:13 +02:00
Niklas Hambüchen 6855f6e80b kill: Extract signal names into signames.h/signames.c 2017-10-14 04:46:13 +02:00
Karel Zak 1458c5c750 lib/pager: check open() return code [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-10-11 14:42:49 +02:00
Karel Zak 4bb91e84ff lib/mbsedit: check wctomb() return code [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-10-11 14:40:37 +02:00
Karel Zak c4e60bc080 losetup: fix conflicting types for ‘loopcxt_set_blocksize’
Reported-by: Lars Wendler <polynomial-c@gentoo.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-10-04 10:08:08 +02:00
Karel Zak 43dc893449 lib/randutils: remove superfluous continue
Addresses: fa94979207 (commitcomment-24678670)
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-10-02 11:30:49 +02:00
Stanislav Brabec a1a41597bf losetup: Add support for logical block size
Kernel since 4.14 supports setting of logical block size[1]. It allows to
create loop devices that report logical block size different from 512.

Add support for this feature to losetup.

References:
[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/drivers/block/loop.c?id=89e4fdecb51cf5535867026274bc97de9480ade5

[kzak@redhat.com: - fix loopcxt_get_blocksize()
                  - remove lo_blocksize from loop_info64]

Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
Cc: Ming Lei <ming.lei@redhat.com>
Cc: Hannes Reinecke <hare@suse.com>
Cc: Omar Sandoval <osandov@fb.com>
Cc: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-09-27 14:30:02 +02:00
Tobias Stoeckmann c7f87da2ba setproctitle: fix out of boundary access
A program using setproctitle can trigger an out of boundary access
if an attacker was able to clear the environment before execution.

The check in setproctitle prevents overflows, but does not take into
account that the whole length of the arguments could be 1, which is
possible by supplying such a program name to execlp(3) or using a
symbolic link, e.g. argv[0] = "l", argv[1] = NULL.

Only login uses setproctitle, which is not affected by this
problem due to initializing the environment right before the call.
2017-09-26 12:04:41 +02:00
Karel Zak 1742c8d84c lib/pwdutils: add xgetlogin()
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-09-18 11:48:56 +02:00
Karel Zak 4f5f35fc83 login: add xgetpwnam()
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-09-18 11:48:56 +02:00
Philip Prindeville 0047626887 libuuid: add support for hash-based UUIDs
Adding V3 and V5 UUIDs per RFC-4122.

[kzak@redhat.com: - fix symbols file]

Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-09-05 11:26:39 +02:00
Karel Zak 0158e68fbd lib/match: make match_fstype() case insensitive
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-08-31 14:15:23 +02:00
Ruediger Meier e282116f6d misc: fix some printf format strings
Noticed on xcode/OSX.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-08-16 15:51:15 +02:00
Karel Zak 51013d3efa lib/randutils: reset lose counter
It's probaly good idea to reset lose counter when we fallback from
getrandom() to /dev/urandom.

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-08-14 10:56:08 +02:00
Karel Zak 5264aebb4f lib/randutils: improve getrandom() usage
The getrandom() does not have to return all requested bytes (missing
entropy or when interrupted by signal). The current implementation in
util-linux stupidly asks for all random data again, rather than only
for missing bytes.

The current code also does not care if we repeat our requests for
ever; that's bad.

This patch uses the same way as we already use for reading from
/dev/urandom. It means:

 * repeat getrandom() for only missing bytes
 * limit number of unsuccessful request (16 times)
 * fallback to /dev/urandom on ENOSYS (old kernel or so...)

Addresses: https://github.com/karelzak/util-linux/issues/496
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-08-14 10:33:06 +02:00
Christopher James Halse Rogers cc7d1fd16d lib/randutils.c: More paranoia in getrandom() call.
If getrandom() is called with nbytes ≥ 256 then it can return with less than the requested
bytes filled.

In this case we *could* adjust the buffer by the number of bytes actually read, but it's
simpler to just redo the call.
2017-08-08 11:28:25 +10:00
Christopher James Halse Rogers a7df0f5f35 lib/randutils.c: Fall back gracefully when kernel doesn't support getrandom(2).
The 3.16 kernel is supported until 2020, and various distros have kernels of the same
vintage. It's entirely possible for code built against newer headers to be run against
these kernels, so fall-back to the old “read /dev/{u,}random” method if the kernel doesn'
support getrandom()
2017-08-08 11:27:49 +10:00
Ruediger Meier fc77d25f97 parse-date: fix printf format
warning on 32bit systems:
../lib/parse-date.y: In function 'parse_date':
../lib/parse-date.y:1509:7: warning: format '%ld' expects argument of type 'long int', but argument 4 has type 'intmax_t' [-Wformat=]
       abs_time_zone_min);
       ^

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-07-18 11:06:52 +02:00
Karel Zak 884659b32a libblkid: don't use CDROM_GET_CAPABILITY ioctl for DM devices
For some reason kernel commit e980f62353c697cbf0c4325e43df6e44399aeb64
add extra warning when the ioctl is used for DM devices. It seems we
can avoid this ioctl when the device has dm/uuid.

Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1469532
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-07-12 14:35:53 +02:00
Karel Zak 07fd6640c8 Merge branch 'path-fixes' of https://github.com/rudimeier/util-linux
* 'path-fixes' of https://github.com/rudimeier/util-linux:
  hwclock: don't ifdef printf arguments
  setpriv: align --help
  tools: add segfault detection for checkusage.sh
  misc: avoid some dead initialization warnings
  lscpu: make clang analyzer happy
  lsmem: fix, using freed memory
  lib/path: add error handling to path_vcreate()
  lib/path: fix crash, pathbuf overflow
2017-06-29 15:29:33 +02:00
Ruediger Meier c3ae785433 misc: avoid some dead initialization warnings
Clang analyzer warnings:

Dead store, Dead initialization:

    lib/mbsedit.c:154:8: warning: Value stored to 'in' during its initialization is never read
            char *in = (char *) &c;
                  ^~   ~~~~~~~~~~~

    misc-utils/findmnt-verify.c:129:14: warning: Value stored to 'cn' during its initialization is never read
            const char *cn = tgt;
                        ^~   ~~~
Dead store, Dead increment:

    sys-utils/hwclock.c:1461:2: warning: Value stored to 'argv' is never read
            argv += optind;
            ^       ~~~~~~

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-29 14:04:25 +02:00
Ruediger Meier 81435af3be lsmem: fix, using freed memory
Simply avoiding strdup(). Error handling improved.

This was the Clang Analyzer warning:

    Memory Error, Use-after-free
    sys-utils/lsmem.c:259:3: warning: Use of memory after it is freed
                    err(EXIT_FAILURE, _("Failed to open %s"), path);
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-29 14:04:18 +02:00
Ruediger Meier f567220b71 lib/path: add error handling to path_vcreate()
Do not operate on truncated/random paths. Note, path_strdup()
can now really return NULL, to be handled in next commit.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-29 14:03:49 +02:00
Ruediger Meier ad553030fb lib/blkdev: remove support for Linux < 2.6
Remove a hack for kernels between  2.4.15 and 2.4.17

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-29 12:56:22 +02:00
Ruediger Meier e230ae7b68 lib/path: fix crash, pathbuf overflow
Before:

$ lscpu -s "$(tr '\0' 'x' < /dev/zero | head -c 10000)"
Segmentation fault (core dumped)

After:

$ lscpu -s "$(tr '\0' 'x' < /dev/zero | head -c 10000)"
lscpu: invalid argument to --sysroot: File name too long

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-27 21:39:35 +02:00
Ruediger Meier 9c8b9fbacc lib: fix strutils.h, remove STRTOXX_EXIT_CODE
As discussed on the mailing list. We fix all places
where the non-working define STRTOXX_EXIT_CODE was used.

Regarding tunelp, also see 7e3c80a7.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-22 22:40:12 +02:00
Karel Zak eba71dd06c Merge branch '170424' of github.com:jwpi/util-linux
* '170424' of github.com:jwpi/util-linux:
  parse-date: time_zone_hhmm() bug fixes
  parse-date: remove unused ordinal_day_seen
  parse-date: remove unused year_seen
  parse-date: refactor tm_diff()
  parse-date: use to_uchar() instead of assignment.
  parse-date: use uintmax_t where appropriate
  parse-date: use int where appropriate
  parse-date: use intmax_t where appropriate
  parse-date: remove unused EPOCH_YEAR
  parse-date: replace ISDIGIT with c_isdigit
2017-06-20 10:20:49 +02:00
J William Piggott 480d6bd80a parse-date: time_zone_hhmm() bug fixes
* fix incorrect arg type used for abs().
* prevent integer arithmetic overflow by limiting offset to 4 digits.
* don't accept malformed offset values like:
    -4:3
    +12:719
    +0000001:23
    -9:00000001
    +0000001123

Reported-by: Ruediger Meier <ruediger.meier@ga-group.nl>
Influenced-by: gnulib 30784c4 Paul Eggert <eggert@cs.ucla.edu>
Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-06-15 15:03:04 -04:00
J William Piggott dc65dd64ae parse-date: remove unused ordinal_day_seen
Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-06-15 15:03:04 -04:00
J William Piggott cf4b385c35 parse-date: remove unused year_seen
Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-06-15 15:03:04 -04:00
J William Piggott 1d6702edfd parse-date: refactor tm_diff()
Reported-by: Ruediger Meier <ruediger.meier@ga-group.nl>
Influenced-by: gnulib 30784c4 Paul Eggert <eggert@cs.ucla.edu>
Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-06-15 15:02:58 -04:00
J William Piggott bfbace763c parse-date: use to_uchar() instead of assignment.
Reported-by: Ruediger Meier <ruediger.meier@ga-group.nl>
Influenced-by: gnulib 30784c4 Paul Eggert <eggert@cs.ucla.edu>
Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-06-15 15:02:49 -04:00
J William Piggott 07668cd1fa parse-date: use uintmax_t where appropriate
Reported-by: Ruediger Meier <ruediger.meier@ga-group.nl>
Influenced-by: gnulib 30784c4 Paul Eggert <eggert@cs.ucla.edu>
Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-06-15 15:02:41 -04:00
J William Piggott 75f8be9eb6 parse-date: use int where appropriate
Reported-by: Ruediger Meier <ruediger.meier@ga-group.nl>
Influenced-by: gnulib 30784c4 Paul Eggert <eggert@cs.ucla.edu>
Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-06-15 15:02:32 -04:00
J William Piggott 960f98c97a parse-date: use intmax_t where appropriate
Reported-by: Ruediger Meier <ruediger.meier@ga-group.nl>
Influenced-by: gnulib 30784c4 Paul Eggert <eggert@cs.ucla.edu>
Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-06-15 15:02:21 -04:00
J William Piggott 36a5afd8e8 parse-date: remove unused EPOCH_YEAR
Reported-by: Ruediger Meier <ruediger.meier@ga-group.nl>
Influenced-by: gnulib 30784c4 Paul Eggert <eggert@cs.ucla.edu>
Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-06-15 15:02:07 -04:00
J William Piggott 6fc1d48d3d parse-date: replace ISDIGIT with c_isdigit
Reported-by: Ruediger Meier <ruediger.meier@ga-group.nl>
Influenced-by: gnulib 30784c4 Paul Eggert <eggert@cs.ucla.edu>
Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-06-15 15:00:33 -04:00
Ruediger Meier db981e3e0d lib: style cosmetics plymouth-ctrl.c
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-15 09:13:04 +02:00
Sami Kerola 143ea06851 lib: remove _RLD_ from forbid environment variable list
The RLD environment variables are related to runtime linker vulnerability in
TELNET on systems running Silicon Graphics IRIX.  It is extremely unlikely
current util-linux would be compiled on such system.

Reference: http://www.cert.org/historical/advisories/CA-1995-14.cfm
Reference: http://signatures.juniper.net/documentation/signatures/TELNET%3AEXPLOIT%3ASGI-RLD.html
Refefence: http://www.polarhome.com/service/man/?qf=rld&tf=2&of=IRIX&sf=1
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2017-06-14 12:23:42 +02:00
Sami Kerola 35e82b2926 lib: simplify cpuset if clauses that return
There is no need for 'else' when 'if' will return.  In same go move call of
tolower() to last possible moment in char_to_val(), a lot of time hex values
should hit 0-9 range, and it can be omitted.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2017-06-14 12:22:54 +02:00
Sami Kerola f4d3783867 misc: remove stray semicolons
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2017-06-14 12:21:29 +02:00
Ruediger Meier acecab61e5 misc: fix gcc-7 snprintf warnings -Wformat-truncation
../lib/loopdev.c: In function 'loopcxt_next_from_sysfs':
../lib/loopdev.c:545:32: warning: '/loop/backing_file' directive output may be truncated writing 18 bytes into a region of size between 1 and 256 [-Wformat-truncation=]
   snprintf(name, sizeof(name), "%s/loop/backing_file", d->d_name);
                                ^~~~~~~~~~~~~~~~~~~~~~
../lib/loopdev.c:545:3: note: 'snprintf' output between 19 and 274 bytes into a destination of size 256
   snprintf(name, sizeof(name), "%s/loop/backing_file", d->d_name);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

../lib/sysfs.c: In function 'sysfs_is_partition_dirent':
../lib/sysfs.c:343:31: warning: '/start' directive output may be truncated writing 6 bytes into a region of size between 1 and 256 [-Wformat-truncation=]
  snprintf(path, sizeof(path), "%s/start", d->d_name);
                               ^~~~~~~~~~
../lib/sysfs.c:343:2: note: 'snprintf' output between 7 and 262 bytes into a destination of size 256
  snprintf(path, sizeof(path), "%s/start", d->d_name);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../lib/sysfs.c: In function 'sysfs_partno_to_devno':
../lib/sysfs.c:372:32: warning: '/partition' directive output may be truncated writing 10 bytes into a region of size between 1 and 256 [-Wformat-truncation=]
   snprintf(path, sizeof(path), "%s/partition", d->d_name);
                                ^~~~~~~~~~~~~~
../lib/sysfs.c:372:3: note: 'snprintf' output between 11 and 266 bytes into a destination of size 256
   snprintf(path, sizeof(path), "%s/partition", d->d_name);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../lib/sysfs.c:377:33: warning: '/dev' directive output may be truncated writing 4 bytes into a region of size between 1 and 256 [-Wformat-truncation=]
    snprintf(path, sizeof(path), "%s/dev", d->d_name);
                                 ^~~~~~~~
../lib/sysfs.c:377:4: note: 'snprintf' output between 5 and 260 bytes into a destination of size 256
    snprintf(path, sizeof(path), "%s/dev", d->d_name);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-14 11:48:22 +02:00
Karel Zak f46a8d7e66 lib/ttyutils: return terminal lines too
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-06-12 11:38:38 +02:00
Karel Zak 99791a1984 lib/pager: use xalloc
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-06-02 12:07:36 +02:00
Karel Zak 535a4090b4 lib/pager: don't use pager if command not available
for example:
 # PAGER=foo dmesg -H
 sh: foo: command not found

the same problem is we have with fdisk 'l' command:

 # PAGER=foo fdisk /dev/sda
 Welcome to fdisk (util-linux 2.30-rc2-33-41b71).
 ...
 Command (m for help): l
 sh: foo: command not found

It seems better to don't use pager at all if not available.

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-06-01 14:20:20 +02:00
Ruediger Meier f2ff0adf5d misc: fix some warnings
sys-utils/prlimit.c: In function 'do_prlimit':
sys-utils/prlimit.c:367:16: warning: format '%ju' expects argument of type 'uintmax_t', but argument 2 has type 'rlim_t {aka long long unsigned int}' [-Wformat=]
     printf("<%ju", new->rlim_cur);

lib/plymouth-ctrl.c: In function 'open_un_socket_and_connect':
lib/plymouth-ctrl.c:88:20: warning: passing argument 2 of 'connect' from incompatible pointer type [-Wincompatible-pointer-types]
  ret = connect(fd, &su, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(su.sun_path+1));
                    ^
In file included from lib/plymouth-ctrl.c:35:0:
/usr/include/sys/socket.h:314:5: note: expected 'const struct sockaddr *' but argument is of type 'struct sockaddr_un *'
 int connect (int, const struct sockaddr *, socklen_t);

login-utils/last.c: In function 'list':
login-utils/last.c:506:54: warning: pointer targets in passing argument 4 of 'dns_lookup' differ in signedness [-Wpointer-sign]
   r = dns_lookup(domain, sizeof(domain), ctl->useip, p->ut_addr_v6);
                                                      ^
login-utils/last.c:291:12: note: expected 'int32_t * {aka int *}' but argument is of type 'unsigned int *'
 static int dns_lookup(char *result, int size, int useip, int32_t *a)
            ^~~~~~~~~~

In file included from sys-utils/hwclock-cmos.c:92:0:
sys-utils/hwclock.h:67:32: warning: 'struct timeval' declared inside parameter list will not be visible outside of this definition or declaration
 extern double time_diff(struct timeval subtrahend, struct timeval subtractor);

misc-utils/test_uuidd.c: In function 'create_nthreads':
misc-utils/test_uuidd.c:187:19: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
        proc->pid, (int) th->tid, th->index));

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-01 10:28:18 +02:00
Ruediger Meier c0d7b11a3f build-sys: fix library order when linking
We got some errors on Alpine Linux where $LTLIBINTL is non-empty:

./.libs/libcommon.a(libcommon_la-blkdev.o): In function `open_blkdev_or_file':
lib/blkdev.c:282: undefined reference to `libintl_gettext
collect2: error: ld returned 1 exit status

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-01 03:16:48 +02:00
Karel Zak 3947ca4ca9 build-sys: ncurses headers cleanup
* assume ncursesw headers in ncursesw/ directory only
* prefer long paths, <term.h> and <ncurses.h> should be last
  possibility
* fix typos

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-05-31 11:01:46 +02:00
Karel Zak c9ae73c03f lib/colors: fix test compilation
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-05-31 01:29:25 +02:00
Karel Zak 23d4726784 lib/colors: ncurses cleanup
* use proper paths to term.h
* keep ncurses support optional
* link with TINFO_LIBS (-ltinfo), or fallback to NCURSES_LIBS (-ltinfo -lncurses)
* don't include unnecessary ncurses.h (term.h is enough)

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-05-30 17:14:12 +02:00
Karel Zak 853a2ea486 lib/loopdev: remove unnecessary header
Reported-by: Assaf Gordon <assafgordon@gmail.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-05-30 10:47:06 +02:00
Karel Zak 22cfe66265 lib/parse-date: don't use xalloc in lib-like code
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-05-12 11:17:02 +02:00
Karel Zak b8af37d767 build-sys: remove duplicate includes
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-05-12 11:17:02 +02:00
Sami Kerola c6b0cbdd95 lib/mbsedit: fix typo in comment
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2017-05-10 10:53:28 +02:00
Karel Zak dc9a23fe66 lib/timer: add comment
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-04-28 13:25:41 +02:00
Rasmus Villemoes e2cd1072b4 lib/timer.c: prevent pathological race condition
flock(1) uses the timer facility to interrupt a blocking flock(2)
call. However, in a pathological case (or with a sufficiently short
timeout), the timer may fire and the signal be delivered after the
timer is set up, but before we get around to doing the flock(2)
call. In that case, we'd block forever. Checking timeout_expired right
before calling flock(2) does not eliminate that race, so the only
option is to make the timer fire repeatedly. Having the timer fire
after we've returned from flock(2) is not a problem, since we only
check timeout_expired in case of EINTR (also, this firing after return
could also happen with the current code).

There is currently one other user of setup_timer (misc-utils/uuidd.c),
but in that case the signal handler simply exits. Future users of
setup_timer obviously need to ensure that they can tolerate multiple
signal deliveries.

Choosing 1% of the initial timeout as the repeating interval is
somewhat arbitrary. However, I put a lower bound of 0.01s, since
setting the interval much smaller than this may end up effectively
live-locking the process, handling a never-ending stream of signals.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
2017-04-26 13:35:26 +02:00
Rasmus Villemoes c6a9147fd8 lib/timer.c: remove bogus SA_SIGINFO from timer_settime call
The only valid flag for timer_settime is TIMER_ABSTIME, which we
certainly don't want here. This seems to be harmless since
timer_settime doesn't validate the flags parameter, TIMER_ABSTIME is
universally 0x1, and no architecture has SA_SIGINFO == 1.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
2017-04-26 13:35:18 +02:00
J William Piggott dc0b13af68 parse-date: remove extraneous form feed
* lib/parse-date.y: Remove extraneous form feed (FF, 0x0C, ^L).

Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-04-10 14:26:12 -04:00
Karel Zak d1e27bf190 Merge branch '2017wk13' of https://github.com/kerolasa/lelux-utiliteetit
* '2017wk13' of https://github.com/kerolasa/lelux-utiliteetit:
  docs: improve agetty.8 manual page
  agetty: make --remote to forward --nohostname as -H to login
  lib/cpuset: stop changing variable that is not read
  agetty: remove variable that is set but not read
2017-04-04 14:20:25 +02:00
Karel Zak cab84ea39c lib/strutils: fix typos in huge size suffixes
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-04-03 12:52:53 +02:00
Sami Kerola 4783f2005d
lib/cpuset: stop changing variable that is not read
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2017-04-01 09:24:37 +01:00
Karel Zak 790119b885 agetty: fix characters reorder in login prompt
The current agetty uses TIOCSTI ioctl to return already read chars
from login name back to the terminal (without read() before
tcsetattr() we will lost data already written by user). The ioctl
based solution is fragile due to race -- we can return chars when
terminal already contains another new chars. The result is reordered
chars in login name.

The solution is to use extra buffer for already read data.

Reported-by: Michael Tretter <m.tretter@pengutronix.de>
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-03-27 14:54:39 +02:00
Karel Zak 3006dd7352 Merge branch '2017wk11' of git://github.com/kerolasa/lelux-utiliteetit
* '2017wk11' of git://github.com/kerolasa/lelux-utiliteetit:
  blkid: add control struct
  blkid: simplify version option handling
  tests: add static keyword where needed [smatch scan]
  tests: do not use plain 0 as NULL [smatch scan]
  libsmartcols: fix test variable shadowing
2017-03-24 11:59:57 +01:00
Karel Zak 7e26832d70 Merge remote-tracking branch 'jwpi/hwclock-date7'
* jwpi/hwclock-date7:
  lib: add parse-date documentation
  hwclock: use parse_date function
  build-sys: add parse-date.y
  lib: add parse-date.y
2017-03-23 12:36:47 +01:00
Sami Kerola 71f08e9706
tests: do not use plain 0 as NULL [smatch scan]
Likewise commit 8791804065.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2017-03-17 21:22:26 +00:00
Karel Zak ab7093775f lib/colors: assert rathen than overflow [-Wnull-dereference]
lib/colors.c: In function 'colors_read_schemes':
lib/colors.c:412:7: warning: potential null pointer dereference [-Wnull-dereference]

Reported-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-03-13 14:44:22 +01:00
Karel Zak 30d7f61f4f build-sys: add parse-date.y
* add lib/parse-date.y to build system
* add necessary autotools stuff to generate .c on the fly
  (autotools are smart enough to add generated file to tarball)
* check for bison version by ./autogen.sh
* add non-wanted junk to .gitignore

With some modification by J William Piggott with regard to
moving the parse-date API into timeutils.h

Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-03-04 11:01:56 -05:00
J William Piggott 7088bd8832 lib: add parse-date.y
* include/timeutils.h
    Add parse-date API

* lib/parse-date.y - new file
    Lib function that parses a date string into a timespec struct.
    Derived from gnulib-dd7a871 parse-datetime.y with these changes:
            * reduced to a single function API renamed to parse_date()
            * removed gnulib dependencies
            * removed debugging
            * converted to util-linux coding style

* include/cctype.h - new file
    Like ctype.h only hard coded to the 'C' locale.
    Used by lib/parse-date.y.
    Derived from gnulib-dd7a871 c-ctype.h with these changes:
            * removed gnulib dependencies
            * converted to util-linux coding style
            * add requisite util-linux constants

Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-03-04 10:49:41 -05:00
Sami Kerola a2c8c533ce lib/idcache: add void to function declaration [smatch scan]
lib/idcache.c:29:29: warning: non-ANSI function declaration of function
'new_idcache'

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2017-02-20 13:00:58 +01:00
Sami Kerola 2ba641e5f3 misc: add static keyword to where needed [smatch scan]
text-utils/rev.c:68:9: warning: symbol 'buf' was not declared. Should it be
static?

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2017-02-20 12:58:49 +01:00
Sami Kerola 8791804065 misc: do not use plain 0 as NULL [smatch scan]
text-utils/tailf.c:69:21: warning: Using plain integer as NULL pointer

Since many 'struct option' has used zero as NULL make them more readable in
same go by reindenting, and using named argument requirements.

Reference: https://lwn.net/Articles/93577/
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2017-02-20 12:58:49 +01:00
Karel Zak 299ef2c457 lib/mbsedit: add simple buffer editor
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-02-16 13:47:44 +01:00
Sami Kerola b192dd6943 lib/randutils: glibc 2.25 has getrandom(2) declaration
Use correct header file to include the function.

Reference: http://git.kernel.org/cgit/docs/man-pages/man-pages.git/commit/?id=2cbb6fb4e926e56dc3985b19ac02389321a0af5d
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2017-02-13 14:14:28 +01:00
Yuri Chornoivan a7349ee315 docs: Fix word repetitions 2017-02-13 14:10:12 +01:00
Sami Kerola b72a75e993
lib: add timegm() portability function to lib/timeutils.c
Local timegm() is a replacement function in cases it is missing from libc
implementation.  Hopefully the replacement is never, or very rarely, used.

CC: Ruediger Meier <ruediger.meier@ga-group.nl>
Reviewed-by: J William Piggott <elseifthen@gmx.com>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2017-02-03 22:31:18 +00:00