2013-08-02 16:10:30 +02:00
|
|
|
/* readclock - manipulate the hardware real time clock */
|
2007-01-12 17:35:04 +01:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
2013-04-18 11:08:16 +02:00
|
|
|
#include <lib.h>
|
2007-01-12 17:35:04 +01:00
|
|
|
#include <minix/type.h>
|
2009-12-22 01:05:09 +01:00
|
|
|
#include <minix/const.h>
|
2013-08-02 16:10:30 +02:00
|
|
|
#include <minix/callnr.h>
|
|
|
|
#include <minix/log.h>
|
2007-01-12 17:35:04 +01:00
|
|
|
#include <minix/syslib.h>
|
Initialization protocol for system services.
SYSLIB CHANGES:
- SEF framework now supports a new SEF Init request type from RS. 3 different
callbacks are available (init_fresh, init_lu, init_restart) to specify
initialization code when a service starts fresh, starts after a live update,
or restarts.
SYSTEM SERVICE CHANGES:
- Initialization code for system services is now enclosed in a callback SEF will
automatically call at init time. The return code of the callback will
tell RS whether the initialization completed successfully.
- Each init callback can access information passed by RS to initialize. As of
now, each system service has access to the public entries of RS's system process
table to gather all the information required to initialize. This design
eliminates many existing or potential races at boot time and provides a uniform
initialization interface to system services. The same interface will be reused
for the upcoming publish/subscribe model to handle dynamic
registration / deregistration of system services.
VM CHANGES:
- Uniform privilege management for all system services. Every service uses the
same call mask format. For boot services, VM copies the call mask from init
data. For dynamic services, VM still receives the call mask via rs_set_priv
call that will be soon replaced by the upcoming publish/subscribe model.
RS CHANGES:
- The system process table has been reorganized and split into private entries
and public entries. Only the latter ones are exposed to system services.
- VM call masks are now entirely configured in rs/table.c
- RS has now its own slot in the system process table. Only kernel tasks and
user processes not included in the boot image are now left out from the system
process table.
- RS implements the initialization protocol for system services.
- For services in the boot image, RS blocks till initialization is complete and
panics when failure is reported back. Services are initialized in their order of
appearance in the boot image priv table and RS blocks to implements synchronous
initialization for every system service having the flag SF_SYNCH_BOOT set.
- For services started dynamically, the initialization protocol is implemented
as though it were the first ping for the service. In this case, if the
system service fails to report back (or reports failure), RS brings the service
down rather than trying to restart it.
2010-01-08 02:20:42 +01:00
|
|
|
#include <minix/sysutil.h>
|
2007-01-12 17:35:04 +01:00
|
|
|
#include <minix/com.h>
|
2013-08-02 16:10:30 +02:00
|
|
|
#include <minix/type.h>
|
|
|
|
#include <minix/safecopies.h>
|
2007-01-12 17:35:04 +01:00
|
|
|
#include <sys/svrctl.h>
|
|
|
|
|
2013-08-02 16:10:30 +02:00
|
|
|
#include "readclock.h"
|
|
|
|
|
2013-08-07 03:34:08 +02:00
|
|
|
static struct rtc rtc;
|
|
|
|
|
2013-08-02 16:10:30 +02:00
|
|
|
static struct log log = {
|
|
|
|
.name = "readclock",
|
|
|
|
.log_level = LEVEL_INFO,
|
|
|
|
.log_func = default_log
|
|
|
|
};
|
|
|
|
|
|
|
|
/* functions for transfering struct tm to/from this driver and calling proc. */
|
|
|
|
static int fetch_t(endpoint_t who_e, vir_bytes rtcdev_tm, struct tm *t);
|
|
|
|
static int store_t(endpoint_t who_e, vir_bytes rtcdev_tm, struct tm *t);
|
2007-01-12 17:35:04 +01:00
|
|
|
|
Basic System Event Framework (SEF) with ping and live update.
SYSLIB CHANGES:
- SEF must be used by every system process and is thereby part of the system
library.
- The framework provides a receive() interface (sef_receive) for system
processes to automatically catch known system even messages and process them.
- SEF provides a default behavior for each type of system event, but allows
system processes to register callbacks to override the default behavior.
- Custom (local to the process) or predefined (provided by SEF) callback
implementations can be registered to SEF.
- SEF currently includes support for 2 types of system events:
1. SEF Ping. The event occurs every time RS sends a ping to figure out
whether a system process is still alive. The default callback implementation
provided by SEF is to notify RS back to let it know the process is alive
and kicking.
2. SEF Live update. The event occurs every time RS sends a prepare to update
message to let a system process know an update is available and to prepare
for it. The live update support is very basic for now. SEF only deals with
verifying if the prepare state can be supported by the process, dumping the
state for debugging purposes, and providing an event-driven programming
model to the process to react to state changes check-in when ready to update.
- SEF should be extended in the future to integrate support for more types of
system events. Ideally, all the cross-cutting concerns should be integrated into
SEF to avoid duplicating code and ease extensibility. Examples include:
* PM notify messages primarily used at shutdown.
* SYSTEM notify messages primarily used for signals.
* CLOCK notify messages used for system alarms.
* Debug messages. IS could still be in charge of fkey handling but would
forward the debug message to the target process (e.g. PM, if the user
requested debug information about PM). SEF would then catch the message and
do nothing unless the process has registered an appropriate callback to
deal with the event. This simplifies the programming model to print debug
information, avoids duplicating code, and reduces the effort to print
debug information.
SYSTEM PROCESSES CHANGES:
- Every system process registers SEF callbacks it needs to override the default
system behavior and calls sef_startup() right after being started.
- sef_startup() does almost nothing now, but will be extended in the future to
support callbacks of its own to let RS control and synchronize with every
system process at initialization time.
- Every system process calls sef_receive() now rather than receive() directly,
to let SEF handle predefined system events.
RS CHANGES:
- RS supports a basic single-component live update protocol now, as follows:
* When an update command is issued (via "service update *"), RS notifies the
target system process to prepare for a specific update state.
* If the process doesn't respond back in time, the update is aborted.
* When the process responds back, RS kills it and marks it for refreshing.
* The process is then automatically restarted as for a buggy process and can
start running again.
* Live update is currently prototyped as a controlled failure.
2009-12-21 15:12:21 +01:00
|
|
|
/* SEF functions and variables. */
|
2012-03-25 20:25:53 +02:00
|
|
|
static void sef_local_startup(void);
|
2013-08-02 16:10:30 +02:00
|
|
|
static int sef_cb_init(int type, sef_init_info_t * info);
|
Basic System Event Framework (SEF) with ping and live update.
SYSLIB CHANGES:
- SEF must be used by every system process and is thereby part of the system
library.
- The framework provides a receive() interface (sef_receive) for system
processes to automatically catch known system even messages and process them.
- SEF provides a default behavior for each type of system event, but allows
system processes to register callbacks to override the default behavior.
- Custom (local to the process) or predefined (provided by SEF) callback
implementations can be registered to SEF.
- SEF currently includes support for 2 types of system events:
1. SEF Ping. The event occurs every time RS sends a ping to figure out
whether a system process is still alive. The default callback implementation
provided by SEF is to notify RS back to let it know the process is alive
and kicking.
2. SEF Live update. The event occurs every time RS sends a prepare to update
message to let a system process know an update is available and to prepare
for it. The live update support is very basic for now. SEF only deals with
verifying if the prepare state can be supported by the process, dumping the
state for debugging purposes, and providing an event-driven programming
model to the process to react to state changes check-in when ready to update.
- SEF should be extended in the future to integrate support for more types of
system events. Ideally, all the cross-cutting concerns should be integrated into
SEF to avoid duplicating code and ease extensibility. Examples include:
* PM notify messages primarily used at shutdown.
* SYSTEM notify messages primarily used for signals.
* CLOCK notify messages used for system alarms.
* Debug messages. IS could still be in charge of fkey handling but would
forward the debug message to the target process (e.g. PM, if the user
requested debug information about PM). SEF would then catch the message and
do nothing unless the process has registered an appropriate callback to
deal with the event. This simplifies the programming model to print debug
information, avoids duplicating code, and reduces the effort to print
debug information.
SYSTEM PROCESSES CHANGES:
- Every system process registers SEF callbacks it needs to override the default
system behavior and calls sef_startup() right after being started.
- sef_startup() does almost nothing now, but will be extended in the future to
support callbacks of its own to let RS control and synchronize with every
system process at initialization time.
- Every system process calls sef_receive() now rather than receive() directly,
to let SEF handle predefined system events.
RS CHANGES:
- RS supports a basic single-component live update protocol now, as follows:
* When an update command is issued (via "service update *"), RS notifies the
target system process to prepare for a specific update state.
* If the process doesn't respond back in time, the update is aborted.
* When the process responds back, RS kills it and marks it for refreshing.
* The process is then automatically restarted as for a buggy process and can
start running again.
* Live update is currently prototyped as a controlled failure.
2009-12-21 15:12:21 +01:00
|
|
|
|
2013-08-02 16:10:30 +02:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
2007-01-12 17:35:04 +01:00
|
|
|
{
|
2013-08-02 16:10:30 +02:00
|
|
|
int r;
|
|
|
|
endpoint_t caller;
|
|
|
|
struct tm t;
|
|
|
|
message m;
|
|
|
|
int ipc_status, reply_status;
|
|
|
|
|
|
|
|
env_setargs(argc, argv);
|
|
|
|
sef_local_startup();
|
|
|
|
|
|
|
|
while (TRUE) {
|
|
|
|
|
|
|
|
/* Receive Message */
|
|
|
|
r = sef_receive_status(ANY, &m, &ipc_status);
|
|
|
|
if (r != OK) {
|
|
|
|
log_warn(&log, "sef_receive_status() failed\n");
|
|
|
|
continue;
|
2007-01-12 17:35:04 +01:00
|
|
|
}
|
|
|
|
|
2013-08-02 16:10:30 +02:00
|
|
|
if (is_ipc_notify(ipc_status)) {
|
Basic System Event Framework (SEF) with ping and live update.
SYSLIB CHANGES:
- SEF must be used by every system process and is thereby part of the system
library.
- The framework provides a receive() interface (sef_receive) for system
processes to automatically catch known system even messages and process them.
- SEF provides a default behavior for each type of system event, but allows
system processes to register callbacks to override the default behavior.
- Custom (local to the process) or predefined (provided by SEF) callback
implementations can be registered to SEF.
- SEF currently includes support for 2 types of system events:
1. SEF Ping. The event occurs every time RS sends a ping to figure out
whether a system process is still alive. The default callback implementation
provided by SEF is to notify RS back to let it know the process is alive
and kicking.
2. SEF Live update. The event occurs every time RS sends a prepare to update
message to let a system process know an update is available and to prepare
for it. The live update support is very basic for now. SEF only deals with
verifying if the prepare state can be supported by the process, dumping the
state for debugging purposes, and providing an event-driven programming
model to the process to react to state changes check-in when ready to update.
- SEF should be extended in the future to integrate support for more types of
system events. Ideally, all the cross-cutting concerns should be integrated into
SEF to avoid duplicating code and ease extensibility. Examples include:
* PM notify messages primarily used at shutdown.
* SYSTEM notify messages primarily used for signals.
* CLOCK notify messages used for system alarms.
* Debug messages. IS could still be in charge of fkey handling but would
forward the debug message to the target process (e.g. PM, if the user
requested debug information about PM). SEF would then catch the message and
do nothing unless the process has registered an appropriate callback to
deal with the event. This simplifies the programming model to print debug
information, avoids duplicating code, and reduces the effort to print
debug information.
SYSTEM PROCESSES CHANGES:
- Every system process registers SEF callbacks it needs to override the default
system behavior and calls sef_startup() right after being started.
- sef_startup() does almost nothing now, but will be extended in the future to
support callbacks of its own to let RS control and synchronize with every
system process at initialization time.
- Every system process calls sef_receive() now rather than receive() directly,
to let SEF handle predefined system events.
RS CHANGES:
- RS supports a basic single-component live update protocol now, as follows:
* When an update command is issued (via "service update *"), RS notifies the
target system process to prepare for a specific update state.
* If the process doesn't respond back in time, the update is aborted.
* When the process responds back, RS kills it and marks it for refreshing.
* The process is then automatically restarted as for a buggy process and can
start running again.
* Live update is currently prototyped as a controlled failure.
2009-12-21 15:12:21 +01:00
|
|
|
|
2013-08-02 16:10:30 +02:00
|
|
|
/* Do not reply to notifications. */
|
|
|
|
continue;
|
|
|
|
}
|
2007-01-12 17:35:04 +01:00
|
|
|
|
2013-08-02 16:10:30 +02:00
|
|
|
caller = m.m_source;
|
|
|
|
|
2013-08-07 03:34:08 +02:00
|
|
|
log_debug(&log, "Got message 0x%x from 0x%x\n", m.m_type,
|
|
|
|
caller);
|
2013-08-02 16:10:30 +02:00
|
|
|
|
|
|
|
switch (m.m_type) {
|
|
|
|
case RTCDEV_GET_TIME:
|
|
|
|
/* Any user can read the time */
|
2014-05-19 09:19:01 +02:00
|
|
|
reply_status = rtc.get_time(&t, m.m_lc_readclock_rtcdev.flags);
|
2013-08-02 16:10:30 +02:00
|
|
|
if (reply_status != OK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write results back to calling process */
|
|
|
|
reply_status =
|
2014-05-19 09:19:01 +02:00
|
|
|
store_t(caller, m.m_lc_readclock_rtcdev.tm, &t);
|
2013-08-02 16:10:30 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RTCDEV_SET_TIME:
|
|
|
|
/* Only super user is allowed to set the time */
|
|
|
|
if (getnuid(caller) == SUPER_USER) {
|
|
|
|
/* read time from calling process */
|
|
|
|
reply_status =
|
2014-05-19 09:19:01 +02:00
|
|
|
fetch_t(caller, m.m_lc_readclock_rtcdev.tm,
|
2013-08-02 16:10:30 +02:00
|
|
|
&t);
|
|
|
|
if (reply_status != OK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
reply_status =
|
2014-05-19 09:19:01 +02:00
|
|
|
rtc.set_time(&t, m.m_lc_readclock_rtcdev.flags);
|
2013-08-02 16:10:30 +02:00
|
|
|
} else {
|
|
|
|
reply_status = EPERM;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RTCDEV_PWR_OFF:
|
|
|
|
/* Only PM is allowed to set the power off time */
|
|
|
|
if (caller == PM_PROC_NR) {
|
2013-08-07 03:34:08 +02:00
|
|
|
reply_status = rtc.pwr_off();
|
2013-08-02 16:10:30 +02:00
|
|
|
} else {
|
|
|
|
reply_status = EPERM;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Unrecognized call */
|
|
|
|
reply_status = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2007-01-12 17:35:04 +01:00
|
|
|
|
2013-08-02 16:10:30 +02:00
|
|
|
/* Send Reply */
|
|
|
|
m.m_type = RTCDEV_REPLY;
|
2014-05-19 09:19:01 +02:00
|
|
|
m.m_readclock_lc_rtcdev.status = reply_status;
|
2007-01-12 17:35:04 +01:00
|
|
|
|
2013-08-02 16:10:30 +02:00
|
|
|
log_debug(&log, "Sending Reply");
|
2007-01-12 17:35:04 +01:00
|
|
|
|
2013-11-01 13:34:14 +01:00
|
|
|
r = ipc_sendnb(caller, &m);
|
2013-08-02 16:10:30 +02:00
|
|
|
if (r != OK) {
|
2013-11-01 13:34:14 +01:00
|
|
|
log_warn(&log, "ipc_sendnb() failed\n");
|
2013-08-02 16:10:30 +02:00
|
|
|
continue;
|
2007-01-12 17:35:04 +01:00
|
|
|
}
|
2013-08-02 16:10:30 +02:00
|
|
|
}
|
2007-01-12 17:35:04 +01:00
|
|
|
|
2013-08-07 03:34:08 +02:00
|
|
|
rtc.exit();
|
2013-08-02 16:10:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2007-01-12 17:35:04 +01:00
|
|
|
|
2013-08-02 16:10:30 +02:00
|
|
|
static int
|
|
|
|
sef_cb_init(int type, sef_init_info_t * UNUSED(info))
|
2007-01-12 17:35:04 +01:00
|
|
|
{
|
2013-08-02 16:10:30 +02:00
|
|
|
int r;
|
2007-01-12 17:35:04 +01:00
|
|
|
|
2013-08-07 03:34:08 +02:00
|
|
|
r = arch_setup(&rtc);
|
2013-08-02 16:10:30 +02:00
|
|
|
if (r != OK) {
|
2013-08-07 03:34:08 +02:00
|
|
|
log_warn(&log, "Clock setup failed\n");
|
2013-08-02 16:10:30 +02:00
|
|
|
return r;
|
|
|
|
}
|
2007-01-12 17:35:04 +01:00
|
|
|
|
2013-08-07 03:34:08 +02:00
|
|
|
r = rtc.init();
|
|
|
|
if (r != OK) {
|
|
|
|
log_warn(&log, "Clock initalization failed\n");
|
|
|
|
return r;
|
2013-08-02 16:10:30 +02:00
|
|
|
}
|
2007-01-12 17:35:04 +01:00
|
|
|
|
2013-08-02 16:10:30 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sef_local_startup()
|
2007-01-12 17:35:04 +01:00
|
|
|
{
|
2013-08-02 16:10:30 +02:00
|
|
|
/*
|
|
|
|
* Register init callbacks. Use the same function for all event types
|
|
|
|
*/
|
|
|
|
sef_setcb_init_fresh(sef_cb_init);
|
|
|
|
sef_setcb_init_lu(sef_cb_init);
|
|
|
|
sef_setcb_init_restart(sef_cb_init);
|
2007-01-12 17:35:04 +01:00
|
|
|
|
2013-08-02 16:10:30 +02:00
|
|
|
/*
|
|
|
|
* Register live update callbacks.
|
2007-01-12 17:35:04 +01:00
|
|
|
*/
|
2013-08-02 16:10:30 +02:00
|
|
|
/* Agree to update immediately when LU is requested in a valid state. */
|
|
|
|
sef_setcb_lu_prepare(sef_cb_lu_prepare_always_ready);
|
|
|
|
/* Support live update starting from any standard state. */
|
|
|
|
sef_setcb_lu_state_isvalid(sef_cb_lu_state_isvalid_standard);
|
|
|
|
|
|
|
|
/* Let SEF perform startup. */
|
|
|
|
sef_startup();
|
2007-01-12 17:35:04 +01:00
|
|
|
}
|
|
|
|
|
2013-08-02 16:10:30 +02:00
|
|
|
int
|
|
|
|
bcd_to_dec(int n)
|
2007-01-12 17:35:04 +01:00
|
|
|
{
|
2013-08-02 16:10:30 +02:00
|
|
|
return ((n >> 4) & 0x0F) * 10 + (n & 0x0F);
|
2007-01-12 17:35:04 +01:00
|
|
|
}
|
|
|
|
|
2013-08-02 16:10:30 +02:00
|
|
|
int
|
|
|
|
dec_to_bcd(int n)
|
2007-01-12 17:35:04 +01:00
|
|
|
{
|
2013-08-02 16:10:30 +02:00
|
|
|
return ((n / 10) << 4) | (n % 10);
|
2007-01-12 17:35:04 +01:00
|
|
|
}
|
|
|
|
|
2013-08-02 16:10:30 +02:00
|
|
|
static int
|
|
|
|
fetch_t(endpoint_t who_e, vir_bytes rtcdev_tm, struct tm *t)
|
2007-01-12 17:35:04 +01:00
|
|
|
{
|
2013-08-02 16:10:30 +02:00
|
|
|
return sys_datacopy(who_e, rtcdev_tm, SELF, (vir_bytes) t,
|
|
|
|
sizeof(struct tm));
|
2007-01-12 17:35:04 +01:00
|
|
|
}
|
|
|
|
|
2013-08-02 16:10:30 +02:00
|
|
|
static int
|
|
|
|
store_t(endpoint_t who_e, vir_bytes rtcdev_tm, struct tm *t)
|
2007-01-12 17:35:04 +01:00
|
|
|
{
|
2013-08-02 16:10:30 +02:00
|
|
|
return sys_datacopy(SELF, (vir_bytes) t, who_e, rtcdev_tm,
|
|
|
|
sizeof(struct tm));
|
2007-01-12 17:35:04 +01:00
|
|
|
}
|