]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/cups-deviced.c
2 * Device scanning mini-daemon for CUPS.
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
16 #include <cups/array.h>
27 #define MAX_BACKENDS 200 /* Maximum number of backends we'll run */
31 * Backend information...
36 char *name
; /* Name of backend */
37 int pid
, /* Process ID */
38 status
; /* Exit status */
39 cups_file_t
*pipe
; /* Pipe from backend stdout */
40 int count
; /* Number of devices found */
45 * Device information structure...
50 char device_class
[128], /* Device class */
51 device_info
[128], /* Device info/description */
52 device_uri
[1024]; /* Device URI */
60 static int num_backends
= 0,
64 static cupsd_backend_t backends
[MAX_BACKENDS
];
65 /* Array of backends */
66 static struct pollfd backend_fds
[MAX_BACKENDS
];
67 /* Array for poll() */
68 static cups_array_t
*devices
; /* Array of devices */
69 static uid_t normal_user
; /* Normal user ID */
70 static int device_limit
; /* Maximum number of devices */
71 static int send_class
, /* Send device-class attribute? */
72 send_info
, /* Send device-info attribute? */
74 /* Send device-make-and-model attribute? */
75 send_uri
, /* Send device-uri attribute? */
76 send_id
, /* Send device-id attribute? */
77 send_location
; /* Send device-location attribute? */
78 static int dead_children
= 0;
86 static int add_device(const char *device_class
,
87 const char *device_make_and_model
,
88 const char *device_info
,
89 const char *device_uri
,
90 const char *device_id
,
91 const char *device_location
);
92 static int compare_devices(cupsd_device_t
*p0
,
94 static double get_current_time(void);
95 static int get_device(cupsd_backend_t
*backend
);
96 static void process_children(void);
97 static void sigchld_handler(int sig
);
98 static int start_backend(const char *backend
, int root
);
102 * 'main()' - Scan for devices and return an IPP response.
106 * cups-deviced request_id limit options
109 int /* O - Exit code */
110 main(int argc
, /* I - Number of command-line args */
111 char *argv
[]) /* I - Command-line arguments */
113 int i
; /* Looping var */
114 int request_id
; /* Request ID */
115 int timeout
; /* Timeout in seconds */
116 const char *server_bin
; /* CUPS_SERVERBIN environment variable */
117 char filename
[1024]; /* Backend directory filename */
118 cups_dir_t
*dir
; /* Directory pointer */
119 cups_dentry_t
*dent
; /* Directory entry */
120 double current_time
, /* Current time */
121 end_time
; /* Ending time */
122 int num_options
; /* Number of options */
123 cups_option_t
*options
; /* Options */
124 cups_array_t
*requested
, /* requested-attributes values */
125 *exclude
, /* exclude-schemes values */
126 *include
; /* include-schemes values */
127 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
128 struct sigaction action
; /* Actions for POSIX signals */
129 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
132 setbuf(stderr
, NULL
);
135 * Check the command-line...
140 fputs("Usage: cups-deviced request-id limit timeout user-id options\n", stderr
);
145 request_id
= atoi(argv
[1]);
148 fprintf(stderr
, "ERROR: [cups-deviced] Bad request ID %d!\n", request_id
);
153 device_limit
= atoi(argv
[2]);
154 if (device_limit
< 0)
156 fprintf(stderr
, "ERROR: [cups-deviced] Bad limit %d!\n", device_limit
);
161 timeout
= atoi(argv
[3]);
164 fprintf(stderr
, "ERROR: [cups-deviced] Bad timeout %d!\n", timeout
);
169 normal_user
= (uid_t
)atoi(argv
[4]);
170 if (normal_user
<= 0)
172 fprintf(stderr
, "ERROR: [cups-deviced] Bad user %d!\n", normal_user
);
177 num_options
= cupsParseOptions(argv
[5], 0, &options
);
178 requested
= cupsdCreateStringsArray(cupsGetOption("requested-attributes",
179 num_options
, options
));
180 exclude
= cupsdCreateStringsArray(cupsGetOption("exclude-schemes",
181 num_options
, options
));
182 include
= cupsdCreateStringsArray(cupsGetOption("include-schemes",
183 num_options
, options
));
185 if (!requested
|| cupsArrayFind(requested
, "all") != NULL
)
187 send_class
= send_info
= send_make_and_model
= send_uri
= send_id
=
192 send_class
= cupsArrayFind(requested
, "device-class") != NULL
;
193 send_info
= cupsArrayFind(requested
, "device-info") != NULL
;
194 send_make_and_model
= cupsArrayFind(requested
, "device-make-and-model") != NULL
;
195 send_uri
= cupsArrayFind(requested
, "device-uri") != NULL
;
196 send_id
= cupsArrayFind(requested
, "device-id") != NULL
;
197 send_location
= cupsArrayFind(requested
, "device-location") != NULL
;
201 * Listen to child signals...
204 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
205 sigset(SIGCHLD
, sigchld_handler
);
206 #elif defined(HAVE_SIGACTION)
207 memset(&action
, 0, sizeof(action
));
209 sigemptyset(&action
.sa_mask
);
210 sigaddset(&action
.sa_mask
, SIGCHLD
);
211 action
.sa_handler
= sigchld_handler
;
212 sigaction(SIGCHLD
, &action
, NULL
);
214 signal(SIGCLD
, sigchld_handler
); /* No, SIGCLD isn't a typo... */
215 #endif /* HAVE_SIGSET */
218 * Try opening the backend directory...
221 if ((server_bin
= getenv("CUPS_SERVERBIN")) == NULL
)
222 server_bin
= CUPS_SERVERBIN
;
224 snprintf(filename
, sizeof(filename
), "%s/backend", server_bin
);
226 if ((dir
= cupsDirOpen(filename
)) == NULL
)
228 fprintf(stderr
, "ERROR: [cups-deviced] Unable to open backend directory "
229 "\"%s\": %s", filename
, strerror(errno
));
235 * Setup the devices array...
238 devices
= cupsArrayNew((cups_array_func_t
)compare_devices
, NULL
);
241 * Loop through all of the device backends...
244 while ((dent
= cupsDirRead(dir
)) != NULL
)
247 * Skip entries that are not executable files...
250 if (!S_ISREG(dent
->fileinfo
.st_mode
) ||
251 !isalnum(dent
->filename
[0] & 255) ||
252 (dent
->fileinfo
.st_mode
& (S_IRUSR
| S_IXUSR
)) != (S_IRUSR
| S_IXUSR
))
256 * Skip excluded or not included backends...
259 if (cupsArrayFind(exclude
, dent
->filename
) ||
260 (include
&& !cupsArrayFind(include
, dent
->filename
)))
264 * Backends without permissions for normal users run as root,
265 * all others run as the unprivileged user...
268 start_backend(dent
->filename
, !(dent
->fileinfo
.st_mode
& (S_IWGRP
| S_IRWXO
)));
277 if (getenv("SOFTWARE"))
278 puts("Content-Type: application/ipp\n");
280 cupsdSendIPPHeader(IPP_OK
, request_id
);
281 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
282 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
283 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language", "en-US");
285 end_time
= get_current_time() + timeout
;
287 while (active_backends
> 0 && (current_time
= get_current_time()) < end_time
)
290 * Collect the output from the backends...
293 timeout
= (int)(1000 * (end_time
- current_time
));
295 if (poll(backend_fds
, (nfds_t
)num_backends
, timeout
) > 0)
297 for (i
= 0; i
< num_backends
; i
++)
298 if (backend_fds
[i
].revents
&& backends
[i
].pipe
)
300 cups_file_t
*bpipe
= backends
[i
].pipe
;
301 /* Copy of pipe for backend... */
305 if (get_device(backends
+ i
))
307 backend_fds
[i
].fd
= 0;
308 backend_fds
[i
].events
= 0;
312 while (_cupsFilePeekAhead(bpipe
, '\n'));
317 * Get exit status from children...
324 cupsdSendIPPTrailer();
327 * Terminate any remaining backends and exit...
330 if (active_backends
> 0)
332 for (i
= 0; i
< num_backends
; i
++)
334 kill(backends
[i
].pid
, SIGTERM
);
342 * 'add_device()' - Add a new device to the list.
345 static int /* O - 0 on success, -1 on error */
347 const char *device_class
, /* I - Device class */
348 const char *device_make_and_model
, /* I - Device make and model */
349 const char *device_info
, /* I - Device information */
350 const char *device_uri
, /* I - Device URI */
351 const char *device_id
, /* I - 1284 device ID */
352 const char *device_location
) /* I - Physical location */
354 cupsd_device_t
*device
; /* New device */
358 * Allocate memory for the device record...
361 if ((device
= calloc(1, sizeof(cupsd_device_t
))) == NULL
)
363 fputs("ERROR: [cups-deviced] Ran out of memory allocating a device!\n",
369 * Copy the strings over...
372 strlcpy(device
->device_class
, device_class
, sizeof(device
->device_class
));
373 strlcpy(device
->device_info
, device_info
, sizeof(device
->device_info
));
374 strlcpy(device
->device_uri
, device_uri
, sizeof(device
->device_uri
));
377 * Add the device to the array and return...
380 if (cupsArrayFind(devices
, device
))
390 cupsArrayAdd(devices
, device
);
392 if (device_limit
<= 0 || cupsArrayCount(devices
) < device_limit
)
395 * Send device info...
398 cupsdSendIPPGroup(IPP_TAG_PRINTER
);
400 cupsdSendIPPString(IPP_TAG_KEYWORD
, "device-class",
403 cupsdSendIPPString(IPP_TAG_TEXT
, "device-info", device_info
);
404 if (send_make_and_model
)
405 cupsdSendIPPString(IPP_TAG_TEXT
, "device-make-and-model",
406 device_make_and_model
);
408 cupsdSendIPPString(IPP_TAG_URI
, "device-uri", device_uri
);
410 cupsdSendIPPString(IPP_TAG_TEXT
, "device-id",
411 device_id
? device_id
: "");
413 cupsdSendIPPString(IPP_TAG_TEXT
, "device-location",
414 device_location
? device_location
: "");
417 fputs("DEBUG: Flushed attributes...\n", stderr
);
426 * 'compare_devices()' - Compare device names to eliminate duplicates.
429 static int /* O - Result of comparison */
430 compare_devices(cupsd_device_t
*d0
, /* I - First device */
431 cupsd_device_t
*d1
) /* I - Second device */
433 int diff
; /* Difference between strings */
437 * Sort devices by device-info, device-class, and device-uri...
440 if ((diff
= cupsdCompareNames(d0
->device_info
, d1
->device_info
)) != 0)
442 else if ((diff
= _cups_strcasecmp(d0
->device_class
, d1
->device_class
)) != 0)
445 return (_cups_strcasecmp(d0
->device_uri
, d1
->device_uri
));
450 * 'get_current_time()' - Get the current time as a double value in seconds.
453 static double /* O - Time in seconds */
454 get_current_time(void)
456 struct timeval curtime
; /* Current time */
459 gettimeofday(&curtime
, NULL
);
461 return (curtime
.tv_sec
+ 0.000001 * curtime
.tv_usec
);
466 * 'get_device()' - Get a device from a backend.
469 static int /* O - 0 on success, -1 on error */
470 get_device(cupsd_backend_t
*backend
) /* I - Backend to read from */
472 char line
[2048], /* Line from backend */
473 temp
[2048], /* Copy of line */
474 *ptr
, /* Pointer into line */
475 *dclass
, /* Device class */
476 *uri
, /* Device URI */
477 *make_model
, /* Make and model */
478 *info
, /* Device info */
479 *device_id
, /* 1284 device ID */
480 *location
; /* Physical location */
483 if (cupsFileGets(backend
->pipe
, line
, sizeof(line
)))
486 * Each line is of the form:
488 * class URI "make model" "name" ["1284 device ID"] ["location"]
491 strlcpy(temp
, line
, sizeof(temp
));
499 for (ptr
= temp
; *ptr
; ptr
++)
500 if (isspace(*ptr
& 255))
503 while (isspace(*ptr
& 255))
513 for (uri
= ptr
; *ptr
; ptr
++)
514 if (isspace(*ptr
& 255))
517 while (isspace(*ptr
& 255))
521 * device-make-and-model
527 for (ptr
++, make_model
= ptr
; *ptr
&& *ptr
!= '\"'; ptr
++)
529 if (*ptr
== '\\' && ptr
[1])
530 _cups_strcpy(ptr
, ptr
+ 1);
536 for (*ptr
++ = '\0'; isspace(*ptr
& 255); *ptr
++ = '\0');
545 for (ptr
++, info
= ptr
; *ptr
&& *ptr
!= '\"'; ptr
++)
547 if (*ptr
== '\\' && ptr
[1])
548 _cups_strcpy(ptr
, ptr
+ 1);
554 for (*ptr
++ = '\0'; isspace(*ptr
& 255); *ptr
++ = '\0');
562 for (ptr
++, device_id
= ptr
; *ptr
&& *ptr
!= '\"'; ptr
++)
564 if (*ptr
== '\\' && ptr
[1])
565 _cups_strcpy(ptr
, ptr
+ 1);
571 for (*ptr
++ = '\0'; isspace(*ptr
& 255); *ptr
++ = '\0');
579 for (ptr
++, location
= ptr
; *ptr
&& *ptr
!= '\"'; ptr
++)
581 if (*ptr
== '\\' && ptr
[1])
582 _cups_strcpy(ptr
, ptr
+ 1);
600 * Add the device to the array of available devices...
603 if (!add_device(dclass
, make_model
, info
, uri
, device_id
, location
))
604 fprintf(stderr
, "DEBUG: [cups-deviced] Found device \"%s\"...\n", uri
);
613 cupsFileClose(backend
->pipe
);
614 backend
->pipe
= NULL
;
619 * Bad format; strip trailing newline and write an error message.
624 if (line
[strlen(line
) - 1] == '\n')
625 line
[strlen(line
) - 1] = '\0';
627 fprintf(stderr
, "ERROR: [cups-deviced] Bad line from \"%s\": %s\n",
628 backend
->name
, line
);
634 * 'process_children()' - Process all dead children...
638 process_children(void)
640 int i
; /* Looping var */
641 int status
; /* Exit status of child */
642 int pid
; /* Process ID of child */
643 cupsd_backend_t
*backend
; /* Current backend */
644 const char *name
; /* Name of process */
648 * Reset the dead_children flag...
654 * Collect the exit status of some children...
658 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0)
659 #elif defined(HAVE_WAIT3)
660 while ((pid
= wait3(&status
, WNOHANG
, NULL
)) > 0)
662 if ((pid
= wait(&status
)) > 0)
663 #endif /* HAVE_WAITPID */
665 if (status
== SIGTERM
)
668 for (i
= num_backends
, backend
= backends
; i
> 0; i
--, backend
++)
669 if (backend
->pid
== pid
)
674 name
= backend
->name
;
676 backend
->status
= status
;
685 if (WIFEXITED(status
))
687 "ERROR: [cups-deviced] PID %d (%s) stopped with status %d!\n",
688 pid
, name
, WEXITSTATUS(status
));
691 "ERROR: [cups-deviced] PID %d (%s) crashed on signal %d!\n",
692 pid
, name
, WTERMSIG(status
));
696 "DEBUG: [cups-deviced] PID %d (%s) exited with no errors.\n",
703 * 'sigchld_handler()' - Handle 'child' signals from old processes.
707 sigchld_handler(int sig
) /* I - Signal number */
712 * Flag that we have dead children...
718 * Reset the signal handler as needed...
721 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
722 signal(SIGCLD
, sigchld_handler
);
723 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
728 * 'start_backend()' - Run a backend to gather the available devices.
731 static int /* O - 0 on success, -1 on error */
732 start_backend(const char *name
, /* I - Backend to run */
733 int root
) /* I - Run as root? */
735 const char *server_bin
; /* CUPS_SERVERBIN environment variable */
736 char program
[1024]; /* Full path to backend */
737 cupsd_backend_t
*backend
; /* Current backend */
738 char *argv
[2]; /* Command-line arguments */
741 if (num_backends
>= MAX_BACKENDS
)
743 fprintf(stderr
, "ERROR: Too many backends (%d)!\n", num_backends
);
747 if ((server_bin
= getenv("CUPS_SERVERBIN")) == NULL
)
748 server_bin
= CUPS_SERVERBIN
;
750 snprintf(program
, sizeof(program
), "%s/backend/%s", server_bin
, name
);
752 if (_cupsFileCheck(program
, _CUPS_FILE_CHECK_PROGRAM
, !geteuid(),
753 _cupsFileCheckFilter
, NULL
))
756 backend
= backends
+ num_backends
;
758 argv
[0] = (char *)name
;
761 if ((backend
->pipe
= cupsdPipeCommand(&(backend
->pid
), program
, argv
,
762 root
? 0 : normal_user
)) == NULL
)
764 fprintf(stderr
, "ERROR: [cups-deviced] Unable to execute \"%s\" - %s\n",
765 program
, strerror(errno
));
770 * Fill in the rest of the backend information...
773 fprintf(stderr
, "DEBUG: [cups-deviced] Started backend %s (PID %d)\n",
774 program
, backend
->pid
);
776 backend_fds
[num_backends
].fd
= cupsFileNumber(backend
->pipe
);
777 backend_fds
[num_backends
].events
= POLLIN
;
779 backend
->name
= strdup(name
);