summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJim Meyering <meyering@redhat.com>2012-04-27 21:24:03 +0200
committerJim Meyering <meyering@redhat.com>2012-04-27 23:00:55 +0200
commitc8b8279d2bd9ed1005ebbcca018446d3a245b97c (patch)
treeeac81bce0d8daa50d906be42eeb0287a8f0b036f /src
parenta6719d9f7252bbd85eaab840a61dfc93d57b05c5 (diff)
downloadcoreutils-c8b8279d2bd9ed1005ebbcca018446d3a245b97c.tar.xz
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.
Diffstat (limited to 'src')
-rw-r--r--src/id.c21
1 files 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);
}