| 1 |
/**************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* This file is part of Frama-C. */ |
| 4 |
/* */ |
| 5 |
/* Copyright (C) 2007-2022 */ |
| 6 |
/* CEA (Commissariat à l'énergie atomique et aux énergies */ |
| 7 |
/* alternatives) */ |
| 8 |
/* */ |
| 9 |
/* you can redistribute it and/or modify it under the terms of the GNU */ |
| 10 |
/* Lesser General Public License as published by the Free Software */ |
| 11 |
/* Foundation, version 2.1. */ |
| 12 |
/* */ |
| 13 |
/* It is distributed in the hope that it will be useful, */ |
| 14 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 15 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 16 |
/* GNU Lesser General Public License for more details. */ |
| 17 |
/* */ |
| 18 |
/* See the GNU Lesser General Public License version 2.1 */ |
| 19 |
/* for more details (enclosed in the file licenses/LGPLv2.1). */ |
| 20 |
/* */ |
| 21 |
/**************************************************************************/ |
| 22 |
|
| 23 |
#ifndef __FC_SYS_TIME_H__ |
| 24 |
#define __FC_SYS_TIME_H__ |
| 25 |
#include "../features.h" |
| 26 |
__PUSH_FC_STDLIB |
| 27 |
#include "../__fc_define_fd_set_t.h" |
| 28 |
#include "../__fc_define_suseconds_t.h" |
| 29 |
#include "../__fc_define_time_t.h" |
| 30 |
#include "../__fc_define_timeval.h" |
| 31 |
#include "../__fc_string_axiomatic.h" |
| 32 |
__BEGIN_DECLS |
| 33 |
|
| 34 |
struct timezone { |
| 35 |
int tz_minuteswest; |
| 36 |
int tz_dsttime; |
| 37 |
}; |
| 38 |
|
| 39 |
/* Abstract representation of the current time. */ |
| 40 |
//@ ghost volatile unsigned int __fc_time; |
| 41 |
//@ ghost extern int __fc_tz; |
| 42 |
|
| 43 |
/*@ |
| 44 |
requires valid_path: valid_read_string(path); |
| 45 |
requires valid_times_or_null: \valid_read(times+(0..1)) || times == \null; |
| 46 |
assigns \result \from indirect:path[0..strlen(path)], |
| 47 |
indirect:times, indirect:times[0..1]; |
| 48 |
*/ |
| 49 |
extern int utimes(const char *path, const struct timeval times[2]); |
| 50 |
|
| 51 |
/*@ assigns tv->tv_sec, tv->tv_usec \from __fc_time; |
| 52 |
@ assigns *(struct timezone *)tz \from __fc_tz; |
| 53 |
@ assigns \result \from indirect:tv, indirect:tz, *tv, *(struct timezone *)tz, __fc_tz; |
| 54 |
@ ensures result_ok_or_error: \result == 0 || \result == -1; |
| 55 |
@ behavior tv_and_tz_null: |
| 56 |
@ assumes null_tv_tz: tv == \null && tz == \null; |
| 57 |
@ assigns \result \from indirect:__fc_tz; |
| 58 |
@ |
| 59 |
@ behavior tv_not_null: |
| 60 |
@ assumes non_null_tv_null_tz: tv != \null && tz == \null; |
| 61 |
@ assigns tv->tv_sec, tv->tv_usec \from indirect:__fc_time; |
| 62 |
@ assigns \result \from indirect:*tv, indirect:__fc_tz; |
| 63 |
@ ensures initialization:tv_sec:tv_usec: |
| 64 |
@ \initialized(&tv->tv_sec) && \initialized(&tv->tv_usec); |
| 65 |
@ ensures tv_usec_bounded: 0 <= tv->tv_usec <= 999999; |
| 66 |
@ |
| 67 |
@ behavior tz_not_null: |
| 68 |
@ assumes null_tv_non_null_tz: tv == \null && tz != \null; |
| 69 |
@ assigns *(struct timezone *)tz \from __fc_tz; |
| 70 |
@ assigns \result \from indirect:*(struct timezone *)tz, indirect:__fc_tz; |
| 71 |
@ ensures initialization:tz: \initialized((struct timezone *)tz); |
| 72 |
@ |
| 73 |
@ behavior tv_and_tz_not_null: |
| 74 |
@ assumes non_null_tv_tz: tv != \null && tz != \null; |
| 75 |
@ assigns tv->tv_sec, tv->tv_usec \from indirect:__fc_time; |
| 76 |
@ assigns *(struct timezone *)tz \from __fc_tz; |
| 77 |
@ assigns \result \from indirect:*tv, indirect:*(struct timezone *)tz, indirect:__fc_tz; |
| 78 |
@ ensures initialization:tv_sec:tv_usec: |
| 79 |
@ \initialized(&tv->tv_sec) && \initialized(&tv->tv_usec); |
| 80 |
@ ensures initialization:tz: \initialized((struct timezone *)tz); |
| 81 |
@ |
| 82 |
@ complete behaviors; |
| 83 |
@ disjoint behaviors; |
| 84 |
@*/ |
| 85 |
extern int gettimeofday(struct timeval * restrict tv, void * restrict tz); |
| 86 |
|
| 87 |
/*@ |
| 88 |
requires valid_tv_or_null: \valid_read(tv) || tv == \null; |
| 89 |
requires valid_tz_or_null: \valid_read(tz) || tz == \null; |
| 90 |
assigns __fc_time, __fc_tz \from tv->tv_sec, tv->tv_usec, |
| 91 |
tz->tz_dsttime, tz->tz_minuteswest; |
| 92 |
assigns \result \from indirect:*tv, indirect:*tz; |
| 93 |
ensures result_ok_or_error: \result == 0 || \result == -1; |
| 94 |
*/ |
| 95 |
extern int settimeofday(const struct timeval *tv, const struct timezone *tz); |
| 96 |
|
| 97 |
// Note: definitions related to itimerval are obsolescent in POSIX and may be |
| 98 |
// removed in future versions. Strictly conforming applications should not use |
| 99 |
// them. |
| 100 |
#define ITIMER_REAL 0 |
| 101 |
#define ITIMER_VIRTUAL 1 |
| 102 |
#define ITIMER_PROF 2 |
| 103 |
|
| 104 |
struct itimerval { |
| 105 |
struct timeval it_interval; /* timer interval */ |
| 106 |
struct timeval it_value; /* current value */ |
| 107 |
}; |
| 108 |
|
| 109 |
/* Abstract representation of interval timers. */ |
| 110 |
//@ ghost volatile struct itimerval __fc_itimer_real; |
| 111 |
//@ ghost volatile struct itimerval __fc_itimer_virtual; |
| 112 |
//@ ghost volatile struct itimerval __fc_itimer_prof; |
| 113 |
|
| 114 |
// NOTE: for functions setitimer/getitimer, we do not currently model |
| 115 |
// the signals generated by the timers when they reach zero. |
| 116 |
|
| 117 |
/*@ |
| 118 |
requires valid_curr_value: \valid(curr_value); |
| 119 |
assigns \result \from indirect:which; |
| 120 |
assigns *curr_value \from __fc_itimer_real, __fc_itimer_virtual, |
| 121 |
__fc_itimer_prof; |
| 122 |
ensures initialization:curr_value: \initialized(curr_value); |
| 123 |
behavior real: |
| 124 |
assumes itimer_real: which == ITIMER_REAL; |
| 125 |
assigns \result \from \nothing; |
| 126 |
assigns *curr_value \from __fc_itimer_real; |
| 127 |
ensures result_ok: \result == 0; |
| 128 |
behavior virtual: |
| 129 |
assumes itimer_virtual: which == ITIMER_VIRTUAL; |
| 130 |
assigns \result \from \nothing; |
| 131 |
assigns *curr_value \from __fc_itimer_virtual; |
| 132 |
ensures result_ok: \result == 0; |
| 133 |
behavior prof: |
| 134 |
assumes itimer_prof: which == ITIMER_PROF; |
| 135 |
assigns \result \from \nothing; |
| 136 |
assigns *curr_value \from __fc_itimer_prof; |
| 137 |
ensures result_ok: \result == 0; |
| 138 |
behavior invalid: |
| 139 |
assumes invalid_which: |
| 140 |
which != ITIMER_REAL && which != ITIMER_VIRTUAL && which != ITIMER_PROF; |
| 141 |
assigns \result \from \nothing; |
| 142 |
ensures result_error: \result == -1; |
| 143 |
complete behaviors; |
| 144 |
disjoint behaviors; |
| 145 |
*/ |
| 146 |
extern int getitimer(int which, struct itimerval *curr_value); |
| 147 |
|
| 148 |
// TODO: replace with a predicate, when Value will be able to evalute it |
| 149 |
// precisely |
| 150 |
#define __VALID_ITIMERVAL(tv) (0 <= (tv)->it_value.tv_usec <= 999999 && \ |
| 151 |
0 <= (tv)->it_interval.tv_usec <= 999999) |
| 152 |
|
| 153 |
// Note: this specification is more strict than POSIX, since we forbid new_value |
| 154 |
// from being NULL. |
| 155 |
// Note: old_value depends on new_value because e.g. if new_value contains |
| 156 |
// an invalid tv_usec field, the function may fail. |
| 157 |
/*@ |
| 158 |
requires valid_new_value: \valid_read(new_value); |
| 159 |
requires old_value_null_or_valid: old_value == \null || \valid(old_value); |
| 160 |
assigns *old_value \from indirect:which, indirect:old_value, |
| 161 |
indirect:new_value, __fc_itimer_real, |
| 162 |
__fc_itimer_virtual, __fc_itimer_prof; |
| 163 |
assigns \result \from indirect:which, indirect:new_value, indirect:*new_value; |
| 164 |
ensures result_ok_or_error: \result == 0 || \result == -1; |
| 165 |
behavior real: |
| 166 |
assumes itimer_real_and_valid: |
| 167 |
which == ITIMER_REAL && __VALID_ITIMERVAL(new_value); |
| 168 |
assigns \result \from \nothing; |
| 169 |
assigns *old_value \from __fc_itimer_real; |
| 170 |
assigns __fc_itimer_real \from *new_value; |
| 171 |
ensures result_ok: \result == 0; |
| 172 |
ensures initialization:old_value: \initialized(old_value); |
| 173 |
behavior virtual: |
| 174 |
assumes itimer_virtual_and_valid: |
| 175 |
which == ITIMER_VIRTUAL && __VALID_ITIMERVAL(new_value); |
| 176 |
assigns \result \from \nothing; |
| 177 |
assigns *old_value \from __fc_itimer_virtual; |
| 178 |
ensures result_ok: \result == 0; |
| 179 |
ensures initialization:old_value: \initialized(old_value); |
| 180 |
behavior prof: |
| 181 |
assumes itimer_prof_and_valid: |
| 182 |
which == ITIMER_PROF && __VALID_ITIMERVAL(new_value); |
| 183 |
assigns \result \from \nothing; |
| 184 |
assigns *old_value \from __fc_itimer_prof; |
| 185 |
ensures result_ok: \result == 0; |
| 186 |
ensures initialization:old_value: \initialized(old_value); |
| 187 |
behavior invalid: |
| 188 |
assumes invalid_itimer_or_new_value: |
| 189 |
(which != ITIMER_REAL && which != ITIMER_VIRTUAL && which != ITIMER_PROF) |
| 190 |
|| !__VALID_ITIMERVAL(new_value); |
| 191 |
assigns \result \from \nothing; |
| 192 |
ensures result_error: \result == -1; |
| 193 |
disjoint behaviors; |
| 194 |
*/ |
| 195 |
extern int setitimer (int which, |
| 196 |
const struct itimerval *restrict new_value, |
| 197 |
struct itimerval *restrict old_value); |
| 198 |
|
| 199 |
// Non-POSIX, non-C99 functions (present in Linux and most BSDs) |
| 200 |
extern void timeradd(struct timeval *a, struct timeval *b, |
| 201 |
struct timeval *res); |
| 202 |
|
| 203 |
extern void timersub(struct timeval *a, struct timeval *b, |
| 204 |
struct timeval *res); |
| 205 |
|
| 206 |
extern void timerclear(struct timeval *tvp); |
| 207 |
|
| 208 |
extern int timerisset(struct timeval *tvp); |
| 209 |
|
| 210 |
#define timercmp(a, b, _CMP) _timercmp(a, b) |
| 211 |
extern int _timercmp(struct timeval *a, struct timeval *b); |
| 212 |
|
| 213 |
// From POSIX, and for better compatibility with existing code bases: |
| 214 |
// "Inclusion of the <sys/time.h> header may make visible all symbols |
| 215 |
// from the <sys/select.h> header." |
| 216 |
#include "select.h" |
| 217 |
|
| 218 |
__END_DECLS |
| 219 |
__POP_FC_STDLIB |
| 220 |
#endif |