From f8929ed1507bb4fbcedb5f89b68c49db36cefd20 Mon Sep 17 00:00:00 2001 From: Gerard Wagener Date: Tue, 12 Jan 2010 16:35:11 +0100 Subject: [PATCH] Tried to split up the code --- arch/um/include/shared/aha.h | 8 ++- arch/um/kernel/aha.c | 109 +++++++++++++++++++---------------- 2 files changed, 66 insertions(+), 51 deletions(-) diff --git a/arch/um/include/shared/aha.h b/arch/um/include/shared/aha.h index 83176904da9..22638b7657a 100644 --- a/arch/um/include/shared/aha.h +++ b/arch/um/include/shared/aha.h @@ -1,5 +1,7 @@ #ifndef AHA #define AHA + +#define AHA_DEBUG #include "linux/kernel.h" /* printk is declared there */ //#include "linux/gfp.h" /* GFP_KERNEL */ @@ -25,7 +27,11 @@ struct ReplyMessage{ int substitue; int insult; }; - +#ifdef AHA_DEBUG + #define AHA_PRINTK(args...) printk(args) +#else + #define AHA_PRINTK(...) +#endif int aha_create_filename(char *fn, int size); char* aha_dump_execve(char __user *file, char __user *__user *argv,\ char __user *__user *env); diff --git a/arch/um/kernel/aha.c b/arch/um/kernel/aha.c index 0ddda02eaf0..1de29ccfb91 100644 --- a/arch/um/kernel/aha.c +++ b/arch/um/kernel/aha.c @@ -19,8 +19,35 @@ int aha_create_filename(char *fn, int size) return snprintf(fn,size,"AHA_%lx.dat",ncycles); } -/* - * Tansfers the file names and arguments to the host OS +inline void __aha_os_write_file_ck(int fd, char* buf, int cnt) +{ + if ((cnt > 0) & (cnt < MAX_DUMP_BUF)){ + os_write_file(fd,buf,cnt); + } +} + +/* Log PIDs and PPID */ +inline void __aha_dump_pid_ppids(int fd,char* buf,int cnt) +{ + struct task_struct *tsk; + tsk = current; + cnt = snprintf(buf,MAX_DUMP_BUF,"pid=%d\n",tsk->pid); + __aha_os_write_file_ck(fd,buf,cnt); + cnt = snprintf(buf,MAX_DUMP_BUF,"ppid=%d\n",tsk->parent->pid); + __aha_os_write_file_ck(fd,buf,cnt); + cnt = snprintf(buf,MAX_DUMP_BUF,"rppid=%d\n",tsk->real_parent->pid); + __aha_os_write_file_ck(fd,buf,cnt); +} + +inline void __aha_set_done_tag(int fd, char* buf,int cnt) +{ + /* FIXME the MAGIC word is not escaped it could emerge as argument */ + cnt = snprintf(buf,cnt,"DONE=1\n"); + __aha_os_write_file_ck(fd,buf,cnt); + +} + + /* Tansfers the file names and arguments to the host OS * The transfer via files is an good awfull solution. * The dumping is done in a best effort manner. If it succeds * to write all the data the tag / line DONE is at the end of the @@ -32,70 +59,52 @@ char* aha_dump_execve(char __user *file, char __user *__user *argv, char __user *__user *env) { char *p, *a, *q, *r; - struct openflags flg; int mode = 0644; int fd,cnt; - struct task_struct *tsk; + struct openflags flg; + r = NULL; flg.w = 1; flg.c = 1; - cnt = 0; - r = NULL; + + /* Allocate memory once to win time */ p = kmalloc(MAX_DUMP_BUF,GFP_KERNEL); q = kmalloc(MAX_DUMP_BUF, GFP_KERNEL); r = kmalloc(MAX_DUMP_BUF,GFP_KERNEL); - if (p && q && r) { - if (aha_create_filename(r,MAX_DUMP_BUF)<0) - return NULL; + if (!(p && q && r)) + return NULL; + if (aha_create_filename(r,MAX_DUMP_BUF)<0) + return NULL; /* Go into output queue */ - cnt=snprintf(p,MAX_DUMP_BUF,"out/%s",r); - if ((cnt<0) | (cnt>MAX_DUMP_BUF)) - return NULL; - if ((fd = os_open_file(p,flg,mode))<0) - return NULL; + cnt=snprintf(p,MAX_DUMP_BUF,"out/%s",r); + if ((cnt<0) | (cnt>MAX_DUMP_BUF)) + return NULL; + if ((fd = os_open_file(p,flg,mode))<0) + return NULL; - /* Dump the file from execve */ - if (strncpy_from_user(p,file,MAX_DUMP_BUF) > 0){ - cnt = snprintf((char*)q,MAX_DUMP_BUF,"file=%s\n",p); - if ((cnt>0) & (cnt < MAX_DUMP_BUF)) - os_write_file(fd,q,cnt); - - } - /* Dump the arguments */ - for (;;) { - if (get_user(a,argv)) - break; + /* Dump the file from execve */ + if (strncpy_from_user(p,file,MAX_DUMP_BUF) > 0){ + cnt = snprintf((char*)q,MAX_DUMP_BUF,"file=%s\n",p); + __aha_os_write_file_ck(fd,q,cnt); + } + /* Dump the arguments */ + for (;;) { + if (get_user(a,argv)) + break; if (!a) break; if (strncpy_from_user(p,a, MAX_DUMP_BUF) > 0) { cnt=snprintf(q,MAX_DUMP_BUF,"argument=%s\n",p); - if ((cnt>0) & (cntpid); - if ((cnt>0) & (cntparent->pid); - if ((cnt>0) & (cntreal_parent->pid); - if ((cnt>0) & (cnt0) & (cnt < MAX_DUMP_BUF)) - os_write_file(fd,q,cnt); - os_close_file(fd); - kfree(p); - kfree(q); } - return r; + __aha_dump_pid_ppids(fd,q,cnt); + __aha_set_done_tag(fd,q,cnt); + os_close_file(fd); + kfree(p); + kfree(q); + + return r; /* Return the filename that was created */ } void aha_handle_insult_messages(struct ReplyMessage *msg, char __user* file,