Commit Graph

937 Commits

Author SHA1 Message Date
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
Karel Zak 61cbc8a3f5 lib/strutils: return end pointer by isdigit_string()
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-01-28 12:39:30 +01:00
Karel Zak 63dc575be3 build-sys: test_linux_version is Linux only
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-01-04 15:44:51 +01:00
Karel Zak e33da967ef lib/linux_version: fix code indention
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-01-04 15:42:02 +01:00
Karel Zak e8f7acb0d3 lib: use unique ifdefs for tests
Let's use unique TEST_PROGRAM_<NAME> ifdefs to make build system more
robust.

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-01-04 11:44:37 +01:00
Karel Zak 5b9403a68f lib/linux_version: add test for manual testing
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-01-02 11:36:53 +01:00
Karel Zak 6b95593d79 lib/linux_version: fix stupid typo
... introduced by commit d404065a. Sorry.

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-01-02 11:32:24 +01:00
Karel Zak 963f7dfb82 Merge branch 'getrandom' of git://github.com/kerolasa/lelux-utiliteetit
* 'getrandom' of git://github.com/kerolasa/lelux-utiliteetit:
  lib/randutils: use getrandom(2) when it is available
2016-12-22 15:27:25 +01:00
Sébastien Helleu d673b74e9d docs: replace FTP by HTTPS in kernel.org URLs
The links to ftp://ftp.kernel.org/ are replaced by
https://www.kernel.org/.

Signed-off-by: Karel Zak <kzak@redhat.com>
2016-12-19 11:22:26 +01:00
Sami Kerola cc01c2dca4
lib/randutils: use getrandom(2) when it is available
System call getrandom(2) is relatively new, available since kernel 3.17 but
not supported by glibc 2.24.  That in mind autotools is made to check
availability of this function and keep old code as fallback.  It is
reasonable assume it will take years before the syscall(2) and fallback are
unproblematic to remove.

One might ask why bother using getrandom(2).  Main reason is to avoid
unnecessary system calls to achieve exactly same end result.  That
demonstrated with 'strace -c ./mcookie' showing 36 calls before, and 32
after this change.  Secondly the getrandom(2) function got to kernel with
promise it can be used to avoid file descriptor run down, and since uuidd
uses random_get_bytes() it should fulfill it's promise here.

Reference: http://man7.org/linux/man-pages/man2/getrandom.2.html
Reference: https://lwn.net/Articles/606141/
Reviewed-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2016-12-11 11:46:54 +00:00
Michael Kerrisk 46f057ed37 docs: various pages: Format pathnames as italic (.I)
In the majority of pages, pathnames are formatted as Italic,
which is the norm. However, there are several cases where they
are formatted as bold. This patch fixes a number of those
exceptions.

Signed-off-by: Michael Kerrisk <mtk.man-pages@gmail.com>
2016-12-09 13:45:09 +01:00
Ruediger Meier 223939d95b misc: spelling, always use "cannot" instead of "can not"
Just to be consistent ...

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-11-30 14:56:50 +01:00
Heiko Carstens 54394eab03 lib,strutils: add strtoux[16|32|64]_or_err functions
Add helper functions which allow to parse hexadecimal numbers.
Based on a patch from Clemens von Mann.

Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
2016-11-09 10:02:32 +01:00
Gustavo Zacarias 7f0d4d56a2 lib/crc32: prefix public functions
Make the publicly-visible crc32 library functions prefixed by ul_, such
as crc32() -> ul_crc32().
This is because it clashes with the crc32() function from zlib.
For newer versions of glib (2.50+) zlib and libblkid are required
dependencies and otherwise results in build failure when building
statically.

Signed-off-by: Gustavo Zacarias <gustavo@zacarias.com.ar>
2016-10-19 12:11:59 +02:00
Karel Zak 0b27fcc189 lib/randutils: remove unnecessary function
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-10-04 16:18:16 +02:00
Karel Zak a86b9f252c lib/mbsalign: free buffer after error [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-10-04 16:14:48 +02:00
Karel Zak ff471d89a1 libsmartcols: support multi-line cells based on line breaks
Now libsmartcols completely control when and how wrap long
lines/cells. This is sometimes user unfriendly and it would be nice to
support multi-line cells where wrap is based on \n (new line char).

This patch add new column flag SCOLS_FL_WRAPNL.

Signed-off-by: Karel Zak <kzak@redhat.com>
2016-09-06 10:51:25 +02:00
Karel Zak a55d646bd3 lib/randutils: add xsrand() and rand_get_number()
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-09-06 10:19:52 +02:00
Tobias Stoeckmann cb129d9cc4 lib/loopdev: Set errno in is_loopdev on error
The function is_loopdev does not set errno if the supplied string does
not reference a valid loop device. Fix this to avoid an error message
like this one:

  losetup: /: failed to use device: Success

I prefer this one:

  losetup: /: failed to use device: No such device

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2016-08-31 09:49:01 +02:00
Karel Zak e132ae59ca lib/pager: fix test
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-08-26 17:47:01 +02:00
Karel Zak a9fcbf6f6a lib/pager: restore signals setting by pager_close()
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-08-26 13:02:37 +02:00
Karel Zak e215d467ca lib/pager: cleanup and extend API
* clean up function names

* add functions to temporary redirect to the pager and then restore
  original terminal output

Signed-off-by: Karel Zak <kzak@redhat.com>
2016-08-26 12:07:25 +02:00
Stanislav Brabec bfd4e1f758 loopdev: Implememt loopcxt_set_status()
Implement stand-alone loopcxt_set_status(). It allows manipulation with some
loop device parameters even if it is initialized.

Its function is limited by the kernel implementation, and only a small subset of
changes is allowed.

For more see linux/drivers/block/loop.c:loop_set_status()

Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
2016-08-17 12:49:06 +02:00
Karel Zak 9a94b634a3 losetup: add --nooverlap options
This patch introduces overlap detections and loop devices
re-use for losetup(8). We already support this feature for mount(8)
where it's enabled by default (because we mount filesystems and it's
always mistake to share the same filesystem between more loop
devices).

Stanislav has suggested to enable this feature also for losetup by
default. I'm not sure about it, IMHO it's better to keep losetup(8)
simple and stupid by default, and inform users about possible problems
and solutions in the man page.

The feature forces losetup to scan all loop devices always when new
one is requested. This maybe disadvantage (especially when we use
control-loop  to avoid /sys or /dev scans) on system with huge number
of loop devices.

Co-Author: Stanislav Brabec <sbrabec@suse.cz>
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-08-17 12:28:33 +02:00
Sami Kerola 984a60965a
misc: always check setenv(3) return value
At least glibc setenv(3) can fail when system runs out of memory.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2016-08-12 21:25:36 +01:00
Franco Fichtner 6978b2c430 blkdev: guard against missing DIOCGDINFO on FreeBSD 11
DIOCGMEDIASIZE is supported and available since FreeBSD 5, it may
make sense to kill the backwards-glue completely instead.
2016-08-11 14:58:50 +02:00
Karel Zak d4e89dea4e libmount: ignore redundant slashes
///aaa/bbb and /aaa/bbb/ are the same paths. This is important
especially with NFS where number of slashes are not the same in
the /proc/self/mountinfo and fstab or utab. The regular URI is

 euler://tmp

but /proc contains

 euler:/tmp

Reported-by: Ales Novak <alnovak@suse.cz>
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-08-08 17:23:54 +02:00
Karel Zak dff7e16046 libmount: one iteration to detect overlap and reuse loopdev
The current code scans loopdevs to detect already used loop device and
another scan to detect overlap.

Let's use one scan only, for this purpose loopcxt_find_overlap() has
been modified to return info (rc==2) about full size and offset match.

Signed-off-by: Karel Zak <kzak@redhat.com>
2016-08-04 11:48:26 +02:00
Karel Zak c444a71b1b lib/loopdev: cleanup sizelimit check
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-08-04 10:49:46 +02:00
Stanislav Brabec 211e1d4609 Implement loopcxt_check_conflict()
Add a function that searches for a possible conflicting (i. e. overlaying loop
device).

Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
2016-08-03 11:53:28 +02:00
Stanislav Brabec 74a4705a99 Add sizelimit to internal API
Fully safe checks of loop device need to check sizelimit. To prevent need of two
nearly equal functions, introduce sizelimit parameter to several internal
functions:
loopdev_is_used()
loopdev_find_by_backing_file()
loopcxt_is_used()
loopcxt_find_by_backing_file()

If sizelimit is zero, fall back to the old behavior (ignoring of sizelimit).

Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
2016-08-03 11:53:28 +02:00
Sami Kerola 74ce680a3e
misc: simplify if clauses [oclint]
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2016-07-21 21:14:33 +01:00
Karel Zak b8fd5c0558 tests: fix ttyutils test
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-07-15 10:11:35 +02:00
Sami Kerola 285c1f3a3e
lib: try to find tty in get_terminal_name()
Try all standard terminal input/output file descriptors when finding tty
name in get_germinal_name().  This should make all invocations of the
function as robust as they can get.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2016-07-03 23:35:10 +01:00
Sami Kerola c161694682
libcommon: add ISO_8601_GMTIME that will print UTC-0 timestamps
When timestamps are intented to be conversable back from string to binary it
is best to stick with UTC-0 timezone.  This is needed in utmpdump(1).

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2016-06-26 21:58:18 +01:00
Karel Zak c191740c28 misc: cleanup non-widechar compilation
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-06-03 14:46:17 +02:00
Karel Zak 4a423fb963 lib/mbsalign: fix for non-widechar
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-06-03 12:18:21 +02:00
Karel Zak 638402edb0 lib/sysfs: be more smart for non-scsi devices
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-06-02 14:40:13 +02:00
Karel Zak 8f0ac57921 lib/ttyutils: use stdout for get_terminal_width()
We use the function to be sure with *output* width. Note that the
current code (with STDIN) is broken because in some situations
libsmartcols is not able to detect terminal width and fall back to
default 80.

Signed-off-by: Karel Zak <kzak@redhat.com>
2016-06-02 12:31:45 +02:00
Sebastian Rasmussen 9e93004171 misc: Fix various typos
Fix various typos in error messages, warnings, debug strings,
comments and names of static functions.

Signed-off-by: Sebastian Rasmussen <sebras@gmail.com>
2016-05-31 23:40:21 +02:00
Karel Zak 1eb16fd780 build-sys: add --disable-plymouth-support
The plymouth support depends on Linux specific SOCK_* flags and all
the feature is probably unnecessary in some cases (non-plymouth
distros, etc.)

Signed-off-by: Karel Zak <kzak@redhat.com>
2016-05-26 15:08:53 +02:00
Karel Zak eee665bb5a lib/timeutils: add strtime_short()
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-05-24 13:21:26 +02:00
Karel Zak 33c7ffa303 include/timeutils: rewrite iso formatting functions
- use buffers rather than allocate memory
- support .usec and ,usec convention
- use strftime for timezone (we need to care about daylight
  saving time)

Signed-off-by: Karel Zak <kzak@redhat.com>
2016-05-24 10:58:52 +02:00
Karel Zak 01b47d46d7 include/timeutils: use pointer for time_t
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-05-23 11:56:31 +02:00
Werner Fink fe3f7e17aa sulogin: agetty: use the plymouth local protocol instead the plymouth binary
for stopping plymouthd.  That do not depend on the existence of
the plymouth binary if it e.g. becomes uninstalled or an other
service is providing plymouthd facilities.

[kzak@redhat.com: - fix compiler warnings [-Wpointer-sign]
                  - use sizeof() for write_all()
                  - cast to char* for read_all]

Signed-off-by: Werner Fink <werner@suse.de>
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-05-20 11:21:10 +02:00
Karel Zak 3c201431ee lib/timeutils: add strxxx_iso() functions
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-05-11 15:31:02 +02:00
Sami Kerola 34f6177a24
lib: avoid double free in loopdev.c
Found with scan-build.

Reviewed-by: Yuriy M. Kaminskiy <yumkam@gmail.com>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2016-04-20 22:03:26 +01:00
Karel Zak 2238214ddc libmount: try absolute target before canonicalize
The path canonicalization is expensive and in many cases unwanted due
to problems with readlink() on unreachable NFS and automounters.

This patch add a possibility to search also by $(CWD)/<path> if the
<path> is relative to reduce number of situation when we convert the
path to the canonical absolute path.

The common use-case:

	# cd /some/long/path
	# umount ./mountpoint

old version:

15543: libmount:      TAB: [0x560a99a54230]: lookup TARGET: './test'
15543: libmount:    CACHE: [0x560a99a54290]: canonicalize path ./test
15543: libmount:    CACHE: [0x560a99a54290]: add entry [ 1] (path): /mnt/test: ./test
15543: libmount:      TAB: [0x560a99a54230]: lookup canonical TARGET: '/mnt/test'
15543: libmount:      CXT: [0x560a99a54050]: umount fs: /mnt/test

new version:

15597: libmount:      TAB: [0xabf230]: lookup TARGET: './test'
15597: libmount:      TAB: [0xabf230]: lookup absolute TARGET: '/mnt/test'
15597: libmount:      CXT: [0xabf050]: umount fs: /mnt/test

Signed-off-by: Karel Zak <kzak@redhat.com>
2016-04-13 12:29:16 +02:00
Karel Zak d551d66891 lib/strutils: use int for fgetc()
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-04-04 11:01:15 +02:00
Kent Overstreet 7ce27dc517 libblkid: Update for newer bcache superblocks
Later version of bcache add different checksum types, and allow for superblocks
greater than 4k - skipping the checksum check (as in most other probes) is the
easiest solution.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2016-03-23 10:52:38 +01:00
Karel Zak 2d47fa3921 partx: fix --nr usage
Reported-by: Serge van den Boom <serge+util-linux@vdboom.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-03-22 15:49:00 +01:00
Karel Zak 40f00b4f8e libmount: improve conversion from root= to the devname
Currently the code supports /dev/name or PARTUUID= only. We also
need to support 'maj:min' and 'hexhex' notations.

Reported-by: George Spelvin <linux@horizon.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-03-15 14:07:34 +01:00
Karel Zak d404065a82 lib/linux_version: avoid major and minor shadowing
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-03-09 11:49:42 +01:00
Karel Zak 075d2c0754 include: move sys/sysmacros.h to c.h
The file is no portable (#ifdef HAVE_SYS_SYSMACROS_H is necessary),
but needed on many places. It seems better to keep it in c.h.

Signed-off-by: Karel Zak <kzak@redhat.com>
2016-03-08 14:29:45 +01:00
Karel Zak 3fe3f560b7 Revert "include sysmacros.h where used"
This reverts commit 46a40c0184.
2016-03-08 14:26:33 +01:00
Mike Frysinger 46a40c0184 include sysmacros.h where used
BSD/Linux systems stick major/minor/makedev in sysmacros.h.  Newer Linux
libraries have been moving away from including sysmacros.h implicitly via
sys/types.h, so include it directly.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
2016-03-08 14:21:55 +01:00
Ruediger Meier fea1cbf748 misc: never cast void* from malloc(3) and friends
Such cast could hide serious compiler warnings in case we are
missing includes (e.g. <stdlib.h> or "xalloc.h").

See
http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-03-07 23:29:27 +01:00
Ruediger Meier dc0495166f build-sys: add and use openat build conditionals
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-03-07 15:37:25 +01:00
Ruediger Meier 1a048dc5e2 lib: move fopen_at() to fileutils.h, remove at.h
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-03-07 15:36:00 +01:00
Ruediger Meier 2208b3ccb2 lib: remove openat fallback functions (include/at.h)
I have validated that we are still compatible at least back to
  - openSUSE 11.4
  - SLE 11
  - RHEL/CentOS 6
  - OSX 10.10.x, (Xcode 6.3)
  - FreeBSD 10.2

Confirmed incompatibility:
  - OSX 10.9.x, (Xcode 6.2)

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-03-07 15:33:56 +01:00
Yuriy M. Kaminskiy 06fa581748 misc: safer (and uniform) handling of return value
When `rc` is `INT_MAX`, `rc + 1` result in signed integer overflow.

Signed-off-by: Karel Zak <kzak@redhat.com>
2016-03-07 15:11:06 +01:00
Yuriy M. Kaminskiy ff27b20f3c lsns.c: fix error return
If non-negative value returned, errno could be unset (especially 0).


Signed-off-by: Karel Zak <kzak@redhat.com>
2016-03-07 15:11:06 +01:00
Ruediger Meier ab753d8fee lib: include strutils.h for mempcpy()
We have that mempcpy fallback since 2013 (02887b73) but forgot to
include it.

This fixes a segfault of cal(1) on FreeBSD and OSX.

Compiler warning was:
lib/mbsalign.c:468:14: warning: implicit declaration of function 'mempcpy' is invalid in C99 [-Wimplicit-function-declaration]
      dest = mempcpy (dest, str_to_print, min (n_used_bytes, space_left));
             ^
lib/mbsalign.c:468:12: warning: incompatible integer to pointer conversion assigning to 'char *' from 'int' [-Wint-conversion]
      dest = mempcpy (dest, str_to_print, min (n_used_bytes, space_left));

CC: Daniel Trebbien <dtrebbien@gmail.com>
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-29 13:05:24 +01:00
Ruediger Meier 4d751c008e lib: provide fallback if mkostemp(3) missing
It's missing on OSX.

CC: Yuriy M. Kaminskiy <yumkam@gmail.com>
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-29 13:05:18 +01:00
Ruediger Meier eaac9f88f9 misc: fix some includes
features.h:  any glibc header includes this already
libgen.h:    was unused there
sys/uio.h:   for writev(3p)
sys/queue.h  seems like it was never used

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-29 13:04:56 +01:00
Ruediger Meier b0b54cc582 build-sys: always add AM_CFLAGS
We were missing our nice compliler warnings for many programs
and libs. See next commits how many trivial and non-trival
warnings have to be fixed.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-23 01:17:29 +01:00
Ruediger Meier b201f1d86c build-sys: always use global LDADD
This was a major showstopper when building on a system where
LTLIBINTL libs are needed (e.g. OSX). Maybe there are a few test
programs which wouldn't need LDADD ... never mind.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-23 01:16:58 +01:00
Ruediger Meier f643d3349e lib/strutils: parse_size(), sync errno and return value
Maybe strtosize_or_err() is the only function which uses
this errno (wrongly). But it doesnt hurt to maintain rc
as well as errno.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-21 18:10:00 +01:00
Ruediger Meier ca8eff2ade lib/strutils: add more ERANGE messages
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-21 18:09:28 +01:00
Ruediger Meier 73b9e094a8 lib/strutils: fix errno usage in strtoint functions
Don't use undefined errno:
$ ./logger --no-act -t "wtf" --id="" message
logger: failed to parse id: '': No such file or directory

Don't print useless EINVAL message but ERANGE only:
$ ./logger --no-act -t "wtf" --id="XX" message
logger: failed to parse id: 'XX': Invalid argument

Note the ERANGE-only improvement does not change anything on
Linux/c99 as strtoul(3) only set ERANGE there.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-21 18:08:26 +01:00
Ruediger Meier 440d1db7b0 include: fix sys/sysinfo.h
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-18 16:54:38 +01:00
Ruediger Meier ac1c53e451 lib: fix ismounted includes for FreeBSD
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-18 16:54:32 +01:00
Ruediger Meier b9071bf5dc lib: remove unused variable
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-18 16:54:29 +01:00
Ruediger Meier 06c8aec38c lib: define LOGIN_NAME_MAX fallback
LOGIN_NAME_MAX was unused in misc-utils/namei.c since 04a5cb58.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-18 16:54:14 +01:00
Ruediger Meier b0b24b11f8 lib: rename strmode() and setmode()
On BSD they are part of the standard C library.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-18 12:53:48 +01:00
Karel Zak 4683cf3673 lib/color: add more ESC seq.
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-02-17 12:51:15 +01:00
Ruediger Meier 7f79adb342 exec_shell: prevent basename from modifying env
Fix warning:
passing 'const char *' to parameter of type 'char *' discards
qualifiers [-Wincompatible-pointer-types-discards-qualifiers]

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-11 13:40:28 +01:00
Ruediger Meier 4a04d7f3d6 lib/path: ifdef path_set_prefix() like in path.h
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-11 13:40:25 +01:00
Ruediger Meier 8b9cf26aff lib: fix unused parameters and variables
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-11 13:40:22 +01:00
Ruediger Meier 7231fb2a5b misc: fix some printf format strings
Fix the warnings below for OSX clang and add a few more
casts for timeval:

lib/at.c:131:27: warning: format specifies type 'intmax_t' (aka 'long') but the argument has type 'off_t' (aka 'long long') [-Wformat]

                        printf("%16jd bytes ", st.st_size);

                                ~~~~~          ^~~~~~~~~~

lib/strutils.c:522:52: warning: format specifies type 'intmax_t' (aka 'long') but the argument has type 'uint64_t' (aka 'unsigned long long') [-Wformat]

                snprintf(buf, sizeof(buf), "%d%s%jd%s", dec, dp, frac, suffix);

                                                ~~~              ^~~~

lib/sysfs.c:468:42: warning: format specifies type 'uintmax_t' (aka 'unsigned long') but the argument has type 'uint64_t' (aka 'unsigned long long') [-Wformat]

        len = snprintf(buf, sizeof(buf), "%ju", num);

                                          ~~~   ^~~

libuuid/src/gen_uuid.c:316:34: warning: format specifies type 'unsigned long' but the argument has type '__darwin_suseconds_t' (aka 'int') [-Wformat]

                              clock_seq, last.tv_sec, last.tv_usec, adjustment);

                                                      ^~~~~~~~~~~~

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-11 13:39:56 +01:00
Ruediger Meier 948d1f3114 include: add missing includes
lib/pager.c: signal.h ... raise(3p)
lib/sysfs.c: libgen.h ... basename(3p)
lib/exec_shell.c: libgen.h ... basename(3p)

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-11 12:31:04 +01:00
Ruediger Meier 930a566207 libcommon: don't include sysinfo.h
It was added in 929f939e but should have been removed again
in 08ca3e26.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-11 12:31:03 +01:00
Karel Zak d94c519877 libsmartcols: support multi-line cells
The initial implementation has been introduced by SCOLS_FL_WRAP columns,
but this patch clean ups all and makes things more elegant.

Note that use SCOLS_FL_TREE | SCOLS_FL_WRAP for a column is bad idea
and I don't think we need to fix it.

References: https://github.com/karelzak/util-linux/issues/269
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-02-10 15:34:21 +01:00
Ruediger Meier 3fd1f7711e docs: fix typos found by codespell
Using "codespell" from https://github.com/lucasdemarchi/codespell

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-03 15:10:08 +01:00
Karel Zak 57867795aa libsmartcols: support multibyte titles, rename wrap to padding
- let's support multibyte table titles
- use lib/mbalign.c to align the title
- rename title_wrap to title_padding (we already use "wrap" on another
  places for another things)

Signed-off-by: Karel Zak <kzak@redhat.com>
2016-01-22 16:51:59 +01:00
Karel Zak 6426f926ee lib/mbsalign: fix warnings when compile without widechars
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-01-13 10:08:13 +01:00
Boris Egorov 43b4f7ea5f lib/tty: Pass default width to get_terminal_width()
Almost any code calling get_terminal_width() checks returned width for
non-positive values and sets it to some default value (say, 80). So,
let's pass this default value directly to the function.

[kzak@redhat.com: - get_terminal_width() refactoring]

Signed-off-by: Karel Zak <kzak@redhat.com>
2016-01-06 12:22:07 +01:00
Karel Zak 4ba2a6e515 Merge branch 'races' of git://github.com/kerolasa/lelux-utiliteetit 2015-12-02 10:27:43 +01:00
Karel Zak 04a5cb58b6 namei: move icache to lib/
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-11-27 13:56:01 +01:00
Karel Zak 0e756daeb6 lslocks: use stuff from lib/procutils
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-11-25 14:17:22 +01:00
Sami Kerola 8fe1e638fa
lib/blkdev: add open_blkdev_or_file() function
Purpose of this function is to open a path that is potentially pointing to a
block device or file without races.  The function also proper open(3) flags
are used to check the device is not busy, and finally warning is been
printed if a block device happens to be misaligned.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2015-11-22 20:56:05 +00:00
Ming Lei faeef4d2e9 losetup: support list direct io
So that user can see if DIO is set for current loop device.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
2015-11-19 11:27:36 +01:00
Ming Lei 64c3bb3cb7 losetup: support ioctl cmd of LOOP_SET_DIRECT_IO
From v4.4, linux kernel starts to support direct I/O and
AIO to backing file for loop driver, so allow losetup to
enable the feature by using LOOP_SET_DIRECT_IO ioctl cmd.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
2015-11-19 11:27:36 +01:00
Karel Zak 4419ffb9ef lib/sysfs: make sysfs_get_devname()/blkid_devno_to_wholedisk() more robust
Now the function uses result buffer for internal stuff (readlink), so
it requires that the buffer is large enough. This is unexpected as
caller assumes that the buffer has to be large enough for devname only.

References: http://www.spinics.net/lists/util-linux-ng/msg12015.html
Reported-by: Tom Yan <tom.ty89@gmail.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-10-30 11:08:32 +01:00
Karel Zak de8d3863c8 lib/strv: add strv_extendv()
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-10-01 14:43:05 +02:00
Karel Zak 7020de0be8 lib/crc32: don't require to modify GPT header
This patch introduces smart crc32 function that is able to exclude
specified. The advantage is that we does not have to modify GPT header
(set the current in-header crc field to zero) when we count crc32.

This allows to keep GPT header in read-only buffers and simplify code.

Signed-off-by: Karel Zak <kzak@redhat.com>
2015-09-24 09:26:42 +02:00
Karel Zak 32c9ce4baf lib/sysfs: make sysfs_next_subsystem() more robust
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-08-31 10:11:01 +02:00
Andreas Henriksson 74819b5f64 lib/strutil: fix parse_switch resource leak [coverity scan]
Signed-off-by: Andreas Henriksson <andreas@fatal.se>
2015-08-31 10:09:43 +02:00
Karel Zak bde91c85bd chsh, chfn, vipw: fix filenames collision
The utils when compiled WITHOUT libuser then mkostemp()ing
"/etc/%s.XXXXXX" where the filename prefix is argv[0] basename.

An attacker could repeatedly execute the util with modified argv[0]
and after many many attempts mkostemp() may generate suffix which
makes sense. The result maybe temporary file with name like rc.status
ld.so.preload or krb5.keytab, etc.

Note that distros usually use libuser based ch{sh,fn} or stuff from
shadow-utils.

It's probably very minor security bug.

Addresses: CVE-2015-5224
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-08-24 10:05:55 +02:00
Sami Kerola ee24ab6f1e misc: fix redundant assignment and reassignments before use [cppcheck]
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2015-08-08 07:46:39 +01:00
Karel Zak 09db9538d7 lib/blkdev: use 64-bit to count size in bytes
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-08-05 12:40:35 +02:00