]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/cups-deviced.c
Fix source file header text duplication text duplication.
[thirdparty/cups.git] / scheduler / cups-deviced.c
index d35940c0786c7745ebf0400760a53228fefca63b..13efa54ffdacfd51e5066fc788cc787a0bb34389 100644 (file)
@@ -1,28 +1,14 @@
 /*
- * "$Id: cups-deviced.c 7816 2008-07-30 20:53:31Z mike $"
+ * Device scanning mini-daemon for CUPS.
  *
- *   Device scanning mini-daemon for the Common UNIX Printing System (CUPS).
+ * Copyright 2007-2014 by Apple Inc.
+ * Copyright 1997-2006 by Easy Software Products.
  *
- *   Copyright 2007-2008 by Apple Inc.
- *   Copyright 1997-2006 by Easy Software Products.
- *
- *   These coded instructions, statements, and computer programs are the
- *   property of Apple Inc. and are protected by Federal copyright
- *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- *   which should have been included with this file.  If this file is
- *   file is missing or damaged, see the license at "http://www.cups.org/".
- *
- * Contents:
- *
- *   main()                 - Scan for devices and return an IPP response.
- *   add_device()           - Add a new device to the list.
- *   compare_devices()      - Compare device names to eliminate duplicates.
- *   create_strings_array() - Create a CUPS array of strings.
- *   get_current_time()     - Get the current time as a double value in seconds.
- *   get_device()           - Get a device from a backend.
- *   process_children()     - Process all dead children...
- *   sigchld_handler()      - Handle 'child' signals from old processes.
- *   start_backend()        - Run a backend to gather the available devices.
+ * These coded instructions, statements, and computer programs are the
+ * property of Apple Inc. and are protected by Federal copyright
+ * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ * which should have been included with this file.  If this file is
+ * missing or damaged, see the license at "http://www.cups.org/".
  */
 
 /*
@@ -83,7 +69,7 @@ static cupsd_backend_t        backends[MAX_BACKENDS];
 static struct pollfd   backend_fds[MAX_BACKENDS];
                                        /* Array for poll() */
 static cups_array_t    *devices;       /* Array of devices */
-static int             normal_user;    /* Normal user ID */
+static uid_t           normal_user;    /* Normal user ID */
 static int             device_limit;   /* Maximum number of devices */
 static int             send_class,     /* Send device-class attribute? */
                        send_info,      /* Send device-info attribute? */
@@ -108,7 +94,6 @@ static int           add_device(const char *device_class,
                                   const char *device_location);
 static int             compare_devices(cupsd_device_t *p0,
                                        cupsd_device_t *p1);
-static cups_array_t    *create_strings_array(const char *s);
 static double          get_current_time(void);
 static int             get_device(cupsd_backend_t *backend);
 static void            process_children(void);
@@ -140,7 +125,8 @@ main(int  argc,                             /* I - Number of command-line args */
   int          num_options;            /* Number of options */
   cups_option_t        *options;               /* Options */
   cups_array_t *requested,             /* requested-attributes values */
-               *exclude;               /* exclude-schemes values */
+               *exclude,               /* exclude-schemes values */
+               *include;               /* include-schemes values */
 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
   struct sigaction action;             /* Actions for POSIX signals */
 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
@@ -183,7 +169,7 @@ main(int  argc,                             /* I - Number of command-line args */
     return (1);
   }
 
-  normal_user = atoi(argv[4]);
+  normal_user = (uid_t)atoi(argv[4]);
   if (normal_user <= 0)
   {
     fprintf(stderr, "ERROR: [cups-deviced] Bad user %d!\n", normal_user);
@@ -192,10 +178,12 @@ main(int  argc,                           /* I - Number of command-line args */
   }
 
   num_options = cupsParseOptions(argv[5], 0, &options);
-  requested   = create_strings_array(cupsGetOption("requested-attributes",
-                                                   num_options, options));
-  exclude     = create_strings_array(cupsGetOption("exclude-schemes",
-                                                   num_options, options));
+  requested   = cupsdCreateStringsArray(cupsGetOption("requested-attributes",
+                                                      num_options, options));
+  exclude     = cupsdCreateStringsArray(cupsGetOption("exclude-schemes",
+                                                      num_options, options));
+  include     = cupsdCreateStringsArray(cupsGetOption("include-schemes",
+                                                      num_options, options));
 
   if (!requested || cupsArrayFind(requested, "all") != NULL)
   {
@@ -267,7 +255,12 @@ main(int  argc,                            /* I - Number of command-line args */
         (dent->fileinfo.st_mode & (S_IRUSR | S_IXUSR)) != (S_IRUSR | S_IXUSR))
       continue;
 
-    if (cupsArrayFind(exclude, dent->filename))
+   /*
+    * Skip excluded or not included backends...
+    */
+
+    if (cupsArrayFind(exclude, dent->filename) ||
+        (include && !cupsArrayFind(include, dent->filename)))
       continue;
 
    /*
@@ -275,8 +268,7 @@ main(int  argc,                             /* I - Number of command-line args */
     * all others run as the unprivileged user...
     */
 
-    start_backend(dent->filename,
-                  !(dent->fileinfo.st_mode & (S_IRWXG | S_IRWXO)));
+    start_backend(dent->filename, !(dent->fileinfo.st_mode & (S_IWGRP | S_IRWXO)));
   }
 
   cupsDirClose(dir);
@@ -303,15 +295,25 @@ main(int  argc,                           /* I - Number of command-line args */
 
     timeout = (int)(1000 * (end_time - current_time));
 
-    if (poll(backend_fds, num_backends, timeout) > 0)
+    if (poll(backend_fds, (nfds_t)num_backends, timeout) > 0)
     {
       for (i = 0; i < num_backends; i ++)
         if (backend_fds[i].revents && backends[i].pipe)
-         if (get_device(backends + i))
+       {
+         cups_file_t *bpipe = backends[i].pipe;
+                                       /* Copy of pipe for backend... */
+
+         do
          {
-           backend_fds[i].fd     = 0;
-           backend_fds[i].events = 0;
+           if (get_device(backends + i))
+           {
+             backend_fds[i].fd     = 0;
+             backend_fds[i].events = 0;
+             break;
+           }
          }
+         while (bpipe->ptr && memchr(bpipe->ptr, '\n', (size_t)(bpipe->end - bpipe->ptr)));
+        }
     }
 
    /*
@@ -440,53 +442,10 @@ compare_devices(cupsd_device_t *d0,       /* I - First device */
 
   if ((diff = cupsdCompareNames(d0->device_info, d1->device_info)) != 0)
     return (diff);
-  else if ((diff = strcasecmp(d0->device_class, d1->device_class)) != 0)
+  else if ((diff = _cups_strcasecmp(d0->device_class, d1->device_class)) != 0)
     return (diff);
   else
-    return (strcasecmp(d0->device_uri, d1->device_uri));
-}
-
-
-/*
- * 'create_strings_array()' - Create a CUPS array of strings.
- */
-
-static cups_array_t *                  /* O - CUPS array */
-create_strings_array(const char *s)    /* I - Comma-delimited strings */
-{
-  cups_array_t *a;                     /* CUPS array */
-  const char   *start,                 /* Start of string */
-               *end;                   /* End of string */
-  char         *ptr;                   /* New string */
-
-
-  if (!s)
-    return (NULL);
-
-  if ((a = cupsArrayNew((cups_array_func_t)strcmp, NULL)) != NULL)
-  {
-    for (start = end = s; *end; start = end + 1)
-    {
-     /*
-      * Find the end of the current delimited string...
-      */
-
-      if ((end = strchr(start, ',')) == NULL)
-        end = start + strlen(start);
-
-     /*
-      * Duplicate the string and add it to the array...
-      */
-
-      if ((ptr = calloc(1, end - start + 1)) == NULL)
-        break;
-
-      memcpy(ptr, start, end - start);
-      cupsArrayAdd(a, ptr);
-    }
-  }
-
-  return (a);
+    return (_cups_strcasecmp(d0->device_uri, d1->device_uri));
 }
 
 
@@ -793,6 +752,10 @@ start_backend(const char *name,            /* I - Backend to run */
 
   snprintf(program, sizeof(program), "%s/backend/%s", server_bin, name);
 
+  if (_cupsFileCheck(program, _CUPS_FILE_CHECK_PROGRAM, !geteuid(),
+                     _cupsFileCheckFilter, NULL))
+    return (-1);
+
   backend = backends + num_backends;
 
   argv[0] = (char *)name;
@@ -825,8 +788,3 @@ start_backend(const char *name,             /* I - Backend to run */
 
   return (0);
 }
-
-
-/*
- * End of "$Id: cups-deviced.c 7816 2008-07-30 20:53:31Z mike $".
- */