1

What am I doing wrong here? I expect settimeofday() to change the system time, not return EINVAL.

$ uname -a
Linux io 4.3.5-300.fc23.x86_64 #1 SMP Mon Feb 1 03:18:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
$ cat settimeofday.c 
#include <sys/time.h>
#include <stdio.h>

int main()
{
    struct timeval tv = {0, 0};
    if (settimeofday(&tv, 0) == -1)
        perror("settimeofday");
}
$ gcc settimeofday.c 
$ sudo ./a.out 
settimeofday: Invalid argument

The error is coming from a Thinkpad T450 running Fedora 23. The same code runs fine on OS X.

EDIT

To clarify, the command is being executed as root:

# whoami
root
# sudo ./a.out 
settimeofday: Invalid argument

As expected, I get EPERM not EINVAL if I run the program as a regular user:

$ ./a.out 
settimeofday: Operation not permitted
Josh Johnson
  • 8,832
  • 4
  • 25
  • 31

2 Answers2

5

Commit e1d7ba was introduced to the Linux kernel in mid-2015 and restricts the value of the tv_sec field. The restriction is influenced by system uptime -- see the commit message and related LKML discussion for details.

That's what was causing the settimeofday call to return EINVAL and explains why the code runs on OS X and older Linux machines.

Josh Johnson
  • 8,832
  • 4
  • 25
  • 31
0

As shown in the manpage of settimeofday().

If either tv or tz is NULL, the corresponding structure is not set or returned.

gzh
  • 3,507
  • 2
  • 19
  • 23
  • 1
    And a few lines further down it says: `The use of the timezone structure is obsolete; the tz argument should normally be specified as NULL.` – John Hascall Feb 12 '16 at 04:39
  • @John Hascall, After fully read the manpage of setimeofday(), it seems that the tz_dsttime fied of timezone will not be supported in linux, but the tz_minuteswest filed is still needed to be set. I am not sure I read correctly. – gzh Feb 12 '16 at 05:01
  • Numerous reputable examples (such as Robert Love's Linux System Programming) show settimeofday() used with the second argument set to NULL. Furthermore, I tried passing in a non-NULL timezone struct and it made no difference. – Josh Johnson Feb 12 '16 at 05:06
  • It seems you are prohibited to set tv to {0, 0}, the start of Epoch time. as mentioned in [this page](http://www.gnu.org/software/libc/manual/html_node/High_002dResolution-Calendar.html), _Use adjtime (below) to make a smooth transition_ @JoshuaJohnson – gzh Feb 12 '16 at 05:22
  • 1
    The consequence of set tv to {0, 0}: http://www.wired.com/2016/02/dont-set-your-iphone-back-to-1970-no-matter-what/ – gzh Feb 16 '16 at 12:14