]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/cups-deviced.c
Import CUPS 1.4svn-r7226.
[thirdparty/cups.git] / scheduler / cups-deviced.c
1 /*
2 * "$Id: cups-deviced.c 7011 2007-10-10 21:13:35Z 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_dev() - Add a new device to the list.
19 * compare_devs() - Compare device names for sorting.
20 * run_backend() - Run a backend to gather the available devices.
21 * sigalrm_handler() - Handle alarm signals for backends that get hung
22 */
23
24 /*
25 * Include necessary headers...
26 */
27
28 #include "util.h"
29 #include <cups/array.h>
30 #include <cups/dir.h>
31 #include <fcntl.h>
32
33
34 /*
35 * Device information structure...
36 */
37
38 typedef struct
39 {
40 char device_class[128], /* Device class */
41 device_make_and_model[128], /* Make and model, if known */
42 device_info[128], /* Device info/description */
43 device_uri[1024], /* Device URI */
44 device_id[1024]; /* 1284 Device ID */
45 } dev_info_t;
46
47
48 /*
49 * Local globals...
50 */
51
52 static int alarm_tripped; /* Non-zero if alarm was tripped */
53 static cups_array_t *devs; /* Device info */
54 static int normal_user; /* Normal user ID */
55
56
57 /*
58 * Local functions...
59 */
60
61 static dev_info_t *add_dev(const char *device_class,
62 const char *device_make_and_model,
63 const char *device_info,
64 const char *device_uri,
65 const char *device_id);
66 static int compare_devs(dev_info_t *p0, dev_info_t *p1);
67 static FILE *run_backend(const char *backend, int uid, int *pid);
68 static void sigalrm_handler(int sig);
69
70
71 /*
72 * 'main()' - Scan for devices and return an IPP response.
73 *
74 * Usage:
75 *
76 * cups-deviced request_id limit options
77 */
78
79 int /* O - Exit code */
80 main(int argc, /* I - Number of command-line args */
81 char *argv[]) /* I - Command-line arguments */
82 {
83 const char *server_bin; /* CUPS_SERVERBIN environment variable */
84 char backends[1024]; /* Location of backends */
85 int request_id; /* Request ID */
86 int count; /* Number of devices from backend */
87 int compat; /* Compatibility device? */
88 FILE *fp; /* Pipe to device backend */
89 int pid; /* Process ID of backend */
90 cups_dir_t *dir; /* Directory pointer */
91 cups_dentry_t *dent; /* Directory entry */
92 char filename[1024], /* Name of backend */
93 line[2048], /* Line from backend */
94 dclass[64], /* Device class */
95 uri[1024], /* Device URI */
96 info[128], /* Device info */
97 make_model[256], /* Make and model */
98 device_id[1024]; /* 1284 device ID */
99 int num_options; /* Number of options */
100 cups_option_t *options; /* Options */
101 const char *requested; /* requested-attributes option */
102 int send_class, /* Send device-class attribute? */
103 send_info, /* Send device-info attribute? */
104 send_make_and_model, /* Send device-make-and-model attribute? */
105 send_uri, /* Send device-uri attribute? */
106 send_id; /* Send device-id attribute? */
107 dev_info_t *dev; /* Current device */
108 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
109 struct sigaction action; /* Actions for POSIX signals */
110 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
111
112
113 setbuf(stderr, NULL);
114
115 /*
116 * Check the command-line...
117 */
118
119 if (argc > 1)
120 request_id = atoi(argv[1]);
121 else
122 request_id = 1;
123
124 if (argc != 5)
125 {
126 fputs("Usage: cups-deviced request-id limit user-id options\n", stderr);
127
128 return (1);
129 }
130
131 if (request_id < 1)
132 {
133 fprintf(stderr, "cups-deviced: Bad request ID %d!\n", request_id);
134
135 return (1);
136 }
137
138 normal_user = atoi(argv[3]);
139 if (normal_user <= 0)
140 {
141 fprintf(stderr, "cups-deviced: Bad user %d!\n", normal_user);
142
143 return (1);
144 }
145
146 num_options = cupsParseOptions(argv[4], 0, &options);
147 requested = cupsGetOption("requested-attributes", num_options, options);
148
149 if (!requested || strstr(requested, "all"))
150 {
151 send_class = 1;
152 send_info = 1;
153 send_make_and_model = 1;
154 send_uri = 1;
155 send_id = 1;
156 }
157 else
158 {
159 send_class = strstr(requested, "device-class") != NULL;
160 send_info = strstr(requested, "device-info") != NULL;
161 send_make_and_model = strstr(requested, "device-make-and-model") != NULL;
162 send_uri = strstr(requested, "device-uri") != NULL;
163 send_id = strstr(requested, "device-id") != NULL;
164 }
165
166 /*
167 * Try opening the backend directory...
168 */
169
170 if ((server_bin = getenv("CUPS_SERVERBIN")) == NULL)
171 server_bin = CUPS_SERVERBIN;
172
173 snprintf(backends, sizeof(backends), "%s/backend", server_bin);
174
175 if ((dir = cupsDirOpen(backends)) == NULL)
176 {
177 fprintf(stderr, "ERROR: [cups-deviced] Unable to open backend directory "
178 "\"%s\": %s", backends, strerror(errno));
179
180 return (1);
181 }
182
183 /*
184 * Setup the devices array...
185 */
186
187 devs = cupsArrayNew((cups_array_func_t)compare_devs, NULL);
188
189 /*
190 * Loop through all of the device backends...
191 */
192
193 while ((dent = cupsDirRead(dir)) != NULL)
194 {
195 /*
196 * Skip entries that are not executable files...
197 */
198
199 if (!S_ISREG(dent->fileinfo.st_mode) ||
200 (dent->fileinfo.st_mode & (S_IRUSR | S_IXUSR)) != (S_IRUSR | S_IXUSR))
201 continue;
202
203 /*
204 * Change effective users depending on the backend permissions...
205 */
206
207 snprintf(filename, sizeof(filename), "%s/%s", backends, dent->filename);
208
209 /*
210 * Backends without permissions for normal users run as root,
211 * all others run as the unprivileged user...
212 */
213
214 fp = run_backend(filename,
215 (dent->fileinfo.st_mode & (S_IRWXG | S_IRWXO))
216 ? normal_user : 0,
217 &pid);
218
219 /*
220 * Collect the output from the backend...
221 */
222
223 if (fp)
224 {
225 /*
226 * Set an alarm for the first read from the backend; this avoids
227 * problems when a backend is hung getting device information.
228 */
229
230 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
231 sigset(SIGALRM, sigalrm_handler);
232 #elif defined(HAVE_SIGACTION)
233 memset(&action, 0, sizeof(action));
234
235 sigemptyset(&action.sa_mask);
236 sigaddset(&action.sa_mask, SIGALRM);
237 action.sa_handler = sigalrm_handler;
238 sigaction(SIGALRM, &action, NULL);
239 #else
240 signal(SIGALRM, sigalrm_handler);
241 #endif /* HAVE_SIGSET */
242
243 alarm_tripped = 0;
244 count = 0;
245 compat = !strcmp(dent->filename, "smb");
246
247 alarm(30);
248
249 while (fgets(line, sizeof(line), fp) != NULL)
250 {
251 /*
252 * Reset the alarm clock...
253 */
254
255 alarm(30);
256
257 /*
258 * Each line is of the form:
259 *
260 * class URI "make model" "name" ["1284 device ID"]
261 */
262
263 device_id[0] = '\0';
264
265 if (!strncasecmp(line, "Usage", 5))
266 compat = 1;
267 else if (sscanf(line,
268 "%63s%1023s%*[ \t]\"%255[^\"]\"%*[ \t]\"%127[^\"]\""
269 "%*[ \t]\"%1023[^\"]",
270 dclass, uri, make_model, info, device_id) < 4)
271 {
272 /*
273 * Bad format; strip trailing newline and write an error message.
274 */
275
276 if (line[strlen(line) - 1] == '\n')
277 line[strlen(line) - 1] = '\0';
278
279 fprintf(stderr, "ERROR: [cups-deviced] Bad line from \"%s\": %s\n",
280 dent->filename, line);
281 compat = 1;
282 break;
283 }
284 else
285 {
286 /*
287 * Add the device to the array of available devices...
288 */
289
290 dev = add_dev(dclass, make_model, info, uri, device_id);
291 if (!dev)
292 {
293 cupsDirClose(dir);
294 fclose(fp);
295 kill(pid, SIGTERM);
296 return (1);
297 }
298
299 fprintf(stderr, "DEBUG: [cups-deviced] Added device \"%s\"...\n",
300 uri);
301 count ++;
302 }
303 }
304
305 /*
306 * Turn the alarm clock off and close the pipe to the command...
307 */
308
309 alarm(0);
310
311 if (alarm_tripped)
312 fprintf(stderr, "WARNING: [cups-deviced] Backend \"%s\" did not "
313 "respond within 30 seconds!\n", dent->filename);
314
315 fclose(fp);
316 kill(pid, SIGTERM);
317
318 /*
319 * Hack for backends that don't support the CUPS 1.1 calling convention:
320 * add a network device with the method == backend name.
321 */
322
323 if (count == 0 && compat)
324 {
325 snprintf(line, sizeof(line), "Unknown Network Device (%s)",
326 dent->filename);
327
328 dev = add_dev("network", line, "Unknown", dent->filename, "");
329 if (!dev)
330 {
331 cupsDirClose(dir);
332 return (1);
333 }
334
335 fprintf(stderr, "DEBUG: [cups-deviced] Compatibility device "
336 "\"%s\"...\n", dent->filename);
337 }
338 }
339 else
340 fprintf(stderr, "WARNING: [cups-deviced] Unable to execute \"%s\" "
341 "backend: %s\n", dent->filename, strerror(errno));
342 }
343
344 cupsDirClose(dir);
345
346 /*
347 * Output the list of devices...
348 */
349
350 puts("Content-Type: application/ipp\n");
351
352 cupsdSendIPPHeader(IPP_OK, request_id);
353 cupsdSendIPPGroup(IPP_TAG_OPERATION);
354 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
355 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", "en-US");
356
357 if ((count = atoi(argv[2])) <= 0)
358 count = cupsArrayCount(devs);
359
360 if (count > cupsArrayCount(devs))
361 count = cupsArrayCount(devs);
362
363 for (dev = (dev_info_t *)cupsArrayFirst(devs);
364 count > 0;
365 count --, dev = (dev_info_t *)cupsArrayNext(devs))
366 {
367 /*
368 * Add strings to attributes...
369 */
370
371 cupsdSendIPPGroup(IPP_TAG_PRINTER);
372 if (send_class)
373 cupsdSendIPPString(IPP_TAG_KEYWORD, "device-class", dev->device_class);
374 if (send_info)
375 cupsdSendIPPString(IPP_TAG_TEXT, "device-info", dev->device_info);
376 if (send_make_and_model)
377 cupsdSendIPPString(IPP_TAG_TEXT, "device-make-and-model",
378 dev->device_make_and_model);
379 if (send_uri)
380 cupsdSendIPPString(IPP_TAG_URI, "device-uri", dev->device_uri);
381 if (send_id)
382 cupsdSendIPPString(IPP_TAG_TEXT, "device-id", dev->device_id);
383 }
384
385 cupsdSendIPPTrailer();
386
387 /*
388 * Free the devices array and return...
389 */
390
391 for (dev = (dev_info_t *)cupsArrayFirst(devs);
392 dev;
393 dev = (dev_info_t *)cupsArrayNext(devs))
394 free(dev);
395
396 cupsArrayDelete(devs);
397
398 return (0);
399 }
400
401
402 /*
403 * 'add_dev()' - Add a new device to the list.
404 */
405
406 static dev_info_t * /* O - New device or NULL on error */
407 add_dev(
408 const char *device_class, /* I - Device class */
409 const char *device_make_and_model, /* I - Device make and model */
410 const char *device_info, /* I - Device information */
411 const char *device_uri, /* I - Device URI */
412 const char *device_id) /* I - 1284 device ID */
413 {
414 dev_info_t *dev, /* New device */
415 *temp; /* Found device */
416
417
418 /*
419 * Allocate memory for the device record...
420 */
421
422 if ((dev = calloc(1, sizeof(dev_info_t))) == NULL)
423 {
424 fputs("ERROR: [cups-deviced] Ran out of memory allocating a device!\n",
425 stderr);
426 return (NULL);
427 }
428
429 /*
430 * Copy the strings over...
431 */
432
433 strlcpy(dev->device_class, device_class, sizeof(dev->device_class));
434 strlcpy(dev->device_make_and_model, device_make_and_model,
435 sizeof(dev->device_make_and_model));
436 strlcpy(dev->device_info, device_info, sizeof(dev->device_info));
437 strlcpy(dev->device_uri, device_uri, sizeof(dev->device_uri));
438 strlcpy(dev->device_id, device_id, sizeof(dev->device_id));
439
440 /*
441 * Add the device to the array and return...
442 */
443
444 if ((temp = cupsArrayFind(devs, dev)) != NULL)
445 {
446 /*
447 * Avoid duplicates!
448 */
449
450 free(dev);
451 dev = temp;
452 }
453 else
454 cupsArrayAdd(devs, dev);
455
456 return (dev);
457 }
458
459
460 /*
461 * 'compare_devs()' - Compare device names for sorting.
462 */
463
464 static int /* O - Result of comparison */
465 compare_devs(dev_info_t *d0, /* I - First device */
466 dev_info_t *d1) /* I - Second device */
467 {
468 int diff; /* Difference between strings */
469
470
471 /*
472 * Sort devices by device-info, device-class, and device-uri...
473 */
474
475 if ((diff = cupsdCompareNames(d0->device_info, d1->device_info)) != 0)
476 return (diff);
477 else if ((diff = strcasecmp(d0->device_class, d1->device_class)) != 0)
478 return (diff);
479 else
480 return (strcasecmp(d0->device_uri, d1->device_uri));
481 }
482
483
484 /*
485 * 'run_backend()' - Run a backend to gather the available devices.
486 */
487
488 static FILE * /* O - stdout of backend */
489 run_backend(const char *backend, /* I - Backend to run */
490 int uid, /* I - User ID to run as */
491 int *pid) /* O - Process ID of backend */
492 {
493 int fds[2]; /* Pipe file descriptors */
494
495
496 if (pipe(fds))
497 {
498 fprintf(stderr, "ERROR: Unable to create a pipe for \"%s\" - %s\n",
499 backend, strerror(errno));
500 return (NULL);
501 }
502
503 if ((*pid = fork()) < 0)
504 {
505 /*
506 * Error!
507 */
508
509 fprintf(stderr, "ERROR: Unable to fork for \"%s\" - %s\n", backend,
510 strerror(errno));
511 close(fds[0]);
512 close(fds[1]);
513 return (NULL);
514 }
515 else if (!*pid)
516 {
517 /*
518 * Child comes here...
519 */
520
521 if (!getuid() && uid)
522 setuid(uid); /* Run as restricted user */
523
524 close(0); /* </dev/null */
525 open("/dev/null", O_RDONLY);
526
527 close(1); /* >pipe */
528 dup(fds[1]);
529
530 close(fds[0]); /* Close copies of pipes */
531 close(fds[1]);
532
533 execl(backend, backend, (char *)0); /* Run it! */
534 fprintf(stderr, "ERROR: Unable to execute \"%s\" - %s\n", backend,
535 strerror(errno));
536 exit(1);
537 }
538
539 /*
540 * Parent comes here, make a FILE * from the input side of the pipe...
541 */
542
543 close(fds[1]);
544
545 return (fdopen(fds[0], "r"));
546 }
547
548
549 /*
550 * 'sigalrm_handler()' - Handle alarm signals for backends that get hung
551 * trying to list the available devices...
552 */
553
554 static void
555 sigalrm_handler(int sig) /* I - Signal number */
556 {
557 (void)sig; /* remove compiler warnings... */
558
559 alarm_tripped = 1;
560 }
561
562
563 /*
564 * End of "$Id: cups-deviced.c 7011 2007-10-10 21:13:35Z mike $".
565 */