]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/cups-deviced.c
Merge changes from CUPS 1.4svn-r7961.
[thirdparty/cups.git] / scheduler / cups-deviced.c
1 /*
2 * "$Id: cups-deviced.c 7816 2008-07-30 20:53:31Z mike $"
3 *
4 * Device scanning mini-daemon for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * Contents:
16 *
17 * main() - Scan for devices and return an IPP response.
18 * add_device() - Add a new device to the list.
19 * compare_devices() - Compare device names to eliminate duplicates.
20 * create_strings_array() - Create a CUPS array of strings.
21 * get_current_time() - Get the current time as a double value in seconds.
22 * get_device() - Get a device from a backend.
23 * process_children() - Process all dead children...
24 * sigchld_handler() - Handle 'child' signals from old processes.
25 * start_backend() - Run a backend to gather the available devices.
26 */
27
28 /*
29 * Include necessary headers...
30 */
31
32 #include "util.h"
33 #include <cups/array.h>
34 #include <cups/dir.h>
35 #include <fcntl.h>
36 #include <sys/wait.h>
37 #include <poll.h>
38
39
40 /*
41 * Constants...
42 */
43
44 #define MAX_BACKENDS 200 /* Maximum number of backends we'll run */
45
46
47 /*
48 * Backend information...
49 */
50
51 typedef struct
52 {
53 char *name; /* Name of backend */
54 int pid, /* Process ID */
55 status; /* Exit status */
56 cups_file_t *pipe; /* Pipe from backend stdout */
57 int count; /* Number of devices found */
58 } cupsd_backend_t;
59
60
61 /*
62 * Device information structure...
63 */
64
65 typedef struct
66 {
67 char device_class[128], /* Device class */
68 device_info[128], /* Device info/description */
69 device_uri[1024]; /* Device URI */
70 } cupsd_device_t;
71
72
73 /*
74 * Local globals...
75 */
76
77 static int num_backends = 0,
78 /* Total backends */
79 active_backends = 0;
80 /* Active backends */
81 static cupsd_backend_t backends[MAX_BACKENDS];
82 /* Array of backends */
83 static struct pollfd backend_fds[MAX_BACKENDS];
84 /* Array for poll() */
85 static cups_array_t *devices; /* Array of devices */
86 static int normal_user; /* Normal user ID */
87 static int device_limit; /* Maximum number of devices */
88 static int send_class, /* Send device-class attribute? */
89 send_info, /* Send device-info attribute? */
90 send_make_and_model,
91 /* Send device-make-and-model attribute? */
92 send_uri, /* Send device-uri attribute? */
93 send_id, /* Send device-id attribute? */
94 send_location; /* Send device-location attribute? */
95 static int dead_children = 0;
96 /* Dead children? */
97
98
99 /*
100 * Local functions...
101 */
102
103 static int add_device(const char *device_class,
104 const char *device_make_and_model,
105 const char *device_info,
106 const char *device_uri,
107 const char *device_id,
108 const char *device_location);
109 static int compare_devices(cupsd_device_t *p0,
110 cupsd_device_t *p1);
111 static cups_array_t *create_strings_array(const char *s);
112 static double get_current_time(void);
113 static int get_device(cupsd_backend_t *backend);
114 static void process_children(void);
115 static void sigchld_handler(int sig);
116 static int start_backend(const char *backend, int root);
117
118
119 /*
120 * 'main()' - Scan for devices and return an IPP response.
121 *
122 * Usage:
123 *
124 * cups-deviced request_id limit options
125 */
126
127 int /* O - Exit code */
128 main(int argc, /* I - Number of command-line args */
129 char *argv[]) /* I - Command-line arguments */
130 {
131 int i; /* Looping var */
132 int request_id; /* Request ID */
133 int timeout; /* Timeout in seconds */
134 const char *server_bin; /* CUPS_SERVERBIN environment variable */
135 char filename[1024]; /* Backend directory filename */
136 cups_dir_t *dir; /* Directory pointer */
137 cups_dentry_t *dent; /* Directory entry */
138 double current_time, /* Current time */
139 end_time; /* Ending time */
140 int num_options; /* Number of options */
141 cups_option_t *options; /* Options */
142 cups_array_t *requested, /* requested-attributes values */
143 *exclude; /* exclude-schemes values */
144 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
145 struct sigaction action; /* Actions for POSIX signals */
146 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
147
148
149 setbuf(stderr, NULL);
150
151 /*
152 * Check the command-line...
153 */
154
155 if (argc != 6)
156 {
157 fputs("Usage: cups-deviced request-id limit timeout user-id options\n", stderr);
158
159 return (1);
160 }
161
162 request_id = atoi(argv[1]);
163 if (request_id < 1)
164 {
165 fprintf(stderr, "ERROR: [cups-deviced] Bad request ID %d!\n", request_id);
166
167 return (1);
168 }
169
170 device_limit = atoi(argv[2]);
171 if (device_limit < 0)
172 {
173 fprintf(stderr, "ERROR: [cups-deviced] Bad limit %d!\n", device_limit);
174
175 return (1);
176 }
177
178 timeout = atoi(argv[3]);
179 if (timeout < 1)
180 {
181 fprintf(stderr, "ERROR: [cups-deviced] Bad timeout %d!\n", timeout);
182
183 return (1);
184 }
185
186 normal_user = atoi(argv[4]);
187 if (normal_user <= 0)
188 {
189 fprintf(stderr, "ERROR: [cups-deviced] Bad user %d!\n", normal_user);
190
191 return (1);
192 }
193
194 num_options = cupsParseOptions(argv[5], 0, &options);
195 requested = create_strings_array(cupsGetOption("requested-attributes",
196 num_options, options));
197 exclude = create_strings_array(cupsGetOption("exclude-schemes",
198 num_options, options));
199
200 if (!requested || cupsArrayFind(requested, "all") != NULL)
201 {
202 send_class = send_info = send_make_and_model = send_uri = send_id =
203 send_location = 1;
204 }
205 else
206 {
207 send_class = cupsArrayFind(requested, "device-class") != NULL;
208 send_info = cupsArrayFind(requested, "device-info") != NULL;
209 send_make_and_model = cupsArrayFind(requested, "device-make-and-model") != NULL;
210 send_uri = cupsArrayFind(requested, "device-uri") != NULL;
211 send_id = cupsArrayFind(requested, "device-id") != NULL;
212 send_location = cupsArrayFind(requested, "device-location") != NULL;
213 }
214
215 /*
216 * Listen to child signals...
217 */
218
219 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
220 sigset(SIGCHLD, sigchld_handler);
221 #elif defined(HAVE_SIGACTION)
222 memset(&action, 0, sizeof(action));
223
224 sigemptyset(&action.sa_mask);
225 sigaddset(&action.sa_mask, SIGCHLD);
226 action.sa_handler = sigchld_handler;
227 sigaction(SIGCHLD, &action, NULL);
228 #else
229 signal(SIGCLD, sigchld_handler); /* No, SIGCLD isn't a typo... */
230 #endif /* HAVE_SIGSET */
231
232 /*
233 * Try opening the backend directory...
234 */
235
236 if ((server_bin = getenv("CUPS_SERVERBIN")) == NULL)
237 server_bin = CUPS_SERVERBIN;
238
239 snprintf(filename, sizeof(filename), "%s/backend", server_bin);
240
241 if ((dir = cupsDirOpen(filename)) == NULL)
242 {
243 fprintf(stderr, "ERROR: [cups-deviced] Unable to open backend directory "
244 "\"%s\": %s", filename, strerror(errno));
245
246 return (1);
247 }
248
249 /*
250 * Setup the devices array...
251 */
252
253 devices = cupsArrayNew((cups_array_func_t)compare_devices, NULL);
254
255 /*
256 * Loop through all of the device backends...
257 */
258
259 while ((dent = cupsDirRead(dir)) != NULL)
260 {
261 /*
262 * Skip entries that are not executable files...
263 */
264
265 if (!S_ISREG(dent->fileinfo.st_mode) ||
266 !isalnum(dent->filename[0] & 255) ||
267 (dent->fileinfo.st_mode & (S_IRUSR | S_IXUSR)) != (S_IRUSR | S_IXUSR))
268 continue;
269
270 if (cupsArrayFind(exclude, dent->filename))
271 continue;
272
273 /*
274 * Backends without permissions for normal users run as root,
275 * all others run as the unprivileged user...
276 */
277
278 start_backend(dent->filename,
279 !(dent->fileinfo.st_mode & (S_IRWXG | S_IRWXO)));
280 }
281
282 cupsDirClose(dir);
283
284 /*
285 * Collect devices...
286 */
287
288 if (getenv("SOFTWARE"))
289 puts("Content-Type: application/ipp\n");
290
291 cupsdSendIPPHeader(IPP_OK, request_id);
292 cupsdSendIPPGroup(IPP_TAG_OPERATION);
293 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
294 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", "en-US");
295
296 end_time = get_current_time() + timeout;
297
298 while (active_backends > 0 && (current_time = get_current_time()) < end_time)
299 {
300 /*
301 * Collect the output from the backends...
302 */
303
304 timeout = (int)(1000 * (end_time - current_time));
305
306 if (poll(backend_fds, num_backends, timeout) > 0)
307 {
308 for (i = 0; i < num_backends; i ++)
309 if (backend_fds[i].revents && backends[i].pipe)
310 if (get_device(backends + i))
311 {
312 backend_fds[i].fd = 0;
313 backend_fds[i].events = 0;
314 }
315 }
316
317 /*
318 * Get exit status from children...
319 */
320
321 if (dead_children)
322 process_children();
323 }
324
325 cupsdSendIPPTrailer();
326
327 /*
328 * Terminate any remaining backends and exit...
329 */
330
331 if (active_backends > 0)
332 {
333 for (i = 0; i < num_backends; i ++)
334 if (backends[i].pid)
335 kill(backends[i].pid, SIGTERM);
336 }
337
338 return (0);
339 }
340
341
342 /*
343 * 'add_device()' - Add a new device to the list.
344 */
345
346 static int /* O - 0 on success, -1 on error */
347 add_device(
348 const char *device_class, /* I - Device class */
349 const char *device_make_and_model, /* I - Device make and model */
350 const char *device_info, /* I - Device information */
351 const char *device_uri, /* I - Device URI */
352 const char *device_id, /* I - 1284 device ID */
353 const char *device_location) /* I - Physical location */
354 {
355 cupsd_device_t *device; /* New device */
356
357
358 /*
359 * Allocate memory for the device record...
360 */
361
362 if ((device = calloc(1, sizeof(cupsd_device_t))) == NULL)
363 {
364 fputs("ERROR: [cups-deviced] Ran out of memory allocating a device!\n",
365 stderr);
366 return (-1);
367 }
368
369 /*
370 * Copy the strings over...
371 */
372
373 strlcpy(device->device_class, device_class, sizeof(device->device_class));
374 strlcpy(device->device_info, device_info, sizeof(device->device_info));
375 strlcpy(device->device_uri, device_uri, sizeof(device->device_uri));
376
377 /*
378 * Add the device to the array and return...
379 */
380
381 if (cupsArrayFind(devices, device))
382 {
383 /*
384 * Avoid duplicates!
385 */
386
387 free(device);
388 }
389 else
390 {
391 cupsArrayAdd(devices, device);
392
393 if (device_limit <= 0 || cupsArrayCount(devices) < device_limit)
394 {
395 /*
396 * Send device info...
397 */
398
399 cupsdSendIPPGroup(IPP_TAG_PRINTER);
400 if (send_class)
401 cupsdSendIPPString(IPP_TAG_KEYWORD, "device-class",
402 device_class);
403 if (send_info)
404 cupsdSendIPPString(IPP_TAG_TEXT, "device-info", device_info);
405 if (send_make_and_model)
406 cupsdSendIPPString(IPP_TAG_TEXT, "device-make-and-model",
407 device_make_and_model);
408 if (send_uri)
409 cupsdSendIPPString(IPP_TAG_URI, "device-uri", device_uri);
410 if (send_id)
411 cupsdSendIPPString(IPP_TAG_TEXT, "device-id",
412 device_id ? device_id : "");
413 if (send_location)
414 cupsdSendIPPString(IPP_TAG_TEXT, "device-location",
415 device_location ? device_location : "");
416
417 fflush(stdout);
418 fputs("DEBUG: Flushed attributes...\n", stderr);
419 }
420 }
421
422 return (0);
423 }
424
425
426 /*
427 * 'compare_devices()' - Compare device names to eliminate duplicates.
428 */
429
430 static int /* O - Result of comparison */
431 compare_devices(cupsd_device_t *d0, /* I - First device */
432 cupsd_device_t *d1) /* I - Second device */
433 {
434 int diff; /* Difference between strings */
435
436
437 /*
438 * Sort devices by device-info, device-class, and device-uri...
439 */
440
441 if ((diff = cupsdCompareNames(d0->device_info, d1->device_info)) != 0)
442 return (diff);
443 else if ((diff = strcasecmp(d0->device_class, d1->device_class)) != 0)
444 return (diff);
445 else
446 return (strcasecmp(d0->device_uri, d1->device_uri));
447 }
448
449
450 /*
451 * 'create_strings_array()' - Create a CUPS array of strings.
452 */
453
454 static cups_array_t * /* O - CUPS array */
455 create_strings_array(const char *s) /* I - Comma-delimited strings */
456 {
457 cups_array_t *a; /* CUPS array */
458 const char *start, /* Start of string */
459 *end; /* End of string */
460 char *ptr; /* New string */
461
462
463 if (!s)
464 return (NULL);
465
466 if ((a = cupsArrayNew((cups_array_func_t)strcmp, NULL)) != NULL)
467 {
468 for (start = end = s; *end; start = end + 1)
469 {
470 /*
471 * Find the end of the current delimited string...
472 */
473
474 if ((end = strchr(start, ',')) == NULL)
475 end = start + strlen(start);
476
477 /*
478 * Duplicate the string and add it to the array...
479 */
480
481 if ((ptr = calloc(1, end - start + 1)) == NULL)
482 break;
483
484 memcpy(ptr, start, end - start);
485 cupsArrayAdd(a, ptr);
486 }
487 }
488
489 return (a);
490 }
491
492
493 /*
494 * 'get_current_time()' - Get the current time as a double value in seconds.
495 */
496
497 static double /* O - Time in seconds */
498 get_current_time(void)
499 {
500 struct timeval curtime; /* Current time */
501
502
503 gettimeofday(&curtime, NULL);
504
505 return (curtime.tv_sec + 0.000001 * curtime.tv_usec);
506 }
507
508
509 /*
510 * 'get_device()' - Get a device from a backend.
511 */
512
513 static int /* O - 0 on success, -1 on error */
514 get_device(cupsd_backend_t *backend) /* I - Backend to read from */
515 {
516 char line[2048], /* Line from backend */
517 temp[2048], /* Copy of line */
518 *ptr, /* Pointer into line */
519 *dclass, /* Device class */
520 *uri, /* Device URI */
521 *make_model, /* Make and model */
522 *info, /* Device info */
523 *device_id, /* 1284 device ID */
524 *location; /* Physical location */
525
526
527 if (cupsFileGets(backend->pipe, line, sizeof(line)))
528 {
529 /*
530 * Each line is of the form:
531 *
532 * class URI "make model" "name" ["1284 device ID"] ["location"]
533 */
534
535 strlcpy(temp, line, sizeof(temp));
536
537 /*
538 * device-class
539 */
540
541 dclass = temp;
542
543 for (ptr = temp; *ptr; ptr ++)
544 if (isspace(*ptr & 255))
545 break;
546
547 while (isspace(*ptr & 255))
548 *ptr++ = '\0';
549
550 /*
551 * device-uri
552 */
553
554 if (!*ptr)
555 goto error;
556
557 for (uri = ptr; *ptr; ptr ++)
558 if (isspace(*ptr & 255))
559 break;
560
561 while (isspace(*ptr & 255))
562 *ptr++ = '\0';
563
564 /*
565 * device-make-and-model
566 */
567
568 if (*ptr != '\"')
569 goto error;
570
571 for (ptr ++, make_model = ptr; *ptr && *ptr != '\"'; ptr ++)
572 {
573 if (*ptr == '\\' && ptr[1])
574 _cups_strcpy(ptr, ptr + 1);
575 }
576
577 if (*ptr != '\"')
578 goto error;
579
580 for (*ptr++ = '\0'; isspace(*ptr & 255); *ptr++ = '\0');
581
582 /*
583 * device-info
584 */
585
586 if (*ptr != '\"')
587 goto error;
588
589 for (ptr ++, info = ptr; *ptr && *ptr != '\"'; ptr ++)
590 {
591 if (*ptr == '\\' && ptr[1])
592 _cups_strcpy(ptr, ptr + 1);
593 }
594
595 if (*ptr != '\"')
596 goto error;
597
598 for (*ptr++ = '\0'; isspace(*ptr & 255); *ptr++ = '\0');
599
600 /*
601 * device-id
602 */
603
604 if (*ptr == '\"')
605 {
606 for (ptr ++, device_id = ptr; *ptr && *ptr != '\"'; ptr ++)
607 {
608 if (*ptr == '\\' && ptr[1])
609 _cups_strcpy(ptr, ptr + 1);
610 }
611
612 if (*ptr != '\"')
613 goto error;
614
615 for (*ptr++ = '\0'; isspace(*ptr & 255); *ptr++ = '\0');
616
617 /*
618 * device-location
619 */
620
621 if (*ptr == '\"')
622 {
623 for (ptr ++, location = ptr; *ptr && *ptr != '\"'; ptr ++)
624 {
625 if (*ptr == '\\' && ptr[1])
626 _cups_strcpy(ptr, ptr + 1);
627 }
628
629 if (*ptr != '\"')
630 goto error;
631
632 *ptr = '\0';
633 }
634 else
635 location = NULL;
636 }
637 else
638 {
639 device_id = NULL;
640 location = NULL;
641 }
642
643 /*
644 * Add the device to the array of available devices...
645 */
646
647 if (!add_device(dclass, make_model, info, uri, device_id, location))
648 fprintf(stderr, "DEBUG: [cups-deviced] Found device \"%s\"...\n", uri);
649
650 return (0);
651 }
652
653 /*
654 * End of file...
655 */
656
657 cupsFileClose(backend->pipe);
658 backend->pipe = NULL;
659
660 return (-1);
661
662 /*
663 * Bad format; strip trailing newline and write an error message.
664 */
665
666 error:
667
668 if (line[strlen(line) - 1] == '\n')
669 line[strlen(line) - 1] = '\0';
670
671 fprintf(stderr, "ERROR: [cups-deviced] Bad line from \"%s\": %s\n",
672 backend->name, line);
673 return (0);
674 }
675
676
677 /*
678 * 'process_children()' - Process all dead children...
679 */
680
681 static void
682 process_children(void)
683 {
684 int i; /* Looping var */
685 int status; /* Exit status of child */
686 int pid; /* Process ID of child */
687 cupsd_backend_t *backend; /* Current backend */
688 const char *name; /* Name of process */
689
690
691 /*
692 * Reset the dead_children flag...
693 */
694
695 dead_children = 0;
696
697 /*
698 * Collect the exit status of some children...
699 */
700
701 #ifdef HAVE_WAITPID
702 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
703 #elif defined(HAVE_WAIT3)
704 while ((pid = wait3(&status, WNOHANG, NULL)) > 0)
705 #else
706 if ((pid = wait(&status)) > 0)
707 #endif /* HAVE_WAITPID */
708 {
709 if (status == SIGTERM)
710 status = 0;
711
712 for (i = num_backends, backend = backends; i > 0; i --, backend ++)
713 if (backend->pid == pid)
714 break;
715
716 if (i > 0)
717 {
718 name = backend->name;
719 backend->pid = 0;
720 backend->status = status;
721
722 active_backends --;
723 }
724 else
725 name = "Unknown";
726
727 if (status)
728 {
729 if (WIFEXITED(status))
730 fprintf(stderr,
731 "ERROR: [cups-deviced] PID %d (%s) stopped with status %d!\n",
732 pid, name, WEXITSTATUS(status));
733 else
734 fprintf(stderr,
735 "ERROR: [cups-deviced] PID %d (%s) crashed on signal %d!\n",
736 pid, name, WTERMSIG(status));
737 }
738 else
739 fprintf(stderr,
740 "DEBUG: [cups-deviced] PID %d (%s) exited with no errors.\n",
741 pid, name);
742 }
743 }
744
745
746 /*
747 * 'sigchld_handler()' - Handle 'child' signals from old processes.
748 */
749
750 static void
751 sigchld_handler(int sig) /* I - Signal number */
752 {
753 (void)sig;
754
755 /*
756 * Flag that we have dead children...
757 */
758
759 dead_children = 1;
760
761 /*
762 * Reset the signal handler as needed...
763 */
764
765 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
766 signal(SIGCLD, sigchld_handler);
767 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
768 }
769
770
771 /*
772 * 'start_backend()' - Run a backend to gather the available devices.
773 */
774
775 static int /* O - 0 on success, -1 on error */
776 start_backend(const char *name, /* I - Backend to run */
777 int root) /* I - Run as root? */
778 {
779 const char *server_bin; /* CUPS_SERVERBIN environment variable */
780 char program[1024]; /* Full path to backend */
781 cupsd_backend_t *backend; /* Current backend */
782 char *argv[2]; /* Command-line arguments */
783
784
785 if (num_backends >= MAX_BACKENDS)
786 {
787 fprintf(stderr, "ERROR: Too many backends (%d)!\n", num_backends);
788 return (-1);
789 }
790
791 if ((server_bin = getenv("CUPS_SERVERBIN")) == NULL)
792 server_bin = CUPS_SERVERBIN;
793
794 snprintf(program, sizeof(program), "%s/backend/%s", server_bin, name);
795
796 backend = backends + num_backends;
797
798 argv[0] = (char *)name;
799 argv[1] = NULL;
800
801 if ((backend->pipe = cupsdPipeCommand(&(backend->pid), program, argv,
802 root ? 0 : normal_user)) == NULL)
803 {
804 fprintf(stderr, "ERROR: [cups-deviced] Unable to execute \"%s\" - %s\n",
805 program, strerror(errno));
806 return (-1);
807 }
808
809 /*
810 * Fill in the rest of the backend information...
811 */
812
813 fprintf(stderr, "DEBUG: [cups-deviced] Started backend %s (PID %d)\n",
814 program, backend->pid);
815
816 backend_fds[num_backends].fd = cupsFileNumber(backend->pipe);
817 backend_fds[num_backends].events = POLLIN;
818
819 backend->name = strdup(name);
820 backend->status = 0;
821 backend->count = 0;
822
823 active_backends ++;
824 num_backends ++;
825
826 return (0);
827 }
828
829
830 /*
831 * End of "$Id: cups-deviced.c 7816 2008-07-30 20:53:31Z mike $".
832 */