aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew G. Morgan <morgan@kernel.org>2022-09-04 14:36:52 -0700
committerAndrew G. Morgan <morgan@kernel.org>2022-09-04 14:36:52 -0700
commit26e3a096a4eb4edd8bbcaab57ac8df38e6594a1d (patch)
tree31f183690f8df0ab4ef9c3020c908853b8368fe5
parentfc804acc078ef03e2c5b3a233f118a537f260ccd (diff)
downloadlibcap-26e3a096a4eb4edd8bbcaab57ac8df38e6594a1d.tar.gz
Clean up getpcaps code.
Address some corner cases and trim down the size of the code a bit. Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
-rw-r--r--progs/getpcaps.c103
1 files changed, 52 insertions, 51 deletions
diff --git a/progs/getpcaps.c b/progs/getpcaps.c
index 1e914b2..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"
@@ -38,44 +38,44 @@ int main(int argc, char **argv)
usage(1);
}
- for ( ++argv; --argc > 0; ++argv ) {
+ 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(argv[0], "--iab")) {
+ } else if (!strcmp(arg, "--iab")) {
iab = 1;
continue;
}
errno = 0;
- lpid = strtol(argv[0], &endarg, 10);
- if (*endarg != '\0') {
- errno = EINVAL;
- }
+ lpid = strtol(arg, &endarg, 10);
if (errno == 0) {
- if (lpid < 0 || pid != (pid_t) pid)
+ 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",
- argv[0], strerror(errno));
+ fprintf(stderr, "Cannot parse pid %s: (%s)\n", arg, strerror(errno));
retval = 1;
continue;
}
@@ -87,43 +87,44 @@ int main(int argc, char **argv)
" (%s)\n", pid, strerror(errno));
retval = 1;
continue;
- } else {
- char *result = cap_to_text(cap_d, NULL);
- if (iab) {
- printf("%s:", *argv);
- 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);
+ }
+
+ 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);
+ }
+ 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);
}
- 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", *argv, result);
- } else if (verbose == 2) {
- fprintf(stderr, "Capabilities for `%s': %s\n", *argv, result);
- } else {
- printf("%s: %s\n", *argv, result);
+ printf(" [%s]", iab_text);
+ cap_free(iab_text);
}
- cap_free(result);
- result = NULL;
- cap_free(cap_d);
+ 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;