Varie correzioni, completata revisione capitolo sull'I/O su file
[gapil.git] / sources / getcap.c
1 /* getcap.c
2  * 
3  * Copyright (C) 2006-2012 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  * Include needed headers
32  */
33 #define _GNU_SOURCE
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 */
41
42 /* Help printing routine */
43 void usage(void);
44
45 int main(int argc, char *argv[])
46 {
47 /* 
48  * Variables definition  
49  */
50     int i;
51     pid_t pid = 0;
52     cap_t capab;
53     char *string;
54     int res;
55
56     /*
57      * Input section: decode command line parameters 
58      * Use getopt function
59      */
60     opterr = 0;  /* don't want writing to stderr */
61     while ( (i = getopt(argc, argv, "hp:")) != -1) {
62         switch (i) {
63         /* 
64          * Handling options 
65          */ 
66         case 'h':   /* help option */
67             printf("Wrong -h option use\n");
68             usage();
69             return -1;
70             break;
71         case 'p':   /* specify pid */
72             pid = strtol(optarg, NULL, 10);
73             break;
74         case '?':   /* unrecognized options */
75             printf("Unrecognized options -%c\n",optopt);
76             usage();
77         default:    /* should not reached */
78             usage();
79         }
80     }
81     /* ***********************************************************
82      * 
83      *           Options processing completed
84      *
85      *                Main code beginning
86      * 
87      * ***********************************************************/
88     if ( (argc-optind) != 0)  {
89         printf("From %d arguments, removed %d options\n", argc, optind);
90         usage();
91     }
92     
93     if (!pid) {
94         capab = cap_get_proc();
95         if (capab == NULL) {
96             perror("cannot get current process capabilities");
97             return 1;
98         }
99     } else {
100         capab = cap_get_pid(pid);
101         if (capab == NULL) {
102             perror("cannot get process capabilities");
103             return 1;
104         }
105     }
106
107     string = cap_to_text(capab, NULL);
108     printf("Capability: %s\n", string);
109
110     cap_free(capab);
111     cap_free(string);
112     return 0;
113 }
114 /*
115  * routine to print usage info and exit
116  */
117 void usage(void) {
118     printf("Program getcap: print current process capabilities \n");
119     printf("Usage:\n");
120     printf("  getcap [-h] [-p pid] \n");
121     printf("  -h           print this help\n");
122     printf("  -p PID       print process PID capabilities\n");
123     
124     exit(1);
125 }
126
127