4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
21 * Copyright (c) 2010, Oracle and/or it's affiliates. All rights reserved.
25 * This compiles to a module that can be preloaded during a build. If this
26 * is preloaded, it interposes on time(2), gettimeofday(3C), and
27 * clock_gethrtime(3C) and returns a constant number of seconds since epoch
28 * when the execname matches one of the desired "programs" and TIME_CONSTANT
29 * contains an integer value to be returned.
38 /* The list of programs that we want to use a constant time. */
39 static char *programs
[] = {
40 "autogen", "bash", "cpp", "cc1", "date", "doxygen",
41 "erl", "grops", "gs", "gtroff", "javadoc", "ksh", "ksh93", "ld",
42 "perl", "perl5.8.4", "perl5.10", "ruby", "sh", NULL
46 stack_info(uintptr_t pc
, int signo
, void *arg
)
51 if (dladdr1((void *)pc
, &info
, &sym
, RTLD_DL_SYMENT
) != NULL
) {
52 if (strstr(info
.dli_fname
, ".so") == NULL
)
53 *(char **)arg
= (char *)info
.dli_fname
;
62 static char *execname
;
64 if (execname
== NULL
) {
67 if (getcontext(&ctx
) == 0)
68 walkcontext(&ctx
, stack_info
, &execname
);
70 if (execname
!= NULL
) {
71 char *s
= strrchr(execname
, '/');
84 char *execname
= my_execname();
87 if (execname
!= NULL
) {
90 for (i
= 0; programs
[i
] != NULL
; i
++)
91 if (strcmp(execname
, programs
[i
]) == 0) {
92 static char *time_string
;
94 if (time_string
== NULL
)
95 time_string
= getenv("TIME_CONSTANT");
97 if (time_string
!= NULL
)
98 result
= atoll(time_string
);
110 time_t result
= time_constant();
112 if (result
== (time_t)-1) {
113 static time_t (*fptr
)(time_t *);
116 fptr
= (time_t (*)(time_t *))dlsym(RTLD_NEXT
, "time");
118 result
= (fptr
)(ptr
);
119 } else if (ptr
!= NULL
)
126 gettimeofday(struct timeval
*tp
, void *tzp
)
128 static int (*fptr
)(struct timeval
*, void *);
132 fptr
= (int (*)(struct timeval
*, void *))dlsym(RTLD_NEXT
,
135 if ((result
= (fptr
)(tp
, tzp
)) == 0) {
136 time_t curtime
= time_constant();
138 if (curtime
!= (time_t)-1)
139 tp
->tv_sec
= curtime
;
146 clock_gettime(clockid_t clock_id
, struct timespec
*tp
)
148 static int (*fptr
)(clockid_t
, struct timespec
*);
152 fptr
= (int (*)(clockid_t
, struct timespec
*))dlsym(RTLD_NEXT
,
155 if ((result
= (fptr
)(clock_id
, tp
)) == 0) {
156 time_t curtime
= time_constant();
158 if (curtime
!= (time_t)-1)
159 tp
->tv_sec
= curtime
;