Find the answer to your Linux question:
Results 1 to 6 of 6
hi there, i am working on a homework assignment of mine, and i need to write a module that tells us the system time in seconds and microseconds, and i ...
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    35

    how can i write into a proc

    hi there,

    i am working on a homework assignment of mine, and i need to write a module that tells us the system time in seconds and microseconds, and i need a lil help, i am stuck in the part where i need to write into a proc file i created, i just dono how to do it, i searched everywhere for a function that may help me but to no avail, anyone here can help me please???

    here is my current code

    Code:
    #include <linux/init.h>
    #include <linux/module.h>
    #include <linux/proc_fs.h>
    #include <linux/kernel.h>
    #include <linux/time.h>
    
    
    #define PROCFS_NAME 	"rexor"
    
    static struct proc_dir_entry *proc_file;
    
    
    
    static int __init init(void)
    {
    	struct timeval time; 
    
    
    	proc_file = create_proc_entry(PROCFS_NAME, 0, NULL);
    
    	if(proc_file == NULL)
    	{
    		remove_proc_entry(PROCFS_NAME, NULL);
    		printk(KERN_ALERT "Error: Could not initialize /proc/%s\n", 				PROCFS_NAME);
    
    		return -ENOMEM;
    
    	}
           	
         do_gettimeofday(&time);
    //     printk(KERN_EMERG "%d.%d", time.tv_sec,time.tv_usec);
    	proc_file->write_proc ;
    
    
         return 0;
    }
    static void __exit exit(void)
    {
    	remove_proc_entry(PROCFS_NAME, NULL);
    }
    MODULE_LICENSE("GPL");
    
    module_init(init);
    module_exit(exit);
    using do_gettimeofday i am able to obtain the current time, but i just dono how to write this time into /proc/rexor

    thanks in advance

    P.S: ITS MY FIRST POST YAYE :P, also i know rexor is a stupid name for a proc dictionary, i will change it later to currentTime or smth.
    P.P.S: i dont want to make another post about this, so does anyone here know what callback_read does, cuz i think it may help me out in this problem

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Hello,

    this webpage should help you there.
    Access the Linux kernel using the /proc filesystem


    callback_read is supposed to be a callback function, i.e. it is up to you what is does.
    You write this function as you want and it gets called when "something" specific happens.
    Debian GNU/Linux -- You know you want it.

  3. #3
    Just Joined!
    Join Date
    May 2009
    Posts
    35
    yea thank u but that is not what i ment

    i am looking for a function similar to sprintf in C, isnt there a sprintk or smth?

    EDIT: nvm i figured it out

  4. #4
    Just Joined!
    Join Date
    May 2009
    Posts
    35
    k i need help, i thought i figured it out, but i didnt

    here is my current code

    Code:
    #include <linux/init.h>
    #include <linux/module.h>
    #include <linux/proc_fs.h>
    #include <linux/kernel.h>
    #include <linux/time.h>
    
    MODULE_LICENSE("GPL");
    MODULE_DESCRIPTION("an LKM that gives the current system time");
    MODULE_AUTHOR("sfsdfdsf");
    
    
    #define PROCFS_NAME 	"currenttime"
    
    struct proc_dir_entry *proc_file;
    
    
    
    int init(void)
    {
    	struct timeval time; 
    
    
    	proc_file = create_proc_entry(PROCFS_NAME, 0,  NULL);
    
    	if(proc_file == NULL)
    	{
    		remove_proc_entry(PROCFS_NAME, NULL);
    		printk(KERN_ALERT "Error: Could not initialize /proc/%s\n", PROCFS_NAME);
    
    		return -ENOMEM;
    
    	}
           	
    	do_gettimeofday(&time);
    
    	
    	//sprintf(proc_file,"%d %d\n",time.tv_sec, time.tv_usec);
    	
    
    
    
         return 0;
    }
    void exit(void)
    {
    	remove_proc_entry(PROCFS_NAME, NULL);
    }
    
    
    module_init(init);
    module_exit(exit);
    the problem is with sprintf, as i said i want currenttime to hold the current time in seconds and nanoseconds, but i am stuck, anyone could help me please

    EDIT: well, it now works yaye

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    You are using sprintf() to output to a structure. This is not valid. You can only sprintf() to a char array (buffer).
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  6. #6
    Just Joined!
    Join Date
    May 2009
    Posts
    35
    Quote Originally Posted by Rubberman View Post
    You are using sprintf() to output to a structure. This is not valid. You can only sprintf() to a char array (buffer).
    yea i figured that out, and it works flawlessly (this time for sure :P)

    anywayz thank u all for your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...