[SOLVED] Kernel Module and Userspace communication Signals - SIGUSR1
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Kernel Module and Userspace communication Signals - SIGUSR1
Hello Everybody,
I am a newbie and wrote this piece of code, I am expecting to receive a message on user space after inserting kernel Module.. But I am not able to receive that message, anybody can please make me correct?
Isn't this the signature of the send_sig_info function?
Code:
int send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
Is your kernel module compiling without any warnings?
Thank you Anisha..
I told that i am a newbie, yes I am receiving some warnings also:
Quote:
warning: passing argument 2 of ‘send_sig_info’ makes pointer from integer without a cast
expected ‘struct siginfo *’ but argument is of type ‘int’
warning: passing argument 3 of ‘send_sig_info’ makes pointer from integer without a cast
note: expected ‘struct task_struct *’ but argument is of type ‘int’
but i didn't noticed until it was compiling...
but after replacing:
Quote:
int send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
I am still not able to get required output..
Can you please help me to make this program workable? Please...
task = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);
status = send_sig_info(SIGUSR1, &info, task);
//send_sig_info(10, 4478, 1); /* 4478 is pid i am getting after running userspace program */
warning: ISO C90 forbids mixed declarations and code
Actually I never worked with C language and neither in kernel programing this is first time i just have a little piece of work in which i am stuck. I really need a help, because this is piece of work is hard for me.
actual code is a big Kernel Module implemented in for network communication implemented by someone else, it has multiple files and algorithms implemented. I just have to produce signals on 4 different places and to send those signals on userspace if some conditions occurs..
So thats why i am trying some codes for kernel and userspace communication. but unfortunately i didn't find any working code yet which can report to userspace in some conditions. If you can provide a fully working code for linux kernel and userspace communication Then it will be great help..
Would you not need to allocate memory to this pointer
before storing something in it?
Is this the correct way of assignment to the task_struct?
Did you try to insmod the module? What was the result?
Quote:
Originally Posted by sindhu4sind
Actually I never worked with C language
The word never sounds horrible in this case.
You need to know C for any sort of Kernel programming.
Would you not need to allocate memory to this pointer
before storing something in it?
Is this the correct way of assignment to the task_struct?
Did you try to insmod the module? What was the result?
The word never sounds horrible in this case.
You need to know C for any sort of Kernel programming.
Quote:
Originally Posted by pan64
what is going on with init_pid_ns? What is returned by find_pid_ns?
pid_task returns a valid pointer, we do not need to allocate memory.
Thank you so much Anisha Kaul & pan64
I have achieved what i wanted to achieve, here is my cod:
KERNEL CODE
Quote:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/siginfo.h>
#include <linux/rcupdate.h>
#include <linux/sched.h>
#include <linux/uaccess.h>
#include <linux/signal.h>
MODULE_LICENSE ("GPL");
MODULE_AUTHOR (“Raheel A. Memon<raheel@mju.ac.kr>”);
MODULE_DESCRIPTION (“Testing signals from kernel to userspace”);
struct siginfo sinfo;
pid_t pid;
struct task_struct *task;
int init_module () {
memset(&sinfo, 0, sizeof(struct siginfo));
sinfo.si_signo = SIGIO;
sinfo.si_code = SI_USER;
pid = 5218; // Everytime a new PID
//task = find_task_by_vpid(pid); // I am also working on new and old version of UBUNTU so thats why this is here
task = pid_task(find_vpid(pid), PIDTYPE_PID);
printk("%d .\n", task);
if(task == NULL) {
printk("Cannot find PID from user program\r\n");
return 0;
}
send_sig_info(SIGIO, &sinfo, task);
return 0;
}
void cleanup_module () {
printk(KERN_ALERT"\nGoodBye World\n\n");
}
Userspace Program:
Quote:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
void signal_handler (int signum){
if (signum == SIGIO) printf ("SIGIO\r\n"); return;
}
int main () {
int i=1 ;
signal(SIGIO, signal_handler);
printf("My PID is %d.\n",getpid());
while (i);
return 0;
}
This is working fine, in my condition what i have needed to do.
But i have 2 more questions:
1. What if I want to transfer a Message from kernel to userspace
2. here I am assigning everytime new PID to my program, what should i do that kernel can get PID from userspace by itself.
I am very thankful for your support.
I am looking for your response you both are really nice that supported me though i was not speaking so technically, neither my programs was so much technical.
never mind, I'm glad you could make it.
in general a process can store its own pid in a file and you can identify the program by that (you can find such files for example in /var/run).
Happy with solution ... mark as SOLVED
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.