tannhauser's solution to "Keystroke counter daemon"

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

include <stdio.h><\/h1>\n\n

include <stdlib.h><\/h1>\n\n

include <linux/input.h><\/h1>\n\n

include <fcntl.h><\/h1>\n\n

include <string.h><\/h1>\n\n

include <unistd.h><\/h1>\n\n

include <time.h><\/h1>\n\n

include <sys/types.h><\/h1>\n\n

include <sys/stat.h><\/h1>\n\n

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

    <\/span>if<\/span> <\/span>(<\/span>pid<\/span> <\/span>&<\/span>gt<\/span>;<\/span> <\/span>0<\/span>)<\/span>\n            <\/span>exit<\/span>(<\/span>EXIT_SUCCESS<\/span>);<\/span>\n    <\/span>umask<\/span>(<\/span>0<\/span>);<\/span>\n\n    <\/span>int<\/span> <\/span>itertime<\/span> <\/span>=<\/span> <\/span>60<\/span>;<\/span> <\/span>\n    <\/span>char<\/span> <\/span>logname<\/span>[<\/span>256<\/span>]<\/span> <\/span>=<\/span> <\/span>&<\/span>quot<\/span>;<\/span>/<\/span>etc<\/span>/<\/span>keystrokes<\/span>.<\/span>log&quot<\/span>;;<\/span> <\/span>\n    <\/span>char<\/span> <\/span>devicename<\/span>[<\/span>100<\/span>]<\/span> <\/span>=<\/span> <\/span>&<\/span>quot<\/span>;<\/span>/<\/span>dev<\/span>/<\/span>input<\/span>/<\/span>event0&quot<\/span>;;<\/span>\n    <\/span>enum<\/span> <\/span>modes<\/span> <\/span>{<\/span>MFILE<\/span> <\/span>=<\/span> <\/span>0<\/span>,<\/span> <\/span>MTIME<\/span>,<\/span> <\/span>MDEV<\/span>,<\/span> <\/span>MHELP<\/span>};<\/span>\n    <\/span>int<\/span> <\/span>mode<\/span> <\/span>=<\/span> <\/span>-1<\/span>;<\/span>\n    <\/span>for<\/span> <\/span>(<\/span>int<\/span> <\/span>i<\/span> <\/span>=<\/span> <\/span>1<\/span>;<\/span> <\/span>i<\/span> <\/span>&<\/span>lt<\/span>;<\/span> <\/span>argc<\/span>;<\/span> <\/span>++<\/span>i<\/span>){<\/span>\n        <\/span>if<\/span> <\/span>(<\/span>!<\/span>strcmp<\/span> <\/span>(<\/span>argv<\/span>[<\/span>i<\/span>],<\/span> <\/span>&<\/span>quot<\/span>;<\/span>-<\/span>f&quot<\/span>;))<\/span>\n            <\/span>{<\/span>mode<\/span> <\/span>=<\/span> <\/span>MFILE<\/span>;<\/span>  <\/span>continue<\/span>;}<\/span>\n        <\/span>if<\/span> <\/span>(<\/span>!<\/span>strcmp<\/span> <\/span>(<\/span>argv<\/span>[<\/span>i<\/span>],<\/span> <\/span>&<\/span>quot<\/span>;<\/span>-<\/span>t&quot<\/span>;))<\/span>\n            <\/span>{<\/span>mode<\/span> <\/span>=<\/span> <\/span>MTIME<\/span>;<\/span> <\/span>continue<\/span>;}<\/span>\n        <\/span>if<\/span> <\/span>(<\/span>!<\/span>strcmp<\/span> <\/span>(<\/span>argv<\/span>[<\/span>i<\/span>],<\/span> <\/span>&<\/span>quot<\/span>;<\/span>-<\/span>d&quot<\/span>;))<\/span>\n            <\/span>{<\/span>mode<\/span> <\/span>=<\/span> <\/span>MDEV<\/span>;<\/span> <\/span>continue<\/span>;}<\/span>\n        <\/span>if<\/span> <\/span>(<\/span>!<\/span>strcmp<\/span> <\/span>(<\/span>argv<\/span>[<\/span>i<\/span>],<\/span> <\/span>&<\/span>quot<\/span>;<\/span>-<\/span>h&quot<\/span>;))<\/span> <\/span>\n            <\/span>{<\/span>mode<\/span> <\/span>=<\/span> <\/span>MHELP<\/span>;}<\/span>\n\n        <\/span>switch<\/span> <\/span>(<\/span>mode<\/span>){<\/span>\n            <\/span>case<\/span> <\/span>MFILE:<\/span>\n                <\/span>strcpy<\/span>(<\/span>logname<\/span>,<\/span> <\/span>argv<\/span>[<\/span>i<\/span>]);<\/span>\n                <\/span>mode<\/span> <\/span>=<\/span> <\/span>-1<\/span>;<\/span>\n                <\/span>break<\/span>;<\/span>\n            <\/span>case<\/span> <\/span>MTIME:<\/span>\n                <\/span>itertime<\/span> <\/span>=<\/span> <\/span>atoi<\/span>(<\/span>argv<\/span>[<\/span>i<\/span>]);<\/span>\n                <\/span>mode<\/span> <\/span>=<\/span> <\/span>-1<\/span>;<\/span>\n                <\/span>break<\/span>;<\/span>\n            <\/span>case<\/span> <\/span>MDEV:<\/span>\n                <\/span>strcpy<\/span>(<\/span>devicename<\/span>,<\/span> <\/span>argv<\/span>[<\/span>i<\/span>]);<\/span>\n                <\/span>mode<\/span> <\/span>=<\/span> <\/span>-1<\/span>;<\/span>\n                <\/span>break<\/span>;<\/span>\n            <\/span>default:<\/span>\n                <\/span>printf<\/span>(<\/span>&<\/span>quot<\/span>;<\/span>Options:<\/span>\\<\/span>n<\/span>-<\/span>t<\/span> <\/span>time_interval<\/span> <\/span>(<\/span>default<\/span> <\/span>60<\/span>sec<\/span>)<\/span>\\<\/span>n&quot<\/span>;<\/span>\n                            <\/span>&<\/span>quot<\/span>;<\/span>-<\/span>f<\/span> <\/span>log_filename<\/span> <\/span>(<\/span>default<\/span> <\/span>/<\/span>etc<\/span>/<\/span>keystroked<\/span>/<\/span>keystrokes<\/span>.<\/span>log<\/span>\\<\/span>n&quot<\/span>;<\/span>\n                            <\/span>&<\/span>quot<\/span>;<\/span>-<\/span>d<\/span> <\/span>device<\/span> <\/span>(<\/span>default<\/span> <\/span>/<\/span>dev<\/span>/<\/span>input<\/span>/<\/span>event0<\/span>,<\/span> <\/span>will<\/span> <\/span>prob<\/span> <\/span>work<\/span> <\/span>on<\/span> <\/span>Arch<\/span> <\/span>Linux<\/span>)<\/span>\\<\/span>n&quot<\/span>;<\/span>\n                            <\/span>&<\/span>quot<\/span>;<\/span>NOTE!<\/span> <\/span>Should<\/span> <\/span>be<\/span> <\/span>run<\/span> <\/span>with<\/span> <\/span>root<\/span> <\/span>privilegies<\/span>\\<\/span>n&quot<\/span>;);<\/span>\n                <\/span>mode<\/span> <\/span>=<\/span> <\/span>-1<\/span>;<\/span>\n            <\/span>break<\/span>;<\/span>\n        <\/span>}<\/span>\n    <\/span>}<\/span>\n    <\/span>printf<\/span>(<\/span>&<\/span>quot<\/span>;<\/span>ok&quot<\/span>;);<\/span>       <\/span>\n    <\/span>int<\/span> <\/span>device<\/span> <\/span>=<\/span> <\/span>open<\/span>(<\/span>devicename<\/span>,<\/span> <\/span>O_RDONLY<\/span>);<\/span>\n\n    <\/span>if<\/span> <\/span>(<\/span>device<\/span> <\/span>&<\/span>lt<\/span>;<\/span> <\/span>0<\/span>)<\/span>\n        <\/span>exit<\/span>(<\/span>EXIT_FAILURE<\/span>);<\/span>\n\n    <\/span>long<\/span> <\/span>flag<\/span> <\/span>=<\/span> <\/span>fcntl<\/span>(<\/span>device<\/span>,<\/span> <\/span>F_GETFL<\/span>,<\/span> <\/span>0<\/span>);<\/span>\n    <\/span>fcntl<\/span>(<\/span>device<\/span>,<\/span> <\/span>F_SETFL<\/span>,<\/span> <\/span>flag<\/span> <\/span>|<\/span> <\/span>O_NONBLOCK<\/span>);<\/span>\n\n    <\/span>sid<\/span> <\/span>=<\/span> <\/span>setsid<\/span>();<\/span>\n    <\/span>if<\/span> <\/span>(<\/span>sid<\/span> <\/span>&<\/span>lt<\/span>;<\/span> <\/span>0<\/span>)<\/span>\n            <\/span>exit<\/span>(<\/span>EXIT_FAILURE<\/span>);<\/span>\n\n    <\/span>close<\/span>(<\/span>STDIN_FILENO<\/span>);<\/span>\n    <\/span>close<\/span>(<\/span>STDOUT_FILENO<\/span>);<\/span>\n    <\/span>close<\/span>(<\/span>STDERR_FILENO<\/span>);<\/span>\n\n    <\/span>time_t<\/span> <\/span>start<\/span>,<\/span> <\/span>end<\/span>;<\/span>\n    <\/span>struct<\/span> <\/span>input_event<\/span> <\/span>ev<\/span>;<\/span>\n    <\/span>unsigned<\/span> <\/span>long<\/span> <\/span>counter<\/span> <\/span>=<\/span> <\/span>0<\/span>;<\/span>  <\/span>\n    <\/span>struct<\/span> <\/span>tm<\/span> <\/span>*<\/span>loc<\/span>;<\/span>\n    <\/span>char<\/span> <\/span>buf<\/span>[<\/span>100<\/span>];<\/span>\n\n    <\/span>start<\/span> <\/span>=<\/span> <\/span>time<\/span>(<\/span>NULL<\/span>);<\/span>\n    <\/span>loc<\/span> <\/span>=<\/span> <\/span>localtime<\/span>(<\/span>&<\/span>amp<\/span>;<\/span>start<\/span>);<\/span>\n    <\/span>strftime<\/span>(<\/span>buf<\/span>,<\/span> <\/span>sizeof<\/span>(<\/span>char<\/span>)<\/span>*<\/span>100<\/span> <\/span>-<\/span> <\/span>1<\/span>,<\/span> <\/span>&<\/span>quot<\/span>;<\/span>%<\/span>F<\/span> <\/span>%<\/span>T&quot<\/span>;,<\/span> <\/span>loc<\/span>);<\/span>\n    <\/span>FILE<\/span> <\/span>*<\/span>logfile<\/span> <\/span>=<\/span> <\/span>fopen<\/span>(<\/span>logname<\/span>,<\/span> <\/span>&<\/span>quot<\/span>;<\/span>a&quot<\/span>;);<\/span>\n    <\/span>fprintf<\/span>(<\/span>logfile<\/span>,<\/span> <\/span>&<\/span>quot<\/span>;<\/span>Started<\/span> <\/span>%<\/span>s<\/span>...\\<\/span>n&quot<\/span>;,<\/span> <\/span>buf<\/span>);<\/span>\n    <\/span>fclose<\/span>(<\/span>logfile<\/span>);<\/span>\n\n    <\/span>while<\/span> <\/span>(<\/span>1<\/span>)<\/span> <\/span>{<\/span>\n        <\/span>end<\/span> <\/span>=<\/span> <\/span>time<\/span>(<\/span>NULL<\/span>);<\/span>\n        <\/span>if<\/span> <\/span>(<\/span>difftime<\/span>(<\/span>end<\/span>,<\/span> <\/span>start<\/span>)<\/span> <\/span>&<\/span>lt<\/span>;<\/span> <\/span>itertime<\/span>){<\/span> <\/span>\n            <\/span>int<\/span> <\/span>c<\/span> <\/span>=<\/span> <\/span>read<\/span>(<\/span>device<\/span>,<\/span> <\/span>&<\/span>amp<\/span>;<\/span>ev<\/span>,<\/span> <\/span>sizeof<\/span>(<\/span>struct<\/span> <\/span>input_event<\/span>));<\/span>\n            <\/span>if<\/span> <\/span>(<\/span>c<\/span> <\/span>&<\/span>gt<\/span>;<\/span>=<\/span> <\/span>0<\/span> <\/span>&<\/span>amp<\/span>;<\/span>&<\/span>amp<\/span>;<\/span> <\/span>ev<\/span>.<\/span>type<\/span> <\/span>==<\/span> <\/span>1<\/span> <\/span>&<\/span>amp<\/span>;<\/span>&<\/span>amp<\/span>;<\/span> <\/span>ev<\/span>.<\/span>value<\/span> <\/span>==<\/span> <\/span>1<\/span>)<\/span>\n                <\/span>++<\/span>counter<\/span>;<\/span>\n       <\/span>}<\/span> <\/span>else<\/span> <\/span>{<\/span>\n            <\/span>loc<\/span> <\/span>=<\/span> <\/span>localtime<\/span>(<\/span>&<\/span>amp<\/span>;<\/span>end<\/span>);<\/span>\n            <\/span>strftime<\/span>(<\/span>buf<\/span>,<\/span> <\/span>sizeof<\/span>(<\/span>char<\/span>)<\/span>*<\/span>100<\/span> <\/span>-<\/span> <\/span>1<\/span>,<\/span> <\/span>&<\/span>quot<\/span>;<\/span>%<\/span>F<\/span> <\/span>%<\/span>T&quot<\/span>;,<\/span> <\/span>loc<\/span>);<\/span>\n            <\/span>logfile<\/span> <\/span>=<\/span> <\/span>fopen<\/span>(<\/span>logname<\/span>,<\/span> <\/span>&<\/span>quot<\/span>;<\/span>a&quot<\/span>;);<\/span>\n\n            <\/span>fprintf<\/span>(<\/span>logfile<\/span>,<\/span> <\/span>&<\/span>quot<\/span>;<\/span>keyboard<\/span> <\/span>%<\/span>s<\/span> <\/span>%<\/span>ld<\/span>\\<\/span>n&quot<\/span>;,<\/span> <\/span>buf<\/span>,<\/span> <\/span>counter<\/span>);<\/span>\n            <\/span>fclose<\/span>(<\/span>logfile<\/span>);<\/span>\n            <\/span>counter<\/span> <\/span>=<\/span> <\/span>0<\/span>;<\/span>\n            <\/span>start<\/span> <\/span>=<\/span> <\/span>time<\/span>(<\/span>NULL<\/span>);<\/span>         <\/span>\n       <\/span>}<\/span>\n    <\/span>}<\/span>\n\n\n    <\/span>close<\/span>(<\/span>device<\/span>);<\/span>\n\n\n    <\/span>exit<\/span>(<\/span>EXIT_SUCCESS<\/span>);<\/span>\n<\/pre><\/div>\n

}<\/p>\n

/*
*   /*
*   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(&quot;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, &quot;%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(&amp;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);
}
); }
/* * compileCompile 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 (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) <precode> #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); 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); } precode>
CompileHere'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 (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);

        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);
}
codepre>
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);
        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:
/*
*   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 (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(&quot;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, &quot;%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(&amp;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 is 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 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" "NOTE! Should" "-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(&quot;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, &quot;%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(&amp;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 > #> #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_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); 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: /* * 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 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 #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_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); 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 #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"; 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 -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 #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"; 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); }

User: tannhauser

Question: Keystroke counter daemon

Back to question