4 * Backend test program for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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 * usage() - Show usage information.
24 * Include necessary headers.
29 #include <cups/string.h>
30 #include <cups/cups.h>
31 #include <cups/sidechannel.h>
42 static void usage(void);
46 * 'main()' - Run the named backend.
50 * betest [-s] [-t] device-uri job-id user title copies options [file]
53 int /* O - Exit status */
54 main(int argc
, /* I - Number of command-line args */
55 char *argv
[]) /* I - Command-line arguments */
57 int first_arg
, /* First argument for backend */
58 do_query
= 0, /* Do PostScript query? */
59 do_side_tests
= 0, /* Test side-channel ops? */
60 do_trickle
= 0; /* Trickle data to backend */
61 char scheme
[255], /* Scheme in URI == backend */
62 backend
[1024]; /* Backend path */
63 const char *serverbin
; /* CUPS_SERVERBIN environment variable */
64 int back_fds
[2], /* Back-channel pipe */
65 side_fds
[2], /* Side-channel socket */
66 data_fds
[2], /* Data pipe */
68 status
; /* Exit status */
72 * See if we have side-channel tests to do...
76 argv
[first_arg
] && argv
[first_arg
][0] == '-';
78 if (!strcmp(argv
[first_arg
], "-ps"))
80 else if (!strcmp(argv
[first_arg
], "-s"))
82 else if (!strcmp(argv
[first_arg
], "-t"))
88 if (argc
< 6 || argc
> 7 || (argc
== 7 && do_trickle
))
92 * Extract the scheme from the device-uri - that's the program we want to
96 if (sscanf(argv
[first_arg
], "%254[^:]", scheme
) != 1)
98 fputs("testbackend: Bad device-uri - no colon!\n", stderr
);
102 if (!access(scheme
, X_OK
))
103 strlcpy(backend
, scheme
, sizeof(backend
));
106 if ((serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
107 serverbin
= CUPS_SERVERBIN
;
109 snprintf(backend
, sizeof(backend
), "%s/backend/%s", serverbin
, scheme
);
110 if (access(backend
, X_OK
))
112 fprintf(stderr
, "testbackend: Unknown device scheme \"%s\"!\n", scheme
);
118 * Create the back-channel pipe and side-channel socket...
121 open("/dev/null", O_WRONLY
); /* Make sure fd 3 and 4 are used */
122 open("/dev/null", O_WRONLY
);
125 fcntl(back_fds
[0], F_SETFL
, fcntl(back_fds
[0], F_GETFL
) | O_NONBLOCK
);
126 fcntl(back_fds
[1], F_SETFL
, fcntl(back_fds
[1], F_GETFL
) | O_NONBLOCK
);
128 socketpair(AF_LOCAL
, SOCK_STREAM
, 0, side_fds
);
129 fcntl(side_fds
[0], F_SETFL
, fcntl(side_fds
[0], F_GETFL
) | O_NONBLOCK
);
130 fcntl(side_fds
[1], F_SETFL
, fcntl(side_fds
[1], F_GETFL
) | O_NONBLOCK
);
133 * Execute the trickle process as needed...
136 if (do_trickle
|| do_query
)
140 if ((pid
= fork()) == 0)
143 * Trickle/query child comes here... Rearrange file descriptors so that
148 open("/dev/null", O_RDONLY
);
168 * Write 10 spaces, 1 per second...
171 int i
; /* Looping var */
173 for (i
= 0; i
< 10; i
++)
182 * Do a simple PostScript query job to get the default page size.
185 char buffer
[1024]; /* Buffer for response data */
186 ssize_t bytes
; /* Number of bytes of response data */
187 static const char *ps_query
= /* PostScript query file */
190 "currentpagedevice /PageSize get aload pop\n"
191 "2 copy gt {exch} if\n"
194 "dup [612 792] (Letter) put\n"
195 "dup [612 1008] (Legal) put\n"
196 "dup [612 935] (w612h935) put\n"
197 "dup [522 756] (Executive) put\n"
198 "dup [595 842] (A4) put\n"
199 "dup [420 595] (A5) put\n"
200 "dup [499 709] (ISOB5) put\n"
201 "dup [516 728] (B5) put\n"
202 "dup [612 936] (w612h936) put\n"
203 "dup [284 419] (Postcard) put\n"
204 "dup [419.5 567] (DoublePostcard) put\n"
205 "dup [558 774] (w558h774) put\n"
206 "dup [553 765] (w553h765) put\n"
207 "dup [522 737] (w522h737) put\n"
208 "dup [499 709] (EnvISOB5) put\n"
209 "dup [297 684] (Env10) put\n"
210 "dup [459 649] (EnvC5) put\n"
211 "dup [312 624] (EnvDL) put\n"
212 "dup [279 540] (EnvMonarch) put\n"
213 "{ exch aload pop 4 index sub abs 5 le exch\n"
214 " 5 index sub abs 5 le and\n"
215 " {exch pop exit} {pop} ifelse\n"
222 write(1, ps_query
, strlen(ps_query
));
223 write(2, "DEBUG: START\n", 13);
224 while ((bytes
= cupsBackChannelRead(buffer
, sizeof(buffer
), 30.0)) > 0)
225 write(2, buffer
, bytes
);
226 write(2, "\nDEBUG: END\n", 12);
233 perror("testbackend: Unable to fork");
238 data_fds
[0] = data_fds
[1] = -1;
241 * Execute the backend...
244 if ((pid
= fork()) == 0)
247 * Child comes here...
250 if (do_trickle
|| do_query
)
268 execv(backend
, argv
+ first_arg
);
269 fprintf(stderr
, "textbackend: Unable to execute \"%s\": %s\n", backend
,
275 perror("testbackend: Unable to fork");
280 * Parent comes here, setup back and side channel file descriptors...
283 if (do_trickle
|| do_query
)
300 * Do side-channel tests as needed, then wait for the backend...
305 int length
; /* Length of buffer */
306 char buffer
[2049]; /* Buffer for reponse */
307 cups_sc_status_t scstatus
; /* Status of side-channel command */
308 static const char * const statuses
[] =
310 "CUPS_SC_STATUS_NONE", /* No status */
311 "CUPS_SC_STATUS_OK", /* Operation succeeded */
312 "CUPS_SC_STATUS_IO_ERROR", /* An I/O error occurred */
313 "CUPS_SC_STATUS_TIMEOUT", /* The backend did not respond */
314 "CUPS_SC_STATUS_NO_RESPONSE", /* The device did not respond */
315 "CUPS_SC_STATUS_BAD_MESSAGE", /* The command/response message was invalid */
316 "CUPS_SC_STATUS_TOO_BIG", /* Response too big */
317 "CUPS_SC_STATUS_NOT_IMPLEMENTED" /* Command not implemented */
324 scstatus
= cupsSideChannelDoRequest(CUPS_SC_CMD_DRAIN_OUTPUT
, buffer
,
326 printf("CUPS_SC_CMD_DRAIN_OUTPUT returned %s\n", statuses
[scstatus
]);
329 scstatus
= cupsSideChannelDoRequest(CUPS_SC_CMD_GET_BIDI
, buffer
,
331 printf("CUPS_SC_CMD_GET_BIDI returned %s, %d\n", statuses
[scstatus
], buffer
[0]);
333 length
= sizeof(buffer
) - 1;
334 scstatus
= cupsSideChannelDoRequest(CUPS_SC_CMD_GET_DEVICE_ID
, buffer
,
336 buffer
[length
] = '\0';
337 printf("CUPS_SC_CMD_GET_DEVICE_ID returned %s, \"%s\"\n",
338 statuses
[scstatus
], buffer
);
341 scstatus
= cupsSideChannelDoRequest(CUPS_SC_CMD_GET_STATE
, buffer
,
343 printf("CUPS_SC_CMD_GET_STATE returned %s, %02X\n", statuses
[scstatus
],
346 length
= sizeof(buffer
);
347 scstatus
= cupsSideChannelSNMPGet(".1.3.6.1.2.1.43.10.2.1.4.1.1", buffer
,
349 printf("CUPS_SC_CMD_SNMP_GET returned %s, %s\n", statuses
[scstatus
],
353 scstatus
= cupsSideChannelDoRequest(CUPS_SC_CMD_SOFT_RESET
, buffer
,
355 printf("CUPS_SC_CMD_SOFT_RESET returned %s\n", statuses
[scstatus
]);
358 while (wait(&status
) != pid
);
362 if (WIFEXITED(status
))
363 printf("%s exited with status %d!\n", backend
, WEXITSTATUS(status
));
365 printf("%s crashed with signal %d!\n", backend
, WTERMSIG(status
));
369 * Exit accordingly...
372 return (status
!= 0);
377 * 'usage()' - Show usage information.
383 fputs("Usage: testbackend [-ps] [-s] [-t] device-uri job-id user title copies "
384 "options [file]\n", stderr
);