Thread: Reading a proc file in user space

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    9

    Reading a proc file in user space

    hello,

    I have a proc file which contains a buffer of ints, which I want to be able to read from user space.

    I assume you open the file as you would a normal file
    Code:
     File fp* = fopen("/proc/kernelRead","r");
    but from here I am unsure how you would read. would you use fscanf? if you do, how would the proc.read method get called?

    thanks for any help!

  2. #2
    Registered User
    Join Date
    Dec 2010
    Posts
    25
    Quote Originally Posted by Fillis52 View Post
    hello,

    I have a proc file which contains a buffer of ints, which I want to be able to read from user space.

    I assume you open the file as you would a normal file
    Code:
     File fp* = fopen("/proc/kernelRead","r");
    but from here I am unsure how you would read. would you use fscanf? if you do, how would the proc.read method get called?

    thanks for any help!
    You want something like this?

    Code:
    #include <stdio.h>
    
    int main(){
      FILE *fp = fopen("/proc/cpuinfo","r");
    
      char c;
      while (c != EOF){
        c = fgetc(fp);
        printf("%c", c);
      }
    
      return 0;
    }
    I just read/printed to console character by character. There are a lot of ways to mess around with this. You can also seek files and do a lot of clever stuff. What is the output of the file like? Will it be similar each time? Could you parse it or tokenize it? Etc~
    Last edited by tenchu; 12-06-2010 at 08:10 AM.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Fillis52 View Post
    hello,

    I have a proc file which contains a buffer of ints, which I want to be able to read from user space.

    I assume you open the file as you would a normal file
    Code:
     File fp* = fopen("/proc/kernelRead","r");
    but from here I am unsure how you would read. would you use fscanf? if you do, how would the proc.read method get called?

    thanks for any help!
    That would be a serious no-no. Since the file could contain almost any uncontrolled thing.

    To jump to an address in user space you would have to write assembly language support for it into your program.

    But you should also know that any decent virus scanner is going to seriously freak out about code execution in user allocated memory...

    You would be well advised to learn how to create libraries (lib) and dynamic libraries (dll) instead of trying to ececute code from user files.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> That would be a serious no-no.
    There is nothing wrong with a normal app reading from a pseudo file in proc-FS.

    procfs - Wikipedia, the free encyclopedia

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  3. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

Tags for this Thread