summaryrefslogtreecommitdiff
path: root/src/basic/time-util.c
Commit message (Collapse)AuthorAge
* tree-wide: remove Lennart's copyright linesLennart Poettering2018-08-24
| | | | | | | | | | | These lines are generally out-of-date, incomplete and unnecessary. With SPDX and git repository much more accurate and fine grained information about licensing and authorship is available, hence let's drop the per-file copyright notice. Of course, removing copyright lines of others is problematic, hence this commit only removes my own lines and leaves all others untouched. It might be nicer if sooner or later those could go away too, making git the only and accurate source of authorship information.
* tree-wide: drop 'This file is part of systemd' blurbLennart Poettering2018-08-24
| | | | | | | | | | | | | | | | This part of the copyright blurb stems from the GPL use recommendations: https://www.gnu.org/licenses/gpl-howto.en.html The concept appears to originate in times where version control was per file, instead of per tree, and was a way to glue the files together. Ultimately, we nowadays don't live in that world anymore, and this information is entirely useless anyway, as people are very welcome to copy these files into any projects they like, and they shouldn't have to change bits that are part of our copyright header for that. hence, let's just get rid of this old cruft, and shorten our codebase a bit.
* util-lib: reject too long path for timedate_is_valid()Yu Watanabe2018-08-24
| | | | This should fix https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8827.
* time-util: introduce common implementation of TFD_TIMER_CANCEL_ON_SET client ↵Lennart Poettering2018-08-24
| | | | | | code We now use pretty much the same code at three places, let's unify that.
* time-util: fix build with gcc8 -Werror=format-truncation=Martin Jansa2018-08-24
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * it fails with gcc8 when -O1 or -Os is used (and -ftree-vrp which is added by -O2 and higher isn't used) ../git/src/basic/time-util.c: In function 'format_timespan': ../git/src/basic/time-util.c:508:46: error: '%0*llu' directive output between 1 and 2147483647 bytes may cause result to exceed 'INT_MAX' [-Werror=format-truncation=] "%s"USEC_FMT".%0*"PRI_USEC"%s", ^~~~ ../git/src/basic/time-util.c:508:60: note: format string is defined here "%s"USEC_FMT".%0*"PRI_USEC"%s", ../git/src/basic/time-util.c:508:46: note: directive argument in the range [0, 18446744073709551614] "%s"USEC_FMT".%0*"PRI_USEC"%s", ^~~~ ../git/src/basic/time-util.c:507:37: note: 'snprintf' output 4 or more bytes (assuming 2147483651) into a destination of size 4294967295 k = snprintf(p, l, ^~~~~~~~~~~~~~ "%s"USEC_FMT".%0*"PRI_USEC"%s", ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ p > buf ? " " : "", ~~~~~~~~~~~~~~~~~~~ a, ~~ j, ~~ b, ~~ table[i].suffix); ~~~~~~~~~~~~~~~~ cc1: some warnings being treated as errors [zj: change 'char' to 'signed char']
* Always allow timestamps to be printedZbigniew Jędrzejewski-Szmek2018-08-24
| | | | | | | | | | If the timestamp is above 9999-12-30, (or 2038-something-something on 32 bit), use XXXX-XX-XX XX:XX:XX as the replacement. The problem with refusing to print timestamps is that our code accepts such timestamps, so we can't really just refuse to process them afterwards. Also, it makes journal files non-portable, because suddently we might completely refuse to print entries which are totally OK on a different machine.
* basic: add log_level argument to timezone_is_validMike Gilbert2018-08-24
|
* basic: timezone_is_valid: check for magic bytes "TZif"Mike Gilbert2018-08-24
| | | | Fixes: https://github.com/systemd/systemd/issues/8905
* time-util: fix indentation for commentsYu Watanabe2018-08-24
|
* tree-wide: be more careful with the type of array sizesLennart Poettering2018-08-24
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Previously we were a bit sloppy with the index and size types of arrays, we'd regularly use unsigned. While I don't think this ever resulted in real issues I think we should be more careful there and follow a stricter regime: unless there's a strong reason not to use size_t for array sizes and indexes, size_t it should be. Any allocations we do ultimately will use size_t anyway, and converting forth and back between unsigned and size_t will always be a source of problems. Note that on 32bit machines "unsigned" and "size_t" are equivalent, and on 64bit machines our arrays shouldn't grow that large anyway, and if they do we have a problem, however that kind of overly large allocation we have protections for usually, but for overflows we do not have that so much, hence let's add it. So yeah, it's a story of the current code being already "good enough", but I think some extra type hygiene is better. This patch tries to be comprehensive, but it probably isn't and I missed a few cases. But I guess we can cover that later as we notice it. Among smaller fixes, this changes: 1. strv_length()' return type becomes size_t 2. the unit file changes array size becomes size_t 3. DNS answer and query array sizes become size_t Fixes: https://bugs.freedesktop.org/show_bug.cgi?id=76745
* tree-wide: drop license boilerplateZbigniew Jędrzejewski-Szmek2018-08-24
| | | | | | | | | | Files which are installed as-is (any .service and other unit files, .conf files, .policy files, etc), are left as is. My assumption is that SPDX identifiers are not yet that well known, so it's better to retain the extended header to avoid any doubt. I also kept any copyright lines. We can probably remove them, but it'd nice to obtain explicit acks from all involved authors before doing that.
* tree-wide: use TAKE_PTR() and TAKE_FD() macrosYu Watanabe2018-08-24
|
* log: minimize includes in log.hLennart Poettering2018-05-30
| | | | | | | | | | | | | | | | | | | | | | | | | | 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-05-30
| | | | | | | | | 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 Poettering2018-05-30
| | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* Prep v236 : Add missing SPDX-License-Identifier (2/9) src/basicSven Eden2018-03-26
|
* tree-wide: drop a few == NULL and != NULL comparisonLennart Poettering2017-12-08
| | | | | | | 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-19
| | | | | | | | Distcc removes comments, making the comment silencing not work. I know there was a decision against a macro in commit ec251fe7d5bc24b5d38b0853bc5969f3a0ba06e2
* time-util: Disable explicit fallthrough wanring on gcc-7+Sven Eden2017-12-12
|
* unit: when JobTimeoutSec= is turned off, implicitly turn off ↵Lennart Poettering2017-12-08
| | | | | | | | | | | | | 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 elogind versions. Fixes: #6402
* tree-wide: use IN_SET macro (#6977)Yu Watanabe2017-12-08
|
* Apply updates from upstreamSven Eden2017-12-07
|
* build-sys: use #if Y instead of #ifdef Y everywhereZbigniew Jędrzejewski-Szmek2017-11-23
| | | | | | | | | | | | | | | The advantage is that is the name is mispellt, cpp will warn us. $ git grep -Ee "conf.set\('(HAVE|ENABLE)_" -l|xargs sed -r -i "s/conf.set\('(HAVE|ENABLE)_/conf.set10('\1_/" $ git grep -Ee '#ifn?def (HAVE|ENABLE)' -l|xargs sed -r -i 's/#ifdef (HAVE|ENABLE)/#if \1/; s/#ifndef (HAVE|ENABLE)/#if ! \1/;' $ git grep -Ee 'if.*defined\(HAVE' -l|xargs sed -i -r 's/defined\((HAVE_[A-Z0-9_]*)\)/\1/g' $ git grep -Ee 'if.*defined\(ENABLE' -l|xargs sed -i -r 's/defined\((ENABLE_[A-Z0-9_]*)\)/\1/g' + manual changes to meson.build squash! build-sys: use #if Y instead of #ifdef Y everywhere v2: - fix incorrect setting of HAVE_LIBIDN2
* Add abstraction model for BPF programsDaniel Mack2017-11-20
| | | | | This object takes a number of bpf_insn members and wraps them together with the in-kernel reference id. Will be needed by the firewall code.
* v235: Added missing updatesSven Eden2017-11-19
|
* time-util: mktime_or_timegm are changing the struct tmMarcel Hollerbach2017-09-20
| | | | | 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-20
| | | | | | | | | | 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-19
| | | | | timezone was shadowing timezone from time.h which leads to a buildbreak since elogind is built with -Werror
* Fix for dst/non-dst timezonesIvan Kurnosov2017-09-17
| | | | | | | | | 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-17
| | | | Just to reduce the indentation a bit.
* Added timezone to the CalendarSpec, parser/formatter and the timedatectlIvan Kurnosov2017-09-25
|
* time-util: make parse_timestamp() return -EINVAL if the input is very old ↵Yu Watanabe2017-07-25
| | | | | | | | | 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-25
| | | | | | | | | | 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-25
| | | | | | | | | | | | | 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-07-25
| | | | 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-07-25
| | | | | 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-07-25
| | | | | | | | | | | *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-07-17
|
* time-util: add overflow checking to monotonic timestamp specificationsLennart Poettering2017-07-17
|
* time-util: when formatting usec_t as raw integers use PRIu64Lennart Poettering2017-07-17
| | | | 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-07-17
| | | | | | 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-07-17
| | | | | | | | | | | 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-07-17
| | | | | | | | | | 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-07-17
| | | | | | | | | | | | | | | 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 Poettering2017-07-17
| | | | | | | | | 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-Szmek2017-07-17
|
* time-util: export timespec_load_nsec()Ivan Shapovalov2017-07-05
|
* util-lib: make timestamp generation and parsing reversible (#3869)Lennart Poettering2017-07-05
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 elogind prints wouldn't be parsable by elogind. 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.
* Prep v231: Apply missing fixes from upstream (1/6) src/basicSven Eden2017-06-16
|
* Prep v230: Apply missing upstream fixes and updates (2/8) src/basic.Sven Eden2017-06-16
|