1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "time.h"
33 #include "stdint.h"
34 #include "stdio.h"
35 #include "sys/times.h"
36 #include "time_posix.h"
37 #include "unistd.h"
38 #ifdef LOSCFG_SECURITY_CAPABILITY
39 #include "capability_api.h"
40 #endif
41 #include "los_signal.h"
42 #ifdef LOSCFG_KERNEL_VDSO
43 #include "los_vdso.h"
44 #endif
45 #ifdef LOSCFG_SECURITY_VID
46 #include "vid_api.h"
47 #endif
48 #include "user_copy.h"
49 #include "los_process_pri.h"
50 #include "los_swtmr_pri.h"
51 #include "los_sys_pri.h"
52
53 #define CPUCLOCK_PERTHREAD_MASK 4
54 #define CPUCLOCK_ID_OFFSET 3
55
56 /*
57 * Do a time package defined return. This requires the error code
58 * to be placed in errno, and if it is non-zero, -1 returned as the
59 * result of the function. This also gives us a place to put any
60 * generic tidyup handling needed for things like signal delivery and
61 * cancellation.
62 */
63 #define TIME_RETURN(err) do { \
64 INT32 retVal = 0; \
65 if ((err) != 0) { \
66 retVal = -1; \
67 errno = (err); \
68 } \
69 return retVal; \
70 } while (0)
71
72 #ifdef LOSCFG_AARCH64
73 /*
74 * This two structures originally didn't exit,
75 * they added by liteos to support 64bit interfaces on 32bit platform,
76 * in 64bit platform, timeval64 define to timeval which is platform adaptive.
77 */
78 #define timeval64 timeval
79 #define timespec64 timespec
80 #endif
81
ValidTimeval(const struct timeval * tv)82 STATIC INLINE BOOL ValidTimeval(const struct timeval *tv)
83 {
84 /* Fail a NULL pointer */
85 if (tv == NULL) {
86 return FALSE;
87 }
88
89 /* Fail illegal microseconds values */
90 if ((tv->tv_usec < 0) || (tv->tv_usec >= OS_SYS_US_PER_SECOND) || (tv->tv_sec < 0)) {
91 return FALSE;
92 }
93
94 return TRUE;
95 }
96
ValidTimeval64(const struct timeval64 * tv)97 STATIC INLINE BOOL ValidTimeval64(const struct timeval64 *tv)
98 {
99 /* Fail a NULL pointer */
100 if (tv == NULL) {
101 return FALSE;
102 }
103
104 /* Fail illegal microseconds values */
105 if ((tv->tv_usec < 0) || (tv->tv_usec >= OS_SYS_US_PER_SECOND) || (tv->tv_sec < 0)) {
106 return FALSE;
107 }
108
109 return TRUE;
110 }
111
ValidTimerID(UINT16 swtmrID)112 STATIC INLINE BOOL ValidTimerID(UINT16 swtmrID)
113 {
114 /* check timer id */
115 if (swtmrID >= OS_SWTMR_MAX_TIMERID) {
116 return FALSE;
117 }
118
119 /* check owner of this timer */
120 if (OS_SWT_FROM_SID(swtmrID)->uwOwnerPid != LOS_GetCurrProcessID()) {
121 return FALSE;
122 }
123
124 return TRUE;
125 }
126
127 STATIC SPIN_LOCK_INIT(g_timeSpin);
128 STATIC long long g_adjTimeLeft; /* absolute value of adjtime */
129 STATIC INT32 g_adjDirection; /* 1, speed up; 0, slow down; */
130
131 /* Adjust pacement, nanoseconds per SCHED_CLOCK_INTETRVAL_TICKS ticks */
132 STATIC const long long g_adjPacement = (((LOSCFG_BASE_CORE_ADJ_PER_SECOND * SCHED_CLOCK_INTETRVAL_TICKS) /
133 LOSCFG_BASE_CORE_TICK_PER_SECOND) * OS_SYS_NS_PER_US);
134
135 /* accumulative time delta from continuous modify, such as adjtime */
136 STATIC struct timespec64 g_accDeltaFromAdj;
137 /* accumulative time delta from discontinuous modify, such as settimeofday */
138 STATIC struct timespec64 g_accDeltaFromSet;
139
OsAdjTime(VOID)140 VOID OsAdjTime(VOID)
141 {
142 UINT32 intSave;
143
144 LOS_SpinLockSave(&g_timeSpin, &intSave);
145 if (!g_adjTimeLeft) {
146 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
147 return;
148 }
149
150 if (g_adjTimeLeft > g_adjPacement) {
151 if (g_adjDirection) {
152 if ((g_accDeltaFromAdj.tv_nsec + g_adjPacement) >= OS_SYS_NS_PER_SECOND) {
153 g_accDeltaFromAdj.tv_sec++;
154 g_accDeltaFromAdj.tv_nsec = (g_accDeltaFromAdj.tv_nsec + g_adjPacement) % OS_SYS_NS_PER_SECOND;
155 } else {
156 g_accDeltaFromAdj.tv_nsec = g_accDeltaFromAdj.tv_nsec + g_adjPacement;
157 }
158 } else {
159 if ((g_accDeltaFromAdj.tv_nsec - g_adjPacement) < 0) {
160 g_accDeltaFromAdj.tv_sec--;
161 g_accDeltaFromAdj.tv_nsec = g_accDeltaFromAdj.tv_nsec - g_adjPacement + OS_SYS_NS_PER_SECOND;
162 } else {
163 g_accDeltaFromAdj.tv_nsec = g_accDeltaFromAdj.tv_nsec - g_adjPacement;
164 }
165 }
166
167 g_adjTimeLeft -= g_adjPacement;
168 } else {
169 if (g_adjDirection) {
170 if ((g_accDeltaFromAdj.tv_nsec + g_adjTimeLeft) >= OS_SYS_NS_PER_SECOND) {
171 g_accDeltaFromAdj.tv_sec++;
172 g_accDeltaFromAdj.tv_nsec = (g_accDeltaFromAdj.tv_nsec + g_adjTimeLeft) % OS_SYS_NS_PER_SECOND;
173 } else {
174 g_accDeltaFromAdj.tv_nsec = g_accDeltaFromAdj.tv_nsec + g_adjTimeLeft;
175 }
176 } else {
177 if ((g_accDeltaFromAdj.tv_nsec - g_adjTimeLeft) < 0) {
178 g_accDeltaFromAdj.tv_sec--;
179 g_accDeltaFromAdj.tv_nsec = g_accDeltaFromAdj.tv_nsec - g_adjTimeLeft + OS_SYS_NS_PER_SECOND;
180 } else {
181 g_accDeltaFromAdj.tv_nsec = g_accDeltaFromAdj.tv_nsec - g_adjTimeLeft;
182 }
183 }
184
185 g_adjTimeLeft = 0;
186 }
187 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
188 return;
189 }
190
191 /*
192 * Function: adjtime
193 * Description: correct the time to synchronize the system clock.
194 * Input: delta - The amount of time by which the clock is to be adjusted.
195 * Output: oldDelta - the amount of time remaining from any previous adjustment that has not yet been completed.
196 * Return: On success, returns 0. On failure, -1 is returned, and errno is set to indicate the error.
197 */
adjtime(const struct timeval * delta,struct timeval * oldDelta)198 int adjtime(const struct timeval *delta, struct timeval *oldDelta)
199 {
200 UINT32 intSave;
201 LOS_SpinLockSave(&g_timeSpin, &intSave);
202 /* return the amount of time remaining from any previous adjustment that has not yet been completed. */
203 if (oldDelta != NULL) {
204 if (g_adjDirection == 1) {
205 oldDelta->tv_sec = g_adjTimeLeft / OS_SYS_NS_PER_SECOND;
206 oldDelta->tv_usec = (g_adjTimeLeft % OS_SYS_NS_PER_SECOND) / OS_SYS_NS_PER_US;
207 } else {
208 oldDelta->tv_sec = -(g_adjTimeLeft / OS_SYS_NS_PER_SECOND);
209 oldDelta->tv_usec = -((g_adjTimeLeft % OS_SYS_NS_PER_SECOND) / OS_SYS_NS_PER_US);
210 }
211 }
212
213 if ((delta == NULL) || ((delta->tv_sec == 0) && (delta->tv_usec == 0))) {
214 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
215 return 0;
216 }
217
218 if ((delta->tv_usec > OS_SYS_US_PER_SECOND) || (delta->tv_usec < -OS_SYS_US_PER_SECOND)) {
219 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
220 TIME_RETURN(EINVAL);
221 }
222
223 /*
224 * 2: in the glibc implementation, delta must be less than or equal to (INT_MAX / 1000000 - 2) and
225 * greater than or equal to (INT_MIN / 1000000 + 2)
226 */
227 if ((delta->tv_sec < (INT_MIN / OS_SYS_US_PER_SECOND + 2)) ||
228 (delta->tv_sec > (INT_MAX / OS_SYS_US_PER_SECOND + 2))) {
229 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
230 TIME_RETURN(EINVAL);
231 }
232
233 g_adjTimeLeft = (INT64)delta->tv_sec * OS_SYS_NS_PER_SECOND + delta->tv_usec * OS_SYS_NS_PER_US;
234 if (g_adjTimeLeft > 0) {
235 g_adjDirection = 1;
236 } else {
237 g_adjDirection = 0;
238 g_adjTimeLeft = -g_adjTimeLeft;
239 }
240
241 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
242 return 0;
243 }
244
OsTimeSpecAdd(const struct timespec64 t1,const struct timespec64 t2)245 STATIC INLINE struct timespec64 OsTimeSpecAdd(const struct timespec64 t1, const struct timespec64 t2)
246 {
247 struct timespec64 ret = {0};
248
249 ret.tv_sec = t1.tv_sec + t2.tv_sec;
250 ret.tv_nsec = t1.tv_nsec + t2.tv_nsec;
251 if (ret.tv_nsec >= OS_SYS_NS_PER_SECOND) {
252 ret.tv_sec += 1;
253 ret.tv_nsec -= OS_SYS_NS_PER_SECOND;
254 } else if (ret.tv_nsec < 0L) {
255 ret.tv_sec -= 1;
256 ret.tv_nsec += OS_SYS_NS_PER_SECOND;
257 }
258
259 return ret;
260 }
261
OsTimeSpecSub(const struct timespec64 t1,const struct timespec64 t2)262 STATIC INLINE struct timespec64 OsTimeSpecSub(const struct timespec64 t1, const struct timespec64 t2)
263 {
264 struct timespec64 ret = {0};
265
266 ret.tv_sec = t1.tv_sec - t2.tv_sec;
267 ret.tv_nsec = t1.tv_nsec - t2.tv_nsec;
268 if (ret.tv_nsec < 0) {
269 ret.tv_sec -= 1;
270 ret.tv_nsec += OS_SYS_NS_PER_SECOND;
271 }
272
273 return ret;
274 }
275
OsGetHwTime(struct timespec64 * hwTime)276 STATIC VOID OsGetHwTime(struct timespec64 *hwTime)
277 {
278 UINT64 nowNsec;
279
280 nowNsec = LOS_CurrNanosec();
281 hwTime->tv_sec = nowNsec / OS_SYS_NS_PER_SECOND;
282 hwTime->tv_nsec = nowNsec - hwTime->tv_sec * OS_SYS_NS_PER_SECOND;
283 }
284
OsSetTimeOfDay(const struct timeval64 * tv,const struct timezone * tz)285 STATIC INT32 OsSetTimeOfDay(const struct timeval64 *tv, const struct timezone *tz)
286 {
287 UINT32 intSave;
288 struct timespec64 setTime = {0};
289 struct timespec64 hwTime = {0};
290 struct timespec64 realTime = {0};
291 struct timespec64 tmp = {0};
292
293 #ifdef LOSCFG_SECURITY_CAPABILITY
294 if (!IsCapPermit(CAP_SET_TIMEOFDAY)) {
295 TIME_RETURN(EPERM);
296 }
297 #endif
298
299 (VOID)tz;
300 OsGetHwTime(&hwTime);
301 setTime.tv_sec = tv->tv_sec;
302 setTime.tv_nsec = tv->tv_usec * OS_SYS_NS_PER_US;
303
304 LOS_SpinLockSave(&g_timeSpin, &intSave);
305 /* stop on-going continuous adjusement */
306 if (g_adjTimeLeft) {
307 g_adjTimeLeft = 0;
308 }
309 realTime = OsTimeSpecAdd(hwTime, g_accDeltaFromAdj);
310 realTime = OsTimeSpecAdd(realTime, g_accDeltaFromSet);
311
312 tmp = OsTimeSpecSub(setTime, realTime);
313 g_accDeltaFromSet = OsTimeSpecAdd(g_accDeltaFromSet, tmp);
314
315 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
316
317 return 0;
318 }
319
settimeofday(const struct timeval * tv,const struct timezone * tz)320 int settimeofday(const struct timeval *tv, const struct timezone *tz)
321 {
322 struct timeval64 stTimeVal64 = {0};
323
324 if (!ValidTimeval(tv)) {
325 TIME_RETURN(EINVAL);
326 }
327
328 stTimeVal64.tv_sec = tv->tv_sec;
329 stTimeVal64.tv_usec = tv->tv_usec;
330
331 return OsSetTimeOfDay(&stTimeVal64, tz);
332 }
333
334 #ifndef LOSCFG_AARCH64
settimeofday64(const struct timeval64 * tv,const struct timezone * tz)335 int settimeofday64(const struct timeval64 *tv, const struct timezone *tz)
336 {
337 if (!ValidTimeval64(tv)) {
338 TIME_RETURN(EINVAL);
339 }
340
341 return OsSetTimeOfDay(tv, tz);
342 }
343 #endif
344
setlocalseconds(int seconds)345 int setlocalseconds(int seconds)
346 {
347 struct timeval tv = {0};
348
349 tv.tv_sec = seconds;
350 tv.tv_usec = 0;
351
352 return settimeofday(&tv, NULL);
353 }
354
OsGetTimeOfDay(struct timeval64 * tv,struct timezone * tz)355 STATIC INT32 OsGetTimeOfDay(struct timeval64 *tv, struct timezone *tz)
356 {
357 UINT32 intSave;
358
359 (VOID)tz;
360 struct timespec64 hwTime = {0};
361 struct timespec64 realTime = {0};
362
363 OsGetHwTime(&hwTime);
364
365 LOS_SpinLockSave(&g_timeSpin, &intSave);
366 realTime = OsTimeSpecAdd(hwTime, g_accDeltaFromAdj);
367 realTime = OsTimeSpecAdd(realTime, g_accDeltaFromSet);
368 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
369
370 tv->tv_sec = realTime.tv_sec;
371 tv->tv_usec = realTime.tv_nsec / OS_SYS_NS_PER_US;
372
373 if (tv->tv_sec < 0) {
374 TIME_RETURN(EINVAL);
375 }
376 return 0;
377 }
378
379 #ifndef LOSCFG_AARCH64
gettimeofday64(struct timeval64 * tv,struct timezone * tz)380 int gettimeofday64(struct timeval64 *tv, struct timezone *tz)
381 {
382 if (tv == NULL) {
383 TIME_RETURN(EINVAL);
384 }
385
386 return OsGetTimeOfDay(tv, tz);
387 }
388 #endif
389
390 #ifdef LOSCFG_LIBC_NEWLIB
gettimeofday(struct timeval * tv,void * _tz)391 int gettimeofday(struct timeval *tv, void *_tz)
392 #else
393 int gettimeofday(struct timeval *tv, struct timezone *tz)
394 #endif
395 {
396 struct timeval64 stTimeVal64 = {0};
397 #ifdef LOSCFG_LIBC_NEWLIB
398 struct timezone *tz = (struct timezone *)_tz;
399 #endif
400
401 if (tv == NULL) {
402 TIME_RETURN(EINVAL);
403 }
404
405 if (OsGetTimeOfDay(&stTimeVal64, tz) == -1) {
406 return -1;
407 }
408
409 #ifdef LOSCFG_AARCH64
410 tv->tv_sec = stTimeVal64.tv_sec;
411 tv->tv_usec = stTimeVal64.tv_usec;
412 #else
413 if (stTimeVal64.tv_sec > (long long)LONG_MAX) {
414 return -1;
415 }
416 tv->tv_sec = (time_t)stTimeVal64.tv_sec;
417 tv->tv_usec = (suseconds_t)stTimeVal64.tv_usec;
418 #endif
419
420 return 0;
421 }
422
clock_settime(clockid_t clockID,const struct timespec * tp)423 int clock_settime(clockid_t clockID, const struct timespec *tp)
424 {
425 struct timeval tv = {0};
426
427 switch (clockID) {
428 case CLOCK_REALTIME:
429 /* we only support the realtime clock currently */
430 break;
431 case CLOCK_MONOTONIC_COARSE:
432 case CLOCK_REALTIME_COARSE:
433 case CLOCK_MONOTONIC_RAW:
434 case CLOCK_PROCESS_CPUTIME_ID:
435 case CLOCK_BOOTTIME:
436 case CLOCK_REALTIME_ALARM:
437 case CLOCK_BOOTTIME_ALARM:
438 case CLOCK_TAI:
439 case CLOCK_THREAD_CPUTIME_ID:
440 TIME_RETURN(ENOTSUP);
441 case CLOCK_MONOTONIC:
442 default:
443 TIME_RETURN(EINVAL);
444 }
445
446 if (!ValidTimeSpec(tp)) {
447 TIME_RETURN(EINVAL);
448 }
449
450 #ifdef LOSCFG_SECURITY_CAPABILITY
451 if (!IsCapPermit(CAP_CLOCK_SETTIME)) {
452 TIME_RETURN(EPERM);
453 }
454 #endif
455
456 tv.tv_sec = tp->tv_sec;
457 tv.tv_usec = tp->tv_nsec / OS_SYS_NS_PER_US;
458 return settimeofday(&tv, NULL);
459 }
460
461 #ifdef LOSCFG_KERNEL_CPUP
GetTidFromClockID(clockid_t clockID)462 inline UINT32 GetTidFromClockID(clockid_t clockID)
463 {
464 // In musl/src/thread/pthread_getcpuclockid.c, we know 'clockid = (-tid - 1) * 8 + 6'
465 UINT32 tid = -(clockID - 6) / 8 - 1; // 6 8 1 inverse operation from clockID to tid
466 return tid;
467 }
468
GetPidFromClockID(clockid_t clockID)469 inline const pid_t GetPidFromClockID(clockid_t clockID)
470 {
471 // In musl/src/time/clock_getcpuclockid.c, we know 'clockid = (-pid - 1) * 8 + 2'
472 const pid_t pid = -(clockID - 2) / 8 - 1; // 2 8 1 inverse operation from clockID to pid
473 return pid;
474 }
475
PthreadGetCputime(clockid_t clockID,struct timespec * ats)476 static int PthreadGetCputime(clockid_t clockID, struct timespec *ats)
477 {
478 uint64_t runtime;
479 UINT32 intSave;
480 UINT32 tid = GetTidFromClockID(clockID);
481 if (OS_TID_CHECK_INVALID(tid)) {
482 return -EINVAL;
483 }
484
485 LosTaskCB *task = OsGetTaskCB(tid);
486
487 if (OsCurrTaskGet()->processID != task->processID) {
488 return -EINVAL;
489 }
490
491 SCHEDULER_LOCK(intSave);
492 runtime = task->taskCpup.allTime;
493 SCHEDULER_UNLOCK(intSave);
494
495 ats->tv_sec = runtime / OS_SYS_NS_PER_SECOND;
496 ats->tv_nsec = runtime % OS_SYS_NS_PER_SECOND;
497
498 return 0;
499 }
500
ProcessGetCputime(clockid_t clockID,struct timespec * ats)501 static int ProcessGetCputime(clockid_t clockID, struct timespec *ats)
502 {
503 UINT64 runtime;
504 UINT32 intSave;
505 const pid_t pid = GetPidFromClockID(clockID);
506 LosProcessCB *spcb = NULL;
507
508 if (OsProcessIDUserCheckInvalid(pid) || pid < 0) {
509 return -EINVAL;
510 }
511
512 spcb = OS_PCB_FROM_PID(pid);
513 if (OsProcessIsUnused(spcb)) {
514 return -EINVAL;
515 }
516
517 SCHEDULER_LOCK(intSave);
518 if (spcb->processCpup == NULL) {
519 SCHEDULER_UNLOCK(intSave);
520 return -EINVAL;
521 }
522 runtime = spcb->processCpup->allTime;
523 SCHEDULER_UNLOCK(intSave);
524
525 ats->tv_sec = runtime / OS_SYS_NS_PER_SECOND;
526 ats->tv_nsec = runtime % OS_SYS_NS_PER_SECOND;
527
528 return 0;
529 }
530
GetCputime(clockid_t clockID,struct timespec * tp)531 static int GetCputime(clockid_t clockID, struct timespec *tp)
532 {
533 int ret;
534
535 if (clockID >= 0) {
536 return -EINVAL;
537 }
538
539 if ((UINT32)clockID & CPUCLOCK_PERTHREAD_MASK) {
540 ret = PthreadGetCputime(clockID, tp);
541 } else {
542 ret = ProcessGetCputime(clockID, tp);
543 }
544
545 return ret;
546 }
547
CheckClock(const clockid_t clockID)548 static int CheckClock(const clockid_t clockID)
549 {
550 int error = 0;
551 const pid_t pid = GetPidFromClockID(clockID);
552
553 if (!((UINT32)clockID & CPUCLOCK_PERTHREAD_MASK)) {
554 LosProcessCB *spcb = NULL;
555 if (OsProcessIDUserCheckInvalid(pid) || pid < 0) {
556 return -EINVAL;
557 }
558 spcb = OS_PCB_FROM_PID(pid);
559 if (OsProcessIsUnused(spcb)) {
560 error = -EINVAL;
561 }
562 } else {
563 error = -EINVAL;
564 }
565
566 return error;
567 }
568
CpuClockGetres(const clockid_t clockID,struct timespec * tp)569 static int CpuClockGetres(const clockid_t clockID, struct timespec *tp)
570 {
571 if (clockID > 0) {
572 return -EINVAL;
573 }
574
575 int error = CheckClock(clockID);
576 if (!error) {
577 error = ProcessGetCputime(clockID, tp);
578 }
579
580 return error;
581 }
582 #endif
583
clock_gettime(clockid_t clockID,struct timespec * tp)584 int clock_gettime(clockid_t clockID, struct timespec *tp)
585 {
586 UINT32 intSave;
587 struct timespec64 tmp = {0};
588 struct timespec64 hwTime = {0};
589
590 if (clockID > MAX_CLOCKS) {
591 goto ERROUT;
592 }
593
594 if (tp == NULL) {
595 goto ERROUT;
596 }
597
598 OsGetHwTime(&hwTime);
599
600 switch (clockID) {
601 case CLOCK_MONOTONIC_RAW:
602 tp->tv_sec = hwTime.tv_sec;
603 tp->tv_nsec = hwTime.tv_nsec;
604 break;
605 case CLOCK_MONOTONIC:
606 LOS_SpinLockSave(&g_timeSpin, &intSave);
607 tmp = OsTimeSpecAdd(hwTime, g_accDeltaFromAdj);
608 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
609 tp->tv_sec = tmp.tv_sec;
610 tp->tv_nsec = tmp.tv_nsec;
611 break;
612 case CLOCK_REALTIME:
613 LOS_SpinLockSave(&g_timeSpin, &intSave);
614 tmp = OsTimeSpecAdd(hwTime, g_accDeltaFromAdj);
615 tmp = OsTimeSpecAdd(tmp, g_accDeltaFromSet);
616 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
617 tp->tv_sec = tmp.tv_sec;
618 tp->tv_nsec = tmp.tv_nsec;
619 break;
620 case CLOCK_MONOTONIC_COARSE:
621 case CLOCK_REALTIME_COARSE:
622 case CLOCK_THREAD_CPUTIME_ID:
623 case CLOCK_PROCESS_CPUTIME_ID:
624 case CLOCK_BOOTTIME:
625 case CLOCK_REALTIME_ALARM:
626 case CLOCK_BOOTTIME_ALARM:
627 case CLOCK_TAI:
628 TIME_RETURN(ENOTSUP);
629 default:
630 {
631 #ifdef LOSCFG_KERNEL_CPUP
632 int ret = GetCputime(clockID, tp);
633 TIME_RETURN(-ret);
634 #else
635 TIME_RETURN(EINVAL);
636 #endif
637 }
638 }
639
640 return 0;
641
642 ERROUT:
643 TIME_RETURN(EINVAL);
644 }
645
clock_getres(clockid_t clockID,struct timespec * tp)646 int clock_getres(clockid_t clockID, struct timespec *tp)
647 {
648 if (tp == NULL) {
649 TIME_RETURN(EINVAL);
650 }
651
652 switch (clockID) {
653 case CLOCK_MONOTONIC_RAW:
654 case CLOCK_MONOTONIC:
655 case CLOCK_REALTIME:
656 /* the accessible rtc resolution */
657 tp->tv_nsec = OS_SYS_NS_PER_US; /* the precision of clock_gettime is 1us */
658 tp->tv_sec = 0;
659 break;
660 case CLOCK_MONOTONIC_COARSE:
661 case CLOCK_REALTIME_COARSE:
662 /* the clock coarse resolution, supported by vdso.
663 * the precision of clock_gettime is 1tick */
664 tp->tv_nsec = OS_SYS_NS_PER_SECOND / LOSCFG_BASE_CORE_TICK_PER_SECOND;
665 tp->tv_sec = 0;
666 break;
667 case CLOCK_THREAD_CPUTIME_ID:
668 case CLOCK_PROCESS_CPUTIME_ID:
669 case CLOCK_BOOTTIME:
670 case CLOCK_REALTIME_ALARM:
671 case CLOCK_BOOTTIME_ALARM:
672 case CLOCK_TAI:
673 TIME_RETURN(ENOTSUP);
674 default:
675 #ifdef LOSCFG_KERNEL_CPUP
676 {
677 int ret = CpuClockGetres(clockID, tp);
678 TIME_RETURN(-ret);
679 }
680 #else
681 TIME_RETURN(EINVAL);
682 #endif
683 }
684
685 TIME_RETURN(0);
686 }
687
clock_nanosleep(clockid_t clk,int flags,const struct timespec * req,struct timespec * rem)688 int clock_nanosleep(clockid_t clk, int flags, const struct timespec *req, struct timespec *rem)
689 {
690 switch (clk) {
691 case CLOCK_REALTIME:
692 if (flags == 0) {
693 /* we only support the realtime clock currently */
694 return nanosleep(req, rem);
695 }
696 /* fallthrough */
697 case CLOCK_MONOTONIC_COARSE:
698 case CLOCK_REALTIME_COARSE:
699 case CLOCK_MONOTONIC_RAW:
700 case CLOCK_MONOTONIC:
701 case CLOCK_PROCESS_CPUTIME_ID:
702 case CLOCK_BOOTTIME:
703 case CLOCK_REALTIME_ALARM:
704 case CLOCK_BOOTTIME_ALARM:
705 case CLOCK_TAI:
706 if (flags == 0 || flags == TIMER_ABSTIME) {
707 TIME_RETURN(ENOTSUP);
708 }
709 /* fallthrough */
710 case CLOCK_THREAD_CPUTIME_ID:
711 default:
712 TIME_RETURN(EINVAL);
713 }
714
715 TIME_RETURN(0);
716 }
717
718 typedef struct {
719 int sigev_signo;
720 pid_t pid;
721 unsigned int tid;
722 union sigval sigev_value;
723 } swtmr_proc_arg;
724
SwtmrProc(UINTPTR tmrArg)725 static VOID SwtmrProc(UINTPTR tmrArg)
726 {
727 #ifdef LOSCFG_KERNEL_VM
728 INT32 sig, ret;
729 UINT32 intSave;
730 pid_t pid;
731 siginfo_t info;
732 LosTaskCB *stcb = NULL;
733
734 swtmr_proc_arg *arg = (swtmr_proc_arg *)tmrArg;
735 OS_GOTO_EXIT_IF(arg == NULL, EINVAL);
736
737 sig = arg->sigev_signo;
738 pid = arg->pid;
739 OS_GOTO_EXIT_IF(!GOOD_SIGNO(sig), EINVAL);
740
741 /* Create the siginfo structure */
742 info.si_signo = sig;
743 info.si_code = SI_TIMER;
744 info.si_value.sival_ptr = arg->sigev_value.sival_ptr;
745
746 /* Send signals to threads or processes */
747 if (arg->tid > 0) {
748 /* Make sure that the para is valid */
749 OS_GOTO_EXIT_IF(OS_TID_CHECK_INVALID(arg->tid), EINVAL);
750 stcb = OsGetTaskCB(arg->tid);
751 ret = OsUserProcessOperatePermissionsCheck(stcb, stcb->processID);
752 OS_GOTO_EXIT_IF(ret != LOS_OK, -ret);
753
754 /* Dispatch the signal to thread, bypassing normal task group thread
755 * dispatch rules. */
756 SCHEDULER_LOCK(intSave);
757 ret = OsTcbDispatch(stcb, &info);
758 SCHEDULER_UNLOCK(intSave);
759 OS_GOTO_EXIT_IF(ret != LOS_OK, -ret);
760 } else {
761 /* Make sure that the para is valid */
762 OS_GOTO_EXIT_IF(pid <= 0 || OS_PID_CHECK_INVALID(pid), EINVAL);
763 /* Dispatch the signal to process */
764 SCHEDULER_LOCK(intSave);
765 OsDispatch(pid, &info, OS_USER_KILL_PERMISSION);
766 SCHEDULER_UNLOCK(intSave);
767 }
768 return;
769 EXIT:
770 PRINT_ERR("Dispatch signals failed!, ret: %d\r\n", ret);
771 #endif
772 return;
773 }
774
timer_create(clockid_t clockID,struct sigevent * restrict evp,timer_t * restrict timerID)775 int timer_create(clockid_t clockID, struct sigevent *restrict evp, timer_t *restrict timerID)
776 {
777 UINT32 ret;
778 UINT16 swtmrID;
779 #ifdef LOSCFG_SECURITY_VID
780 UINT16 vid;
781 #endif
782
783 if (!timerID || (clockID != CLOCK_REALTIME) || !evp) {
784 errno = EINVAL;
785 return -1;
786 }
787
788 if ((evp->sigev_notify != SIGEV_THREAD) || evp->sigev_notify_attributes) {
789 errno = ENOTSUP;
790 return -1;
791 }
792
793 ret = LOS_SwtmrCreate(1, LOS_SWTMR_MODE_ONCE, (SWTMR_PROC_FUNC)evp->sigev_notify_function,
794 &swtmrID, (UINTPTR)evp->sigev_value.sival_ptr);
795 if (ret != LOS_OK) {
796 errno = (ret == LOS_ERRNO_SWTMR_MAXSIZE) ? EAGAIN : EINVAL;
797 return -1;
798 }
799
800 #ifdef LOSCFG_SECURITY_VID
801 vid = AddNodeByRid(swtmrID);
802 if (vid == MAX_INVALID_TIMER_VID) {
803 (VOID)LOS_SwtmrDelete(swtmrID);
804 return -1;
805 }
806 swtmrID = vid;
807 #endif
808 *timerID = (timer_t)(UINTPTR)swtmrID;
809 return 0;
810 }
811
OsTimerCreate(clockid_t clockID,struct ksigevent * evp,timer_t * timerID)812 int OsTimerCreate(clockid_t clockID, struct ksigevent *evp, timer_t *timerID)
813 {
814 UINT32 ret;
815 UINT16 swtmrID;
816 swtmr_proc_arg *arg = NULL;
817 int signo;
818 #ifdef LOSCFG_SECURITY_VID
819 UINT16 vid;
820 #endif
821
822 if ((clockID != CLOCK_REALTIME) || (timerID == NULL)) {
823 errno = EINVAL;
824 return -1;
825 }
826
827 signo = evp ? evp->sigev_signo : SIGALRM;
828 if (signo > SIGRTMAX || signo < 1) {
829 errno = EINVAL;
830 return -1;
831 }
832 if (evp && (evp->sigev_notify != SIGEV_SIGNAL && evp->sigev_notify != SIGEV_THREAD_ID)) {
833 errno = ENOTSUP;
834 return -1;
835 }
836
837 arg = (swtmr_proc_arg *)malloc(sizeof(swtmr_proc_arg));
838 if (arg == NULL) {
839 errno = ENOMEM;
840 return -1;
841 }
842
843 arg->tid = evp ? evp->sigev_tid : 0;
844 arg->sigev_signo = signo;
845 arg->pid = LOS_GetCurrProcessID();
846 arg->sigev_value.sival_ptr = evp ? evp->sigev_value.sival_ptr : NULL;
847 ret = LOS_SwtmrCreate(1, LOS_SWTMR_MODE_ONCE, SwtmrProc, &swtmrID, (UINTPTR)arg);
848 if (ret != LOS_OK) {
849 errno = (ret == LOS_ERRNO_SWTMR_MAXSIZE) ? EAGAIN : EINVAL;
850 free(arg);
851 return -1;
852 }
853
854 #ifdef LOSCFG_SECURITY_VID
855 vid = AddNodeByRid(swtmrID);
856 if (vid == MAX_INVALID_TIMER_VID) {
857 free(arg);
858 (VOID)LOS_SwtmrDelete(swtmrID);
859 return -1;
860 }
861 swtmrID = vid;
862 #endif
863 *timerID = (timer_t)(UINTPTR)swtmrID;
864 return 0;
865 }
866
timer_delete(timer_t timerID)867 int timer_delete(timer_t timerID)
868 {
869 UINT16 swtmrID = (UINT16)(UINTPTR)timerID;
870 VOID *arg = NULL;
871 UINTPTR swtmrProc;
872
873 #ifdef LOSCFG_SECURITY_VID
874 swtmrID = GetRidByVid(swtmrID);
875 #endif
876 if (OS_INT_ACTIVE || !ValidTimerID(swtmrID)) {
877 goto ERROUT;
878 }
879
880 arg = (VOID *)OS_SWT_FROM_SID(swtmrID)->uwArg;
881 swtmrProc = (UINTPTR)OS_SWT_FROM_SID(swtmrID)->pfnHandler;
882 if (LOS_SwtmrDelete(swtmrID)) {
883 goto ERROUT;
884 }
885 if ((swtmrProc == (UINTPTR)SwtmrProc) && (arg != NULL)) {
886 free(arg);
887 }
888
889 #ifdef LOSCFG_SECURITY_VID
890 RemoveNodeByVid((UINT16)(UINTPTR)timerID);
891 #endif
892 return 0;
893
894 ERROUT:
895 errno = EINVAL;
896 return -1;
897 }
898
timer_settime(timer_t timerID,int flags,const struct itimerspec * value,struct itimerspec * oldValue)899 int timer_settime(timer_t timerID, int flags,
900 const struct itimerspec *value, /* new value */
901 struct itimerspec *oldValue) /* old value to return, always 0 */
902 {
903 UINT16 swtmrID = (UINT16)(UINTPTR)timerID;
904 SWTMR_CTRL_S *swtmr = NULL;
905 UINT32 interval, expiry, ret;
906 UINT32 intSave;
907
908 if (flags != 0) {
909 /* flags not supported currently */
910 errno = ENOSYS;
911 return -1;
912 }
913
914 #ifdef LOSCFG_SECURITY_VID
915 swtmrID = GetRidByVid(swtmrID);
916 #endif
917 if ((value == NULL) || OS_INT_ACTIVE || !ValidTimerID(swtmrID)) {
918 errno = EINVAL;
919 return -1;
920 }
921
922 if (!ValidTimeSpec(&value->it_value) || !ValidTimeSpec(&value->it_interval)) {
923 errno = EINVAL;
924 return -1;
925 }
926
927 if (oldValue) {
928 (VOID)timer_gettime(timerID, oldValue);
929 }
930
931 swtmr = OS_SWT_FROM_SID(swtmrID);
932 ret = LOS_SwtmrStop(swtmr->usTimerID);
933 if ((ret != LOS_OK) && (ret != LOS_ERRNO_SWTMR_NOT_STARTED)) {
934 errno = EINVAL;
935 return -1;
936 }
937
938 expiry = OsTimeSpec2Tick(&value->it_value);
939 interval = OsTimeSpec2Tick(&value->it_interval);
940
941 LOS_SpinLockSave(&g_swtmrSpin, &intSave);
942 swtmr->ucMode = interval ? LOS_SWTMR_MODE_OPP : LOS_SWTMR_MODE_NO_SELFDELETE;
943 swtmr->uwExpiry = expiry + !!expiry; // PS: skip the first tick because it is NOT a full tick.
944 swtmr->uwInterval = interval;
945 swtmr->uwOverrun = 0;
946 LOS_SpinUnlockRestore(&g_swtmrSpin, intSave);
947
948 if ((value->it_value.tv_sec == 0) && (value->it_value.tv_nsec == 0)) {
949 /*
950 * 1) when expiry is 0, means timer should be stopped.
951 * 2) If timer is ticking, stopping timer is already done before.
952 * 3) If timer is created but not ticking, return 0 as well.
953 */
954 return 0;
955 }
956
957 if (LOS_SwtmrStart(swtmr->usTimerID)) {
958 errno = EINVAL;
959 return -1;
960 }
961
962 return 0;
963 }
964
timer_gettime(timer_t timerID,struct itimerspec * value)965 int timer_gettime(timer_t timerID, struct itimerspec *value)
966 {
967 UINT32 tick = 0;
968 SWTMR_CTRL_S *swtmr = NULL;
969 UINT16 swtmrID = (UINT16)(UINTPTR)timerID;
970 UINT32 ret;
971
972 #ifdef LOSCFG_SECURITY_VID
973 swtmrID = GetRidByVid(swtmrID);
974 #endif
975 if ((value == NULL) || !ValidTimerID(swtmrID)) {
976 errno = EINVAL;
977 return -1;
978 }
979
980 swtmr = OS_SWT_FROM_SID(swtmrID);
981
982 /* get expire time */
983 ret = LOS_SwtmrTimeGet(swtmr->usTimerID, &tick);
984 if ((ret != LOS_OK) && (ret != LOS_ERRNO_SWTMR_NOT_STARTED)) {
985 errno = EINVAL;
986 return -1;
987 }
988
989 OsTick2TimeSpec(&value->it_value, tick);
990 OsTick2TimeSpec(&value->it_interval, (swtmr->ucMode == LOS_SWTMR_MODE_ONCE) ? 0 : swtmr->uwInterval);
991 return 0;
992 }
993
timer_getoverrun(timer_t timerID)994 int timer_getoverrun(timer_t timerID)
995 {
996 UINT16 swtmrID = (UINT16)(UINTPTR)timerID;
997 SWTMR_CTRL_S *swtmr = NULL;
998 INT32 overRun;
999
1000 #ifdef LOSCFG_SECURITY_VID
1001 swtmrID = GetRidByVid(swtmrID);
1002 #endif
1003 if (!ValidTimerID(swtmrID)) {
1004 errno = EINVAL;
1005 return -1;
1006 }
1007
1008 swtmr = OS_SWT_FROM_SID(swtmrID);
1009 if (swtmr->usTimerID >= OS_SWTMR_MAX_TIMERID) {
1010 errno = EINVAL;
1011 return -1;
1012 }
1013
1014 overRun = (INT32)(swtmr->uwOverrun);
1015 return (overRun > DELAYTIMER_MAX) ? DELAYTIMER_MAX : overRun;
1016 }
1017
DoNanoSleep(UINT64 nanoseconds)1018 STATIC INT32 DoNanoSleep(UINT64 nanoseconds)
1019 {
1020 UINT32 ret;
1021
1022 ret = LOS_TaskDelay(OsNS2Tick(nanoseconds));
1023 if (ret == LOS_OK || ret == LOS_ERRNO_TSK_YIELD_NOT_ENOUGH_TASK) {
1024 return 0;
1025 }
1026 return -1;
1027 }
1028
1029 #ifdef LOSCFG_LIBC_NEWLIB
usleep(unsigned long useconds)1030 int usleep(unsigned long useconds)
1031 #else
1032 int usleep(unsigned useconds)
1033 #endif
1034 {
1035 return DoNanoSleep((UINT64)useconds * OS_SYS_NS_PER_US);
1036 }
1037
nanosleep(const struct timespec * rqtp,struct timespec * rmtp)1038 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
1039 {
1040 UINT64 nanoseconds;
1041 INT32 ret = -1;
1042
1043 (VOID)rmtp;
1044 /* expire time */
1045
1046 if (!ValidTimeSpec(rqtp)) {
1047 errno = EINVAL;
1048 return ret;
1049 }
1050
1051 nanoseconds = (UINT64)rqtp->tv_sec * OS_SYS_NS_PER_SECOND + rqtp->tv_nsec;
1052
1053 return DoNanoSleep(nanoseconds);
1054 }
1055
sleep(unsigned int seconds)1056 unsigned int sleep(unsigned int seconds)
1057 {
1058 return DoNanoSleep((UINT64)seconds * OS_SYS_NS_PER_SECOND);
1059 }
1060
difftime(time_t time2,time_t time1)1061 double difftime(time_t time2, time_t time1)
1062 {
1063 return (double)(time2 - time1);
1064 }
1065
clock(VOID)1066 clock_t clock(VOID)
1067 {
1068 clock_t clockMsec;
1069 UINT64 nowNsec;
1070
1071 nowNsec = LOS_CurrNanosec();
1072 clockMsec = (clock_t)(nowNsec / (OS_SYS_NS_PER_SECOND / CLOCKS_PER_SEC));
1073
1074 return clockMsec;
1075 }
1076
times(struct tms * buf)1077 clock_t times(struct tms *buf)
1078 {
1079 clock_t clockTick = -1;
1080
1081 (void)buf;
1082 set_errno(ENOSYS);
1083
1084 return clockTick;
1085 }
1086
setitimer(int which,const struct itimerval * value,struct itimerval * ovalue)1087 int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue)
1088 {
1089 UINT32 intSave;
1090 LosTaskCB *taskCB = OS_TCB_FROM_TID(LOS_CurTaskIDGet());
1091 LosProcessCB *processCB = OS_PCB_FROM_PID(taskCB->processID);
1092 timer_t timerID = 0;
1093 struct itimerspec spec;
1094 struct itimerspec ospec;
1095 int ret = LOS_OK;
1096
1097 /* we only support the realtime clock timer currently */
1098 if (which != ITIMER_REAL || !value) {
1099 set_errno(EINVAL);
1100 return -1;
1101 }
1102
1103 /* To avoid creating an invalid timer after the timer has already been create */
1104 if (processCB->timerID == (timer_t)(UINTPTR)MAX_INVALID_TIMER_VID) {
1105 ret = OsTimerCreate(CLOCK_REALTIME, NULL, &timerID);
1106 if (ret != LOS_OK) {
1107 return ret;
1108 }
1109 }
1110
1111 /* The initialization of this global timer must be in spinlock
1112 * OsTimerCreate cannot be located in spinlock.
1113 */
1114 SCHEDULER_LOCK(intSave);
1115 if (processCB->timerID == (timer_t)(UINTPTR)MAX_INVALID_TIMER_VID) {
1116 processCB->timerID = timerID;
1117 SCHEDULER_UNLOCK(intSave);
1118 } else {
1119 SCHEDULER_UNLOCK(intSave);
1120 if (timerID) {
1121 timer_delete(timerID);
1122 }
1123 }
1124
1125 if (!ValidTimeval(&value->it_value) || !ValidTimeval(&value->it_interval)) {
1126 set_errno(EINVAL);
1127 return -1;
1128 }
1129
1130 TIMEVAL_TO_TIMESPEC(&value->it_value, &spec.it_value);
1131 TIMEVAL_TO_TIMESPEC(&value->it_interval, &spec.it_interval);
1132
1133 ret = timer_settime(processCB->timerID, 0, &spec, ovalue ? &ospec : NULL);
1134 if (ret == LOS_OK && ovalue) {
1135 TIMESPEC_TO_TIMEVAL(&ovalue->it_value, &ospec.it_value);
1136 TIMESPEC_TO_TIMEVAL(&ovalue->it_interval, &ospec.it_interval);
1137 }
1138
1139 return ret;
1140 }
1141
getitimer(int which,struct itimerval * value)1142 int getitimer(int which, struct itimerval *value)
1143 {
1144 LosTaskCB *taskCB = OS_TCB_FROM_TID(LOS_CurTaskIDGet());
1145 LosProcessCB *processCB = OS_PCB_FROM_PID(taskCB->processID);
1146 struct itimerspec spec = {};
1147
1148 int ret = LOS_OK;
1149
1150 /* we only support the realtime clock timer currently */
1151 if (which != ITIMER_REAL || !value) {
1152 set_errno(EINVAL);
1153 return -1;
1154 }
1155
1156 if (processCB->timerID != (timer_t)(UINTPTR)MAX_INVALID_TIMER_VID) {
1157 ret = timer_gettime(processCB->timerID, &spec);
1158 }
1159
1160 if (ret == LOS_OK) {
1161 TIMESPEC_TO_TIMEVAL(&value->it_value, &spec.it_value);
1162 TIMESPEC_TO_TIMEVAL(&value->it_interval, &spec.it_interval);
1163 }
1164
1165 return ret;
1166 }
1167
1168 #ifdef LOSCFG_KERNEL_VDSO
OsVdsoTimeGet(VdsoDataPage * vdsoDataPage)1169 VOID OsVdsoTimeGet(VdsoDataPage *vdsoDataPage)
1170 {
1171 UINT32 intSave;
1172 struct timespec64 tmp = {0};
1173 struct timespec64 hwTime = {0};
1174
1175 if (vdsoDataPage == NULL) {
1176 return;
1177 }
1178
1179 OsGetHwTime(&hwTime);
1180
1181 LOS_SpinLockSave(&g_timeSpin, &intSave);
1182 tmp = OsTimeSpecAdd(hwTime, g_accDeltaFromAdj);
1183 vdsoDataPage->monoTimeSec = tmp.tv_sec;
1184 vdsoDataPage->monoTimeNsec = tmp.tv_nsec;
1185
1186 tmp = OsTimeSpecAdd(tmp, g_accDeltaFromSet);
1187 vdsoDataPage->realTimeSec = tmp.tv_sec;
1188 vdsoDataPage->realTimeNsec = tmp.tv_nsec;
1189 LOS_SpinUnlockRestore(&g_timeSpin, intSave);
1190 }
1191 #endif
1192
time(time_t * t)1193 time_t time(time_t *t)
1194 {
1195 struct timeval tp;
1196 int ret;
1197
1198 /* Get the current time from the system */
1199 ret = gettimeofday(&tp, (struct timezone *)NULL);
1200 if (ret == LOS_OK) {
1201 /* Return the seconds since the epoch */
1202 if (t) {
1203 *t = tp.tv_sec;
1204 }
1205 return tp.tv_sec;
1206 }
1207 return (time_t)OS_ERROR;
1208 }
1209