]>
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 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
15 #include <cups/array.h>
26 #define MAX_BACKENDS 200 /* Maximum number of backends we'll run */
30 * Backend information...
35 char *name
; /* Name of backend */
36 int pid
, /* Process ID */
37 status
; /* Exit status */
38 cups_file_t
*pipe
; /* Pipe from backend stdout */
39 int count
; /* Number of devices found */
44 * Device information structure...
49 char device_class
[128], /* Device class */
50 device_info
[128], /* Device info/description */
51 device_uri
[1024]; /* Device URI */
59 static int num_backends
= 0,
63 static cupsd_backend_t backends
[MAX_BACKENDS
];
64 /* Array of backends */
65 static struct pollfd backend_fds
[MAX_BACKENDS
];
66 /* Array for poll() */
67 static cups_array_t
*devices
; /* Array of devices */
68 static uid_t normal_user
; /* Normal user ID */
69 static int device_limit
; /* Maximum number of devices */
70 static int send_class
, /* Send device-class attribute? */
71 send_info
, /* Send device-info attribute? */
73 /* Send device-make-and-model attribute? */
74 send_uri
, /* Send device-uri attribute? */
75 send_id
, /* Send device-id attribute? */
76 send_location
; /* Send device-location attribute? */
77 static int dead_children
= 0;
85 static int add_device(const char *device_class
,
86 const char *device_make_and_model
,
87 const char *device_info
,
88 const char *device_uri
,
89 const char *device_id
,
90 const char *device_location
);
91 static int compare_devices(cupsd_device_t
*p0
,
93 static double get_current_time(void);
94 static int get_device(cupsd_backend_t
*backend
);
95 static void process_children(void);
96 static void sigchld_handler(int sig
);
97 static int start_backend(const char *backend
, int root
);
101 * 'main()' - Scan for devices and return an IPP response.
105 * cups-deviced request_id limit options
108 int /* O - Exit code */
109 main(int argc
, /* I - Number of command-line args */
110 char *argv
[]) /* I - Command-line arguments */
112 int i
; /* Looping var */
113 int request_id
; /* Request ID */
114 int timeout
; /* Timeout in seconds */
115 const char *server_bin
; /* CUPS_SERVERBIN environment variable */
116 char filename
[1024]; /* Backend directory filename */
117 cups_dir_t
*dir
; /* Directory pointer */
118 cups_dentry_t
*dent
; /* Directory entry */
119 double current_time
, /* Current time */
120 end_time
; /* Ending time */
121 int num_options
; /* Number of options */
122 cups_option_t
*options
; /* Options */
123 cups_array_t
*requested
, /* requested-attributes values */
124 *exclude
, /* exclude-schemes values */
125 *include
; /* include-schemes values */
126 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
127 struct sigaction action
; /* Actions for POSIX signals */
128 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
131 setbuf(stderr
, NULL
);
134 * Check the command-line...
139 fputs("Usage: cups-deviced request-id limit timeout user-id options\n", stderr
);
144 request_id
= atoi(argv
[1]);
147 fprintf(stderr
, "ERROR: [cups-deviced] Bad request ID %d!\n", request_id
);
152 device_limit
= atoi(argv
[2]);
153 if (device_limit
< 0)
155 fprintf(stderr
, "ERROR: [cups-deviced] Bad limit %d!\n", device_limit
);
160 timeout
= atoi(argv
[3]);
163 fprintf(stderr
, "ERROR: [cups-deviced] Bad timeout %d!\n", timeout
);
168 normal_user
= (uid_t
)atoi(argv
[4]);
169 if (normal_user
<= 0)
171 fprintf(stderr
, "ERROR: [cups-deviced] Bad user %d!\n", normal_user
);
176 num_options
= cupsParseOptions(argv
[5], 0, &options
);
177 requested
= cupsdCreateStringsArray(cupsGetOption("requested-attributes",
178 num_options
, options
));
179 exclude
= cupsdCreateStringsArray(cupsGetOption("exclude-schemes",
180 num_options
, options
));
181 include
= cupsdCreateStringsArray(cupsGetOption("include-schemes",
182 num_options
, options
));
184 if (!requested
|| cupsArrayFind(requested
, "all") != NULL
)
186 send_class
= send_info
= send_make_and_model
= send_uri
= send_id
=
191 send_class
= cupsArrayFind(requested
, "device-class") != NULL
;
192 send_info
= cupsArrayFind(requested
, "device-info") != NULL
;
193 send_make_and_model
= cupsArrayFind(requested
, "device-make-and-model") != NULL
;
194 send_uri
= cupsArrayFind(requested
, "device-uri") != NULL
;
195 send_id
= cupsArrayFind(requested
, "device-id") != NULL
;
196 send_location
= cupsArrayFind(requested
, "device-location") != NULL
;
200 * Listen to child signals...
203 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
204 sigset(SIGCHLD
, sigchld_handler
);
205 #elif defined(HAVE_SIGACTION)
206 memset(&action
, 0, sizeof(action
));
208 sigemptyset(&action
.sa_mask
);
209 sigaddset(&action
.sa_mask
, SIGCHLD
);
210 action
.sa_handler
= sigchld_handler
;
211 sigaction(SIGCHLD
, &action
, NULL
);
213 signal(SIGCLD
, sigchld_handler
); /* No, SIGCLD isn't a typo... */
214 #endif /* HAVE_SIGSET */
217 * Try opening the backend directory...
220 if ((server_bin
= getenv("CUPS_SERVERBIN")) == NULL
)
221 server_bin
= CUPS_SERVERBIN
;
223 snprintf(filename
, sizeof(filename
), "%s/backend", server_bin
);
225 if ((dir
= cupsDirOpen(filename
)) == NULL
)
227 fprintf(stderr
, "ERROR: [cups-deviced] Unable to open backend directory "
228 "\"%s\": %s", filename
, strerror(errno
));
234 * Setup the devices array...
237 devices
= cupsArrayNew((cups_array_func_t
)compare_devices
, NULL
);
240 * Loop through all of the device backends...
243 while ((dent
= cupsDirRead(dir
)) != NULL
)
246 * Skip entries that are not executable files...
249 if (!S_ISREG(dent
->fileinfo
.st_mode
) ||
250 !isalnum(dent
->filename
[0] & 255) ||
251 (dent
->fileinfo
.st_mode
& (S_IRUSR
| S_IXUSR
)) != (S_IRUSR
| S_IXUSR
))
255 * Skip excluded or not included backends...
258 if (cupsArrayFind(exclude
, dent
->filename
) ||
259 (include
&& !cupsArrayFind(include
, dent
->filename
)))
263 * Backends without permissions for normal users run as root,
264 * all others run as the unprivileged user...
267 start_backend(dent
->filename
, !(dent
->fileinfo
.st_mode
& (S_IWGRP
| S_IRWXO
)));
276 if (getenv("SOFTWARE"))
277 puts("Content-Type: application/ipp\n");
279 cupsdSendIPPHeader(IPP_OK
, request_id
);
280 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
281 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
282 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language", "en-US");
284 end_time
= get_current_time() + timeout
;
286 while (active_backends
> 0 && (current_time
= get_current_time()) < end_time
)
289 * Collect the output from the backends...
292 timeout
= (int)(1000 * (end_time
- current_time
));
294 if (poll(backend_fds
, (nfds_t
)num_backends
, timeout
) > 0)
296 for (i
= 0; i
< num_backends
; i
++)
297 if (backend_fds
[i
].revents
&& backends
[i
].pipe
)
299 cups_file_t
*bpipe
= backends
[i
].pipe
;
300 /* Copy of pipe for backend... */
304 if (get_device(backends
+ i
))
306 backend_fds
[i
].fd
= 0;
307 backend_fds
[i
].events
= 0;
311 while (bpipe
->ptr
&& memchr(bpipe
->ptr
, '\n', (size_t)(bpipe
->end
- bpipe
->ptr
)));
316 * Get exit status from children...
323 cupsdSendIPPTrailer();
326 * Terminate any remaining backends and exit...
329 if (active_backends
> 0)
331 for (i
= 0; i
< num_backends
; i
++)
333 kill(backends
[i
].pid
, SIGTERM
);
341 * 'add_device()' - Add a new device to the list.
344 static int /* O - 0 on success, -1 on error */
346 const char *device_class
, /* I - Device class */
347 const char *device_make_and_model
, /* I - Device make and model */
348 const char *device_info
, /* I - Device information */
349 const char *device_uri
, /* I - Device URI */
350 const char *device_id
, /* I - 1284 device ID */
351 const char *device_location
) /* I - Physical location */
353 cupsd_device_t
*device
; /* New device */
357 * Allocate memory for the device record...
360 if ((device
= calloc(1, sizeof(cupsd_device_t
))) == NULL
)
362 fputs("ERROR: [cups-deviced] Ran out of memory allocating a device!\n",
368 * Copy the strings over...
371 strlcpy(device
->device_class
, device_class
, sizeof(device
->device_class
));
372 strlcpy(device
->device_info
, device_info
, sizeof(device
->device_info
));
373 strlcpy(device
->device_uri
, device_uri
, sizeof(device
->device_uri
));
376 * Add the device to the array and return...
379 if (cupsArrayFind(devices
, device
))
389 cupsArrayAdd(devices
, device
);
391 if (device_limit
<= 0 || cupsArrayCount(devices
) < device_limit
)
394 * Send device info...
397 cupsdSendIPPGroup(IPP_TAG_PRINTER
);
399 cupsdSendIPPString(IPP_TAG_KEYWORD
, "device-class",
402 cupsdSendIPPString(IPP_TAG_TEXT
, "device-info", device_info
);
403 if (send_make_and_model
)
404 cupsdSendIPPString(IPP_TAG_TEXT
, "device-make-and-model",
405 device_make_and_model
);
407 cupsdSendIPPString(IPP_TAG_URI
, "device-uri", device_uri
);
409 cupsdSendIPPString(IPP_TAG_TEXT
, "device-id",
410 device_id
? device_id
: "");
412 cupsdSendIPPString(IPP_TAG_TEXT
, "device-location",
413 device_location
? device_location
: "");
416 fputs("DEBUG: Flushed attributes...\n", stderr
);
425 * 'compare_devices()' - Compare device names to eliminate duplicates.
428 static int /* O - Result of comparison */
429 compare_devices(cupsd_device_t
*d0
, /* I - First device */
430 cupsd_device_t
*d1
) /* I - Second device */
432 int diff
; /* Difference between strings */
436 * Sort devices by device-info, device-class, and device-uri...
439 if ((diff
= cupsdCompareNames(d0
->device_info
, d1
->device_info
)) != 0)
441 else if ((diff
= _cups_strcasecmp(d0
->device_class
, d1
->device_class
)) != 0)
444 return (_cups_strcasecmp(d0
->device_uri
, d1
->device_uri
));
449 * 'get_current_time()' - Get the current time as a double value in seconds.
452 static double /* O - Time in seconds */
453 get_current_time(void)
455 struct timeval curtime
; /* Current time */
458 gettimeofday(&curtime
, NULL
);
460 return (curtime
.tv_sec
+ 0.000001 * curtime
.tv_usec
);
465 * 'get_device()' - Get a device from a backend.
468 static int /* O - 0 on success, -1 on error */
469 get_device(cupsd_backend_t
*backend
) /* I - Backend to read from */
471 char line
[2048], /* Line from backend */
472 temp
[2048], /* Copy of line */
473 *ptr
, /* Pointer into line */
474 *dclass
, /* Device class */
475 *uri
, /* Device URI */
476 *make_model
, /* Make and model */
477 *info
, /* Device info */
478 *device_id
, /* 1284 device ID */
479 *location
; /* Physical location */
482 if (cupsFileGets(backend
->pipe
, line
, sizeof(line
)))
485 * Each line is of the form:
487 * class URI "make model" "name" ["1284 device ID"] ["location"]
490 strlcpy(temp
, line
, sizeof(temp
));
498 for (ptr
= temp
; *ptr
; ptr
++)
499 if (isspace(*ptr
& 255))
502 while (isspace(*ptr
& 255))
512 for (uri
= ptr
; *ptr
; ptr
++)
513 if (isspace(*ptr
& 255))
516 while (isspace(*ptr
& 255))
520 * device-make-and-model
526 for (ptr
++, make_model
= ptr
; *ptr
&& *ptr
!= '\"'; ptr
++)
528 if (*ptr
== '\\' && ptr
[1])
529 _cups_strcpy(ptr
, ptr
+ 1);
535 for (*ptr
++ = '\0'; isspace(*ptr
& 255); *ptr
++ = '\0');
544 for (ptr
++, info
= ptr
; *ptr
&& *ptr
!= '\"'; ptr
++)
546 if (*ptr
== '\\' && ptr
[1])
547 _cups_strcpy(ptr
, ptr
+ 1);
553 for (*ptr
++ = '\0'; isspace(*ptr
& 255); *ptr
++ = '\0');
561 for (ptr
++, device_id
= ptr
; *ptr
&& *ptr
!= '\"'; ptr
++)
563 if (*ptr
== '\\' && ptr
[1])
564 _cups_strcpy(ptr
, ptr
+ 1);
570 for (*ptr
++ = '\0'; isspace(*ptr
& 255); *ptr
++ = '\0');
578 for (ptr
++, location
= ptr
; *ptr
&& *ptr
!= '\"'; ptr
++)
580 if (*ptr
== '\\' && ptr
[1])
581 _cups_strcpy(ptr
, ptr
+ 1);
599 * Add the device to the array of available devices...
602 if (!add_device(dclass
, make_model
, info
, uri
, device_id
, location
))
603 fprintf(stderr
, "DEBUG: [cups-deviced] Found device \"%s\"...\n", uri
);
612 cupsFileClose(backend
->pipe
);
613 backend
->pipe
= NULL
;
618 * Bad format; strip trailing newline and write an error message.
623 if (line
[strlen(line
) - 1] == '\n')
624 line
[strlen(line
) - 1] = '\0';
626 fprintf(stderr
, "ERROR: [cups-deviced] Bad line from \"%s\": %s\n",
627 backend
->name
, line
);
633 * 'process_children()' - Process all dead children...
637 process_children(void)
639 int i
; /* Looping var */
640 int status
; /* Exit status of child */
641 int pid
; /* Process ID of child */
642 cupsd_backend_t
*backend
; /* Current backend */
643 const char *name
; /* Name of process */
647 * Reset the dead_children flag...
653 * Collect the exit status of some children...
657 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0)
658 #elif defined(HAVE_WAIT3)
659 while ((pid
= wait3(&status
, WNOHANG
, NULL
)) > 0)
661 if ((pid
= wait(&status
)) > 0)
662 #endif /* HAVE_WAITPID */
664 if (status
== SIGTERM
)
667 for (i
= num_backends
, backend
= backends
; i
> 0; i
--, backend
++)
668 if (backend
->pid
== pid
)
673 name
= backend
->name
;
675 backend
->status
= status
;
684 if (WIFEXITED(status
))
686 "ERROR: [cups-deviced] PID %d (%s) stopped with status %d!\n",
687 pid
, name
, WEXITSTATUS(status
));
690 "ERROR: [cups-deviced] PID %d (%s) crashed on signal %d!\n",
691 pid
, name
, WTERMSIG(status
));
695 "DEBUG: [cups-deviced] PID %d (%s) exited with no errors.\n",
702 * 'sigchld_handler()' - Handle 'child' signals from old processes.
706 sigchld_handler(int sig
) /* I - Signal number */
711 * Flag that we have dead children...
717 * Reset the signal handler as needed...
720 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
721 signal(SIGCLD
, sigchld_handler
);
722 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
727 * 'start_backend()' - Run a backend to gather the available devices.
730 static int /* O - 0 on success, -1 on error */
731 start_backend(const char *name
, /* I - Backend to run */
732 int root
) /* I - Run as root? */
734 const char *server_bin
; /* CUPS_SERVERBIN environment variable */
735 char program
[1024]; /* Full path to backend */
736 cupsd_backend_t
*backend
; /* Current backend */
737 char *argv
[2]; /* Command-line arguments */
740 if (num_backends
>= MAX_BACKENDS
)
742 fprintf(stderr
, "ERROR: Too many backends (%d)!\n", num_backends
);
746 if ((server_bin
= getenv("CUPS_SERVERBIN")) == NULL
)
747 server_bin
= CUPS_SERVERBIN
;
749 snprintf(program
, sizeof(program
), "%s/backend/%s", server_bin
, name
);
751 if (_cupsFileCheck(program
, _CUPS_FILE_CHECK_PROGRAM
, !geteuid(),
752 _cupsFileCheckFilter
, NULL
))
755 backend
= backends
+ num_backends
;
757 argv
[0] = (char *)name
;
760 if ((backend
->pipe
= cupsdPipeCommand(&(backend
->pid
), program
, argv
,
761 root
? 0 : normal_user
)) == NULL
)
763 fprintf(stderr
, "ERROR: [cups-deviced] Unable to execute \"%s\" - %s\n",
764 program
, strerror(errno
));
769 * Fill in the rest of the backend information...
772 fprintf(stderr
, "DEBUG: [cups-deviced] Started backend %s (PID %d)\n",
773 program
, backend
->pid
);
775 backend_fds
[num_backends
].fd
= cupsFileNumber(backend
->pipe
);
776 backend_fds
[num_backends
].events
= POLLIN
;
778 backend
->name
= strdup(name
);