2 * "$Id: testbackend.c 9042 2010-03-24 00:45:34Z mike $"
4 * Backend test program for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2005 by Easy Software Products, all rights reserved.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
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...
26 * Include necessary headers.
29 #include <cups/string-private.h>
30 #include <cups/cups.h>
31 #include <cups/sidechannel.h>
42 static int job_canceled = 0;
49 static void sigterm_handler(int sig);
50 static void usage(void);
51 static void walk_cb(const char *oid, const char *data, int datalen,
56 * 'main()' - Run the named backend.
60 * betest [-s] [-t] device-uri job-id user title copies options [file]
63 int /* O - Exit status */
64 main(int argc, /* I - Number of command-line args */
65 char *argv[]) /* I - Command-line arguments */
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 const char *serverbin; /* CUPS_SERVERBIN environment variable */
80 int fd, /* Temporary file descriptor */
81 back_fds[2], /* Back-channel pipe */
82 side_fds[2], /* Side-channel socket */
83 data_fds[2], /* Data pipe */
84 back_pid = -1, /* Backend process ID */
85 data_pid = -1, /* Trickle process ID */
87 status; /* Exit status */
91 * See if we have side-channel tests to do...
95 argv[first_arg] && argv[first_arg][0] == '-';
97 if (!strcmp(argv[first_arg], "-d"))
99 else if (!strcmp(argv[first_arg], "-cancel"))
101 else if (!strcmp(argv[first_arg], "-pcl"))
103 else if (!strcmp(argv[first_arg], "-ps"))
105 else if (!strcmp(argv[first_arg], "-s"))
107 else if (!strcmp(argv[first_arg], "-t"))
109 else if (!strcmp(argv[first_arg], "-get") && (first_arg + 1) < argc)
114 oid = argv[first_arg];
116 else if (!strcmp(argv[first_arg], "-walk") && (first_arg + 1) < argc)
122 oid = argv[first_arg];
128 if (argc < 6 || argc > 7 || (argc == 7 && do_trickle))
132 * Extract the scheme from the device-uri - that's the program we want to
136 if (sscanf(argv[first_arg], "%254[^:]", scheme) != 1)
138 fputs("testbackend: Bad device-uri - no colon!\n", stderr);
142 if (!access(scheme, X_OK))
143 strlcpy(backend, scheme, sizeof(backend));
146 if ((serverbin = getenv("CUPS_SERVERBIN")) == NULL)
147 serverbin = CUPS_SERVERBIN;
149 snprintf(backend, sizeof(backend), "%s/backend/%s", serverbin, scheme);
150 if (access(backend, X_OK))
152 fprintf(stderr, "testbackend: Unknown device scheme \"%s\"!\n", scheme);
158 * Create the back-channel pipe and side-channel socket...
161 open("/dev/null", O_WRONLY); /* Make sure fd 3 and 4 are used */
162 open("/dev/null", O_WRONLY);
165 fcntl(back_fds[0], F_SETFL, fcntl(back_fds[0], F_GETFL) | O_NONBLOCK);
166 fcntl(back_fds[1], F_SETFL, fcntl(back_fds[1], F_GETFL) | O_NONBLOCK);
168 socketpair(AF_LOCAL, SOCK_STREAM, 0, side_fds);
169 fcntl(side_fds[0], F_SETFL, fcntl(side_fds[0], F_GETFL) | O_NONBLOCK);
170 fcntl(side_fds[1], F_SETFL, fcntl(side_fds[1], F_GETFL) | O_NONBLOCK);
173 * Execute the trickle process as needed...
176 if (do_trickle || do_pcl || do_ps || do_cancel)
180 signal(SIGTERM, sigterm_handler);
182 if ((data_pid = fork()) == 0)
185 * Trickle/query child comes here. Rearrange file descriptors so that
186 * FD 1, 3, and 4 point to the backend...
189 if ((fd = open("/dev/null", O_RDONLY)) != 0)
195 if (data_fds[1] != 1)
197 dup2(data_fds[1], 1);
202 if (back_fds[0] != 3)
204 dup2(back_fds[0], 3);
209 if (side_fds[0] != 4)
211 dup2(side_fds[0], 4);
219 * Write 10 spaces, 1 per second...
222 int i; /* Looping var */
224 for (i = 0; i < 10; i ++)
233 * Write PS or PCL lines until we see SIGTERM...
236 int line = 0, page = 0; /* Current line and page */
237 ssize_t bytes; /* Number of bytes of response data */
238 char buffer[1024]; /* Output buffer */
242 write(1, "\033E", 2);
244 write(1, "%!\n/Courier findfont 12 scalefont setfont 0 setgray\n", 52);
246 while (!job_canceled)
253 snprintf(buffer, sizeof(buffer), "PCL Page %d\r\n\r\n", page);
255 snprintf(buffer, sizeof(buffer),
256 "18 732 moveto (PS Page %d) show\n", page);
258 write(1, buffer, strlen(buffer));
264 snprintf(buffer, sizeof(buffer), "Line %d\r\n", line);
266 snprintf(buffer, sizeof(buffer), "18 %d moveto (Line %d) show\n",
267 720 - line * 12, line);
269 write(1, buffer, strlen(buffer));
274 * Eject after 55 lines...
281 write(1, "showpage\n", 9);
285 * Check for back-channel data...
288 if ((bytes = cupsBackChannelRead(buffer, sizeof(buffer), 0)) > 0)
289 write(2, buffer, bytes);
292 * Throttle output to ~100hz...
299 * Eject current page with info...
303 snprintf(buffer, sizeof(buffer),
304 "Canceled on line %d of page %d\r\n\014\033E", line, page);
306 snprintf(buffer, sizeof(buffer),
307 "\n18 %d moveto (Canceled on line %d of page %d)\nshowpage\n",
308 720 - line * 12, line, page);
310 write(1, buffer, strlen(buffer));
313 * See if we get any back-channel data...
316 while ((bytes = cupsBackChannelRead(buffer, sizeof(buffer), 5.0)) > 0)
317 write(2, buffer, bytes);
324 * Do PS or PCL query + test pages.
327 char buffer[1024]; /* Buffer for response data */
328 ssize_t bytes; /* Number of bytes of response data */
329 double timeout; /* Timeout */
330 const char *data; /* Data to send */
331 static const char *pcl_data = /* PCL data */
332 "\033%-12345X@PJL\r\n"
333 "@PJL JOB NAME = \"Hello, World!\"\r\n"
334 "@PJL INFO USTATUS\r\n"
335 "@PJL ENTER LANGUAGE = PCL\r\n"
339 "\033%-12345X@PJL\r\n"
340 "@PJL EOJ NAME=\"Hello, World!\"\r\n"
342 static const char *ps_data = /* PostScript data */
346 "currentpagedevice /PageSize get aload pop\n"
347 "2 copy gt {exch} if\n"
350 "dup [612 792] (Letter) put\n"
351 "dup [612 1008] (Legal) put\n"
352 "dup [612 935] (w612h935) put\n"
353 "dup [522 756] (Executive) put\n"
354 "dup [595 842] (A4) put\n"
355 "dup [420 595] (A5) put\n"
356 "dup [499 709] (ISOB5) put\n"
357 "dup [516 728] (B5) put\n"
358 "dup [612 936] (w612h936) put\n"
359 "dup [284 419] (Postcard) put\n"
360 "dup [419.5 567] (DoublePostcard) put\n"
361 "dup [558 774] (w558h774) put\n"
362 "dup [553 765] (w553h765) put\n"
363 "dup [522 737] (w522h737) put\n"
364 "dup [499 709] (EnvISOB5) put\n"
365 "dup [297 684] (Env10) put\n"
366 "dup [459 649] (EnvC5) put\n"
367 "dup [312 624] (EnvDL) put\n"
368 "dup [279 540] (EnvMonarch) put\n"
369 "{ exch aload pop 4 index sub abs 5 le exch\n"
370 " 5 index sub abs 5 le and\n"
371 " {exch pop exit} {pop} ifelse\n"
374 "/Courier findfont 12 scalefont setfont\n"
375 "0 setgray 36 720 moveto (Hello, ) show product show (!) show\n"
386 write(1, data, strlen(data));
387 write(2, "DEBUG: START\n", 13);
389 while ((bytes = cupsBackChannelRead(buffer, sizeof(buffer),
392 write(2, buffer, bytes);
395 write(2, "\nDEBUG: END\n", 12);
400 else if (data_pid < 0)
402 perror("testbackend: Unable to fork");
407 data_fds[0] = data_fds[1] = -1;
410 * Execute the backend...
413 if ((back_pid = fork()) == 0)
416 * Child comes here...
419 if (do_trickle || do_ps || do_pcl || do_cancel)
421 if (data_fds[0] != 0)
423 dup2(data_fds[0], 0);
431 if ((fd = open("/dev/null", O_WRONLY)) != 2)
438 if (back_fds[1] != 3)
440 dup2(back_fds[1], 3);
445 if (side_fds[1] != 4)
447 dup2(side_fds[1], 4);
452 execv(backend, argv + first_arg);
453 fprintf(stderr, "testbackend: Unable to execute \"%s\": %s\n", backend,
457 else if (back_pid < 0)
459 perror("testbackend: Unable to fork");
464 * Parent comes here, setup back and side channel file descriptors...
467 if (do_trickle || do_ps || do_pcl || do_cancel)
473 if (back_fds[0] != 3)
475 dup2(back_fds[0], 3);
480 if (side_fds[0] != 4)
482 dup2(side_fds[0], 4);
488 * Do side-channel tests as needed, then wait for the backend...
493 int length; /* Length of buffer */
494 char buffer[2049]; /* Buffer for reponse */
495 cups_sc_status_t scstatus; /* Status of side-channel command */
496 static const char * const statuses[] =
498 "CUPS_SC_STATUS_NONE", /* No status */
499 "CUPS_SC_STATUS_OK", /* Operation succeeded */
500 "CUPS_SC_STATUS_IO_ERROR", /* An I/O error occurred */
501 "CUPS_SC_STATUS_TIMEOUT", /* The backend did not respond */
502 "CUPS_SC_STATUS_NO_RESPONSE", /* The device did not respond */
503 "CUPS_SC_STATUS_BAD_MESSAGE", /* The command/response message was invalid */
504 "CUPS_SC_STATUS_TOO_BIG", /* Response too big */
505 "CUPS_SC_STATUS_NOT_IMPLEMENTED" /* Command not implemented */
512 scstatus = cupsSideChannelDoRequest(CUPS_SC_CMD_DRAIN_OUTPUT, buffer,
514 printf("CUPS_SC_CMD_DRAIN_OUTPUT returned %s\n", statuses[scstatus]);
517 scstatus = cupsSideChannelDoRequest(CUPS_SC_CMD_GET_BIDI, buffer,
519 printf("CUPS_SC_CMD_GET_BIDI returned %s, %d\n", statuses[scstatus], buffer[0]);
521 length = sizeof(buffer) - 1;
522 scstatus = cupsSideChannelDoRequest(CUPS_SC_CMD_GET_DEVICE_ID, buffer,
524 buffer[length] = '\0';
525 printf("CUPS_SC_CMD_GET_DEVICE_ID returned %s, \"%s\"\n",
526 statuses[scstatus], buffer);
529 scstatus = cupsSideChannelDoRequest(CUPS_SC_CMD_GET_STATE, buffer,
531 printf("CUPS_SC_CMD_GET_STATE returned %s, %02X\n", statuses[scstatus],
537 * Walk the OID tree...
540 scstatus = cupsSideChannelSNMPWalk(oid, 5.0, walk_cb, NULL);
541 printf("CUPS_SC_CMD_SNMP_WALK returned %s\n", statuses[scstatus]);
546 * Lookup the same OID twice...
549 length = sizeof(buffer);
550 scstatus = cupsSideChannelSNMPGet(oid, buffer, &length, 5.0);
551 printf("CUPS_SC_CMD_SNMP_GET %s returned %s, %s\n", oid,
552 statuses[scstatus], buffer);
554 length = sizeof(buffer);
555 scstatus = cupsSideChannelSNMPGet(oid, buffer, &length, 5.0);
556 printf("CUPS_SC_CMD_SNMP_GET %s returned %s, %s\n", oid,
557 statuses[scstatus], buffer);
561 scstatus = cupsSideChannelDoRequest(CUPS_SC_CMD_SOFT_RESET, buffer,
563 printf("CUPS_SC_CMD_SOFT_RESET returned %s\n", statuses[scstatus]);
569 kill(data_pid, SIGTERM);
570 kill(back_pid, SIGTERM);
573 while ((pid = wait(&status)) > 0)
577 if (WIFEXITED(status))
578 printf("%s exited with status %d!\n",
579 pid == back_pid ? backend : "test",
580 WEXITSTATUS(status));
582 printf("%s crashed with signal %d!\n",
583 pid == back_pid ? backend : "test",
589 * Exit accordingly...
592 return (status != 0);
597 * 'sigterm_handler()' - Flag when we get SIGTERM.
601 sigterm_handler(int sig) /* I - Signal */
610 * 'usage()' - Show usage information.
616 puts("Usage: testbackend [-cancel] [-d] [-ps | -pcl] [-s [-oid OID] "
617 "[-walk OID]] [-t] device-uri job-id user title copies options [file]");
620 puts(" -cancel Simulate a canceled print job after 2 seconds.");
621 puts(" -d Show log messages from backend.");
622 puts(" -oid OID Lookup the specified SNMP OID.");
623 puts(" (.1.3.6.1.2.1.43.10.2.1.4.1.1 is a good one for printers)");
624 puts(" -pcl Send PCL+PJL query and test page to backend.");
625 puts(" -ps Send PostScript query and test page to backend.");
626 puts(" -s Do side-channel + SNMP tests.");
627 puts(" -t Send spaces slowly to backend ('trickle').");
628 puts(" -walk OID Walk the specified SNMP OID.");
629 puts(" (.1.3.6.1.2.1.43 is a good one for printers)");
636 * 'walk_cb()' - Show results of cupsSideChannelSNMPWalk...
640 walk_cb(const char *oid, /* I - OID */
641 const char *data, /* I - Data */
642 int datalen, /* I - Length of data */
643 void *context) /* I - Context (unused) */
645 printf("CUPS_SC_CMD_SNMP_WALK %s=%s (%d bytes)\n", oid, data, datalen);
650 * End of "$Id: testbackend.c 9042 2010-03-24 00:45:34Z mike $".