Re: passing array from kernel space to user space

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





On 4/6/06, Vincenzo Mallozzi <vinjunior@xxxxxxxx> wrote:
I've thinked  solution like this (similar to the one
taha has suggested):

static ssize_t read(struct file *file, char __user
*buf, size_t count,
loff_t *pos){
   int element_size =  sizeof(char)/ sizeof(pid_t) /*
To get the
array size for pids*/
   int char_array_size = count / element_size;
   char *char_array = (char*) pid_array;

   /* .... This is your array ... fill what ever you
want to fill in
*/
}


but I've a doubt.
the array i had to pass to the user space is a array
of string,

why strings ? the array contains pids, right ? so why not just passing them up as an array of pid_t members ?

static ssize_t read(struct file *file, char __user
*buf, size_t count,
loff_t *pos){
        if (count % sizeof(pid_t))
                return -EINVAL;

        /* the first element in pids_array is the count of pids */
        pids_array_size = (pids_array[0] + 1) * sizeof(pid_t);
        ret_count = (count >= pids_array_size ? pids_array_size : count);
        if (copy_to_user(buf, pids_array, ret_count))
                return -EFAULT;
        return ret_count;
}

while the user_space code could be:
pid_t user_buf[MAX_PIDS_COUNT + 1];
read(your_dev_fd, user_buf, MAX_PIDS_COUNT + 1);

but you've to store the count of your pids in the first element of the pid_t array in kernel .. i.e. element user_buf[0] will be your pids count and pids will start from index 1
this helps you handling the case when you don't know the maximum pids count, then you can read the first element first (that will be the count), allocate sufficient user buffer, and then read the whole array again

pid_t pid_count;
read(dev_fd, &pid_count, sizeof(pid_t));
size = (pid_count + 1) * sizeof(pid_t);
buf = malloc(size);
read(dev_fd, buf, size);

hope this helps

MHD.Tayseer



therefore of char pointers. So the solution
i've wrote above is sufficient?

thanks.
Vincenzo MAllozzi



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux