I am trying to execute a bash script in the user space using the call_usermodehelper function like this.
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
#include<linux/kmod.h>
#include <linux/moduleparam.h>
MODULE_LICENSE( "GPL" );
static char *mystring = "blah";
module_param(mystring, charp, 0000);
MODULE_PARM_DESC(mystring, "A character string");
static int __init hello_start(void) {
printk(KERN_INFO "Loading rooted module...\n");
pr_info("mystring is a string: %s\n", mystring);
char* argv[] = {"/bin/bash", "home/grim-reaper/kernel/FocusModule/focusmyman.sh", NULL};
static char* envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/bin:/usr/sbin:/usr/bin", NULL };
call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
return 0;
}
static void __exit hello_end(void){
printk(KERN_INFO "exit.\n");
char* argv[] = {"/bin/bash", "home/grim-reaper/script-remove.sh", NULL};
static char* envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/bin:/usr/sbin:/usr/bin", NULL };
call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
}
module_init(hello_start);
module_exit(hello_end);
The shell script is as follows:
#!/bin/bash
if [ -f ~/kernel/FocusModule/focus.txt ]; then
echo "File exists and is a regular file"
else
touch focus.txt
fi
while true; do
active_window=$(xdotool getwindowfocus getwindowname)
echo "$active_window" >> ~/kernel/FocusModule/focus.txt
sleep 5
done
This script works perfectly when executed in the terminal, but it simply doesn't start execution when called from the module. The module compilation and dmesg
show no errors. I'm at a loss at what's wrong. Please help. I've used an identical module for the execution of another script, which has just a chmod
command, and it works perfectly.