Commit graph

99 commits

Author SHA1 Message Date
Lennart Poettering 54d8ef14d8 time-util: rename usec_sub() to usec_sub_signed() and add usec_sub_unsigned()
Quite often we just want to subtract two normal usec_t values, hence
provide an implementation for that.
2017-06-22 20:51:06 +02:00
Zbigniew Jędrzejewski-Szmek 9c0565b2c3 basic/time-util: make parsing of dual_timestamp more strict
*scanf functions set errno on i/o error. For sscanf, this doesn't really apply,
so (based on the man page), it seems that errno is unlikely to be ever set to a
useful value. So just ignore errno. The error message includes the string that
was parsed, so it should be always pretty clear why parsing failed.

On the other hand, detect trailing characters and minus prefix that weren't
converted properly. This matches what our safe_ato* functions do. Add tests to
elucidate various edge cases.
2017-05-19 15:01:20 -04:00
Benjamin Robin d201d90838 time-util: Fix overflow check introduce in commit f977849 (#5216) 2017-02-03 16:13:55 +01:00
Lennart Poettering 315782db14 time-util: add overflow checking to monotonic timestamp specifications 2017-02-02 20:12:32 +01:00
Lennart Poettering 74c5b33b0a time-util: when formatting usec_t as raw integers use PRIu64
After all, usec_t is defined as uint64_t, and not as unsigned long long.
2017-02-02 20:12:31 +01:00
Lennart Poettering f977849c24 time-util: when converting to time_t do something useful in 2038
On systems where time_t is 32bit we should invalidate the
timeval/timespec instead of proceeding with a potentially overflown
value.
2017-02-02 20:12:31 +01:00
Lennart Poettering 1bb4b028a3 time-util: refuse formatting/parsing times that we can't store
usec_t is always 64bit, which means it can cover quite a number of
years. However, 4 digit year display and glibc limitations around time_t
limit what we can actually parse and format. Let's make this explicit,
so that we never end up formatting dates we can#t parse and vice versa.

Note that this is really just about formatting/parsing. Internal
calculations with times outside of the formattable range are not
affected.
2017-02-02 20:12:31 +01:00
Lennart Poettering c477ff141b time: time_t is signed, and mktime() is happy to return negative time
Passing a year such as 1960 to mktime() will result in a negative return
value. This is quite confusing, as the man page claims that on failure
the call will return -1...

Given that our own usec_t type is unsigned, and we can't express times
before 1970 hence, let's consider all negative times returned by
mktime() as invalid, regardless if just -1, or anything else negative.
2017-02-02 20:12:31 +01:00
Zbigniew Jędrzejewski-Szmek ec251fe7d5 tree-wide: adjust fall through comments so that gcc is happy
gcc 7 adds -Wimplicit-fallthrough=3 to -Wextra. There are a few ways
we could deal with that. After we take into account the need to stay compatible
with older versions of the compiler (and other compilers), I don't think adding
__attribute__((fallthrough)), even as a macro, is worth the trouble. It sticks
out too much, a comment is just as good. But gcc has some very specific
requiremnts how the comment should look. Adjust it the specific form that it
likes. I don't think the extra stuff we had in those comments was adding much
value.

(Note: the documentation seems to be wrong, and seems to describe a different
pattern from the one that is actually used. I guess either the docs or the code
will have to change before gcc 7 is finalized.)
2017-01-31 14:04:55 -05:00
Lennart Poettering 5efdbf11d1 time-util: accept "µs" as time unit, in addition to "us" (#4836)
Let's accept "µs" as alternative time unit for microseconds. We already accept
"us" and "usec" for them, lets extend on this and accept the proper scientific
unit specification too.

We will never output this as time unit, but it's fine to accept it, after all
we are pretty permissive with time units already.
2016-12-06 10:51:26 +01:00
Zbigniew Jędrzejewski-Szmek 70887c5f29 tree-wide: add PRI_[NU]SEC, and use time format strings more 2016-11-07 22:49:09 -05:00
Ivan Shapovalov 3a730176b3 time-util: export timespec_load_nsec() 2016-09-15 05:21:09 +03:00
Lennart Poettering 21b3a0fcd1 util-lib: make timestamp generation and parsing reversible (#3869)
This patch improves parsing and generation of timestamps and calendar
specifications in two ways:

- The week day is now always printed in the abbreviated English form, instead
  of the locale's setting. This makes sure we can always parse the week day
  again, even if the locale is changed. Given that we don't follow locale
  settings for printing timestamps in any other way either (for example, we
  always use 24h syntax in order to make uniform parsing possible), it only
  makes sense to also stick to a generic, non-localized form for the timestamp,
  too.

- When parsing a timestamp, the local timezone (in its DST or non-DST name)
  may be specified, in addition to "UTC". Other timezones are still not
  supported however (not because we wouldn't want to, but mostly because libc
  offers no nice API for that). In itself this brings no new features, however
  it ensures that any locally formatted timestamp's timezone is also parsable
  again.

These two changes ensure that the output of format_timestamp() may always be
passed to parse_timestamp() and results in the original input. The related
flavours for usec/UTC also work accordingly. Calendar specifications are
extended in a similar way.

The man page is updated accordingly, in particular this removes the claim that
timestamps systemd prints wouldn't be parsable by systemd. They are now.

The man page previously showed invalid timestamps as examples. This has been
removed, as the man page shouldn't be a unit test, where such negative examples
would be useful. The man page also no longer mentions the names of internal
functions, such as format_timestamp_us() or UNIX error codes such as EINVAL.
2016-08-03 19:04:53 -04:00
Lennart Poettering fe624c4c07 time-util: add triple timestamp object
We already have a double timestamp object that we use whenever we need both a
MONOTONIC and a REALTIME timestamp taken and stored. With this change we
also add a triple timestamp object that in addition stores a BOOTTIME
timestamp, which is useful for a few usecases.

Note that we keep dual_timestamp around, as it is useful in many cases where
triple_timestamp is not, in particular because retrieving the monotonic and
realtime timestamps is much cheaper on Linux that getting the boottime
timestamp.
2016-06-06 19:59:07 +02:00
Lennart Poettering 3411372e35 tree-wide: don't assume CLOCK_BOOTIME is generally available
Before we invoke now(CLOCK_BOOTTIME), let's make sure we actually have that
clock, since now() will otherwise hit an assert.

Specifically, let's refuse CLOCK_BOOTTIME early in sd-event if the kernel
doesn't actually support it.

This is a follow-up for #3037, and specifically:

https://github.com/systemd/systemd/pull/3037#issuecomment-210199167
2016-04-22 16:06:20 +02:00
Lennart Poettering bdf19f8fb2 Revert "time-util: fall back to CLOCK_MONOTONIC if CLOCK_BOOTTIME unsupported" 2016-04-01 09:48:36 +02:00
Lubomir Rintel 2abd5b5a49 time-util: fall back to CLOCK_MONOTONIC if CLOCK_BOOTTIME unsupported
It was added in 2.6.39, and causes an assertion to fail when running in mock
hosted on 2.6.23-based RHEL-6:

Assertion 'clock_gettime(map_clock_id(clock_id), &ts) == 0' failed at systemd/src/basic/time-util.c:70, function now(). Aborting.
2016-03-15 12:43:33 +01:00
Vito Caputo 313cefa1d9 tree-wide: make ++/-- usage consistent WRT spacing
Throughout the tree there's spurious use of spaces separating ++ and --
operators from their respective operands.  Make ++ and -- operator
consistent with the majority of existing uses; discard the spaces.
2016-02-22 20:32:04 -08:00
Zbigniew Jędrzejewski-Szmek a2daa2f075 time-util: check for overflow in conversion from ts to nsec_t
CID #1320855.
2016-02-20 19:37:10 -05:00
Zbigniew Jędrzejewski-Szmek 240a7ba9d8 time-util: rewrite check in a way that does not confuse gcc
gcc thinks that multiplier might be unitialized. Split out the inner
loop to make the function easier to grok.
2016-02-18 19:39:10 -05:00
Benjamin Robin b895a7353b time-util: Rename and fix call of deserialize_timestamp_value()
The deserialize_timestamp_value() is renamed timestamp_deserialize() to be more
consistent with dual_timestamp_deserialize()
And add the NULL check back on realtime and monotonic
2016-02-15 23:26:34 +01:00
Alexander Kuleshov ebf30a086d time-util: introduce deserialize_timestamp_value()
The time-util.c provides dual_timestamp_deserialize() function to
convert value to usec_t and set it as value of ts->monotonic and
ts->realtime.

There are some places in code which do the same but only for one
clockid_t (realtime or monotonic), when dual_timestamp_deserialize()
sets value of both.

This patch introduces the deserialize_timestamp_value() which converts
 a given value to usec_t and write it to a given timestamp.
2016-02-16 00:01:44 +06:00
Lennart Poettering 32c1f5a579 time-util: map ALARM clockids to non-ALARM clockids in now()
Fixes: #2597
2016-02-12 21:30:15 +01:00
Daniel Mack b26fa1a2fb tree-wide: remove Emacs lines from all files
This should be handled fine now by .dir-locals.el, so need to carry that
stuff in every file.
2016-02-10 13:41:57 +01:00
Alexander Kuleshov aaea9db80c time-util: make timespec_load_nsec() static
The timespec_load_nsec() function has no callers outside of the
time-util.c, so we can make it static.
2016-02-10 17:46:52 +06:00
Lennart Poettering 70b65964d7 Merge pull request #2542 from 0xAX/get_ts_delta
time-util: cleanups
2016-02-09 00:23:47 +01:00
Alexander Kuleshov 5d634ca8ce time-util: introduce usec_sub()
The dual_timestamp_from_realtime(), dual_timestamp_from_monotonic()
and dual_timestamp_from_boottime_or_monotonic() shares the same
code for comparison given ts with delta. Let's move it to the
separate inline function to prevent code duplication.
2016-02-09 01:15:17 +06:00
Alexander Kuleshov 0056086af6 time-util: merge format_timestamp_internal() and format_timestamp_internal_us()
The time_util.c provides format_timestamp_internal() and
format_timestamp_internal_us() functions for a timestamp formating. Both
functions are very similar and differ only in formats handling.

We can add additional boolean parameter to the format_timestamp_internal()
function which will represent is a format for us timestamp or not.
This allows us to get rid of format_timestamp_internal_us() that is prevent
code duplication.

We can remove format_timestamp_internal_us() safely, because it is static and
has no users outside of the time_util.c. New fourth parameter will be passed
inside of the format_timestamp(), format_timestamp_us() and etc, functions,
but the public API is not changed.
2016-02-09 00:15:40 +06:00
Alexander Kuleshov 0345d25293 time-util: use dual_timestamp_get()
The time-util.c provides dual_timestamp_get() function for getting
realtime and monotonic timestamps. Let's use it instead of direct
realtime/monotonic calculation.
2016-02-06 18:26:05 +06:00
Thomas Hindoe Paaboel Andersen 93cc7779e0 basic: re-sort includes
My previous patch to only include what we use accidentially placed
the added inlcudes in non-sorted order.
2015-12-01 23:40:17 +01:00
Thomas Hindoe Paaboel Andersen 11c3a36649 basic: include only what we use
This is a cleaned up result of running iwyu but without forward
declarations on src/basic.
2015-11-30 21:51:03 +01:00
Hristo Venev 436dd70f53 calendarspec: sub-second support, v3 2015-11-17 23:52:09 +02:00
Lennart Poettering eb55ec9fec util-lib: when parsing time values, accept "M" as short for "month"
nginx defines an uppercase "M" that way (in contrast to the lowercase
"m" for "minute"), and it sounds like an OK logic to follow, so that we
understand a true superset of time values nginx understands.

http://nginx.org/en/docs/syntax.html
2015-11-13 19:50:52 +01:00
Lennart Poettering 519cffec89 time-util: add parse_time(), which is like parse_sec() but allows specification of default time unit if none is specified
This is useful if we want to parse RLIMIT_RTTIME values where the common
UNIX syntax is without any units but refers to a non-second unit (µs in
this case), but where we want to allow specification of units.
2015-11-10 17:31:31 +01:00
Susant Sahani 87b8ce69f6 Time-util: introduce usec to jiffies 2015-11-03 21:58:42 +05:30
Lennart Poettering b5efdb8af4 util-lib: split out allocation calls into alloc-util.[ch] 2015-10-27 13:45:53 +01:00
Lennart Poettering f4f15635ec util-lib: move a number of fs operations into fs-util.[ch] 2015-10-27 13:25:56 +01:00
Lennart Poettering 0d39fa9c69 util-lib: move more file I/O related calls into fileio.[ch] 2015-10-27 13:25:55 +01:00
Hristo Venev 078efddd37 basic: use the return value of endswith
It returns the position where the suffix begins, which can be used for
strndup to extract the prefix without calling strlen.
2015-10-25 18:46:20 +02:00
Lennart Poettering 3ffd4af220 util-lib: split out fd-related operations into fd-util.[ch]
There are more than enough to deserve their own .c file, hence move them
over.
2015-10-25 13:19:18 +01:00
Lennart Poettering 07630cea1f util-lib: split our string related calls from util.[ch] into its own file string-util.[ch]
There are more than enough calls doing string manipulations to deserve
its own files, hence do something about it.

This patch also sorts the #include blocks of all files that needed to be
updated, according to the sorting suggestions from CODING_STYLE. Since
pretty much every file needs our string manipulation functions this
effectively means that most files have sorted #include blocks now.

Also touches a few unrelated include files.
2015-10-24 23:05:02 +02:00
Hristo Venev e4eaf99a31 basic: parse_timestamp UTC and fractional seconds support 2015-10-15 02:57:57 +03:00
Hristo Venev 7c67c79c9a basic: add mktime_or_timegm and localtime_or_gmtime_r
to time-util.h. They take an extra argument `bool utc`.
2015-10-15 02:34:45 +03:00
Thomas Hindoe Paaboel Andersen 64d6c22905 tree-wide: do not shadow the global var timezone 2015-09-01 18:20:13 +02:00
Tom Gundersen 84d449b552 Merge pull request #1063 from poettering/dbus-interface-from-type
cgls/cgtop: a variety of modernizations
2015-08-28 12:01:46 +02:00
Lennart Poettering 45d7a8bb6c cgtop: major modernizations
In preparation of the unified cgroup support, let's clean up cgtop:

a) rework time code to be based on "nsec_t" rather than "struct timespec"

b) Introduce long option --order= for selecting ordering

c) count number of processes only in the main hierarchy, don't bother
   with the controller hierarchies. We don't allow orthogonal
   hierarchies in systemd anymore, hence there's no point to check the
   other hierarchies.

d) Deal with non-monotonic cpuacct values (see #749)

e) When sorting groups, don't do prefix compare when ordering by number
   of tasks, since this is not accumulative for all children.

f) Actually make --cpu without parameter work

g) Don't output control characters when we get them as input.

Fixes #749.
2015-08-28 02:27:29 +02:00
Lennart Poettering 5c904ba5a5 time-util: add new get_timezone() call to get local timezone
Let's move the timedated-specific code to time-util.h and make it
generic.
2015-08-26 20:36:42 +02:00
Lennart Poettering fbe550738d machined: introduce pseudo-machine ".host" refererring to the host system
Some of the operations machined/machinectl implement are also very
useful when applied to the host system (such as machinectl login,
machinectl shell or machinectl status), hence introduce a pseudo-machine
by the name of ".host" in machined that refers to the host system, and
may be used top execute operations on the host system with.

This copies the pseudo-image ".host" machined already implements for
image related commands.

(This commit also adds a PK privilege for opening a PTY in a container,
which was previously not accessible for non-root.)
2015-08-24 22:46:45 +02:00
Kay Sievers a095315b3c build-sys: split internal basic/ library from shared/
basic/      can be used by everything
            cannot use anything outside of basic/

libsystemd/ can use basic/
            cannot use shared/

shared/     can use libsystemd/
2015-06-11 10:52:46 +02:00
Renamed from src/shared/time-util.c (Browse further)