Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem in sending data between User Space & Kernel Space



Hi ALL,

          I'm creating an application which has user space as well as kernel space.I'm using ioctl command to send the data from user space to kernel space.

      I'm passing my custom defined structure from user space to kernel space. If i create the structure member on stack ( i.e char str[15] ="Hello World\0" & data = "" ) , then i'm unable to receive at kernel level.

         But if create memory using malloc then i'm able to access at kernel level.

     Is there any restriction that Kernel space can only access the heap variables at user space but not the stack variables.
   
     Is this implementaion in Mac is differing from Unix implementation.

     Please help me to solve this.Thanks in Advance.

Following is my Code which works perfectly using heap.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
IOCTL Command args structure
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
 typedef struct {
    int len;
    char* data;
} data_struct;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
KERNEL SPACE CODE
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
int fp_ioctl(dev_t dev, u_long cmd, caddr_t arg,int fflag, struct proc *p)
{
    printf("fp_ioctl called\n");

    data_struct *mdata = (data_struct*) arg;
   
    if (!mdata) {
        printf("mdata is null!\n");
        return 0;
    }
   
    int fd = mdata->len;
    printf("received mdata fd [%d]\n", fd);
    printf("received mdata data [%u]\n", mdata->data);

    char *kerndata  = OSMalloc( (sizeof(char) * fd) + 1, gMallocTag);

    if ( kerndata == NULL )
        printf("Unable to allocate memory for kerndata -----\n");
     
   int res = copyin((void *)mdata->data, (void *)kerndata, fd);

    printf("kerndata -----[%s]\n", kerndata);
   
    if ( kerndata )
        OSFree(kerndata, (sizeof(char) * fd) + 1, gMallocTag);
       
    return 0;
}   

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
USER SPACE CODE
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

int sendioctl(char *filename) {
    int result;
    int handle;
      
    result = OpenDriver(&handle);
    if (result) {
        return -1;
    }
   
    int fileDesc=open(filename,O_RDONLY);
    if (fileDesc<0) return 4;
    printf("fileDesc is [%d]\n", fileDesc);
   
    data_struct mdata;
    char str[40]= "Hello World  xcbxcbvxcbvcxbcvbcbxxcc456\0";
    int len = strlen(str);
   
    // Create Char * Memory
    mdata.data ="" (char *) malloc ( sizeof(char) * len );
   
    if ( mdata.data == NULL)
        return -12345;
    // Fill Char * Memory with zeros
    memset(mdata.data,0,len);
    // Copy Str to  Char *
    memcpy(mdata.data,str,len);
   
    mdata.len=len ;

       
    unsigned long id = FILE_DATA_STRUCT_COMMAND;
    result = ioctl(handle, id, (char*) &mdata);

   if ( mdata.data != NULL )
        free(mdata.data);

    CloseDriver(handle);
    printf("CloseDriver\n");
   
    close(fileDesc);
    printf("closed first file\n", result);

    return result;
}

-JanakiRam
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/darwin-dev/email@hidden

This email sent to email@hidden

References: 
 >Problem in sending data between User Space & Kernel Space (From: JanakiRam <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.