#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ntp_machine.h"
#include "ntp_syslog.h"
#include "ntp_stdlib.h"
#include "ntp_unixtime.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef SYS_WINNT
int _getch(void);
#else
#ifdef SYS_VXWORKS
#include "taskLib.h"
#include "sysLib.h"
#include "time.h"
#include "ntp_syslog.h"
#include "ioLib.h"
#include <socket.h>
int h_errno;
struct hostent *gethostbyname(char *name)
{
struct hostent *host1;
h_errno = 0;
host1 = (struct hostent *) malloc (sizeof(struct hostent));
host1->h_name = name;
host1->h_addrtype = AF_INET;
host1->h_aliases = name;
host1->h_length = 4;
host1->h_addr_list[0] = (char *)hostGetByName (name);
host1->h_addr_list[1] = NULL;
return host1;
}
struct hostent *gethostbyaddr(char *name, int size, int addr_type)
{
struct hostent *host1;
h_errno = 0;
host1 = (struct hostent *) malloc (sizeof(struct hostent));
host1->h_name = name;
host1->h_addrtype = AF_INET;
host1->h_aliases = name;
host1->h_length = 4;
host1->h_addr_list = NULL;
return host1;
}
struct servent *getservbyname (char *name, char *type)
{
struct servent *serv1;
serv1 = (struct servent *) malloc (sizeof(struct servent));
serv1->s_name = "ntp";
serv1->s_aliases = NULL;
serv1->s_port = 123;
serv1->s_proto = "udp";
return serv1;
}
#define CLKRATE sysClkRateGet()
#define CLK_GRANULARITY 1
#define USECS_PER_SEC MILLION
#define TICK (((USECS_PER_SEC / CLKRATE) / CLK_GRANULARITY) * CLK_GRANULARITY)
void sleep(int seconds)
{
taskDelay(seconds*TICK);
}
void alarm (int seconds)
{
sleep(seconds);
}
#endif
#ifdef SYS_PTX
#include <sys/procstats.h>
int
gettimeofday(
struct timeval *tvp
)
{
return (get_process_stats(tvp, PS_SELF, (struct procstats *) 0,
(struct procstats *) 0));
}
#endif
#ifdef MPE
#undef bind
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
extern void GETPRIVMODE(void);
extern void GETUSERMODE(void);
int __ntp_mpe_bind(int s, void *addr, int addrlen);
int __ntp_mpe_bind(int s, void *addr, int addrlen) {
int priv = 0;
int result;
if (addrlen == sizeof(struct sockaddr_in)) {
if (((struct sockaddr_in *)addr)->sin_port > 0 &&
((struct sockaddr_in *)addr)->sin_port < 1024) {
priv = 1;
GETPRIVMODE();
}
result = bind(s,addr,addrlen);
if (priv == 1) GETUSERMODE();
} else
result = bind(s,addr,addrlen);
return result;
}
#undef fcntl
#include <errno.h>
#include <fcntl.h>
int __ntp_mpe_fcntl(int fd, int cmd, int arg);
int __ntp_mpe_fcntl(int fd, int cmd, int arg) {
int len;
struct sockaddr sa;
extern int sfcntl(int, int, int);
len = sizeof sa;
if (getsockname(fd, &sa, &len) == -1) {
if (errno == EAFNOSUPPORT)
return sfcntl(fd, cmd, arg);
if (errno == ENOTSOCK)
return fcntl(fd, cmd, arg);
return (-1);
} else
return sfcntl(fd, cmd, arg);
}
#include <limits.h>
#include <signal.h>
struct {
unsigned long current_msec;
unsigned long interval_msec;
unsigned long value_msec;
struct itimerval current_itimerval;
struct sigaction oldact;
} setitimer_mpe_ctx = { 0, 0, 0 };
extern unsigned int px_alarm(unsigned long, int *);
static RETSIGTYPE setitimer_mpe_handler(int sig)
{
int alarm_hpe_status;
setitimer_mpe_ctx.current_msec = setitimer_mpe_ctx.interval_msec;
if (setitimer_mpe_ctx.interval_msec > 0) {
px_alarm(setitimer_mpe_ctx.interval_msec,&alarm_hpe_status);
} else {
sigaction(SIGALRM, &setitimer_mpe_ctx.oldact, NULL);
}
if (setitimer_mpe_ctx.oldact.sa_handler != SIG_DFL &&
setitimer_mpe_ctx.oldact.sa_handler != SIG_ERR &&
setitimer_mpe_ctx.oldact.sa_handler != SIG_IGN)
(*setitimer_mpe_ctx.oldact.sa_handler)(SIGALRM);
}
int
setitimer(int which, struct itimerval *value,
struct itimerval *ovalue)
{
int alarm_hpe_status;
unsigned long remaining_msec, value_msec, interval_msec;
struct sigaction newact;
if (value->it_value.tv_sec > (UINT_MAX / 1000))
value_msec = UINT_MAX;
else
value_msec = value->it_value.tv_sec * 1000;
value_msec += value->it_value.tv_usec / 1000;
if (value->it_interval.tv_sec > (UINT_MAX / 1000))
interval_msec = UINT_MAX;
else
interval_msec = value->it_interval.tv_sec * 1000;
interval_msec += value->it_interval.tv_usec / 1000;
if (value_msec > 0 && interval_msec > 0) {
if (sigaction(SIGALRM, NULL, &setitimer_mpe_ctx.oldact) < 0) {
fprintf(stderr,"MPE setitimer old handler failed, errno=%d\n",errno);
return -1;
}
newact.sa_handler = &setitimer_mpe_handler;
newact.sa_mask = setitimer_mpe_ctx.oldact.sa_mask;
newact.sa_flags = setitimer_mpe_ctx.oldact.sa_flags;
if (sigaction(SIGALRM, &newact, NULL) < 0) {
fprintf(stderr,"MPE setitimer new handler failed, errno=%d\n",errno);
return -1;
}
}
if (ovalue != NULL) *ovalue = setitimer_mpe_ctx.current_itimerval;
setitimer_mpe_ctx.current_itimerval = *value;
setitimer_mpe_ctx.current_msec = value_msec;
setitimer_mpe_ctx.value_msec = value_msec;
setitimer_mpe_ctx.interval_msec = interval_msec;
remaining_msec = px_alarm(value_msec, &alarm_hpe_status);
if (alarm_hpe_status == 0)
return (0);
else
return (-1);
}
int gettimeofday(struct timeval *tvp)
{
extern void GETPRIVMODE(void);
extern void GETUSERMODE(void);
extern long long get_time(void);
extern void get_time_change_info(long long *, char *, char *);
extern long long ticks_to_micro(long long);
char pwf_since_boot, recover_pwf_time;
long long mpetime, offset_ticks, offset_usec;
GETPRIVMODE();
mpetime = get_time();
get_time_change_info(&offset_ticks, &pwf_since_boot, &recover_pwf_time);
offset_usec = ticks_to_micro(offset_ticks);
GETUSERMODE();
mpetime = mpetime - offset_usec;
tvp->tv_sec = mpetime / 1000000LL;
tvp->tv_usec = mpetime % 1000000LL;
return 0;
}
#define HAVE_SETTIMEOFDAY
int settimeofday(struct timeval *tvp)
{
extern void GETPRIVMODE(void);
extern void GETUSERMODE(void);
extern void get_time_change_info(long long *, char *, char *);
extern void initialize_system_time(long long, int);
extern void set_time_correction(long long, int, int);
extern long long ticks_to_micro(long long);
char pwf_since_boot, recover_pwf_time;
long long big_sec, big_usec, mpetime, offset_ticks, offset_usec;
big_sec = tvp->tv_sec;
big_usec = tvp->tv_usec;
mpetime = (big_sec * 1000000LL) + big_usec;
GETPRIVMODE();
set_time_correction(0LL,0,0);
get_time_change_info(&offset_ticks, &pwf_since_boot, &recover_pwf_time);
offset_usec = ticks_to_micro(offset_ticks);
mpetime = mpetime + offset_usec;
initialize_system_time(mpetime,1);
GETUSERMODE();
return 0;
}
#endif
const char *set_tod_using = "UNKNOWN";
int
ntp_set_tod(
struct timeval *tvp,
void *tzp
)
{
int rc = -1;
#ifdef DEBUG
if (debug)
printf("In ntp_set_tod\n");
#endif
#ifdef HAVE_CLOCK_SETTIME
if (rc) {
struct timespec ts;
set_tod_using = "clock_settime";
ts.tv_sec = tvp->tv_sec;
ts.tv_nsec = 1000 * tvp->tv_usec;
errno = 0;
rc = clock_settime(CLOCK_REALTIME, &ts);
#ifdef DEBUG
if (debug) {
printf("ntp_set_tod: %s: %d: %s\n",
set_tod_using, rc, strerror(errno));
}
#endif
}
#endif
#ifdef HAVE_SETTIMEOFDAY
if (rc) {
struct timeval adjtv;
set_tod_using = "settimeofday";
adjtv.tv_sec = adjtv.tv_usec = 0;
adjtime(&adjtv, NULL);
errno = 0;
rc = SETTIMEOFDAY(tvp, tzp);
#ifdef DEBUG
if (debug) {
printf("ntp_set_tod: %s: %d: %s\n",
set_tod_using, rc, strerror(errno));
}
#endif
}
#endif
#ifdef HAVE_STIME
if (rc) {
long tp = tvp->tv_sec;
set_tod_using = "stime";
errno = 0;
rc = stime(&tp);
#ifdef DEBUG
if (debug) {
printf("ntp_set_tod: %s: %d: %s\n",
set_tod_using, rc, strerror(errno));
}
#endif
}
#endif
if (rc)
set_tod_using = "Failed!";
#ifdef DEBUG
if (debug) {
printf("ntp_set_tod: Final result: %s: %d: %s\n",
set_tod_using, rc, strerror(errno));
}
#endif
return rc;
}
#endif
#if defined (SYS_WINNT) || defined (SYS_VXWORKS) || defined(MPE)
char *
getpass(const char * prompt)
{
int c, i;
static char password[32];
#ifdef DEBUG
fprintf(stderr, "%s", prompt);
fflush(stderr);
#endif
for (i=0; i<sizeof(password)-1 && ((c=_getch())!='\n' && c!='\r'); i++) {
password[i] = (char) c;
}
password[i] = '\0';
return password;
}
#endif
#if !defined(HAVE_MEMSET)
void
ntp_memset(
char *a,
int x,
int c
)
{
while (c-- > 0)
*a++ = (char) x;
}
#endif