summaryrefslogtreecommitdiff
path: root/src/basic/time-util.c
Commit message (Collapse)AuthorAgeFilesLines
* tree-wide: use TAKE_PTR() and TAKE_FD() macrosYu Watanabe2018-04-051-2/+1
|
* log: minimize includes in log.hLennart Poettering2018-01-111-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | log.h really should only include the bare minimum of other headers, as it is really pulled into pretty much everything else and already in itself one of the most basic pieces of code we have. Let's hence drop inclusion of: 1. sd-id128.h because it's entirely unneeded in current log.h 2. errno.h, dito. 3. sys/signalfd.h which we can replace by a simple struct forward declaration 4. process-util.h which was needed for getpid_cached() which we now hide in a funciton log_emergency_level() instead, which nicely abstracts the details away. 5. sys/socket.h which was needed for struct iovec, but a simple struct forward declaration suffices for that too. Ultimately this actually makes our source tree larger (since users of the functionality above must now include it themselves, log.h won't do that for them), but I think it helps to untangle our web of includes a tiny bit. (Background: I'd like to isolate the generic bits of src/basic/ enough so that we can do a git submodule import into casync for it)
* process-spec: add another flag FORK_WAIT to safe_fork()Lennart Poettering2018-01-041-8/+1
| | | | | | | | | This new flag will cause safe_fork() to wait for the forked off child before returning. This allows us to unify a number of cases where we immediately wait on the forked off child, witout running any code in the parent after the fork, and without direct interest in the precise exit status of the process, except recgonizing EXIT_SUCCESS vs everything else.
* tree-wide: introduce new safe_fork() helper and port everything overLennart Poettering2017-12-251-8/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | This adds a new safe_fork() wrapper around fork() and makes use of it everywhere. The new wrapper does a couple of things we previously did manually and separately in a safer, more correct and automatic way: 1. Optionally resets signal handlers/mask in the child 2. Sets a name on all processes we fork off right after forking off (and the patch assigns useful names for all processes we fork off now, following a systematic naming scheme: always enclosed in () – in order to indicate that these are not proper, exec()ed processes, but only forked off children, and if the process is long-running with only our own code, without execve()'ing something else, it gets am "sd-" prefix.) 3. Optionally closes all file descriptors in the child 4. Optionally sets a PR_SET_DEATHSIG to SIGTERM in the child, in a safe way so that the parent dying before this happens being handled safely. 5. Optionally reopens the logs 6. Optionally connects stdin/stdout/stderr to /dev/null 7. Debug logs about the forked off processes.
* tree-wide: drop a few == NULL and != NULL comparisonLennart Poettering2017-12-111-1/+1
| | | | | | | Our CODING_STYLE suggests not comparing with NULL, but relying on C's downgrade-to-bool feature for that. Fix up some code to match these guidelines. (This is not comprehensive, the coccinelle output for this is unfortunately kinda borked)
* tree-wide: adjust fall through comments so that gcc is happyShawn Landden2017-11-201-2/+1
| | | | | | | | Distcc removes comments, making the comment silencing not work. I know there was a decision against a macro in commit ec251fe7d5bc24b5d38b0853bc5969f3a0ba06e2
* util: add new helper in_utc_timezone()Lennart Poettering2017-11-201-0/+6
| | | | | As the name suggests it checks whether we are running in an UTC timezone.
* Add SPDX license identifiers to source files under the LGPLZbigniew Jędrzejewski-Szmek2017-11-191-0/+1
| | | | | This follows what the kernel is doing, c.f. https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5fd54ace4721fc5ce2bb5aef6318fcf17f421460.
* unit: when JobTimeoutSec= is turned off, implicitly turn off ↵Lennart Poettering2017-10-051-0/+4
| | | | | | | | | | | | | JobRunningTimeoutSec= too We added JobRunningTimeoutSec= late, and Dracut configured only JobTimeoutSec= to turn of root device timeouts before. With this change we'll propagate a reset of JobTimeoutSec= into JobRunningTimeoutSec=, but only if the latter wasn't set explicitly. This should restore compatibility with older systemd versions. Fixes: #6402
* tree-wide: use IN_SET macro (#6977)Yu Watanabe2017-10-041-1/+1
|
* time-util: mktime_or_timegm are changing the struct tmMarcel Hollerbach2017-09-221-3/+3
| | | | | after that wm_day etc. seems to be changed. Moving the check infront of the mktime_or_timegm fixes that.
* time-util: correctly handle the timezone when parsingMarcel Hollerbach2017-09-221-3/+11
| | | | | | | | | | The timezone was cut off the string once the timezone was not UTC. If it is not UTC but a other timezone that matches tzname[0] or tzname[1], then we can leave it to the impl function to parse that correctly. If not we can just fallback to whatever is the current timezone is in the given t_timezone. This should fix the testuite and tests.
* time-util: fix shadowing of timezoneMarcel Hollerbach2017-09-211-4/+4
| | | | | timezone was shadowing timezone from time.h which leads to a buildbreak since systemd is built with -Werror
* Fix for dst/non-dst timezonesIvan Kurnosov2017-09-171-5/+3
| | | | | | | | | The problem was with the tm.tm_isdst that is set to the current environment value: either DST or not. While the current state is not relevant to the state in the desired date. Hence — it should be reset so that the mktime_or_timegm could normalise it later.
* Simplify the if cases for timezone checkingZbigniew Jędrzejewski-Szmek2017-09-171-6/+2
| | | | Just to reduce the indentation a bit.
* Added timezone to the CalendarSpec, parser/formatter and the timedatectlIvan Kurnosov2017-09-171-61/+131
|
* time-util: make parse_timestamp() return -EINVAL if the input is very old ↵Yu Watanabe2017-07-111-11/+7
| | | | | | | | date (#6327) This reverts 7635ab8e74ea4a94e81143c3077570a986df375c and makes parse_timestamp() return -EINVAL if the input is older than 1970-01-01. Fixes #6290.
* time-util: make parse_timestamp() set 0 if the input is very old date (#6297)Yu Watanabe2017-07-081-4/+8
| | | | | | | | | If the input is older than "1970-01-01 UTC", then `parse_timestamp()` fails and returns -EINVAL. However, if the input is e.g. `-100years`, then the function succeeds and sets `usec = 0`. This commit makes the function also succeed for old dates and set `usec = 0`. Fixes #6290.
* Parse "timeout=0" as infinity in various generators (#6264)Zbigniew Jędrzejewski-Szmek2017-07-031-0/+10
| | | | | | | | | | | | This extends 2d79a0bbb9f651656384a0a86ed814e6306fb5dd to the kernel command line parsing. The parsing is changed a bit to only understand "0" as infinity. If units are specified, parse normally, e.g. "0s" is just 0. This makes it possible to provide a zero timeout if necessary. Simple test is added. Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1462378.
* time-util: add new call usec_shift_clock() for converting times between clocksLennart Poettering2017-06-221-0/+19
| | | | We use that quite often, let's implement one clean version of it.
* time-util: rename usec_sub() to usec_sub_signed() and add usec_sub_unsigned()Lennart Poettering2017-06-221-6/+6
| | | | | Quite often we just want to subtract two normal usec_t values, hence provide an implementation for that.
* basic/time-util: make parsing of dual_timestamp more strictZbigniew Jędrzejewski-Szmek2017-05-191-2/+16
| | | | | | | | | | | *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.
* time-util: Fix overflow check introduce in commit f977849 (#5216)Benjamin Robin2017-02-031-1/+1
|
* time-util: add overflow checking to monotonic timestamp specificationsLennart Poettering2017-02-021-0/+2
|
* time-util: when formatting usec_t as raw integers use PRIu64Lennart Poettering2017-02-021-2/+2
| | | | After all, usec_t is defined as uint64_t, and not as unsigned long long.
* time-util: when converting to time_t do something useful in 2038Lennart Poettering2017-02-021-2/+4
| | | | | | On systems where time_t is 32bit we should invalidate the timeval/timespec instead of proceeding with a potentially overflown value.
* time-util: refuse formatting/parsing times that we can't storeLennart Poettering2017-02-021-2/+9
| | | | | | | | | | | 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.
* time: time_t is signed, and mktime() is happy to return negative timeLennart Poettering2017-02-021-5/+4
| | | | | | | | | | 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.
* tree-wide: adjust fall through comments so that gcc is happyZbigniew Jędrzejewski-Szmek2017-01-311-1/+1
| | | | | | | | | | | | | | | 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.)
* time-util: accept "µs" as time unit, in addition to "us" (#4836)Lennart Poettering2016-12-061-0/+2
| | | | | | | | 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.
* tree-wide: add PRI_[NU]SEC, and use time format strings moreZbigniew Jędrzejewski-Szmek2016-11-071-4/+4
|
* time-util: export timespec_load_nsec()Ivan Shapovalov2016-09-151-3/+1
|
* util-lib: make timestamp generation and parsing reversible (#3869)Lennart Poettering2016-08-031-23/+123
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* time-util: add triple timestamp objectLennart Poettering2016-06-061-0/+72
| | | | | | | | | | | | 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.
* tree-wide: don't assume CLOCK_BOOTIME is generally availableLennart Poettering2016-04-221-14/+23
| | | | | | | | | | | | 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
* Revert "time-util: fall back to CLOCK_MONOTONIC if CLOCK_BOOTTIME unsupported"revert-2843-clock_boottimeLennart Poettering2016-04-011-5/+2
|
* time-util: fall back to CLOCK_MONOTONIC if CLOCK_BOOTTIME unsupportedLubomir Rintel2016-03-151-2/+5
| | | | | | | 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.
* tree-wide: make ++/-- usage consistent WRT spacingVito Caputo2016-02-221-2/+2
| | | | | | 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.
* time-util: check for overflow in conversion from ts to nsec_tZbigniew Jędrzejewski-Szmek2016-02-201-7/+6
| | | | CID #1320855.
* time-util: rewrite check in a way that does not confuse gccZbigniew Jędrzejewski-Szmek2016-02-181-18/+19
| | | | | gcc thinks that multiplier might be unitialized. Split out the inner loop to make the function easier to grok.
* time-util: Rename and fix call of deserialize_timestamp_value()Benjamin Robin2016-02-151-4/+3
| | | | | | 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
* time-util: introduce deserialize_timestamp_value()Alexander Kuleshov2016-02-161-0/+13
| | | | | | | | | | | | | 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.
* time-util: map ALARM clockids to non-ALARM clockids in now()Lennart Poettering2016-02-121-2/+22
| | | | Fixes: #2597
* tree-wide: remove Emacs lines from all filesDaniel Mack2016-02-101-2/+0
| | | | | This should be handled fine now by .dir-locals.el, so need to carry that stuff in every file.
* time-util: make timespec_load_nsec() staticAlexander Kuleshov2016-02-101-2/+3
| | | | | The timespec_load_nsec() function has no callers outside of the time-util.c, so we can make it static.
* Merge pull request #2542 from 0xAX/get_ts_deltaLennart Poettering2016-02-091-22/+4
|\ | | | | time-util: cleanups
| * time-util: introduce usec_sub()Alexander Kuleshov2016-02-091-22/+4
| | | | | | | | | | | | | | 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.
* | time-util: merge format_timestamp_internal() and format_timestamp_internal_us()Alexander Kuleshov2016-02-091-28/+18
|/ | | | | | | | | | | | | | | | 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.
* time-util: use dual_timestamp_get()Alexander Kuleshov2016-02-061-2/+1
| | | | | | 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.
* basic: re-sort includesThomas Hindoe Paaboel Andersen2015-12-011-2/+2
| | | | | My previous patch to only include what we use accidentially placed the added inlcudes in non-sorted order.