comparison dbutil.c @ 954:e257f94ef8a7 coverity

merge
author Matt Johnston <matt@ucc.asn.au>
date Mon, 28 Jul 2014 22:56:07 +0800
parents 356a25a108a3
children db9fa5971d24
comparison
equal deleted inserted replaced
945:9969cee83d0a 954:e257f94ef8a7
159 } 159 }
160 160
161 gettimeofday(&tv, NULL); 161 gettimeofday(&tv, NULL);
162 162
163 va_start(param, format); 163 va_start(param, format);
164 fprintf(stderr, "TRACE (%d) %d.%d: ", getpid(), tv.tv_sec, tv.tv_usec); 164 fprintf(stderr, "TRACE (%d) %d.%d: ", getpid(), (int)tv.tv_sec, (int)tv.tv_usec);
165 vfprintf(stderr, format, param); 165 vfprintf(stderr, format, param);
166 fprintf(stderr, "\n"); 166 fprintf(stderr, "\n");
167 va_end(param); 167 va_end(param);
168 } 168 }
169 169
181 } 181 }
182 182
183 gettimeofday(&tv, NULL); 183 gettimeofday(&tv, NULL);
184 184
185 va_start(param, format); 185 va_start(param, format);
186 fprintf(stderr, "TRACE2 (%d) %d.%d: ", getpid(), tv.tv_sec, tv.tv_usec); 186 fprintf(stderr, "TRACE2 (%d) %d.%d: ", getpid(), (int)tv.tv_sec, (int)tv.tv_usec);
187 vfprintf(stderr, format, param); 187 vfprintf(stderr, format, param);
188 fprintf(stderr, "\n"); 188 fprintf(stderr, "\n");
189 va_end(param); 189 va_end(param);
190 } 190 }
191 #endif /* DEBUG_TRACE */ 191 #endif /* DEBUG_TRACE */
943 c |= (xa[i] ^ xb[i]); 943 c |= (xa[i] ^ xb[i]);
944 } 944 }
945 return c; 945 return c;
946 } 946 }
947 947
948 time_t monotonic_now() {
949
950 #if defined(__linux__) && defined(SYS_clock_gettime) 948 #if defined(__linux__) && defined(SYS_clock_gettime)
951 /* CLOCK_MONOTONIC_COARSE was added in Linux 2.6.32. Probably cheaper. */ 949 /* CLOCK_MONOTONIC_COARSE was added in Linux 2.6.32 but took a while to
950 reach userspace include headers */
952 #ifndef CLOCK_MONOTONIC_COARSE 951 #ifndef CLOCK_MONOTONIC_COARSE
953 #define CLOCK_MONOTONIC_COARSE 6 952 #define CLOCK_MONOTONIC_COARSE 6
954 #endif 953 #endif
955 static clockid_t clock_source = CLOCK_MONOTONIC_COARSE; 954 static clockid_t get_linux_clock_source() {
956 struct timespec ts; 955 struct timespec ts;
957 956 if (syscall(SYS_clock_gettime, CLOCK_MONOTONIC_COARSE, &ts) == 0) {
958 if (syscall(SYS_clock_gettime, clock_source, &ts) == EINVAL) { 957 return CLOCK_MONOTONIC_COARSE;
959 clock_source = CLOCK_MONOTONIC; 958 }
960 syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &ts); 959
961 } 960 if (syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &ts) == 0) {
962 return ts.tv_sec; 961 return CLOCK_MONOTONIC;
963 #elif defined(HAVE_MACH_ABSOLUTE_TIME) 962 }
963 return -1;
964 }
965 #endif
966
967 time_t monotonic_now() {
968 #if defined(__linux__) && defined(SYS_clock_gettime)
969 static clockid_t clock_source = -2;
970
971 if (clock_source == -2) {
972 /* First run, find out which one works.
973 -1 will fall back to time() */
974 clock_source = get_linux_clock_source();
975 }
976
977 if (clock_source >= 0) {
978 struct timespec ts;
979 if (syscall(SYS_clock_gettime, clock_source, &ts) != 0) {
980 /* Intermittent clock failures should not happen */
981 dropbear_exit("Clock broke");
982 }
983 return ts.tv_sec;
984 }
985 #endif /* linux clock_gettime */
986
987 #if defined(HAVE_MACH_ABSOLUTE_TIME)
964 /* OS X, see https://developer.apple.com/library/mac/qa/qa1398/_index.html */ 988 /* OS X, see https://developer.apple.com/library/mac/qa/qa1398/_index.html */
965 static mach_timebase_info_data_t timebase_info; 989 static mach_timebase_info_data_t timebase_info;
966 if (timebase_info.denom == 0) { 990 if (timebase_info.denom == 0) {
967 mach_timebase_info(&timebase_info); 991 mach_timebase_info(&timebase_info);
968 } 992 }
969 return mach_absolute_time() * timebase_info.numer / timebase_info.denom 993 return mach_absolute_time() * timebase_info.numer / timebase_info.denom
970 / 1e9; 994 / 1e9;
971 #else 995 #endif /* osx mach_absolute_time */
996
972 /* Fallback for everything else - this will sometimes go backwards */ 997 /* Fallback for everything else - this will sometimes go backwards */
973 return time(NULL); 998 return time(NULL);
974 #endif 999 }
975 1000
976 }
977