clock_settime backward correction












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.










share|improve this question

























  • I require nanosecond precision do you have hardware that supports measuring in nanosecond intervals? The securelvel is something from openbsd, that manual page 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.

    – Kamil Cuk
    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. If you have a hardware that supports measuring nanosecond interval, I guess you should write a kernel module for that.

    – Kamil Cuk
    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)

    – Kamil Cuk
    Nov 25 '18 at 20:37


















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.










share|improve this question

























  • I require nanosecond precision do you have hardware that supports measuring in nanosecond intervals? The securelvel is something from openbsd, that manual page 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.

    – Kamil Cuk
    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. If you have a hardware that supports measuring nanosecond interval, I guess you should write a kernel module for that.

    – Kamil Cuk
    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)

    – Kamil Cuk
    Nov 25 '18 at 20:37
















0












0








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.










share|improve this question
















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.







c init ubuntu-18.04 time.h sysctl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 20:17







habitual_programmer

















asked Nov 25 '18 at 1:31









habitual_programmerhabitual_programmer

84




84













  • I require nanosecond precision do you have hardware that supports measuring in nanosecond intervals? The securelvel is something from openbsd, that manual page 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.

    – Kamil Cuk
    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. If you have a hardware that supports measuring nanosecond interval, I guess you should write a kernel module for that.

    – Kamil Cuk
    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)

    – Kamil Cuk
    Nov 25 '18 at 20:37





















  • I require nanosecond precision do you have hardware that supports measuring in nanosecond intervals? The securelvel is something from openbsd, that manual page 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.

    – Kamil Cuk
    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. If you have a hardware that supports measuring nanosecond interval, I guess you should write a kernel module for that.

    – Kamil Cuk
    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)

    – Kamil Cuk
    Nov 25 '18 at 20:37



















I require nanosecond precision do you have hardware that supports measuring in nanosecond intervals? The securelvel is something from openbsd, that manual page 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.

– Kamil Cuk
Nov 25 '18 at 20:23







I require nanosecond precision do you have hardware that supports measuring in nanosecond intervals? The securelvel is something from openbsd, that manual page 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.

– Kamil Cuk
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. If you have a hardware that supports measuring nanosecond interval, I guess you should write a kernel module for that.

– Kamil Cuk
Nov 25 '18 at 20:27







In linux clock_settime still will give you milisecond resolution (I think), as in glibc the call is translated to settimeofday. If you have a hardware that supports measuring nanosecond interval, I guess you should write a kernel module for that.

– Kamil Cuk
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







@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





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)

– Kamil Cuk
Nov 25 '18 at 20:37







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)

– Kamil Cuk
Nov 25 '18 at 20:37














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463933%2fclock-settime-backward-correction%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463933%2fclock-settime-backward-correction%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen