2 * "$Id: lpd.c 5886 2006-08-24 19:53:17Z mike $"
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) */
264 /* Get the default timeout from a system preference... */
266 CFPropertyListRef pvalue
; /* Preference value */
267 SInt32 toval
; /* Timeout value */
270 pvalue
= CFPreferencesCopyValue(CFSTR("timeout"),
271 CFSTR("com.apple.print.backends"),
272 kCFPreferencesAnyUser
,
273 kCFPreferencesCurrentHost
);
276 if (CFGetTypeID(pvalue
) == CFNumberGetTypeID())
278 CFNumberGetValue(pvalue
, kCFNumberSInt32Type
, &toval
);
279 contimeout
= (int)toval
;
287 #endif /* __APPLE__ */
289 if ((options
= strchr(resource
, '?')) != NULL
)
292 * Yup, terminate the device name string and move to the first
293 * character of the options...
308 for (ptr
= name
; *options
&& *options
!= '=';)
309 if (ptr
< (name
+ sizeof(name
) - 1))
321 for (ptr
= value
; *options
&& *options
!= '+' && *options
!= '&';)
322 if (ptr
< (value
+ sizeof(value
) - 1))
326 if (*options
== '+' || *options
== '&')
333 * Process the option...
336 if (!strcasecmp(name
, "banner"))
342 banner
= !value
[0] || !strcasecmp(value
, "on") ||
343 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
345 else if (!strcasecmp(name
, "format") && value
[0])
348 * Set output format...
351 if (strchr("cdfglnoprtv", value
[0]))
354 fprintf(stderr
, "ERROR: Unknown format character \"%c\"\n", value
[0]);
356 else if (!strcasecmp(name
, "order") && value
[0])
359 * Set control/data order...
362 if (!strcasecmp(value
, "control,data"))
363 order
= ORDER_CONTROL_DATA
;
364 else if (!strcasecmp(value
, "data,control"))
365 order
= ORDER_DATA_CONTROL
;
367 fprintf(stderr
, "ERROR: Unknown file order \"%s\"\n", value
);
369 else if (!strcasecmp(name
, "reserve"))
372 * Set port reservation mode...
375 if (!value
[0] || !strcasecmp(value
, "on") ||
376 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true") ||
377 !strcasecmp(value
, "rfc1179"))
378 reserve
= RESERVE_RFC1179
;
379 else if (!strcasecmp(value
, "any"))
380 reserve
= RESERVE_ANY
;
382 reserve
= RESERVE_NONE
;
384 else if (!strcasecmp(name
, "manual_copies"))
387 * Set manual copies...
390 manual_copies
= !value
[0] || !strcasecmp(value
, "on") ||
391 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
393 else if (!strcasecmp(name
, "sanitize_title"))
396 * Set sanitize title...
399 sanitize_title
= !value
[0] || !strcasecmp(value
, "on") ||
400 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
402 else if (!strcasecmp(name
, "timeout"))
409 timeout
= atoi(value
);
411 else if (!strcasecmp(name
, "contimeout"))
418 contimeout
= atoi(value
);
424 * Sanitize the document title...
427 strlcpy(title
, argv
[3], sizeof(title
));
432 * Sanitize the title string so that we don't cause problems on
436 for (ptr
= title
; *ptr
; ptr
++)
437 if (!isalnum(*ptr
& 255) && !isspace(*ptr
& 255))
449 manual_copies
= atoi(argv
[4]);
455 copies
= atoi(argv
[4]);
458 status
= lpd_queue(hostname
, port
, resource
+ 1, filename
,
459 username
, title
, copies
,
460 banner
, format
, order
, reserve
, manual_copies
,
461 timeout
, contimeout
);
464 fprintf(stderr
, "PAGE: 1 %d\n", atoi(argv
[4]));
467 status
= lpd_queue(hostname
, port
, resource
+ 1, filename
,
469 banner
, format
, order
, reserve
, 1,
470 timeout
, contimeout
);
473 * Remove the temporary file if necessary...
480 * Return the queue status...
488 * 'lpd_command()' - Send an LPR command sequence and wait for a reply.
491 static int /* O - Status of command */
492 lpd_command(int fd
, /* I - Socket connection to LPD host */
493 int timeout
, /* I - Seconds to wait for a response */
494 char *format
, /* I - printf()-style format string */
495 ...) /* I - Additional args as necessary */
497 va_list ap
; /* Argument pointer */
498 char buf
[1024]; /* Output buffer */
499 int bytes
; /* Number of bytes to output */
500 char status
; /* Status from command */
504 * Don't try to send commands if the job has been cancelled...
511 * Format the string...
514 va_start(ap
, format
);
515 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
518 fprintf(stderr
, "DEBUG: lpd_command %2.2x %s", buf
[0], buf
+ 1);
521 * Send the command...
524 fprintf(stderr
, "DEBUG: Sending command string (%d bytes)...\n", bytes
);
526 if (lpd_write(fd
, buf
, bytes
) < bytes
)
528 perror("ERROR: Unable to send LPD command");
533 * Read back the status from the command and return it...
536 fprintf(stderr
, "DEBUG: Reading command status...\n");
540 if (recv(fd
, &status
, 1, 0) < 1)
542 fprintf(stderr
, "WARNING: Remote host did not respond with command "
543 "status byte after %d seconds!\n", timeout
);
549 fprintf(stderr
, "DEBUG: lpd_command returning %d\n", status
);
556 * 'lpd_queue()' - Queue a file using the Line Printer Daemon protocol.
559 static int /* O - Zero on success, non-zero on failure */
560 lpd_queue(const char *hostname
, /* I - Host to connect to */
561 int port
, /* I - Port to connect on */
562 const char *printer
, /* I - Printer/queue name */
563 const char *filename
, /* I - File to print */
564 const char *user
, /* I - Requesting user */
565 const char *title
, /* I - Job title */
566 int copies
, /* I - Number of copies */
567 int banner
, /* I - Print LPD banner? */
568 int format
, /* I - Format specifier */
569 int order
, /* I - Order of data/control files */
570 int reserve
, /* I - Reserve ports? */
571 int manual_copies
, /* I - Do copies by hand... */
572 int timeout
, /* I - Timeout... */
573 int contimeout
) /* I - Connection timeout */
575 FILE *fp
; /* Job file */
576 char localhost
[255]; /* Local host name */
577 int error
; /* Error number */
578 struct stat filestats
; /* File statistics */
579 int lport
; /* LPD connection local port */
580 int fd
; /* LPD socket */
581 char control
[10240], /* LPD control 'file' */
582 *cptr
; /* Pointer into control file string */
583 char status
; /* Status byte from command */
584 char portname
[255]; /* Port name */
585 http_addrlist_t
*addrlist
, /* Address list */
586 *addr
; /* Socket address */
587 int copy
; /* Copies written */
588 time_t start_time
; /* Time of first connect */
590 int recoverable
; /* Recoverable error shown? */
591 #endif /* __APPLE__ */
592 size_t nbytes
; /* Number of bytes written */
593 off_t tbytes
; /* Total bytes written */
594 char buffer
[32768]; /* Output buffer */
595 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
596 struct sigaction action
; /* Actions for POSIX signals */
597 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
601 * Setup an alarm handler for timeouts...
604 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
605 sigset(SIGALRM
, lpd_timeout
);
606 #elif defined(HAVE_SIGACTION)
607 memset(&action
, 0, sizeof(action
));
609 sigemptyset(&action
.sa_mask
);
610 action
.sa_handler
= lpd_timeout
;
611 sigaction(SIGALRM
, &action
, NULL
);
613 signal(SIGALRM
, lpd_timeout
);
614 #endif /* HAVE_SIGSET */
617 * Find the printer...
620 sprintf(portname
, "%d", port
);
622 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
624 fprintf(stderr
, "ERROR: Unable to locate printer \'%s\'!\n",
626 return (CUPS_BACKEND_STOP
);
630 * Remember when we starting trying to connect to the printer...
635 #endif /* __APPLE__ */
636 start_time
= time(NULL
);
639 * Loop forever trying to print the file...
645 * First try to reserve a port for this connection...
648 fputs("STATE: +connecting-to-device\n", stderr
);
649 fprintf(stderr
, "INFO: Attempting to connect to host %s for printer %s\n",
652 for (lport
= reserve
== RESERVE_RFC1179
? 732 : 1024, addr
= addrlist
;;
656 * Stop if this job has been cancelled...
661 httpAddrFreeList(addrlist
);
663 return (CUPS_BACKEND_FAILED
);
667 * Choose the next priviledged port...
675 if (lport
< 721 && reserve
== RESERVE_RFC1179
)
681 if (geteuid() || !reserve
)
683 if (getuid() || !reserve
)
684 #endif /* HAVE_GETEUID */
687 * Just create a regular socket...
690 if ((fd
= socket(addr
->addr
.addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
692 perror("ERROR: Unable to create socket");
703 * We're running as root and want to comply with RFC 1179. Reserve a
704 * priviledged lport between 721 and 731...
707 if ((fd
= rresvport_af(&lport
, addr
->addr
.addr
.sa_family
)) < 0)
709 perror("ERROR: Unable to reserve port");
717 * Connect to the printer or server...
722 httpAddrFreeList(addrlist
);
726 return (CUPS_BACKEND_FAILED
);
729 if (!connect(fd
, &(addr
->addr
.addr
), httpAddrLength(&(addr
->addr
))))
739 if (getenv("CLASS") != NULL
)
742 * If the CLASS environment variable is set, the job was submitted
743 * to a class and not to a specific queue. In this case, we want
744 * to abort immediately so that the job can be requeued on the next
745 * available printer in the class.
748 fprintf(stderr
, "INFO: Unable to connect to %s, queuing on next printer in class...\n",
751 httpAddrFreeList(addrlist
);
754 * Sleep 5 seconds to keep the job from requeuing too rapidly...
759 return (CUPS_BACKEND_FAILED
);
762 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
763 error
== EHOSTUNREACH
)
765 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
767 fputs("ERROR: Printer not responding!\n", stderr
);
768 return (CUPS_BACKEND_FAILED
);
773 fprintf(stderr
, "WARNING: recoverable: "
774 "Network host \'%s\' is busy, down, or "
775 "unreachable; will retry in 30 seconds...\n",
778 fprintf(stderr
, "WARNING: "
779 "Network host \'%s\' is busy, down, or "
780 "unreachable; will retry in 30 seconds...\n",
782 #endif /* __APPLE__ */
785 else if (error
== EADDRINUSE
)
788 * Try on another port...
797 perror("ERROR: recoverable: "
798 "Unable to connect to printer; will retry in 30 seconds...");
801 "Unable to connect to printer; will retry in 30 seconds...");
802 #endif /* __APPLE__ */
811 * If we've shown a recoverable error make sure the printer proxies
812 * have a chance to see the recovered message. Not pretty but
813 * necessary for now...
816 fputs("INFO: recovered: \n", stderr
);
819 #endif /* __APPLE__ */
821 fputs("STATE: -connecting-to-device\n", stderr
);
822 fprintf(stderr
, "INFO: Connected to %s...\n", hostname
);
823 fprintf(stderr
, "DEBUG: Connected on ports %d (local %d)...\n", port
,
827 * Next, open the print file and figure out its size...
830 if (stat(filename
, &filestats
))
832 httpAddrFreeList(addrlist
);
835 perror("ERROR: unable to stat print file");
836 return (CUPS_BACKEND_FAILED
);
839 filestats
.st_size
*= manual_copies
;
841 if ((fp
= fopen(filename
, "rb")) == NULL
)
843 httpAddrFreeList(addrlist
);
846 perror("ERROR: unable to open print file for reading");
847 return (CUPS_BACKEND_FAILED
);
851 * Send a job header to the printer, specifying no banner page and
855 if (lpd_command(fd
, timeout
, "\002%s\n",
856 printer
)) /* Receive print job(s) */
858 httpAddrFreeList(addrlist
);
860 return (CUPS_BACKEND_FAILED
);
863 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
865 snprintf(control
, sizeof(control
),
866 "H%.31s\n" /* RFC 1179, Section 7.2 - host name <= 31 chars */
867 "P%.31s\n" /* RFC 1179, Section 7.2 - user name <= 31 chars */
868 "J%.99s\n", /* RFC 1179, Section 7.2 - job name <= 99 chars */
869 localhost
, user
, title
);
870 cptr
= control
+ strlen(control
);
874 snprintf(cptr
, sizeof(control
) - (cptr
- control
),
875 "C%.31s\n" /* RFC 1179, Section 7.2 - class name <= 31 chars */
878 cptr
+= strlen(cptr
);
883 snprintf(cptr
, sizeof(control
) - (cptr
- control
), "%cdfA%03d%.15s\n",
884 format
, (int)getpid() % 1000, localhost
);
885 cptr
+= strlen(cptr
);
889 snprintf(cptr
, sizeof(control
) - (cptr
- control
),
891 "N%.131s\n", /* RFC 1179, Section 7.2 - sourcefile name <= 131 chars */
892 (int)getpid() % 1000, localhost
, title
);
894 fprintf(stderr
, "DEBUG: Control file is:\n%s", control
);
896 if (order
== ORDER_CONTROL_DATA
)
898 if (lpd_command(fd
, timeout
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
899 (int)getpid() % 1000, localhost
))
901 httpAddrFreeList(addrlist
);
904 return (CUPS_BACKEND_FAILED
);
907 fprintf(stderr
, "INFO: Sending control file (%u bytes)\n",
908 (unsigned)strlen(control
));
910 if (lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
913 perror("ERROR: Unable to write control file");
919 if (read(fd
, &status
, 1) < 1)
921 fprintf(stderr
, "WARNING: Remote host did not respond with control "
922 "status byte after %d seconds!\n", timeout
);
930 fprintf(stderr
, "ERROR: Remote host did not accept control file (%d)\n",
933 fputs("INFO: Control file sent successfully\n", stderr
);
941 * Send the print file...
944 if (lpd_command(fd
, timeout
, "\003" CUPS_LLFMT
" dfA%03.3d%.15s\n",
945 CUPS_LLCAST filestats
.st_size
, (int)getpid() % 1000,
948 httpAddrFreeList(addrlist
);
951 return (CUPS_BACKEND_FAILED
);
954 fprintf(stderr
, "INFO: Sending data file (" CUPS_LLFMT
" bytes)\n",
955 CUPS_LLCAST filestats
.st_size
);
958 for (copy
= 0; copy
< manual_copies
; copy
++)
962 while ((nbytes
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0)
964 fprintf(stderr
, "INFO: Spooling LPR job, %.0f%% complete...\n",
965 100.0 * tbytes
/ filestats
.st_size
);
967 if (lpd_write(fd
, buffer
, nbytes
) < nbytes
)
969 perror("ERROR: Unable to send print file to printer");
977 if (tbytes
< filestats
.st_size
)
979 else if (lpd_write(fd
, "", 1) < 1)
981 perror("ERROR: Unable to send trailing nul to printer");
987 * Read the status byte from the printer; if we can't read the byte
988 * back now, we should set status to "errno", however at this point
989 * we know the printer got the whole file and we don't necessarily
990 * want to requeue it over and over...
995 if (recv(fd
, &status
, 1, 0) < 1)
997 fprintf(stderr
, "WARNING: Remote host did not respond with data "
998 "status byte after %d seconds!\n", timeout
);
1006 fprintf(stderr
, "ERROR: Remote host did not accept data file (%d)\n",
1009 fputs("INFO: Data file sent successfully\n", stderr
);
1012 if (status
== 0 && order
== ORDER_DATA_CONTROL
)
1014 if (lpd_command(fd
, timeout
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1015 (int)getpid() % 1000, localhost
))
1017 httpAddrFreeList(addrlist
);
1020 return (CUPS_BACKEND_FAILED
);
1023 fprintf(stderr
, "INFO: Sending control file (%lu bytes)\n",
1024 (unsigned long)strlen(control
));
1026 if (lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1029 perror("ERROR: Unable to write control file");
1035 if (read(fd
, &status
, 1) < 1)
1037 fprintf(stderr
, "WARNING: Remote host did not respond with control "
1038 "status byte after %d seconds!\n", timeout
);
1046 fprintf(stderr
, "ERROR: Remote host did not accept control file (%d)\n",
1049 fputs("INFO: Control file sent successfully\n", stderr
);
1053 * Close the socket connection and input file...
1061 httpAddrFreeList(addrlist
);
1063 return (CUPS_BACKEND_OK
);
1067 * Waiting for a retry...
1073 httpAddrFreeList(addrlist
);
1076 * If we get here, then the job has been cancelled...
1079 return (CUPS_BACKEND_FAILED
);
1084 * 'lpd_timeout()' - Handle timeout alarms...
1088 lpd_timeout(int sig
) /* I - Signal number */
1092 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
1093 signal(SIGALRM
, lpd_timeout
);
1094 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
1099 * 'lpd_write()' - Write a buffer of data to an LPD server.
1102 static int /* O - Number of bytes written or -1 on error */
1103 lpd_write(int lpd_fd
, /* I - LPD socket */
1104 char *buffer
, /* I - Buffer to write */
1105 int length
) /* I - Number of bytes to write */
1107 int bytes
, /* Number of bytes written */
1108 total
; /* Total number of bytes written */
1115 while ((bytes
= send(lpd_fd
, buffer
, length
- total
, 0)) >= 0)
1120 if (total
== length
)
1131 #ifndef HAVE_RRESVPORT_AF
1133 * 'rresvport_af()' - A simple implementation of rresvport_af().
1136 static int /* O - Socket or -1 on error */
1137 rresvport_af(int *port
, /* IO - Port number to bind to */
1138 int family
) /* I - Address family */
1140 http_addr_t addr
; /* Socket address */
1141 int fd
; /* Socket file descriptor */
1145 * Try to create an IPv4 socket...
1148 if ((fd
= socket(family
, SOCK_STREAM
, 0)) < 0)
1152 * Initialize the address buffer...
1155 memset(&addr
, 0, sizeof(addr
));
1156 addr
.addr
.sa_family
= family
;
1159 * Try to bind the socket to a reserved port...
1165 * Set the port number...
1169 if (family
== AF_INET6
)
1170 addr
.ipv6
.sin6_port
= htons(*port
);
1172 # endif /* AF_INET6 */
1173 addr
.ipv4
.sin_port
= htons(*port
);
1176 * Try binding the port to the socket; return if all is OK...
1179 if (!bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1183 * Stop if we have any error other than "address already in use"...
1186 if (errno
!= EADDRINUSE
)
1198 * Try the next port...
1205 * Wasn't able to bind to a reserved port, so close the socket and return
1217 #endif /* !HAVE_RRESVPORT_AF */
1221 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1225 sigterm_handler(int sig
) /* I - Signal */
1227 (void)sig
; /* remove compiler warnings... */
1234 * End of "$Id: lpd.c 5886 2006-08-24 19:53:17Z mike $".