0

I'm trying to do a couple of tests where I need to set the computer time backward or forward depending on some external values. I know that I can do this using clock_settime() in time.h. I've encountered the problem that when needing to set the time backward, the operation fails.

The documentation for clock_settime states that

Only the CLOCK_REALTIME clock can be set, and only the superuser may do so. If the system securelevel is greater than 1 (see init(8)), the time may only be advanced. This limitation is imposed to prevent a malicious superuser from setting arbitrary time stamps on files. The system time can still be adjusted backwards using the adjtime(2) system call even when the system is secure.

I require nanosecond precision, and adjtime() as far as I understand, does not allow nanosecond precision. The other problem with ajdtime() is that it does not set the clock outright, rather slows it down, until the clock catches up to the set value.

I've done some reading on init() but I'm not sure how to lower the securelevel, and frankly I'd rather not be forced to do this, however, if there's no other way, I'm willing to try it.

Thanks in advance

Update 1

I started looking into altering securelevel and now i'm not even sure if that's something that can be done on Ubuntu. Around the web, I have come across mentions of editing /etc/init/rc-sysinit.conf, /etc/init/rc.conf, or /etc/sysctl.conf and, again, I'm not sure what needs to be added in order to lower the securelevel if, in fact, this is something that can be done. Especially since I could not find a 'rc.securelvel' file.

  • `I require nanosecond precision` do you have hardware that supports measuring in nanosecond intervals? The `securelvel` is something from openbsd, that [manual page](https://man.openbsd.org/clock_gettime.2) is also from openbsd, you run ubuntu, it's linux, I expect you can set CLOCK_REALTIME to anything you want, have you tried it? All files you meantioned in /etc are just not really used anymore, (99% of) linux uses systemd currently to handle startup. – KamilCuk Nov 25 '18 at 20:23
  • In linux `clock_settime` still will give you milisecond resolution (I think), [as in glibc the call is translated to settimeofday](https://github.com/lattera/glibc/blob/master/sysdeps/unix/clock_settime.c#L88). If you have a hardware that supports measuring nanosecond interval, I guess you should write a kernel module for that. – KamilCuk Nov 25 '18 at 20:27
  • @KamilCuk yes I have PTP-enabled NIC, and my cpu does provide nanosecond precision. And yes, the following code is what I have, I can set the clock forward but not backward `if ( (clock_settime(CLOCK_REALTIME, &ts)) != 0){ perror("clock_settime"); exit(EXIT_FAILURE); }` – habitual_programmer Nov 25 '18 at 20:31
  • Also, as I understand, `clock_settime` does provide nanosecond resolution – habitual_programmer Nov 25 '18 at 20:33
  • 1. No. `clock_settime` provides an _API_ with nanosecond resolution. That doesn't mean that the underlying implementation does indeed support nanosecond resolution and I think kernel does not, I don't know how to get to `hrtimer` to get it to return nanoseconds. 2. Cpu time is measured (in linux) using ticks per second with max 1000 (so still 1ms resolution). Check the output of `sysconf(_SC_CLK_TCK)` – KamilCuk Nov 25 '18 at 20:37
  • @KamilCuk Do you think, then, there's something incorrect about the manner in which I'm calling `clock_settime` perhaps? – habitual_programmer Nov 25 '18 at 20:44
  • No? I'm saying that if you're are using ubuntu, it uses glibc for the underlying C standard library implementation, which converts timespec into timeval in the underlying implementation of [`clock_settime(CLOCK_REALTIME, `](https://github.com/lattera/glibc/blob/master/sysdeps/unix/clock_settime.c#L90). This will get you nowhere. Also you are reading manual page for openbsd kernel, yet ubuntu uses linux kernel, which doesn't have `securelevel`. It will get you nowhere. Interest yourself in writing kernel module which will give you an abstraction you need. `ntpd` also uses adjtime. – KamilCuk Nov 25 '18 at 20:48
  • Okay, I understand, thank you for your help. Also the output to the `sysconf(_SC_CLK_TCK)` is 100 – habitual_programmer Nov 25 '18 at 20:53
  • So your cpu measures time in 10 ms intervals. Only if you have a hardware that supports nanosecond resolution (like [HPET](https://en.wikipedia.org/wiki/High_Precision_Event_Timer)) you can use nanosecond resolution. HPET is still a timer, not a clock, you would need to set a timer to trigger every nanosecond and handle leap-nano-seconds. – KamilCuk Nov 25 '18 at 20:55

0 Answers0