File: getpcaps.c

package info (click to toggle)
libcap2 1%3A2.44-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,424 kB
  • sloc: ansic: 4,968; sh: 939; makefile: 463
file content (77 lines) | stat: -rw-r--r-- 1,868 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Copyright (c) 1997,2008 Andrew G. Morgan  <morgan@kernel.org>
 *
 * This displays the capabilities of given target process(es).
 */

#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/capability.h>

static void usage(int exiter)
{
    fprintf(stderr,
"usage: getcaps <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"
"  Optional arguments:\n"
"     --help or --usage     display this message.\n"
"     --verbose             use a more verbose output format.\n"
"     --ugly or --legacy    use the archaic legacy output format.\n\n"
"[Copyright (c) 1997-8,2007,2019 Andrew G. Morgan  <morgan@kernel.org>]\n"
	);
    exit(exiter);
}

int main(int argc, char **argv)
{
    int retval = 0;
    int verbose = 0;

    if (argc < 2) {
	usage(1);
    }

    for ( ++argv; --argc > 0; ++argv ) {
	ssize_t length;
	int pid;
	cap_t cap_d;

	if (!strcmp(argv[0], "--help") || !strcmp(argv[0], "--usage")) {
	    usage(0);
	} else if (!strcmp(argv[0], "--verbose")) {
	    verbose = 1;
	    continue;
	} else if (!strcmp(argv[0], "--ugly") || !strcmp(argv[0], "--legacy")) {
	    verbose = 2;
	    continue;
	}

	pid = atoi(argv[0]);

	cap_d = cap_get_pid(pid);
	if (cap_d == NULL) {
		fprintf(stderr, "Failed to get cap's for process %d:"
			" (%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);
	    }
	    cap_free(result);
	    result = NULL;
	    cap_free(cap_d);
	}
    }

    return retval;
}