2013-03-30 17:59:21 +01:00
|
|
|
/* The kernel call implemented in this file:
|
|
|
|
* m_type: SYS_SETTIME
|
|
|
|
*
|
|
|
|
* The parameters for this kernel call are:
|
2014-05-22 15:31:51 +02:00
|
|
|
* m_lsys_krn_sys_settime.now
|
|
|
|
* m_lsys_krn_sys_settime.clock_id
|
|
|
|
* m_lsys_krn_sys_settime.sec
|
|
|
|
* m_lsys_krn_sys_settime.nsec
|
2013-03-30 17:59:21 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kernel/system.h"
|
|
|
|
#include <minix/endpoint.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* do_settime *
|
|
|
|
*===========================================================================*/
|
|
|
|
int do_settime(struct proc * caller, message * m_ptr)
|
|
|
|
{
|
2013-08-23 20:27:27 +02:00
|
|
|
clock_t newclock;
|
|
|
|
int32_t ticks;
|
2013-09-20 19:47:18 +02:00
|
|
|
time_t timediff, timediff_ticks;
|
2013-03-30 17:59:21 +01:00
|
|
|
|
2014-05-22 15:31:51 +02:00
|
|
|
if (m_ptr->m_lsys_krn_sys_settime.clock_id != CLOCK_REALTIME) /* only realtime can change */
|
2013-03-30 17:59:21 +01:00
|
|
|
return EINVAL;
|
|
|
|
|
2014-05-22 15:31:51 +02:00
|
|
|
if (m_ptr->m_lsys_krn_sys_settime.now == 0) { /* user just wants to adjtime() */
|
2013-03-31 03:24:24 +02:00
|
|
|
/* convert delta value from seconds and nseconds to ticks */
|
2014-05-22 15:31:51 +02:00
|
|
|
ticks = (m_ptr->m_lsys_krn_sys_settime.sec * system_hz) +
|
|
|
|
(m_ptr->m_lsys_krn_sys_settime.nsec/(1000000000/system_hz));
|
2013-03-31 03:24:24 +02:00
|
|
|
set_adjtime_delta(ticks);
|
|
|
|
return(OK);
|
|
|
|
} /* else user wants to set the time */
|
|
|
|
|
2014-05-22 15:31:51 +02:00
|
|
|
timediff = m_ptr->m_lsys_krn_sys_settime.sec - boottime;
|
2013-09-20 19:47:18 +02:00
|
|
|
timediff_ticks = timediff * system_hz;
|
2013-06-24 15:46:20 +02:00
|
|
|
|
2013-03-30 17:59:21 +01:00
|
|
|
/* prevent a negative value for realtime */
|
2014-05-22 15:31:51 +02:00
|
|
|
if (m_ptr->m_lsys_krn_sys_settime.sec <= boottime ||
|
2013-06-24 15:46:20 +02:00
|
|
|
timediff_ticks < LONG_MIN/2 || timediff_ticks > LONG_MAX/2) {
|
2013-03-30 17:59:21 +01:00
|
|
|
/* boottime was likely wrong, try to correct it. */
|
2014-05-22 15:31:51 +02:00
|
|
|
boottime = m_ptr->m_lsys_krn_sys_settime.sec;
|
2013-03-30 17:59:21 +01:00
|
|
|
set_realtime(1);
|
|
|
|
return(OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* calculate the new value of realtime in ticks */
|
2014-05-22 15:31:51 +02:00
|
|
|
newclock = timediff_ticks +
|
|
|
|
(m_ptr->m_lsys_krn_sys_settime.nsec/(1000000000/system_hz));
|
2013-03-30 17:59:21 +01:00
|
|
|
|
2013-03-31 03:24:24 +02:00
|
|
|
set_realtime(newclock);
|
2013-03-30 17:59:21 +01:00
|
|
|
|
|
|
|
return(OK);
|
|
|
|
}
|