2017-03-15 16:06:21 +05:30
|
|
|
/*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-FileCopyrightText: 2017, Chris Lamb
|
2017-03-15 16:06:21 +05:30
|
|
|
*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2017-03-15 16:06:21 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ident "$Id$"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "defines.h"
|
|
|
|
#include "prototypes.h"
|
2021-11-29 05:07:53 +05:30
|
|
|
#include "shadowlog.h"
|
2017-03-15 16:06:21 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* gettime() returns the time as the number of seconds since the Epoch
|
|
|
|
*
|
|
|
|
* Like time(), gettime() returns the time as the number of seconds since the
|
|
|
|
* Epoch, 1970-01-01 00:00:00 +0000 (UTC), except that if the SOURCE_DATE_EPOCH
|
|
|
|
* environment variable is exported it will use that instead.
|
|
|
|
*/
|
|
|
|
/*@observer@*/time_t gettime ()
|
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
char *source_date_epoch;
|
|
|
|
time_t fallback;
|
|
|
|
unsigned long long epoch;
|
2021-11-29 05:07:53 +05:30
|
|
|
FILE *shadow_logfd = log_get_logfd();
|
2017-03-15 16:06:21 +05:30
|
|
|
|
|
|
|
fallback = time (NULL);
|
2019-03-31 20:29:45 +05:30
|
|
|
source_date_epoch = shadow_getenv ("SOURCE_DATE_EPOCH");
|
2017-03-15 16:06:21 +05:30
|
|
|
|
|
|
|
if (!source_date_epoch)
|
|
|
|
return fallback;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
epoch = strtoull (source_date_epoch, &endptr, 10);
|
|
|
|
if ((errno == ERANGE && (epoch == ULLONG_MAX || epoch == 0))
|
|
|
|
|| (errno != 0 && epoch == 0)) {
|
2021-05-09 04:12:14 +05:30
|
|
|
fprintf (shadow_logfd,
|
2017-03-15 16:06:21 +05:30
|
|
|
_("Environment variable $SOURCE_DATE_EPOCH: strtoull: %s\n"),
|
|
|
|
strerror(errno));
|
|
|
|
} else if (endptr == source_date_epoch) {
|
2021-05-09 04:12:14 +05:30
|
|
|
fprintf (shadow_logfd,
|
2017-03-15 16:06:21 +05:30
|
|
|
_("Environment variable $SOURCE_DATE_EPOCH: No digits were found: %s\n"),
|
|
|
|
endptr);
|
|
|
|
} else if (*endptr != '\0') {
|
2021-05-09 04:12:14 +05:30
|
|
|
fprintf (shadow_logfd,
|
2017-03-15 16:06:21 +05:30
|
|
|
_("Environment variable $SOURCE_DATE_EPOCH: Trailing garbage: %s\n"),
|
|
|
|
endptr);
|
|
|
|
} else if (epoch > ULONG_MAX) {
|
2021-05-09 04:12:14 +05:30
|
|
|
fprintf (shadow_logfd,
|
2017-03-15 16:06:21 +05:30
|
|
|
_("Environment variable $SOURCE_DATE_EPOCH: value must be smaller than or equal to %lu but was found to be: %llu\n"),
|
|
|
|
ULONG_MAX, epoch);
|
2023-01-24 20:35:20 +05:30
|
|
|
} else if ((time_t)epoch > fallback) {
|
2021-05-09 04:12:14 +05:30
|
|
|
fprintf (shadow_logfd,
|
2017-03-15 16:06:21 +05:30
|
|
|
_("Environment variable $SOURCE_DATE_EPOCH: value must be smaller than or equal to the current time (%lu) but was found to be: %llu\n"),
|
|
|
|
fallback, epoch);
|
|
|
|
} else {
|
|
|
|
/* Valid */
|
|
|
|
return (time_t)epoch;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fallback;
|
|
|
|
}
|