4 * Line Printer Daemon backend for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * main() - Send a file to the printer or server.
29 * lpd_command() - Send an LPR command sequence and wait for a reply.
30 * lpd_queue() - Queue a file using the Line Printer Daemon protocol.
31 * lpd_timeout() - Handle timeout alarms...
32 * lpd_write() - Write a buffer of data to an LPD server.
33 * rresvport_af() - A simple implementation of rresvport_af().
34 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
38 * Include necessary headers.
41 #include <cups/backend.h>
42 #include <cups/http-private.h>
43 #include <cups/cups.h>
48 #include <cups/string.h>
50 #include <sys/types.h>
57 # include <sys/socket.h>
58 # include <netinet/in.h>
59 # include <arpa/inet.h>
63 # include <CoreFoundation/CFNumber.h>
64 # include <CoreFoundation/CFPreferences.h>
65 #endif /* __APPLE__ */
72 static char tmpfilename
[1024] = ""; /* Temporary spool file name */
73 static int abort_job
= 0; /* Non-zero if we get SIGTERM */
77 * The order for control and data files in LPD requests...
80 #define ORDER_CONTROL_DATA 0 /* Control file first, then data */
81 #define ORDER_DATA_CONTROL 1 /* Data file first, then control */
88 #define RESERVE_NONE 0 /* Don't reserve a priviledged port */
89 #define RESERVE_RFC1179 1 /* Reserve port 721-731 */
90 #define RESERVE_ANY 2 /* Reserve port 1-1023 */
97 static int lpd_command(int lpd_fd
, int timeout
, char *format
, ...);
98 static int lpd_queue(const char *hostname
, int port
, const char *printer
,
100 const char *user
, const char *title
, int copies
,
101 int banner
, int format
, int order
, int reserve
,
102 int manual_copies
, int timeout
, int contimeout
);
103 static void lpd_timeout(int sig
);
104 static int lpd_write(int lpd_fd
, char *buffer
, int length
);
105 #ifndef HAVE_RRESVPORT_AF
106 static int rresvport_af(int *port
, int family
);
107 #endif /* !HAVE_RRESVPORT_AF */
108 static void sigterm_handler(int sig
);
112 * 'main()' - Send a file to the printer or server.
116 * printer-uri job-id user title copies options [file]
119 int /* O - Exit status */
120 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
121 char *argv
[]) /* I - Command-line arguments */
123 char method
[255], /* Method in URI */
124 hostname
[1024], /* Hostname */
125 username
[255], /* Username info */
126 resource
[1024], /* Resource info (printer name) */
127 *options
, /* Pointer to options */
128 name
[255], /* Name of option */
129 value
[255], /* Value of option */
130 *ptr
, /* Pointer into name or value */
131 *filename
, /* File to print */
132 title
[256]; /* Title string */
133 int port
; /* Port number */
134 int status
; /* Status of LPD job */
135 int banner
; /* Print banner page? */
136 int format
; /* Print format */
137 int order
; /* Order of control/data files */
138 int reserve
; /* Reserve priviledged port? */
139 int sanitize_title
; /* Sanitize title string? */
140 int manual_copies
, /* Do manual copies? */
141 timeout
, /* Timeout */
142 contimeout
, /* Connection timeout */
143 copies
; /* Number of copies */
144 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
145 struct sigaction action
; /* Actions for POSIX signals */
146 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
150 * Make sure status messages are not buffered...
153 setbuf(stderr
, NULL
);
156 * Ignore SIGPIPE and catch SIGTERM signals...
160 sigset(SIGPIPE
, SIG_IGN
);
161 sigset(SIGTERM
, sigterm_handler
);
162 #elif defined(HAVE_SIGACTION)
163 memset(&action
, 0, sizeof(action
));
164 action
.sa_handler
= SIG_IGN
;
165 sigaction(SIGPIPE
, &action
, NULL
);
167 sigemptyset(&action
.sa_mask
);
168 sigaddset(&action
.sa_mask
, SIGTERM
);
169 action
.sa_handler
= sigterm_handler
;
170 sigaction(SIGTERM
, &action
, NULL
);
172 signal(SIGPIPE
, SIG_IGN
);
173 signal(SIGTERM
, sigterm_handler
);
174 #endif /* HAVE_SIGSET */
177 * Check command-line...
182 puts("network lpd \"Unknown\" \"LPD/LPR Host or Printer\"");
183 return (CUPS_BACKEND_OK
);
185 else if (argc
< 6 || argc
> 7)
187 fprintf(stderr
, "Usage: %s job-id user title copies options [file]\n",
189 return (CUPS_BACKEND_FAILED
);
193 * If we have 7 arguments, print the file named on the command-line.
194 * Otherwise, copy stdin to a temporary file and print the temporary
201 * Copy stdin to a temporary file...
204 int fd
; /* Temporary file */
205 char buffer
[8192]; /* Buffer for copying */
206 int bytes
; /* Number of bytes read */
209 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
211 perror("ERROR: unable to create temporary file");
212 return (CUPS_BACKEND_FAILED
);
215 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0)
216 if (write(fd
, buffer
, bytes
) < bytes
)
218 perror("ERROR: unable to write to temporary file");
221 return (CUPS_BACKEND_FAILED
);
225 filename
= tmpfilename
;
231 * Extract the hostname and printer name from the URI...
234 httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
235 method
, sizeof(method
), username
, sizeof(username
),
236 hostname
, sizeof(hostname
), &port
,
237 resource
, sizeof(resource
));
242 * If no username is in the device URI, then use the print job user...
245 strlcpy(username
, argv
[2], sizeof(username
));
249 * See if there are any options...
254 order
= ORDER_CONTROL_DATA
;
255 reserve
= RESERVE_ANY
;
258 contimeout
= 7 * 24 * 60 * 60;
261 /* We want to pass utf-8 characters, not re-map them (3071945) */
265 CFPropertyListRef pvalue
; /* Preference value */
266 SInt32 toval
; /* Timeout value */
269 pvalue
= CFPreferencesCopyValue(CFSTR("timeout"),
270 CFSTR("com.apple.print.backends"),
271 kCFPreferencesAnyUser
,
272 kCFPreferencesCurrentHost
);
275 if (CFGetTypeID(pvalue
) == CFNumberGetTypeID())
277 CFNumberGetValue(pvalue
, kCFNumberSInt32Type
, &toval
);
278 contimeout
= (int)toval
;
286 #endif /* __APPLE__ */
288 if ((options
= strchr(resource
, '?')) != NULL
)
291 * Yup, terminate the device name string and move to the first
292 * character of the options...
307 for (ptr
= name
; *options
&& *options
!= '=';)
308 if (ptr
< (name
+ sizeof(name
) - 1))
320 for (ptr
= value
; *options
&& *options
!= '+' && *options
!= '&';)
321 if (ptr
< (value
+ sizeof(value
) - 1))
325 if (*options
== '+' || *options
== '&')
332 * Process the option...
335 if (!strcasecmp(name
, "banner"))
341 banner
= !value
[0] || !strcasecmp(value
, "on") ||
342 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
344 else if (!strcasecmp(name
, "format") && value
[0])
347 * Set output format...
350 if (strchr("cdfglnoprtv", value
[0]))
353 fprintf(stderr
, "ERROR: Unknown format character \"%c\"\n", value
[0]);
355 else if (!strcasecmp(name
, "order") && value
[0])
358 * Set control/data order...
361 if (!strcasecmp(value
, "control,data"))
362 order
= ORDER_CONTROL_DATA
;
363 else if (!strcasecmp(value
, "data,control"))
364 order
= ORDER_DATA_CONTROL
;
366 fprintf(stderr
, "ERROR: Unknown file order \"%s\"\n", value
);
368 else if (!strcasecmp(name
, "reserve"))
371 * Set port reservation mode...
374 if (!value
[0] || !strcasecmp(value
, "on") ||
375 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true") ||
376 !strcasecmp(value
, "rfc1179"))
377 reserve
= RESERVE_RFC1179
;
378 else if (!strcasecmp(value
, "any"))
379 reserve
= RESERVE_ANY
;
381 reserve
= RESERVE_NONE
;
383 else if (!strcasecmp(name
, "manual_copies"))
386 * Set manual copies...
389 manual_copies
= !value
[0] || !strcasecmp(value
, "on") ||
390 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
392 else if (!strcasecmp(name
, "sanitize_title"))
395 * Set sanitize title...
398 sanitize_title
= !value
[0] || !strcasecmp(value
, "on") ||
399 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
401 else if (!strcasecmp(name
, "timeout"))
408 timeout
= atoi(value
);
410 else if (!strcasecmp(name
, "contimeout"))
417 contimeout
= atoi(value
);
423 * Sanitize the document title...
426 strlcpy(title
, argv
[3], sizeof(title
));
431 * Sanitize the title string so that we don't cause problems on
435 for (ptr
= title
; *ptr
; ptr
++)
436 if (!isalnum(*ptr
& 255) && !isspace(*ptr
& 255))
448 manual_copies
= atoi(argv
[4]);
454 copies
= atoi(argv
[4]);
457 status
= lpd_queue(hostname
, port
, resource
+ 1, filename
,
458 username
, title
, copies
,
459 banner
, format
, order
, reserve
, manual_copies
,
460 timeout
, contimeout
);
463 fprintf(stderr
, "PAGE: 1 %d\n", atoi(argv
[4]));
466 status
= lpd_queue(hostname
, port
, resource
+ 1, filename
,
468 banner
, format
, order
, reserve
, 1,
469 timeout
, contimeout
);
472 * Remove the temporary file if necessary...
479 * Return the queue status...
487 * 'lpd_command()' - Send an LPR command sequence and wait for a reply.
490 static int /* O - Status of command */
491 lpd_command(int fd
, /* I - Socket connection to LPD host */
492 int timeout
, /* I - Seconds to wait for a response */
493 char *format
, /* I - printf()-style format string */
494 ...) /* I - Additional args as necessary */
496 va_list ap
; /* Argument pointer */
497 char buf
[1024]; /* Output buffer */
498 int bytes
; /* Number of bytes to output */
499 char status
; /* Status from command */
503 * Don't try to send commands if the job has been cancelled...
510 * Format the string...
513 va_start(ap
, format
);
514 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
517 fprintf(stderr
, "DEBUG: lpd_command %2.2x %s", buf
[0], buf
+ 1);
520 * Send the command...
523 fprintf(stderr
, "DEBUG: Sending command string (%d bytes)...\n", bytes
);
525 if (lpd_write(fd
, buf
, bytes
) < bytes
)
527 perror("ERROR: Unable to send LPD command");
532 * Read back the status from the command and return it...
535 fprintf(stderr
, "DEBUG: Reading command status...\n");
539 if (recv(fd
, &status
, 1, 0) < 1)
541 fprintf(stderr
, "WARNING: Remote host did not respond with command "
542 "status byte after %d seconds!\n", timeout
);
548 fprintf(stderr
, "DEBUG: lpd_command returning %d\n", status
);
555 * 'lpd_queue()' - Queue a file using the Line Printer Daemon protocol.
558 static int /* O - Zero on success, non-zero on failure */
559 lpd_queue(const char *hostname
, /* I - Host to connect to */
560 int port
, /* I - Port to connect on */
561 const char *printer
, /* I - Printer/queue name */
562 const char *filename
, /* I - File to print */
563 const char *user
, /* I - Requesting user */
564 const char *title
, /* I - Job title */
565 int copies
, /* I - Number of copies */
566 int banner
, /* I - Print LPD banner? */
567 int format
, /* I - Format specifier */
568 int order
, /* I - Order of data/control files */
569 int reserve
, /* I - Reserve ports? */
570 int manual_copies
, /* I - Do copies by hand... */
571 int timeout
, /* I - Timeout... */
572 int contimeout
) /* I - Connection timeout */
574 FILE *fp
; /* Job file */
575 char localhost
[255]; /* Local host name */
576 int error
; /* Error number */
577 struct stat filestats
; /* File statistics */
578 int lport
; /* LPD connection local port */
579 int fd
; /* LPD socket */
580 char control
[10240], /* LPD control 'file' */
581 *cptr
; /* Pointer into control file string */
582 char status
; /* Status byte from command */
583 char portname
[255]; /* Port name */
584 http_addrlist_t
*addrlist
, /* Address list */
585 *addr
; /* Socket address */
586 int copy
; /* Copies written */
587 time_t start_time
; /* Time of first connect */
589 int recoverable
; /* Recoverable error shown? */
590 #endif /* __APPLE__ */
591 size_t nbytes
; /* Number of bytes written */
592 off_t tbytes
; /* Total bytes written */
593 char buffer
[32768]; /* Output buffer */
594 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
595 struct sigaction action
; /* Actions for POSIX signals */
596 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
600 * Setup an alarm handler for timeouts...
603 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
604 sigset(SIGALRM
, lpd_timeout
);
605 #elif defined(HAVE_SIGACTION)
606 memset(&action
, 0, sizeof(action
));
608 sigemptyset(&action
.sa_mask
);
609 action
.sa_handler
= lpd_timeout
;
610 sigaction(SIGALRM
, &action
, NULL
);
612 signal(SIGALRM
, lpd_timeout
);
613 #endif /* HAVE_SIGSET */
616 * Find the printer...
619 sprintf(portname
, "%d", port
);
621 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
623 fprintf(stderr
, "ERROR: Unable to locate printer \'%s\'!\n",
625 return (CUPS_BACKEND_STOP
);
629 * Remember when we starting trying to connect to the printer...
634 #endif /* __APPLE__ */
635 start_time
= time(NULL
);
638 * Loop forever trying to print the file...
644 * First try to reserve a port for this connection...
647 fputs("STATE: +connecting-to-device\n", stderr
);
648 fprintf(stderr
, "INFO: Attempting to connect to host %s for printer %s\n",
651 for (lport
= reserve
== RESERVE_RFC1179
? 732 : 1024, addr
= addrlist
;;
655 * Stop if this job has been cancelled...
660 httpAddrFreeList(addrlist
);
662 return (CUPS_BACKEND_FAILED
);
666 * Choose the next priviledged port...
674 if (lport
< 721 && reserve
== RESERVE_RFC1179
)
680 if (geteuid() || !reserve
)
682 if (getuid() || !reserve
)
683 #endif /* HAVE_GETEUID */
686 * Just create a regular socket...
689 if ((fd
= socket(addr
->addr
.addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
691 perror("ERROR: Unable to create socket");
702 * We're running as root and want to comply with RFC 1179. Reserve a
703 * priviledged lport between 721 and 731...
706 if ((fd
= rresvport_af(&lport
, addr
->addr
.addr
.sa_family
)) < 0)
708 perror("ERROR: Unable to reserve port");
716 * Connect to the printer or server...
721 httpAddrFreeList(addrlist
);
725 return (CUPS_BACKEND_FAILED
);
728 if (!connect(fd
, &(addr
->addr
.addr
), httpAddrLength(&(addr
->addr
))))
738 if (getenv("CLASS") != NULL
)
741 * If the CLASS environment variable is set, the job was submitted
742 * to a class and not to a specific queue. In this case, we want
743 * to abort immediately so that the job can be requeued on the next
744 * available printer in the class.
747 fprintf(stderr
, "INFO: Unable to connect to %s, queuing on next printer in class...\n",
750 httpAddrFreeList(addrlist
);
753 * Sleep 5 seconds to keep the job from requeuing too rapidly...
758 return (CUPS_BACKEND_FAILED
);
761 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
762 error
== EHOSTUNREACH
)
764 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
766 fputs("ERROR: Printer not responding!\n", stderr
);
767 return (CUPS_BACKEND_FAILED
);
772 fprintf(stderr
, "WARNING: recoverable: "
773 "Network host \'%s\' is busy, down, or "
774 "unreachable; will retry in 30 seconds...\n",
777 fprintf(stderr
, "WARNING: "
778 "Network host \'%s\' is busy, down, or "
779 "unreachable; will retry in 30 seconds...\n",
781 #endif /* __APPLE__ */
784 else if (error
== EADDRINUSE
)
787 * Try on another port...
796 perror("ERROR: recoverable: "
797 "Unable to connect to printer; will retry in 30 seconds...");
800 "Unable to connect to printer; will retry in 30 seconds...");
801 #endif /* __APPLE__ */
810 * If we've shown a recoverable error make sure the printer proxies
811 * have a chance to see the recovered message. Not pretty but
812 * necessary for now...
815 fputs("INFO: recovered: \n", stderr
);
818 #endif /* __APPLE__ */
820 fputs("STATE: -connecting-to-device\n", stderr
);
821 fprintf(stderr
, "INFO: Connected to %s...\n", hostname
);
822 fprintf(stderr
, "DEBUG: Connected on ports %d (local %d)...\n", port
,
826 * Next, open the print file and figure out its size...
829 if (stat(filename
, &filestats
))
831 httpAddrFreeList(addrlist
);
834 perror("ERROR: unable to stat print file");
835 return (CUPS_BACKEND_FAILED
);
838 filestats
.st_size
*= manual_copies
;
840 if ((fp
= fopen(filename
, "rb")) == NULL
)
842 httpAddrFreeList(addrlist
);
845 perror("ERROR: unable to open print file for reading");
846 return (CUPS_BACKEND_FAILED
);
850 * Send a job header to the printer, specifying no banner page and
854 if (lpd_command(fd
, timeout
, "\002%s\n",
855 printer
)) /* Receive print job(s) */
857 httpAddrFreeList(addrlist
);
859 return (CUPS_BACKEND_FAILED
);
862 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
864 snprintf(control
, sizeof(control
),
865 "H%.31s\n" /* RFC 1179, Section 7.2 - host name <= 31 chars */
866 "P%.31s\n" /* RFC 1179, Section 7.2 - user name <= 31 chars */
867 "J%.99s\n", /* RFC 1179, Section 7.2 - job name <= 99 chars */
868 localhost
, user
, title
);
869 cptr
= control
+ strlen(control
);
873 snprintf(cptr
, sizeof(control
) - (cptr
- control
),
874 "C%.31s\n" /* RFC 1179, Section 7.2 - class name <= 31 chars */
877 cptr
+= strlen(cptr
);
882 snprintf(cptr
, sizeof(control
) - (cptr
- control
), "%cdfA%03d%.15s\n",
883 format
, (int)getpid() % 1000, localhost
);
884 cptr
+= strlen(cptr
);
888 snprintf(cptr
, sizeof(control
) - (cptr
- control
),
890 "N%.131s\n", /* RFC 1179, Section 7.2 - sourcefile name <= 131 chars */
891 (int)getpid() % 1000, localhost
, title
);
893 fprintf(stderr
, "DEBUG: Control file is:\n%s", control
);
895 if (order
== ORDER_CONTROL_DATA
)
897 if (lpd_command(fd
, timeout
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
898 (int)getpid() % 1000, localhost
))
900 httpAddrFreeList(addrlist
);
903 return (CUPS_BACKEND_FAILED
);
906 fprintf(stderr
, "INFO: Sending control file (%u bytes)\n",
907 (unsigned)strlen(control
));
909 if (lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
912 perror("ERROR: Unable to write control file");
918 if (read(fd
, &status
, 1) < 1)
920 fprintf(stderr
, "WARNING: Remote host did not respond with control "
921 "status byte after %d seconds!\n", timeout
);
929 fprintf(stderr
, "ERROR: Remote host did not accept control file (%d)\n",
932 fputs("INFO: Control file sent successfully\n", stderr
);
940 * Send the print file...
943 if (lpd_command(fd
, timeout
, "\003" CUPS_LLFMT
" dfA%03.3d%.15s\n",
944 CUPS_LLCAST filestats
.st_size
, (int)getpid() % 1000,
947 httpAddrFreeList(addrlist
);
950 return (CUPS_BACKEND_FAILED
);
953 fprintf(stderr
, "INFO: Sending data file (" CUPS_LLFMT
" bytes)\n",
954 CUPS_LLCAST filestats
.st_size
);
957 for (copy
= 0; copy
< manual_copies
; copy
++)
961 while ((nbytes
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0)
963 fprintf(stderr
, "INFO: Spooling LPR job, %.0f%% complete...\n",
964 100.0 * tbytes
/ filestats
.st_size
);
966 if (lpd_write(fd
, buffer
, nbytes
) < nbytes
)
968 perror("ERROR: Unable to send print file to printer");
976 if (tbytes
< filestats
.st_size
)
978 else if (lpd_write(fd
, "", 1) < 1)
980 perror("ERROR: Unable to send trailing nul to printer");
986 * Read the status byte from the printer; if we can't read the byte
987 * back now, we should set status to "errno", however at this point
988 * we know the printer got the whole file and we don't necessarily
989 * want to requeue it over and over...
994 if (recv(fd
, &status
, 1, 0) < 1)
996 fprintf(stderr
, "WARNING: Remote host did not respond with data "
997 "status byte after %d seconds!\n", timeout
);
1005 fprintf(stderr
, "ERROR: Remote host did not accept data file (%d)\n",
1008 fputs("INFO: Data file sent successfully\n", stderr
);
1011 if (status
== 0 && order
== ORDER_DATA_CONTROL
)
1013 if (lpd_command(fd
, timeout
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1014 (int)getpid() % 1000, localhost
))
1016 httpAddrFreeList(addrlist
);
1019 return (CUPS_BACKEND_FAILED
);
1022 fprintf(stderr
, "INFO: Sending control file (%lu bytes)\n",
1023 (unsigned long)strlen(control
));
1025 if (lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1028 perror("ERROR: Unable to write control file");
1034 if (read(fd
, &status
, 1) < 1)
1036 fprintf(stderr
, "WARNING: Remote host did not respond with control "
1037 "status byte after %d seconds!\n", timeout
);
1045 fprintf(stderr
, "ERROR: Remote host did not accept control file (%d)\n",
1048 fputs("INFO: Control file sent successfully\n", stderr
);
1052 * Close the socket connection and input file...
1060 httpAddrFreeList(addrlist
);
1062 return (CUPS_BACKEND_OK
);
1066 * Waiting for a retry...
1072 httpAddrFreeList(addrlist
);
1075 * If we get here, then the job has been cancelled...
1078 return (CUPS_BACKEND_FAILED
);
1083 * 'lpd_timeout()' - Handle timeout alarms...
1087 lpd_timeout(int sig
) /* I - Signal number */
1091 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
1092 signal(SIGALRM
, lpd_timeout
);
1093 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
1098 * 'lpd_write()' - Write a buffer of data to an LPD server.
1101 static int /* O - Number of bytes written or -1 on error */
1102 lpd_write(int lpd_fd
, /* I - LPD socket */
1103 char *buffer
, /* I - Buffer to write */
1104 int length
) /* I - Number of bytes to write */
1106 int bytes
, /* Number of bytes written */
1107 total
; /* Total number of bytes written */
1114 while ((bytes
= send(lpd_fd
, buffer
, length
- total
, 0)) >= 0)
1119 if (total
== length
)
1130 #ifndef HAVE_RRESVPORT_AF
1132 * 'rresvport_af()' - A simple implementation of rresvport_af().
1135 static int /* O - Socket or -1 on error */
1136 rresvport_af(int *port
, /* IO - Port number to bind to */
1137 int family
) /* I - Address family */
1139 http_addr_t addr
; /* Socket address */
1140 int fd
; /* Socket file descriptor */
1144 * Try to create an IPv4 socket...
1147 if ((fd
= socket(family
, SOCK_STREAM
, 0)) < 0)
1151 * Initialize the address buffer...
1154 memset(&addr
, 0, sizeof(addr
));
1155 addr
.addr
.sa_family
= family
;
1158 * Try to bind the socket to a reserved port...
1164 * Set the port number...
1168 if (family
== AF_INET6
)
1169 addr
.ipv6
.sin6_port
= htons(*port
);
1171 # endif /* AF_INET6 */
1172 addr
.ipv4
.sin_port
= htons(*port
);
1175 * Try binding the port to the socket; return if all is OK...
1178 if (!bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1182 * Stop if we have any error other than "address already in use"...
1185 if (errno
!= EADDRINUSE
)
1197 * Try the next port...
1204 * Wasn't able to bind to a reserved port, so close the socket and return
1216 #endif /* !HAVE_RRESVPORT_AF */
1220 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1224 sigterm_handler(int sig
) /* I - Signal */
1226 (void)sig
; /* remove compiler warnings... */