Print this page
4809 NANOSEC should be 'long long' to avoid integer overflow bugs
4810 spa_async_tasks_pending suffers from an integer overflow bug
4811 in.mpathd: tv2ns suffers from an integer overflow bug
Reviewed by: Marcel Telka <marcel.telka@nexenta.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/sys/time.h
+++ new/usr/src/uts/common/sys/time.h
1 1 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
2 2 /* All Rights Reserved */
3 3
4 4
5 5 /*
6 6 * Copyright (c) 1982, 1986, 1993 Regents of the University of California.
7 7 * All rights reserved. The Berkeley software License Agreement
8 8 * specifies the terms and conditions for redistribution.
9 9 */
10 10
11 11 /*
12 12 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
13 13 * Use is subject to license terms.
14 14 */
15 15
16 16 #ifndef _SYS_TIME_H
17 17 #define _SYS_TIME_H
18 18
19 19 #include <sys/feature_tests.h>
20 20
21 21 /*
22 22 * Structure returned by gettimeofday(2) system call,
23 23 * and used in other calls.
24 24 */
25 25
26 26 #ifdef __cplusplus
27 27 extern "C" {
28 28 #endif
29 29
30 30 #if !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
31 31 defined(__EXTENSIONS__)
32 32 #ifndef _ASM
33 33
34 34 #if !defined(_TIME_T) || __cplusplus >= 199711L
35 35 #define _TIME_T
36 36 typedef long time_t; /* time of day in seconds */
37 37 #endif /* _TIME_T */
38 38
39 39 #ifndef _SUSECONDS_T
40 40 #define _SUSECONDS_T
41 41 typedef long suseconds_t; /* signed # of microseconds */
42 42 #endif /* _SUSECONDS_T */
43 43
44 44 struct timeval {
45 45 time_t tv_sec; /* seconds */
46 46 suseconds_t tv_usec; /* and microseconds */
47 47 };
48 48
49 49 #if defined(_SYSCALL32)
50 50
51 51 #include <sys/types32.h>
52 52
53 53 #define TIMEVAL32_TO_TIMEVAL(tv, tv32) { \
54 54 (tv)->tv_sec = (time_t)(tv32)->tv_sec; \
55 55 (tv)->tv_usec = (tv32)->tv_usec; \
56 56 }
57 57
58 58 #define TIMEVAL_TO_TIMEVAL32(tv32, tv) { \
59 59 (tv32)->tv_sec = (time32_t)(tv)->tv_sec; \
60 60 (tv32)->tv_usec = (tv)->tv_usec; \
61 61 }
62 62
63 63 #define TIME32_MAX INT32_MAX
64 64 #define TIME32_MIN INT32_MIN
65 65
66 66 #define TIMEVAL_OVERFLOW(tv) \
67 67 ((tv)->tv_sec < TIME32_MIN || (tv)->tv_sec > TIME32_MAX)
68 68
69 69 #endif /* _SYSCALL32 || _KERNEL */
70 70
71 71 #endif /* _ASM */
72 72 #endif /* !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) ... */
73 73
74 74 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
75 75 #ifndef _ASM
76 76 struct timezone {
77 77 int tz_minuteswest; /* minutes west of Greenwich */
78 78 int tz_dsttime; /* type of dst correction */
79 79 };
80 80
81 81 #endif /* _ASM */
82 82 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
83 83
84 84 #ifdef __cplusplus
85 85 }
86 86 #endif
87 87
88 88 /*
89 89 * Needed for longlong_t type. Placement of this due to <sys/types.h>
90 90 * including <sys/select.h> which relies on the presense of the itimerval
91 91 * structure.
92 92 */
93 93 #ifndef _ASM
94 94 #include <sys/types.h>
95 95 #endif /* _ASM */
96 96
97 97 #ifdef __cplusplus
98 98 extern "C" {
99 99 #endif
100 100
101 101 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
102 102
103 103 #define DST_NONE 0 /* not on dst */
104 104 #define DST_USA 1 /* USA style dst */
105 105 #define DST_AUST 2 /* Australian style dst */
106 106 #define DST_WET 3 /* Western European dst */
107 107 #define DST_MET 4 /* Middle European dst */
108 108 #define DST_EET 5 /* Eastern European dst */
109 109 #define DST_CAN 6 /* Canada */
110 110 #define DST_GB 7 /* Great Britain and Eire */
111 111 #define DST_RUM 8 /* Rumania */
112 112 #define DST_TUR 9 /* Turkey */
113 113 #define DST_AUSTALT 10 /* Australian style with shift in 1986 */
114 114
115 115 /*
116 116 * Operations on timevals.
117 117 */
118 118 #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
119 119 #define timercmp(tvp, uvp, cmp) \
120 120 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
121 121 /* CSTYLED */ \
122 122 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
123 123 /* CSTYLED */ \
124 124 ((tvp)->tv_sec cmp (uvp)->tv_sec))
125 125
126 126 #define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
127 127
128 128 #ifdef __lint
129 129 /*
130 130 * Make innocuous, lint-happy versions until do {} while (0) is acknowleged as
131 131 * lint-safe. If the compiler could know that we always make tv_usec < 1000000
132 132 * we wouldn't need a special linted version.
133 133 */
134 134 #define timeradd(tvp, uvp, vvp) \
135 135 do \
136 136 { \
137 137 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
138 138 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
139 139 if ((vvp)->tv_usec >= 1000000) \
140 140 { \
141 141 (vvp)->tv_sec++; \
142 142 (vvp)->tv_usec -= 1000000; \
143 143 } \
144 144 } while ((vvp)->tv_usec >= 1000000)
145 145 #define timersub(tvp, uvp, vvp) \
146 146 do \
147 147 { \
148 148 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
149 149 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
150 150 if ((vvp)->tv_usec < 0) \
151 151 { \
152 152 (vvp)->tv_sec--; \
153 153 (vvp)->tv_usec += 1000000; \
154 154 } \
155 155 } while ((vvp)->tv_usec >= 1000000)
156 156 #else
157 157 #define timeradd(tvp, uvp, vvp) \
158 158 do \
159 159 { \
160 160 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
161 161 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
162 162 if ((vvp)->tv_usec >= 1000000) \
163 163 { \
164 164 (vvp)->tv_sec++; \
165 165 (vvp)->tv_usec -= 1000000; \
166 166 } \
167 167 } while (0)
168 168
169 169 #define timersub(tvp, uvp, vvp) \
170 170 do \
171 171 { \
172 172 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
173 173 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
174 174 if ((vvp)->tv_usec < 0) \
175 175 { \
176 176 (vvp)->tv_sec--; \
177 177 (vvp)->tv_usec += 1000000; \
178 178 } \
179 179 } while (0)
180 180 #endif /* __lint */
181 181
182 182 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
183 183
184 184 #if !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || defined(__EXTENSIONS__)
185 185 /*
186 186 * Names of the interval timers, and structure
187 187 * defining a timer setting.
188 188 */
189 189 #define ITIMER_REAL 0 /* Decrements in real time */
190 190 #define ITIMER_VIRTUAL 1 /* Decrements in process virtual time */
191 191 #define ITIMER_PROF 2 /* Decrements both in process virtual */
192 192 /* time and when system is running on */
193 193 /* behalf of the process. */
194 194 #define ITIMER_REALPROF 3 /* Decrements in real time for real- */
195 195 /* time profiling of multithreaded */
196 196 /* programs. */
197 197
198 198 #ifndef _ASM
199 199 struct itimerval {
200 200 struct timeval it_interval; /* timer interval */
201 201 struct timeval it_value; /* current value */
202 202 };
203 203
204 204 #if defined(_SYSCALL32)
205 205
206 206 struct itimerval32 {
207 207 struct timeval32 it_interval;
208 208 struct timeval32 it_value;
209 209 };
210 210
211 211 #define ITIMERVAL32_TO_ITIMERVAL(itv, itv32) { \
212 212 TIMEVAL32_TO_TIMEVAL(&(itv)->it_interval, &(itv32)->it_interval); \
213 213 TIMEVAL32_TO_TIMEVAL(&(itv)->it_value, &(itv32)->it_value); \
214 214 }
215 215
216 216 #define ITIMERVAL_TO_ITIMERVAL32(itv32, itv) { \
217 217 TIMEVAL_TO_TIMEVAL32(&(itv32)->it_interval, &(itv)->it_interval); \
218 218 TIMEVAL_TO_TIMEVAL32(&(itv32)->it_value, &(itv)->it_value); \
219 219 }
220 220
221 221 #define ITIMERVAL_OVERFLOW(itv) \
222 222 (TIMEVAL_OVERFLOW(&(itv)->it_interval) || \
223 223 TIMEVAL_OVERFLOW(&(itv)->it_value))
224 224
225 225 #endif /* _SYSCALL32 */
226 226 #endif /* _ASM */
↓ open down ↓ |
226 lines elided |
↑ open up ↑ |
227 227 #endif /* !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) ... */
228 228
229 229
230 230 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
231 231 /*
232 232 * Definitions for commonly used resolutions.
233 233 */
234 234 #define SEC 1
235 235 #define MILLISEC 1000
236 236 #define MICROSEC 1000000
237 -#define NANOSEC 1000000000
237 +#define NANOSEC 1000000000LL
238 238
239 239 #define MSEC2NSEC(m) ((hrtime_t)(m) * (NANOSEC / MILLISEC))
240 240 #define NSEC2MSEC(n) ((n) / (NANOSEC / MILLISEC))
241 241
242 242 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
243 243
244 244 #ifndef _ASM
245 245
246 246 /*
247 247 * Time expressed as a 64-bit nanosecond counter.
248 248 */
249 249 typedef longlong_t hrtime_t;
250 250
251 251 #ifdef _KERNEL
252 252
253 253 #include <sys/time_impl.h>
254 254 #include <sys/mutex.h>
255 255
256 256 extern int tick_per_msec; /* clock ticks per millisecond (may be zero) */
257 257 extern int msec_per_tick; /* milliseconds per clock tick (may be zero) */
258 258 extern int usec_per_tick; /* microseconds per clock tick */
259 259 extern int nsec_per_tick; /* nanoseconds per clock tick */
260 260
261 261 /*
262 262 * Macros to convert from common units of time (sec, msec, usec, nsec,
263 263 * timeval, timestruc) to clock ticks and vice versa.
264 264 */
265 265 #define TICK_TO_SEC(tick) ((tick) / hz)
266 266 #define SEC_TO_TICK(sec) ((sec) * hz)
267 267
268 268 #define TICK_TO_MSEC(tick) \
269 269 (msec_per_tick ? (tick) * msec_per_tick : (tick) / tick_per_msec)
270 270 #define MSEC_TO_TICK(msec) \
271 271 (msec_per_tick ? (msec) / msec_per_tick : (msec) * tick_per_msec)
272 272 #define MSEC_TO_TICK_ROUNDUP(msec) \
273 273 (msec_per_tick ? \
274 274 ((msec) == 0 ? 0 : ((msec) - 1) / msec_per_tick + 1) : \
275 275 (msec) * tick_per_msec)
276 276
277 277 #define TICK_TO_USEC(tick) ((tick) * usec_per_tick)
278 278 #define USEC_TO_TICK(usec) ((usec) / usec_per_tick)
279 279 #define USEC_TO_TICK_ROUNDUP(usec) \
280 280 ((usec) == 0 ? 0 : USEC_TO_TICK((usec) - 1) + 1)
281 281
282 282 #define TICK_TO_NSEC(tick) ((hrtime_t)(tick) * nsec_per_tick)
283 283 #define NSEC_TO_TICK(nsec) ((nsec) / nsec_per_tick)
284 284 #define NSEC_TO_TICK_ROUNDUP(nsec) \
285 285 ((nsec) == 0 ? 0 : NSEC_TO_TICK((nsec) - 1) + 1)
286 286
287 287 #define TICK_TO_TIMEVAL(tick, tvp) { \
288 288 clock_t __tmptck = (tick); \
289 289 (tvp)->tv_sec = TICK_TO_SEC(__tmptck); \
290 290 (tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK((tvp)->tv_sec)); \
291 291 }
292 292
293 293 #define TICK_TO_TIMEVAL32(tick, tvp) { \
294 294 clock_t __tmptck = (tick); \
295 295 time_t __tmptm = TICK_TO_SEC(__tmptck); \
296 296 (tvp)->tv_sec = (time32_t)__tmptm; \
297 297 (tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK(__tmptm)); \
298 298 }
299 299
300 300 #define TICK_TO_TIMESTRUC(tick, tsp) { \
301 301 clock_t __tmptck = (tick); \
302 302 (tsp)->tv_sec = TICK_TO_SEC(__tmptck); \
303 303 (tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK((tsp)->tv_sec)); \
304 304 }
305 305
306 306 #define TICK_TO_TIMESTRUC32(tick, tsp) { \
307 307 clock_t __tmptck = (tick); \
308 308 time_t __tmptm = TICK_TO_SEC(__tmptck); \
309 309 (tsp)->tv_sec = (time32_t)__tmptm; \
310 310 (tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK(__tmptm)); \
311 311 }
312 312
313 313 #define TIMEVAL_TO_TICK(tvp) \
314 314 (SEC_TO_TICK((tvp)->tv_sec) + USEC_TO_TICK((tvp)->tv_usec))
315 315
316 316 #define TIMESTRUC_TO_TICK(tsp) \
317 317 (SEC_TO_TICK((tsp)->tv_sec) + NSEC_TO_TICK((tsp)->tv_nsec))
318 318
319 319 typedef struct todinfo {
320 320 int tod_sec; /* seconds 0-59 */
321 321 int tod_min; /* minutes 0-59 */
322 322 int tod_hour; /* hours 0-23 */
323 323 int tod_dow; /* day of week 1-7 */
324 324 int tod_day; /* day of month 1-31 */
325 325 int tod_month; /* month 1-12 */
326 326 int tod_year; /* year 70+ */
327 327 } todinfo_t;
328 328
329 329 extern int64_t timedelta;
330 330 extern int timechanged;
331 331 extern int tod_needsync;
332 332 extern kmutex_t tod_lock;
333 333 extern volatile timestruc_t hrestime;
334 334 extern hrtime_t hres_last_tick;
335 335 extern int64_t hrestime_adj;
336 336 extern uint_t adj_shift;
337 337
338 338 extern timestruc_t tod_get(void);
339 339 extern void tod_set(timestruc_t);
340 340 extern void set_hrestime(timestruc_t *);
341 341 extern todinfo_t utc_to_tod(time_t);
342 342 extern time_t tod_to_utc(todinfo_t);
343 343 extern int hr_clock_lock(void);
344 344 extern void hr_clock_unlock(int);
345 345 extern hrtime_t gethrtime(void);
346 346 extern hrtime_t gethrtime_unscaled(void);
347 347 extern hrtime_t gethrtime_max(void);
348 348 extern hrtime_t gethrtime_waitfree(void);
349 349 extern void scalehrtime(hrtime_t *);
350 350 extern uint64_t unscalehrtime(hrtime_t);
351 351 extern void gethrestime(timespec_t *);
352 352 extern time_t gethrestime_sec(void);
353 353 extern void gethrestime_lasttick(timespec_t *);
354 354 extern void hrt2ts(hrtime_t, timestruc_t *);
355 355 extern hrtime_t ts2hrt(const timestruc_t *);
356 356 extern void hrt2tv(hrtime_t, struct timeval *);
357 357 extern hrtime_t tv2hrt(struct timeval *);
358 358 extern int itimerfix(struct timeval *, int);
359 359 extern int itimerdecr(struct itimerval *, int);
360 360 extern void timevaladd(struct timeval *, struct timeval *);
361 361 extern void timevalsub(struct timeval *, struct timeval *);
362 362 extern void timevalfix(struct timeval *);
363 363 extern void dtrace_hres_tick(void);
364 364
365 365 extern clock_t ddi_get_lbolt(void);
366 366 extern int64_t ddi_get_lbolt64(void);
367 367
368 368 #if defined(_SYSCALL32)
369 369 extern void hrt2ts32(hrtime_t, timestruc32_t *);
370 370 #endif
371 371
372 372 #endif /* _KERNEL */
373 373
374 374 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
375 375 #if defined(__STDC__)
376 376 int adjtime(struct timeval *, struct timeval *);
377 377 #else
378 378 int adjtime();
379 379 #endif
380 380 #endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
381 381
382 382 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || \
383 383 defined(_ATFILE_SOURCE) || defined(__EXTENSIONS__)
384 384 #if defined(__STDC__)
385 385 int futimesat(int, const char *, const struct timeval *);
386 386 #else
387 387 int futimesat();
388 388 #endif /* defined(__STDC__) */
389 389 #endif /* defined(__ATFILE_SOURCE) */
390 390
391 391 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
392 392 defined(__EXTENSIONS__)
393 393
394 394 #if defined(__STDC__)
395 395
396 396 int getitimer(int, struct itimerval *);
397 397 int utimes(const char *, const struct timeval *);
398 398 #if defined(_XPG4_2)
399 399 int setitimer(int, const struct itimerval *_RESTRICT_KYWD,
400 400 struct itimerval *_RESTRICT_KYWD);
401 401 #else
402 402 int setitimer(int, struct itimerval *_RESTRICT_KYWD,
403 403 struct itimerval *_RESTRICT_KYWD);
404 404 #endif /* defined(_XPG2_2) */
405 405
406 406 #else /* __STDC__ */
407 407
408 408 int gettimer();
409 409 int settimer();
410 410 int utimes();
411 411 #endif /* __STDC__ */
412 412 #endif /* !defined(_KERNEL) ... defined(_XPG4_2) */
413 413
414 414 /*
415 415 * gettimeofday() and settimeofday() were included in SVr4 due to their
416 416 * common use in BSD based applications. They were to be included exactly
417 417 * as in BSD, with two parameters. However, AT&T/USL noted that the second
418 418 * parameter was unused and deleted it, thereby making a routine included
419 419 * for compatibility, incompatible.
420 420 *
421 421 * XSH4.2 (spec 1170) defines gettimeofday and settimeofday to have two
422 422 * parameters.
423 423 *
424 424 * This has caused general disagreement in the application community as to
425 425 * the syntax of these routines. Solaris defaults to the XSH4.2 definition.
426 426 * The flag _SVID_GETTOD may be used to force the SVID version.
427 427 */
428 428 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
429 429
430 430 #if defined(__STDC__)
431 431 #if defined(_SVID_GETTOD)
432 432 int settimeofday(struct timeval *);
433 433 #else
434 434 int settimeofday(struct timeval *, void *);
435 435 #endif
436 436 hrtime_t gethrtime(void);
437 437 hrtime_t gethrvtime(void);
438 438 #else /* __STDC__ */
439 439 int settimeofday();
440 440 hrtime_t gethrtime();
441 441 hrtime_t gethrvtime();
442 442 #endif /* __STDC__ */
443 443
444 444 #endif /* !(defined _KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
445 445
446 446 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
447 447 defined(__EXTENSIONS__)
448 448
449 449 #if defined(__STDC__)
450 450 #if defined(_SVID_GETTOD)
451 451 int gettimeofday(struct timeval *);
452 452 #else
453 453 int gettimeofday(struct timeval *_RESTRICT_KYWD, void *_RESTRICT_KYWD);
454 454 #endif
455 455 #else /* __STDC__ */
456 456 int gettimeofday();
457 457 #endif /* __STDC__ */
458 458
459 459 #endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
460 460
461 461 /*
462 462 * The inclusion of <time.h> is historical and was added for
463 463 * backward compatibility in delta 1.2 when a number of definitions
464 464 * were moved out of <sys/time.h>. More recently, the timespec and
465 465 * itimerspec structure definitions, along with the _CLOCK_*, CLOCK_*,
466 466 * _TIMER_*, and TIMER_* symbols were moved to <sys/time_impl.h>,
467 467 * which is now included by <time.h>. This change was due to POSIX
468 468 * 1003.1b-1993 and X/Open UNIX 98 requirements. For non-POSIX and
469 469 * non-X/Open applications, including this header will still make
470 470 * visible these definitions.
471 471 */
472 472 #if !defined(_BOOT) && !defined(_KERNEL) && \
473 473 !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
474 474 #include <time.h>
475 475 #endif
476 476
477 477 /*
478 478 * The inclusion of <sys/select.h> is needed for the FD_CLR,
479 479 * FD_ISSET, FD_SET, and FD_SETSIZE macros as well as the
480 480 * select() prototype defined in the XOpen specifications
481 481 * beginning with XSH4v2. Placement required after definition
482 482 * for itimerval.
483 483 */
484 484 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
485 485 defined(__EXTENSIONS__)
486 486 #include <sys/select.h>
487 487 #endif
488 488
489 489 #endif /* _ASM */
490 490
491 491 #ifdef __cplusplus
492 492 }
493 493 #endif
494 494
495 495 #endif /* _SYS_TIME_H */
↓ open down ↓ |
248 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX