From: Yu Ning <ning.yu@ubuntu.com>
Subject: autofs-5.1.1 - change time() to use monotonic_clock()
Git-repo: git://git.kernel.org/pub/scm/linux/storage/autofs/autofs.git
Git-commit: 9f3d839396ce476009f5cc275def7451473c341c
Patch-mainline: 5.1.2
References: bsc#1046493

The time returned by gettimeofday() is affected by discontinuous jumps
in the system time, so it causes an issue that autofs may not auto
unmount a mount point if system time is manually changed by the system
administrator.

To fix the issue we need to convert to using a monotonic clock source
instead of the clock source used by gettimeofday().

Finally hange the time() function calls to monotonic_clock() calls.

Signed-off-by: Yu Ning <ning.yu@ubuntu.com>
Signed-off-by: Ian Kent <raven@themaw.net>
Acked-by: Jeff Mahoney <jeffm@suse.com>
---
 daemon/automount.c        |    4 ++--
 daemon/direct.c           |    4 ++--
 daemon/indirect.c         |    4 ++--
 daemon/lookup.c           |    6 +++---
 daemon/state.c            |    2 +-
 lib/cache.c               |    2 +-
 lib/master.c              |    4 ++--
 modules/dclist.c          |    2 +-
 modules/lookup_file.c     |    6 +++---
 modules/lookup_hesiod.c   |    4 ++--
 modules/lookup_hosts.c    |    4 ++--
 modules/lookup_ldap.c     |    8 ++++----
 modules/lookup_nisplus.c  |    8 ++++----
 modules/lookup_program.c  |    6 +++---
 modules/lookup_sss.c      |    6 +++---
 modules/lookup_userhome.c |    2 +-
 modules/lookup_yp.c       |    6 +++---
 modules/mount_autofs.c    |    2 +-
 modules/replicated.c      |    4 ++--
 19 files changed, 42 insertions(+), 42 deletions(-)

--- a/daemon/automount.c
+++ b/daemon/automount.c
@@ -1403,7 +1403,7 @@ static void *statemachine(void *arg)
 			break;
 
 		case SIGHUP:
-			do_hup_signal(master_list, time(NULL));
+			do_hup_signal(master_list, monotonic_time(NULL));
 			break;
 
 		default:
@@ -1947,7 +1947,7 @@ int main(int argc, char *argv[])
 	unsigned ghost, logging, daemon_check;
 	unsigned dumpmaps, foreground, have_global_options;
 	time_t timeout;
-	time_t age = time(NULL);
+	time_t age = monotonic_time(NULL);
 	struct rlimit rlim;
 	const char *options = "+hp:t:vmdD:fVrO:l:n:CF";
 	static const struct option long_options[] = {
--- a/daemon/direct.c
+++ b/daemon/direct.c
@@ -464,7 +464,7 @@ int mount_autofs_direct(struct autofs_po
 	struct mapent_cache *nc, *mc;
 	struct mapent *me, *ne, *nested;
 	struct mnt_list *mnts;
-	time_t now = time(NULL);
+	time_t now = monotonic_time(NULL);
 
 	if (strcmp(ap->path, "/-")) {
 		error(ap->logopt, "expected direct map, exiting");
@@ -1383,7 +1383,7 @@ int handle_packet_missing_direct(struct
 	}
 
 	/* Check if we recorded a mount fail for this key */
-	if (me->status >= time(NULL)) {
+	if (me->status >= monotonic_time(NULL)) {
 		ops->send_fail(ap->logopt,
 			       ioctlfd, pkt->wait_queue_token, -ENOENT);
 		ops->close(ap->logopt, ioctlfd);
--- a/daemon/indirect.c
+++ b/daemon/indirect.c
@@ -197,7 +197,7 @@ out_err:
 
 int mount_autofs_indirect(struct autofs_point *ap, const char *root)
 {
-	time_t now = time(NULL);
+	time_t now = monotonic_time(NULL);
 	int status;
 	int map;
 
@@ -815,7 +815,7 @@ int handle_packet_missing_indirect(struc
 	/* Check if we recorded a mount fail for this key anywhere */
 	me = lookup_source_mapent(ap, pkt->name, LKP_DISTINCT);
 	if (me) {
-		if (me->status >= time(NULL)) {
+		if (me->status >= monotonic_time(NULL)) {
 			ops->send_fail(ap->logopt, ap->ioctlfd,
 				       pkt->wait_queue_token, -ENOENT);
 			cache_unlock(me->mc);
--- a/daemon/lookup.c
+++ b/daemon/lookup.c
@@ -751,7 +751,7 @@ static int lookup_name_file_source_insta
 	struct map_source *instance;
 	char src_file[] = "file";
 	char src_prog[] = "program";
-	time_t age = time(NULL);
+	time_t age = monotonic_time(NULL);
 	struct stat st;
 	char *type, *format;
 
@@ -788,7 +788,7 @@ static int lookup_name_source_instance(s
 {
 	struct map_source *instance;
 	const char *format;
-	time_t age = time(NULL);
+	time_t age = monotonic_time(NULL);
 
 	format = map->format;
 
@@ -944,7 +944,7 @@ static void update_negative_cache(struct
 		else
 			map = entry->maps;
 		if (map) {
-			time_t now = time(NULL);
+			time_t now = monotonic_time(NULL);
 			int rv = CHE_FAIL;
 
 			cache_writelock(map->mc);
--- a/daemon/state.c
+++ b/daemon/state.c
@@ -553,7 +553,7 @@ static unsigned int st_readmap(struct au
 	pthread_t thid;
 	struct readmap_args *ra;
 	int status;
-	int now = time(NULL);
+	int now = monotonic_time(NULL);
 
 	debug(ap->logopt, "state %d path %s", ap->state, ap->path);
 
--- a/lib/cache.c
+++ b/lib/cache.c
@@ -749,7 +749,7 @@ void cache_update_negative(struct mapent
 			   struct map_source *ms, const char *key,
 			   time_t timeout)
 {
-	time_t now = time(NULL);
+	time_t now = monotonic_time(NULL);
 	struct mapent *me;
 	int rv = CHE_OK;
 
--- a/lib/master.c
+++ b/lib/master.c
@@ -1426,7 +1426,7 @@ int dump_map(struct master *master, cons
 		struct map_source *source;
 		struct master_mapent *this;
 		struct autofs_point *ap;
-		time_t now = time(NULL);
+		time_t now = monotonic_time(NULL);
 
 		this = list_entry(p, struct master_mapent, list);
 		p = p->next;
@@ -1544,7 +1544,7 @@ int master_show_mounts(struct master *ma
 		struct map_source *source;
 		struct master_mapent *this;
 		struct autofs_point *ap;
-		time_t now = time(NULL);
+		time_t now = monotonic_time(NULL);
 		unsigned int count = 0;
 
 		this = list_entry(p, struct master_mapent, list);
--- a/modules/dclist.c
+++ b/modules/dclist.c
@@ -568,7 +568,7 @@ struct dclist *get_dc_list(unsigned int
 	if (!list)
 		goto out_error;
 
-	dclist->expire = time(NULL) + min_ttl;
+	dclist->expire = monotonic_time(NULL) + min_ttl;
 	dclist->uri = list;
 
 	return dclist;
--- a/modules/lookup_file.c
+++ b/modules/lookup_file.c
@@ -732,7 +732,7 @@ static int lookup_one(struct autofs_poin
 	struct mapent_cache *mc;
 	char mkey[KEY_MAX_LEN + 1];
 	char mapent[MAPENT_MAX_LEN + 1];
-	time_t age = time(NULL);
+	time_t age = monotonic_time(NULL);
 	FILE *f;
 	unsigned int k_len, m_len;
 	int entry, ret;
@@ -831,7 +831,7 @@ static int lookup_wild(struct autofs_poi
 	struct mapent_cache *mc;
 	char mkey[KEY_MAX_LEN + 1];
 	char mapent[MAPENT_MAX_LEN + 1];
-	time_t age = time(NULL);
+	time_t age = monotonic_time(NULL);
 	FILE *f;
 	unsigned int k_len, m_len;
 	int entry, ret;
@@ -974,7 +974,7 @@ int lookup_mount(struct autofs_point *ap
 	/* Check if we recorded a mount fail for this key anywhere */
 	me = lookup_source_mapent(ap, key, LKP_DISTINCT);
 	if (me) {
-		if (me->status >= time(NULL)) {
+		if (me->status >= monotonic_time(NULL)) {
 			cache_unlock(me->mc);
 			return NSS_STATUS_NOTFOUND;
 		} else {
--- a/modules/lookup_hesiod.c
+++ b/modules/lookup_hesiod.c
@@ -121,7 +121,7 @@ int lookup_mount(struct autofs_point *ap
 	/* Check if we recorded a mount fail for this key anywhere */
 	me = lookup_source_mapent(ap, name, LKP_DISTINCT);
 	if (me) {
-		if (me->status >= time(NULL)) {
+		if (me->status >= monotonic_time(NULL)) {
 			cache_unlock(me->mc);
 			return NSS_STATUS_NOTFOUND;
 		} else {
@@ -180,7 +180,7 @@ int lookup_mount(struct autofs_point *ap
 	}
 
 	cache_writelock(mc);
-	rv = cache_update(mc, source, name, best_record, time(NULL));
+	rv = cache_update(mc, source, name, best_record, monotonic_time(NULL));
 	cache_unlock(mc);
 	if (rv == CHE_FAIL)
 		return NSS_STATUS_UNAVAIL;
--- a/modules/lookup_hosts.c
+++ b/modules/lookup_hosts.c
@@ -283,7 +283,7 @@ int lookup_mount(struct autofs_point *ap
 	struct mapent *me;
 	char *mapent = NULL;
 	int mapent_len;
-	time_t now = time(NULL);
+	time_t now = monotonic_time(NULL);
 	int ret;
 
 	source = ap->entry->current;
@@ -295,7 +295,7 @@ int lookup_mount(struct autofs_point *ap
 	/* Check if we recorded a mount fail for this key anywhere */
 	me = lookup_source_mapent(ap, name, LKP_DISTINCT);
 	if (me) {
-		if (me->status >= time(NULL)) {
+		if (me->status >= monotonic_time(NULL)) {
 			cache_unlock(me->mc);
 			return NSS_STATUS_NOTFOUND;
 		} else {
--- a/modules/lookup_ldap.c
+++ b/modules/lookup_ldap.c
@@ -795,7 +795,7 @@ static LDAP *do_reconnect(unsigned logop
 
 	uris_mutex_lock(ctxt);
 	if (ctxt->dclist) {
-		if (!ldap || ctxt->dclist->expire < time(NULL)) {
+		if (!ldap || ctxt->dclist->expire < monotonic_time(NULL)) {
 			free_dclist(ctxt->dclist);
 			ctxt->dclist = NULL;
 		}
@@ -2526,7 +2526,7 @@ static int lookup_one(struct autofs_poin
 	struct mapent_cache *mc;
 	int rv, i, l, ql, count;
 	char buf[MAX_ERR_BUF];
-	time_t age = time(NULL);
+	time_t age = monotonic_time(NULL);
 	char *query;
 	LDAPMessage *result = NULL, *e;
 	char *class, *info, *entry;
@@ -2881,7 +2881,7 @@ static int check_map_indirect(struct aut
 {
 	struct mapent_cache *mc;
 	struct mapent *me;
-	time_t now = time(NULL);
+	time_t now = monotonic_time(NULL);
 	time_t t_last_read;
 	int ret, cur_state;
 
@@ -2968,7 +2968,7 @@ int lookup_mount(struct autofs_point *ap
 	/* Check if we recorded a mount fail for this key anywhere */
 	me = lookup_source_mapent(ap, key, LKP_DISTINCT);
 	if (me) {
-		if (me->status >= time(NULL)) {
+		if (me->status >= monotonic_time(NULL)) {
 			cache_unlock(me->mc);
 			return NSS_STATUS_NOTFOUND;
 		} else {
--- a/modules/lookup_nisplus.c
+++ b/modules/lookup_nisplus.c
@@ -276,7 +276,7 @@ static int lookup_one(struct autofs_poin
 	nis_result *result;
 	nis_object *this;
 	char *mapent;
-	time_t age = time(NULL);
+	time_t age = monotonic_time(NULL);
 	int ret, cur_state;
 	char buf[MAX_ERR_BUF];
 
@@ -330,7 +330,7 @@ static int lookup_wild(struct autofs_poi
 	nis_result *result;
 	nis_object *this;
 	char *mapent;
-	time_t age = time(NULL);
+	time_t age = monotonic_time(NULL);
 	int ret, cur_state;
 	char buf[MAX_ERR_BUF];
 
@@ -381,7 +381,7 @@ static int check_map_indirect(struct aut
 {
 	struct mapent_cache *mc;
 	struct mapent *me, *exists;
-	time_t now = time(NULL);
+	time_t now = monotonic_time(NULL);
 	time_t t_last_read;
 	int ret = 0;
 
@@ -495,7 +495,7 @@ int lookup_mount(struct autofs_point *ap
 	/* Check if we recorded a mount fail for this key anywhere */
 	me = lookup_source_mapent(ap, key, LKP_DISTINCT);
 	if (me) {
-		if (me->status >= time(NULL)) {
+		if (me->status >= monotonic_time(NULL)) {
 			cache_unlock(me->mc);
 			return NSS_STATUS_NOTFOUND;
 		} else {
--- a/modules/lookup_program.c
+++ b/modules/lookup_program.c
@@ -143,7 +143,7 @@ int lookup_mount(struct autofs_point *ap
 	/* Check if we recorded a mount fail for this key anywhere */
 	me = lookup_source_mapent(ap, name, LKP_DISTINCT);
 	if (me) {
-		if (me->status >= time(NULL)) {
+		if (me->status >= monotonic_time(NULL)) {
 			cache_unlock(me->mc);
 			return NSS_STATUS_NOTFOUND;
 		} else {
@@ -192,7 +192,7 @@ int lookup_mount(struct autofs_point *ap
 		 * proceed with the program map lookup.
 		 */
 		if (strchr(name, '/') ||
-		    me->age + ap->negative_timeout > time(NULL)) {
+		    me->age + ap->negative_timeout > monotonic_time(NULL)) {
 			char *ent = NULL;
 
 			if (me->mapent) {
@@ -441,7 +441,7 @@ next:
 	}
 
 	cache_writelock(mc);
-	ret = cache_update(mc, source, name, mapent, time(NULL));
+	ret = cache_update(mc, source, name, mapent, monotonic_time(NULL));
 	cache_unlock(mc);
 	if (ret == CHE_FAIL) {
 		free(mapent);
--- a/modules/lookup_sss.c
+++ b/modules/lookup_sss.c
@@ -374,7 +374,7 @@ static int lookup_one(struct autofs_poin
 	struct mapent_cache *mc;
 	struct mapent *we;
 	void *sss_ctxt = NULL;
-	time_t age = time(NULL);
+	time_t age = monotonic_time(NULL);
 	char buf[MAX_ERR_BUF];
 	char *value = NULL;
 	char *s_key;
@@ -491,7 +491,7 @@ static int check_map_indirect(struct aut
 	struct map_source *source;
 	struct mapent_cache *mc;
 	struct mapent *me;
-	time_t now = time(NULL);
+	time_t now = monotonic_time(NULL);
 	time_t t_last_read;
 	int ret, cur_state;
 
@@ -585,7 +585,7 @@ int lookup_mount(struct autofs_point *ap
 	/* Check if we recorded a mount fail for this key anywhere */
 	me = lookup_source_mapent(ap, key, LKP_DISTINCT);
 	if (me) {
-		if (me->status >= time(NULL)) {
+		if (me->status >= monotonic_time(NULL)) {
 			cache_unlock(me->mc);
 			return NSS_STATUS_NOTFOUND;
 		} else {
--- a/modules/lookup_userhome.c
+++ b/modules/lookup_userhome.c
@@ -77,7 +77,7 @@ int lookup_mount(struct autofs_point *ap
 	}
 
 	cache_writelock(mc);
-	ret = cache_update(mc, source, name, NULL, time(NULL));
+	ret = cache_update(mc, source, name, NULL, monotonic_time(NULL));
 	cache_unlock(mc);
 
 	if (ret == CHE_FAIL) {
--- a/modules/lookup_yp.c
+++ b/modules/lookup_yp.c
@@ -384,7 +384,7 @@ static int lookup_one(struct autofs_poin
 	char *mapname;
 	char *mapent;
 	int mapent_len;
-	time_t age = time(NULL);
+	time_t age = monotonic_time(NULL);
 	int ret;
 
 	mc = source->mc;
@@ -438,7 +438,7 @@ static int lookup_wild(struct autofs_poi
 	char *mapname;
 	char *mapent;
 	int mapent_len;
-	time_t age = time(NULL);
+	time_t age = monotonic_time(NULL);
 	int ret;
 
 	mc = source->mc;
@@ -598,7 +598,7 @@ int lookup_mount(struct autofs_point *ap
 	/* Check if we recorded a mount fail for this key anywhere */
 	me = lookup_source_mapent(ap, key, LKP_DISTINCT);
 	if (me) {
-		if (me->status >= time(NULL)) {
+		if (me->status >= monotonic_time(NULL)) {
 			cache_unlock(me->mc);
 			return NSS_STATUS_NOTFOUND;
 		} else {
--- a/modules/mount_autofs.c
+++ b/modules/mount_autofs.c
@@ -218,7 +218,7 @@ int mount_mount(struct autofs_point *ap,
 
 	source = master_add_map_source(entry,
 				       info->type, info->format,
-				       time(NULL), argc, argv);
+				       monotonic_time(NULL), argc, argv);
 	if (!source) {
 		error(ap->logopt,
 		      MODPREFIX "failed to add map source to entry");
--- a/modules/replicated.c
+++ b/modules/replicated.c
@@ -97,14 +97,14 @@ void seed_random(void)
 
 	fd = open_fd("/dev/urandom", O_RDONLY);
 	if (fd < 0) {
-		srandom(time(NULL));
+		srandom(monotonic_time(NULL));
 		return;
 	}
 
 	if (read(fd, &seed, sizeof(seed)) != -1)
 		srandom(seed);
 	else
-		srandom(time(NULL));
+		srandom(monotonic_time(NULL));
 
 	close(fd);
 
