]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/cups-deviced.c
2 * Device scanning mini-daemon for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers...
19 #include <cups/array.h>
30 #define MAX_BACKENDS 200 /* Maximum number of backends we'll run */
34 * Backend information...
39 char *name
; /* Name of backend */
40 int pid
, /* Process ID */
41 status
; /* Exit status */
42 cups_file_t
*pipe
; /* Pipe from backend stdout */
43 int count
; /* Number of devices found */
48 * Device information structure...
53 char device_class
[128], /* Device class */
54 device_info
[128], /* Device info/description */
55 device_uri
[1024]; /* Device URI */
63 static int num_backends
= 0,
67 static cupsd_backend_t backends
[MAX_BACKENDS
];
68 /* Array of backends */
69 static struct pollfd backend_fds
[MAX_BACKENDS
];
70 /* Array for poll() */
71 static cups_array_t
*devices
; /* Array of devices */
72 static uid_t normal_user
; /* Normal user ID */
73 static int device_limit
; /* Maximum number of devices */
74 static int send_class
, /* Send device-class attribute? */
75 send_info
, /* Send device-info attribute? */
77 /* Send device-make-and-model attribute? */
78 send_uri
, /* Send device-uri attribute? */
79 send_id
, /* Send device-id attribute? */
80 send_location
; /* Send device-location attribute? */
81 static int dead_children
= 0;
89 static int add_device(const char *device_class
,
90 const char *device_make_and_model
,
91 const char *device_info
,
92 const char *device_uri
,
93 const char *device_id
,
94 const char *device_location
);
95 static int compare_devices(cupsd_device_t
*p0
,
97 static double get_current_time(void);
98 static int get_device(cupsd_backend_t
*backend
);
99 static void process_children(void);
100 static void sigchld_handler(int sig
);
101 static int start_backend(const char *backend
, int root
);
105 * 'main()' - Scan for devices and return an IPP response.
109 * cups-deviced request_id limit options
112 int /* O - Exit code */
113 main(int argc
, /* I - Number of command-line args */
114 char *argv
[]) /* I - Command-line arguments */
116 int i
; /* Looping var */
117 int request_id
; /* Request ID */
118 int timeout
; /* Timeout in seconds */
119 const char *server_bin
; /* CUPS_SERVERBIN environment variable */
120 char filename
[1024]; /* Backend directory filename */
121 cups_dir_t
*dir
; /* Directory pointer */
122 cups_dentry_t
*dent
; /* Directory entry */
123 double current_time
, /* Current time */
124 end_time
; /* Ending time */
125 int num_options
; /* Number of options */
126 cups_option_t
*options
; /* Options */
127 cups_array_t
*requested
, /* requested-attributes values */
128 *exclude
, /* exclude-schemes values */
129 *include
; /* include-schemes values */
130 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
131 struct sigaction action
; /* Actions for POSIX signals */
132 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
135 setbuf(stderr
, NULL
);
138 * Check the command-line...
143 fputs("Usage: cups-deviced request-id limit timeout user-id options\n", stderr
);
148 request_id
= atoi(argv
[1]);
151 fprintf(stderr
, "ERROR: [cups-deviced] Bad request ID %d!\n", request_id
);
156 device_limit
= atoi(argv
[2]);
157 if (device_limit
< 0)
159 fprintf(stderr
, "ERROR: [cups-deviced] Bad limit %d!\n", device_limit
);
164 timeout
= atoi(argv
[3]);
167 fprintf(stderr
, "ERROR: [cups-deviced] Bad timeout %d!\n", timeout
);
172 normal_user
= (uid_t
)atoi(argv
[4]);
173 if (normal_user
<= 0)
175 fprintf(stderr
, "ERROR: [cups-deviced] Bad user %d!\n", normal_user
);
180 num_options
= cupsParseOptions(argv
[5], 0, &options
);
181 requested
= cupsdCreateStringsArray(cupsGetOption("requested-attributes",
182 num_options
, options
));
183 exclude
= cupsdCreateStringsArray(cupsGetOption("exclude-schemes",
184 num_options
, options
));
185 include
= cupsdCreateStringsArray(cupsGetOption("include-schemes",
186 num_options
, options
));
188 if (!requested
|| cupsArrayFind(requested
, "all") != NULL
)
190 send_class
= send_info
= send_make_and_model
= send_uri
= send_id
=
195 send_class
= cupsArrayFind(requested
, "device-class") != NULL
;
196 send_info
= cupsArrayFind(requested
, "device-info") != NULL
;
197 send_make_and_model
= cupsArrayFind(requested
, "device-make-and-model") != NULL
;
198 send_uri
= cupsArrayFind(requested
, "device-uri") != NULL
;
199 send_id
= cupsArrayFind(requested
, "device-id") != NULL
;
200 send_location
= cupsArrayFind(requested
, "device-location") != NULL
;
204 * Listen to child signals...
207 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
208 sigset(SIGCHLD
, sigchld_handler
);
209 #elif defined(HAVE_SIGACTION)
210 memset(&action
, 0, sizeof(action
));
212 sigemptyset(&action
.sa_mask
);
213 sigaddset(&action
.sa_mask
, SIGCHLD
);
214 action
.sa_handler
= sigchld_handler
;
215 sigaction(SIGCHLD
, &action
, NULL
);
217 signal(SIGCLD
, sigchld_handler
); /* No, SIGCLD isn't a typo... */
218 #endif /* HAVE_SIGSET */
221 * Try opening the backend directory...
224 if ((server_bin
= getenv("CUPS_SERVERBIN")) == NULL
)
225 server_bin
= CUPS_SERVERBIN
;
227 snprintf(filename
, sizeof(filename
), "%s/backend", server_bin
);
229 if ((dir
= cupsDirOpen(filename
)) == NULL
)
231 fprintf(stderr
, "ERROR: [cups-deviced] Unable to open backend directory "
232 "\"%s\": %s", filename
, strerror(errno
));
238 * Setup the devices array...
241 devices
= cupsArrayNew((cups_array_func_t
)compare_devices
, NULL
);
244 * Loop through all of the device backends...
247 while ((dent
= cupsDirRead(dir
)) != NULL
)
250 * Skip entries that are not executable files...
253 if (!S_ISREG(dent
->fileinfo
.st_mode
) ||
254 !isalnum(dent
->filename
[0] & 255) ||
255 (dent
->fileinfo
.st_mode
& (S_IRUSR
| S_IXUSR
)) != (S_IRUSR
| S_IXUSR
))
259 * Skip excluded or not included backends...
262 if (cupsArrayFind(exclude
, dent
->filename
) ||
263 (include
&& !cupsArrayFind(include
, dent
->filename
)))
267 * Backends without permissions for normal users run as root,
268 * all others run as the unprivileged user...
271 start_backend(dent
->filename
, !(dent
->fileinfo
.st_mode
& (S_IWGRP
| S_IRWXO
)));
280 if (getenv("SOFTWARE"))
281 puts("Content-Type: application/ipp\n");
283 cupsdSendIPPHeader(IPP_OK
, request_id
);
284 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
285 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
286 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language", "en-US");
288 end_time
= get_current_time() + timeout
;
290 while (active_backends
> 0 && (current_time
= get_current_time()) < end_time
)
293 * Collect the output from the backends...
296 timeout
= (int)(1000 * (end_time
- current_time
));
298 if (poll(backend_fds
, (nfds_t
)num_backends
, timeout
) > 0)
300 for (i
= 0; i
< num_backends
; i
++)
301 if (backend_fds
[i
].revents
&& backends
[i
].pipe
)
303 cups_file_t
*bpipe
= backends
[i
].pipe
;
304 /* Copy of pipe for backend... */
308 if (get_device(backends
+ i
))
310 backend_fds
[i
].fd
= 0;
311 backend_fds
[i
].events
= 0;
315 while (bpipe
->ptr
&& memchr(bpipe
->ptr
, '\n', (size_t)(bpipe
->end
- bpipe
->ptr
)));
320 * Get exit status from children...
327 cupsdSendIPPTrailer();
330 * Terminate any remaining backends and exit...
333 if (active_backends
> 0)
335 for (i
= 0; i
< num_backends
; i
++)
337 kill(backends
[i
].pid
, SIGTERM
);
345 * 'add_device()' - Add a new device to the list.
348 static int /* O - 0 on success, -1 on error */
350 const char *device_class
, /* I - Device class */
351 const char *device_make_and_model
, /* I - Device make and model */
352 const char *device_info
, /* I - Device information */
353 const char *device_uri
, /* I - Device URI */
354 const char *device_id
, /* I - 1284 device ID */
355 const char *device_location
) /* I - Physical location */
357 cupsd_device_t
*device
; /* New device */
361 * Allocate memory for the device record...
364 if ((device
= calloc(1, sizeof(cupsd_device_t
))) == NULL
)
366 fputs("ERROR: [cups-deviced] Ran out of memory allocating a device!\n",
372 * Copy the strings over...
375 strlcpy(device
->device_class
, device_class
, sizeof(device
->device_class
));
376 strlcpy(device
->device_info
, device_info
, sizeof(device
->device_info
));
377 strlcpy(device
->device_uri
, device_uri
, sizeof(device
->device_uri
));
380 * Add the device to the array and return...
383 if (cupsArrayFind(devices
, device
))
393 cupsArrayAdd(devices
, device
);
395 if (device_limit
<= 0 || cupsArrayCount(devices
) < device_limit
)
398 * Send device info...
401 cupsdSendIPPGroup(IPP_TAG_PRINTER
);
403 cupsdSendIPPString(IPP_TAG_KEYWORD
, "device-class",
406 cupsdSendIPPString(IPP_TAG_TEXT
, "device-info", device_info
);
407 if (send_make_and_model
)
408 cupsdSendIPPString(IPP_TAG_TEXT
, "device-make-and-model",
409 device_make_and_model
);
411 cupsdSendIPPString(IPP_TAG_URI
, "device-uri", device_uri
);
413 cupsdSendIPPString(IPP_TAG_TEXT
, "device-id",
414 device_id
? device_id
: "");
416 cupsdSendIPPString(IPP_TAG_TEXT
, "device-location",
417 device_location
? device_location
: "");
420 fputs("DEBUG: Flushed attributes...\n", stderr
);
429 * 'compare_devices()' - Compare device names to eliminate duplicates.
432 static int /* O - Result of comparison */
433 compare_devices(cupsd_device_t
*d0
, /* I - First device */
434 cupsd_device_t
*d1
) /* I - Second device */
436 int diff
; /* Difference between strings */
440 * Sort devices by device-info, device-class, and device-uri...
443 if ((diff
= cupsdCompareNames(d0
->device_info
, d1
->device_info
)) != 0)
445 else if ((diff
= _cups_strcasecmp(d0
->device_class
, d1
->device_class
)) != 0)
448 return (_cups_strcasecmp(d0
->device_uri
, d1
->device_uri
));
453 * 'get_current_time()' - Get the current time as a double value in seconds.
456 static double /* O - Time in seconds */
457 get_current_time(void)
459 struct timeval curtime
; /* Current time */
462 gettimeofday(&curtime
, NULL
);
464 return (curtime
.tv_sec
+ 0.000001 * curtime
.tv_usec
);
469 * 'get_device()' - Get a device from a backend.
472 static int /* O - 0 on success, -1 on error */
473 get_device(cupsd_backend_t
*backend
) /* I - Backend to read from */
475 char line
[2048], /* Line from backend */
476 temp
[2048], /* Copy of line */
477 *ptr
, /* Pointer into line */
478 *dclass
, /* Device class */
479 *uri
, /* Device URI */
480 *make_model
, /* Make and model */
481 *info
, /* Device info */
482 *device_id
, /* 1284 device ID */
483 *location
; /* Physical location */
486 if (cupsFileGets(backend
->pipe
, line
, sizeof(line
)))
489 * Each line is of the form:
491 * class URI "make model" "name" ["1284 device ID"] ["location"]
494 strlcpy(temp
, line
, sizeof(temp
));
502 for (ptr
= temp
; *ptr
; ptr
++)
503 if (isspace(*ptr
& 255))
506 while (isspace(*ptr
& 255))
516 for (uri
= ptr
; *ptr
; ptr
++)
517 if (isspace(*ptr
& 255))
520 while (isspace(*ptr
& 255))
524 * device-make-and-model
530 for (ptr
++, make_model
= ptr
; *ptr
&& *ptr
!= '\"'; ptr
++)
532 if (*ptr
== '\\' && ptr
[1])
533 _cups_strcpy(ptr
, ptr
+ 1);
539 for (*ptr
++ = '\0'; isspace(*ptr
& 255); *ptr
++ = '\0');
548 for (ptr
++, info
= ptr
; *ptr
&& *ptr
!= '\"'; ptr
++)
550 if (*ptr
== '\\' && ptr
[1])
551 _cups_strcpy(ptr
, ptr
+ 1);
557 for (*ptr
++ = '\0'; isspace(*ptr
& 255); *ptr
++ = '\0');
565 for (ptr
++, device_id
= ptr
; *ptr
&& *ptr
!= '\"'; ptr
++)
567 if (*ptr
== '\\' && ptr
[1])
568 _cups_strcpy(ptr
, ptr
+ 1);
574 for (*ptr
++ = '\0'; isspace(*ptr
& 255); *ptr
++ = '\0');
582 for (ptr
++, location
= ptr
; *ptr
&& *ptr
!= '\"'; ptr
++)
584 if (*ptr
== '\\' && ptr
[1])
585 _cups_strcpy(ptr
, ptr
+ 1);
603 * Add the device to the array of available devices...
606 if (!add_device(dclass
, make_model
, info
, uri
, device_id
, location
))
607 fprintf(stderr
, "DEBUG: [cups-deviced] Found device \"%s\"...\n", uri
);
616 cupsFileClose(backend
->pipe
);
617 backend
->pipe
= NULL
;
622 * Bad format; strip trailing newline and write an error message.
627 if (line
[strlen(line
) - 1] == '\n')
628 line
[strlen(line
) - 1] = '\0';
630 fprintf(stderr
, "ERROR: [cups-deviced] Bad line from \"%s\": %s\n",
631 backend
->name
, line
);
637 * 'process_children()' - Process all dead children...
641 process_children(void)
643 int i
; /* Looping var */
644 int status
; /* Exit status of child */
645 int pid
; /* Process ID of child */
646 cupsd_backend_t
*backend
; /* Current backend */
647 const char *name
; /* Name of process */
651 * Reset the dead_children flag...
657 * Collect the exit status of some children...
661 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0)
662 #elif defined(HAVE_WAIT3)
663 while ((pid
= wait3(&status
, WNOHANG
, NULL
)) > 0)
665 if ((pid
= wait(&status
)) > 0)
666 #endif /* HAVE_WAITPID */
668 if (status
== SIGTERM
)
671 for (i
= num_backends
, backend
= backends
; i
> 0; i
--, backend
++)
672 if (backend
->pid
== pid
)
677 name
= backend
->name
;
679 backend
->status
= status
;
688 if (WIFEXITED(status
))
690 "ERROR: [cups-deviced] PID %d (%s) stopped with status %d!\n",
691 pid
, name
, WEXITSTATUS(status
));
694 "ERROR: [cups-deviced] PID %d (%s) crashed on signal %d!\n",
695 pid
, name
, WTERMSIG(status
));
699 "DEBUG: [cups-deviced] PID %d (%s) exited with no errors.\n",
706 * 'sigchld_handler()' - Handle 'child' signals from old processes.
710 sigchld_handler(int sig
) /* I - Signal number */
715 * Flag that we have dead children...
721 * Reset the signal handler as needed...
724 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
725 signal(SIGCLD
, sigchld_handler
);
726 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
731 * 'start_backend()' - Run a backend to gather the available devices.
734 static int /* O - 0 on success, -1 on error */
735 start_backend(const char *name
, /* I - Backend to run */
736 int root
) /* I - Run as root? */
738 const char *server_bin
; /* CUPS_SERVERBIN environment variable */
739 char program
[1024]; /* Full path to backend */
740 cupsd_backend_t
*backend
; /* Current backend */
741 char *argv
[2]; /* Command-line arguments */
744 if (num_backends
>= MAX_BACKENDS
)
746 fprintf(stderr
, "ERROR: Too many backends (%d)!\n", num_backends
);
750 if ((server_bin
= getenv("CUPS_SERVERBIN")) == NULL
)
751 server_bin
= CUPS_SERVERBIN
;
753 snprintf(program
, sizeof(program
), "%s/backend/%s", server_bin
, name
);
755 if (_cupsFileCheck(program
, _CUPS_FILE_CHECK_PROGRAM
, !geteuid(),
756 _cupsFileCheckFilter
, NULL
))
759 backend
= backends
+ num_backends
;
761 argv
[0] = (char *)name
;
764 if ((backend
->pipe
= cupsdPipeCommand(&(backend
->pid
), program
, argv
,
765 root
? 0 : normal_user
)) == NULL
)
767 fprintf(stderr
, "ERROR: [cups-deviced] Unable to execute \"%s\" - %s\n",
768 program
, strerror(errno
));
773 * Fill in the rest of the backend information...
776 fprintf(stderr
, "DEBUG: [cups-deviced] Started backend %s (PID %d)\n",
777 program
, backend
->pid
);
779 backend_fds
[num_backends
].fd
= cupsFileNumber(backend
->pipe
);
780 backend_fds
[num_backends
].events
= POLLIN
;
782 backend
->name
= strdup(name
);