1 | /* |
---|
2 | * adjime() function |
---|
3 | * |
---|
4 | * This method was initially added as part of porting NTP to RTEMS. |
---|
5 | * It is a BSD compatability function and now is available on |
---|
6 | * GNU/Linux. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * COPYRIGHT (c) 1989-2007. |
---|
11 | * On-Line Applications Research Corporation (OAR). |
---|
12 | * |
---|
13 | * The license and distribution terms for this file may be |
---|
14 | * found in the file LICENSE in this distribution or at |
---|
15 | * http://www.rtems.com/license/LICENSE. |
---|
16 | * |
---|
17 | * $Id$ |
---|
18 | */ |
---|
19 | |
---|
20 | #if HAVE_CONFIG_H |
---|
21 | #include "config.h" |
---|
22 | #endif |
---|
23 | |
---|
24 | #include <time.h> |
---|
25 | #include <sys/time.h> |
---|
26 | #include <errno.h> |
---|
27 | |
---|
28 | #include <rtems/system.h> |
---|
29 | #include <rtems/config.h> |
---|
30 | #include <rtems/seterr.h> |
---|
31 | #include <rtems/score/tod.h> |
---|
32 | #include <rtems/posix/time.h> |
---|
33 | #include <rtems/score/thread.h> |
---|
34 | #include <rtems/score/timespec.h> |
---|
35 | |
---|
36 | /* |
---|
37 | * At one point there was a static variable named adjustment |
---|
38 | * used by this implementation. I don't see any reason for it |
---|
39 | * to be here based upon the GNU/Linux documentation. |
---|
40 | */ |
---|
41 | |
---|
42 | int adjtime( |
---|
43 | struct timeval *delta, |
---|
44 | struct timeval *olddelta |
---|
45 | ) |
---|
46 | { |
---|
47 | struct timespec ts; |
---|
48 | long adjustment; |
---|
49 | |
---|
50 | /* |
---|
51 | * Simple validations |
---|
52 | */ |
---|
53 | if ( !delta ) |
---|
54 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
55 | |
---|
56 | if ( delta->tv_usec >= TOD_MICROSECONDS_PER_SECOND ) |
---|
57 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
58 | |
---|
59 | if ( olddelta ) { |
---|
60 | olddelta->tv_sec = 0; |
---|
61 | olddelta->tv_usec = 0; |
---|
62 | } |
---|
63 | |
---|
64 | /* convert delta to microseconds */ |
---|
65 | adjustment = (delta->tv_sec * TOD_MICROSECONDS_PER_SECOND); |
---|
66 | adjustment += delta->tv_usec; |
---|
67 | |
---|
68 | /* too small to account for */ |
---|
69 | if ( adjustment < rtems_configuration_get_microseconds_per_tick() ) |
---|
70 | return 0; |
---|
71 | |
---|
72 | /* |
---|
73 | * This prevents context switches while we are adjusting the TOD |
---|
74 | */ |
---|
75 | |
---|
76 | _Thread_Disable_dispatch(); |
---|
77 | |
---|
78 | _TOD_Get( &ts ); |
---|
79 | |
---|
80 | ts.tv_sec += delta->tv_sec; |
---|
81 | ts.tv_nsec += delta->tv_usec * TOD_NANOSECONDS_PER_MICROSECOND; |
---|
82 | |
---|
83 | /* if adjustment is too much positive */ |
---|
84 | while ( ts.tv_nsec >= TOD_NANOSECONDS_PER_SECOND ) { |
---|
85 | ts.tv_nsec -= TOD_NANOSECONDS_PER_SECOND; |
---|
86 | ts.tv_sec++; |
---|
87 | } |
---|
88 | |
---|
89 | /* if adjustment is too much negative */ |
---|
90 | while ( ts.tv_nsec <= (-1 * TOD_NANOSECONDS_PER_SECOND) ) { |
---|
91 | ts.tv_nsec += TOD_NANOSECONDS_PER_SECOND; |
---|
92 | ts.tv_sec--; |
---|
93 | } |
---|
94 | |
---|
95 | _TOD_Set( &ts ); |
---|
96 | |
---|
97 | _Thread_Enable_dispatch(); |
---|
98 | |
---|
99 | /* set the user's output */ |
---|
100 | if ( olddelta ) |
---|
101 | *olddelta = *delta; |
---|
102 | |
---|
103 | return 0; |
---|
104 | } |
---|