3 * Copyright (C) 2006-2012 Simone Piccardi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /****************************************************************
22 * Program to test capabilities functions
24 * Author: Simone Piccardi
27 * Usage: getcap -h give all info's
29 ****************************************************************/
31 * Include needed headers
34 #include <errno.h> /* error definitions and routines */
35 #include <stdlib.h> /* C standard library */
36 #include <unistd.h> /* unix standard library */
37 #include <stdio.h> /* standard I/O library */
38 #include <string.h> /* C strings library */
39 #include <sys/types.h> /* primitive system data types */
40 #include <sys/capability.h> /* need package libcap-dev */
42 /* Help printing routine */
45 int main(int argc, char *argv[])
48 * Variables definition
57 * Input section: decode command line parameters
60 opterr = 0; /* don't want writing to stderr */
61 while ( (i = getopt(argc, argv, "hp:")) != -1) {
66 case 'h': /* help option */
67 printf("Wrong -h option use\n");
71 case 'p': /* specify pid */
72 pid = strtol(optarg, NULL, 10);
74 case '?': /* unrecognized options */
75 printf("Unrecognized options -%c\n",optopt);
77 default: /* should not reached */
81 /* ***********************************************************
83 * Options processing completed
87 * ***********************************************************/
88 if ( (argc-optind) != 0) {
89 printf("From %d arguments, removed %d options\n", argc, optind);
94 capab = cap_get_proc();
96 perror("cannot get current process capabilities");
100 capab = cap_get_pid(pid);
102 perror("cannot get process capabilities");
107 string = cap_to_text(capab, NULL);
108 printf("Capability: %s\n", string);
115 * routine to print usage info and exit
118 printf("Program getcap: print current process capabilities \n");
120 printf(" getcap [-h] [-p pid] \n");
121 printf(" -h print this help\n");
122 printf(" -p PID print process PID capabilities\n");