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