LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How do you write to a proc file from a user program? (http://www.linuxquestions.org/questions/programming-9/how-do-you-write-to-a-proc-file-from-a-user-program-766130/)

beaglebird 11-02-2009 12:23 AM

How do you write to a proc file from a user program?
 
I know how to write to a proc file from the command line (e.g. echo hello > myProcFile) but I need to do that in a user program (written in C). I tried using fopen but it returned an error. So is there anything else I can do besides using something like execl echo?

pixellany 11-02-2009 12:31 AM

/proc is a virtual file system---physically you are just accessing data structures in memory. I would have assumed you would access it as you would any ohter file. The big difference is that writing to /proc is not something that you typically should be doing.

for something like fopen to open a file, I assume the user running the program must have the appropriate privileges. What happens if you run you program as root?

beaglebird 11-02-2009 12:50 AM

Thank you for replying!

I get the same result when I run my program as root.

And to clarify, I am doing this for a school assignment. I had to make a module that creates a /proc file entry when it is initialized. The next step, which I am stuck on, is to have a user program write and read to/from the /proc file.

I thought that perhaps fopen didn't work because /proc is virtual. Or maybe I need to set permissions on the /proc file when I create it?

beaglebird 11-02-2009 01:13 AM

Actually, scratch that. I just realized I had coded the path of the file using '~' which obviously means different things when I'm me vs when I'm root. So that means you solved my problem. Thank you very much!


All times are GMT -5. The time now is 07:31 AM.