Commit Graph

37 Commits

Author SHA1 Message Date
Karel Zak fe4e122ab3 lsmem: check errno after strto..()
Addresses: https://github.com/karelzak/util-linux/issues/1356
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-06-21 15:29:33 +02:00
Karel Zak 4397707e7a lsmem: use ul_path_readf_string() readable for analysers [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-03-25 12:23:21 +01:00
Karel Zak c02980d6f2 lsmem: report inaccessible /sys/devices/system/memory
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1794160
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-07-08 08:51:45 +02:00
Karel Zak bf1d0a4ea0 lsmem: make it without leaks for non-error output
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-07-08 08:48:45 +02:00
Rosen Penev ad296391f9
[clang-tidy] fix wrong *cmp usage
Found with bugprone-suspicious-string-compare

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2020-04-20 13:21:00 -07:00
Karel Zak 2c308875a7 misc: consolidate version printing and close_stdout()
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-04-16 15:14:13 +02:00
Karel Zak e4319a105b lsmem: use new ul_path_* API
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:07:46 +02:00
Sami Kerola fcd4bbff87
lsmem: add --output-all option
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2018-05-03 19:56:20 +01:00
Karel Zak 704f9ba6e6 lsmem: improve JSON output
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-04-05 12:14:46 +02:00
Karel Zak 92368ce179 lsmem: make output more compatible with another utils
We do not use placeholders (e.g. "-") for missing data in libsmartcols
utils, just use empty space in output.

Signed-off-by: Karel Zak <kzak@redhat.com>
2018-04-05 12:02:33 +02:00
Karel Zak 93c2a8b275 lsmem: remove unused variable
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-04-05 11:44:27 +02:00
Gerald Schaefer 49834246f5 lsmem: fix total online/offline memory calculation
lsmem currently calculates the total online/offline memory by iterating
over all lsmem->blocks. Depending on the lsmem options, there may be
only one lsmem->block, because all sysfs memory blocks could be merged
into one. In this case, the calculation is wrong, because the individual
online/offline state of the sysfs memory blocks is not preserved, but
rather lsmem->blocks[0].state is set to the state of the first sysfs
memory block, typically MEMORY_STATE_ONLINE (at least on s390).

This means that "Total offline memory" will always be calculated as 0
in such cases, e.g. when using "lsmem --summary", or any options that
would merge the table output to one line, like "lsmem -o RANGE":

~# lsmem --summary
Memory block size:         1G
Total online memory:      20G
Total offline memory:      0B

Adding the "-a" option shows the real summary, since there is no block
merging going on, and the calculation is therefore correct:

~# lsmem -a --summary
Memory block size:         1G
Total online memory:      16G
Total offline memory:      4G

Fix this by moving the online/offline calculation into the loop that
is iterating over all sysfs memory blocks, instead of iterating over
potentially merged lsmem->blocks.

Reported-by: Alexander Klein <alkl@linux.vnet.ibm.com>
Signed-off-by: Gerald Schaefer <gerald.schaefer@de.ibm.com>
2018-03-20 18:57:07 +01:00
Karel Zak 8e7a0c1622 lsmem: fix memory leak [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-02-20 15:04:51 +01:00
Karel Zak 96cbe362c0 lsmem: make --split optional, follow output by default
Let's keep lsmem backwardly compatible (<=v2.30) and create ranges
according to the output columns by default. This default behavior may
be modified by --split command line option.

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-11-03 10:58:33 +01:00
Karel Zak d12e945dbf lsmem: add --split
Now the way how lsmem lists memory ranges is affected by used output
columns.  It makes it very difficult to use in scripts where you want
to use for example only one column

	ranges=$(lsmem -oRANGE)

and in this case all is merged to the one (or two) huge ranges and all
attributes are ignored. The --split allows to control this behavior

	ranges=$(lsmem -oRANGE --split=STATE,ZONES)

forces lsmem to list ranges by STATE and ZONES differences.

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-10-20 12:37:04 +02:00
Karel Zak 9c41d2701f lsmem: improve path_read_xxx() usage
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-10-20 12:37:04 +02:00
Gerald Schaefer 60a7e9e94e lsmem/chmem: add memory zone awareness
With this patch, valid memory zones can be shown with lsmem, and chmem can
set memory online/offline in a specific memory zone, if allowed by the
kernel. The valid memory zones are read from the "valid_zones" sysfs
attribute, and setting memory online to a specific zone is done by
echoing "online_kernel" or "online_movable" to the "state" sysfs
attribute, in addition to the previous "online".

This patch also changes the default behavior of chmem, when setting memory
online without specifying a memory zone. If valid, memory will be set
online to the zone Movable. This zone is preferable for memory hotplug, as
it makes memory offline much more likely to succeed.

Signed-off-by: Gerald Schaefer <gerald.schaefer@de.ibm.com>
2017-10-20 12:37:04 +02:00
Ruediger Meier f45f3ec34a misc: consolidate macro style USAGE_HELP_OPTIONS
changed in include/c.h and applied via sed:

  sed -i 's/fprintf.*\(USAGE_MAN_TAIL.*\)/printf(\1/' $(git ls-files -- "*.c")
  sed -i 's/print_usage_help_options\(.*\);/printf(USAGE_HELP_OPTIONS\1);/' $(git ls-files -- "*.c")

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-29 16:54:33 +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 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 b1a294c448 misc: introduce print_usage_help_options()
Consolidate --help and --version descriptions. We are
now able to align them to the other options.

We changed include/c.h. The rest of this patch was
generated by sed, plus manually setting the right
alignment numbers. We do not change anything but
white spaces in the --help output.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-27 12:26:19 +02:00
Ruediger Meier 6e1eda6f22 misc: never use usage(stderr)
Here we fix all cases where we have usage(FILE*)
functions.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-26 14:38:24 +02:00
J William Piggott 6e2d5a4460 include/c.h: add USAGE_COMMANDS and USAGE_COLUMNS
* login-utils/lslogins.c: all uses changed
* misc-utils/findmnt.c: likewise
* sys-utils/blkzone.c: likewise
* disk-utils/sfdisk.c: likewise
* sys-utils/lscpu.c: likewise
* sys-utils/lsmem.c: likewise
* sys-utils/wdctl.c: likewise

Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-06-24 13:57:12 -04:00
Yuri Chornoivan 0508f347c8 Fix minor typos 2017-05-24 00:22:20 +03: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
Karel Zak 677ec86cef Use --help suggestion on invalid option
The current default is to print all usage() output. This is overkill
in many case.

Addresses: https://github.com/karelzak/util-linux/issues/338
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-12-19 13:13:34 +01:00
Heiko Carstens 51a90159dd lsmem: improve node lookup
Break the loop as soon as we found the node a memory block belongs to,
it doesn't make sense to continue scanning.

Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
2016-11-09 10:02:32 +01:00
Karel Zak 4c0e1eaf34 lsmem: align STATE to the right
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-11-09 10:02:32 +01:00
Karel Zak a5c4535b0d lsmem: add Copyright
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-11-09 10:02:32 +01:00
Karel Zak 3845c7302d lsmem: add missing placeholders
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-11-09 10:02:32 +01:00
Karel Zak d4625442c7 lsmem: cleanup for --summary=only
* don't fill scols table
* check for collision with raw,pairs and json options

Signed-off-by: Karel Zak <kzak@redhat.com>
2016-11-09 10:02:32 +01:00
Karel Zak 4775cc69a7 lsmem: cleanup man page
* add missing new options
* remove columns description (for ls-like utils we maintains columns
  description only in the --help output)

Signed-off-by: Karel Zak <kzak@redhat.com>
2016-11-09 10:02:32 +01:00
Karel Zak 019ad6c1c2 lsmem: disable summary for JSONm, raw and export output
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-11-09 10:02:32 +01:00
Karel Zak c5332fbbcd lsmem: add --summary[=<when>] option
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-11-09 10:02:32 +01:00
Karel Zak 0cf0710aed lsmem: rename control struct
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-11-09 10:02:32 +01:00
Karel Zak 0a1ed6dffb lsmem: cleanup, use libsmartcols for all output
* add --pairs, --raw a --json outputs
* add --noheadings to disable header
* add --bytes
* add --output <list>

Signed-off-by: Karel Zak <kzak@redhat.com>
2016-11-09 10:02:32 +01:00
Heiko Carstens cad2d1ac92 lsmem: new tool
Move the s390 specific lsmem tool to util-linux.

The lsmem tool was originally written in perl and is part of the
s390-tools package which can be found here:
https://www.ibm.com/developerworks/linux/linux390/s390-tools.html

Given that the tool is architecture independent, there is no reason to
keep it in an s390 specific repository. It seems to be useful for
other architectures as well.

This patch converts the tool to C and adds it to util-linux, while the
command line options stay compatible. The only exception is the option
"-v" which used to be the short form of "--version". That got changed
to "-V" so it behaves like most other tools contained within
util-linux.

The lsmem tool inspect the contents of /sys/devices/system/memory and
prints a summary output similar to what lscpu does:

RANGE                                 SIZE STATE   REMOVABLE BLOCK
0x0000000000000000-0x000000005fffffff 1,5G online  yes       0-5
0x0000000060000000-0x000000007fffffff 512M online  no        6-7
0x0000000080000000-0x000000013fffffff   3G online  yes       8-19
0x0000000140000000-0x000000014fffffff 256M offline -         20
0x0000000150000000-0x000000017fffffff 768M online  no        21-23

Memory block size   :     256M
Total online memory :     5,8G
Total offline memory:     256M

In order to keep the output small the tool merges subsequent address
ranges where the attributes are identical. To avoid merging of line
the "-a" option can be used.

The lsmem tool also has "--extendend" and "--parsable" option which
can be used to customize the output, e.g.  limit the output to
specified columns. This is quite similar to what the lscpu tool does.

This is 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