Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8002617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:25:01+00:00 2026-06-04T16:25:01+00:00

I am trying to get some message from kernel space to userspace, when a

  • 0

I am trying to get some message from kernel space to userspace, when a condition fails!

Here’s my kernel code:

#define MESSAGTOUSER 1

int ret_val;
struct siginfo sinfo;
pid_t id;
struct task_struct *task;

unsigned char msgBuffer[20];
unsigned char buf1[20]= "HI";

static int major_no;
static struct class *safe_class;

static long device_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
static int device_open(struct inode *inode, struct file *file);
static int device_write(struct file *file, const char *gdata, size_t len, loff_t *off);
static int device_read(struct file *file, char *buf, size_t len, loff_t *off);
static int device_release(struct inode *inode, struct file *file);


int failureDetection (char* faultMsg) {
    strcpy (msgBuffer, faultMsg);
    printk(KERN_ALERT"\nMessage from HBM %s\n", msgBuffer);
    printk(KERN_ALERT".......... RETURN VALUE ...... : %d", ret_val);

    int Reg_Dev(void);
    memset (&sinfo, 0, sizeof(struct siginfo));
    sinfo.si_signo = SIGUSR1;
    sinfo.si_code  = SI_USER;

    if (id == 0) {
        printk("\ncan't find User PID: %d\n", id);
    }else {
    //task = pid_task(find_vpid(pid), PIDTYPE_PID);
    task = find_task_by_vpid(id);
    send_sig_info(SIGUSR1, &sinfo, task);
    }
    return 0;
}

static int device_open(struct inode *inode, struct file *file){
    /*sucess*/
    return 0;
}

void strPrint(void) {
    printk("value of msgBuffer: %s", msgBuffer);
}

static int device_write(struct file *file, const char *gdata, size_t len, loff_t *off){
    get_user (id,(int *)gdata);
    if(id <0)
        printk(KERN_ALERT"Cann't find PID from userspace its : %i", id);
    else
        printk(KERN_ALERT"Successfully received the PID of userspace %i", id);
    return len;
}

static int
device_read(struct file *file, char *buf, size_t len, loff_t *off){
    /*success*/
    return 0;
}

static int device_release(struct inode *inode, struct file *file){
    /*success*/
    return 0;
}


static long device_ioctl(struct file *file, unsigned int cmd, unsigned long arg) {


    switch (cmd) {
        case MESSAGTOUSER:
            ret_val = copy_to_user((char *)arg, msgBuffer, sizeof(arg));
            printk("Msg of Kernel %s", msgBuffer);
            break;
        default:
            break;
    }
    return 0;

}



static struct file_operations fops = {
    .open = device_open,
    .write = device_write,
    .read = device_read,
    .release = device_release,
    .unlocked_ioctl = device_ioctl
};


int Reg_Dev(void) {

    major_no = register_chrdev(0, "safe_dev", &fops);
    safe_class = class_create(THIS_MODULE, "safe_dev");
    device_create(safe_class,NULL, MKDEV(major_no, 0), "safe_dev");
    printk("\n Device Registered and Created \n");
    return 0;
}

void UnReg_dev (void) {
    printk("\nUser PID : %d\n", id);
    unregister_chrdev(major_no, "safe_dev");
    device_destroy(safe_class, MKDEV(major_no,0));
    class_unregister(safe_class);
    class_destroy(safe_class);
    printk("\n Device Un-Registered and Destroyed \n");
}

extern int Reg_Dev(void);

for he userspace i have this code:

#define PORT 9930
#define G_IP "192.168.10.71"
#define BUFLEN 512

#define MESSAGTOUSER 0

unsigned char *str[20];
char b1[BUFLEN], b2[BUFLEN];
struct sockaddr_in me,client;
int s, i, n=sizeof(me);
int fd;



void error_handler(char *s) {
  perror(s);
  exit(1);
}



void signal_handler (int signum) {

  if(signum == SIGUSR1)
  {
    printf("\n%s\n",str);

    if((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))  == -1)
       error_handler("\nERROR: in Socket\n");

    memset((char *) &me, 0, sizeof(me));
    me.sin_family = AF_INET;
    me.sin_port = PORT;

    if (inet_aton(G_IP, &me.sin_addr)==0) 
    {
       fprintf(stderr, "inet_aton() failed\n");
       exit(1);
    }
    printf("Message from Kernel : %s", &str);
    //strcpy (str, newStr);


    int cntr =0;    sprintf(b2, "\nFailure Message: %s\n",str);
    printf("\nsending Fault to PMN Group : Tick - %d\n", cntr++);


    if(sendto(s, str, sizeof(str),0,(struct sockaddr *) &me,n)==-1)
        error_handler("\nERROR: in sendto()\n");
    close (s);
//    counter ++;
 //   sendAndReceiveOverUDP();
    return;
  }
}

int main() {
   pid_t u_id;
   u_id = getpid();
   int i = 1;

   fd = open("/dev/safe_dev",O_RDWR);
   write(fd, &u_id, 4);
   ioctl (fd, MESSAGTOUSER, &str);
   printf("\n PID sent to device successfully: %d \n", u_id);
   close(fd);

   signal(SIGUSR1, signal_handler);
   printf("\nMy PID is: %d\n",u_id);
   //printf("Subnet 1 working fine.. Tick - %d", tv.tv_sec);

   while (1)
   sleep(1);
   return 0;
}

Now what I am expecting to receive on Userspace:

Message from Kernel: A<->B
Sending Fault o PMN Group : tick - 0

Message from Kernel: B<->B
Sending Fault o PMN Group : tick - 1
....
...

but what is the output:

Message from Kernel:
Sending Fault o PMN Group : tick - 0

Message from Kernel:
Sending Fault o PMN Group : tick - 1
....
...

It seems that copy_to_user is not working, while in simple program just copying a string from kernel to user is working fine, but while i am using in this scenario then its not working, its compiling without any warning,

  • Some other Details:
  • failureDetection() is getting a string like A<->B mentioned in output from rest of the programs..
  • the same message from failureDetection is printing on kernel level but not transferring at the user level.
  • I have also tried to create an own string in this and tried to transfer that, but it is also not working! suppose msgBuffer = HI, then I should receive HI on to the userspace. but its not happening! can anyone please please make me correct whats wrong with this code? how can i get updates onto the userspace!!??

Sindhu..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:25:02+00:00Added an answer on June 4, 2026 at 4:25 pm

    The copy_to_user() only happens in response to the ioctl(), which only happens once, very early on in your code. Presumably at that point the kernel buffer msgBuffer is empty, because the failureDetection() function has not yet run at that point. It doesn’t matter if failureDetection() runs later and sets msgBuffer then, because your userspace program never calls the ioctl() again so it doesn’t see the new contents of msgBuffer.

    You also have a bug in your copy_to_user() call – instead of sizeof(args) (which is a constant 4) you should probably use sizeof msgBuffer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get some form data from POST method. Here's the code
I am trying to get some code working from an example I came across.
I'm trying to code some basic kernel module - userspace program communication using netlink
Hey, I'm trying to get an extension to wait for a xml message from
I'm trying to get some json data from a remote website. I run my
I'm brand new to Prolog. I am simply trying to get some output from
I'm trying to write some code that uses boost::asio's sockets to send a message
I got this error while trying to get facebook access_token from node.js server: {error:{message:(#803)
Im trying to get some checkbox with a specific name document.getElementsByName(test); Unfortunatley i cant
Just trying to get some rows out of a database and loop through but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.