]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
Fixed Avahi API misuse in the ippfind
authorAlexander Pevzner <pzz@apevzner.com>
Sat, 5 Apr 2025 12:28:17 +0000 (15:28 +0300)
committerAlexander Pevzner <pzz@apevzner.com>
Sat, 5 Apr 2025 12:45:34 +0000 (15:45 +0300)
  - avahi_simple_poll_set_func and poll_callback removed
  - avahi_simple_poll_iterate timeout reduced to 100 ms

ippfind previously used avahi_simple_poll_set_func() to replace the
system-provided poll() function with a custom poll_callback() that
enforced a fixed 100 ms timeout.

The original code comment claimed that Avahi always calls this callback
with a zero timeout (referencing Avahi #127). However, after
instrumenting poll_callback() with printf to log the actual timeout
values, I observed that Avahi sometimes passes zero and sometimes passes
the timeout provided to avahi_simple_poll_iterate(). Despite this, there
was no CPU overuse—as would be expected if a zero timeout were always
used.

In reality, there is no bug in Avahi.

The timeout passed to the poll callback represents the time remaining
until Avahi's next scheduled time-based event. Avahi may set internal
timers with short or even zero timeouts, causing poll() to be called
with a timeout shorter than the one passed to
avahi_simple_poll_iterate(). This is normal and expected behavior.

Additionally, the Avahi API reference states that
avahi_simple_poll_iterate() blocks for AT MOST the specified timeout. In
practice, it blocks until the nearest event occurs, but never longer
than the given timeout. We cannot assume that this function will always
delay execution for the full duration of its argument.

To improve precision, I also reduced the avahi_simple_poll_iterate()
timeout from 500 ms to 100 ms. This change only affects how accurately
ippfind measures search time when the search ends due to deadline
expiration.

With these adjustments, ippfind maintains the same behavior as before
but no longer misuses the Avahi API.

tools/ippfind.c

index a7b97604afbd6c7fd4b081283da01d3548c5e9ea..8963557182b06945e1a8dd3776f57b373c4b1fac 100644 (file)
@@ -173,9 +173,6 @@ static ippfind_expr_t       *new_expr(ippfind_op_t op, int invert,
 #ifdef HAVE_MDNSRESPONDER
 static void DNSSD_API  resolve_callback(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode, const char *fullName, const char *hostTarget, uint16_t port, uint16_t txtLen, const unsigned char *txtRecord, void *context) _CUPS_NONNULL(1,5,6,9, 10);
 #elif defined(HAVE_AVAHI)
-static int             poll_callback(struct pollfd *pollfds,
-                                     unsigned int num_pollfds, int timeout,
-                                     void *context);
 static void            resolve_callback(AvahiServiceResolver *res,
                                         AvahiIfIndex interface,
                                         AvahiProtocol protocol,
@@ -1141,8 +1138,6 @@ main(int  argc,                           /* I - Number of command-line args */
     return (IPPFIND_EXIT_BONJOUR);
   }
 
-  avahi_simple_poll_set_func(avahi_poll, poll_callback, NULL);
-
   avahi_client = avahi_client_new(avahi_simple_poll_get(avahi_poll),
                                  0, client_callback, avahi_poll, &err);
   if (!avahi_client)
@@ -1332,7 +1327,7 @@ main(int  argc,                           /* I - Number of command-line args */
     }
 
 #elif defined(HAVE_AVAHI)
-    if (avahi_simple_poll_iterate(avahi_poll, 500) > 0)
+    if (avahi_simple_poll_iterate(avahi_poll, 100) > 0)
     {
      /*
       * We've been told to exit the loop.  Perhaps the connection to
@@ -2463,31 +2458,6 @@ new_expr(ippfind_op_t op,                /* I - Operation */
   return (temp);
 }
 
-
-#ifdef HAVE_AVAHI
-/*
- * 'poll_callback()' - Wait for input on the specified file descriptors.
- *
- * Note: This function is needed because avahi_simple_poll_iterate is broken
- *       and always uses a timeout of 0 (!) milliseconds.
- *       (Avahi Github issue #127)
- */
-
-static int                             /* O - Number of file descriptors matching */
-poll_callback(
-    struct pollfd *pollfds,            /* I - File descriptors */
-    unsigned int  num_pollfds,         /* I - Number of file descriptors */
-    int           timeout,             /* I - Timeout in milliseconds (unused) */
-    void          *context)            /* I - User data (unused) */
-{
-  (void)timeout;
-  (void)context;
-
-  return (poll(pollfds, num_pollfds, 100));
-}
-#endif /* HAVE_AVAHI */
-
-
 /*
  * 'resolve_callback()' - Process resolve data.
  */