]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/testbackend.c
3213450f74b82814aeb8aacfbf3d6fd582d88038
[thirdparty/cups.git] / backend / testbackend.c
1 /*
2 * "$Id$"
3 *
4 * Backend test program for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2009 by Apple Inc.
7 * Copyright 1997-2005 by Easy Software Products, all rights reserved.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * "LICENSE" which should have been included with this file. If this
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 * main() - Run the named backend.
20 * sigterm_handler() - Flag when we get SIGTERM.
21 * usage() - Show usage information.
22 * walk_cb() - Show results of cupsSideChannelSNMPWalk...
23 */
24
25 /*
26 * Include necessary headers.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <cups/string.h>
32 #include <cups/cups.h>
33 #include <cups/sidechannel.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <sys/wait.h>
38 #include <signal.h>
39
40
41 /*
42 * Local globals...
43 */
44
45 static int job_canceled = 0;
46
47
48 /*
49 * Local functions...
50 */
51
52 static void sigterm_handler(int sig);
53 static void usage(void);
54 static void walk_cb(const char *oid, const char *data, int datalen,
55 void *context);
56
57
58 /*
59 * 'main()' - Run the named backend.
60 *
61 * Usage:
62 *
63 * betest [-s] [-t] device-uri job-id user title copies options [file]
64 */
65
66 int /* O - Exit status */
67 main(int argc, /* I - Number of command-line args */
68 char *argv[]) /* I - Command-line arguments */
69 {
70 int first_arg, /* First argument for backend */
71 do_cancel = 0, /* Simulate a cancel-job via SIGTERM */
72 do_ps = 0, /* Do PostScript query+test? */
73 do_pcl = 0, /* Do PCL query+test? */
74 do_side_tests = 0, /* Test side-channel ops? */
75 do_trickle = 0, /* Trickle data to backend */
76 do_walk = 0, /* Do OID lookup (0) or walking (1) */
77 show_log = 0; /* Show log messages from backends? */
78 const char *oid = ".1.3.6.1.2.1.43.10.2.1.4.1.1";
79 /* OID to lookup or walk */
80 char scheme[255], /* Scheme in URI == backend */
81 backend[1024]; /* Backend path */
82 const char *serverbin; /* CUPS_SERVERBIN environment variable */
83 int fd, /* Temporary file descriptor */
84 back_fds[2], /* Back-channel pipe */
85 side_fds[2], /* Side-channel socket */
86 data_fds[2], /* Data pipe */
87 back_pid = -1, /* Backend process ID */
88 data_pid = -1, /* Trickle process ID */
89 pid, /* Process ID */
90 status; /* Exit status */
91
92
93 /*
94 * See if we have side-channel tests to do...
95 */
96
97 for (first_arg = 1;
98 argv[first_arg] && argv[first_arg][0] == '-';
99 first_arg ++)
100 if (!strcmp(argv[first_arg], "-d"))
101 show_log = 1;
102 else if (!strcmp(argv[first_arg], "-cancel"))
103 do_cancel = 1;
104 else if (!strcmp(argv[first_arg], "-pcl"))
105 do_pcl = 1;
106 else if (!strcmp(argv[first_arg], "-ps"))
107 do_ps = 1;
108 else if (!strcmp(argv[first_arg], "-s"))
109 do_side_tests = 1;
110 else if (!strcmp(argv[first_arg], "-t"))
111 do_trickle = 1;
112 else if (!strcmp(argv[first_arg], "-get") && (first_arg + 1) < argc)
113 {
114 first_arg ++;
115
116 do_side_tests = 1;
117 oid = argv[first_arg];
118 }
119 else if (!strcmp(argv[first_arg], "-walk") && (first_arg + 1) < argc)
120 {
121 first_arg ++;
122
123 do_side_tests = 1;
124 do_walk = 1;
125 oid = argv[first_arg];
126 }
127 else
128 usage();
129
130 argc -= first_arg;
131 if (argc < 6 || argc > 7 || (argc == 7 && do_trickle))
132 usage();
133
134 /*
135 * Extract the scheme from the device-uri - that's the program we want to
136 * execute.
137 */
138
139 if (sscanf(argv[first_arg], "%254[^:]", scheme) != 1)
140 {
141 fputs("testbackend: Bad device-uri - no colon!\n", stderr);
142 return (1);
143 }
144
145 if (!access(scheme, X_OK))
146 strlcpy(backend, scheme, sizeof(backend));
147 else
148 {
149 if ((serverbin = getenv("CUPS_SERVERBIN")) == NULL)
150 serverbin = CUPS_SERVERBIN;
151
152 snprintf(backend, sizeof(backend), "%s/backend/%s", serverbin, scheme);
153 if (access(backend, X_OK))
154 {
155 fprintf(stderr, "testbackend: Unknown device scheme \"%s\"!\n", scheme);
156 return (1);
157 }
158 }
159
160 /*
161 * Create the back-channel pipe and side-channel socket...
162 */
163
164 open("/dev/null", O_WRONLY); /* Make sure fd 3 and 4 are used */
165 open("/dev/null", O_WRONLY);
166
167 pipe(back_fds);
168 fcntl(back_fds[0], F_SETFL, fcntl(back_fds[0], F_GETFL) | O_NONBLOCK);
169 fcntl(back_fds[1], F_SETFL, fcntl(back_fds[1], F_GETFL) | O_NONBLOCK);
170
171 socketpair(AF_LOCAL, SOCK_STREAM, 0, side_fds);
172 fcntl(side_fds[0], F_SETFL, fcntl(side_fds[0], F_GETFL) | O_NONBLOCK);
173 fcntl(side_fds[1], F_SETFL, fcntl(side_fds[1], F_GETFL) | O_NONBLOCK);
174
175 /*
176 * Execute the trickle process as needed...
177 */
178
179 if (do_trickle || do_pcl || do_ps || do_cancel)
180 {
181 pipe(data_fds);
182
183 signal(SIGTERM, sigterm_handler);
184
185 if ((data_pid = fork()) == 0)
186 {
187 /*
188 * Trickle/query child comes here. Rearrange file descriptors so that
189 * FD 1, 3, and 4 point to the backend...
190 */
191
192 if ((fd = open("/dev/null", O_RDONLY)) != 0)
193 {
194 dup2(fd, 0);
195 close(fd);
196 }
197
198 if (data_fds[1] != 1)
199 {
200 dup2(data_fds[1], 1);
201 close(data_fds[1]);
202 }
203 close(data_fds[0]);
204
205 if (back_fds[0] != 3)
206 {
207 dup2(back_fds[0], 3);
208 close(back_fds[0]);
209 }
210 close(back_fds[1]);
211
212 if (side_fds[0] != 4)
213 {
214 dup2(side_fds[0], 4);
215 close(side_fds[0]);
216 }
217 close(side_fds[1]);
218
219 if (do_trickle)
220 {
221 /*
222 * Write 10 spaces, 1 per second...
223 */
224
225 int i; /* Looping var */
226
227 for (i = 0; i < 10; i ++)
228 {
229 write(1, " ", 1);
230 sleep(1);
231 }
232 }
233 else if (do_cancel)
234 {
235 /*
236 * Write PS or PCL lines until we see SIGTERM...
237 */
238
239 int line = 0, page = 0; /* Current line and page */
240 ssize_t bytes; /* Number of bytes of response data */
241 char buffer[1024]; /* Output buffer */
242
243
244 if (do_pcl)
245 write(1, "\033E", 2);
246 else
247 write(1, "%!\n/Courier findfont 12 scalefont setfont 0 setgray\n", 52);
248
249 while (!job_canceled)
250 {
251 if (line == 0)
252 {
253 page ++;
254
255 if (do_pcl)
256 snprintf(buffer, sizeof(buffer), "PCL Page %d\r\n\r\n", page);
257 else
258 snprintf(buffer, sizeof(buffer),
259 "18 732 moveto (PS Page %d) show\n", page);
260
261 write(1, buffer, strlen(buffer));
262 }
263
264 line ++;
265
266 if (do_pcl)
267 snprintf(buffer, sizeof(buffer), "Line %d\r\n", line);
268 else
269 snprintf(buffer, sizeof(buffer), "18 %d moveto (Line %d) show\n",
270 720 - line * 12, line);
271
272 write(1, buffer, strlen(buffer));
273
274 if (line >= 55)
275 {
276 /*
277 * Eject after 55 lines...
278 */
279
280 line = 0;
281 if (do_pcl)
282 write(1, "\014", 1);
283 else
284 write(1, "showpage\n", 9);
285 }
286
287 /*
288 * Check for back-channel data...
289 */
290
291 if ((bytes = cupsBackChannelRead(buffer, sizeof(buffer), 0)) > 0)
292 write(2, buffer, bytes);
293
294 /*
295 * Throttle output to ~100hz...
296 */
297
298 usleep(10000);
299 }
300
301 /*
302 * Eject current page with info...
303 */
304
305 if (do_pcl)
306 snprintf(buffer, sizeof(buffer),
307 "Canceled on line %d of page %d\r\n\014\033E", line, page);
308 else
309 snprintf(buffer, sizeof(buffer),
310 "\n18 %d moveto (Canceled on line %d of page %d)\nshowpage\n",
311 720 - line * 12, line, page);
312
313 write(1, buffer, strlen(buffer));
314
315 /*
316 * See if we get any back-channel data...
317 */
318
319 while ((bytes = cupsBackChannelRead(buffer, sizeof(buffer), 5.0)) > 0)
320 write(2, buffer, bytes);
321
322 exit(0);
323 }
324 else
325 {
326 /*
327 * Do PS or PCL query + test pages.
328 */
329
330 char buffer[1024]; /* Buffer for response data */
331 ssize_t bytes; /* Number of bytes of response data */
332 double timeout; /* Timeout */
333 const char *data; /* Data to send */
334 static const char *pcl_data = /* PCL data */
335 "\033%-12345X@PJL\r\n"
336 "@PJL JOB NAME = \"Hello, World!\"\r\n"
337 "@PJL INFO USTATUS\r\n"
338 "@PJL ENTER LANGUAGE = PCL\r\n"
339 "\033E"
340 "Hello, World!\n"
341 "\014"
342 "\033%-12345X@PJL\r\n"
343 "@PJL EOJ NAME=\"Hello, World!\"\r\n"
344 "\033%-12345X";
345 static const char *ps_data = /* PostScript data */
346 "%!\n"
347 "save\n"
348 "product = flush\n"
349 "currentpagedevice /PageSize get aload pop\n"
350 "2 copy gt {exch} if\n"
351 "(Unknown)\n"
352 "19 dict\n"
353 "dup [612 792] (Letter) put\n"
354 "dup [612 1008] (Legal) put\n"
355 "dup [612 935] (w612h935) put\n"
356 "dup [522 756] (Executive) put\n"
357 "dup [595 842] (A4) put\n"
358 "dup [420 595] (A5) put\n"
359 "dup [499 709] (ISOB5) put\n"
360 "dup [516 728] (B5) put\n"
361 "dup [612 936] (w612h936) put\n"
362 "dup [284 419] (Postcard) put\n"
363 "dup [419.5 567] (DoublePostcard) put\n"
364 "dup [558 774] (w558h774) put\n"
365 "dup [553 765] (w553h765) put\n"
366 "dup [522 737] (w522h737) put\n"
367 "dup [499 709] (EnvISOB5) put\n"
368 "dup [297 684] (Env10) put\n"
369 "dup [459 649] (EnvC5) put\n"
370 "dup [312 624] (EnvDL) put\n"
371 "dup [279 540] (EnvMonarch) put\n"
372 "{ exch aload pop 4 index sub abs 5 le exch\n"
373 " 5 index sub abs 5 le and\n"
374 " {exch pop exit} {pop} ifelse\n"
375 "} bind forall\n"
376 "= flush pop pop\n"
377 "/Courier findfont 12 scalefont setfont\n"
378 "0 setgray 36 720 moveto (Hello, ) show product show (!) show\n"
379 "showpage\n"
380 "restore\n"
381 "\004";
382
383
384 if (do_pcl)
385 data = pcl_data;
386 else
387 data = ps_data;
388
389 write(1, data, strlen(data));
390 write(2, "DEBUG: START\n", 13);
391 timeout = 60.0;
392 while ((bytes = cupsBackChannelRead(buffer, sizeof(buffer),
393 timeout)) > 0)
394 {
395 write(2, buffer, bytes);
396 timeout = 5.0;
397 }
398 write(2, "\nDEBUG: END\n", 12);
399 }
400
401 exit(0);
402 }
403 else if (data_pid < 0)
404 {
405 perror("testbackend: Unable to fork");
406 return (1);
407 }
408 }
409 else
410 data_fds[0] = data_fds[1] = -1;
411
412 /*
413 * Execute the backend...
414 */
415
416 if ((back_pid = fork()) == 0)
417 {
418 /*
419 * Child comes here...
420 */
421
422 if (do_trickle || do_ps || do_pcl || do_cancel)
423 {
424 if (data_fds[0] != 0)
425 {
426 dup2(data_fds[0], 0);
427 close(data_fds[0]);
428 }
429 close(data_fds[1]);
430 }
431
432 if (!show_log)
433 {
434 if ((fd = open("/dev/null", O_WRONLY)) != 2)
435 {
436 dup2(fd, 2);
437 close(fd);
438 }
439 }
440
441 if (back_fds[1] != 3)
442 {
443 dup2(back_fds[1], 3);
444 close(back_fds[0]);
445 }
446 close(back_fds[1]);
447
448 if (side_fds[1] != 4)
449 {
450 dup2(side_fds[1], 4);
451 close(side_fds[0]);
452 }
453 close(side_fds[1]);
454
455 execv(backend, argv + first_arg);
456 fprintf(stderr, "testbackend: Unable to execute \"%s\": %s\n", backend,
457 strerror(errno));
458 return (errno);
459 }
460 else if (back_pid < 0)
461 {
462 perror("testbackend: Unable to fork");
463 return (1);
464 }
465
466 /*
467 * Parent comes here, setup back and side channel file descriptors...
468 */
469
470 if (do_trickle || do_ps || do_pcl || do_cancel)
471 {
472 close(data_fds[0]);
473 close(data_fds[1]);
474 }
475
476 if (back_fds[0] != 3)
477 {
478 dup2(back_fds[0], 3);
479 close(back_fds[0]);
480 }
481 close(back_fds[1]);
482
483 if (side_fds[0] != 4)
484 {
485 dup2(side_fds[0], 4);
486 close(side_fds[0]);
487 }
488 close(side_fds[1]);
489
490 /*
491 * Do side-channel tests as needed, then wait for the backend...
492 */
493
494 if (do_side_tests)
495 {
496 int length; /* Length of buffer */
497 char buffer[2049]; /* Buffer for reponse */
498 cups_sc_status_t scstatus; /* Status of side-channel command */
499 static const char * const statuses[] =
500 {
501 "CUPS_SC_STATUS_NONE", /* No status */
502 "CUPS_SC_STATUS_OK", /* Operation succeeded */
503 "CUPS_SC_STATUS_IO_ERROR", /* An I/O error occurred */
504 "CUPS_SC_STATUS_TIMEOUT", /* The backend did not respond */
505 "CUPS_SC_STATUS_NO_RESPONSE", /* The device did not respond */
506 "CUPS_SC_STATUS_BAD_MESSAGE", /* The command/response message was invalid */
507 "CUPS_SC_STATUS_TOO_BIG", /* Response too big */
508 "CUPS_SC_STATUS_NOT_IMPLEMENTED" /* Command not implemented */
509 };
510
511
512 sleep(2);
513
514 length = 0;
515 scstatus = cupsSideChannelDoRequest(CUPS_SC_CMD_DRAIN_OUTPUT, buffer,
516 &length, 60.0);
517 printf("CUPS_SC_CMD_DRAIN_OUTPUT returned %s\n", statuses[scstatus]);
518
519 length = 1;
520 scstatus = cupsSideChannelDoRequest(CUPS_SC_CMD_GET_BIDI, buffer,
521 &length, 5.0);
522 printf("CUPS_SC_CMD_GET_BIDI returned %s, %d\n", statuses[scstatus], buffer[0]);
523
524 length = sizeof(buffer) - 1;
525 scstatus = cupsSideChannelDoRequest(CUPS_SC_CMD_GET_DEVICE_ID, buffer,
526 &length, 5.0);
527 buffer[length] = '\0';
528 printf("CUPS_SC_CMD_GET_DEVICE_ID returned %s, \"%s\"\n",
529 statuses[scstatus], buffer);
530
531 length = 1;
532 scstatus = cupsSideChannelDoRequest(CUPS_SC_CMD_GET_STATE, buffer,
533 &length, 5.0);
534 printf("CUPS_SC_CMD_GET_STATE returned %s, %02X\n", statuses[scstatus],
535 buffer[0] & 255);
536
537 if (do_walk)
538 {
539 /*
540 * Walk the OID tree...
541 */
542
543 scstatus = cupsSideChannelSNMPWalk(oid, 5.0, walk_cb, NULL);
544 printf("CUPS_SC_CMD_SNMP_WALK returned %s\n", statuses[scstatus]);
545 }
546 else
547 {
548 /*
549 * Lookup the same OID twice...
550 */
551
552 length = sizeof(buffer);
553 scstatus = cupsSideChannelSNMPGet(oid, buffer, &length, 5.0);
554 printf("CUPS_SC_CMD_SNMP_GET %s returned %s, %s\n", oid,
555 statuses[scstatus], buffer);
556
557 length = sizeof(buffer);
558 scstatus = cupsSideChannelSNMPGet(oid, buffer, &length, 5.0);
559 printf("CUPS_SC_CMD_SNMP_GET %s returned %s, %s\n", oid,
560 statuses[scstatus], buffer);
561 }
562
563 length = 0;
564 scstatus = cupsSideChannelDoRequest(CUPS_SC_CMD_SOFT_RESET, buffer,
565 &length, 5.0);
566 printf("CUPS_SC_CMD_SOFT_RESET returned %s\n", statuses[scstatus]);
567 }
568
569 if (do_cancel)
570 {
571 sleep(1);
572 kill(data_pid, SIGTERM);
573 kill(back_pid, SIGTERM);
574 }
575
576 while ((pid = wait(&status)) > 0)
577 {
578 if (status)
579 {
580 if (WIFEXITED(status))
581 printf("%s exited with status %d!\n",
582 pid == back_pid ? backend : "test",
583 WEXITSTATUS(status));
584 else
585 printf("%s crashed with signal %d!\n",
586 pid == back_pid ? backend : "test",
587 WTERMSIG(status));
588 }
589 }
590
591 /*
592 * Exit accordingly...
593 */
594
595 return (status != 0);
596 }
597
598
599 /*
600 * 'sigterm_handler()' - Flag when we get SIGTERM.
601 */
602
603 static void
604 sigterm_handler(int sig) /* I - Signal */
605 {
606 (void)sig;
607
608 job_canceled = 1;
609 }
610
611
612 /*
613 * 'usage()' - Show usage information.
614 */
615
616 static void
617 usage(void)
618 {
619 puts("Usage: testbackend [-cancel] [-d] [-ps | -pcl] [-s [-oid OID] "
620 "[-walk OID]] [-t] device-uri job-id user title copies options [file]");
621 puts("");
622 puts("Options:");
623 puts(" -cancel Simulate a canceled print job after 2 seconds.");
624 puts(" -d Show log messages from backend.");
625 puts(" -oid OID Lookup the specified SNMP OID.");
626 puts(" (.1.3.6.1.2.1.43.10.2.1.4.1.1 is a good one for printers)");
627 puts(" -pcl Send PCL+PJL query and test page to backend.");
628 puts(" -ps Send PostScript query and test page to backend.");
629 puts(" -s Do side-channel + SNMP tests.");
630 puts(" -t Send spaces slowly to backend ('trickle').");
631 puts(" -walk OID Walk the specified SNMP OID.");
632 puts(" (.1.3.6.1.2.1.43 is a good one for printers)");
633
634 exit(1);
635 }
636
637
638 /*
639 * 'walk_cb()' - Show results of cupsSideChannelSNMPWalk...
640 */
641
642 static void
643 walk_cb(const char *oid, /* I - OID */
644 const char *data, /* I - Data */
645 int datalen, /* I - Length of data */
646 void *context) /* I - Context (unused) */
647 {
648 printf("CUPS_SC_CMD_SNMP_WALK %s=%s (%d bytes)\n", oid, data, datalen);
649 }
650
651
652 /*
653 * End of "$Id$".
654 */