]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/cups-deviced.c
4 * Device scanning mini-daemon for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers...
21 #include <cups/array.h>
32 #define MAX_BACKENDS 200 /* Maximum number of backends we'll run */
36 * Backend information...
41 char *name
; /* Name of backend */
42 int pid
, /* Process ID */
43 status
; /* Exit status */
44 cups_file_t
*pipe
; /* Pipe from backend stdout */
45 int count
; /* Number of devices found */
50 * Device information structure...
55 char device_class
[128], /* Device class */
56 device_info
[128], /* Device info/description */
57 device_uri
[1024]; /* Device URI */
65 static int num_backends
= 0,
69 static cupsd_backend_t backends
[MAX_BACKENDS
];
70 /* Array of backends */
71 static struct pollfd backend_fds
[MAX_BACKENDS
];
72 /* Array for poll() */
73 static cups_array_t
*devices
; /* Array of devices */
74 static uid_t normal_user
; /* Normal user ID */
75 static int device_limit
; /* Maximum number of devices */
76 static int send_class
, /* Send device-class attribute? */
77 send_info
, /* Send device-info attribute? */
79 /* Send device-make-and-model attribute? */
80 send_uri
, /* Send device-uri attribute? */
81 send_id
, /* Send device-id attribute? */
82 send_location
; /* Send device-location attribute? */
83 static int dead_children
= 0;
91 static int add_device(const char *device_class
,
92 const char *device_make_and_model
,
93 const char *device_info
,
94 const char *device_uri
,
95 const char *device_id
,
96 const char *device_location
);
97 static int compare_devices(cupsd_device_t
*p0
,
99 static double get_current_time(void);
100 static int get_device(cupsd_backend_t
*backend
);
101 static void process_children(void);
102 static void sigchld_handler(int sig
);
103 static int start_backend(const char *backend
, int root
);
107 * 'main()' - Scan for devices and return an IPP response.
111 * cups-deviced request_id limit options
114 int /* O - Exit code */
115 main(int argc
, /* I - Number of command-line args */
116 char *argv
[]) /* I - Command-line arguments */
118 int i
; /* Looping var */
119 int request_id
; /* Request ID */
120 int timeout
; /* Timeout in seconds */
121 const char *server_bin
; /* CUPS_SERVERBIN environment variable */
122 char filename
[1024]; /* Backend directory filename */
123 cups_dir_t
*dir
; /* Directory pointer */
124 cups_dentry_t
*dent
; /* Directory entry */
125 double current_time
, /* Current time */
126 end_time
; /* Ending time */
127 int num_options
; /* Number of options */
128 cups_option_t
*options
; /* Options */
129 cups_array_t
*requested
, /* requested-attributes values */
130 *exclude
, /* exclude-schemes values */
131 *include
; /* include-schemes values */
132 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
133 struct sigaction action
; /* Actions for POSIX signals */
134 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
137 setbuf(stderr
, NULL
);
140 * Check the command-line...
145 fputs("Usage: cups-deviced request-id limit timeout user-id options\n", stderr
);
150 request_id
= atoi(argv
[1]);
153 fprintf(stderr
, "ERROR: [cups-deviced] Bad request ID %d!\n", request_id
);
158 device_limit
= atoi(argv
[2]);
159 if (device_limit
< 0)
161 fprintf(stderr
, "ERROR: [cups-deviced] Bad limit %d!\n", device_limit
);
166 timeout
= atoi(argv
[3]);
169 fprintf(stderr
, "ERROR: [cups-deviced] Bad timeout %d!\n", timeout
);
174 normal_user
= (uid_t
)atoi(argv
[4]);
175 if (normal_user
<= 0)
177 fprintf(stderr
, "ERROR: [cups-deviced] Bad user %d!\n", normal_user
);
182 num_options
= cupsParseOptions(argv
[5], 0, &options
);
183 requested
= cupsdCreateStringsArray(cupsGetOption("requested-attributes",
184 num_options
, options
));
185 exclude
= cupsdCreateStringsArray(cupsGetOption("exclude-schemes",
186 num_options
, options
));
187 include
= cupsdCreateStringsArray(cupsGetOption("include-schemes",
188 num_options
, options
));
190 if (!requested
|| cupsArrayFind(requested
, "all") != NULL
)
192 send_class
= send_info
= send_make_and_model
= send_uri
= send_id
=
197 send_class
= cupsArrayFind(requested
, "device-class") != NULL
;
198 send_info
= cupsArrayFind(requested
, "device-info") != NULL
;
199 send_make_and_model
= cupsArrayFind(requested
, "device-make-and-model") != NULL
;
200 send_uri
= cupsArrayFind(requested
, "device-uri") != NULL
;
201 send_id
= cupsArrayFind(requested
, "device-id") != NULL
;
202 send_location
= cupsArrayFind(requested
, "device-location") != NULL
;
206 * Listen to child signals...
209 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
210 sigset(SIGCHLD
, sigchld_handler
);
211 #elif defined(HAVE_SIGACTION)
212 memset(&action
, 0, sizeof(action
));
214 sigemptyset(&action
.sa_mask
);
215 sigaddset(&action
.sa_mask
, SIGCHLD
);
216 action
.sa_handler
= sigchld_handler
;
217 sigaction(SIGCHLD
, &action
, NULL
);
219 signal(SIGCLD
, sigchld_handler
); /* No, SIGCLD isn't a typo... */
220 #endif /* HAVE_SIGSET */
223 * Try opening the backend directory...
226 if ((server_bin
= getenv("CUPS_SERVERBIN")) == NULL
)
227 server_bin
= CUPS_SERVERBIN
;
229 snprintf(filename
, sizeof(filename
), "%s/backend", server_bin
);
231 if ((dir
= cupsDirOpen(filename
)) == NULL
)
233 fprintf(stderr
, "ERROR: [cups-deviced] Unable to open backend directory "
234 "\"%s\": %s", filename
, strerror(errno
));
240 * Setup the devices array...
243 devices
= cupsArrayNew((cups_array_func_t
)compare_devices
, NULL
);
246 * Loop through all of the device backends...
249 while ((dent
= cupsDirRead(dir
)) != NULL
)
252 * Skip entries that are not executable files...
255 if (!S_ISREG(dent
->fileinfo
.st_mode
) ||
256 !isalnum(dent
->filename
[0] & 255) ||
257 (dent
->fileinfo
.st_mode
& (S_IRUSR
| S_IXUSR
)) != (S_IRUSR
| S_IXUSR
))
261 * Skip excluded or not included backends...
264 if (cupsArrayFind(exclude
, dent
->filename
) ||
265 (include
&& !cupsArrayFind(include
, dent
->filename
)))
269 * Backends without permissions for normal users run as root,
270 * all others run as the unprivileged user...
273 start_backend(dent
->filename
, !(dent
->fileinfo
.st_mode
& (S_IWGRP
| S_IRWXO
)));
282 if (getenv("SOFTWARE"))
283 puts("Content-Type: application/ipp\n");
285 cupsdSendIPPHeader(IPP_OK
, request_id
);
286 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
287 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
288 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language", "en-US");
290 end_time
= get_current_time() + timeout
;
292 while (active_backends
> 0 && (current_time
= get_current_time()) < end_time
)
295 * Collect the output from the backends...
298 timeout
= (int)(1000 * (end_time
- current_time
));
300 if (poll(backend_fds
, (nfds_t
)num_backends
, timeout
) > 0)
302 for (i
= 0; i
< num_backends
; i
++)
303 if (backend_fds
[i
].revents
&& backends
[i
].pipe
)
305 cups_file_t
*bpipe
= backends
[i
].pipe
;
306 /* Copy of pipe for backend... */
310 if (get_device(backends
+ i
))
312 backend_fds
[i
].fd
= 0;
313 backend_fds
[i
].events
= 0;
317 while (bpipe
->ptr
&& memchr(bpipe
->ptr
, '\n', (size_t)(bpipe
->end
- bpipe
->ptr
)));
322 * Get exit status from children...
329 cupsdSendIPPTrailer();
332 * Terminate any remaining backends and exit...
335 if (active_backends
> 0)
337 for (i
= 0; i
< num_backends
; i
++)
339 kill(backends
[i
].pid
, SIGTERM
);
347 * 'add_device()' - Add a new device to the list.
350 static int /* O - 0 on success, -1 on error */
352 const char *device_class
, /* I - Device class */
353 const char *device_make_and_model
, /* I - Device make and model */
354 const char *device_info
, /* I - Device information */
355 const char *device_uri
, /* I - Device URI */
356 const char *device_id
, /* I - 1284 device ID */
357 const char *device_location
) /* I - Physical location */
359 cupsd_device_t
*device
; /* New device */
363 * Allocate memory for the device record...
366 if ((device
= calloc(1, sizeof(cupsd_device_t
))) == NULL
)
368 fputs("ERROR: [cups-deviced] Ran out of memory allocating a device!\n",
374 * Copy the strings over...
377 strlcpy(device
->device_class
, device_class
, sizeof(device
->device_class
));
378 strlcpy(device
->device_info
, device_info
, sizeof(device
->device_info
));
379 strlcpy(device
->device_uri
, device_uri
, sizeof(device
->device_uri
));
382 * Add the device to the array and return...
385 if (cupsArrayFind(devices
, device
))
395 cupsArrayAdd(devices
, device
);
397 if (device_limit
<= 0 || cupsArrayCount(devices
) < device_limit
)
400 * Send device info...
403 cupsdSendIPPGroup(IPP_TAG_PRINTER
);
405 cupsdSendIPPString(IPP_TAG_KEYWORD
, "device-class",
408 cupsdSendIPPString(IPP_TAG_TEXT
, "device-info", device_info
);
409 if (send_make_and_model
)
410 cupsdSendIPPString(IPP_TAG_TEXT
, "device-make-and-model",
411 device_make_and_model
);
413 cupsdSendIPPString(IPP_TAG_URI
, "device-uri", device_uri
);
415 cupsdSendIPPString(IPP_TAG_TEXT
, "device-id",
416 device_id
? device_id
: "");
418 cupsdSendIPPString(IPP_TAG_TEXT
, "device-location",
419 device_location
? device_location
: "");
422 fputs("DEBUG: Flushed attributes...\n", stderr
);
431 * 'compare_devices()' - Compare device names to eliminate duplicates.
434 static int /* O - Result of comparison */
435 compare_devices(cupsd_device_t
*d0
, /* I - First device */
436 cupsd_device_t
*d1
) /* I - Second device */
438 int diff
; /* Difference between strings */
442 * Sort devices by device-info, device-class, and device-uri...
445 if ((diff
= cupsdCompareNames(d0
->device_info
, d1
->device_info
)) != 0)
447 else if ((diff
= _cups_strcasecmp(d0
->device_class
, d1
->device_class
)) != 0)
450 return (_cups_strcasecmp(d0
->device_uri
, d1
->device_uri
));
455 * 'get_current_time()' - Get the current time as a double value in seconds.
458 static double /* O - Time in seconds */
459 get_current_time(void)
461 struct timeval curtime
; /* Current time */
464 gettimeofday(&curtime
, NULL
);
466 return (curtime
.tv_sec
+ 0.000001 * curtime
.tv_usec
);
471 * 'get_device()' - Get a device from a backend.
474 static int /* O - 0 on success, -1 on error */
475 get_device(cupsd_backend_t
*backend
) /* I - Backend to read from */
477 char line
[2048], /* Line from backend */
478 temp
[2048], /* Copy of line */
479 *ptr
, /* Pointer into line */
480 *dclass
, /* Device class */
481 *uri
, /* Device URI */
482 *make_model
, /* Make and model */
483 *info
, /* Device info */
484 *device_id
, /* 1284 device ID */
485 *location
; /* Physical location */
488 if (cupsFileGets(backend
->pipe
, line
, sizeof(line
)))
491 * Each line is of the form:
493 * class URI "make model" "name" ["1284 device ID"] ["location"]
496 strlcpy(temp
, line
, sizeof(temp
));
504 for (ptr
= temp
; *ptr
; ptr
++)
505 if (isspace(*ptr
& 255))
508 while (isspace(*ptr
& 255))
518 for (uri
= ptr
; *ptr
; ptr
++)
519 if (isspace(*ptr
& 255))
522 while (isspace(*ptr
& 255))
526 * device-make-and-model
532 for (ptr
++, make_model
= ptr
; *ptr
&& *ptr
!= '\"'; ptr
++)
534 if (*ptr
== '\\' && ptr
[1])
535 _cups_strcpy(ptr
, ptr
+ 1);
541 for (*ptr
++ = '\0'; isspace(*ptr
& 255); *ptr
++ = '\0');
550 for (ptr
++, info
= ptr
; *ptr
&& *ptr
!= '\"'; ptr
++)
552 if (*ptr
== '\\' && ptr
[1])
553 _cups_strcpy(ptr
, ptr
+ 1);
559 for (*ptr
++ = '\0'; isspace(*ptr
& 255); *ptr
++ = '\0');
567 for (ptr
++, device_id
= ptr
; *ptr
&& *ptr
!= '\"'; ptr
++)
569 if (*ptr
== '\\' && ptr
[1])
570 _cups_strcpy(ptr
, ptr
+ 1);
576 for (*ptr
++ = '\0'; isspace(*ptr
& 255); *ptr
++ = '\0');
584 for (ptr
++, location
= ptr
; *ptr
&& *ptr
!= '\"'; ptr
++)
586 if (*ptr
== '\\' && ptr
[1])
587 _cups_strcpy(ptr
, ptr
+ 1);
605 * Add the device to the array of available devices...
608 if (!add_device(dclass
, make_model
, info
, uri
, device_id
, location
))
609 fprintf(stderr
, "DEBUG: [cups-deviced] Found device \"%s\"...\n", uri
);
618 cupsFileClose(backend
->pipe
);
619 backend
->pipe
= NULL
;
624 * Bad format; strip trailing newline and write an error message.
629 if (line
[strlen(line
) - 1] == '\n')
630 line
[strlen(line
) - 1] = '\0';
632 fprintf(stderr
, "ERROR: [cups-deviced] Bad line from \"%s\": %s\n",
633 backend
->name
, line
);
639 * 'process_children()' - Process all dead children...
643 process_children(void)
645 int i
; /* Looping var */
646 int status
; /* Exit status of child */
647 int pid
; /* Process ID of child */
648 cupsd_backend_t
*backend
; /* Current backend */
649 const char *name
; /* Name of process */
653 * Reset the dead_children flag...
659 * Collect the exit status of some children...
663 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0)
664 #elif defined(HAVE_WAIT3)
665 while ((pid
= wait3(&status
, WNOHANG
, NULL
)) > 0)
667 if ((pid
= wait(&status
)) > 0)
668 #endif /* HAVE_WAITPID */
670 if (status
== SIGTERM
)
673 for (i
= num_backends
, backend
= backends
; i
> 0; i
--, backend
++)
674 if (backend
->pid
== pid
)
679 name
= backend
->name
;
681 backend
->status
= status
;
690 if (WIFEXITED(status
))
692 "ERROR: [cups-deviced] PID %d (%s) stopped with status %d!\n",
693 pid
, name
, WEXITSTATUS(status
));
696 "ERROR: [cups-deviced] PID %d (%s) crashed on signal %d!\n",
697 pid
, name
, WTERMSIG(status
));
701 "DEBUG: [cups-deviced] PID %d (%s) exited with no errors.\n",
708 * 'sigchld_handler()' - Handle 'child' signals from old processes.
712 sigchld_handler(int sig
) /* I - Signal number */
717 * Flag that we have dead children...
723 * Reset the signal handler as needed...
726 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
727 signal(SIGCLD
, sigchld_handler
);
728 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
733 * 'start_backend()' - Run a backend to gather the available devices.
736 static int /* O - 0 on success, -1 on error */
737 start_backend(const char *name
, /* I - Backend to run */
738 int root
) /* I - Run as root? */
740 const char *server_bin
; /* CUPS_SERVERBIN environment variable */
741 char program
[1024]; /* Full path to backend */
742 cupsd_backend_t
*backend
; /* Current backend */
743 char *argv
[2]; /* Command-line arguments */
746 if (num_backends
>= MAX_BACKENDS
)
748 fprintf(stderr
, "ERROR: Too many backends (%d)!\n", num_backends
);
752 if ((server_bin
= getenv("CUPS_SERVERBIN")) == NULL
)
753 server_bin
= CUPS_SERVERBIN
;
755 snprintf(program
, sizeof(program
), "%s/backend/%s", server_bin
, name
);
757 if (_cupsFileCheck(program
, _CUPS_FILE_CHECK_PROGRAM
, !geteuid(),
758 _cupsFileCheckFilter
, NULL
))
761 backend
= backends
+ num_backends
;
763 argv
[0] = (char *)name
;
766 if ((backend
->pipe
= cupsdPipeCommand(&(backend
->pid
), program
, argv
,
767 root
? 0 : normal_user
)) == NULL
)
769 fprintf(stderr
, "ERROR: [cups-deviced] Unable to execute \"%s\" - %s\n",
770 program
, strerror(errno
));
775 * Fill in the rest of the backend information...
778 fprintf(stderr
, "DEBUG: [cups-deviced] Started backend %s (PID %d)\n",
779 program
, backend
->pid
);
781 backend_fds
[num_backends
].fd
= cupsFileNumber(backend
->pipe
);
782 backend_fds
[num_backends
].events
= POLLIN
;
784 backend
->name
= strdup(name
);