On Thu, 9 Aug 2007, Matt Bailey wrote:

> I'm trying to use ddi_copyin to copy a string from a user application to a 
> psuedo driver in kernel space.
>
> How do I copy data and print it out?  Anyone know of any resources I can look 
> at with an example?
>
> Here's the ioctl code:
>
> char *strPtr, *string;
> ...
>                case STR_COPY:
>                        ddi_copyin((char *)arg, strPtr, sizeof (char*), mode);
>                        strcpy(string, strPtr); // EXPLODES HERE

Of course it explodes. You've been copying in a "char *", not an array of 
characters terminated by a zero-valued byte.

>                       break;
> ...
>
> User code:
>
>                        ioctl(fd, STR_COPY, stringPtr);

Such things are a big problem, since your kernel driver doesn't know the 
length of the data. It's not passed on.

The only solution I can see to your request is to do the copyin() byte by 
byte, and return errors on failure to do so:

        char *uaddr, *curaddr, lastbyte;

        case STR_COPY:
                uaddr = (char *)arg;
                curaddr = strPtr;
                memset(strPtr, -1, SIZE_DRIVER_BUFFER);

                while (ddi_copyin(uaddr++, curaddr, 1, mode) == 0 &&
                    *(curaddr++) != '\0');

                /*
                 * Check if we overflowed the source or target buffers
                 */
                if (*curaddr)
                        return (EFAULT);
                else if (strlen(strPtr) + 1 ==  SIZE_DRIVER_BUFFER &&
                    ddi_copyin(uaddr++, &lastbyte, 1, mode) == 0)
                        return (EOVERFLOW);

                strcpy(string, strPtr);


>
> New to driver programming, be gentle...

Does the above work ?

I don't understand the second strcpy, though - you could directly read 
into your kernel driver buffer to start with. And unbounded strcpy - brrr 
:(

>
> Thanks

You're welcome,
FrankH.

>
>
> This message posted from opensolaris.org
> _______________________________________________
> opensolaris-code mailing list
> [email protected]
> http://mail.opensolaris.org/mailman/listinfo/opensolaris-code
>
_______________________________________________
opensolaris-code mailing list
[email protected]
http://mail.opensolaris.org/mailman/listinfo/opensolaris-code

Reply via email to