[PATCH 1/1] delgroup: Add --only-if-empty argument option
Magnus Armholt
magnus.armholt at gmail.com
Mon Jan 30 05:05:47 UTC 2023
Any opinions about this?
-Magnus
On Tue, Nov 15, 2022 at 12:59 PM Magnus Armholt <magnus.armholt at gmail.com>
wrote:
> Add option to only remove group if it is empty.
> The option can also be given to deluser, if delgroup is enabled.
> When --only-if-empty flag is given to deluser, it will not
> return failure when removing same named group fails due to
> being non-empty.
> ---
> loginutils/deluser.c | 50 +++++++++++++++++++++++++++++++++++++-------
> 1 file changed, 42 insertions(+), 8 deletions(-)
>
> diff --git a/loginutils/deluser.c b/loginutils/deluser.c
> index 8e7df737c..689adc7db 100644
> --- a/loginutils/deluser.c
> +++ b/loginutils/deluser.c
> @@ -36,16 +36,23 @@
> //kbuild:lib-$(CONFIG_DELGROUP) += deluser.o
>
> //usage:#define deluser_trivial_usage
> -//usage: IF_LONG_OPTS("[--remove-home] ") "USER"
> +//usage: IF_LONG_OPTS("[--remove-home] ")
> +//usage: IF_DELGROUP(IF_LONG_OPTS("[--only-if-empty] ")) "USER"
> //usage:#define deluser_full_usage "\n\n"
> //usage: "Delete USER from the system"
> -// --remove-home is self-explanatory enough to put it in --help
> +//usage: "\n --remove-home Remove also home folder of user
> USER"
> +//usage: "\n --only-if-empty Only remove group, with same
> name as user USER,"
> +//usage: "\n from the system if it is empty"
> +
>
> //usage:#define delgroup_trivial_usage
> -//usage: IF_FEATURE_DEL_USER_FROM_GROUP("[USER] ")"GROUP"
> +//usage: IF_LONG_OPTS("[--only-if-empty] ")
> +//usage: IF_FEATURE_DEL_USER_FROM_GROUP("[USER] ")"GROUP"
> //usage:#define delgroup_full_usage "\n\n"
> //usage: "Delete group GROUP from the system"
> //usage: IF_FEATURE_DEL_USER_FROM_GROUP(" or user USER from group
> GROUP")
> +//usage: "\n --only-if-empty Only remove group GROUP from the
> system"
> +//usage: "\n if it is empty"
>
> #include "libbb.h"
>
> @@ -65,13 +72,34 @@ int deluser_main(int argc, char **argv)
>
> #if !ENABLE_LONG_OPTS
> const int opt_delhome = 0;
> + const int opt_delgroup_onlyifempty = 0;
> #else
> + int opt_delgroup_onlyifempty = 0;
> int opt_delhome = 0;
> if (do_deluser) {
> - opt_delhome = getopt32long(argv, "",
> - "remove-home\0" No_argument "\xff");
> - argv += opt_delhome;
> - argc -= opt_delhome;
> + int num_opts = 0;
> + int opts = getopt32long(argv, "",
> + "remove-home\0" No_argument "\xef"
> + "only-if-empty\0" No_argument "\xff");
> + if (opts & 1 << 0)
> + {
> + opt_delhome = 1;
> + num_opts++;
> + }
> + if (opts & 1 << 1)
> + {
> + opt_delgroup_onlyifempty = 1;
> + num_opts++;
> + }
> +
> + argv += num_opts;
> + argc -= num_opts;
> +
> + } else {
> + opt_delgroup_onlyifempty = getopt32long(argv, "",
> + "only-if-empty\0" No_argument "\xff");
> + argv += opt_delgroup_onlyifempty;
> + argc -= opt_delgroup_onlyifempty;
> }
> #endif
>
> @@ -114,10 +142,16 @@ int deluser_main(int argc, char **argv)
> if (do_deluser < 0) { /* delgroup after deluser? */
> gr = getgrnam(name);
> if (!gr)
> - return EXIT_SUCCESS;
> + return EXIT_SUCCESS;
>
> } else {
> gr = xgetgrnam(name); /* bail out if GROUP
> is wrong */
> }
> + if (opt_delgroup_onlyifempty && gr->gr_mem !=
> NULL) {
> + if (do_deluser < 0) { /* delgroup after
> deluser? */
> + return EXIT_SUCCESS;
> + }
> + bb_error_msg_and_die("'%s' is not empty",
> name);
> + }
> if (!member) {
> /* "delgroup GROUP" */
> struct passwd *pw;
> --
> 2.34.1
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20230130/e67fa460/attachment.html>
More information about the busybox
mailing list