[PATCH v2 2/9] setpriv: prepare option parsing logic for additional opts

Denys Vlasenko vda.linux at googlemail.com
Tue Jul 4 15:18:51 UTC 2017


On Sun, Jul 2, 2017 at 3:42 PM, Patrick Steinhardt <ps at pks.im> wrote:
> The current option parsing logic of setpriv only supports the case where
> we want to execute a sub-program and have at most one argument. Refactor
> handling of options to solve these shortcomings to make it easy to
> support `setpriv --dump`, which does not accept any additional
> arguments, as well as the case where additional options are passed to
> setpriv. This is done by handling `argc` ourselves, throwing an error
> when no program is specified, as well as introducing an enum for the
> different option bitmasks.
> ---
>  util-linux/setpriv.c | 17 ++++++++++++++---
>  1 file changed, 14 insertions(+), 3 deletions(-)

Applied patch 1 without editing,
patch 2 with the following edits:

> diff --git a/util-linux/setpriv.c b/util-linux/setpriv.c
> index 686ad45d5..d586f4bef 100644
> --- a/util-linux/setpriv.c
> +++ b/util-linux/setpriv.c
> @@ -51,6 +51,12 @@
>  #define PR_SET_NO_NEW_PRIVS 38
>  #endif
>
> +enum {
> +       OPTBIT_NNP,
> +
> +       OPT_NNP = (1 << OPTBIT_NNP),
> +};
> +
>  int setpriv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
>  int setpriv_main(int argc UNUSED_PARAM, char **argv)
>  {
> @@ -60,15 +66,20 @@ int setpriv_main(int argc UNUSED_PARAM, char **argv)
>                 ;
>         int opts;
>
> -       opt_complementary = "-1";
> +       opt_complementary = "";

If you don't need it, just do not set it. No need to set to "".

>         applet_long_options = setpriv_longopts;
>         opts = getopt32(argv, "+");
>
> -       if (opts) {
> +       argc -= optind;
> +       argv += optind;
> +
> +       if (!argc)
> +               bb_error_msg_and_die("no program specified");

Try to not use argc. if (!argv[0]) works. Also, bb_show_usage()
is less code and more informative.


More information about the busybox mailing list