]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/cups-deviced.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / cups-deviced.c
CommitLineData
ef416fc2 1/*
bc44d920 2 * "$Id: cups-deviced.c 6649 2007-07-11 21:46:42Z mike $"
ef416fc2 3 *
4 * Device scanning mini-daemon for the Common UNIX Printing System (CUPS).
5 *
bc44d920 6 * Copyright 2007 by Apple Inc.
bd7854cb 7 * Copyright 1997-2006 by Easy Software Products.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 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/".
ef416fc2 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 * sigalrm_handler() - Handle alarm signals for backends that get hung
21 */
22
23/*
24 * Include necessary headers...
25 */
26
27#include "util.h"
28#include <cups/array.h>
29#include <cups/dir.h>
30
b423cd4c 31#ifdef __hpux
32# define seteuid(uid) setresuid(-1, (uid), -1)
33#endif /* __hpux */
34
ef416fc2 35
36/*
37 * Device information structure...
38 */
39
40typedef struct
41{
42 char device_class[128], /* Device class */
43 device_make_and_model[128], /* Make and model, if known */
44 device_info[128], /* Device info/description */
45 device_uri[1024], /* Device URI */
46 device_id[1024]; /* 1284 Device ID */
47} dev_info_t;
48
49
50/*
51 * Local globals...
52 */
53
54static int alarm_tripped; /* Non-zero if alarm was tripped */
55static cups_array_t *devs; /* Device info */
e00b005a 56static int normal_user; /* Normal user ID */
ef416fc2 57
58
59/*
60 * Local functions...
61 */
62
63static dev_info_t *add_dev(const char *device_class,
64 const char *device_make_and_model,
65 const char *device_info,
66 const char *device_uri,
67 const char *device_id);
68static int compare_devs(dev_info_t *p0, dev_info_t *p1);
69static void sigalrm_handler(int sig);
70
71
72/*
73 * 'main()' - Scan for devices and return an IPP response.
74 *
75 * Usage:
76 *
77 * cups-deviced request_id limit options
78 */
79
80int /* O - Exit code */
81main(int argc, /* I - Number of command-line args */
82 char *argv[]) /* I - Command-line arguments */
83{
84 const char *server_bin; /* CUPS_SERVERBIN environment variable */
85 char backends[1024]; /* Location of backends */
e00b005a 86 int request_id; /* Request ID */
ef416fc2 87 int count; /* Number of devices from backend */
88 int compat; /* Compatibility device? */
89 FILE *fp; /* Pipe to device 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
e00b005a 113 setbuf(stderr, NULL);
114
ef416fc2 115 /*
116 * Check the command-line...
117 */
118
e00b005a 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
e00b005a 128 return (1);
129 }
130
131 if (request_id < 1)
ef416fc2 132 {
e00b005a 133 fprintf(stderr, "cups-deviced: Bad request ID %d!\n", request_id);
134
ef416fc2 135 return (1);
136 }
137
e00b005a 138 normal_user = atoi(argv[3]);
139 if (normal_user <= 0)
140 {
141 fprintf(stderr, "cups-deviced: Bad user %d!\n", normal_user);
142
e00b005a 143 return (1);
144 }
145
146 num_options = cupsParseOptions(argv[4], 0, &options);
ef416fc2 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 {
e00b005a 177 fprintf(stderr, "ERROR: [cups-deviced] Unable to open backend directory "
178 "\"%s\": %s", backends, strerror(errno));
179
ef416fc2 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 {
e00b005a 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 if (!getuid())
208 {
209 /*
210 * Backends without permissions for normal users run as root,
211 * all others run as the unprivileged user...
212 */
213
214 if (!(dent->fileinfo.st_mode & (S_IRWXG | S_IRWXO)))
215 seteuid(0);
216 else
217 seteuid(normal_user);
218 }
219
ef416fc2 220 /*
221 * Run the backend with no arguments and collect the output...
222 */
223
224 snprintf(filename, sizeof(filename), "%s/%s", backends, dent->filename);
225 if ((fp = popen(filename, "r")) != NULL)
226 {
227 /*
228 * Set an alarm for the first read from the backend; this avoids
229 * problems when a backend is hung getting device information.
230 */
231
232#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
233 sigset(SIGALRM, sigalrm_handler);
234#elif defined(HAVE_SIGACTION)
235 memset(&action, 0, sizeof(action));
236
237 sigemptyset(&action.sa_mask);
238 sigaddset(&action.sa_mask, SIGALRM);
239 action.sa_handler = sigalrm_handler;
240 sigaction(SIGALRM, &action, NULL);
241#else
242 signal(SIGALRM, sigalrm_handler);
243#endif /* HAVE_SIGSET */
244
245 alarm_tripped = 0;
246 count = 0;
247 compat = !strcmp(dent->filename, "smb");
248
249 alarm(30);
250
251 while (fgets(line, sizeof(line), fp) != NULL)
252 {
253 /*
254 * Reset the alarm clock...
255 */
256
257 alarm(30);
258
259 /*
260 * Each line is of the form:
261 *
262 * class URI "make model" "name" ["1284 device ID"]
263 */
264
265 device_id[0] = '\0';
266
267 if (!strncasecmp(line, "Usage", 5))
268 compat = 1;
269 else if (sscanf(line,
480ef0fe 270 "%63s%1023s%*[ \t]\"%255[^\"]\"%*[ \t]\"%127[^\"]\""
ef416fc2 271 "%*[ \t]\"%1023[^\"]",
272 dclass, uri, make_model, info, device_id) < 4)
273 {
274 /*
275 * Bad format; strip trailing newline and write an error message.
276 */
277
278 if (line[strlen(line) - 1] == '\n')
279 line[strlen(line) - 1] = '\0';
280
281 fprintf(stderr, "ERROR: [cups-deviced] Bad line from \"%s\": %s\n",
282 dent->filename, line);
283 compat = 1;
284 break;
285 }
286 else
287 {
288 /*
289 * Add the device to the array of available devices...
290 */
291
292 dev = add_dev(dclass, make_model, info, uri, device_id);
293 if (!dev)
294 {
295 cupsDirClose(dir);
296 return (1);
297 }
298
e00b005a 299 fprintf(stderr, "DEBUG: [cups-deviced] Added device \"%s\"...\n",
300 uri);
ef416fc2 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)
e00b005a 312 fprintf(stderr, "WARNING: [cups-deviced] Backend \"%s\" did not "
313 "respond within 30 seconds!\n", dent->filename);
ef416fc2 314
315 pclose(fp);
316
317 /*
318 * Hack for backends that don't support the CUPS 1.1 calling convention:
319 * add a network device with the method == backend name.
320 */
321
322 if (count == 0 && compat)
323 {
324 snprintf(line, sizeof(line), "Unknown Network Device (%s)",
325 dent->filename);
326
327 dev = add_dev("network", line, "Unknown", dent->filename, "");
328 if (!dev)
329 {
330 cupsDirClose(dir);
331 return (1);
332 }
333
e00b005a 334 fprintf(stderr, "DEBUG: [cups-deviced] Compatibility device "
335 "\"%s\"...\n", dent->filename);
ef416fc2 336 }
337 }
338 else
e00b005a 339 fprintf(stderr, "WARNING: [cups-deviced] Unable to execute \"%s\" "
340 "backend: %s\n", dent->filename, strerror(errno));
ef416fc2 341 }
342
343 cupsDirClose(dir);
344
e00b005a 345 /*
346 * Switch back to root as needed...
347 */
348
349 if (!getuid() && geteuid())
350 seteuid(0);
351
ef416fc2 352 /*
353 * Output the list of devices...
354 */
355
356 puts("Content-Type: application/ipp\n");
357
e00b005a 358 cupsdSendIPPHeader(IPP_OK, request_id);
ef416fc2 359 cupsdSendIPPGroup(IPP_TAG_OPERATION);
360 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
361 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", "en-US");
362
363 if ((count = atoi(argv[2])) <= 0)
364 count = cupsArrayCount(devs);
365
366 if (count > cupsArrayCount(devs))
367 count = cupsArrayCount(devs);
368
369 for (dev = (dev_info_t *)cupsArrayFirst(devs);
370 count > 0;
371 count --, dev = (dev_info_t *)cupsArrayNext(devs))
372 {
373 /*
374 * Add strings to attributes...
375 */
376
377 cupsdSendIPPGroup(IPP_TAG_PRINTER);
378 if (send_class)
379 cupsdSendIPPString(IPP_TAG_KEYWORD, "device-class", dev->device_class);
380 if (send_info)
381 cupsdSendIPPString(IPP_TAG_TEXT, "device-info", dev->device_info);
382 if (send_make_and_model)
383 cupsdSendIPPString(IPP_TAG_TEXT, "device-make-and-model",
384 dev->device_make_and_model);
385 if (send_uri)
386 cupsdSendIPPString(IPP_TAG_URI, "device-uri", dev->device_uri);
387 if (send_id)
388 cupsdSendIPPString(IPP_TAG_TEXT, "device-id", dev->device_id);
389 }
390
391 cupsdSendIPPTrailer();
392
393 /*
394 * Free the devices array and return...
395 */
396
397 for (dev = (dev_info_t *)cupsArrayFirst(devs);
398 dev;
399 dev = (dev_info_t *)cupsArrayNext(devs))
400 free(dev);
401
402 cupsArrayDelete(devs);
403
404 return (0);
405}
406
407
408/*
409 * 'add_dev()' - Add a new device to the list.
410 */
411
412static dev_info_t * /* O - New device or NULL on error */
413add_dev(
414 const char *device_class, /* I - Device class */
415 const char *device_make_and_model, /* I - Device make and model */
416 const char *device_info, /* I - Device information */
417 const char *device_uri, /* I - Device URI */
418 const char *device_id) /* I - 1284 device ID */
419{
420 dev_info_t *dev; /* New device */
421
422
423 /*
424 * Allocate memory for the device record...
425 */
426
427 if ((dev = calloc(1, sizeof(dev_info_t))) == NULL)
428 {
429 fputs("ERROR: [cups-deviced] Ran out of memory allocating a device!\n",
430 stderr);
431 return (NULL);
432 }
433
434 /*
435 * Copy the strings over...
436 */
437
438 strlcpy(dev->device_class, device_class, sizeof(dev->device_class));
439 strlcpy(dev->device_make_and_model, device_make_and_model,
440 sizeof(dev->device_make_and_model));
441 strlcpy(dev->device_info, device_info, sizeof(dev->device_info));
442 strlcpy(dev->device_uri, device_uri, sizeof(dev->device_uri));
443 strlcpy(dev->device_id, device_id, sizeof(dev->device_id));
444
445 /*
446 * Add the device to the array and return...
447 */
448
449 cupsArrayAdd(devs, dev);
450
451 return (dev);
452}
453
454
455/*
456 * 'compare_devs()' - Compare device names for sorting.
457 */
458
459static int /* O - Result of comparison */
460compare_devs(dev_info_t *d0, /* I - First device */
461 dev_info_t *d1) /* I - Second device */
462{
463 int diff; /* Difference between strings */
464
465
466 /*
467 * Sort devices by device-info, device-class, and device-uri...
468 */
469
470 if ((diff = cupsdCompareNames(d0->device_info, d1->device_info)) != 0)
471 return (diff);
472 else if ((diff = strcasecmp(d0->device_class, d1->device_class)) != 0)
473 return (diff);
474 else
475 return (strcasecmp(d0->device_uri, d1->device_uri));
476}
477
478
479/*
480 * 'sigalrm_handler()' - Handle alarm signals for backends that get hung
481 * trying to list the available devices...
482 */
483
484static void
485sigalrm_handler(int sig) /* I - Signal number */
486{
487 (void)sig; /* remove compiler warnings... */
488
489 alarm_tripped = 1;
490}
491
492
493/*
bc44d920 494 * End of "$Id: cups-deviced.c 6649 2007-07-11 21:46:42Z mike $".
ef416fc2 495 */