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