Problem in sending data between User Space & Kernel Space
Problem in sending data between User Space & Kernel Space
- Subject: Problem in sending data between User Space & Kernel Space
- From: JanakiRam <email@hidden>
- Date: Tue, 24 Jul 2007 11:11:48 +0530
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.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
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);
size_t done = 0;
int res = copyinstr((void *)mdata->data, (void *)kerndata,fd, &done);
printf("kerndata -----[%s]\n", kerndata);
printf("size_t done -----[%d]\n", done);
if ( kerndata )
OSFree(kerndata, (sizeof(char) * fd) + 1, gMallocTag);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
USER SPACE CODE
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
int sendioctl(char *filename) {
int result;
int handle;
char *rootDir = "/";
if (filename[0] ==0) filename = rootDir;
printf("do hook of [%s]\n",filename);
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;
printf("sending mdata fd [%d]\n", mdata.len);
printf("sending mdata data [%u]\n",
mdata.data );
printf("sending mdata data [%s]\n", mdata.data );
printf("sending mdata strlen [%d]\n", strlen(mdata.data));
unsigned long id = FILE_DATA_STRUCT_COMMAND;
result = ioctl(handle, id, (char*) &mdata);
printf("first ioctl call result is [%d]. Sleeping for 5 secs..\n", result);
if ( mdata.data != NULL )
free(mdata.data);
CloseDriver(handle);
printf("CloseDriver\n");
close(fileDesc);
printf("closed first file\n", result);
return result;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden