text-utils: add bits command
Subsystems (2)
RFC PATCH 1/1 text-utils: add bits command
Add a new test utility to convert between bit masks in various formats. This can be handy to avoid parsing affinity masks in one's head and/or to interact with the kernel in a more human friendly way.
This is a rewrite in C of the bits command from my linux-tools python package so that it can be more widely available.
Here is an example:
~# cat /sys/kernel/debug/tracing/tracing_cpumask fffffff,ffffffff,ffffffff,ffffffff
~# bits -l ,$(cat /sys/kernel/debug/tracing/tracing_cpumask) 0-128
~# bits -g 58,59,120,123 9000000,00000000,0c000000,00000000
~# bits -g 58,59 > /sys/kernel/debug/tracing/tracing_cpumask ~# echo 1 > /sys/kernel/debug/tracing/tracing_on
Add man page and basic tests.
--- bash-completion/bits | 21 ++ meson.build | 11 ++ tests/commands.sh | 1 + tests/expected/misc/bits | 0 tests/expected/misc/bits-and | 1 + tests/expected/misc/bits-binary | 1 + tests/expected/misc/bits-default | 1 + tests/expected/misc/bits-grouped-mask | 1 + tests/expected/misc/bits-list | 1 + tests/expected/misc/bits-mask | 1 + tests/expected/misc/bits-not | 1 + tests/expected/misc/bits-or | 1 + tests/expected/misc/bits-overflow | 1 + tests/expected/misc/bits-parse-mask | 1 + tests/expected/misc/bits-parse-range | 1 + tests/expected/misc/bits-stdin | 1 + tests/expected/misc/bits-xor | 1 + tests/ts/misc/bits | 82 ++++++++ text-utils/Makemodule.am | 6 + text-utils/bits.1.adoc | 147 ++++++++++++++ text-utils/bits.c | 265 ++++++++++++++++++++++++++ text-utils/meson.build | 4 + 22 files changed, 550 insertions(+) create mode 100644 bash-completion/bits create mode 100644 tests/expected/misc/bits create mode 100644 tests/expected/misc/bits-and create mode 100644 tests/expected/misc/bits-binary create mode 100644 tests/expected/misc/bits-default create mode 100644 tests/expected/misc/bits-grouped-mask create mode 100644 tests/expected/misc/bits-list create mode 100644 tests/expected/misc/bits-mask create mode 100644 tests/expected/misc/bits-not create mode 100644 tests/expected/misc/bits-or create mode 100644 tests/expected/misc/bits-overflow create mode 100644 tests/expected/misc/bits-parse-mask create mode 100644 tests/expected/misc/bits-parse-range create mode 100644 tests/expected/misc/bits-stdin create mode 100644 tests/expected/misc/bits-xor create mode 100755 tests/ts/misc/bits create mode 100644 text-utils/bits.1.adoc create mode 100644 text-utils/bits.c
bash-completion/bits new file +21 -0
@@ -0,0 +1,21 @@ +_bits_module() +{ + local cur prev OPTS + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + case $prev in + '-h'|'--help'|'-V'|'--version') + return 0 + ;; + esac + case $cur in + -*) + OPTS="--version --help --mask --grouped-mask --bit --list" + COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) ) + return 0 + ;; + esac + return 0 +} +complete -F _bits_module bits
meson.build +11 -0
@@ -1201,6 +1201,17 @@ endif ############################################################ +exe = executable( + 'bits', + bits_sources, + include_directories : includes, + link_with : lib_common, + install_dir : usrbin_exec_dir, + install : true) +exes += exe +manadocs += ['text-utils/bits.1.adoc'] +bashcompletions += ['bits'] + if is_glibc exe = executable( 'col',
tests/commands.sh +1 -0
@@ -64,6 +64,7 @@ TS_HELPER_TIMEUTILS="${ts_helpersdir}test_timeutils" # paths to commands TS_CMD_ADDPART=${TS_CMD_ADDPART:-"${ts_commandsdir}addpart"} TS_CMD_DELPART=${TS_CMD_DELPART:-"${ts_commandsdir}delpart"} +TS_CMD_BITS=${TS_CMD_BITS-"${ts_commandsdir}bits"} TS_CMD_BLKDISCARD=${TS_CMD_BLKID-"${ts_commandsdir}blkdiscard"} TS_CMD_BLKID=${TS_CMD_BLKID-"${ts_commandsdir}blkid"} TS_CMD_CAL=${TS_CMD_CAL-"${ts_commandsdir}cal"}
tests/expected/misc/bits new file +0 -0
tests/expected/misc/bits-and new file +1 -0
@@ -0,0 +1 @@ +75-100
tests/expected/misc/bits-binary new file +1 -0
@@ -0,0 +1 @@ +0b1_0000_0000_0010_0000_0000_0100_0000_0000_1000_0000_0000
tests/expected/misc/bits-default new file +1 -0
@@ -0,0 +1 @@ +0x100200400800
tests/expected/misc/bits-grouped-mask new file +1 -0
@@ -0,0 +1 @@ +1002,00400800
tests/expected/misc/bits-list new file +1 -0
@@ -0,0 +1 @@ +11,22,33,44
tests/expected/misc/bits-mask new file +1 -0
@@ -0,0 +1 @@ +0x100200400800
tests/expected/misc/bits-not new file +1 -0
@@ -0,0 +1 @@ +50-74
tests/expected/misc/bits-or new file +1 -0
@@ -0,0 +1 @@ +50-150
tests/expected/misc/bits-overflow new file +1 -0
@@ -0,0 +1 @@ +0x0
tests/expected/misc/bits-parse-mask new file +1 -0
@@ -0,0 +1 @@ +1,3,6,7,9,11,14-16,18,19,21,23-25,27
tests/expected/misc/bits-parse-range new file +1 -0
@@ -0,0 +1 @@ +7fffff,ffffffff,ffffffff,fffc0000,00000000
tests/expected/misc/bits-stdin new file +1 -0
@@ -0,0 +1 @@ +11,33,44
tests/expected/misc/bits-xor new file +1 -0
@@ -0,0 +1 @@ +50-74,101-150
tests/ts/misc/bits new file +82 -0
82 lines not on this page · raw message
text-utils/Makemodule.am +6 -0
@@ -106,3 +106,9 @@ test_more_LDADD = $(more_LDADD) endif # BUILD_MORE +if BUILD_BITS +usrbin_exec_PROGRAMS += bits +MANPAGES += text-utils/bits.1 +dist_noinst_DATA += text-utils/bits.1.adoc +bits_SOURCES = text-utils/bits.c +endif
text-utils/bits.1.adoc new file +147 -0
147 lines not on this page · raw message
text-utils/bits.c new file +265 -0
265 lines not on this page · raw message
text-utils/meson.build +4 -0
@@ -1,3 +1,7 @@ +bits_sources = files( + 'bits.c', +) + col_sources = files( 'col.c', )
Signature · 1 line
2.47.0
Quoted 1 line · Robin Jarry
Add a new test utility to convert between bit masks in various formats.
"test" or "text" utility?
Quoted 63 lines
This can be handy to avoid parsing affinity masks in one's head and/or to interact with the kernel in a more human friendly way.
This is a rewrite in C of the bits command from my linux-tools python package so that it can be more widely available.
Here is an example:
~# cat
/sys/kernel/debug/tracing/tracing_cpumaskfffffff,ffffffff,ffffffff,ffffffff
~# bits -l ,$(cat/sys/kernel/debug/tracing/tracing_cpumask) 0-128
~# bits -g 58,59,120,123 9000000,00000000,0c000000,00000000
~# bits -g 58,59 >/sys/kernel/debug/tracing/tracing_cpumask~# echo 1 >/sys/kernel/debug/tracing/tracing_onAdd man page and basic tests.
Signed-off-by Robin Jarry <[email protected]>--- bash-completion/bits | 21 ++ meson.build | 11 ++ tests/commands.sh | 1 + tests/expected/misc/bits | 0 tests/expected/misc/bits-and | 1 + tests/expected/misc/bits-binary | 1 + tests/expected/misc/bits-default | 1 + tests/expected/misc/bits-grouped-mask | 1 + tests/expected/misc/bits-list | 1 + tests/expected/misc/bits-mask | 1 + tests/expected/misc/bits-not | 1 + tests/expected/misc/bits-or | 1 + tests/expected/misc/bits-overflow | 1 + tests/expected/misc/bits-parse-mask | 1 + tests/expected/misc/bits-parse-range | 1 + tests/expected/misc/bits-stdin | 1 + tests/expected/misc/bits-xor | 1 + tests/ts/misc/bits | 82 ++++++++ text-utils/Makemodule.am | 6 + text-utils/bits.1.adoc | 147 ++++++++++++++ text-utils/bits.c | 265 ++++++++++++++++++++++++++ text-utils/meson.build | 4 + 22 files changed, 550 insertions(+) create mode 100644 bash-completion/bits create mode 100644 tests/expected/misc/bits create mode 100644 tests/expected/misc/bits-and create mode 100644 tests/expected/misc/bits-binary create mode 100644 tests/expected/misc/bits-default create mode 100644 tests/expected/misc/bits-grouped-mask create mode 100644 tests/expected/misc/bits-list create mode 100644 tests/expected/misc/bits-mask create mode 100644 tests/expected/misc/bits-not create mode 100644 tests/expected/misc/bits-or create mode 100644 tests/expected/misc/bits-overflow create mode 100644 tests/expected/misc/bits-parse-mask create mode 100644 tests/expected/misc/bits-parse-range create mode 100644 tests/expected/misc/bits-stdin create mode 100644 tests/expected/misc/bits-xor create mode 100755 tests/ts/misc/bits create mode 100644 text-utils/bits.1.adoc create mode 100644 text-utils/bits.c
...
Quoted 8 lines
+++ b/tests/ts/misc/bits
tests/ts/misc/bits new file +6
-0@@ -0,0 +1,82 @@ +#!/bin/bash + +# +# Copyright (c) 2024 Robin Jarry +# +# This file is part of util-linux.
tests/ts/misc/bits line 16
Quoted 10 lines
tests/ts/misc/bits new file +10
-0+# +# This file is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This file is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details.
New files should use SPDX tags.
Quoted 31 lines
text-utils/bits.c new file +25
-0@@ -0,0 +1,265 @@ +/* +* SPDX-License-Identifier: GPL-2.0-or-later +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* Copyright (c) 2024 Robin Jarry +* +* bits - convert bit masks from/to various formats +*/ + +#include <errno.h> +#include <getopt.h> +#include <sched.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include "c.h" +#include "closestream.h" +#include "cpuset.h"
text-utils/bits.c line 35
Quoted 10 lines
text-utils/bits.c new file +10
-0+#include "nls.h" +#include "strutils.h" +#include "strv.h" + +typedef enum { +OP_OR , +OP_AND , +OP_NOT , +OP_XOR , +}bitvise_op_t ;
"bitwise"?
Also avoid using the _t suffix as it's reserved.
"enum bitwise_op" seems clear enough.
text-utils/bits.c line 40
Quoted 5 lines
text-utils/bits.c new file +5
-0+ +static int parse_mask_or_list (const char *cmdline_arg,cpu_set_t *all_bits) +{ +bitvise_op_t op =OP_OR ; +cpu_set_t bits, copy;
This seems very geared towards cpu masks.
What happens if a user wants to use it with very large non-CPU bitmasks, as the tool is advertised as a generic utility?
Call the utility "cpumask"?
text-utils/bits.c line 41
Quoted 1 line
text-utils/bits.c new file +1
-0+char buf[BUFSIZ ];
Only use BUFSIZ in cases where the actual value does not matter, for example loops.
On musl BUFSIZ == 1024 and this could not be enough fairly soon.
text-utils/bits.c line 46
Quoted 5 lines
text-utils/bits.c new file +5
-0+char *arg = buf; + +/* copy to allow modifying the argument contents */ +strlcpy (buf, cmdline_arg,sizeof (buf)); + buf[sizeof (buf) -1 ] ='\0' ;
You can just use xstrdup().
Quoted 5 lines
text-utils/bits.c new file +5
-0+ +/* strip optional operator first */ +if (startswith (arg,"&" )) { + op =OP_AND ; + arg++;
text-utils/bits.c line 61
Quoted 10 lines
text-utils/bits.c new file +10
-0+ }else if (startswith (arg,"^" )) { + op =OP_XOR ; + arg++; + }else if (startswith (arg,"~" )) { + op =OP_NOT ; + arg++; + }else if (startswith (arg,"|" )) { + op =OP_OR ; + arg++; + }
The whole op enum could also be replaced by the actual character, this is all in a single function.
op = '&';
...
switch (op) {
case '&':
}
text-utils/bits.c line 65
Quoted 4 lines
text-utils/bits.c new file +4
-0+ +if (startswith (arg,"," ) ||startswith (arg,"0x" )) { +if (cpumask_parse (arg, &bits,sizeof (bits)) <0 ) +return -EINVAL ;
As this is a commandline tool you can call errx() here to kill the process and provide better error information.
It would also make the call sequence slightly more simple.
Quoted 33 lines
text-utils/bits.c new file +33
-0+ }else { +if (cpulist_parse (arg, &bits,sizeof (bits),1 ) <0 ) +return -EINVAL ; + } + +switch (op) { +case OP_AND : + copy = *all_bits; +CPU_AND (all_bits, ©, &bits); +break ; +case OP_OR : + copy = *all_bits; +CPU_OR (all_bits, ©, &bits); +break ; +case OP_XOR : + copy = *all_bits; +CPU_XOR (all_bits, ©, &bits); +break ; +case OP_NOT : +for (int i =0 ; i <CPU_SETSIZE ; i++) { +if (CPU_ISSET (i, &bits)) +CPU_CLR (i, all_bits); + } +break ; + } + +return 0 ; +} + +typedef enum { + MODE_BINARY, + MODE_GROUPED_MASK, + MODE_LIST,
text-utils/bits.c line 108
Quoted 10 lines
text-utils/bits.c new file +10
-0+MODE_MASK , +} output_mode_t; + +static void print_bits (cpu_set_t *bits,output_mode_t mode) +{ +bool started = false; +char buf[BUFSIZ ]; +ssize_t n =0 ; + + buf[0 ] ='\0' ;
Is this necessary?
Quoted 39 lines
text-utils/bits.c new file +39
-0+ +switch (mode) { +case MODE_MASK : +cpumask_create (buf,sizeof (buf), bits,sizeof (*bits)); + +/* strip leading zeroes */ +while (buf[n] =='0' ) + n++; +if (buf[n] =='\0' ) +printf ("0x0\n" ); +else +printf ("0x%s\n" , buf + n); +break ; + +case MODE_GROUPED_MASK : +cpumask_create (buf,sizeof (buf), bits,sizeof (*bits)); + +/* strip leading zeroes */ +while (buf[n] =='0' ) + n++; + +while (buf[n] !='\0' ) { +if (started && (n %8 ) ==0 ) +printf ("," ); +if (buf[n] !='0' ) + started = true; +printf ("%c" , buf[n]); + n++; + } +printf ("\n" ); +break ; + +case MODE_BINARY : +printf ("0b" ); +for (n =CPU_SETSIZE ; n >=0 ; n--) { +if (started && ((n +1 ) %4 ) ==0 ) +printf ("_" ); +if (CPU_ISSET (n, bits)) { + started = true;
text-utils/bits.c line 157
Quoted 10 lines
text-utils/bits.c new file +10
-0+printf ("1" ); + }else if (started) { +printf ("0" ); + } + } +printf ("\n" ); +break ; + +case MODE_LIST : +cpulist_create (buf,sizeof (buf), bits,sizeof (*bits));
This could use the return values of cpulist_create() and cpumask_create().
Quoted 77 lines
text-utils/bits.c new file +77
-0+printf ("%s\n" , buf); +break ; + } + +} + +static void __attribute__((__noreturn__))usage (void ) +{ +fputs (USAGE_HEADER , stdout); +fprintf (stdout,_ (" %s [options] [<mask_or_list>...]\n" ), program_invocation_short_name); + +fputs (USAGE_SEPARATOR , stdout); +fputsln (_ ("Convert bit masks from/to various formats." ), stdout); + +fputs (USAGE_ARGUMENTS , stdout); +fputsln (_ (" <mask_or_list> A set of bits specified as a hex mask value (e.g. 0xeec2)\n" +" or as a comma-separated list of bit IDs.\n" +" If not specified, arguments will be read from stdin." ), stdout); + +fputs (USAGE_OPTIONS , stdout); +fprintf (stdout,USAGE_HELP_OPTIONS (21 )); + +fputs (_ ("\nMode:\n" ), stdout); +fputsln (_ (" -m, --mask Print the combined args as a hex mask value (default).\n" ), stdout); +fputsln (_ (" -g, --grouped-mask Print the combined args as a hex mask value in 32bit\n" +" comma separated groups." ), stdout); +fputsln (_ (" -b, --binary Print the combined args as a binary mask value." ), stdout); +fputsln (_ (" -l, --list Print the combined args as a compressed list of bit IDs." ), stdout); + +fputs (USAGE_SEPARATOR , stdout); +fprintf (stdout,USAGE_MAN_TAIL ("bits(1)" )); +exit (EXIT_SUCCESS ); +} + +int main (int argc,char **argv) +{ +output_mode_t mode =MODE_MASK ; +char **stdin_lines =NULL ; +cpu_set_t bits; +int c; + +static const struct option longopts[] = { + {"version" , no_argument,NULL ,'V' }, + {"help" , no_argument,NULL ,'h' }, + {"mask" , no_argument,NULL ,'m' }, + {"grouped-mask" , no_argument,NULL ,'g' }, + {"binary" , no_argument,NULL ,'b' }, + {"list" , no_argument,NULL ,'l' }, + {NULL ,0 ,NULL ,0 } + }; + +setlocale (LC_ALL ,"" ); +bindtextdomain (PACKAGE ,LOCALEDIR ); +textdomain (PACKAGE ); +close_stdout_atexit (); + +while ((c =getopt_long (argc, argv,"Vhmgbl" , longopts,NULL )) !=-1 ) +switch (c) { +case 'm' : + mode =MODE_MASK ; +break ; +case 'g' : + mode =MODE_GROUPED_MASK ; +break ; +case 'b' : + mode =MODE_BINARY ; +break ; +case 'l' : + mode =MODE_LIST ; +break ; +case 'V' : +print_version (EXIT_SUCCESS ); +case 'h' : +usage (); +default : +errtryhelp (EXIT_FAILURE ); + }
text-utils/bits.c line 244
Quoted 10 lines
text-utils/bits.c new file +10
-0+ + argc -= optind; + argv += optind; +if (argc ==0 ) { +/* no arguments provided, read lines from stdin */ +char buf[BUFSIZ ]; + +while (fgets (buf,sizeof (buf), stdin)) { +/* strip LF, CR, CRLF, LFCR */ + buf[strcspn (buf,"\r\n" )] ='\0' ;
rtrim_whitespace()?
Quoted 6 lines
text-utils/bits.c new file +6
-0+strv_push (&stdin_lines,strdup (buf)); + } + + argc =strv_length (stdin_lines); + argv = stdin_lines; + }
text-utils/bits.c line 260
Quoted 10 lines
text-utils/bits.c new file +10
-0+ +CPU_ZERO (&bits); + +for (; argc >0 ; argc--, argv++) +if (parse_mask_or_list (*argv, &bits) <0 ) { +fprintf (stderr,_ ("error: invalid argument: %s\n" ), *argv); +errtryhelp (EXIT_FAILURE ); + } + +strv_free (stdin_lines);
No need to free this here. The process will exit anyways.
Quoted 5 lines
text-utils/bits.c new file +5
-0+ +print_bits (&bits, mode); + +return EXIT_SUCCESS ; +}
Hi Thomas,
Thomas Weißschuh, Oct 16, 2024 at 23:00:
Quoted 4 lines · Robin Jarry
Quoted 1 line
Add a new test utility to convert between bit masks in various formats.
"test" or "text" utility?
That was a typo, I meant "text" :)
Quoted 78 lines
Quoted 63 lines
This can be handy to avoid parsing affinity masks in one's head and/or to interact with the kernel in a more human friendly way.
This is a rewrite in C of the bits command from my linux-tools python package so that it can be more widely available.
Here is an example:
~# cat
/sys/kernel/debug/tracing/tracing_cpumaskfffffff,ffffffff,ffffffff,ffffffff
~# bits -l ,$(cat/sys/kernel/debug/tracing/tracing_cpumask) 0-128
~# bits -g 58,59,120,123 9000000,00000000,0c000000,00000000
~# bits -g 58,59 >/sys/kernel/debug/tracing/tracing_cpumask~# echo 1 >/sys/kernel/debug/tracing/tracing_onAdd man page and basic tests.
Signed-off-by Robin Jarry <[email protected]>--- bash-completion/bits | 21 ++ meson.build | 11 ++ tests/commands.sh | 1 + tests/expected/misc/bits | 0 tests/expected/misc/bits-and | 1 + tests/expected/misc/bits-binary | 1 + tests/expected/misc/bits-default | 1 + tests/expected/misc/bits-grouped-mask | 1 + tests/expected/misc/bits-list | 1 + tests/expected/misc/bits-mask | 1 + tests/expected/misc/bits-not | 1 + tests/expected/misc/bits-or | 1 + tests/expected/misc/bits-overflow | 1 + tests/expected/misc/bits-parse-mask | 1 + tests/expected/misc/bits-parse-range | 1 + tests/expected/misc/bits-stdin | 1 + tests/expected/misc/bits-xor | 1 + tests/ts/misc/bits | 82 ++++++++ text-utils/Makemodule.am | 6 + text-utils/bits.1.adoc | 147 ++++++++++++++ text-utils/bits.c | 265 ++++++++++++++++++++++++++ text-utils/meson.build | 4 + 22 files changed, 550 insertions(+) create mode 100644 bash-completion/bits create mode 100644 tests/expected/misc/bits create mode 100644 tests/expected/misc/bits-and create mode 100644 tests/expected/misc/bits-binary create mode 100644 tests/expected/misc/bits-default create mode 100644 tests/expected/misc/bits-grouped-mask create mode 100644 tests/expected/misc/bits-list create mode 100644 tests/expected/misc/bits-mask create mode 100644 tests/expected/misc/bits-not create mode 100644 tests/expected/misc/bits-or create mode 100644 tests/expected/misc/bits-overflow create mode 100644 tests/expected/misc/bits-parse-mask create mode 100644 tests/expected/misc/bits-parse-range create mode 100644 tests/expected/misc/bits-stdin create mode 100644 tests/expected/misc/bits-xor create mode 100755 tests/ts/misc/bits create mode 100644 text-utils/bits.1.adoc create mode 100644 text-utils/bits.c...
Quoted 12 lines
+++ b/tests/ts/misc/bits
tests/ts/misc/bits new file +10
-0@@ -0,0 +1,82 @@ +#!/bin/bash + +# +# Copyright (c) 2024 Robin Jarry +# +# This file is part of util-linux. +# +# This file is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or
Quoted 8 lines
tests/ts/misc/bits line 16
Quoted 6 lines
tests/ts/misc/bits new file +6
-0+# (at your option) any later version. +# +# This file is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details.New files should use SPDX tags.
Ack, will change in v2.
Quoted 37 lines
Quoted 37 lines
text-utils/bits.c new file +31
-0@@ -0,0 +1,265 @@ +/* +* SPDX-License-Identifier: GPL-2.0-or-later +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* Copyright (c) 2024 Robin Jarry +* +* bits - convert bit masks from/to various formats +*/ + +#include <errno.h> +#include <getopt.h> +#include <sched.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include "c.h" +#include "closestream.h" +#include "cpuset.h" +#include "nls.h" +#include "strutils.h" +#include "strv.h" + +typedef enum { + OP_OR,
Quoted 8 lines
text-utils/bits.c line 35
Quoted 4 lines
text-utils/bits.c new file +4
-0+OP_AND , +OP_NOT , +OP_XOR , +} bitvise_op_t;"bitwise"?
Also avoid using the _t suffix as it's reserved.
"enumbitwise_op" seems clear enough.
Yes, bitwise is the correct spelling. I will probably remove that in v2 as you suggested below.
Quoted 11 lines
text-utils/bits.c line 40
Quoted 5 lines
text-utils/bits.c new file +5
-0+ +static int parse_mask_or_list (const char *cmdline_arg,cpu_set_t *all_bits) +{ +bitvise_op_t op =OP_OR ; +cpu_set_t bits, copy;This seems very geared towards cpu masks.
What happens if a user wants to use it with very large non-CPU bitmasks, as the tool is advertised as a generic utility?Call the utility "cpumask"?
The default cpu_set_t size can hold up to 1024 bits (128 bytes). I assumed it was more than enough for common use cases. I added a basic test that checks that the program does not crash when specifying a too large mask.
I also didn't want to call that tool "*mask" because it can also deal with lists. But I don't have a strong opinion on the matter.
Quoted 6 lines
text-utils/bits.c line 41
Quoted 2 lines
text-utils/bits.c new file +1
-0+char buf[BUFSIZ ];Only use BUFSIZ in cases where the actual value does not matter, for example loops.
On musl BUFSIZ == 1024 and this could not be enough fairly soon.
Good point. I wasn't sure what is a reasonable maximum argument size. execve(2) says MAX_ARG_STRLEN (32 pages) which seems excessive. I could hardcode 8192 and be done with it. What do you think?
Quoted 8 lines
text-utils/bits.c line 46
Quoted 6 lines
text-utils/bits.c new file +5
-0+char *arg = buf; + +/* copy to allow modifying the argument contents */ +strlcpy (buf, cmdline_arg,sizeof (buf)); + buf[sizeof (buf) -1 ] ='\0' ;You can just use
xstrdup().
But then, I'd have to free it. I wanted to avoid this.
Quoted 20 lines
Quoted 6 lines
text-utils/bits.c new file +5
-0+ +/* strip optional operator first */ +if (startswith (arg,"&" )) { + op =OP_AND ; + arg++;text-utils/bits.c line 61
Quoted 10 lines
text-utils/bits.c new file +10
-0+ }else if (startswith (arg,"^" )) { + op =OP_XOR ; + arg++; + }else if (startswith (arg,"~" )) { + op =OP_NOT ; + arg++; + }else if (startswith (arg,"|" )) { + op =OP_OR ; + arg++; + }The whole op enum could also be replaced by the actual character, this is all in a single function.
Quoted 7 lines
op = '&';
...
switch (op) {
case '&':
}
Good point. I'll do that in v2.
Quoted 9 lines
text-utils/bits.c line 65
Quoted 5 lines
text-utils/bits.c new file +4
-0+ +if (startswith (arg,"," ) ||startswith (arg,"0x" )) { +if (cpumask_parse (arg, &bits,sizeof (bits)) <0 ) +return -EINVAL ;As this is a commandline tool you can call
errx()here to kill the process and provide better error information.
It would also make the call sequence slightly more simple.
Ack.
Quoted 38 lines
Quoted 38 lines
text-utils/bits.c new file +37
-0+ }else { +if (cpulist_parse (arg, &bits,sizeof (bits),1 ) <0 ) +return -EINVAL ; + } + +switch (op) { +case OP_AND : + copy = *all_bits; +CPU_AND (all_bits, ©, &bits); +break ; +case OP_OR : + copy = *all_bits; +CPU_OR (all_bits, ©, &bits); +break ; +case OP_XOR : + copy = *all_bits; +CPU_XOR (all_bits, ©, &bits); +break ; +case OP_NOT : +for (int i =0 ; i <CPU_SETSIZE ; i++) { +if (CPU_ISSET (i, &bits)) +CPU_CLR (i, all_bits); + } +break ; + } + +return 0 ; +} + +typedef enum { +MODE_BINARY , +MODE_GROUPED_MASK , +MODE_LIST , +MODE_MASK , +}output_mode_t ; + +static void print_bits (cpu_set_t *bits,output_mode_t mode)
Quoted 8 lines
text-utils/bits.c line 108
Quoted 6 lines
text-utils/bits.c new file +6
-0+{ +bool started = false; +char buf[BUFSIZ ]; +ssize_t n =0 ; + + buf[0 ] ='\0' ;Is this necessary?
It may have been at some point but it isn't anymore. I'll remove it.
Quoted 45 lines
Quoted 45 lines
text-utils/bits.c new file +44
-0+ +switch (mode) { +case MODE_MASK : +cpumask_create (buf,sizeof (buf), bits,sizeof (*bits)); + +/* strip leading zeroes */ +while (buf[n] =='0' ) + n++; +if (buf[n] =='\0' ) +printf ("0x0\n" ); +else +printf ("0x%s\n" , buf + n); +break ; + +case MODE_GROUPED_MASK : +cpumask_create (buf,sizeof (buf), bits,sizeof (*bits)); + +/* strip leading zeroes */ +while (buf[n] =='0' ) + n++; + +while (buf[n] !='\0' ) { +if (started && (n %8 ) ==0 ) +printf ("," ); +if (buf[n] !='0' ) + started = true; +printf ("%c" , buf[n]); + n++; + } +printf ("\n" ); +break ; + +case MODE_BINARY : +printf ("0b" ); +for (n =CPU_SETSIZE ; n >=0 ; n--) { +if (started && ((n +1 ) %4 ) ==0 ) +printf ("_" ); +if (CPU_ISSET (n, bits)) { + started = true; +printf ("1" ); + }else if (started) { +printf ("0" ); + } + }
Quoted 8 lines
text-utils/bits.c line 157
Quoted 5 lines
text-utils/bits.c new file +5
-0+printf ("\n" ); +break ; + +case MODE_LIST : +cpulist_create (buf,sizeof (buf), bits,sizeof (*bits));This could use the return values of
cpulist_create()andcpumask_create().
Since I am printing into a stack buffer, is there a benefit?
Quoted 81 lines
Quoted 81 lines
text-utils/bits.c new file +81
-0+printf ("%s\n" , buf); +break ; + } + +} + +static void __attribute__((__noreturn__))usage (void ) +{ +fputs (USAGE_HEADER , stdout); +fprintf (stdout,_ (" %s [options] [<mask_or_list>...]\n" ), program_invocation_short_name); + +fputs (USAGE_SEPARATOR , stdout); +fputsln (_ ("Convert bit masks from/to various formats." ), stdout); + +fputs (USAGE_ARGUMENTS , stdout); +fputsln (_ (" <mask_or_list> A set of bits specified as a hex mask value (e.g. 0xeec2)\n" +" or as a comma-separated list of bit IDs.\n" +" If not specified, arguments will be read from stdin." ), stdout); + +fputs (USAGE_OPTIONS , stdout); +fprintf (stdout,USAGE_HELP_OPTIONS (21 )); + +fputs (_ ("\nMode:\n" ), stdout); +fputsln (_ (" -m, --mask Print the combined args as a hex mask value (default).\n" ), stdout); +fputsln (_ (" -g, --grouped-mask Print the combined args as a hex mask value in 32bit\n" +" comma separated groups." ), stdout); +fputsln (_ (" -b, --binary Print the combined args as a binary mask value." ), stdout); +fputsln (_ (" -l, --list Print the combined args as a compressed list of bit IDs." ), stdout); + +fputs (USAGE_SEPARATOR , stdout); +fprintf (stdout,USAGE_MAN_TAIL ("bits(1)" )); +exit (EXIT_SUCCESS ); +} + +int main (int argc,char **argv) +{ +output_mode_t mode =MODE_MASK ; +char **stdin_lines =NULL ; +cpu_set_t bits; +int c; + +static const struct option longopts[] = { + {"version" , no_argument,NULL ,'V' }, + {"help" , no_argument,NULL ,'h' }, + {"mask" , no_argument,NULL ,'m' }, + {"grouped-mask" , no_argument,NULL ,'g' }, + {"binary" , no_argument,NULL ,'b' }, + {"list" , no_argument,NULL ,'l' }, + {NULL ,0 ,NULL ,0 } + }; + +setlocale (LC_ALL ,"" ); +bindtextdomain (PACKAGE ,LOCALEDIR ); +textdomain (PACKAGE ); +close_stdout_atexit (); + +while ((c =getopt_long (argc, argv,"Vhmgbl" , longopts,NULL )) !=-1 ) +switch (c) { +case 'm' : + mode =MODE_MASK ; +break ; +case 'g' : + mode =MODE_GROUPED_MASK ; +break ; +case 'b' : + mode =MODE_BINARY ; +break ; +case 'l' : + mode =MODE_LIST ; +break ; +case 'V' : +print_version (EXIT_SUCCESS ); +case 'h' : +usage (); +default : +errtryhelp (EXIT_FAILURE ); + } + + argc -= optind; + argv += optind; +if (argc ==0 ) {
Quoted 8 lines
text-utils/bits.c line 244
Quoted 6 lines
text-utils/bits.c new file +6
-0+/* no arguments provided, read lines from stdin */ +char buf[BUFSIZ ]; + +while (fgets (buf,sizeof (buf), stdin)) { +/* strip LF, CR, CRLF, LFCR */ + buf[strcspn (buf,"\r\n" )] ='\0' ;
rtrim_whitespace()?
Ack.
Quoted 11 lines
Quoted 11 lines
text-utils/bits.c new file +10
-0+strv_push (&stdin_lines,strdup (buf)); + } + + argc =strv_length (stdin_lines); + argv = stdin_lines; + } + +CPU_ZERO (&bits); + +for (; argc >0 ; argc--, argv++)
Quoted 8 lines
text-utils/bits.c line 260
Quoted 6 lines
text-utils/bits.c new file +6
-0+if (parse_mask_or_list (*argv, &bits) <0 ) { +fprintf (stderr,_ ("error: invalid argument: %s\n" ), *argv); +errtryhelp (EXIT_FAILURE ); + } + +strv_free (stdin_lines);No need to free this here. The process will exit anyways.
I wanted to make ASAN happy. Is this not necessary?
Quoted 6 lines
text-utils/bits.c line 265
Quoted 6 lines
text-utils/bits.c new file +5
-0+ +print_bits (&bits, mode); + +return EXIT_SUCCESS ; +}
Thanks for the review!
Oct 16, 2024 23:11:38 Robin Jarry <[email protected]>:
Quoted 152 lines
Hi Thomas,
Thomas Weißschuh, Oct 16, 2024 at 23:00:
Quoted 4 lines · Robin Jarry
Quoted 1 line
Add a new test utility to convert between bit masks in various formats.
"test" or "text" utility?
That was a typo, I meant "text" :)
Quoted 73 lines
Quoted 58 lines
This can be handy to avoid parsing affinity masks in one's head and/or to interact with the kernel in a more human friendly way. This is a rewrite in C of the bits command from my linux-tools python package so that it can be more widely available.
Here is an example:
~# cat/sys/kernel/debug/tracing/tracing_cpumaskfffffff,ffffffff,ffffffff,ffffffff
~# bits -l ,$(cat/sys/kernel/debug/tracing/tracing_cpumask) 0-128
~# bits -g 58,59,120,123
9000000,00000000,0c000000,00000000
~# bits -g 58,59 >/sys/kernel/debug/tracing/tracing_cpumask~# echo 1 >/sys/kernel/debug/tracing/tracing_on
Add man page and basic tests.Signed-off-by Robin Jarry <[email protected]>---
bash-completion/bits | 21 ++
meson.build | 11 ++ tests/commands.sh | 1 + tests/expected/misc/bits | 0 tests/expected/misc/bits-and | 1 + tests/expected/misc/bits-binary | 1 + tests/expected/misc/bits-default | 1 + tests/expected/misc/bits-grouped-mask | 1 + tests/expected/misc/bits-list | 1 + tests/expected/misc/bits-mask | 1 + tests/expected/misc/bits-not | 1 + tests/expected/misc/bits-or | 1 + tests/expected/misc/bits-overflow | 1 + tests/expected/misc/bits-parse-mask | 1 + tests/expected/misc/bits-parse-range | 1 + tests/expected/misc/bits-stdin | 1 + tests/expected/misc/bits-xor | 1 + tests/ts/misc/bits | 82 ++++++++ text-utils/Makemodule.am | 6 + text-utils/bits.1.adoc | 147 ++++++++++++++ text-utils/bits.c | 265 ++++++++++++++++++++++++++ text-utils/meson.build | 4 +22 files changed, 550 insertions(+)create mode 100644 bash-completion/bits
create mode 100644 tests/expected/misc/bits create mode 100644 tests/expected/misc/bits-and create mode 100644 tests/expected/misc/bits-binary create mode 100644 tests/expected/misc/bits-default create mode 100644 tests/expected/misc/bits-grouped-mask create mode 100644 tests/expected/misc/bits-list create mode 100644 tests/expected/misc/bits-mask create mode 100644 tests/expected/misc/bits-not create mode 100644 tests/expected/misc/bits-or create mode 100644 tests/expected/misc/bits-overflow create mode 100644 tests/expected/misc/bits-parse-mask create mode 100644 tests/expected/misc/bits-parse-range create mode 100644 tests/expected/misc/bits-stdin create mode 100644 tests/expected/misc/bits-xor create mode 100755 tests/ts/misc/bits
create mode 100644 text-utils/bits.1.adoc create mode 100644text-utils/bits.c...
Quoted 12 lines
+++ b/tests/ts/misc/bits
tests/ts/misc/bits new file +10
-0@@ -0,0 +1,82 @@ +#!/bin/bash + +# +# Copyright (c) 2024 Robin Jarry +# +# This file is part of util-linux. +# +# This file is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, orQuoted 8 lines
tests/ts/misc/bits line 16
Quoted 6 lines
tests/ts/misc/bits new file +6
-0+# (at your option) any later version. +# +# This file is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details.New files should use SPDX tags.
Ack, will change in v2.
Quoted 37 lines
Quoted 37 lines
text-utils/bits.c new file +31
-0@@ -0,0 +1,265 @@ +/* +* SPDX-License-Identifier: GPL-2.0-or-later +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* Copyright (c) 2024 Robin Jarry +* +* bits - convert bit masks from/to various formats +*/ + +#include <errno.h> +#include <getopt.h> +#include <sched.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include "c.h" +#include "closestream.h" +#include "cpuset.h" +#include "nls.h" +#include "strutils.h" +#include "strv.h" + +typedef enum { + OP_OR,Quoted 8 lines
text-utils/bits.c line 35
Quoted 4 lines
text-utils/bits.c new file +4
-0+ OP_AND, + OP_NOT, + OP_XOR, +} bitvise_op_t;"bitwise"?
Also avoid using the _t suffix as it's reserved.
"enumbitwise_op" seems clear enough.Yes, bitwise is the correct spelling. I will probably remove that in v2 as you suggested below.
Quoted 10 lines
text-utils/bits.c line 40
Quoted 5 lines
text-utils/bits.c new file +5
-0+ +static int parse_mask_or_list (const char *cmdline_arg,cpu_set_t *all_bits) +{ + bitvise_op_t op = OP_OR; + cpu_set_t bits, copy;This seems very geared towards cpu masks.
What happens if a user wants to use it with very large non-CPU bitmasks, as the tool is advertised as a generic utility?
Quoted 5 lines
Quoted 1 line
Call the utility "cpumask"?
The default
cpu_set_tsize can hold up to 1024 bits (128 bytes). I assumed it was more than enough for common use cases. I added a basic test that checks that the program does not crash when specifying a too large mask.I also didn't want to call that tool "*mask" because it can also deal with lists. But I don't have a strong opinion on the matter.
I don't have an strong opinions on naming. Except that I'm bad at it. The limitation should at least be documented.
(I didn't check of it already is)
Quoted 15 lines
Quoted 5 lines
text-utils/bits.c line 41
Quoted 1 line
text-utils/bits.c new file +1
-0+ char buf[BUFSIZ];Only use BUFSIZ in cases where the actual value does not matter, for example loops.
On musl BUFSIZ == 1024 and this could not be enough fairly soon.Good point. I wasn't sure what is a reasonable maximum argument size. execve(2) says
MAX_ARG_STRLEN(32 pages) which seems excessive. I could hardcode 8192 and be done with it. What do you think?Quoted 7 lines
Quoted 6 lines
text-utils/bits.c new file +5
-0+ char *arg = buf; + + /* copy to allow modifying the argument contents */ + strlcpy(buf, cmdline_arg, sizeof(buf)); + buf[sizeof(buf) - 1] = '\0';
Quoted 3 lines
Quoted 1 line
You can just use
xstrdup().But then, I'd have to free it. I wanted to avoid this.
The CLI utilities of util-linux generally don't free memory if the tool will exit soon anyways.
Quoted 145 lines
Quoted 20 lines
Quoted 6 lines
text-utils/bits.c new file +5
-0+ + /* strip optional operator first */ + if (startswith(arg, "&")) { + op = OP_AND; + arg++;text-utils/bits.c line 61
Quoted 10 lines
text-utils/bits.c new file +10
-0+ } else if (startswith(arg, "^")) { + op = OP_XOR; + arg++; + } else if (startswith(arg, "~")) { + op = OP_NOT; + arg++; + } else if (startswith(arg, "|")) { + op = OP_OR; + arg++; + }The whole op enum could also be replaced by the actual character, this is all in a single function.
Quoted 7 lines
op = '&';
...
switch (op) {
case '&':
}Good point. I'll do that in v2.
Quoted 9 lines
text-utils/bits.c line 65
Quoted 5 lines
text-utils/bits.c new file +4
-0+ + if (startswith(arg, ",") || startswith(arg, "0x")) { + if (cpumask_parse(arg, &bits, sizeof(bits)) < 0) + return -EINVAL;As this is a commandline tool you can call
errx()here to kill the process and provide better error information.
It would also make the call sequence slightly more simple.Ack.
Quoted 38 lines
Quoted 38 lines
text-utils/bits.c new file +37
-0+ } else { + if (cpulist_parse(arg, &bits, sizeof(bits), 1) < 0) + return -EINVAL; + } + + switch (op) { + case OP_AND: + copy = *all_bits; + CPU_AND(all_bits, ©, &bits); + break; + case OP_OR: + copy = *all_bits; + CPU_OR(all_bits, ©, &bits); + break; + case OP_XOR: + copy = *all_bits; + CPU_XOR(all_bits, ©, &bits); + break; + case OP_NOT: + for (int i = 0; i < CPU_SETSIZE; i++) { + if (CPU_ISSET(i, &bits)) + CPU_CLR(i, all_bits); + } + break; + } + + return 0; +} + +typedef enum { + MODE_BINARY, + MODE_GROUPED_MASK, + MODE_LIST, + MODE_MASK, +}output_mode_t ; + +static void print_bits (cpu_set_t *bits,output_mode_t mode)Quoted 8 lines
text-utils/bits.c line 108
Quoted 6 lines
text-utils/bits.c new file +6
-0+{ + bool started = false; + char buf[BUFSIZ]; + ssize_t n = 0; + + buf[0] = '\0';Is this necessary?
It may have been at some point but it isn't anymore. I'll remove it.
Quoted 45 lines
Quoted 45 lines
text-utils/bits.c new file +44
-0+ + switch (mode) { + case MODE_MASK: + cpumask_create(buf, sizeof(buf), bits, sizeof(*bits)); + + /* strip leading zeroes */ + while (buf[n] == '0') + n++; + if (buf[n] == '\0') + printf("0x0\n"); + else + printf("0x%s\n", buf + n); + break; + + case MODE_GROUPED_MASK: + cpumask_create(buf, sizeof(buf), bits, sizeof(*bits)); + + /* strip leading zeroes */ + while (buf[n] == '0') + n++; + + while (buf[n] != '\0') { + if (started && (n % 8) == 0) + printf(","); + if (buf[n] != '0') + started = true; + printf("%c", buf[n]); + n++; + } + printf("\n"); + break; + + case MODE_BINARY: + printf("0b"); + for (n = CPU_SETSIZE; n >= 0; n--) { + if (started && ((n + 1) % 4) == 0) + printf("_"); + if (CPU_ISSET(n, bits)) { + started = true; + printf("1"); + } else if (started) { + printf("0"); + } + }Quoted 8 lines
text-utils/bits.c line 157
Quoted 5 lines
text-utils/bits.c new file +5
-0+ printf("\n"); + break; + + case MODE_LIST: + cpulist_create(buf, sizeof(buf), bits, sizeof(*bits));This could use the return values of
cpulist_create()andcpumask_create().
Quoted 1 line
Since I am printing into a stack buffer, is there a benefit?
Wouldn't the same hold true for a non-stack buffer? Anyways, this shouldn't be important.
Quoted 113 lines
Quoted 82 lines
Quoted 82 lines
text-utils/bits.c new file +81
-0+ printf("%s\n", buf); + break; + } + +} + +static void __attribute__((__noreturn__))usage (void ) +{ + fputs(USAGE_HEADER, stdout); + fprintf(stdout, _(" %s [options] [<mask_or_list>...]\n"), program_invocation_short_name); + + fputs(USAGE_SEPARATOR, stdout); + fputsln(_("Convert bit masks from/to various formats."), stdout); + + fputs(USAGE_ARGUMENTS, stdout); + fputsln(_(" <mask_or_list> A set of bits specified as a hex mask value (e.g. 0xeec2)\n" + " or as a comma-separated list of bit IDs.\n" + " If not specified, arguments will be read from stdin."), stdout); + + fputs(USAGE_OPTIONS, stdout); + fprintf(stdout, USAGE_HELP_OPTIONS(21)); + + fputs(_("\nMode:\n"), stdout); + fputsln(_(" -m, --mask Print the combined args as a hex mask value (default).\n"), stdout); + fputsln(_(" -g, --grouped-mask Print the combined args as a hex mask value in 32bit\n" + " comma separated groups."), stdout); + fputsln(_(" -b, --binary Print the combined args as a binary mask value."), stdout); + fputsln(_(" -l, --list Print the combined args as a compressed list of bit IDs."), stdout); + + fputs(USAGE_SEPARATOR, stdout); + fprintf(stdout, USAGE_MAN_TAIL("bits(1)")); + exit(EXIT_SUCCESS); +} + +int main (int argc,char **argv) +{ + output_mode_t mode = MODE_MASK; + char **stdin_lines = NULL; + cpu_set_t bits; + int c; + + static const struct option longopts[] = { + { "version", no_argument, NULL, 'V'}, + { "help", no_argument, NULL, 'h'}, + { "mask", no_argument, NULL, 'm'}, + { "grouped-mask", no_argument, NULL, 'g'}, + { "binary", no_argument, NULL, 'b'}, + { "list", no_argument, NULL, 'l'}, + { NULL, 0, NULL, 0 } + }; + + setlocale(LC_ALL, ""); + bindtextdomain(PACKAGE, LOCALEDIR); + textdomain(PACKAGE); + close_stdout_atexit(); + + while ((c = getopt_long(argc, argv, "Vhmgbl", longopts, NULL)) != -1) + switch (c) { + case 'm': + mode = MODE_MASK; + break; + case 'g': + mode = MODE_GROUPED_MASK; + break; + case 'b': + mode = MODE_BINARY; + break; + case 'l': + mode = MODE_LIST; + break; + case 'V': + print_version(EXIT_SUCCESS); + case 'h': + usage(); + default: + errtryhelp(EXIT_FAILURE); + } + + argc -= optind; + argv += optind; + if (argc == 0) {Quoted 8 lines
text-utils/bits.c line 244
Quoted 6 lines
text-utils/bits.c new file +6
-0+ /* no arguments provided, read lines from stdin */ + char buf[BUFSIZ]; + + while (fgets(buf, sizeof(buf), stdin)) { + /* strip LF, CR, CRLF, LFCR */ + buf[strcspn(buf, "\r\n")] = '\0';
rtrim_whitespace()?Ack.
Quoted 11 lines
Quoted 11 lines
text-utils/bits.c new file +10
-0+ strv_push(&stdin_lines, strdup(buf)); + } + + argc = strv_length(stdin_lines); + argv = stdin_lines; + } + + CPU_ZERO(&bits); + + for (; argc > 0; argc--, argv++)Quoted 8 lines
text-utils/bits.c line 260
Quoted 6 lines
text-utils/bits.c new file +6
-0+ if (parse_mask_or_list(*argv, &bits) < 0) { + fprintf(stderr, _("error: invalid argument: %s\n"), *argv); + errtryhelp(EXIT_FAILURE); + } + + strv_free(stdin_lines);No need to free this here. The process will exit anyways.
Quoted 1 line
I wanted to make ASAN happy. Is this not necessary?
As mentioned above, freeing memory at the end of the process lifetime isn't really useful.
The kernel will need to clean it up anyways.
Quoted 7 lines
Quoted 5 lines
text-utils/bits.c line 265
Quoted 5 lines
text-utils/bits.c new file +5
-0+ + print_bits(&bits, mode); + + return EXIT_SUCCESS; +}Thanks for the review!
PATCH v2 1/1 text-utils: add bits command
In reply to Robin JarryAdd a new text utility to convert bit masks in various formats.
This can be handy to avoid parsing affinity masks in one's head and/or to interact with the kernel in a more human friendly way. It is a rewrite in C of the bits command from my linux-tools python package so that it can be more widely available.
Here is an example:
~# cat /sys/kernel/debug/tracing/tracing_cpumask ffffffff,ffffffff,ffffffff,ffffffff
~# bits -l ,$(cat /sys/kernel/debug/tracing/tracing_cpumask) 0-128
~# bits -g 58,59,120,123 9000000,00000000,0c000000,00000000
~# bits -g 58,59,120,123 > /sys/kernel/debug/tracing/tracing_cpumask ~# echo 1 > /sys/kernel/debug/tracing/tracing_on
Add man page and basic tests.
---
Notes:
v2:
- Fixed grouped-mask parsing (e.g.: bits -l ,0c000000,00000000). Updated unit tests to verify that it works.
- Changed code wrapping to keep arg strings in a single line.
- s/BUFSIZ/LINE_MAX/
- s/strdup/xstrdup/
- Removed unnecessary string copies.
- Added check for strv_push() return value.
- Removed "bitvise_op_t" enum and replaced with inline char values.
- Exit with explicit errors in parse_mask_or_list().
- Replaced license with SPDX in test file.
- Fixed configure.ac + Makemodule.am. Tested build with make.
- Fixed commit message example.
- Fixed typos.
.gitignore | 1 + bash-completion/bits | 21 ++ configure.ac | 3 + meson.build | 11 + tests/commands.sh | 1 + tests/expected/misc/bits | 0 tests/expected/misc/bits-and | 1 + tests/expected/misc/bits-binary | 1 + tests/expected/misc/bits-default | 1 + tests/expected/misc/bits-grouped-mask | 1 + tests/expected/misc/bits-list | 1 + tests/expected/misc/bits-mask | 1 + tests/expected/misc/bits-not | 1 + tests/expected/misc/bits-or | 1 + tests/expected/misc/bits-overflow | 1 + tests/expected/misc/bits-parse-grouped-mask | 1 + tests/expected/misc/bits-parse-mask | 1 + tests/expected/misc/bits-parse-range | 1 + tests/expected/misc/bits-stdin | 1 + tests/expected/misc/bits-xor | 1 + tests/ts/misc/bits | 81 +++++++ text-utils/Makemodule.am | 9 +- text-utils/bits.1.adoc | 126 ++++++++++ text-utils/bits.c | 256 ++++++++++++++++++++ text-utils/meson.build | 4 + 25 files changed, 526 insertions(+), 1 deletion(-) create mode 100644 bash-completion/bits create mode 100644 tests/expected/misc/bits create mode 100644 tests/expected/misc/bits-and create mode 100644 tests/expected/misc/bits-binary create mode 100644 tests/expected/misc/bits-default create mode 100644 tests/expected/misc/bits-grouped-mask create mode 100644 tests/expected/misc/bits-list create mode 100644 tests/expected/misc/bits-mask create mode 100644 tests/expected/misc/bits-not create mode 100644 tests/expected/misc/bits-or create mode 100644 tests/expected/misc/bits-overflow create mode 100644 tests/expected/misc/bits-parse-grouped-mask create mode 100644 tests/expected/misc/bits-parse-mask create mode 100644 tests/expected/misc/bits-parse-range create mode 100644 tests/expected/misc/bits-stdin create mode 100644 tests/expected/misc/bits-xor create mode 100755 tests/ts/misc/bits create mode 100644 text-utils/bits.1.adoc create mode 100644 text-utils/bits.c
.gitignore +1 -0
@@ -76,6 +76,7 @@ ylwrap # /addpart /agetty +/bits /build*/ /blkdiscard /blkid
bash-completion/bits new file +21 -0
@@ -0,0 +1,21 @@ +_bits_module() +{ + local cur prev OPTS + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + case $prev in + '-h'|'--help'|'-V'|'--version') + return 0 + ;; + esac + case $cur in + -*) + OPTS="--version --help --mask --grouped-mask --bit --list" + COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) ) + return 0 + ;; + esac + return 0 +} +complete -F _bits_module bits
configure.ac +3 -0
@@ -2233,6 +2233,9 @@ UL_REQUIRES_HAVE([scriptlive], [pty], [openpty function (libutil)]) AM_CONDITIONAL([BUILD_SCRIPTLIVE], [test "x$build_scriptlive" = xyes]) +UL_BUILD_INIT([bits], [yes]) +AM_CONDITIONAL([BUILD_BITS], [test "x$build_bits" = xyes]) + UL_BUILD_INIT([col], [check]) UL_REQUIRES_COMPILE([col], [#include <limits.h>], [__GLIBC__], [building for glibc]) AM_CONDITIONAL([BUILD_COL], [test "x$build_col" = xyes])
meson.build +11 -0
@@ -1201,6 +1201,17 @@ endif ############################################################ +exe = executable( + 'bits', + bits_sources, + include_directories : includes, + link_with : lib_common, + install_dir : usrbin_exec_dir, + install : true) +exes += exe +manadocs += ['text-utils/bits.1.adoc'] +bashcompletions += ['bits'] + if is_glibc exe = executable( 'col',
tests/commands.sh +1 -0
@@ -64,6 +64,7 @@ TS_HELPER_TIMEUTILS="${ts_helpersdir}test_timeutils" # paths to commands TS_CMD_ADDPART=${TS_CMD_ADDPART:-"${ts_commandsdir}addpart"} TS_CMD_DELPART=${TS_CMD_DELPART:-"${ts_commandsdir}delpart"} +TS_CMD_BITS=${TS_CMD_BITS-"${ts_commandsdir}bits"} TS_CMD_BLKDISCARD=${TS_CMD_BLKID-"${ts_commandsdir}blkdiscard"} TS_CMD_BLKID=${TS_CMD_BLKID-"${ts_commandsdir}blkid"} TS_CMD_CAL=${TS_CMD_CAL-"${ts_commandsdir}cal"}
tests/expected/misc/bits new file +0 -0
tests/expected/misc/bits-and new file +1 -0
@@ -0,0 +1 @@ +75-100
tests/expected/misc/bits-binary new file +1 -0
@@ -0,0 +1 @@ +0b1_0000_0000_0010_0000_0000_0100_0000_0000_1000_0000_0000
tests/expected/misc/bits-default new file +1 -0
@@ -0,0 +1 @@ +0x100200400800
tests/expected/misc/bits-grouped-mask new file +1 -0
@@ -0,0 +1 @@ +1002,00400800
tests/expected/misc/bits-list new file +1 -0
@@ -0,0 +1 @@ +11,22,33,44
tests/expected/misc/bits-mask new file +1 -0
@@ -0,0 +1 @@ +0x100200400800
tests/expected/misc/bits-not new file +1 -0
@@ -0,0 +1 @@ +50-74
tests/expected/misc/bits-or new file +1 -0
@@ -0,0 +1 @@ +50-150
tests/expected/misc/bits-overflow new file +1 -0
@@ -0,0 +1 @@ +0x0
tests/expected/misc/bits-parse-grouped-mask new file +1 -0
@@ -0,0 +1 @@ +58,59,120,123
tests/expected/misc/bits-parse-mask new file +1 -0
@@ -0,0 +1 @@ +1,3,6,7,9,11,14-16,18,19,21,23-25,27
tests/expected/misc/bits-parse-range new file +1 -0
@@ -0,0 +1 @@ +7fffff,ffffffff,ffffffff,fffc0000,00000000
tests/expected/misc/bits-stdin new file +1 -0
@@ -0,0 +1 @@ +11,33,44
tests/expected/misc/bits-xor new file +1 -0
@@ -0,0 +1 @@ +50-74,101-150
tests/ts/misc/bits new file +81 -0
81 lines not on this page · raw message
text-utils/Makemodule.am +8 -1
@@ -1,3 +1,11 @@ +if BUILD_BITS +usrbin_exec_PROGRAMS += bits +MANPAGES += text-utils/bits.1 +dist_noinst_DATA += text-utils/bits.1.adoc +bits_SOURCES = text-utils/bits.c +bits_LDADD = $(LDADD) libcommon.la +endif + if BUILD_COL usrbin_exec_PROGRAMS += col MANPAGES += text-utils/col.1
@@ -105,4 +113,3 @@ test_more_CFLAGS = -DTEST_PROGRAM $(more_CFLAGS) test_more_LDADD = $(more_LDADD) endif # BUILD_MORE -
text-utils/bits.1.adoc new file +126 -0
126 lines not on this page · raw message
text-utils/bits.c new file +256 -0
256 lines not on this page · raw message
text-utils/meson.build +4 -0
@@ -1,3 +1,7 @@ +bits_sources = files( + 'bits.c', +) + col_sources = files( 'col.c', )
Signature · 1 line
2.47.0