Score:0

Unable to use/find gettimeofday in Ubuntu kernel

hu flag

I am using Ubuntu 20.10 with kernel source 5.8.0-59.66

I need to get the current time of day (from the epoch) within the kernel. Using either RHEL 7.9 or CENTOS 7 I have relied on the gettimeofday() function.

In a nutshell, the documentation seems to indicate the function exists in Ubuntu -

http://manpages.ubuntu.com/manpages/bionic/man2/gettimeofday.2.html

within which it says to include the following

   #include <sys/time.h>

This file or actually the directory "sys" doesn't seem to exist in my kernel source tree. If I do a "find" on time.h I do find a few including ~/include/linux/time.h but it does not contain the function definition.

Am I missing something here? Or perhaps there's a better and/or updated method for getting the time in the manner of gettimeofday?

It works fine on my other non-Ubuntu kernel mods. But this is my first experience with Ubuntu.

I neglected to add the compile error.

error: implicit declaration of function ‘gettimeofday’ 
[-Werror=implicit-function-declaration]
 1329 |         gettimeofday(&time);

The code using this function is

struct  tm time;      
gettimeofday(&time);
local_time = (u32)(time.tm_sec );

Please be aware that this is not the exact code in RHEL and Centos versions. I made minor changes based on compiler errors such as time.tv_sec became time.tm_sec and thus it is not proven.

However I think the first issue is not having the definition for gettimeofday? Or I could be wrong...

TonyB avatar
za flag
Could you show us your code..?
TonyB avatar
za flag
On my ubuntu 20.04 system, sys/time.h is located here: ` /usr/include/x86_64-linux-gnu/sys/time.h` and it contains the following comment (among others) about gettimeofday() ... "This function itself is semi-obsolete; most callers should use time or clock_gettime instead."
rhg910 avatar
hu flag
Excellent. I will try it these evening and officially respond tomorrow.
us flag
Section 2 of the man pages is for system calls from user space. Such functions are generally not available within the kernel itself. Generally, kernel modules can only use the `<linux/*.h>` and `<asm/*.h>` headers; the `<sys/*.h>` headers are not part of the kernel.
us flag
https://stackoverflow.com/questions/50349294/getnstimeofday-is-an-implicit-declaration-in-system-call-when-linux-time-h-i suggests you ought to use `getnstimeofday` from `<linux/ktime.h>`.
rhg910 avatar
hu flag
I would close this post as answered if I saw the check mark referenced in the help pages. As it stand, TonyB's answer pointer me in the right direction, as did Nate's.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.