diff options
Diffstat (limited to 'progs/getpcaps.c')
-rw-r--r-- | progs/getpcaps.c | 93 |
1 files changed, 70 insertions, 23 deletions
diff --git a/progs/getpcaps.c b/progs/getpcaps.c index 5e78487..7e14c36 100644 --- a/progs/getpcaps.c +++ b/progs/getpcaps.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997,2008 Andrew G. Morgan <morgan@kernel.org> + * Copyright (c) 1997-8,2007-8,19,21-22 Andrew G. Morgan <morgan@kernel.org> * * This displays the capabilities of given target process(es). */ @@ -14,7 +14,7 @@ static void usage(int code) { fprintf(stderr, -"usage: getcaps <pid> [<pid> ...]\n\n" +"usage: getcaps [opts] <pid> [<pid> ...]\n\n" " This program displays the capabilities on the queried process(es).\n" " The capabilities are displayed in the cap_from_text(3) format.\n" "\n" @@ -22,6 +22,7 @@ static void usage(int code) " --help, -h or --usage display this message.\n" " --verbose use a more verbose output format.\n" " --ugly or --legacy use the archaic legacy output format.\n" + " --iab show IAB of process too.\n" " --license display license info\n"); exit(code); } @@ -30,34 +31,55 @@ int main(int argc, char **argv) { int retval = 0; int verbose = 0; + int iab = 0; + cap_iab_t noiab = cap_iab_init(); if (argc < 2) { usage(1); } - for ( ++argv; --argc > 0; ++argv ) { - ssize_t length; + for (++argv; --argc > 0; ++argv) { + long lpid; int pid; + char *endarg; cap_t cap_d; + const char *arg = *argv; - if (!strcmp(argv[0], "--help") || !strcmp(argv[0], "--usage") || - !strcmp(argv[0], "-h")) { + if (!strcmp(arg, "--help") || !strcmp(arg, "--usage") || + !strcmp(arg, "-h")) { usage(0); - } else if (!strcmp(argv[0], "--license")) { + } else if (!strcmp(arg, "--license")) { printf("%s see LICENSE file for details.\n" - "[Copyright (c) 1997-8,2007,19,21" + "[Copyright (c) 1997-8,2007-8,19,21-22" " Andrew G. Morgan <morgan@kernel.org>]\n", - argv[0]); + arg); exit(0); - } else if (!strcmp(argv[0], "--verbose")) { + } else if (!strcmp(arg, "--verbose")) { verbose = 1; continue; - } else if (!strcmp(argv[0], "--ugly") || !strcmp(argv[0], "--legacy")) { + } else if (!strcmp(arg, "--ugly") || !strcmp(arg, "--legacy")) { verbose = 2; continue; + } else if (!strcmp(arg, "--iab")) { + iab = 1; + continue; } - pid = atoi(argv[0]); + errno = 0; + lpid = strtol(arg, &endarg, 10); + if (errno == 0) { + if (*endarg != '\0') { + errno = EINVAL; + } else if (lpid < 0 || lpid != (pid_t) lpid) { + errno = EOVERFLOW; + } + } + if (errno != 0) { + fprintf(stderr, "Cannot parse pid %s: (%s)\n", arg, strerror(errno)); + retval = 1; + continue; + } + pid = lpid; cap_d = cap_get_pid(pid); if (cap_d == NULL) { @@ -65,19 +87,44 @@ int main(int argc, char **argv) " (%s)\n", pid, strerror(errno)); retval = 1; continue; - } else { - char *result = cap_to_text(cap_d, &length); - if (verbose == 1) { - printf("Capabilities for '%s': %s\n", *argv, result); - } else if (verbose == 2) { - fprintf(stderr, "Capabilities for `%s': %s\n", *argv, result); - } else { - printf("%s: %s\n", *argv, result); + } + + char *result = cap_to_text(cap_d, NULL); + if (iab) { + printf("%s:", arg); + if (verbose || strcmp("=", result) != 0) { + printf(" \"%s\"", result); + } + cap_iab_t iab_val = cap_iab_get_pid(pid); + if (iab_val == NULL) { + fprintf(stderr, " no IAB value for %d\n", pid); + exit(1); } - cap_free(result); - result = NULL; - cap_free(cap_d); + int cf = cap_iab_compare(noiab, iab_val); + if (verbose || + CAP_IAB_DIFFERS(cf, CAP_IAB_AMB) || + CAP_IAB_DIFFERS(cf, CAP_IAB_BOUND)) { + char *iab_text = cap_iab_to_text(iab_val); + if (iab_text == NULL) { + perror(" no text for IAB"); + exit(1); + } + printf(" [%s]", iab_text); + cap_free(iab_text); + } + cap_free(iab_val); + printf("\n"); + } else if (verbose == 1) { + printf("Capabilities for '%s': %s\n", arg, result); + } else if (verbose == 2) { + fprintf(stderr, "Capabilities for `%s': %s\n", arg, result); + } else { + printf("%s: %s\n", arg, result); } + + cap_free(result); + result = NULL; + cap_free(cap_d); } return retval; |