1 --- p910nd-0.7/p910nd.c.orig 2005-09-29 11:31:02.442914400 +0200
2 +++ p910nd-0.7/p910nd.c 2005-09-29 11:31:49.236800648 +0200
5 #define LOCKFILE "/var/lock/subsys/p910%cd"
7 -#define PRINTERFILE "/dev/lp%c"
8 -#define LOGOPTS LOG_ERR
9 +#define PRINTERFILE "/dev/printers/%c"
11 static char *progname;
12 static char version[] = "p910nd Version 0.7";
14 fprintf(stdout, "%s \n", version);
17 -FILE *open_printer(int lpnumber)
18 +int open_printer(int lpnumber)
22 char lpname[sizeof(PRINTERFILE)];
29 - if ((f = fopen(device, bidir ? "w+" : "w")) == NULL)
30 + if ((lp = open(device, bidir ? O_RDWR : O_WRONLY)) == -1)
32 - syslog(LOGOPTS, "%s: %m\n", device);
34 + syslog(LOG_ERR, "%s: %m\n", device);
36 + /* fallback to /dev/null if device is not available
37 + * otherwise windows spooler will screw up
39 + lp = open("/dev/null", bidir ? O_RDWR : O_WRONLY);
45 int get_lock(int lpnumber)
47 (void)snprintf(lockname, sizeof(lockname), LOCKFILE, lpnumber);
48 if ((lockfd = open(lockname, O_CREAT|O_RDWR)) < 0)
50 - syslog(LOGOPTS, "%s: %m\n", lockname);
51 + syslog(LOG_ERR, "%s: %m\n", lockname);
54 memset(&lplock, 0, sizeof(lplock));
56 lplock.l_pid = getpid();
57 if (fcntl(lockfd, F_SETLKW, &lplock) < 0)
59 - syslog(LOGOPTS, "%s: %m\n", lockname);
60 + syslog(LOG_ERR, "%s: %m\n", lockname);
68 +ssize_t safe_write(int fd, char *buf, size_t count)
72 + while (offset < count) {
73 + ssize_t n = write(fd, buf + offset, count - offset);
75 + if (n < 0 && errno != EINTR)
85 /* Copy network socket to FILE f until EOS */
86 -int copy_stream(int fd, FILE *f)
87 +int copy_stream(int fd, int lp)
90 + int nread, rcvd = 0, sent = 0;
96 - if ((nf = fdopen(fd, "w")) == NULL) {
97 - syslog(LOGOPTS, "fdopen: %m\n");
102 - int maxfd = fileno(f) > fd ? fileno(f) : fd;
103 + int maxfd = lp > fd ? lp : fd;
105 - FD_SET(fileno(f), &readfds);
106 + FD_SET(lp, &readfds);
107 FD_SET(fd, &readfds);
108 result = select(maxfd + 1, &readfds, 0, 0, 0);
110 @@ -169,43 +184,54 @@
111 nread = read(fd, buffer, sizeof(buffer));
114 - (void)fwrite(buffer, sizeof(char), nread, f);
115 + if (safe_write(lp, buffer, nread) < 0) {
116 + syslog(LOG_ERR, "write: %m\n");
121 - if (FD_ISSET(fileno(f), &readfds)) {
122 - nread = read(fileno(f), buffer, sizeof(buffer));
123 - if (nread > 0 && nf != NULL) {
124 - (void)fwrite(buffer, sizeof(char), nread, nf);
126 + if (FD_ISSET(lp, &readfds)) {
127 + nread = read(lp, buffer, sizeof(buffer));
129 + safe_write(fd, buffer, nread);
136 + syslog(LOG_NOTICE, "Finished job: %d bytes received, %d bytes sent\n",
140 - while ((nread = read(fd, buffer, sizeof(buffer))) > 0)
141 - (void)fwrite(buffer, sizeof(char), nread, f);
143 + while ((nread = read(fd, buffer, sizeof(buffer))) > 0) {
144 + if (safe_write(lp, buffer, nread) < 0) {
145 + syslog(LOG_ERR, "write: %m\n");
150 + syslog(LOG_NOTICE, "Finished job: %d bytes received\n", rcvd);
155 void one_job(int lpnumber)
159 struct sockaddr_in client;
160 socklen_t clientlen = sizeof(client);
162 if (getpeername(0, (struct sockaddr*) &client, &clientlen) >= 0)
163 - syslog(LOGOPTS, "Connection from %s port %hu\n",
164 + syslog(LOG_NOTICE, "Connection from %s port %hu\n",
165 inet_ntoa(client.sin_addr),
166 ntohs(client.sin_port));
167 if (get_lock(lpnumber) == 0)
169 - f = open_printer(lpnumber);
170 - if (copy_stream(0, f) < 0)
171 - syslog(LOGOPTS, "copy_stream: %m\n");
173 + if ((lp = open_printer(lpnumber)) != -1)
175 + if (copy_stream(0, lp) < 0)
176 + syslog(LOG_ERR, "copy_stream: %m\n");
183 #ifdef USE_GETPROTOBYNAME
184 struct protoent *proto;
186 - int netfd, fd, one = 1;
187 + int netfd, fd, lp, one = 1;
189 struct sockaddr_in netaddr, client;
190 char pidfilename[sizeof(PIDFILE)];
195 - syslog(LOGOPTS, "fork: %m\n");
196 + syslog(LOG_ERR, "fork: %m\n");
200 @@ -236,14 +262,14 @@
201 resourcelimit.rlim_max = 0;
202 if (getrlimit(RLIMIT_NOFILE, &resourcelimit) < 0)
204 - syslog(LOGOPTS, "getrlimit: %m\n");
205 + syslog(LOG_ERR, "getrlimit: %m\n");
208 for (fd = 0; fd < resourcelimit.rlim_max; ++fd)
212 - syslog(LOGOPTS, "setsid: %m\n");
213 + syslog(LOG_ERR, "setsid: %m\n");
218 (void)snprintf(pidfilename, sizeof(pidfilename), PIDFILE, lpnumber);
219 if ((f = fopen(pidfilename, "w")) == NULL)
221 - syslog(LOGOPTS, "%s: %m\n", pidfilename);
222 + syslog(LOG_ERR, "%s: %m\n", pidfilename);
225 (void)fprintf(f, "%d\n", getpid());
226 @@ -262,11 +288,10 @@
227 if (get_lock(lpnumber) == 0)
230 - f = open_printer(lpnumber);
231 #ifdef USE_GETPROTOBYNAME
232 if ((proto = getprotobyname("tcp")) == NULL)
234 - syslog(LOGOPTS, "Cannot find protocol for TCP!\n");
235 + syslog(LOG_ERR, "Cannot find protocol for TCP!\n");
238 if ((netfd = socket(AF_INET, SOCK_STREAM, proto->p_proto)) < 0)
239 @@ -274,12 +299,12 @@
240 if ((netfd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) < 0)
243 - syslog(LOGOPTS, "socket: %m\n");
244 + syslog(LOG_ERR, "socket: %m\n");
247 if (setsockopt(netfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
249 - syslog(LOGOPTS, "setsocketopt: %m\n");
250 + syslog(LOG_ERR, "setsocketopt: %m\n");
253 netaddr.sin_port = htons(BASEPORT + lpnumber - '0');
254 @@ -287,12 +312,12 @@
255 memset(netaddr.sin_zero, 0, sizeof(netaddr.sin_zero));
256 if (bind(netfd, (struct sockaddr*) &netaddr, sizeof(netaddr)) < 0)
258 - syslog(LOGOPTS, "bind: %m\n");
259 + syslog(LOG_ERR, "bind: %m\n");
262 if (listen(netfd, 5) < 0)
264 - syslog(LOGOPTS, "listen: %m\n");
265 + syslog(LOG_ERR, "listen: %m\n");
268 clientlen = sizeof(client);
269 @@ -302,22 +327,26 @@
271 if (hosts_ctl("p910nd", STRING_UNKNOWN,
272 inet_ntoa(client.sin_addr), STRING_UNKNOWN) == 0) {
273 - syslog(LOGOPTS, "Connection from %s port %hd rejected\n",
274 + syslog(LOG_ERR, "Connection from %s port %hu rejected\n",
275 inet_ntoa(client.sin_addr),
276 ntohs(client.sin_port));
281 - syslog(LOGOPTS, "Connection from %s port %hd accepted\n",
282 + syslog(LOG_NOTICE, "Connection from %s port %hu accepted\n",
283 inet_ntoa(client.sin_addr),
284 ntohs(client.sin_port));
285 /*write(fd, "Printing", 8);*/
286 - if (copy_stream(fd, f) < 0)
287 - syslog(LOGOPTS, "copy_stream: %m\n");
288 + if ((lp = open_printer(lpnumber)) != -1)
290 + if (copy_stream(fd, lp) < 0)
291 + syslog(LOG_ERR, "copy_stream: %m\n");
296 - syslog(LOGOPTS, "accept: %m\n");
297 + syslog(LOG_ERR, "accept: %m\n");
302 if (getsockname(0, (struct sockaddr*) &bind_addr, &ba_len) == 0)
303 return (0); /* under inetd */
304 if (errno != ENOTSOCK) /* strange... */
305 - syslog(LOGOPTS, "getsockname: %m\n");
306 + syslog(LOG_ERR, "getsockname: %m\n");