LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 07-16-2015, 10:13 AM   #1
MastaG
LQ Newbie
 
Registered: May 2012
Posts: 6

Rep: Reputation: Disabled
kernel function to print uptime in seconds?


Hi there,

I'm trying to compile a kernel module which will print the uptime since booting in seconds.
This is what I have so far:
Code:
#include <linux/module.h>  /* Needed by all modules */
#include <linux/kernel.h>  /* Needed for KERN_ALERT */
#include <linux/sysinfo.h>

int init_module(void)
{
   printk("Uptime: ", sysinfo.uptime, "\n");

   // A non 0 return means init_module failed; module can't be loaded.
   return 0;
}


void cleanup_module(void)
{
  printk(KERN_ALERT "Goodbye world 1.\n");
}


MODULE_LICENSE("GPL");
However compiling it will error out with:

make[1]: Map '/usr/src/kernels/4.0.6-300.fc22.x86_64' wordt binnengegaan
CC [M] /home/mastag/time.o
/home/mastag/time.c: In functie ‘init_module’:
/home/mastag/time.c:14:23: fout: ‘sysinfo’ undeclared (first use in this function)
printk("Uptime: ", sysinfo.uptime, "\n");
^
/home/mastag/time.c:14:23: note: each undeclared identifier is reported only once for each function it appears in
scripts/Makefile.build:264: recept voor doel '/home/mastag/time.o' is mislukt
make[2]: *** [/home/mastag/time.o] Fout 1

Why is it undeclared?
I've included sysinfo.h which has the sysinfo struct right?

linux/sysinfo.h contains:
Code:
struct sysinfo {
        __kernel_long_t uptime;         /* Seconds since boot */
        __kernel_ulong_t loads[3];      /* 1, 5, and 15 minute load averages */
        __kernel_ulong_t totalram;      /* Total usable main memory size */
        __kernel_ulong_t freeram;       /* Available memory size */
        __kernel_ulong_t sharedram;     /* Amount of shared memory */
        __kernel_ulong_t bufferram;     /* Memory used by buffers */
        __kernel_ulong_t totalswap;     /* Total swap space size */
        __kernel_ulong_t freeswap;      /* swap space still available */
        __u16 procs;                    /* Number of current processes */
        __u16 pad;                      /* Explicit padding for m68k */
        __kernel_ulong_t totalhigh;     /* Total high memory size */
        __kernel_ulong_t freehigh;      /* Available high memory size */
        __u32 mem_unit;                 /* Memory unit size in bytes */
        char _f[20-2*sizeof(__kernel_ulong_t)-sizeof(__u32)];   /* Padding: libc5 uses this.. */
};
 
Old 07-16-2015, 06:21 PM   #2
replica9000
Senior Member
 
Registered: Jul 2006
Distribution: Debian Unstable
Posts: 1,124
Blog Entries: 2

Rep: Reputation: 260Reputation: 260Reputation: 260
Is this just for exercise? Normally you can get the uptime in seconds with cat /proc/uptime
 
Old 07-28-2015, 04:04 AM   #3
MastaG
LQ Newbie
 
Registered: May 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
No exercise just trying to make some sample kernel modules.
I can get the uptime easily from userspace using a shell script, and I believe the struct in the linux/sysinfo.h kernel header is for using it with glibc.
I think the kernel itself doesn't have a function for calculating it in seconds.
I guess I have to calculate it myself using the jiffies or something..
 
Old 07-29-2015, 08:28 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
In your code so far, you refer to the sysinfo structure as though it were a variable. Hence the compile-time error. Simply surf the kernel source-code for correct examples of how to find the location of this structure and how to use its contents.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash/Sed : Long time (seconds) between print of last line and script termination sumncguy Linux - Newbie 2 11-04-2014 07:04 AM
init uptime longer than system uptime? m4rtin Linux - Server 1 09-15-2010 07:45 AM
how to print function names & parmaters each time control enters the function? tanniru Linux - Networking 1 09-11-2008 01:21 AM
print job spools out but does not initiate print function john test Linux - Newbie 7 07-23-2008 05:13 PM
Why does CUPS print after 15 seconds whether the job is ready or not? Izzard Linux - General 2 12-11-2003 03:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 03:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration