19974eebdb559e73c6b6385e0935bdb9ed1f644f
[gapil.git] / sources / getcap.c
1 /* getcap.c
2  * 
3  * Copyright (C) 2006 Simone Piccardi
4  * 
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.
9  * 
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.
14  * 
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.
18  */
19 /****************************************************************
20  *
21  * Program getcap.c: 
22  * Program to test capabilities functions
23  *
24  * Author: Simone Piccardi
25  * Mar. 2006
26  *
27  * Usage: getcap -h give all info's
28  *
29  *
30  ****************************************************************/
31 /* 
32  * Include needed headers
33  */
34 #define _GNU_SOURCE
35 #include <errno.h>       /* error definitions and routines */ 
36 #include <stdlib.h>      /* C standard library */
37 #include <unistd.h>      /* unix standard library */
38 #include <stdio.h>       /* standard I/O library */
39 #include <string.h>      /* string functions */
40 #include <sys/types.h>
41 #include <sys/capability.h>
42
43 /* Help printing routine */
44 void usage(void);
45
46 int main(int argc, char *argv[])
47 {
48 /* 
49  * Variables definition  
50  */
51     int i;
52     pid_t pid = 0;
53     cap_t capab = NULL;
54     char *string;
55     int res;
56
57     /*
58      * Input section: decode command line parameters 
59      * Use getopt function
60      */
61     opterr = 0;  /* don't want writing to stderr */
62     while ( (i = getopt(argc, argv, "hp:")) != -1) {
63         switch (i) {
64         /* 
65          * Handling options 
66          */ 
67         case 'h':   /* help option */
68             printf("Wrong -h option use\n");
69             usage();
70             return -1;
71             break;
72         case 'p':   /* specify pid */
73             pid = strtol(optarg, NULL, 10);
74             break;
75         case '?':   /* unrecognized options */
76             printf("Unrecognized options -%c\n",optopt);
77             usage();
78         default:    /* should not reached */
79             usage();
80         }
81     }
82     /* ***********************************************************
83      * 
84      *           Options processing completed
85      *
86      *                Main code beginning
87      * 
88      * ***********************************************************/
89     if ( (argc-optind) != 0)  {
90         printf("From %d arguments, removed %d options\n", argc, optind);
91         usage();
92     }
93     
94     if (!pid) {
95         capab = cap_get_proc();
96         if (capab == NULL) {
97             perror("cannot get current process capabilities");
98             return 1;
99         }
100     } else {
101         capab = cap_init();
102         res = capgetp(pid, capab);
103         if (res) {
104             perror("cannot get process capabilities");
105             return 1;
106         }
107     }
108
109     string = cap_to_text(capab, NULL);
110     printf("Capability: %s\n", string);
111
112     cap_free(capab);
113     cap_free(string);
114     return 0;
115 }
116 /*
117  * routine to print usage info and exit
118  */
119 void usage(void) {
120     printf("Program getcap: print current process capabilities \n");
121     printf("Usage:\n");
122     printf("  getcap [-h] [-p pid] \n");
123     printf("  -h           print this help\n");
124     printf("  -p PID       print process PID capabilities\n");
125     
126     exit(1);
127 }
128
129