Commit Graph

91 Commits

Author SHA1 Message Date
Karel Zak cfc0d463b4 swapon: do not use atoi()
Addresses: https://github.com/karelzak/util-linux/issues/1358
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-06-22 17:04:24 +02:00
Karel Zak 17fc8693cd include/c: add drop_permissions(), consolidate UID/GID reset
Fixes: https://github.com/karelzak/util-linux/issues/1354
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-06-21 12:25:31 +02:00
Sebastian Rasmussen 5fba3d7b4e swapon: Keep headings and fields aligned in summary output.
Because the headings are aligned with tabs the fields must
always be a multiple of 8 characters. Moreover if the field
values are shorter than 8 characters, extra tabs must be
inserted before the succeding field to keep alignment.

swapon parses /proc/swaps, generated by the Linux kernel in
mm/swapfile.c. Its function swap_show() and its recent fix in
commit 6f7939405f61de7d0da7f6c90182e96c4f5ff6c1 were used as
inspiration for this commit.

Additionally inform the translators about the requirements of
formatting and relationship between the heading and entry strings.
2021-04-18 03:48:57 +02:00
Rosen Penev bd89499e07
[clang-tidy] do not return in void functions
Found with readability-redundant-control-flow

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2020-04-19 14:03:21 -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 58bc493243 swapon: fix compiler warnings [-Wcast-qual]
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-07-23 11:43:47 +02:00
Sami Kerola b5b43f276e
swapon: add --output-all option
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2018-05-03 19:56:21 +01:00
Karel Zak fd777151bf sys-utils: use errexec()
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-02-01 15:44:25 +01:00
Karel Zak cf9b16f1d4 build-sys: don't check for swapon() number of arguments
The syscall swapon() with two arguments is supported since Linux 1.3.2
and it's really long time ago... Let's assume that all libc header files
have been already fixed.

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-12-18 13:57:20 +01:00
Karel Zak 9f20d80079 misc: cleanup UUID_STR_LEN definitions
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-09-15 13:25:32 +02:00
Philip Prindeville b443c1779e misc: replace magic number 37 with UUID_STR_LEN
Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
2017-09-05 11:49:21 +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 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
Karel Zak c3a4cfc579 misc: consolidate usage() "Available columns"
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-06-26 14:09:53 +02:00
Sami Kerola 1b5042636f misc: fix reassigned values before old ones has been used [cppcheck]
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2017-06-14 12:19:20 +02:00
Karel Zak 780ce22cda misc: consolidate smartcols error messages
... just to keep translators happy

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-05-18 11:39:34 +02: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
Yuri Chornoivan a7349ee315 docs: Fix word repetitions 2017-02-13 14:10:12 +01: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
Karel Zak 07332bfa1e swapon: fix discard option parsing
The current code does not work as expected if there is an option
behind the discard=<arg>, for example:

  swapon /dev/sdc -o discard=once,pri=10

ignores "once" the result is SWAP_FLAG_DISCARD; strace:

Old version:

  swapon("/dev/sdc", SWAP_FLAG_PREFER|SWAP_FLAG_DISCARD|10) = 0

Fixed version:

  swapon("/dev/sdc", SWAP_FLAG_PREFER|SWAP_FLAG_DISCARD|SWAP_FLAG_DISCARD_ONCE|10) = 0

Signed-off-by: Karel Zak <kzak@redhat.com>
2016-09-21 15:08:50 +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 6c44094af6 swapon: rewrite control struct usage
The old version has been pretty broken... the most important is to
keep swap options specified on command line as read-only template.
For example if we call "swapon --all" then we cannot modify the global
options for each fstab swap entry.

The another story has been control struct modification due to device
reinitialization etc.

This patch splits all to:

* struct swapon_control; top-level struct with command line options

* struct swap_device; this is device specific and never globally
  maintained by swapon_control.

* struct swap_prop; used as global read-only template swap options
  and per device swap options (when parse fstab).

Addresses: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=818252
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-03-18 14:28:29 +01:00
Karel Zak 12f5cbe159 swapon: remove unnecessary assert
Addresses: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=818252
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-03-15 11:38:01 +01:00
Karel Zak eac478dcfd swapon: drop perms if setuid before call mkswap
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-02-11 12:07:34 +01:00
Karel Zak a125123f8b swapon: simplify ctl->device usage
* use and initialize ctl->{label,uuid,device} by do_swapon() only
* remove ctl->canonic, function arguments are good enough for this

Signed-off-by: Karel Zak <kzak@redhat.com>
2016-02-11 11:39:16 +01:00
Sami Kerola 8843aa41a7 swapon: do not run execvp() calls when swapon is setuid binary
swapon(8) is not expected to be setuid binary, but if it is try to avoid
obvious security vulnerability of executing user preferred mkswap file as
someone else, such as root.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2016-02-11 10:52:18 +01:00
Sami Kerola 2dd3ba2c85 swapon: move function arguments to control structure
This makes reading what the code does easier.  This change also makes error
messages to prefer none-canonical device path, e.g., the one user supplied
rather than the canonical path needed internally.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2016-02-11 10:52:18 +01:00
Sami Kerola 9eeba70eed swapon: add control struct
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2016-02-11 10:52:18 +01:00
Sami Kerola 8df370d340 swapon: fix stat(3) and open(3) race
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2015-11-09 09:56:14 +01:00
Karel Zak 048811c97a swapon: fix restart waitpid with errno == EINTR
Reported-by: Maks Naumov (https://github.com/karelzak/util-linux/pull/205)
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-07-01 15:35:17 +02:00
Mike Frysinger f1bc59963f swapon: search for mkswap via PATH
Rather than hardcode /sbin/mkswap all the time, use a normal PATH search.
This matches the normal behavior of other tools, and makes local testing
easier.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
2015-04-27 10:28:44 +02:00
Benno Schulenberg fc14ceba5e textual: grammarize and harmonize the stat error message
The message "stat failed %s" seems to say that stat() failed to
do something, or failed to pass a test, but of course it means
that the statting of something failed.  So say so.  Also make
two very similar messages equal to this one.

Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2015-02-02 11:27:10 +01:00
Benno Schulenberg 54fefa078e textual: fix spellos and inconsistencies in several program messages
Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2015-01-26 11:17:55 +01:00
Sami Kerola 7ee26cbf02 maint: fix shadow declaration
This change fixes all shadow declarations.  The worth while to mention
fix is with libfdisk sun geometry.  It comes from bitops.h cpu_to_be16
macro that further expands from include/bits/byteswap.h that has the
shadowing.

libfdisk/src/sun.c:961:173: warning: declaration of '__v' shadows a previous local [-Wshadow]
libfdisk/src/sun.c:961:69: warning: shadowed declaration is here [-Wshadow]
libfdisk/src/sun.c:961:178: warning: declaration of '__x' shadows a previous local [-Wshadow]
libfdisk/src/sun.c:961:74: warning: shadowed declaration is here [-Wshadow]

That could have caused earlier some unexpected results.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2015-01-07 10:08:21 +01:00
Benno Schulenberg 451dbcfae1 textual: add a docstring to most of the utilities
This adds a concise description of a tool to its usage text.

A first form of this patch was proposed by Steven Honeyman
(see http://www.spinics.net/lists/util-linux-ng/msg09994.html).

Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2015-01-06 11:27:38 +01:00
Benno Schulenberg f4cae17663 swapon: reword some usage strings
Also remove some inconsistent periods and properly punctuate
the closing sentence.

Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2014-11-07 13:21:06 +01:00
Karel Zak 54f2e4563d swapon: remove extra word from usage
Reported-by: Benno Schulenberg <bensberg@justemail.net>
Signed-off-by: Karel Zak <kzak@redhat.com>
2014-10-31 10:58:27 +01:00
Benno Schulenberg 6ee8e19489 textual: slice up the usage text of swapon for ease of translation
A new option was added recently; seize this opportunity to cut the
usage text into small and easily managed chunks.

Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2014-10-31 10:39:11 +01:00
Karel Zak e07fdac476 swapon: add -o <list> for compatibility with mount
The new option allows to specify swap options by fstab compatible
string. The concept is the same as for mount(8).

   swapon -o pri=1,discard=pages,nofail /dev/sda2

The advantage is that tools (like systmed) that parses fstab can call
swapon without translation from fstab options to swapon(8) command
line options.

Signed-off-by: Karel Zak <kzak@redhat.com>
2014-10-20 10:59:08 +02:00
Sami Kerola a12124ce7a swapon: align available columns section in usage()
Commit 5db57cfc46 missed doing this change.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-10-01 12:33:10 +02:00
Karel Zak 18b3e5495b swapon: split swapon-common.c
swapon    - requires libmount and libblkid
 swapoff   - requires libmount
 swaplabel - requires libblkid

This patch add lib/swapprober.c with blkid stuff for swap. It allows
to use and link libblkid only when necessary.

Signed-off-by: Karel Zak <kzak@redhat.com>
2014-09-22 13:16:25 +02:00
Sami Kerola 3077ff49f3 swapon: remove invalid error string print outs
To avoid messages like this:

$ swapon /dev/zram0
swapon: /dev/zram0: read swap header failed: Success

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-08-11 14:51:56 +02:00
Karel Zak a3c74d88e1 swapon: don't print errors as data in smartcols table
Signed-off-by: Karel Zak <kzak@redhat.com>
2014-07-22 12:29:38 +02:00
Karel Zak 7c1fa4691f swapon: don't use empty strings for smartcols output
The libsmartcols is smart enough to accept NULL for empty fields
rather than empty strings.

Signed-off-by: Karel Zak <kzak@redhat.com>
2014-07-22 12:24:56 +02:00
Sami Kerola 5db57cfc46 swapon: share get_swap_prober() with swaplabel to print uuid and label
The swapon(8) listing was almost complete, apart from label and uuid.
This change moves the code from swaplabel(8) to shared scope to be used
for printouts in other swap commands, such as swapon.

Adding this feature to lsblk(8) was a consideration, but lsblk is not
interested of swapfiles, so the swapon seems like a better option to add
this information.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-07-22 12:17:20 +02:00
Karel Zak a560a3487b swapon: clean up columns[] usage
Signed-off-by: Karel Zak <kzak@redhat.com>
2014-07-18 13:53:02 +02:00
Karel Zak a6c6662d7c swapon: mark --sumarry as deprecated
Signed-off-by: Karel Zak <kzak@redhat.com>
2014-06-30 11:03:12 +02:00
Sami Kerola 7471244148 swapon: align --summary fields
This commit makes  partitions and files to have remaining three columns
aligned.  Below print out demonstrates earlier misalignment.

$ swapon -s
Filename                                Type            Size    Used    Priority
/dev/sda2                               partition       4194300 0       3
/home/src/util-linux/newswap            file    496     0       -1

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-06-30 10:43:04 +02:00