Re: set_fs

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

 



Brad Dickerson wrote:

Hello,
I am trying to call the open function from within a kernel module.
After searching the web, I tried the following snippet:
mm_segment_t old_fs;
old_fs=get_fs();
set_fs(USER_DS);
fp=open('/etc/config.txt',O_RDONLY);
getLine(fp,line);
close(fp);
set_fs(old_fs);
For some reason, the open fails and returns -1. Any ideas on what I am doing wrong? This is all really new to me.
thanks,
Brad




hi there, please modify it for your use .

kopen (const char *filename, int flags) /* user defined function for opening a file from within kernel space */
{
oldmm = get_fs ();
set_fs (get_ds ());
kfd = sys_open (filename, flags | O_CREAT, S_IRWXU); /* create one if one doesnt exist */
set_fs (oldmm);
return kfd;
}


note that you would have get and set again if you want to read/write to the file .


/* returns number of bytes read or corresponding error code
* in case of failure
* see the man pages for more "man 2 read" */


int
kread (int fd, char *buffer, int size)
{
 int red;

 oldmm = get_fs ();
 set_fs (get_ds ());
 red = sys_read (fd, buffer, size);
 set_fs (oldmm);
 return red;
}

/* returns number of bytes written or corresponding error code
* in case of failure
* see the man pages for more "man 2 write" */



int kwrite (int fd, char *buffer, int size) { int red;

 oldmm = get_fs ();
 set_fs (get_ds ());
 red = sys_write (fd, buffer, size);
 set_fs (oldmm);
 return red;
}

hope this helps  .
cheers,
Amith.



--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[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