update to new upstream release (v0.9.2),
[openwrt.git] / openwrt / package / p910nd / patches / 200-p910nd-0.7.patch
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
3 @@ -73,8 +73,7 @@
4 #else
5 #define LOCKFILE "/var/lock/subsys/p910%cd"
6 #endif
7 -#define PRINTERFILE "/dev/lp%c"
8 -#define LOGOPTS LOG_ERR
9 +#define PRINTERFILE "/dev/printers/%c"
10
11 static char *progname;
12 static char version[] = "p910nd Version 0.7";
13 @@ -93,9 +92,9 @@
14 fprintf(stdout, "%s \n", version);
15 }
16
17 -FILE *open_printer(int lpnumber)
18 +int open_printer(int lpnumber)
19 {
20 - FILE *f;
21 + int lp;
22 char lpname[sizeof(PRINTERFILE)];
23
24 #ifdef TESTING
25 @@ -105,12 +104,16 @@
26 #endif
27 if (device == 0)
28 device = lpname;
29 - if ((f = fopen(device, bidir ? "w+" : "w")) == NULL)
30 + if ((lp = open(device, bidir ? O_RDWR : O_WRONLY)) == -1)
31 {
32 - syslog(LOGOPTS, "%s: %m\n", device);
33 - exit(1);
34 + syslog(LOG_ERR, "%s: %m\n", device);
35 +
36 + /* fallback to /dev/null if device is not available
37 + * otherwise windows spooler will screw up
38 + */
39 + lp = open("/dev/null", bidir ? O_RDWR : O_WRONLY);
40 }
41 - return (f);
42 + return (lp);
43 }
44
45 int get_lock(int lpnumber)
46 @@ -121,7 +124,7 @@
47 (void)snprintf(lockname, sizeof(lockname), LOCKFILE, lpnumber);
48 if ((lockfd = open(lockname, O_CREAT|O_RDWR)) < 0)
49 {
50 - syslog(LOGOPTS, "%s: %m\n", lockname);
51 + syslog(LOG_ERR, "%s: %m\n", lockname);
52 return (0);
53 }
54 memset(&lplock, 0, sizeof(lplock));
55 @@ -129,7 +132,7 @@
56 lplock.l_pid = getpid();
57 if (fcntl(lockfd, F_SETLKW, &lplock) < 0)
58 {
59 - syslog(LOGOPTS, "%s: %m\n", lockname);
60 + syslog(LOG_ERR, "%s: %m\n", lockname);
61 return (0);
62 }
63 return (1);
64 @@ -141,24 +144,36 @@
65 (void)close(lockfd);
66 }
67
68 +ssize_t safe_write(int fd, char *buf, size_t count)
69 +{
70 + size_t offset = 0;
71 +
72 + while (offset < count) {
73 + ssize_t n = write(fd, buf + offset, count - offset);
74 +
75 + if (n < 0 && errno != EINTR)
76 + return n;
77 +
78 + if (n > 0)
79 + offset += n;
80 + }
81 +
82 + return offset;
83 +}
84 +
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)
88 {
89 - int nread;
90 + int nread, rcvd = 0, sent = 0;
91 char buffer[8192];
92
93 if (bidir) {
94 - FILE *nf;
95 -
96 - if ((nf = fdopen(fd, "w")) == NULL) {
97 - syslog(LOGOPTS, "fdopen: %m\n");
98 - }
99 for (;;) {
100 fd_set readfds;
101 int result;
102 - int maxfd = fileno(f) > fd ? fileno(f) : fd;
103 + int maxfd = lp > fd ? lp : fd;
104 FD_ZERO(&readfds);
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);
109 if (result < 0)
110 @@ -169,43 +184,54 @@
111 nread = read(fd, buffer, sizeof(buffer));
112 if (nread <= 0)
113 break;
114 - (void)fwrite(buffer, sizeof(char), nread, f);
115 + if (safe_write(lp, buffer, nread) < 0) {
116 + syslog(LOG_ERR, "write: %m\n");
117 + break;
118 + }
119 + rcvd += nread;
120 }
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);
125 - (void)fflush(nf);
126 + if (FD_ISSET(lp, &readfds)) {
127 + nread = read(lp, buffer, sizeof(buffer));
128 + if (nread > 0) {
129 + safe_write(fd, buffer, nread);
130 + sent += nread;
131 }
132 }
133 }
134 - (void)fflush(f);
135 - (void)fclose(nf);
136 + syslog(LOG_NOTICE, "Finished job: %d bytes received, %d bytes sent\n",
137 + rcvd, sent);
138 return (0);
139 } else {
140 - while ((nread = read(fd, buffer, sizeof(buffer))) > 0)
141 - (void)fwrite(buffer, sizeof(char), nread, f);
142 - (void)fflush(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");
146 + break;
147 + }
148 + rcvd += nread;
149 + }
150 + syslog(LOG_NOTICE, "Finished job: %d bytes received\n", rcvd);
151 return (nread);
152 }
153 }
154
155 void one_job(int lpnumber)
156 {
157 - FILE *f;
158 + int lp;
159 struct sockaddr_in client;
160 socklen_t clientlen = sizeof(client);
161
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)
168 return;
169 - f = open_printer(lpnumber);
170 - if (copy_stream(0, f) < 0)
171 - syslog(LOGOPTS, "copy_stream: %m\n");
172 - fclose(f);
173 + if ((lp = open_printer(lpnumber)) != -1)
174 + {
175 + if (copy_stream(0, lp) < 0)
176 + syslog(LOG_ERR, "copy_stream: %m\n");
177 + close(lp);
178 + }
179 free_lock();
180 }
181
182 @@ -215,7 +241,7 @@
183 #ifdef USE_GETPROTOBYNAME
184 struct protoent *proto;
185 #endif
186 - int netfd, fd, one = 1;
187 + int netfd, fd, lp, one = 1;
188 socklen_t clientlen;
189 struct sockaddr_in netaddr, client;
190 char pidfilename[sizeof(PIDFILE)];
191 @@ -225,7 +251,7 @@
192 switch (fork())
193 {
194 case -1:
195 - syslog(LOGOPTS, "fork: %m\n");
196 + syslog(LOG_ERR, "fork: %m\n");
197 exit (1);
198 case 0: /* child */
199 break;
200 @@ -236,14 +262,14 @@
201 resourcelimit.rlim_max = 0;
202 if (getrlimit(RLIMIT_NOFILE, &resourcelimit) < 0)
203 {
204 - syslog(LOGOPTS, "getrlimit: %m\n");
205 + syslog(LOG_ERR, "getrlimit: %m\n");
206 exit(1);
207 }
208 for (fd = 0; fd < resourcelimit.rlim_max; ++fd)
209 (void)close(fd);
210 if (setsid() < 0)
211 {
212 - syslog(LOGOPTS, "setsid: %m\n");
213 + syslog(LOG_ERR, "setsid: %m\n");
214 exit(1);
215 }
216 (void)chdir("/");
217 @@ -254,7 +280,7 @@
218 (void)snprintf(pidfilename, sizeof(pidfilename), PIDFILE, lpnumber);
219 if ((f = fopen(pidfilename, "w")) == NULL)
220 {
221 - syslog(LOGOPTS, "%s: %m\n", pidfilename);
222 + syslog(LOG_ERR, "%s: %m\n", pidfilename);
223 exit(1);
224 }
225 (void)fprintf(f, "%d\n", getpid());
226 @@ -262,11 +288,10 @@
227 if (get_lock(lpnumber) == 0)
228 exit(1);
229 #endif
230 - f = open_printer(lpnumber);
231 #ifdef USE_GETPROTOBYNAME
232 if ((proto = getprotobyname("tcp")) == NULL)
233 {
234 - syslog(LOGOPTS, "Cannot find protocol for TCP!\n");
235 + syslog(LOG_ERR, "Cannot find protocol for TCP!\n");
236 exit(1);
237 }
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)
241 #endif
242 {
243 - syslog(LOGOPTS, "socket: %m\n");
244 + syslog(LOG_ERR, "socket: %m\n");
245 exit(1);
246 }
247 if (setsockopt(netfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
248 {
249 - syslog(LOGOPTS, "setsocketopt: %m\n");
250 + syslog(LOG_ERR, "setsocketopt: %m\n");
251 exit(1);
252 }
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)
257 {
258 - syslog(LOGOPTS, "bind: %m\n");
259 + syslog(LOG_ERR, "bind: %m\n");
260 exit(1);
261 }
262 if (listen(netfd, 5) < 0)
263 {
264 - syslog(LOGOPTS, "listen: %m\n");
265 + syslog(LOG_ERR, "listen: %m\n");
266 exit(1);
267 }
268 clientlen = sizeof(client);
269 @@ -302,22 +327,26 @@
270 #ifdef USE_LIBWRAP
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));
277 close(fd);
278 continue;
279 }
280 #endif
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)
289 + {
290 + if (copy_stream(fd, lp) < 0)
291 + syslog(LOG_ERR, "copy_stream: %m\n");
292 + close(lp);
293 + }
294 (void)close(fd);
295 }
296 - syslog(LOGOPTS, "accept: %m\n");
297 + syslog(LOG_ERR, "accept: %m\n");
298 free_lock();
299 exit(1);
300 }
301 @@ -338,7 +367,7 @@
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");
307 return (1);
308 }
309
This page took 0.058456 seconds and 5 git commands to generate.