From 56e72337b01216dc7cba418f040a5cc928e5fc6f Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 1 Jun 2011 11:21:15 +0930 Subject: lib/util/time.c: timeval_current_ofs_msec Several places want "milliseconds from current time", and several were simply doing "msec * 1000" which can (and does in one place) result in a usec value over 1 a million. Using a helper to do this is safer and more readable. Signed-off-by: Rusty Russell --- lib/util/time.c | 9 +++++++++ lib/util/time.h | 5 +++++ source3/libsmb/async_smb.c | 3 +-- source3/nmbd/nmbd_processlogon.c | 3 +-- source3/rpc_client/rpc_transport_tstream.c | 6 +++--- source3/smbd/blocking.c | 3 +-- source3/smbd/smb2_lock.c | 4 +--- source4/ntvfs/posix/pvfs_lock.c | 3 +-- 8 files changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/util/time.c b/lib/util/time.c index 4843fc96972..de1553aff8a 100644 --- a/lib/util/time.c +++ b/lib/util/time.c @@ -579,6 +579,15 @@ _PUBLIC_ struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs) return timeval_add(&tv, secs, usecs); } +/** + return a timeval milliseconds into the future +*/ +_PUBLIC_ struct timeval timeval_current_ofs_msec(uint32_t msecs) +{ + struct timeval tv = timeval_current(); + return timeval_add(&tv, msecs / 1000, (msecs % 1000) * 1000); +} + /** compare two timeval structures. Return -1 if tv1 < tv2 diff --git a/lib/util/time.h b/lib/util/time.h index 3a406340f45..4e4f72f71fb 100644 --- a/lib/util/time.h +++ b/lib/util/time.h @@ -212,6 +212,11 @@ struct timeval timeval_sum(const struct timeval *tv1, */ _PUBLIC_ struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs); +/** + return a timeval milliseconds into the future +*/ +_PUBLIC_ struct timeval timeval_current_ofs_msec(uint32_t msecs); + /** compare two timeval structures. Return -1 if tv1 < tv2 diff --git a/source3/libsmb/async_smb.c b/source3/libsmb/async_smb.c index 3ed38718c1a..8fdcac47a9b 100644 --- a/source3/libsmb/async_smb.c +++ b/source3/libsmb/async_smb.c @@ -340,8 +340,7 @@ struct tevent_req *cli_smb_req_create(TALLOC_CTX *mem_ctx, state->iov_count = iov_count + 3; if (cli->timeout) { - endtime = timeval_current_ofs(cli->timeout / 1000, - (cli->timeout % 1000) * 1000); + endtime = timeval_current_ofs_msec(cli->timeout); if (!tevent_req_set_endtime(result, ev, endtime)) { tevent_req_nomem(NULL, result); } diff --git a/source3/nmbd/nmbd_processlogon.c b/source3/nmbd/nmbd_processlogon.c index 790e1c3ce1e..25cdcc69709 100644 --- a/source3/nmbd/nmbd_processlogon.c +++ b/source3/nmbd/nmbd_processlogon.c @@ -604,8 +604,7 @@ logons are not enabled.\n", inet_ntoa(p->ip) )); source_name, source_addr, lp_init_logon_delay())); - when = timeval_current_ofs(0, - lp_init_logon_delay() * 1000); + when = timeval_current_ofs_msec(lp_init_logon_delay()); p->locked = true; event_add_timed(nmbd_event_context(), NULL, diff --git a/source3/rpc_client/rpc_transport_tstream.c b/source3/rpc_client/rpc_transport_tstream.c index e62ab4b2bb3..488c093d3b7 100644 --- a/source3/rpc_client/rpc_transport_tstream.c +++ b/source3/rpc_client/rpc_transport_tstream.c @@ -202,7 +202,7 @@ static struct tevent_req *rpc_tstream_read_send(TALLOC_CTX *mem_ctx, return tevent_req_post(req, ev); } - endtime = timeval_current_ofs(0, transp->timeout * 1000); + endtime = timeval_current_ofs_msec(transp->timeout); if (!tevent_req_set_endtime(subreq, ev, endtime)) { goto fail; } @@ -286,7 +286,7 @@ static struct tevent_req *rpc_tstream_write_send(TALLOC_CTX *mem_ctx, goto fail; } - endtime = timeval_current_ofs(0, transp->timeout * 1000); + endtime = timeval_current_ofs_msec(transp->timeout); if (!tevent_req_set_endtime(subreq, ev, endtime)) { goto fail; } @@ -374,7 +374,7 @@ static struct tevent_req *rpc_tstream_trans_send(TALLOC_CTX *mem_ctx, state->req.iov_base = discard_const_p(void *, data); state->max_rdata_len = max_rdata_len; - endtime = timeval_current_ofs(0, transp->timeout * 1000); + endtime = timeval_current_ofs_msec(transp->timeout); subreq = tstream_writev_queue_send(state, ev, transp->stream, diff --git a/source3/smbd/blocking.c b/source3/smbd/blocking.c index ca4106b346b..fd77e3d15bd 100644 --- a/source3/smbd/blocking.c +++ b/source3/smbd/blocking.c @@ -208,8 +208,7 @@ bool push_blocking_lock_request( struct byte_range_lock *br_lck, blr->expire_time.tv_sec = 0; blr->expire_time.tv_usec = 0; /* Never expire. */ } else { - blr->expire_time = timeval_current_ofs(lock_timeout/1000, - (lock_timeout % 1000) * 1000); + blr->expire_time = timeval_current_ofs_msec(lock_timeout); } blr->lock_num = lock_num; blr->smblctx = smblctx; diff --git a/source3/smbd/smb2_lock.c b/source3/smbd/smb2_lock.c index 712558481c6..5d615e1bed5 100644 --- a/source3/smbd/smb2_lock.c +++ b/source3/smbd/smb2_lock.c @@ -628,9 +628,7 @@ bool push_blocking_lock_request_smb2( struct byte_range_lock *br_lck, blr->expire_time.tv_sec = 0; blr->expire_time.tv_usec = 0; /* Never expire. */ } else { - blr->expire_time = timeval_current_ofs( - lock_timeout/1000, - (lock_timeout % 1000) * 1000); + blr->expire_time = timeval_current_ofs_msec(lock_timeout); } blr->lock_num = lock_num; diff --git a/source4/ntvfs/posix/pvfs_lock.c b/source4/ntvfs/posix/pvfs_lock.c index 76bc73dad82..0d99860e59b 100644 --- a/source4/ntvfs/posix/pvfs_lock.c +++ b/source4/ntvfs/posix/pvfs_lock.c @@ -324,8 +324,7 @@ NTSTATUS pvfs_lock(struct ntvfs_module_context *ntvfs, pending->req = req; pending->end_time = - timeval_current_ofs(lck->lockx.in.timeout/1000, - 1000*(lck->lockx.in.timeout%1000)); + timeval_current_ofs_msec(lck->lockx.in.timeout); } if (lck->lockx.in.mode & LOCKING_ANDX_SHARED_LOCK) { -- cgit