Skip to content

Commit

Permalink
dest.c: Use monotonic time for cups_enum_dest
Browse files Browse the repository at this point in the history
Patch written by Adam Williamson.

`gettimeofday()` is vulnerable to clock jumps, so it is better to use
`clock_gettime()` to avoid issues with libcups if system changes clock,
f.e. in Live CDs.
  • Loading branch information
zdohnal committed Oct 21, 2024
1 parent 3fe0aa4 commit 7c98b0c
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions cups/dest.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ static const char *cups_dnssd_resolve(cups_dest_t *dest, const char *uri,
static int cups_dnssd_resolve_cb(void *context);
static void cups_dnssd_unquote(char *dst, const char *src,
size_t dstsize);
static int cups_elapsed(struct timeval *t);
static int cups_elapsed(struct timespec *t);
#endif /* HAVE_DNSSD */
static int cups_enum_dests(http_t *http, unsigned flags, int msec, int *cancel, cups_ptype_t type, cups_ptype_t mask, cups_dest_cb_t cb, void *user_data);
static int cups_find_dest(const char *name, const char *instance,
Expand Down Expand Up @@ -3404,15 +3404,14 @@ cups_dnssd_unquote(char *dst, /* I - Destination buffer */
*/

static int /* O - Elapsed time in milliseconds */
cups_elapsed(struct timeval *t) /* IO - Previous time */
cups_elapsed(struct timespec *t) // IO - Previous time
{
int msecs; /* Milliseconds */
struct timeval nt; /* New time */
struct timespec nt; // New time

clock_gettime(CLOCK_MONOTONIC, &nt);

gettimeofday(&nt, NULL);

msecs = (int)(1000 * (nt.tv_sec - t->tv_sec) + (nt.tv_usec - t->tv_usec) / 1000);
msecs = (int)(1000 * (nt.tv_sec - t->tv_sec) + (nt.tv_nsec - t->tv_nsec) / 1000 / 1000);

*t = nt;

Expand Down Expand Up @@ -3446,7 +3445,7 @@ cups_enum_dests(
int count, /* Number of queries started */
completed, /* Number of completed queries */
remaining; /* Remainder of timeout */
struct timeval curtime; /* Current time */
struct timespec curtime; // Current time
_cups_dnssd_data_t data; /* Data for callback */
_cups_dnssd_device_t *device; /* Current device */
# ifdef HAVE_MDNSRESPONDER
Expand Down Expand Up @@ -3672,7 +3671,7 @@ cups_enum_dests(
* Get Bonjour-shared printers...
*/

gettimeofday(&curtime, NULL);
clock_gettime(CLOCK_MONOTONIC, &curtime);

# ifdef HAVE_MDNSRESPONDER
if (DNSServiceCreateConnection(&data.main_ref) != kDNSServiceErr_NoError)
Expand Down

0 comments on commit 7c98b0c

Please sign in to comment.