2 * "$Id: cups-deviced.c 4881 2005-12-15 22:03:40Z mike $"
4 * Device scanning mini-daemon for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2005 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * main() - Scan for devices and return an IPP response.
27 * add_dev() - Add a new device to the list.
28 * compare_devs() - Compare device names for sorting.
29 * sigalrm_handler() - Handle alarm signals for backends that get hung
33 * Include necessary headers...
37 #include <cups/array.h>
42 * Device information structure...
47 char device_class[128], /* Device class */
48 device_make_and_model[128], /* Make and model, if known */
49 device_info[128], /* Device info/description */
50 device_uri[1024], /* Device URI */
51 device_id[1024]; /* 1284 Device ID */
59 static int alarm_tripped; /* Non-zero if alarm was tripped */
60 static cups_array_t *devs; /* Device info */
67 static dev_info_t *add_dev(const char *device_class,
68 const char *device_make_and_model,
69 const char *device_info,
70 const char *device_uri,
71 const char *device_id);
72 static int compare_devs(dev_info_t *p0, dev_info_t *p1);
73 static void sigalrm_handler(int sig);
77 * 'main()' - Scan for devices and return an IPP response.
81 * cups-deviced request_id limit options
84 int /* O - Exit code */
85 main(int argc, /* I - Number of command-line args */
86 char *argv[]) /* I - Command-line arguments */
88 const char *server_bin; /* CUPS_SERVERBIN environment variable */
89 char backends[1024]; /* Location of backends */
90 int count; /* Number of devices from backend */
91 int compat; /* Compatibility device? */
92 FILE *fp; /* Pipe to device backend */
93 cups_dir_t *dir; /* Directory pointer */
94 cups_dentry_t *dent; /* Directory entry */
95 char filename[1024], /* Name of backend */
96 line[2048], /* Line from backend */
97 dclass[64], /* Device class */
98 uri[1024], /* Device URI */
99 info[128], /* Device info */
100 make_model[256], /* Make and model */
101 device_id[1024]; /* 1284 device ID */
102 int num_options; /* Number of options */
103 cups_option_t *options; /* Options */
104 const char *requested; /* requested-attributes option */
105 int send_class, /* Send device-class attribute? */
106 send_info, /* Send device-info attribute? */
107 send_make_and_model, /* Send device-make-and-model attribute? */
108 send_uri, /* Send device-uri attribute? */
109 send_id; /* Send device-id attribute? */
110 dev_info_t *dev; /* Current device */
111 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
112 struct sigaction action; /* Actions for POSIX signals */
113 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
117 * Check the command-line...
122 fputs("Usage: cups-deviced request_id limit options\n", stderr);
126 num_options = cupsParseOptions(argv[3], 0, &options);
127 requested = cupsGetOption("requested-attributes", num_options, options);
129 if (!requested || strstr(requested, "all"))
133 send_make_and_model = 1;
139 send_class = strstr(requested, "device-class") != NULL;
140 send_info = strstr(requested, "device-info") != NULL;
141 send_make_and_model = strstr(requested, "device-make-and-model") != NULL;
142 send_uri = strstr(requested, "device-uri") != NULL;
143 send_id = strstr(requested, "device-id") != NULL;
147 * Try opening the backend directory...
150 if ((server_bin = getenv("CUPS_SERVERBIN")) == NULL)
151 server_bin = CUPS_SERVERBIN;
153 snprintf(backends, sizeof(backends), "%s/backend", server_bin);
155 if ((dir = cupsDirOpen(backends)) == NULL)
157 fprintf(stderr, "ERROR: [cups-deviced] Unable to open backend directory \"%s\": %s",
158 backends, strerror(errno));
163 * Setup the devices array...
166 devs = cupsArrayNew((cups_array_func_t)compare_devs, NULL);
169 * Loop through all of the device backends...
172 while ((dent = cupsDirRead(dir)) != NULL)
175 * Run the backend with no arguments and collect the output...
178 snprintf(filename, sizeof(filename), "%s/%s", backends, dent->filename);
179 if ((fp = popen(filename, "r")) != NULL)
182 * Set an alarm for the first read from the backend; this avoids
183 * problems when a backend is hung getting device information.
186 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
187 sigset(SIGALRM, sigalrm_handler);
188 #elif defined(HAVE_SIGACTION)
189 memset(&action, 0, sizeof(action));
191 sigemptyset(&action.sa_mask);
192 sigaddset(&action.sa_mask, SIGALRM);
193 action.sa_handler = sigalrm_handler;
194 sigaction(SIGALRM, &action, NULL);
196 signal(SIGALRM, sigalrm_handler);
197 #endif /* HAVE_SIGSET */
201 compat = !strcmp(dent->filename, "smb");
205 while (fgets(line, sizeof(line), fp) != NULL)
208 * Reset the alarm clock...
214 * Each line is of the form:
216 * class URI "make model" "name" ["1284 device ID"]
221 if (!strncasecmp(line, "Usage", 5))
223 else if (sscanf(line,
224 "%63s%1023s%*[ \t]\"%255[^\"]\"%*[ \t]\"%127[^\"]"
225 "%*[ \t]\"%1023[^\"]",
226 dclass, uri, make_model, info, device_id) < 4)
229 * Bad format; strip trailing newline and write an error message.
232 if (line[strlen(line) - 1] == '\n')
233 line[strlen(line) - 1] = '\0';
235 fprintf(stderr, "ERROR: [cups-deviced] Bad line from \"%s\": %s\n",
236 dent->filename, line);
243 * Add the device to the array of available devices...
246 dev = add_dev(dclass, make_model, info, uri, device_id);
253 fprintf(stderr, "DEBUG: [cups-deviced] Added device \"%s\"...\n", uri);
259 * Turn the alarm clock off and close the pipe to the command...
265 fprintf(stderr, "WARNING: [cups-deviced] Backend \"%s\" did not respond within 30 seconds!\n",
271 * Hack for backends that don't support the CUPS 1.1 calling convention:
272 * add a network device with the method == backend name.
275 if (count == 0 && compat)
277 snprintf(line, sizeof(line), "Unknown Network Device (%s)",
280 dev = add_dev("network", line, "Unknown", dent->filename, "");
287 fprintf(stderr, "DEBUG: [cups-deviced] Compatibility device \"%s\"...\n",
292 fprintf(stderr, "WARNING: [cups-deviced] Unable to execute \"%s\" backend: %s\n",
293 dent->filename, strerror(errno));
299 * Output the list of devices...
302 puts("Content-Type: application/ipp\n");
304 cupsdSendIPPHeader(IPP_OK, atoi(argv[1]));
305 cupsdSendIPPGroup(IPP_TAG_OPERATION);
306 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
307 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", "en-US");
309 if ((count = atoi(argv[2])) <= 0)
310 count = cupsArrayCount(devs);
312 if (count > cupsArrayCount(devs))
313 count = cupsArrayCount(devs);
315 for (dev = (dev_info_t *)cupsArrayFirst(devs);
317 count --, dev = (dev_info_t *)cupsArrayNext(devs))
320 * Add strings to attributes...
323 cupsdSendIPPGroup(IPP_TAG_PRINTER);
325 cupsdSendIPPString(IPP_TAG_KEYWORD, "device-class", dev->device_class);
327 cupsdSendIPPString(IPP_TAG_TEXT, "device-info", dev->device_info);
328 if (send_make_and_model)
329 cupsdSendIPPString(IPP_TAG_TEXT, "device-make-and-model",
330 dev->device_make_and_model);
332 cupsdSendIPPString(IPP_TAG_URI, "device-uri", dev->device_uri);
334 cupsdSendIPPString(IPP_TAG_TEXT, "device-id", dev->device_id);
337 cupsdSendIPPTrailer();
340 * Free the devices array and return...
343 for (dev = (dev_info_t *)cupsArrayFirst(devs);
345 dev = (dev_info_t *)cupsArrayNext(devs))
348 cupsArrayDelete(devs);
355 * 'add_dev()' - Add a new device to the list.
358 static dev_info_t * /* O - New device or NULL on error */
360 const char *device_class, /* I - Device class */
361 const char *device_make_and_model, /* I - Device make and model */
362 const char *device_info, /* I - Device information */
363 const char *device_uri, /* I - Device URI */
364 const char *device_id) /* I - 1284 device ID */
366 dev_info_t *dev; /* New device */
370 * Allocate memory for the device record...
373 if ((dev = calloc(1, sizeof(dev_info_t))) == NULL)
375 fputs("ERROR: [cups-deviced] Ran out of memory allocating a device!\n",
381 * Copy the strings over...
384 strlcpy(dev->device_class, device_class, sizeof(dev->device_class));
385 strlcpy(dev->device_make_and_model, device_make_and_model,
386 sizeof(dev->device_make_and_model));
387 strlcpy(dev->device_info, device_info, sizeof(dev->device_info));
388 strlcpy(dev->device_uri, device_uri, sizeof(dev->device_uri));
389 strlcpy(dev->device_id, device_id, sizeof(dev->device_id));
392 * Add the device to the array and return...
395 cupsArrayAdd(devs, dev);
402 * 'compare_devs()' - Compare device names for sorting.
405 static int /* O - Result of comparison */
406 compare_devs(dev_info_t *d0, /* I - First device */
407 dev_info_t *d1) /* I - Second device */
409 int diff; /* Difference between strings */
413 * Sort devices by device-info, device-class, and device-uri...
416 if ((diff = cupsdCompareNames(d0->device_info, d1->device_info)) != 0)
418 else if ((diff = strcasecmp(d0->device_class, d1->device_class)) != 0)
421 return (strcasecmp(d0->device_uri, d1->device_uri));
426 * 'sigalrm_handler()' - Handle alarm signals for backends that get hung
427 * trying to list the available devices...
431 sigalrm_handler(int sig) /* I - Signal number */
433 (void)sig; /* remove compiler warnings... */
440 * End of "$Id: cups-deviced.c 4881 2005-12-15 22:03:40Z mike $".