Find the answer to your Linux question:
Results 1 to 2 of 2
Hi. I'm trying to copy a struct from the a kernel module to userland. I can copy a struct from userland to the module doing this in the user program: ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2006
    Location
    Ireland
    Posts
    9

    Thumbs down Copying a struct from kernel(module) to userland using /proc, how?


    Hi.

    I'm trying to copy a struct from the a kernel module to userland. I can copy a struct from userland to the module doing this in the user program:

    void write_procfile() /* Test for struct copy */
    {
    FILE *fp;
    int i;
    if((fp = fopen("/proc/nf_lb", "wb")) == NULL)
    {
    printf("Error opening /proc/nf_lb for writing.\n");
    exit(1);
    } /* open /proc/nf_lb for writing */

    if((fwrite(&input, sizeof (struct user_input), 1, fp)) != 1)
    {
    printf("An error has occured. Please make sure to specify correct options.\n");
    }
    fclose(fp); /* close the file */
    }
    and this in the kernel(note, theres a function called automatically when the proc file is written to):

    /* write data to the buffer*/
    if (copy_from_user((char *) &input, buffer, sizeof(struct user_input)))
    {
    return -EFAULT;
    }
    But, I do not know how to send a struct from the module to userland...
    Any ideas? I suppose I'm looking for a function like userlands fwrite().

    Thanks for any help,
    Cormac Redmond

  2. #2
    Linux User fernape's Avatar
    Join Date
    Dec 2005
    Location
    Holland
    Posts
    301
    Did you try with copy_to_user? That appears to be the standar way to write data from kernel space to user space. At least it works with plain text.

    Best regards!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •