LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (https://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   kernel function to print uptime in seconds? (https://www.linuxquestions.org/questions/linux-kernel-70/kernel-function-to-print-uptime-in-seconds-4175548151/)

MastaG 07-16-2015 10:13 AM

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.. */
};


replica9000 07-16-2015 06:21 PM

Is this just for exercise? Normally you can get the uptime in seconds with cat /proc/uptime

MastaG 07-28-2015 04:04 AM

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..

sundialsvcs 07-29-2015 08:28 AM

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.


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