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