X-Git-Url: https://gapil.gnulinux.it/gitweb/?a=blobdiff_plain;f=sources%2Fgetcap.c;fp=sources%2Fgetcap.c;h=116f3cbbfbeb9f8bac1161cbac3724964ed3592b;hb=2fcfdcb7ec5f5fdacbdeaff72961f1c35d0858dd;hp=0000000000000000000000000000000000000000;hpb=29b7512f9179b716d031c978cd39dc96cd84b8b7;p=gapil.git diff --git a/sources/getcap.c b/sources/getcap.c new file mode 100644 index 0000000..116f3cb --- /dev/null +++ b/sources/getcap.c @@ -0,0 +1,128 @@ +/* getcap.c + * + * Copyright (C) 2006 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. + */ +/**************************************************************** + * + * Program getcap.c: + * Program to test capabilities functions + * + * Author: Simone Piccardi + * Mar. 2006 + * + * Usage: getcap -h give all info's + * + * + ****************************************************************/ +/* + * Include needed headers + */ +#define _GNU_SOURCE +#include /* error definitions and routines */ +#include /* C standard library */ +#include /* unix standard library */ +#include /* standard I/O library */ +#include /* string functions */ +#include +#include + +/* Help printing routine */ +void usage(void); + +int main(int argc, char *argv[]) +{ +/* + * Variables definition + */ + int i; + pid_t pid = 0; + cap_t capab = NULL; + char *string; + int res; + + /* + * Input section: decode command line parameters + * Use getopt function + */ + opterr = 0; /* don't want writing to stderr */ + while ( (i = getopt(argc, argv, "hp:")) != -1) { + switch (i) { + /* + * Handling options + */ + case 'h': /* help option */ + printf("Wrong -h option use\n"); + usage(); + return -1; + break; + case 'p': /* specify pid */ + pid = strtol(optarg, NULL, 10); + 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("From %d arguments, removed %d options\n", argc, optind); + usage(); + } + + if (!pid) { + capab = cap_get_proc(); + if (capab == NULL) { + perror("cannot get current process capabilities"); + return 1; + } + } else { + capab = cap_init(); + res = capgetp(pid, capab); + if (res) { + perror("cannot get process capabilities"); + return 1; + } + } + + string = cap_to_text(capab, NULL); + printf("Capability: %s\n", string); + + cap_free(capab); + return 0; +} +/* + * routine to print usage info and exit + */ +void usage(void) { + printf("Program getcap: print current process capabilities \n"); + printf("Usage:\n"); + printf(" getcap [-h] [-p pid] \n"); + printf(" -h print this help\n"); + printf(" -p PID print process PID capabilities\n"); + + exit(1); +} + +