Materiale su splice, ed esempio non funzionante
[gapil.git] / sources / cap_print.c
1 /* cap_print.c
2  * 
3  * Copyright (C) 2005 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  * File cap_print.c: An example for capabilities print
22  *
23  * Author: S. Piccardi Dec. 2005
24  *
25  *****************************************************************************/
26 #include <stdlib.h>      /* C standard library */
27 #include <stdio.h>       /* standard I/O library */
28 #include <unistd.h>      /* unix standard library */
29 #include <sys/capability.h>
30
31 /*
32  * Program cap_print
33  *
34  * Use libcap2 functions and print results
35  */
36 /* Help printing routine */
37 void usage(void);
38
39 int main(int argc, char *argv[]) 
40 {
41    /* 
42     * Variables definition
43     */
44     int i;
45     cap_t capstatus;
46     cap_flag_t capset=CAP_EFFECTIVE;
47     cap_flag_value_t capval;
48     cap_value_t capability;
49     char * buffer;
50     /*
51      * Input section: decode command line parameters 
52      * Use getopt function
53      */
54     opterr = 0;  /* don't want writing to stderr */
55     while ( (i = getopt(argc, argv, "hepi")) != -1) {
56         switch (i) {
57         /* 
58          * Handling options 
59          */ 
60         case 'i':                                     /* read inherited set */
61             capset=CAP_INHERITABLE;
62             break;
63         case 'e':                                     /* read effective set */
64             capset=CAP_EFFECTIVE;
65             break;
66         case 'p':                                     /* read permitted set */
67             capset=CAP_PERMITTED;
68             break;
69         case 'h':                                            /* help option */
70             printf("Wrong -h option use\n");
71             usage();
72             return -1;
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("Wrong number of arguments %d\n", argc - optind);
90         usage();
91     }
92     if ( (capstatus = cap_get_proc()) == NULL) {
93         perror("Lettura delle capabilities fallita");
94     }
95     if ( (buffer = cap_to_text(capstatus, NULL)) == NULL) {
96         perror("Cannot convert capability status:");
97     }
98     printf("Capabilities:\n %s\n", buffer);
99     exit(0);
100 }
101 /*
102  * routine to print usage info and exit
103  */
104 void usage(void) {
105     printf("Program mygethost: do an hostname resolution \n");
106     printf("Usage:\n");
107     printf("  mygethost [-h] hostname \n");
108     printf("  -h   print this help\n");
109     exit(1);
110 }