Inserito un inizio di esempio dell'uso delle funzioni delle capabilities.
[gapil.git] / sources / cap_print.c
diff --git a/sources/cap_print.c b/sources/cap_print.c
new file mode 100644 (file)
index 0000000..4d6e708
--- /dev/null
@@ -0,0 +1,110 @@
+/* cap_print.c
+ * 
+ * Copyright (C) 2005 Simone Piccardi
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+/*****************************************************************************
+ *
+ * File cap_print.c: An example for capabilities print
+ *
+ * Author: S. Piccardi Dec. 2005
+ *
+ *****************************************************************************/
+#include <stdlib.h>        /* C standard library */
+#include <stdio.h>         /* I/O standard library */
+#include <unistd.h>
+#include <sys/capability.h>
+
+/*
+ * Program cap_print
+ *
+ * Use libcap2 functions and print results
+ */
+/* Help printing routine */
+void usage(void);
+
+int main(int argc, char *argv[]) 
+{
+   /* 
+    * Variables definition
+    */
+    int i;
+    cap_t capstatus;
+    cap_flag_t capset=CAP_EFFECTIVE;
+    cap_flag_value_t capval;
+    cap_value_t capability;
+    char * buffer;
+    /*
+     * Input section: decode command line parameters 
+     * Use getopt function
+     */
+    opterr = 0;         /* don't want writing to stderr */
+    while ( (i = getopt(argc, argv, "hepi")) != -1) {
+       switch (i) {
+       /* 
+        * Handling options 
+        */ 
+       case 'i':                                     /* read inherited set */
+           capset=CAP_INHERITABLE;
+           break;
+       case 'e':                                     /* read effective set */
+           capset=CAP_EFFECTIVE;
+           break;
+       case 'p':                                     /* read permitted set */
+           capset=CAP_PERMITTED;
+           break;
+       case 'h':                                            /* help option */
+           printf("Wrong -h option use\n");
+           usage();
+           return -1;
+           break;
+       case '?':                                    /* unrecognized options */
+           printf("Unrecognized options -%c\n",optopt);
+           usage();
+       default:                                       /* should not reached */
+           usage();
+       }
+    }
+    /* ***********************************************************
+     * 
+     *          Options processing completed
+     *
+     *               Main code beginning
+     * 
+     * ***********************************************************/
+    if ((argc - optind) != 0) {
+       printf("Wrong number of arguments %d\n", argc - optind);
+        usage();
+    }
+    if ( (capstatus = cap_get_proc()) == NULL) {
+       perror("Lettura delle capabilities fallita");
+    }
+    if ( (buffer = cap_to_text(capstatus, NULL)) == NULL) {
+       perror("Cannot convert capability status:");
+    }
+    printf("Capabilities:\n %s\n", buffer);
+    exit(0);
+}
+/*
+ * routine to print usage info and exit
+ */
+void usage(void) {
+    printf("Program mygethost: do an hostname resolution \n");
+    printf("Usage:\n");
+    printf("  mygethost [-h] hostname \n");
+    printf("  -h   print this help\n");
+    exit(1);
+}