]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/cups-deviced.c
2 * "$Id: cups-deviced.c 7816 2008-07-30 20:53:31Z mike $"
4 * Device scanning mini-daemon for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
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/".
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.
29 * Include necessary headers...
33 #include <cups/array.h>
44 #define MAX_BACKENDS 200 /* Maximum number of backends we'll run */
48 * Backend information...
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 */
62 * Device information structure...
67 char device_class
[128], /* Device class */
68 device_info
[128], /* Device info/description */
69 device_uri
[1024]; /* Device URI */
77 static int num_backends
= 0,
81 static cupsd_backend_t backends
[MAX_BACKENDS
];
82 /* Array of backends */
83 static struct pollfd backend_fds
[MAX_BACKENDS
];
84 /* Array for poll() */
85 static cups_array_t
*devices
; /* Array of devices */
86 static int normal_user
; /* Normal user ID */
87 static int device_limit
; /* Maximum number of devices */
88 static int send_class
, /* Send device-class attribute? */
89 send_info
, /* Send device-info attribute? */
91 /* Send device-make-and-model attribute? */
92 send_uri
, /* Send device-uri attribute? */
93 send_id
, /* Send device-id attribute? */
94 send_location
; /* Send device-location attribute? */
95 static int dead_children
= 0;
103 static int add_device(const char *device_class
,
104 const char *device_make_and_model
,
105 const char *device_info
,
106 const char *device_uri
,
107 const char *device_id
,
108 const char *device_location
);
109 static int compare_devices(cupsd_device_t
*p0
,
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
);
120 * 'main()' - Scan for devices and return an IPP response.
124 * cups-deviced request_id limit options
127 int /* O - Exit code */
128 main(int argc
, /* I - Number of command-line args */
129 char *argv
[]) /* I - Command-line arguments */
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 */
149 setbuf(stderr
, NULL
);
152 * Check the command-line...
157 fputs("Usage: cups-deviced request-id limit timeout user-id options\n", stderr
);
162 request_id
= atoi(argv
[1]);
165 fprintf(stderr
, "ERROR: [cups-deviced] Bad request ID %d!\n", request_id
);
170 device_limit
= atoi(argv
[2]);
171 if (device_limit
< 0)
173 fprintf(stderr
, "ERROR: [cups-deviced] Bad limit %d!\n", device_limit
);
178 timeout
= atoi(argv
[3]);
181 fprintf(stderr
, "ERROR: [cups-deviced] Bad timeout %d!\n", timeout
);
186 normal_user
= atoi(argv
[4]);
187 if (normal_user
<= 0)
189 fprintf(stderr
, "ERROR: [cups-deviced] Bad user %d!\n", normal_user
);
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
));
200 if (!requested
|| cupsArrayFind(requested
, "all") != NULL
)
202 send_class
= send_info
= send_make_and_model
= send_uri
= send_id
=
207 send_class
= cupsArrayFind(requested
, "device-class") != NULL
;
208 send_info
= cupsArrayFind(requested
, "device-info") != NULL
;
209 send_make_and_model
= cupsArrayFind(requested
, "device-make-and-model") != NULL
;
210 send_uri
= cupsArrayFind(requested
, "device-uri") != NULL
;
211 send_id
= cupsArrayFind(requested
, "device-id") != NULL
;
212 send_location
= cupsArrayFind(requested
, "device-location") != NULL
;
216 * Listen to child signals...
219 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
220 sigset(SIGCHLD
, sigchld_handler
);
221 #elif defined(HAVE_SIGACTION)
222 memset(&action
, 0, sizeof(action
));
224 sigemptyset(&action
.sa_mask
);
225 sigaddset(&action
.sa_mask
, SIGCHLD
);
226 action
.sa_handler
= sigchld_handler
;
227 sigaction(SIGCHLD
, &action
, NULL
);
229 signal(SIGCLD
, sigchld_handler
); /* No, SIGCLD isn't a typo... */
230 #endif /* HAVE_SIGSET */
233 * Try opening the backend directory...
236 if ((server_bin
= getenv("CUPS_SERVERBIN")) == NULL
)
237 server_bin
= CUPS_SERVERBIN
;
239 snprintf(filename
, sizeof(filename
), "%s/backend", server_bin
);
241 if ((dir
= cupsDirOpen(filename
)) == NULL
)
243 fprintf(stderr
, "ERROR: [cups-deviced] Unable to open backend directory "
244 "\"%s\": %s", filename
, strerror(errno
));
250 * Setup the devices array...
253 devices
= cupsArrayNew((cups_array_func_t
)compare_devices
, NULL
);
256 * Loop through all of the device backends...
259 while ((dent
= cupsDirRead(dir
)) != NULL
)
262 * Skip entries that are not executable files...
265 if (!S_ISREG(dent
->fileinfo
.st_mode
) ||
266 !isalnum(dent
->filename
[0] & 255) ||
267 (dent
->fileinfo
.st_mode
& (S_IRUSR
| S_IXUSR
)) != (S_IRUSR
| S_IXUSR
))
270 if (cupsArrayFind(exclude
, dent
->filename
))
274 * Backends without permissions for normal users run as root,
275 * all others run as the unprivileged user...
278 start_backend(dent
->filename
,
279 !(dent
->fileinfo
.st_mode
& (S_IRWXG
| S_IRWXO
)));
288 if (getenv("SOFTWARE"))
289 puts("Content-Type: application/ipp\n");
291 cupsdSendIPPHeader(IPP_OK
, request_id
);
292 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
293 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
294 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language", "en-US");
296 end_time
= get_current_time() + timeout
;
298 while (active_backends
> 0 && (current_time
= get_current_time()) < end_time
)
301 * Collect the output from the backends...
304 timeout
= (int)(1000 * (end_time
- current_time
));
306 if (poll(backend_fds
, num_backends
, timeout
) > 0)
308 for (i
= 0; i
< num_backends
; i
++)
309 if (backend_fds
[i
].revents
&& backends
[i
].pipe
)
310 if (get_device(backends
+ i
))
312 backend_fds
[i
].fd
= 0;
313 backend_fds
[i
].events
= 0;
318 * Get exit status from children...
325 cupsdSendIPPTrailer();
328 * Terminate any remaining backends and exit...
331 if (active_backends
> 0)
333 for (i
= 0; i
< num_backends
; i
++)
335 kill(backends
[i
].pid
, SIGTERM
);
343 * 'add_device()' - Add a new device to the list.
346 static int /* O - 0 on success, -1 on error */
348 const char *device_class
, /* I - Device class */
349 const char *device_make_and_model
, /* I - Device make and model */
350 const char *device_info
, /* I - Device information */
351 const char *device_uri
, /* I - Device URI */
352 const char *device_id
, /* I - 1284 device ID */
353 const char *device_location
) /* I - Physical location */
355 cupsd_device_t
*device
; /* New device */
359 * Allocate memory for the device record...
362 if ((device
= calloc(1, sizeof(cupsd_device_t
))) == NULL
)
364 fputs("ERROR: [cups-deviced] Ran out of memory allocating a device!\n",
370 * Copy the strings over...
373 strlcpy(device
->device_class
, device_class
, sizeof(device
->device_class
));
374 strlcpy(device
->device_info
, device_info
, sizeof(device
->device_info
));
375 strlcpy(device
->device_uri
, device_uri
, sizeof(device
->device_uri
));
378 * Add the device to the array and return...
381 if (cupsArrayFind(devices
, device
))
391 cupsArrayAdd(devices
, device
);
393 if (device_limit
<= 0 || cupsArrayCount(devices
) < device_limit
)
396 * Send device info...
399 cupsdSendIPPGroup(IPP_TAG_PRINTER
);
401 cupsdSendIPPString(IPP_TAG_KEYWORD
, "device-class",
404 cupsdSendIPPString(IPP_TAG_TEXT
, "device-info", device_info
);
405 if (send_make_and_model
)
406 cupsdSendIPPString(IPP_TAG_TEXT
, "device-make-and-model",
407 device_make_and_model
);
409 cupsdSendIPPString(IPP_TAG_URI
, "device-uri", device_uri
);
411 cupsdSendIPPString(IPP_TAG_TEXT
, "device-id",
412 device_id
? device_id
: "");
414 cupsdSendIPPString(IPP_TAG_TEXT
, "device-location",
415 device_location
? device_location
: "");
418 fputs("DEBUG: Flushed attributes...\n", stderr
);
427 * 'compare_devices()' - Compare device names to eliminate duplicates.
430 static int /* O - Result of comparison */
431 compare_devices(cupsd_device_t
*d0
, /* I - First device */
432 cupsd_device_t
*d1
) /* I - Second device */
434 int diff
; /* Difference between strings */
438 * Sort devices by device-info, device-class, and device-uri...
441 if ((diff
= cupsdCompareNames(d0
->device_info
, d1
->device_info
)) != 0)
443 else if ((diff
= strcasecmp(d0
->device_class
, d1
->device_class
)) != 0)
446 return (strcasecmp(d0
->device_uri
, d1
->device_uri
));
451 * 'create_strings_array()' - Create a CUPS array of strings.
454 static cups_array_t
* /* O - CUPS array */
455 create_strings_array(const char *s
) /* I - Comma-delimited strings */
457 cups_array_t
*a
; /* CUPS array */
458 const char *start
, /* Start of string */
459 *end
; /* End of string */
460 char *ptr
; /* New string */
466 if ((a
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
)) != NULL
)
468 for (start
= end
= s
; *end
; start
= end
+ 1)
471 * Find the end of the current delimited string...
474 if ((end
= strchr(start
, ',')) == NULL
)
475 end
= start
+ strlen(start
);
478 * Duplicate the string and add it to the array...
481 if ((ptr
= calloc(1, end
- start
+ 1)) == NULL
)
484 memcpy(ptr
, start
, end
- start
);
485 cupsArrayAdd(a
, ptr
);
494 * 'get_current_time()' - Get the current time as a double value in seconds.
497 static double /* O - Time in seconds */
498 get_current_time(void)
500 struct timeval curtime
; /* Current time */
503 gettimeofday(&curtime
, NULL
);
505 return (curtime
.tv_sec
+ 0.000001 * curtime
.tv_usec
);
510 * 'get_device()' - Get a device from a backend.
513 static int /* O - 0 on success, -1 on error */
514 get_device(cupsd_backend_t
*backend
) /* I - Backend to read from */
516 char line
[2048], /* Line from backend */
517 temp
[2048], /* Copy of line */
518 *ptr
, /* Pointer into line */
519 *dclass
, /* Device class */
520 *uri
, /* Device URI */
521 *make_model
, /* Make and model */
522 *info
, /* Device info */
523 *device_id
, /* 1284 device ID */
524 *location
; /* Physical location */
527 if (cupsFileGets(backend
->pipe
, line
, sizeof(line
)))
530 * Each line is of the form:
532 * class URI "make model" "name" ["1284 device ID"] ["location"]
535 strlcpy(temp
, line
, sizeof(temp
));
543 for (ptr
= temp
; *ptr
; ptr
++)
544 if (isspace(*ptr
& 255))
547 while (isspace(*ptr
& 255))
557 for (uri
= ptr
; *ptr
; ptr
++)
558 if (isspace(*ptr
& 255))
561 while (isspace(*ptr
& 255))
565 * device-make-and-model
571 for (ptr
++, make_model
= ptr
; *ptr
&& *ptr
!= '\"'; ptr
++)
573 if (*ptr
== '\\' && ptr
[1])
574 _cups_strcpy(ptr
, ptr
+ 1);
580 for (*ptr
++ = '\0'; isspace(*ptr
& 255); *ptr
++ = '\0');
589 for (ptr
++, info
= ptr
; *ptr
&& *ptr
!= '\"'; ptr
++)
591 if (*ptr
== '\\' && ptr
[1])
592 _cups_strcpy(ptr
, ptr
+ 1);
598 for (*ptr
++ = '\0'; isspace(*ptr
& 255); *ptr
++ = '\0');
606 for (ptr
++, device_id
= ptr
; *ptr
&& *ptr
!= '\"'; ptr
++)
608 if (*ptr
== '\\' && ptr
[1])
609 _cups_strcpy(ptr
, ptr
+ 1);
615 for (*ptr
++ = '\0'; isspace(*ptr
& 255); *ptr
++ = '\0');
623 for (ptr
++, location
= ptr
; *ptr
&& *ptr
!= '\"'; ptr
++)
625 if (*ptr
== '\\' && ptr
[1])
626 _cups_strcpy(ptr
, ptr
+ 1);
644 * Add the device to the array of available devices...
647 if (!add_device(dclass
, make_model
, info
, uri
, device_id
, location
))
648 fprintf(stderr
, "DEBUG: [cups-deviced] Found device \"%s\"...\n", uri
);
657 cupsFileClose(backend
->pipe
);
658 backend
->pipe
= NULL
;
663 * Bad format; strip trailing newline and write an error message.
668 if (line
[strlen(line
) - 1] == '\n')
669 line
[strlen(line
) - 1] = '\0';
671 fprintf(stderr
, "ERROR: [cups-deviced] Bad line from \"%s\": %s\n",
672 backend
->name
, line
);
678 * 'process_children()' - Process all dead children...
682 process_children(void)
684 int i
; /* Looping var */
685 int status
; /* Exit status of child */
686 int pid
; /* Process ID of child */
687 cupsd_backend_t
*backend
; /* Current backend */
688 const char *name
; /* Name of process */
692 * Reset the dead_children flag...
698 * Collect the exit status of some children...
702 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0)
703 #elif defined(HAVE_WAIT3)
704 while ((pid
= wait3(&status
, WNOHANG
, NULL
)) > 0)
706 if ((pid
= wait(&status
)) > 0)
707 #endif /* HAVE_WAITPID */
709 if (status
== SIGTERM
)
712 for (i
= num_backends
, backend
= backends
; i
> 0; i
--, backend
++)
713 if (backend
->pid
== pid
)
718 name
= backend
->name
;
720 backend
->status
= status
;
729 if (WIFEXITED(status
))
731 "ERROR: [cups-deviced] PID %d (%s) stopped with status %d!\n",
732 pid
, name
, WEXITSTATUS(status
));
735 "ERROR: [cups-deviced] PID %d (%s) crashed on signal %d!\n",
736 pid
, name
, WTERMSIG(status
));
740 "DEBUG: [cups-deviced] PID %d (%s) exited with no errors.\n",
747 * 'sigchld_handler()' - Handle 'child' signals from old processes.
751 sigchld_handler(int sig
) /* I - Signal number */
756 * Flag that we have dead children...
762 * Reset the signal handler as needed...
765 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
766 signal(SIGCLD
, sigchld_handler
);
767 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
772 * 'start_backend()' - Run a backend to gather the available devices.
775 static int /* O - 0 on success, -1 on error */
776 start_backend(const char *name
, /* I - Backend to run */
777 int root
) /* I - Run as root? */
779 const char *server_bin
; /* CUPS_SERVERBIN environment variable */
780 char program
[1024]; /* Full path to backend */
781 cupsd_backend_t
*backend
; /* Current backend */
782 char *argv
[2]; /* Command-line arguments */
785 if (num_backends
>= MAX_BACKENDS
)
787 fprintf(stderr
, "ERROR: Too many backends (%d)!\n", num_backends
);
791 if ((server_bin
= getenv("CUPS_SERVERBIN")) == NULL
)
792 server_bin
= CUPS_SERVERBIN
;
794 snprintf(program
, sizeof(program
), "%s/backend/%s", server_bin
, name
);
796 backend
= backends
+ num_backends
;
798 argv
[0] = (char *)name
;
801 if ((backend
->pipe
= cupsdPipeCommand(&(backend
->pid
), program
, argv
,
802 root
? 0 : normal_user
)) == NULL
)
804 fprintf(stderr
, "ERROR: [cups-deviced] Unable to execute \"%s\" - %s\n",
805 program
, strerror(errno
));
810 * Fill in the rest of the backend information...
813 fprintf(stderr
, "DEBUG: [cups-deviced] Started backend %s (PID %d)\n",
814 program
, backend
->pid
);
816 backend_fds
[num_backends
].fd
= cupsFileNumber(backend
->pipe
);
817 backend_fds
[num_backends
].events
= POLLIN
;
819 backend
->name
= strdup(name
);
831 * End of "$Id: cups-deviced.c 7816 2008-07-30 20:53:31Z mike $".