--- projects/libstatgrab/examples/cpu_usage.c 2003/06/05 14:45:39 1.1 +++ projects/libstatgrab/examples/cpu_usage.c 2004/01/06 15:35:04 1.5 @@ -1,7 +1,7 @@ /* * i-scream central monitoring system - * http://www.i-scream.org.uk - * Copyright (C) 2000-2002 i-scream + * http://www.i-scream.org + * Copyright (C) 2000-2003 i-scream * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -24,28 +24,37 @@ #include int main(int argc, char **argv){ - + extern char *optarg; - extern int optind; - int c; + extern int optind; + int c; int delay = 1; cpu_percent_t *cpu_percent; - /* Throw away the first reading as thats averaged over the machines uptime */ - cpu_percent = cpu_percent_usage(); - - while ((c = getopt(argc, argv, "d:")) != EOF){ - switch (c){ - case 'd': - delay = atoi(optarg); - break; + while ((c = getopt(argc, argv, "d:")) != -1){ + switch (c){ + case 'd': + delay = atoi(optarg); + break; } } - /* Clear the screen ready for display the cpu usage */ - printf("\033[2J"); + /* Initialise statgrab */ + statgrab_init(); + /* Drop setuid/setgid privileges. */ + if (statgrab_drop_privileges() != 0) { + perror("Error. Failed to drop privileges"); + return 1; + } + + /* Throw away the first reading as thats averaged over the machines uptime */ + cpu_percent = cpu_percent_usage(); + + /* Clear the screen ready for display the cpu usage */ + printf("\033[2J"); + while( (cpu_percent = cpu_percent_usage()) != NULL){ printf("\033[2;2H%-12s : %6.2f", "User CPU", cpu_percent->user); printf("\033[3;2H%-12s : %6.2f", "Kernel CPU", cpu_percent->kernel); @@ -59,6 +68,3 @@ int main(int argc, char **argv){ exit(0); } - - -