xutils: Add common code to set up struct itimerval interval and value
authorTobias Klauser <tklauser@distanz.ch>
Sat, 2 Feb 2013 15:29:47 +0000 (2 16:29 +0100)
committerTobias Klauser <tklauser@distanz.ch>
Sat, 2 Feb 2013 15:29:47 +0000 (2 16:29 +0100)
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
netsniff-ng.c
trafgen.c
xutils.c
xutils.h

index 41846c5..2a99e43 100644 (file)
@@ -128,24 +128,14 @@ static void signal_handler(int number)
 
 static void timer_elapsed(int unused)
 {
-       itimer.it_interval.tv_sec = 0;
-       itimer.it_interval.tv_usec = interval;
-
-       itimer.it_value.tv_sec = 0;
-       itimer.it_value.tv_usec = interval;
-
+       set_itimer_interval_value(&itimer, 0, interval);
        pull_and_flush_tx_ring(tx_sock);
        setitimer(ITIMER_REAL, &itimer, NULL);
 }
 
 static void timer_next_dump(int unused)
 {
-       itimer.it_interval.tv_sec = interval;
-       itimer.it_interval.tv_usec = 0;
-
-       itimer.it_value.tv_sec = interval;
-       itimer.it_value.tv_usec = 0;
-
+       set_itimer_interval_value(&itimer, 0, interval);
        next_dump = true;
        setitimer(ITIMER_REAL, &itimer, NULL);
 }
@@ -237,12 +227,7 @@ static void pcap_to_xmit(struct ctx *ctx)
        if (ctx->kpull)
                interval = ctx->kpull;
 
-       itimer.it_interval.tv_sec = 0;
-       itimer.it_interval.tv_usec = interval;
-
-       itimer.it_value.tv_sec = 0;
-       itimer.it_value.tv_usec = interval;
-
+       set_itimer_interval_value(&itimer, 0, interval);
        setitimer(ITIMER_REAL, &itimer, NULL); 
 
        drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
@@ -394,12 +379,7 @@ static void receive_to_xmit(struct ctx *ctx)
        if (ctx->kpull)
                interval = ctx->kpull;
 
-       itimer.it_interval.tv_sec = 0;
-       itimer.it_interval.tv_usec = interval;
-
-       itimer.it_value.tv_sec = 0;
-       itimer.it_value.tv_usec = interval;
-
+       set_itimer_interval_value(&itimer, 0, interval);
        setitimer(ITIMER_REAL, &itimer, NULL);
 
        drop_privileges(ctx->enforce, ctx->uid, ctx->gid);
@@ -739,12 +719,7 @@ static int begin_multi_pcap_file(struct ctx *ctx)
        if (ctx->dump_mode == DUMP_INTERVAL_TIME) {
                interval = ctx->dump_interval;
 
-               itimer.it_interval.tv_sec = interval;
-               itimer.it_interval.tv_usec = 0;
-
-               itimer.it_value.tv_sec = interval;
-               itimer.it_value.tv_usec = 0;
-
+               set_itimer_interval_value(&itimer, interval, 0);
                setitimer(ITIMER_REAL, &itimer, NULL);
        } else {
                interval = 0;
index 273a43f..67c9b4e 100644 (file)
--- a/trafgen.c
+++ b/trafgen.c
@@ -143,12 +143,7 @@ static void signal_handler(int number)
 
 static void timer_elapsed(int number)
 {
-       itimer.it_interval.tv_sec = 0;
-       itimer.it_interval.tv_usec = interval;
-
-       itimer.it_value.tv_sec = 0;
-       itimer.it_value.tv_usec = interval;
-
+       set_itimer_interval_value(&itimer, 0, interval);
        pull_and_flush_tx_ring(sock);
        setitimer(ITIMER_REAL, &itimer, NULL); 
 }
@@ -776,12 +771,7 @@ static void xmit_fastpath_or_die(struct ctx *ctx, int cpu)
        if (ctx->num > 0)
                num = ctx->num;
 
-       itimer.it_interval.tv_sec = 0;
-       itimer.it_interval.tv_usec = interval;
-
-       itimer.it_value.tv_sec = 0;
-       itimer.it_value.tv_usec = interval;
-
+       set_itimer_interval_value(&itimer, 0, interval);
        setitimer(ITIMER_REAL, &itimer, NULL); 
 
        bug_on(gettimeofday(&start, NULL));
index b70fca3..23f7e65 100644 (file)
--- a/xutils.c
+++ b/xutils.c
@@ -1143,3 +1143,13 @@ void reset_system_socket_memory(int *vals, size_t len)
        set_system_socket_mem(sock_wmem_max, vals[2]);
        set_system_socket_mem(sock_wmem_def, vals[3]);
 }
+
+void set_itimer_interval_value(struct itimerval *itimer, unsigned long sec,
+                              unsigned long usec)
+{
+       itimer->it_interval.tv_sec = sec;
+       itimer->it_interval.tv_usec = usec;
+
+       itimer->it_value.tv_sec = sec;
+       itimer->it_value.tv_usec = usec;
+}
index 309a196..e9a09a0 100644 (file)
--- a/xutils.h
+++ b/xutils.h
@@ -100,5 +100,7 @@ extern int get_default_proc_prio(void);
 extern void set_system_socket_memory(int *vals, size_t len);
 extern void reset_system_socket_memory(int *vals, size_t len);
 extern struct timeval tv_subtract(struct timeval time1, struct timeval time2);
+extern void set_itimer_interval_value(struct itimerval *itimer, unsigned long sec,
+                                     unsigned long usec);
 
 #endif /* XSYS_H */