Solution Timeline

All versions (edits) of solutions to Keystroke counter daemon appear below in the order they were created. Comments that appear under revisions were those created when that particular revision was current.

To see the revision history of a single solution (with diffs), click on the solution number (ie. "#1") in the upper right corner of a solution revision below.

← Bounty Expand all edits

/*
* compile with -Wall -std=c99?
* run with root privilegies
*
* Options:
* -t iteration_time, defaul 60 sec
* -f logfile_name, default /etc/keystrokes.log
* -d path-to-device, default /dev/input/event0
*
*/

include <stdio.h>

include <stdlib.h>

include <linux/input.h>

include <fcntl.h>

include <string.h>

include <unistd.h>

include <time.h>

include <sys/types.h>

include <sys/stat.h>

int main(int argc, char const *argv[])
{
pid_t pid, sid;
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);

    if (pid &gt; 0)
            exit(EXIT_SUCCESS);
    umask(0);

    int itertime = 60; 
    char logname[256] = &quot;/etc/keystrokes.log&quot;; 
    char devicename[100] = &quot;/dev/input/event0&quot;;
    enum modes {MFILE = 0, MTIME, MDEV, MHELP};
    int mode = -1;
    for (int i = 1; i &lt; argc; ++i){
        if (!strcmp (argv[i], &quot;-f&quot;))
            {mode = MFILE;  continue;}
        if (!strcmp (argv[i], &quot;-t&quot;))
            {mode = MTIME; continue;}
        if (!strcmp (argv[i], &quot;-d&quot;))
            {mode = MDEV; continue;}
        if (!strcmp (argv[i], &quot;-h&quot;)) 
            {mode = MHELP;}

        switch (mode){
            case MFILE:
                strcpy(logname, argv[i]);
                mode = -1;
                break;
            case MTIME:
                itertime = atoi(argv[i]);
                mode = -1;
                break;
            case MDEV:
                strcpy(devicename, argv[i]);
                mode = -1;
                break;
            default:
                printf(&quot;Options:\n-t time_interval (default 60sec)\n&quot;
                            &quot;-f log_filename (default /etc/keystroked/keystrokes.log\n&quot;
                            &quot;-d device (default /dev/input/event0, will prob work on Arch Linux)\n&quot;
                            &quot;NOTE! Should be run with root privilegies\n&quot;);
                mode = -1;
            break;
        }
    }
    printf(&quot;ok&quot;);       
    int device = open(devicename, O_RDONLY);

    if (device &lt; 0)
        exit(EXIT_FAILURE);

    long flag = fcntl(device, F_GETFL, 0);
    fcntl(device, F_SETFL, flag | O_NONBLOCK);

    sid = setsid();
    if (sid &lt; 0)
            exit(EXIT_FAILURE);

    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);

    time_t start, end;
    struct input_event ev;
    unsigned long counter = 0;  
    struct tm *loc;
    char buf[100];

    start = time(NULL);
    loc = localtime(&amp;start);
    strftime(buf, sizeof(char)*100 - 1, &quot;%F %T&quot;, loc);
    FILE *logfile = fopen(logname, &quot;a&quot;);
    fprintf(logfile, &quot;Started %s...\n&quot;, buf);
    fclose(logfile);

    while (1) {
        end = time(NULL);
        if (difftime(end, start) &lt; itertime){ 
            int c = read(device, &amp;ev, sizeof(struct input_event));
            if (c &gt;= 0 &amp;&amp; ev.type == 1 &amp;&amp; ev.value == 1)
                ++counter;
       } else {
            loc = localtime(&amp;end);
            strftime(buf, sizeof(char)*100 - 1, &quot;%F %T&quot;, loc);
            logfile = fopen(logname, &quot;a&quot;);

            fprintf(logfile, &quot;keyboard %s %ld\n&quot;, buf, counter);
            fclose(logfile);
            counter = 0;
            start = time(NULL);         
       }
    }


    close(device);


    exit(EXIT_SUCCESS);

}

/*
* compile with -Wall -std=c99?
* run with root privilegies
*
* Options:
* -t iteration_time, defaul 60 sec
* -f logfile_name, default /etc/keystrokes.log
* -d path-to-device, default /dev/input/event0
*
*/

include

include

include

include

include

include

include

include

include

int main(int argc, char const *argv[])
{
pid_t pid, sid;
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);

    if (pid > 0)
            exit(EXIT_SUCCESS);
    umask(0);

    int itertime = 60; 
    char logname[256] = "/etc/keystrokes.log"; 
    char devicename[100] = "/dev/input/event0";
    enum modes {MFILE = 0, MTIME, MDEV, MHELP};
    int mode = -1;
    for (int i = 1; i < argc; ++i){
        if (!strcmp (argv[i], "-f"))
            {mode = MFILE;  continue;}
        if (!strcmp (argv[i], "-t"))
            {mode = MTIME; continue;}
        if (!strcmp (argv[i], "-d"))
            {mode = MDEV; continue;}
        if (!strcmp (argv[i], "-h")) 
            {mode = MHELP;}

        switch (mode){
            case MFILE:
                strcpy(logname, argv[i]);
                mode = -1;
                break;
            case MTIME:
                itertime = atoi(argv[i]);
                mode = -1;
                break;
            case MDEV:
                strcpy(devicename, argv[i]);
                mode = -1;
                break;
            default:
                printf("Options:\n-t time_interval (default 60sec)\n"
                            "-f log_filename (default /etc/keystroked/keystrokes.log\n"
                            "-d device (default /dev/input/event0, will prob work on Arch Linux)\n"
                            "NOTE! Should be run with root privilegies\n");
                mode = -1;
            break;
        }
    }
    printf("ok");       
    int device = open(devicename, O_RDONLY);

    if (device < 0)
        exit(EXIT_FAILURE);

    long flag = fcntl(device, F_GETFL, 0);
    fcntl(device, F_SETFL, flag | O_NONBLOCK);

    sid = setsid();
    if (sid < 0)
            exit(EXIT_FAILURE);

    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);

    time_t start, end;
    struct input_event ev;
    unsigned long counter = 0;  
    struct tm *loc;
    char buf[100];

    start = time(NULL);
    loc = localtime(&start);
    strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);
    FILE *logfile = fopen(logname, "a");
    fprintf(logfile, "Started %s...\n", buf);
    fclose(logfile);

    while (1) {
        end = time(NULL);
        if (difftime(end, start) < itertime){ 
            int c = read(device, &ev, sizeof(struct input_event));
            if (c >= 0 && ev.type == 1 && ev.value == 1)
                ++counter;
       } else {
            loc = localtime(&end);
            strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);
            logfile = fopen(logname, "a");

            fprintf(logfile, "keyboard %s %ld\n", buf, counter);
            fclose(logfile);
            counter = 0;
            start = time(NULL);         
       }
    }


    close(device);


    exit(EXIT_SUCCESS);

}

Compile with -Wall -std=c99, run with root privilegies.
Options:
-t iteration_time, defaul 60 sec
-f logfile_name, default /etc/keystrokes.log
-d path-to-device, default /dev/input/event0 (should work in Arch Linux)

include

include

include

include

include

include

include

include

include

int main(int argc, char const *argv[])
{
pid_t pid, sid;
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);

    if (pid > 0)
            exit(EXIT_SUCCESS);
    umask(0);

    int itertime = 60; 
    char logname[256] = "/etc/keystrokes.log"; 
    char devicename[100] = "/dev/input/event0";
    enum modes {MFILE = 0, MTIME, MDEV, MHELP};
    int mode = -1;
    for (int i = 1; i < argc; ++i){
        if (!strcmp (argv[i], "-f"))
            {mode = MFILE;  continue;}
        if (!strcmp (argv[i], "-t"))
            {mode = MTIME; continue;}
        if (!strcmp (argv[i], "-d"))
            {mode = MDEV; continue;}
        if (!strcmp (argv[i], "-h")) 
            {mode = MHELP;}

        switch (mode){
            case MFILE:
                strcpy(logname, argv[i]);
                mode = -1;
                break;
            case MTIME:
                itertime = atoi(argv[i]);
                mode = -1;
                break;
            case MDEV:
                strcpy(devicename, argv[i]);
                mode = -1;
                break;
            default:
                printf("Options:\n-t time_interval (default 60sec)\n"
                            "-f log_filename (default /etc/keystroked/keystrokes.log\n"
                            "-d device (default /dev/input/event0, will prob work on Arch Linux)\n"
                            "NOTE! Should be run with root privilegies\n");
                mode = -1;
            break;
        }
    }
    printf("ok");       
    int device = open(devicename, O_RDONLY);

    if (device < 0)
        exit(EXIT_FAILURE);

    long flag = fcntl(device, F_GETFL, 0);
    fcntl(device, F_SETFL, flag | O_NONBLOCK);

    sid = setsid();
    if (sid < 0)
            exit(EXIT_FAILURE);

    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);

    time_t start, end;
    struct input_event ev;
    unsigned long counter = 0;  
    struct tm *loc;
    char buf[100];

    start = time(NULL);
    loc = localtime(&start);
    strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);
    FILE *logfile = fopen(logname, "a");
    fprintf(logfile, "Started %s...\n", buf);
    fclose(logfile);

    while (1) {
        end = time(NULL);
        if (difftime(end, start) < itertime){ 
            int c = read(device, &ev, sizeof(struct input_event));
            if (c >= 0 && ev.type == 1 && ev.value == 1)
                ++counter;
       } else {
            loc = localtime(&end);
            strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);
            logfile = fopen(logname, "a");

            fprintf(logfile, "keyboard %s %ld\n", buf, counter);
            fclose(logfile);
            counter = 0;
            start = time(NULL);         
       }
    }


    close(device);


    exit(EXIT_SUCCESS);

}

Compile with -Wall -std=c99, run with root privilegies.
Options:
-t iteration_time, defaul 60 sec
-f logfile_name, default /etc/keystrokes.log
-d path-to-device, default /dev/input/event0 (should work in Arch Linux)

include

include

include

include

include

include

include

include

include

int main(int argc, char const *argv[])
{
pid_t pid, sid;
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);
if (pid > 0)
exit(EXIT_SUCCESS);
umask(0);

    int itertime = 60; 
    char logname[256] = "/etc/keystrokes.log"; 
    char devicename[100] = "/dev/input/event0";
    enum modes {MFILE = 0, MTIME, MDEV, MHELP};
    int mode = -1;
    for (int i = 1; i < argc; ++i){
        if (!strcmp (argv[i], "-f"))
            {mode = MFILE;  continue;}
        if (!strcmp (argv[i], "-t"))
            {mode = MTIME; continue;}
        if (!strcmp (argv[i], "-d"))
            {mode = MDEV; continue;}
        if (!strcmp (argv[i], "-h")) 
            {mode = MHELP;}

        switch (mode){
            case MFILE:
                strcpy(logname, argv[i]);
                mode = -1;
                break;
            case MTIME:
                itertime = atoi(argv[i]);
                mode = -1;
                break;
            case MDEV:
                strcpy(devicename, argv[i]);
                mode = -1;
                break;
            default:
                printf("Options:\n-t time_interval (default 60sec)\n"
                            "-f log_filename (default /etc/keystroked/keystrokes.log\n"
                            "-d device (default /dev/input/event0, will prob work on Arch Linux)\n"
                            "NOTE! Should be run with root privilegies\n");
                mode = -1;
            break;
        }
    }
    printf("ok");       
    int device = open(devicename, O_RDONLY);

    if (device < 0)
        exit(EXIT_FAILURE);

    long flag = fcntl(device, F_GETFL, 0);
    fcntl(device, F_SETFL, flag | O_NONBLOCK);

    sid = setsid();
    if (sid < 0)
            exit(EXIT_FAILURE);

    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);

    time_t start, end;
    struct input_event ev;
    unsigned long counter = 0;  
    struct tm *loc;
    char buf[100];

    start = time(NULL);
    loc = localtime(&start);
    strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);
    FILE *logfile = fopen(logname, "a");
    fprintf(logfile, "Started %s...\n", buf);
    fclose(logfile);

    while (1) {
        end = time(NULL);
        if (difftime(end, start) < itertime){ 
            int c = read(device, &ev, sizeof(struct input_event));
            if (c >= 0 && ev.type == 1 && ev.value == 1)
                ++counter;
       } else {
            loc = localtime(&end);
            strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);
            logfile = fopen(logname, "a");

            fprintf(logfile, "keyboard %s %ld\n", buf, counter);
            fclose(logfile);
            counter = 0;
            start = time(NULL);         
       }
    }


    close(device);


    exit(EXIT_SUCCESS);

}

Here's my solution, it was tested In Arch Linux.
Code:

/*
* compile with -Wall -std=c99?
* run with root privilegies
*
* Options:
* -t iteration_time, defaul 60 sec
* -f logfile_name, default /etc/keystrokes.log
* -d path-to-device, default /dev/input/event0
*
*/

include "stdio.h"

include "stdlib.h"

include "linux/input.h"

include "fcntl.h"

include "string.h"

include "unistd.h"

include "time.h"

include "sys/types.h"

include "sys/stat.h"

int main(int argc, char const *argv[])
{
pid_t pid, sid;
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);

    if (pid > 0)
            exit(EXIT_SUCCESS);
    umask(0);

    int itertime = 60; 
    char logname[256] = "/etc/keystrokes.log"; 
    char devicename[100] = "/dev/input/event0";
    enum modes {MFILE = 0, MTIME, MDEV, MHELP};
    int mode = -1;
    for (int i = 1; i < argc; ++i){
        if (!strcmp (argv[i], "-f"))
            {mode = MFILE;  continue;}
        if (!strcmp (argv[i], "-t"))
            {mode = MTIME; continue;}
        if (!strcmp (argv[i], "-d"))
            {mode = MDEV; continue;}
        if (!strcmp (argv[i], "-h")) 
            {mode = MHELP;}

        switch (mode){
            case MFILE:
                strcpy(logname, argv[i]);
                mode = -1;
                break;
            case MTIME:
                itertime = atoi(argv[i]);
                mode = -1;
                break;
            case MDEV:
                strcpy(devicename, argv[i]);
                mode = -1;
                break;
            default:
                printf("Options:\n-t time_interval (default 60sec)\n"
                            "-f log_filename (default /etc/keystroked/keystrokes.log\n"
                            "-d device (default /dev/input/event0, will prob work on Arch Linux)\n"
                            "NOTE! Should be run with root privilegies\n");
                mode = -1;
            break;
        }
    }
    printf("ok");       
    int device = open(devicename, O_RDONLY);

    if (device < 0)
        exit(EXIT_FAILURE);

    long flag = fcntl(device, F_GETFL, 0);
    fcntl(device, F_SETFL, flag | O_NONBLOCK);

    sid = setsid();
    if (sid < 0)
            exit(EXIT_FAILURE);

    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);

    time_t start, end;
    struct input_event ev;
    unsigned long counter = 0;  
    struct tm *loc;
    char buf[100];

    start = time(NULL);
    loc = localtime(&start);
    strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);
    FILE *logfile = fopen(logname, "a");
    fprintf(logfile, "Started %s...\n", buf);
    fclose(logfile);

    while (1) {
        end = time(NULL);
        if (difftime(end, start) < itertime){ 
            int c = read(device, &ev, sizeof(struct input_event));
            if (c >= 0 && ev.type == 1 && ev.value == 1)
                ++counter;
       } else {
            loc = localtime(&end);
            strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);
            logfile = fopen(logname, "a");

            fprintf(logfile, "keyboard %s %ld\n", buf, counter);
            fclose(logfile);
            counter = 0;
            start = time(NULL);         
       }
    }


    close(device);


    exit(EXIT_SUCCESS);

}

Here's my solution, it was tested In Arch Linux.
Code:

/*
* compile with -Wall -std=c99?
* run with root privilegies
*
* Options:
* -t iteration_time, defaul 60 sec
* -f logfile_name, default /etc/keystrokes.log
* -d path-to-device, default /dev/input/event0
*
*/

include "stdio.h"

include "stdlib.h"

include "linux/input.h"

include "fcntl.h"

include "string.h"

include "unistd.h"

include "time.h"

include "sys/types.h"

include "sys/stat.h"

int main(int argc, char const *argv[])
{
pid_t pid, sid;
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);
if (pid > 0)
exit(EXIT_SUCCESS);
umask(0);

    int itertime = 60; 
    char logname[256] = "/etc/keystrokes.log"; 
    char devicename[100] = "/dev/input/event0";
    enum modes {MFILE = 0, MTIME, MDEV, MHELP};
    int mode = -1;
    for (int i = 1; i < argc; ++i){
        if (!strcmp (argv[i], "-f"))
            {mode = MFILE;  continue;}
        if (!strcmp (argv[i], "-t"))
            {mode = MTIME; continue;}
        if (!strcmp (argv[i], "-d"))
            {mode = MDEV; continue;}
        if (!strcmp (argv[i], "-h")) 
            {mode = MHELP;}

        switch (mode){
            case MFILE:
                strcpy(logname, argv[i]);
                mode = -1;
                break;
            case MTIME:
                itertime = atoi(argv[i]);
                mode = -1;
                break;
            case MDEV:
                strcpy(devicename, argv[i]);
                mode = -1;
                break;
            default:
                printf("Options:\n-t time_interval (default 60sec)\n"
                            "-f log_filename (default /etc/keystroked/keystrokes.log\n"
                            "-d device (default /dev/input/event0, will prob work on Arch Linux)\n"
                            "NOTE! Should be run with root privilegies\n");
                mode = -1;
            break;
        }
    }
    printf("ok");       
    int device = open(devicename, O_RDONLY);

    if (device < 0)
        exit(EXIT_FAILURE);

    long flag = fcntl(device, F_GETFL, 0);
    fcntl(device, F_SETFL, flag | O_NONBLOCK);

    sid = setsid();
    if (sid < 0)
            exit(EXIT_FAILURE);

    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);

    time_t start, end;
    struct input_event ev;
    unsigned long counter = 0;  
    struct tm *loc;
    char buf[100];

    start = time(NULL);
    loc = localtime(&start);
    strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);
    FILE *logfile = fopen(logname, "a");
    fprintf(logfile, "Started %s...\n", buf);
    fclose(logfile);

    while (1) {
        end = time(NULL);
        if (difftime(end, start) < itertime){ 
            int c = read(device, &ev, sizeof(struct input_event));
            if (c >= 0 && ev.type == 1 && ev.value == 1)
                ++counter;
       } else {
            loc = localtime(&end);
            strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);
            logfile = fopen(logname, "a");

            fprintf(logfile, "keyboard %s %ld\n", buf, counter);
            fclose(logfile);
            counter = 0;
            start = time(NULL);         
       }
    }


    close(device);


    exit(EXIT_SUCCESS);

}

Here's my solution, it was tested In Arch Linux.
Code:

include "stdio.h"

include "stdlib.h"

include "linux/input.h"

include "fcntl.h"

include "string.h"

include "unistd.h"

include "time.h"

include "sys/types.h"

include "sys/stat.h"

int main(int argc, char const *argv[])
{
pid_t pid, sid;
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);
if (pid > 0)
exit(EXIT_SUCCESS);
umask(0);

    int itertime = 60; 
    char logname[256] = "/etc/keystrokes.log"; 
    char devicename[100] = "/dev/input/event0";
    enum modes {MFILE = 0, MTIME, MDEV, MHELP};
    int mode = -1;
    for (int i = 1; i < argc; ++i){
        if (!strcmp (argv[i], "-f"))
            {mode = MFILE;  continue;}
        if (!strcmp (argv[i], "-t"))
            {mode = MTIME; continue;}
        if (!strcmp (argv[i], "-d"))
            {mode = MDEV; continue;}
        if (!strcmp (argv[i], "-h")) 
            {mode = MHELP;}

        switch (mode){
            case MFILE:
                strcpy(logname, argv[i]);
                mode = -1;
                break;
            case MTIME:
                itertime = atoi(argv[i]);
                mode = -1;
                break;
            case MDEV:
                strcpy(devicename, argv[i]);
                mode = -1;
                break;
            default:
                printf("Options:\n-t time_interval (default 60sec)\n"
                            "-f log_filename (default /etc/keystroked/keystrokes.log\n"
                            "-d device (default /dev/input/event0, will prob work on Arch Linux)\n"
                            "NOTE! Should be run with root privilegies\n");
                mode = -1;
            break;
        }
    }
    printf("ok");       
    int device = open(devicename, O_RDONLY);

    if (device < 0)
        exit(EXIT_FAILURE);

    long flag = fcntl(device, F_GETFL, 0);
    fcntl(device, F_SETFL, flag | O_NONBLOCK);

    sid = setsid();
    if (sid < 0)
            exit(EXIT_FAILURE);

    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);

    time_t start, end;
    struct input_event ev;
    unsigned long counter = 0;  
    struct tm *loc;
    char buf[100];

    start = time(NULL);
    loc = localtime(&start);
    strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);
    FILE *logfile = fopen(logname, "a");
    fprintf(logfile, "Started %s...\n", buf);
    fclose(logfile);

    while (1) {
        end = time(NULL);
        if (difftime(end, start) < itertime){ 
            int c = read(device, &ev, sizeof(struct input_event));
            if (c >= 0 && ev.type == 1 && ev.value == 1)
                ++counter;
       } else {
            loc = localtime(&end);
            strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);
            logfile = fopen(logname, "a");

            fprintf(logfile, "keyboard %s %ld\n", buf, counter);
            fclose(logfile);
            counter = 0;
            start = time(NULL);         
       }
    }


    close(device);


    exit(EXIT_SUCCESS);

}

Here's my solution, it was tested In Arch Linux.
Code:
/*
* compile with -Wall -std=c99?
* run with root privilegies
*
* Options:
* -t iteration_time, defaul 60 sec
* -f logfile_name, default /etc/keystrokes.log
* -d path-to-device, default /dev/input/event0
*
*/

include <stdio.h>

include <stdlib.h>

include <linux/input.h>

include <fcntl.h>

include <string.h>

include <unistd.h>

include <time.h>

include <sys/types.h>

include <sys/stat.h>

int main(int argc, char const *argv[])
{
pid_t pid, sid;
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);

    if (pid &gt; 0)
            exit(EXIT_SUCCESS);
    umask(0);

    int itertime = 60; 
    char logname[256] = &quot;/etc/keystrokes.log&quot;; 
    char devicename[100] = &quot;/dev/input/event0&quot;;
    enum modes {MFILE = 0, MTIME, MDEV, MHELP};
    int mode = -1;
    for (int i = 1; i &lt; argc; ++i){
        if (!strcmp (argv[i], &quot;-f&quot;))
            {mode = MFILE;  continue;}
        if (!strcmp (argv[i], &quot;-t&quot;))
            {mode = MTIME; continue;}
        if (!strcmp (argv[i], &quot;-d&quot;))
            {mode = MDEV; continue;}
        if (!strcmp (argv[i], &quot;-h&quot;)) 
            {mode = MHELP;}

        switch (mode){
            case MFILE:
                strcpy(logname, argv[i]);
                mode = -1;
                break;
            case MTIME:
                itertime = atoi(argv[i]);
                mode = -1;
                break;
            case MDEV:
                strcpy(devicename, argv[i]);
                mode = -1;
                break;
            default:
                printf(&quot;Options:\n-t time_interval (default 60sec)\n&quot;
                            &quot;-f log_filename (default /etc/keystroked/keystrokes.log\n&quot;
                            &quot;-d device (default /dev/input/event0, will prob work on Arch Linux)\n&quot;
                            &quot;NOTE! Should be run with root privilegies\n&quot;);
                mode = -1;
            break;
        }
    }
    printf(&quot;ok&quot;);       
    int device = open(devicename, O_RDONLY);

    if (device &lt; 0)
        exit(EXIT_FAILURE);

    long flag = fcntl(device, F_GETFL, 0);
    fcntl(device, F_SETFL, flag | O_NONBLOCK);

    sid = setsid();
    if (sid &lt; 0)
            exit(EXIT_FAILURE);

    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);

    time_t start, end;
    struct input_event ev;
    unsigned long counter = 0;  
    struct tm *loc;
    char buf[100];

    start = time(NULL);
    loc = localtime(&amp;start);
    strftime(buf, sizeof(char)*100 - 1, &quot;%F %T&quot;, loc);
    FILE *logfile = fopen(logname, &quot;a&quot;);
    fprintf(logfile, &quot;Started %s...\n&quot;, buf);
    fclose(logfile);

    while (1) {
        end = time(NULL);
        if (difftime(end, start) &lt; itertime){ 
            int c = read(device, &amp;ev, sizeof(struct input_event));
            if (c &gt;= 0 &amp;&amp; ev.type == 1 &amp;&amp; ev.value == 1)
                ++counter;
       } else {
            loc = localtime(&amp;end);
            strftime(buf, sizeof(char)*100 - 1, &quot;%F %T&quot;, loc);
            logfile = fopen(logname, &quot;a&quot;);

            fprintf(logfile, &quot;keyboard %s %ld\n&quot;, buf, counter);
            fclose(logfile);
            counter = 0;
            start = time(NULL);         
       }
    }


    close(device);


    exit(EXIT_SUCCESS);

}

Here is my solution, tested on Arch Linux
Source:
`/*
* compile with -Wall -std=c99
* run with root privilegies
*
* Options:
* -t iteration_time, defaul 60 sec
* -f logfile_name, default /etc/keystrokes.log
* -d path-to-device, default /dev/input/event0
*
*/

include

include

include

include

include

include

include

include

include

int main(int argc, char const *argv[])
{
pid_t pid, sid;
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);

    if (pid > 0)
            exit(EXIT_SUCCESS);
    umask(0);

    int itertime = 60; 
    char logname[256] = "/etc/keystrokes.log"; 
    char devicename[100] = "/dev/input/event0";
    enum modes {MFILE = 0, MTIME, MDEV, MHELP};
    int mode = -1;
    for (int i = 1; i < argc; ++i){
        if (!strcmp (argv[i], "-f"))
            {mode = MFILE;  continue;}
        if (!strcmp (argv[i], "-t"))
            {mode = MTIME; continue;}
        if (!strcmp (argv[i], "-d"))
            {mode = MDEV; continue;}
        if (!strcmp (argv[i], "-h")) 
            {mode = MHELP;}

        switch (mode){
            case MFILE:
                strcpy(logname, argv[i]);
                mode = -1;
                break;
            case MTIME:
                itertime = atoi(argv[i]);
                mode = -1;
                break;
            case MDEV:
                strcpy(devicename, argv[i]);
                mode = -1;
                break;
            default:
                printf("Options:\n-t time_interval (default 60sec)\n"
                            "-f log_filename (default /etc/keystroked/keystrokes.log\n"
                            "-d device (default /dev/input/event0, will prob work on Arch Linux)\n"
                            "NOTE! Should be run with root privilegies\n");
                mode = -1;
            break;
        }
    }
    printf("ok");       
    int device = open(devicename, O_RDONLY);

    if (device < 0)
        exit(EXIT_FAILURE);

    long flag = fcntl(device, F_GETFL, 0);
    fcntl(device, F_SETFL, flag | O_NONBLOCK);

    sid = setsid();
    if (sid < 0)
            exit(EXIT_FAILURE);

    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);

    time_t start, end;
    struct input_event ev;
    unsigned long counter = 0;  
    struct tm *loc;
    char buf[100];

    start = time(NULL);
    loc = localtime(&start);
    strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);
    FILE *logfile = fopen(logname, "a");
    fprintf(logfile, "Started %s...\n", buf);
    fclose(logfile);

    while (1) {
        end = time(NULL);
        if (difftime(end, start) < itertime){ 
            int c = read(device, &ev, sizeof(struct input_event));
            if (c >= 0 && ev.type == 1 && ev.value == 1)
                ++counter;
       } else {
            loc = localtime(&end);
            strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);
            logfile = fopen(logname, "a");

            fprintf(logfile, "keyboard %s %ld\n", buf, counter);
            fclose(logfile);
            counter = 0;
            start = time(NULL);         
       }
    }


    close(device);


    exit(EXIT_SUCCESS);

}`

Here is my solution, tested on Arch Linux
Source:
/*
* compile with -Wall -std=c99
* run with root privilegies
*
* Options:
* -t iteration_time, defaul 60 sec
* -f logfile_name, default /etc/keystrokes.log
* -d path-to-device, default /dev/input/event0
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <linux/input.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, char const *argv[])
{
        pid_t pid, sid;
        pid = fork();
        if (pid < 0) 
                exit(EXIT_FAILURE);

        if (pid > 0)
                exit(EXIT_SUCCESS);
        umask(0);

        int itertime = 60; 
        char logname[256] = "/etc/keystrokes.log"; 
        char devicename[100] = "/dev/input/event0";
        enum modes {MFILE = 0, MTIME, MDEV, MHELP};
        int mode = -1;
        for (int i = 1; i < argc; ++i){
            if (!strcmp (argv[i], "-f"))
                {mode = MFILE;  continue;}
            if (!strcmp (argv[i], "-t"))
                {mode = MTIME; continue;}
            if (!strcmp (argv[i], "-d"))
                {mode = MDEV; continue;}
            if (!strcmp (argv[i], "-h")) 
                {mode = MHELP;}

            switch (mode){
                case MFILE:
                    strcpy(logname, argv[i]);
                    mode = -1;
                    break;
                case MTIME:
                    itertime = atoi(argv[i]);
                    mode = -1;
                    break;
                case MDEV:
                    strcpy(devicename, argv[i]);
                    mode = -1;
                    break;
                default:
                    printf("Options:\n-t time_interval (default 60sec)\n"
                                "-f log_filename (default /etc/keystroked/keystrokes.log\n"
                                "-d device (default /dev/input/event0, will prob work on Arch Linux)\n"
                                "NOTE! Should be run with root privilegies\n");
                    mode = -1;
                break;
            }
        }
        printf("ok");       
        int device = open(devicename, O_RDONLY);

        if (device < 0)
            exit(EXIT_FAILURE);

        long flag = fcntl(device, F_GETFL, 0);
        fcntl(device, F_SETFL, flag | O_NONBLOCK);

        sid = setsid();
        if (sid < 0)
                exit(EXIT_FAILURE);

        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);

        time_t start, end;
        struct input_event ev;
        unsigned long counter = 0;  
        struct tm *loc;
        char buf[100];

        start = time(NULL);
        loc = localtime(&start);
        strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);
        FILE *logfile = fopen(logname, "a");
        fprintf(logfile, "Started %s...\n", buf);
        fclose(logfile);

        while (1) {
            end = time(NULL);
            if (difftime(end, start) < itertime){ 
                int c = read(device, &ev, sizeof(struct input_event));
                if (c >= 0 && ev.type == 1 && ev.value == 1)
                    ++counter;
           } else {
                loc = localtime(&end);
                strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);
                logfile = fopen(logname, "a");

                fprintf(logfile, "keyboard %s %ld\n", buf, counter);
                fclose(logfile);
                counter = 0;
                start = time(NULL);         
           }
        }


        close(device);


        exit(EXIT_SUCCESS);
}    

Here is my solution, tested on Arch Linux

Compile it with -Wall -std=c99, run as root.
Options: -t time_interval : (default 60sec)

-f log_file : (default /etc/keystrokes.log)

-d device : (default /dev/input/event0, should work)

-h : show help

Source:

#include <stdio.h>
#include <stdlib.h>
#include <linux/input.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, char const *argv[])
{
        pid_t pid, sid;
        pid = fork();
        if (pid < 0) 
                exit(EXIT_FAILURE);

        if (pid > 0)
                exit(EXIT_SUCCESS);
        umask(0);

        int itertime = 60; 
        char logname[256] = "/etc/keystrokes.log"; 
        char devicename[100] = "/dev/input/event0";
        enum modes {MFILE = 0, MTIME, MDEV, MHELP};
        int mode = -1;
        for (int i = 1; i < argc; ++i){
            if (!strcmp (argv[i], "-f"))
                {mode = MFILE;  continue;}
            if (!strcmp (argv[i], "-t"))
                {mode = MTIME; continue;}
            if (!strcmp (argv[i], "-d"))
                {mode = MDEV; continue;}
            if (!strcmp (argv[i], "-h")) 
                {mode = MHELP;}

            switch (mode){
                case MFILE:
                    strcpy(logname, argv[i]);
                    mode = -1;
                    break;
                case MTIME:
                    itertime = atoi(argv[i]);
                    mode = -1;
                    break;
                case MDEV:
                    strcpy(devicename, argv[i]);
                    mode = -1;
                    break;
                default:
                    printf("Options:\n-t time_interval (default 60sec)\n"
                                "-f log_filename (default /etc/keystroked/keystrokes.log\n"
                                "-d device (default /dev/input/event0, will prob work on Arch Linux)\n"
                                "NOTE! Should be run with root privilegies\n");
                    mode = -1;
                break;
            }
        }
        printf("ok");       
        int device = open(devicename, O_RDONLY);

        if (device < 0)
            exit(EXIT_FAILURE);

        long flag = fcntl(device, F_GETFL, 0);
        fcntl(device, F_SETFL, flag | O_NONBLOCK);

        sid = setsid();
        if (sid < 0)
                exit(EXIT_FAILURE);

        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);

        time_t start, end;
        struct input_event ev;
        unsigned long counter = 0;  
        struct tm *loc;
        char buf[100];

        start = time(NULL);
        loc = localtime(&start);
        strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);
        FILE *logfile = fopen(logname, "a");
        fprintf(logfile, "Started %s...\n", buf);
        fclose(logfile);

        while (1) {
            end = time(NULL);
            if (difftime(end, start) < itertime){ 
                int c = read(device, &ev, sizeof(struct input_event));
                if (c >= 0 && ev.type == 1 && ev.value == 1)
                    ++counter;
           } else {
                loc = localtime(&end);
                strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);
                logfile = fopen(logname, "a");

                fprintf(logfile, "keyboard %s %ld\n", buf, counter);
                fclose(logfile);
                counter = 0;
                start = time(NULL);         
           }
        }


        close(device);


        exit(EXIT_SUCCESS);
}    

Here is my solution, tested on Arch Linux

Compile it with -Wall -std=c99, run as root.
Options: -t time_interval : (default 60sec)

-f log_file : (default /etc/keystrokes.log)

-d device : (default /dev/input/event0, should work)

-h : show help

Source:

#include <stdio.h>
#include <stdlib.h>
#include <linux/input.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, char const *argv[])
{
    pid_t pid, sid;
    pid = fork();
    if (pid < 0) 
            exit(EXIT_FAILURE);
     if (pid > 0)
            exit(EXIT_SUCCESS);
    umask(0);

    int itertime = 60; 
    char logname[256] = "/etc/keystrokes.log"; 
    char devicename[100] = "/dev/input/event0";

    for (int i = 1; i < argc; ++i){
        if (!strcmp (argv[i], "-f")){
            strcpy(logname, argv[++i]);
            continue;
        }

        if (!strcmp (argv[i], "-t")){
            itertime = atoi(argv[++i]);
            continue;
        }

        if (!strcmp (argv[i], "-d")){
            strcpy(devicename, argv[++i]);
            continue;                
        }

        if (!strcmp(argv[i], "-h")){    
            printf("Options:\n-t time_interval (default 60sec)\n"
                    "-f log_filename (default /etc/keystroked/keystrokes.log\n"
                    "-d device (default /dev/input/event0, will prob work on Arch Linux)\n"
                    "NOTE! Should be run with root privilegies\n");
            exit(EXIT_SUCCESS);
        }
    }   
    int device = open(devicename, O_RDONLY);

    if (device < 0)
        exit(EXIT_FAILURE);

    long flag = fcntl(device, F_GETFL, 0);
    fcntl(device, F_SETFL, flag | O_NONBLOCK);

    sid = setsid();
    if (sid < 0)
            exit(EXIT_FAILURE);

    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);

    time_t start, end;
    struct input_event ev;
    unsigned long counter = 0;  
    struct tm *loc;
    char buf[100];

    start = time(NULL);
    loc = localtime(&start);
    strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);

    FILE *logfile = fopen(logname, "a");
    fprintf(logfile, "Started %s...\n", buf);
    fclose(logfile);

    while (1) {
        end = time(NULL);
        if (difftime(end, start) < itertime){ 

            int c = read(device, &ev, sizeof(struct input_event));
            if (c >= 0 && ev.type == 1 && ev.value == 1)
                ++counter;

       } else {
            loc = localtime(&end);
            strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);

            logfile = fopen(logname, "a");
            fprintf(logfile, "keyboard %s %ld\n", buf, counter);
            fclose(logfile);

            counter = 0;
            start = time(NULL);         
       }
    }
    close(device);
    exit(EXIT_SUCCESS);
}    

Here is my solution, tested on Arch Linux

Compile with -std=c99, run as root.
Options: -t time_interval : (default 60sec)

-f log_file : (default /etc/keystrokes.log)

-d device : (default /dev/input/event0, should work)

-h : show help

http://www1.datafilehost.com/d/1928a7bf - .tar.gz archive with precompiled ArchLinux package for i686 and sources with PKGBUILD to rebuild it manually.

Source:

#include <stdio.h>
#include <stdlib.h>
#include <linux/input.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, char const *argv[])
{
    pid_t pid, sid;
    pid = fork();
    if (pid < 0) 
            exit(EXIT_FAILURE);
     if (pid > 0)
            exit(EXIT_SUCCESS);
    umask(0);

    int itertime = 60; 
    char logname[256] = "/etc/keystrokes.log"; 
    char devicename[100] = "/dev/input/event0";

    for (int i = 1; i < argc; ++i){
        if (!strcmp (argv[i], "-f")){
            strcpy(logname, argv[++i]);
            continue;
        }

        if (!strcmp (argv[i], "-t")){
            itertime = atoi(argv[++i]);
            continue;
        }

        if (!strcmp (argv[i], "-d")){
            strcpy(devicename, argv[++i]);
            continue;                
        }

        if (!strcmp(argv[i], "-h")){    
            printf("Options:\n-t time_interval (default 60sec)\n"
                    "-f log_filename (default /etc/keystroked/keystrokes.log\n"
                    "-d device (default /dev/input/event0, will prob work on Arch Linux)\n"
                    "NOTE! Should be run with root privilegies\n");
            exit(EXIT_SUCCESS);
        }
    }   
    int device = open(devicename, O_RDONLY);

    if (device < 0)
        exit(EXIT_FAILURE);

    long flag = fcntl(device, F_GETFL, 0);
    fcntl(device, F_SETFL, flag | O_NONBLOCK);

    sid = setsid();
    if (sid < 0)
            exit(EXIT_FAILURE);

    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);

    time_t start, end;
    struct input_event ev;
    unsigned long counter = 0;  
    struct tm *loc;
    char buf[100];

    start = time(NULL);
    loc = localtime(&start);
    strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);

    FILE *logfile = fopen(logname, "a");
    fprintf(logfile, "Started %s...\n", buf);
    fclose(logfile);

    while (1) {
        end = time(NULL);
        if (difftime(end, start) < itertime){ 

            int c = read(device, &ev, sizeof(struct input_event));
            if (c >= 0 && ev.type == 1 && ev.value == 1)
                ++counter;

       } else {
            loc = localtime(&end);
            strftime(buf, sizeof(char)*100 - 1, "%F %T", loc);

            logfile = fopen(logname, "a");
            fprintf(logfile, "keyboard %s %ld\n", buf, counter);
            fclose(logfile);

            counter = 0;
            start = time(NULL);         
       }
    }
    close(device);
    exit(EXIT_SUCCESS);
}    
Winning solution Tipped