## Overview

Filters (which include printer drivers and port monitors) and backends are used to convert job files to a printable format and send that data to the printer itself. All of these programs use a common interface for processing print jobs and communicating status information to the scheduler. Each is run with a standard set of command-line arguments:

argv[1]
  The job ID
argv[2]
  The user printing the job
argv[3]
  The job name/title
argv[4]
  The number of copies to print
argv[5]
  The options that were provided when the job was submitted
argv[6]
  The file to print (first program only)

The scheduler runs one or more of these programs to print any given job. The first filter reads from the print file and writes to the standard output, while the remaining filters read from the standard input and write to the standard output. The backend is the last filter in the chain and writes to the device.

Filters are always run as a non-privileged user, typically "lp", with no connection to the user's desktop. Backends are run either as a non-privileged user or as root if the file permissions do not allow user or group execution. The file permissions section talks about this in more detail.

### Security Considerations

It is always important to use security programming practices. Filters and most backends are run as a non-priviledged user, so the major security consideration is resource utilization - filters should not depend on unlimited amounts of CPU, memory, or disk space, and should protect against conditions that could lead to excess usage of any resource like infinite loops and unbounded recursion. In addition, filters must never allow the user to specify an arbitrary file path to a separator page, template, or other file used by the filter since that can lead to an unauthorized disclosure of information. Always treat input as suspect and validate it!

If you are developing a backend that runs as root , make sure to check for potential buffer overflows, integer under/overflow conditions, and file accesses since these can lead to privilege escalations. When writing files, always validate the file path and never allow a user to determine where to store a file.

> Note:

> Never write files to a user's home directory. Aside from the security implications, CUPS is a network print service and as such the network user may not be the same as the local user and/or there may not be a local home directory to write to.

> In addition, some operating systems provide additional security mechanisms that further limit file system access, even for backends running as root. On Mac OS X, for example, no backend may write to a user's home directory.

### File Permissions

For security reasons, CUPS will only run filters and backends that are owned by root and do not have world write permissions. The recommended permissions for filters and backends are 0555 - read and execute but no write. Backends that must run as root should use permissions of 0500 - read and execute by root, no access for other users. Write permissions can be enabled for the root user only.

To avoid a warning message, the directory containing your filter(s) must also be owned by root and have world write disabled - permissions of 0755 or 0555 are strongly encouraged.

### Temporary Files

Temporary files should be created in the directory specified by the "TMPDIR" environment variable. The `cupsTempFile2` function can be used to safely create temporary files in this directory.

### Copy Generation

The `argv[4]` argument specifies the number of copies to produce of the input file. In general, you should only generate copies if the filename argument is supplied. The only exception to this are filters that produce device-independent PostScript output, since the PostScript filter pstops is responsible for generating copies of PostScript files.

### Exit Codes

Filters must exit with status 0 when they successfully generate print data or 1 when they encounter an error. Backends can return any of the `cups_backend_t` constants.

### Environment Variables

The following environment variables are defined by the printing system when running print filters and backends:

APPLE_LANGUAGE
  The Apple language identifier associated with the job (Mac OS X only).
CHARSET
  The job character set, typically "utf-8".
CLASS
  When a job is submitted to a printer class, contains the name of the destination printer class. Otherwise this environment variable will not be set.
CONTENT_TYPE
  The MIME type associated with the file (e.g. application/postscript).
CUPS_CACHEDIR
  The directory where cache files can be stored. Cache files can be used to retain information between jobs or files in a job.
CUPS_DATADIR
  The directory where (read-only) CUPS data files can be found.
CUPS_FILETYPE
  The type of file being printed: "job-sheet" for a banner page and "document" for a regular print file.
CUPS_SERVERROOT
  The root directory of the server.
DEVICE_URI
  The device-uri associated with the printer.
FINAL_CONTENT_TYPE
  The MIME type associated with the printer (e.g. application/vnd.cups-postscript).
LANG
  The language locale associated with the job.
PPD
  The full pathname of the PostScript Printer Description (PPD) file for this printer.
PRINTER
  The queue name of the class or printer.
RIP_CACHE
  The recommended amount of memory to use for Raster Image Processors (RIPs).
TMPDIR
  The directory where temporary files should be created.

### Communicating with the Scheduler

Filters and backends communicate with the scheduler by writing messages to the standard error file. The scheduler reads messages from all filters in a job and processes the message based on its prefix. For example, the following code sets the current printer state message to "Printing page 5":

    int page = 5;

    fprintf(stderr, "INFO: Printing page %d\n", page);

Each message is a single line of text starting with one of the following prefix strings:

ALERT: message
  Sets the printer-state-message attribute and adds the specified message to the current error log file using the "alert" log level.
ATTR: attribute=value [attribute=value]
  Sets the named printer or job attribute(s). Typically this is used to set the `marker-colors`, `marker-levels`, `marker-message`, `marker-names`, `marker-types`, `printer-alert`, and `printer-alert-description` printer attributes. Standard `marker-types` values are listed in Table 1.
CRIT: message
  Sets the printer-state-message attribute and adds the specified message to the current error log file using the "critical" log level.
DEBUG: message
  Sets the printer-state-message attribute and adds the specified message to the current error log file using the "debug" log level.
DEBUG2: message
  Sets the printer-state-message attribute and adds the specified message to the current error log file using the "debug2" log level.
EMERG: message
  Sets the printer-state-message attribute and adds the specified message to the current error log file using the "emergency" log level.
ERROR: message
  Sets the printer-state-message attribute and adds the specified message to the current error log file using the "error" log level. Use "ERROR:" messages for non-persistent processing errors.
INFO: message
  Sets the printer-state-message attribute. If the current log level is set to "debug2", also adds the specified message to the current error log file using the "info" log level.
NOTICE: message
  Sets the printer-state-message attribute and adds the specified message to the current error log file using the "notice" log level.
PAGE: page-number #-copies
PAGE: total #-pages
  Adds an entry to the current page log file. The first form adds #-copies to the job-media-sheets-completed attribute. The second form sets the job-media-sheets-completed attribute to #-pages.
PPD: keyword=value [keyword=value ...]
  Changes or adds keywords to the printer's PPD file. Typically this is used to update installable options or default media settings based on the printer configuration.
STATE: printer-state-reason [printer-state-reason ...]
STATE: + printer-state-reason [printer-state-reason ...]
STATE: - printer-state-reason [printer-state-reason ...]
  Sets, adds, or removes printer-state-reason keywords to the current queue. Typically this is used to indicate persistent media, ink, toner, and configuration conditions or errors on a printer. Table 2 lists the standard state keywords - use vendor-prefixed ("com.acme.foo") keywords for custom states.

  > Note:

  > "STATE:" messages often provide visible alerts to the user. For example, on Mac OS X setting a printer-state-reason value with an "-error" or "-warning" suffix will cause the printer's dock item to bounce if the corresponding reason is localized with a cupsIPPReason keyword in the printer's PPD file.

WARNING: message
  Sets the printer-state-message attribute and adds the specified message to the current error log file using the "warning" log level.

Messages without one of these prefixes are treated as if they began with the "DEBUG:" prefix string.

Table 1: Standard marker-types Values
marker-type | Description
--- | ---
developer | Developer unit
fuser | Fuser unit
fuserCleaningPad | Fuser cleaning pad
fuserOil | Fuser oil
ink | Ink supply
opc | Photo conductor
solidWax | Wax supply
staples | Staple supply
toner | Toner supply
transferUnit | Transfer unit
wasteInk | Waste ink tank
wasteToner | Waste toner tank
wasteWax | Waste wax tank

Table 2: Standard State Keywords
Keyword | Description
--- | ---
connecting-to-device | Connecting to printer but not printing yet
cover-open | A cover is open on the printer
input-tray-missing | An input tray is missing from the printer
marker-supply-empty | Out of ink
marker-supply-low | Low on ink
marker-waste-almost-full | Waste tank almost full
marker-waste-full | Waste tank full
media-empty | Out of media
media-jam | Media is jammed in the printer
media-low | Low on media
paused | Stop the printer
timed-out | Unable to connect to printer
toner-empty | Out of toner
toner-low | Low on toner

### Communicating with the Backend

Filters can communicate with the backend via the `cupsBackChannelRead` and `cupsSideChannelDoRequest` functions. The `cupsBackChannelRead` function reads data that has been sent back from the device and is typically used to obtain status and configuration information. For example, the following code polls the backend for back-channel data:

    #include <cups/cups.h>

    char buffer[8192];
    ssize_t bytes;

    /* Use a timeout of 0.0 seconds to poll for back-channel data */
    bytes = cupsBackChannelRead(buffer, sizeof(buffer), 0.0);

Filters can also use `select()` or `poll()` on the back-channel file descriptor (3 or `CUPS_BC_FD`) to read data only when it is available.

The `cupsSideChannelDoRequest` function allows you to get out-of-band status information and do synchronization with the device. For example, the following code gets the current IEEE-1284 device ID string from the backend:

    #include <cups/sidechannel.h>

    char data[2049];
    int datalen;
    cups_sc_status_t status;

    /* Tell cupsSideChannelDoRequest() how big our buffer is, less 1 byte for
       nul-termination... */
    datalen = sizeof(data) - 1;

    /* Get the IEEE-1284 device ID, waiting for up to 1 second */
    status = cupsSideChannelDoRequest(CUPS_SC_CMD_GET_DEVICE_ID, data, &datalen, 1.0);

    /* Use the returned value if OK was returned and the length is non-zero */
    if (status == CUPS_SC_STATUS_OK && datalen > 0)
      data[datalen] = '\0';
    else
      data[0] = '\0';

### Communicating with Filters

Backends communicate with filters using the reciprocal functions `cupsBackChannelWrite`, `cupsSideChannelRead`, and `cupsSideChannelWrite`. We recommend writing back-channel data using a timeout of 1.0 seconds:

    #include <cups/cups.h>

    char buffer[8192];
    ssize_t bytes;

    /* Obtain data from printer/device */
    ...

    /* Use a timeout of 1.0 seconds to give filters a chance to read */
    cupsBackChannelWrite(buffer, bytes, 1.0);

The `cupsSideChannelRead` function reads a side-channel command from a filter, driver, or port monitor. Backends can either poll for commands using a `timeout` of 0.0, wait indefinitely for commands using a `timeout` of -1.0 (probably in a separate thread for that purpose), or use `select` or `poll` on the `CUPS_SC_FD` file descriptor (4) to handle input and output on several file descriptors at the same time.

Once a command is processed, the backend uses the `cupsSideChannelWrite` function to send its response. For example, the following code shows how to poll for a side-channel command and respond to it:

    #include <cups/sidechannel.h>

    cups_sc_command_t command;
    cups_sc_status_t status;
    char data[2048];
    int datalen = sizeof(data);

    /* Poll for a command... */
    if (!cupsSideChannelRead(&command, &status, data, &datalen, 0.0))
    {
      switch (command)
      {
        /* handle supported commands, fill data/datalen/status with values as needed */

        default :
            status  = CUPS_SC_STATUS_NOT_IMPLEMENTED;
    	datalen = 0;
    	break;
      }

      /* Send a response... */
      cupsSideChannelWrite(command, status, data, datalen, 1.0);
    }

### Doing SNMP Queries with Network Printers

The Simple Network Management Protocol (SNMP) allows you to get the current status, page counter, and supply levels from most network printers. Every piece of information is associated with an Object Identifier (OID), and every printer has a community name associated with it. OIDs can be queried directly or by "walking" over a range of OIDs with a common prefix.

The two CUPS SNMP functions provide a simple API for querying network printers through the side-channel interface. Each accepts a string containing an OID like ".1.3.6.1.2.1.43.10.2.1.4.1.1" (the standard page counter OID) along with a timeout for the query.

The `cupsSideChannelSNMPGet` function queries a single OID and returns the value as a string in a buffer you supply:

    #include <cups/sidechannel.h>

    char data[512];
    int datalen = sizeof(data);

    if (cupsSideChannelSNMPGet(".1.3.6.1.2.1.43.10.2.1.4.1.1", data, &datalen, 5.0)
            == CUPS_SC_STATUS_OK)
    {
      /* Do something with the value */
      printf("Page counter is: %s\n", data);
    }

The `cupsSideChannelSNMPWalk` function allows you to query a whole group of OIDs, calling a function of your choice for each OID that is found:

    #include <cups/sidechannel.h>

    void
    my_callback(const char *oid, const char *data, int datalen, void *context)
    {
      /* Do something with the value */
      printf("%s=%s\n", oid, data);
    }

    ...

    void *my_data;

    cupsSNMPSideChannelWalk(".1.3.6.1.2.1.43", 5.0, my_callback, my_data);