- indentation

This commit is contained in:
Babak Farrokhi 2015-09-16 09:33:31 +04:30
parent 8ef5f2e9d2
commit d94abab5ed

View File

@ -73,7 +73,8 @@ struct pidfh *pfh;
/* /*
* Prepare for a clean shutdown * Prepare for a clean shutdown
*/ */
void daemon_shutdown() void
daemon_shutdown()
{ {
pidfile_remove(pfh); pidfile_remove(pfh);
} }
@ -81,9 +82,10 @@ void daemon_shutdown()
/* /*
* Act upon receiving signals * Act upon receiving signals
*/ */
void signal_handler(int sig) void
signal_handler(int sig)
{ {
switch(sig) { switch (sig) {
case SIGHUP: case SIGHUP:
case SIGINT: case SIGINT:
@ -99,7 +101,8 @@ void signal_handler(int sig)
/* /*
* Obtain stats for interface(s). * Obtain stats for interface(s).
*/ */
static void fill_iftot(struct iftot *st) static void
fill_iftot(struct iftot *st)
{ {
struct ifaddrs *ifap, *ifa; struct ifaddrs *ifap, *ifa;
bool found = false; bool found = false;
@ -134,10 +137,10 @@ static void fill_iftot(struct iftot *st)
/* /*
* Print out munin plugin configuration * Print out munin plugin configuration
*/ */
int config(char *iface) int
config(char *iface)
{ {
printf( printf("graph_order rbytes obytes\n"
"graph_order rbytes obytes\n"
"graph_title %s Interface (%d seconds sampling)\n" "graph_title %s Interface (%d seconds sampling)\n"
"graph_category network\n" "graph_category network\n"
"graph_vlabel bits per second\n" "graph_vlabel bits per second\n"
@ -153,23 +156,23 @@ int config(char *iface)
"obytes.negative rbytes\n" "obytes.negative rbytes\n"
"obytes.cdef obytes,8,*\n" "obytes.cdef obytes,8,*\n"
"obytes.min 0\n" "obytes.min 0\n"
"obytes.draw AREA\n" "obytes.draw AREA\n", iface, RESOLUTION, RESOLUTION, RESOLUTION);
,iface,RESOLUTION,RESOLUTION,RESOLUTION
);
return(0); return (0);
} }
/* /*
* Wait for a certain amount of time * Wait for a certain amount of time
*/ */
time_t wait_for(int seconds) { time_t
wait_for(int seconds)
{
struct timespec tp; struct timespec tp;
clock_gettime(CLOCK_REALTIME, &tp); clock_gettime(CLOCK_REALTIME, &tp);
time_t current_epoch = tp.tv_sec; time_t current_epoch = tp.tv_sec;
long nsec_to_sleep = 1000*1000*1000 - tp.tv_nsec; long nsec_to_sleep = 1000 * 1000 * 1000 - tp.tv_nsec;
/* Only sleep if needed */ /* Only sleep if needed */
if (nsec_to_sleep > 0) { if (nsec_to_sleep > 0) {
@ -184,7 +187,8 @@ time_t wait_for(int seconds) {
/* /*
* Daemonize and persist pid * Daemonize and persist pid
*/ */
int daemon_start() int
daemon_start()
{ {
struct iftot ift, *tot; struct iftot ift, *tot;
time_t epoch; time_t epoch;
@ -194,14 +198,13 @@ int daemon_start()
tot = &ift; tot = &ift;
char* no_fork = getenv("no_fork"); char *no_fork = getenv("no_fork");
if (! no_fork || strcmp("1", no_fork)) { if (!no_fork || strcmp("1", no_fork)) {
/* Check if parent process id is set */ /* Check if parent process id is set */
if (getppid() == 1) if (getppid() == 1) {
{
/* PPID exists, therefore we are already a daemon */ /* PPID exists, therefore we are already a daemon */
return(EXIT_FAILURE); return (EXIT_FAILURE);
} }
/* Check if we can acquire the pid file */ /* Check if we can acquire the pid file */
@ -209,15 +212,16 @@ int daemon_start()
if (pfh == NULL) { if (pfh == NULL) {
if (errno == EEXIST) { if (errno == EEXIST) {
errx(EXIT_FAILURE, "Daemon already running, pid: %jd.", (intmax_t)otherpid); errx(EXIT_FAILURE,
"Daemon already running, pid: %jd.",
(intmax_t) otherpid);
} }
warn("Cannot open or create pidfile: %s", pid_filename); warn("Cannot open or create pidfile: %s", pid_filename);
} }
/* fork ourselves if not asked otherwise */ /* fork ourselves if not asked otherwise */
if (fork()) if (fork()) {
{ return (EXIT_SUCCESS);
return(EXIT_SUCCESS);
} }
/* we are the child, complete the daemonization */ /* we are the child, complete the daemonization */
@ -229,11 +233,13 @@ int daemon_start()
/* Block unnecessary signals */ /* Block unnecessary signals */
sigemptyset(&sig_set); sigemptyset(&sig_set);
sigaddset(&sig_set, SIGCHLD); /* ignore child - i.e. we don't need to wait for it */ sigaddset(&sig_set, SIGCHLD); /* ignore child - i.e. we don't need
* to wait for it */
sigaddset(&sig_set, SIGTSTP); /* ignore Tty stop signals */ sigaddset(&sig_set, SIGTSTP); /* ignore Tty stop signals */
sigaddset(&sig_set, SIGTTOU); /* ignore Tty background writes */ sigaddset(&sig_set, SIGTTOU); /* ignore Tty background writes */
sigaddset(&sig_set, SIGTTIN); /* ignore Tty background reads */ sigaddset(&sig_set, SIGTTIN); /* ignore Tty background reads */
sigprocmask(SIG_BLOCK, &sig_set, NULL); /* Block the above specified signals */ sigprocmask(SIG_BLOCK, &sig_set, NULL); /* Block the above specified
* signals */
/* Catch necessary signals */ /* Catch necessary signals */
sig_action.sa_handler = signal_handler; sig_action.sa_handler = signal_handler;
@ -255,27 +261,29 @@ int daemon_start()
FILE *cache_file = fopen(cache_filename, "a"); FILE *cache_file = fopen(cache_filename, "a");
/* looping to collect traffic stat every RESOLUTION seconds */ /* looping to collect traffic stat every RESOLUTION seconds */
while(1) { while (1) {
epoch=wait_for(RESOLUTION); epoch = wait_for(RESOLUTION);
flockfile(cache_file); flockfile(cache_file);
fill_iftot(tot); fill_iftot(tot);
fprintf(cache_file, "obytes.value %ld:%lu\nrbytes.value %ld:%lu\n", epoch, tot->ift_ob, epoch, tot->ift_ib); fprintf(cache_file, "obytes.value %ld:%lu\nrbytes.value %ld:%lu\n",
epoch, tot->ift_ob, epoch, tot->ift_ib);
fflush(cache_file); fflush(cache_file);
funlockfile(cache_file); funlockfile(cache_file);
} }
fclose(cache_file); fclose(cache_file);
return(0); return (0);
} }
int fetch() int
fetch()
{ {
/* this should return data from cache file */ /* this should return data from cache file */
FILE* cache_file = fopen(cache_filename, "r+"); FILE *cache_file = fopen(cache_filename, "r+");
/* lock */ /* lock */
flockfile(cache_file); flockfile(cache_file);
@ -289,26 +297,26 @@ int fetch()
ftruncate(fileno(cache_file), 0); ftruncate(fileno(cache_file), 0);
fclose(cache_file); fclose(cache_file);
return(0); return (0);
} }
int main(int argc, char* argv[]) int
main(int argc, char *argv[])
{ {
if (argv[0] && argv[0][0]) if (argv[0] && argv[0][0])
program_name = argv[0]; program_name = argv[0];
/* figure out program name */ /* figure out program name */
while (strchr(program_name, '/')) { while (strchr(program_name, '/')) {
program_name=strchr(program_name, '/')+1; program_name = strchr(program_name, '/') + 1;
} }
/* extract interface name from plugin name */ /* extract interface name from plugin name */
if (strchr(program_name,'_')) { if (strchr(program_name, '_')) {
interface=strchr(program_name,'_')+1; interface = strchr(program_name, '_') + 1;
} }
/* program should always run with a valid /* program should always run with a valid executable name */
executable name */
if (strlen(interface) < 1) { if (strlen(interface) < 1) {
errx(EX_USAGE, "Please run from symlink"); errx(EX_USAGE, "Please run from symlink");
} }
@ -317,18 +325,19 @@ int main(int argc, char* argv[])
char *MUNIN_PLUGSTATE = getenv("MUNIN_PLUGSTATE"); char *MUNIN_PLUGSTATE = getenv("MUNIN_PLUGSTATE");
/* Default is current directory */ /* Default is current directory */
if (!MUNIN_PLUGSTATE) MUNIN_PLUGSTATE = "."; if (!MUNIN_PLUGSTATE)
MUNIN_PLUGSTATE = ".";
asprintf(&pid_filename, "%s/%s.pid", MUNIN_PLUGSTATE, program_name); asprintf(&pid_filename, "%s/%s.pid", MUNIN_PLUGSTATE, program_name);
asprintf(&cache_filename, "%s/%s.value", MUNIN_PLUGSTATE, program_name); asprintf(&cache_filename, "%s/%s.value", MUNIN_PLUGSTATE, program_name);
if (argc > 1) { if (argc > 1) {
char* first_arg = argv[1]; char *first_arg = argv[1];
if (! strcmp(first_arg, "config")) { if (!strcmp(first_arg, "config")) {
return config(interface); return config(interface);
} }
if (! strcmp(first_arg, "acquire")) { if (!strcmp(first_arg, "acquire")) {
return daemon_start(); return daemon_start();
} }
} }