4

Anwser

Solved!! Thanks to @IanAbbott

the header should be:

#include <linux/ktime.h>
#include <linux/timekeeping.h>

rather than <linux/time.h>.

More detail see discussion.


Original Question

I am writing a system call names sys_my_time.c, which will use getnstimeofday(). I have imported <linux/time.h>. The code like this:

#include <linux/kernel.h>
#include <linux/linkage.h>
#include <linux/time.h>

asmlinkage int sys_my_time() {
  struct timespec t;
  getnstimeofday(&t);
  // ...
  return 0;
}

But while compiling, the error shows:

CC      kernel/sys_my_time.o
kernel/sys_my_time.c: In function ‘sys_my_time’:
kernel/sys_my_time.c:8:3: error: implicit declaration of function ‘getnstimeofday’ [-Werror=implicit-function-declaration]
getnstimeofday(&t);
^
cc1: some warnings being treated as errors
scripts/Makefile.build:320: recipe for target 'kernel/sys_my_time.o' failed
make[1]: *** [kernel/sys_my_time.o] Error 1
Makefile:1029: recipe for target 'kernel' failed
make: *** [kernel] Error 2

I have no idea why the error happens.

P.S. compiling kernel V4.14.25 in Ubuntu 16.04

Community
  • 1
  • 1
tigercosmos
  • 345
  • 3
  • 17
  • Since v3.17.x, `getnstimeofday` is declared by `#include `. – Ian Abbott May 15 '18 at 13:06
  • @Abbott thanks, However when I change to `` and other the same: `In file included from kernel/sys_my_time.c:3:0: ./include/linux/timekeeping.h:19:36: warning: ‘struct timeval’ declared inside parameter list extern void do_gettimeofday(struct timeval *tv); ^ // ...... ./include/linux/timekeeping.h:330:4: error: expected ‘;’, ‘,’ or ‘)’ before ‘ kernel/sys_my_time.c: In function ‘sys_my_time’: kernel/sys_my_time.c:7:18: error: storage size of ‘t’ isn’t known struct timespec t;` – tigercosmos May 15 '18 at 13:24
  • Seems we cannot use `` directly? The error message are not likely caused by me code? – tigercosmos May 15 '18 at 13:26
  • 1
    It maybe depends on some other headers. Try `#include ` instead as that will do a nested #include of `` and other time-related headers. – Ian Abbott May 15 '18 at 13:48
  • @IanAbbott Thanks!! After adding `#include #include `, it is just fine. :) – tigercosmos May 15 '18 at 16:42
  • @Ian Abbott Could you paste in answer, and I could mark it as the best answer. – tigercosmos Jun 12 '18 at 15:26

1 Answers1

2

Since kernel version 3.17.x, getnstimeofday is no longer declared by #include <linux/time.h>. The solution is to add:

#include <linux/ktime.h>

Depending on the kernel version, #include <linux/ktime.h> will pull in the declaration of getnstimeofday from either <linux/time.h> (prior to 3.17.x) or from <linux/timekeeping.h> (for 3.17.x onwards). There is no need to include <linux/timekeeping.h> directly.

Note that <linux/ktime.h> has been available since Linux kernel 2.6.16 onwards.

You may be able to remove your #include <linux/time.h> if there is nothing else in there you need to use. Test that by removing the line and building your code for any kernel 3.17.x or later.

Ian Abbott
  • 15,083
  • 19
  • 33