From c8b8279d2bd9ed1005ebbcca018446d3a245b97c Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Fri, 27 Apr 2012 21:24:03 +0200 Subject: [PATCH] id: don't call getcon unnecessarily * src/id.c (main): Invocations like "id" and "id -G" would call getcon to determine the current security context even though that result would not be used. Similarly, when POSIXLY_CORRECT is set. Rearrange conditionals and hoist the POSIXLY_CORRECT test so that we call getcon only when necessary. --- src/id.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/id.c b/src/id.c index e1b51e764..da8142287 100644 --- a/src/id.c +++ b/src/id.c @@ -177,16 +177,23 @@ main (int argc, char **argv) if (just_user + just_group + just_group_list + just_context > 1) error (EXIT_FAILURE, 0, _("cannot print \"only\" of more than one choice")); - if (just_user + just_group + just_group_list == 0 && (use_real || use_name)) + bool default_format = (just_user + just_group + just_group_list == 0); + + if (default_format && (use_real || use_name)) error (EXIT_FAILURE, 0, _("cannot print only names or real IDs in default format")); - /* If we are on a selinux-enabled kernel and no user is specified, - get our context. Otherwise, leave the context variable alone - - it has been initialized known invalid value and will be not - displayed in print_full_info() */ - if (selinux_enabled && n_ids == 0) + /* If we are on a selinux-enabled kernel, no user is specified, and + either --context is specified or none of (-u,-g,-G) is specified, + and we're not in POSIXLY_CORRECT mode, get our context. Otherwise, + leave the context variable alone - it has been initialized to an + invalid value that will be not displayed in print_full_info(). */ + if (selinux_enabled + && n_ids == 0 + && (just_context || + (default_format && ! getenv ("POSIXLY_CORRECT")))) { + /* Report failure only if --context (-Z) was explicitly requested. */ if (getcon (&context) && just_context) error (EXIT_FAILURE, 0, _("can't get process context")); } @@ -361,6 +368,6 @@ print_full_info (const char *username) /* POSIX mandates the precise output format, and that it not include any context=... part, so skip that if POSIXLY_CORRECT is set. */ - if (context != NULL && ! getenv ("POSIXLY_CORRECT")) + if (context) printf (_(" context=%s"), context); } -- 2.11.4.GIT