]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/cups-deviced.c
Merge changes from CUPS 1.4svn-r7715.
[thirdparty/cups.git] / scheduler / cups-deviced.c
1 /*
2 * "$Id: cups-deviced.c 7624 2008-06-09 15:55:04Z 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_make_and_model[128], /* Make and model, if known */
69 device_info[128], /* Device info/description */
70 device_uri[1024], /* Device URI */
71 device_id[1024]; /* 1284 Device ID */
72 } cupsd_device_t;
73
74
75 /*
76 * Local globals...
77 */
78
79 static int num_backends = 0,
80 /* Total backends */
81 active_backends = 0;
82 /* Active backends */
83 static cupsd_backend_t backends[MAX_BACKENDS];
84 /* Array of backends */
85 static struct pollfd backend_fds[MAX_BACKENDS];
86 /* Array for poll() */
87 static cups_array_t *devices; /* Array of devices */
88 static int normal_user; /* Normal user ID */
89 static int device_limit; /* Maximum number of devices */
90 static int send_class, /* Send device-class attribute? */
91 send_info, /* Send device-info attribute? */
92 send_make_and_model,
93 /* Send device-make-and-model attribute? */
94 send_uri, /* Send device-uri attribute? */
95 send_id; /* Send device-id attribute? */
96 static int dead_children = 0;
97 /* Dead children? */
98
99
100 /*
101 * Local functions...
102 */
103
104 static int add_device(const char *device_class,
105 const char *device_make_and_model,
106 const char *device_info,
107 const char *device_uri,
108 const char *device_id);
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 send_class = send_info = send_make_and_model = send_uri = send_id = 1;
202 else
203 {
204 send_class = cupsArrayFind(requested, "device-class") != NULL;
205 send_info = cupsArrayFind(requested, "device-info") != NULL;
206 send_make_and_model = cupsArrayFind(requested, "device-make-and-model") != NULL;
207 send_uri = cupsArrayFind(requested, "device-uri") != NULL;
208 send_id = cupsArrayFind(requested, "device-id") != NULL;
209 }
210
211 /*
212 * Listen to child signals...
213 */
214
215 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
216 sigset(SIGCHLD, sigchld_handler);
217 #elif defined(HAVE_SIGACTION)
218 memset(&action, 0, sizeof(action));
219
220 sigemptyset(&action.sa_mask);
221 sigaddset(&action.sa_mask, SIGCHLD);
222 action.sa_handler = sigchld_handler;
223 sigaction(SIGCHLD, &action, NULL);
224 #else
225 signal(SIGCLD, sigchld_handler); /* No, SIGCLD isn't a typo... */
226 #endif /* HAVE_SIGSET */
227
228 /*
229 * Try opening the backend directory...
230 */
231
232 if ((server_bin = getenv("CUPS_SERVERBIN")) == NULL)
233 server_bin = CUPS_SERVERBIN;
234
235 snprintf(filename, sizeof(filename), "%s/backend", server_bin);
236
237 if ((dir = cupsDirOpen(filename)) == NULL)
238 {
239 fprintf(stderr, "ERROR: [cups-deviced] Unable to open backend directory "
240 "\"%s\": %s", filename, strerror(errno));
241
242 return (1);
243 }
244
245 /*
246 * Setup the devices array...
247 */
248
249 devices = cupsArrayNew((cups_array_func_t)compare_devices, NULL);
250
251 /*
252 * Loop through all of the device backends...
253 */
254
255 while ((dent = cupsDirRead(dir)) != NULL)
256 {
257 /*
258 * Skip entries that are not executable files...
259 */
260
261 if (!S_ISREG(dent->fileinfo.st_mode) ||
262 !isalnum(dent->filename[0] & 255) ||
263 (dent->fileinfo.st_mode & (S_IRUSR | S_IXUSR)) != (S_IRUSR | S_IXUSR))
264 continue;
265
266 if (cupsArrayFind(exclude, dent->filename))
267 continue;
268
269 /*
270 * Backends without permissions for normal users run as root,
271 * all others run as the unprivileged user...
272 */
273
274 start_backend(dent->filename,
275 !(dent->fileinfo.st_mode & (S_IRWXG | S_IRWXO)));
276 }
277
278 cupsDirClose(dir);
279
280 /*
281 * Collect devices...
282 */
283
284 if (getenv("SOFTWARE"))
285 puts("Content-Type: application/ipp\n");
286
287 cupsdSendIPPHeader(IPP_OK, request_id);
288 cupsdSendIPPGroup(IPP_TAG_OPERATION);
289 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
290 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", "en-US");
291
292 end_time = get_current_time() + timeout;
293
294 while (active_backends > 0 && (current_time = get_current_time()) < end_time)
295 {
296 /*
297 * Collect the output from the backends...
298 */
299
300 timeout = (int)(1000 * (end_time - current_time));
301
302 if (poll(backend_fds, num_backends, timeout) > 0)
303 {
304 for (i = 0; i < num_backends; i ++)
305 if (backend_fds[i].revents && backends[i].pipe)
306 if (get_device(backends + i))
307 {
308 backend_fds[i].fd = 0;
309 backend_fds[i].events = 0;
310 }
311 }
312
313 /*
314 * Get exit status from children...
315 */
316
317 if (dead_children)
318 process_children();
319 }
320
321 cupsdSendIPPTrailer();
322
323 /*
324 * Terminate any remaining backends and exit...
325 */
326
327 if (active_backends > 0)
328 {
329 for (i = 0; i < num_backends; i ++)
330 if (backends[i].pid)
331 kill(backends[i].pid, SIGTERM);
332 }
333
334 return (0);
335 }
336
337
338 /*
339 * 'add_device()' - Add a new device to the list.
340 */
341
342 static int /* O - 0 on success, -1 on error */
343 add_device(
344 const char *device_class, /* I - Device class */
345 const char *device_make_and_model, /* I - Device make and model */
346 const char *device_info, /* I - Device information */
347 const char *device_uri, /* I - Device URI */
348 const char *device_id) /* I - 1284 device ID */
349 {
350 cupsd_device_t *device; /* New device */
351
352
353 /*
354 * Allocate memory for the device record...
355 */
356
357 if ((device = calloc(1, sizeof(cupsd_device_t))) == NULL)
358 {
359 fputs("ERROR: [cups-deviced] Ran out of memory allocating a device!\n",
360 stderr);
361 return (-1);
362 }
363
364 /*
365 * Copy the strings over...
366 */
367
368 strlcpy(device->device_class, device_class, sizeof(device->device_class));
369 strlcpy(device->device_make_and_model, device_make_and_model,
370 sizeof(device->device_make_and_model));
371 strlcpy(device->device_info, device_info, sizeof(device->device_info));
372 strlcpy(device->device_uri, device_uri, sizeof(device->device_uri));
373 strlcpy(device->device_id, device_id, sizeof(device->device_id));
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->device_class);
401 if (send_info)
402 cupsdSendIPPString(IPP_TAG_TEXT, "device-info", device->device_info);
403 if (send_make_and_model)
404 cupsdSendIPPString(IPP_TAG_TEXT, "device-make-and-model",
405 device->device_make_and_model);
406 if (send_uri)
407 cupsdSendIPPString(IPP_TAG_URI, "device-uri", device->device_uri);
408 if (send_id)
409 cupsdSendIPPString(IPP_TAG_TEXT, "device-id", device->device_id);
410
411 fflush(stdout);
412 fputs("DEBUG: Flushed attributes...\n", stderr);
413 }
414 }
415
416 return (0);
417 }
418
419
420 /*
421 * 'compare_devices()' - Compare device names to eliminate duplicates.
422 */
423
424 static int /* O - Result of comparison */
425 compare_devices(cupsd_device_t *d0, /* I - First device */
426 cupsd_device_t *d1) /* I - Second device */
427 {
428 int diff; /* Difference between strings */
429
430
431 /*
432 * Sort devices by device-info, device-class, and device-uri...
433 */
434
435 if ((diff = cupsdCompareNames(d0->device_info, d1->device_info)) != 0)
436 return (diff);
437 else if ((diff = strcasecmp(d0->device_class, d1->device_class)) != 0)
438 return (diff);
439 else
440 return (strcasecmp(d0->device_uri, d1->device_uri));
441 }
442
443
444 /*
445 * 'create_strings_array()' - Create a CUPS array of strings.
446 */
447
448 static cups_array_t * /* O - CUPS array */
449 create_strings_array(const char *s) /* I - Comma-delimited strings */
450 {
451 cups_array_t *a; /* CUPS array */
452 const char *start, /* Start of string */
453 *end; /* End of string */
454 char *ptr; /* New string */
455
456
457 if (!s)
458 return (NULL);
459
460 if ((a = cupsArrayNew((cups_array_func_t)strcmp, NULL)) != NULL)
461 {
462 for (start = end = s; *end; start = end + 1)
463 {
464 /*
465 * Find the end of the current delimited string...
466 */
467
468 if ((end = strchr(start, ',')) == NULL)
469 end = start + strlen(start);
470
471 /*
472 * Duplicate the string and add it to the array...
473 */
474
475 if ((ptr = calloc(1, end - start + 1)) == NULL)
476 break;
477
478 memcpy(ptr, start, end - start);
479 cupsArrayAdd(a, ptr);
480 }
481 }
482
483 return (a);
484 }
485
486
487 /*
488 * 'get_current_time()' - Get the current time as a double value in seconds.
489 */
490
491 static double /* O - Time in seconds */
492 get_current_time(void)
493 {
494 struct timeval curtime; /* Current time */
495
496
497 gettimeofday(&curtime, NULL);
498
499 return (curtime.tv_sec + 0.000001 * curtime.tv_usec);
500 }
501
502
503 /*
504 * 'get_device()' - Get a device from a backend.
505 */
506
507 static int /* O - 0 on success, -1 on error */
508 get_device(cupsd_backend_t *backend) /* I - Backend to read from */
509 {
510 char line[2048], /* Line from backend */
511 dclass[64], /* Device class */
512 uri[1024], /* Device URI */
513 info[128], /* Device info */
514 make_model[256], /* Make and model */
515 device_id[1024]; /* 1284 device ID */
516
517
518 if (cupsFileGets(backend->pipe, line, sizeof(line)))
519 {
520 /*
521 * Each line is of the form:
522 *
523 * class URI "make model" "name" ["1284 device ID"]
524 */
525
526 device_id[0] = '\0';
527
528 if (sscanf(line,
529 "%63s%1023s%*[ \t]\"%255[^\"]\"%*[ \t]\"%127[^\"]\""
530 "%*[ \t]\"%1023[^\"]",
531 dclass, uri, make_model, info, device_id) < 4)
532 {
533 /*
534 * Bad format; strip trailing newline and write an error message.
535 */
536
537 if (line[strlen(line) - 1] == '\n')
538 line[strlen(line) - 1] = '\0';
539
540 fprintf(stderr, "ERROR: [cups-deviced] Bad line from \"%s\": %s\n",
541 backend->name, line);
542 }
543 else
544 {
545 /*
546 * Add the device to the array of available devices...
547 */
548
549 if (!add_device(dclass, make_model, info, uri, device_id))
550 fprintf(stderr, "DEBUG: [cups-deviced] Found device \"%s\"...\n", uri);
551 }
552
553 return (0);
554 }
555
556 /*
557 * End of file...
558 */
559
560 cupsFileClose(backend->pipe);
561 backend->pipe = NULL;
562
563 return (-1);
564 }
565
566
567 /*
568 * 'process_children()' - Process all dead children...
569 */
570
571 static void
572 process_children(void)
573 {
574 int i; /* Looping var */
575 int status; /* Exit status of child */
576 int pid; /* Process ID of child */
577 cupsd_backend_t *backend; /* Current backend */
578 const char *name; /* Name of process */
579
580
581 /*
582 * Reset the dead_children flag...
583 */
584
585 dead_children = 0;
586
587 /*
588 * Collect the exit status of some children...
589 */
590
591 #ifdef HAVE_WAITPID
592 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
593 #elif defined(HAVE_WAIT3)
594 while ((pid = wait3(&status, WNOHANG, NULL)) > 0)
595 #else
596 if ((pid = wait(&status)) > 0)
597 #endif /* HAVE_WAITPID */
598 {
599 if (status == SIGTERM)
600 status = 0;
601
602 for (i = num_backends, backend = backends; i > 0; i --, backend ++)
603 if (backend->pid == pid)
604 break;
605
606 if (i > 0)
607 {
608 name = backend->name;
609 backend->pid = 0;
610 backend->status = status;
611
612 active_backends --;
613 }
614 else
615 name = "Unknown";
616
617 if (status)
618 {
619 if (WIFEXITED(status))
620 fprintf(stderr,
621 "ERROR: [cups-deviced] PID %d (%s) stopped with status %d!\n",
622 pid, name, WEXITSTATUS(status));
623 else
624 fprintf(stderr,
625 "ERROR: [cups-deviced] PID %d (%s) crashed on signal %d!\n",
626 pid, name, WTERMSIG(status));
627 }
628 else
629 fprintf(stderr,
630 "DEBUG: [cups-deviced] PID %d (%s) exited with no errors.\n",
631 pid, name);
632 }
633 }
634
635
636 /*
637 * 'sigchld_handler()' - Handle 'child' signals from old processes.
638 */
639
640 static void
641 sigchld_handler(int sig) /* I - Signal number */
642 {
643 (void)sig;
644
645 /*
646 * Flag that we have dead children...
647 */
648
649 dead_children = 1;
650
651 /*
652 * Reset the signal handler as needed...
653 */
654
655 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
656 signal(SIGCLD, sigchld_handler);
657 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
658 }
659
660
661 /*
662 * 'start_backend()' - Run a backend to gather the available devices.
663 */
664
665 static int /* O - 0 on success, -1 on error */
666 start_backend(const char *name, /* I - Backend to run */
667 int root) /* I - Run as root? */
668 {
669 const char *server_bin; /* CUPS_SERVERBIN environment variable */
670 char program[1024]; /* Full path to backend */
671 cupsd_backend_t *backend; /* Current backend */
672 char *argv[2]; /* Command-line arguments */
673
674
675 if (num_backends >= MAX_BACKENDS)
676 {
677 fprintf(stderr, "ERROR: Too many backends (%d)!\n", num_backends);
678 return (-1);
679 }
680
681 if ((server_bin = getenv("CUPS_SERVERBIN")) == NULL)
682 server_bin = CUPS_SERVERBIN;
683
684 snprintf(program, sizeof(program), "%s/backend/%s", server_bin, name);
685
686 backend = backends + num_backends;
687
688 argv[0] = (char *)name;
689 argv[1] = NULL;
690
691 if ((backend->pipe = cupsdPipeCommand(&(backend->pid), program, argv,
692 root ? 0 : normal_user)) == NULL)
693 {
694 fprintf(stderr, "ERROR: [cups-deviced] Unable to execute \"%s\" - %s\n",
695 program, strerror(errno));
696 return (-1);
697 }
698
699 /*
700 * Fill in the rest of the backend information...
701 */
702
703 fprintf(stderr, "DEBUG: [cups-deviced] Started backend %s (PID %d)\n",
704 program, backend->pid);
705
706 backend_fds[num_backends].fd = cupsFileNumber(backend->pipe);
707 backend_fds[num_backends].events = POLLIN;
708
709 backend->name = strdup(name);
710 backend->status = 0;
711 backend->count = 0;
712
713 active_backends ++;
714 num_backends ++;
715
716 return (0);
717 }
718
719
720 /*
721 * End of "$Id: cups-deviced.c 7624 2008-06-09 15:55:04Z mike $".
722 */