10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*1676Sjpk * Common Development and Distribution License (the "License"). 6*1676Sjpk * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*1676Sjpk * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /*LINTLIBRARY*/ 290Sstevel@tonic-gate 300Sstevel@tonic-gate #include <stdlib.h> 310Sstevel@tonic-gate #include <string.h> 320Sstevel@tonic-gate #include <libintl.h> 330Sstevel@tonic-gate #include <papi_impl.h> 340Sstevel@tonic-gate #include <lp.h> 350Sstevel@tonic-gate 360Sstevel@tonic-gate extern int isclass(char *); 370Sstevel@tonic-gate 380Sstevel@tonic-gate void 390Sstevel@tonic-gate papiPrinterFree(papi_printer_t printer) 400Sstevel@tonic-gate { 410Sstevel@tonic-gate printer_t *tmp = printer; 420Sstevel@tonic-gate 430Sstevel@tonic-gate if (tmp != NULL) { 440Sstevel@tonic-gate papiAttributeListFree(tmp->attributes); 450Sstevel@tonic-gate free(tmp); 460Sstevel@tonic-gate } 470Sstevel@tonic-gate } 480Sstevel@tonic-gate 490Sstevel@tonic-gate void 500Sstevel@tonic-gate papiPrinterListFree(papi_printer_t *printers) 510Sstevel@tonic-gate { 520Sstevel@tonic-gate if (printers != NULL) { 530Sstevel@tonic-gate int i; 540Sstevel@tonic-gate 550Sstevel@tonic-gate for (i = 0; printers[i] != NULL; i++) 560Sstevel@tonic-gate papiPrinterFree(printers[i]); 570Sstevel@tonic-gate free(printers); 580Sstevel@tonic-gate } 590Sstevel@tonic-gate } 600Sstevel@tonic-gate 610Sstevel@tonic-gate papi_status_t 620Sstevel@tonic-gate papiPrintersList(papi_service_t handle, const char **requested_attrs, 630Sstevel@tonic-gate const papi_filter_t *filter, papi_printer_t **printers) 640Sstevel@tonic-gate { 650Sstevel@tonic-gate service_t *svc = handle; 660Sstevel@tonic-gate printer_t *p = NULL; 670Sstevel@tonic-gate short status = MOK; 680Sstevel@tonic-gate char *printer = NULL, 690Sstevel@tonic-gate *form = NULL, 700Sstevel@tonic-gate *request_id = NULL, 710Sstevel@tonic-gate *character_set = NULL, 720Sstevel@tonic-gate *reject_reason = NULL, 730Sstevel@tonic-gate *disable_reason = NULL; 740Sstevel@tonic-gate short printer_status = 0; 750Sstevel@tonic-gate long enable_date = 0, reject_date = 0; 760Sstevel@tonic-gate 770Sstevel@tonic-gate if ((handle == NULL) || (printers == NULL)) 780Sstevel@tonic-gate return (PAPI_BAD_ARGUMENT); 790Sstevel@tonic-gate 800Sstevel@tonic-gate if ((filter == NULL) || 810Sstevel@tonic-gate ((filter->filter.bitmask.mask & PAPI_PRINTER_LOCAL) == 820Sstevel@tonic-gate (filter->filter.bitmask.value & PAPI_PRINTER_LOCAL))) { 830Sstevel@tonic-gate /* ask the spooler for the printer(s) and state */ 840Sstevel@tonic-gate if (snd_msg(svc, S_INQUIRE_PRINTER_STATUS, NAME_ALL) < 0) 850Sstevel@tonic-gate return (PAPI_SERVICE_UNAVAILABLE); 860Sstevel@tonic-gate 870Sstevel@tonic-gate do { 880Sstevel@tonic-gate if (rcv_msg(svc, R_INQUIRE_PRINTER_STATUS, &status, 890Sstevel@tonic-gate &printer, &form, &character_set, 900Sstevel@tonic-gate &disable_reason, &reject_reason, 910Sstevel@tonic-gate &printer_status, &request_id, 920Sstevel@tonic-gate &enable_date, &reject_date) < 0) 930Sstevel@tonic-gate return (PAPI_SERVICE_UNAVAILABLE); 940Sstevel@tonic-gate 950Sstevel@tonic-gate if ((p = calloc(1, sizeof (*p))) == NULL) 960Sstevel@tonic-gate return (PAPI_TEMPORARY_ERROR); 970Sstevel@tonic-gate 980Sstevel@tonic-gate lpsched_printer_configuration_to_attributes(svc, p, 990Sstevel@tonic-gate printer); 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate printer_status_to_attributes(p, printer, form, 1020Sstevel@tonic-gate character_set, disable_reason, 1030Sstevel@tonic-gate reject_reason, printer_status, 1040Sstevel@tonic-gate request_id, enable_date, reject_date); 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate list_append(printers, p); 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate } while (status == MOKMORE); 1090Sstevel@tonic-gate } 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate if ((filter == NULL) || 1120Sstevel@tonic-gate ((filter->filter.bitmask.mask & PAPI_PRINTER_CLASS) == 1130Sstevel@tonic-gate (filter->filter.bitmask.value & PAPI_PRINTER_CLASS))) { 1140Sstevel@tonic-gate /* ask the spooler for the class(es) and state */ 1150Sstevel@tonic-gate if (snd_msg(svc, S_INQUIRE_CLASS, NAME_ALL) < 0) 1160Sstevel@tonic-gate return (PAPI_SERVICE_UNAVAILABLE); 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate do { 1190Sstevel@tonic-gate if (rcv_msg(svc, R_INQUIRE_CLASS, &status, &printer, 1200Sstevel@tonic-gate &printer_status, &reject_reason, 1210Sstevel@tonic-gate &reject_date) < 0) 1220Sstevel@tonic-gate return (PAPI_SERVICE_UNAVAILABLE); 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate if ((p = calloc(1, sizeof (*p))) == NULL) 1250Sstevel@tonic-gate return (PAPI_TEMPORARY_ERROR); 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate lpsched_class_configuration_to_attributes(svc, p, 1280Sstevel@tonic-gate printer); 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate class_status_to_attributes(p, printer, printer_status, 1310Sstevel@tonic-gate reject_reason, reject_date); 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate list_append(printers, p); 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate } while (status == MOKMORE); 1360Sstevel@tonic-gate } 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate return (PAPI_OK); 1390Sstevel@tonic-gate } 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate papi_status_t 1420Sstevel@tonic-gate papiPrinterQuery(papi_service_t handle, const char *name, 1430Sstevel@tonic-gate const char **requested_attrs, 1440Sstevel@tonic-gate const papi_attribute_t **job_attrs, 1450Sstevel@tonic-gate papi_printer_t *printer) 1460Sstevel@tonic-gate { 1470Sstevel@tonic-gate papi_status_t pst; 1480Sstevel@tonic-gate service_t *svc = handle; 1490Sstevel@tonic-gate printer_t *p = NULL; 1500Sstevel@tonic-gate char *dest; 1510Sstevel@tonic-gate short status = MOK; 1520Sstevel@tonic-gate char *pname = NULL, 1530Sstevel@tonic-gate *form = NULL, 1540Sstevel@tonic-gate *request_id = NULL, 1550Sstevel@tonic-gate *character_set = NULL, 1560Sstevel@tonic-gate *reject_reason = NULL, 1570Sstevel@tonic-gate *disable_reason = NULL; 1580Sstevel@tonic-gate short printer_status = 0; 1590Sstevel@tonic-gate long enable_date = 0, reject_date = 0; 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate if ((handle == NULL) || (name == NULL) || (printer == NULL)) 1620Sstevel@tonic-gate return (PAPI_BAD_ARGUMENT); 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate if ((*printer = p = calloc(1, sizeof (*p))) == NULL) 1650Sstevel@tonic-gate return (PAPI_TEMPORARY_ERROR); 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate dest = printer_name_from_uri_id(name, -1); 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate if (isprinter(dest) != 0) { 1700Sstevel@tonic-gate pst = lpsched_printer_configuration_to_attributes(svc, p, dest); 1710Sstevel@tonic-gate if (pst != PAPI_OK) 1720Sstevel@tonic-gate return (pst); 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate /* get the spooler status data now */ 1750Sstevel@tonic-gate if (snd_msg(svc, S_INQUIRE_PRINTER_STATUS, dest) < 0) 1760Sstevel@tonic-gate return (PAPI_SERVICE_UNAVAILABLE); 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate if (rcv_msg(svc, R_INQUIRE_PRINTER_STATUS, &status, &pname, 1790Sstevel@tonic-gate &form, &character_set, &disable_reason, 1800Sstevel@tonic-gate &reject_reason, &printer_status, &request_id, 1810Sstevel@tonic-gate &enable_date, &reject_date) < 0) 1820Sstevel@tonic-gate return (PAPI_SERVICE_UNAVAILABLE); 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate printer_status_to_attributes(p, pname, form, character_set, 1850Sstevel@tonic-gate disable_reason, reject_reason, printer_status, 1860Sstevel@tonic-gate request_id, enable_date, reject_date); 1870Sstevel@tonic-gate } else if (isclass(dest) != 0) { 1880Sstevel@tonic-gate pst = lpsched_class_configuration_to_attributes(svc, p, dest); 1890Sstevel@tonic-gate if (pst != PAPI_OK) 1900Sstevel@tonic-gate return (pst); 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate /* get the spooler status data now */ 1930Sstevel@tonic-gate if (snd_msg(svc, S_INQUIRE_CLASS, dest) < 0) 1940Sstevel@tonic-gate return (PAPI_SERVICE_UNAVAILABLE); 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate if (rcv_msg(svc, R_INQUIRE_CLASS, &status, &pname, 1970Sstevel@tonic-gate &printer_status, &reject_reason, 1980Sstevel@tonic-gate &reject_date) < 0) 1990Sstevel@tonic-gate return (PAPI_SERVICE_UNAVAILABLE); 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate class_status_to_attributes(p, pname, printer_status, 2020Sstevel@tonic-gate reject_reason, reject_date); 2030Sstevel@tonic-gate } else if (strcmp(dest, "PrintService") == 0) { 2040Sstevel@tonic-gate /* fill the printer object with service information */ 2050Sstevel@tonic-gate lpsched_service_information(p); 2060Sstevel@tonic-gate } else 2070Sstevel@tonic-gate return (PAPI_NOT_FOUND); 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate free(dest); 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate return (PAPI_OK); 2120Sstevel@tonic-gate } 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate papi_status_t 2150Sstevel@tonic-gate papiPrinterModify(papi_service_t handle, const char *name, 2160Sstevel@tonic-gate const papi_attribute_t **attributes, papi_printer_t *result) 2170Sstevel@tonic-gate { 2180Sstevel@tonic-gate service_t *svc = handle; 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate if ((svc == NULL) || (name == NULL) || (attributes == NULL)) 2210Sstevel@tonic-gate return (PAPI_BAD_ARGUMENT); 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate return (PAPI_OPERATION_NOT_SUPPORTED); 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate papi_status_t 2270Sstevel@tonic-gate papiPrinterPause(papi_service_t handle, const char *name, const char *message) 2280Sstevel@tonic-gate { 2290Sstevel@tonic-gate papi_status_t result; 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate if ((handle == NULL) || (name == NULL)) 2320Sstevel@tonic-gate return (PAPI_BAD_ARGUMENT); 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate result = lpsched_disable_printer(handle, name, message); 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate return (result); 2370Sstevel@tonic-gate } 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate papi_status_t 2400Sstevel@tonic-gate papiPrinterResume(papi_service_t handle, const char *name) 2410Sstevel@tonic-gate { 2420Sstevel@tonic-gate papi_status_t result; 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate if ((handle == NULL) || (name == NULL)) 2450Sstevel@tonic-gate return (PAPI_BAD_ARGUMENT); 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate result = lpsched_enable_printer(handle, name); 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate return (result); 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate papi_status_t 2530Sstevel@tonic-gate papiPrinterPurgeJobs(papi_service_t handle, const char *name, papi_job_t **jobs) 2540Sstevel@tonic-gate { 2550Sstevel@tonic-gate service_t *svc = handle; 2560Sstevel@tonic-gate papi_status_t result = PAPI_OK_SUBST; 2570Sstevel@tonic-gate short more; 2580Sstevel@tonic-gate long status; 2590Sstevel@tonic-gate char *dest; 2600Sstevel@tonic-gate char *req_id; 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate if ((handle == NULL) || (name == NULL)) 2630Sstevel@tonic-gate return (PAPI_BAD_ARGUMENT); 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate dest = printer_name_from_uri_id(name, -1); 2660Sstevel@tonic-gate if (snd_msg(svc, S_CANCEL, dest, "", "") < 0) 2670Sstevel@tonic-gate return (PAPI_SERVICE_UNAVAILABLE); 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate free(dest); 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate do { 2720Sstevel@tonic-gate if (rcv_msg(svc, R_CANCEL, &more, &status, &req_id) < 0) 2730Sstevel@tonic-gate return (PAPI_SERVICE_UNAVAILABLE); 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate switch (status) { 2760Sstevel@tonic-gate case MOK: 2770Sstevel@tonic-gate papiAttributeListAddString(&svc->attributes, PAPI_ATTR_APPEND, 2780Sstevel@tonic-gate "canceled-jobs", req_id); 2790Sstevel@tonic-gate break; 2800Sstevel@tonic-gate case M2LATE: 2810Sstevel@tonic-gate case MUNKNOWN: 2820Sstevel@tonic-gate case MNOINFO: 2830Sstevel@tonic-gate papiAttributeListAddString(&svc->attributes, PAPI_ATTR_APPEND, 2840Sstevel@tonic-gate "cancel-failed", req_id); 2850Sstevel@tonic-gate result = PAPI_DEVICE_ERROR; 2860Sstevel@tonic-gate break; 2870Sstevel@tonic-gate case MNOPERM: 2880Sstevel@tonic-gate papiAttributeListAddString(&svc->attributes, PAPI_ATTR_APPEND, 2890Sstevel@tonic-gate "cancel-failed", req_id); 2900Sstevel@tonic-gate result = PAPI_NOT_AUTHORIZED; 2910Sstevel@tonic-gate break; 2920Sstevel@tonic-gate default: 2930Sstevel@tonic-gate detailed_error(svc, gettext("cancel failed, bad status (%d)\n"), 2940Sstevel@tonic-gate status); 2950Sstevel@tonic-gate return (PAPI_DEVICE_ERROR); 2960Sstevel@tonic-gate } 2970Sstevel@tonic-gate } while (more == MOKMORE); 2980Sstevel@tonic-gate 2990Sstevel@tonic-gate return (result); 3000Sstevel@tonic-gate } 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate papi_status_t 3030Sstevel@tonic-gate papiPrinterListJobs(papi_service_t handle, const char *name, 3040Sstevel@tonic-gate const char **requested_attrs, const int type_mask, 3050Sstevel@tonic-gate const int max_num_jobs, papi_job_t **jobs) 3060Sstevel@tonic-gate { 3070Sstevel@tonic-gate service_t *svc = handle; 3080Sstevel@tonic-gate char *dest; 3090Sstevel@tonic-gate short rc; 3100Sstevel@tonic-gate int count = 1; 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate if ((handle == NULL) || (name == NULL) || (jobs == NULL)) 3130Sstevel@tonic-gate return (PAPI_BAD_ARGUMENT); 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate dest = printer_name_from_uri_id(name, -1); 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate rc = snd_msg(svc, S_INQUIRE_REQUEST_RANK, 0, "", dest, "", "", ""); 3180Sstevel@tonic-gate free(dest); 3190Sstevel@tonic-gate if (rc < 0) 3200Sstevel@tonic-gate return (PAPI_SERVICE_UNAVAILABLE); 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate do { 3230Sstevel@tonic-gate job_t *job = NULL; 3240Sstevel@tonic-gate char *dest = NULL, 3250Sstevel@tonic-gate *ptr, 3260Sstevel@tonic-gate *form = NULL, 3270Sstevel@tonic-gate *req_id = NULL, 3280Sstevel@tonic-gate *charset = NULL, 3290Sstevel@tonic-gate *owner = NULL, 330*1676Sjpk *slabel = NULL, 3310Sstevel@tonic-gate *file = NULL; 3320Sstevel@tonic-gate time_t date = 0; 3330Sstevel@tonic-gate size_t size = 0; 3340Sstevel@tonic-gate short rank = 0, state = 0; 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate if (rcv_msg(svc, R_INQUIRE_REQUEST_RANK, &rc, &req_id, 337*1676Sjpk &owner, &slabel, &size, &date, &state, &dest, 338*1676Sjpk &form, &charset, &rank, &file) < 0) 3390Sstevel@tonic-gate return (PAPI_SERVICE_UNAVAILABLE); 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate if ((rc != MOK) && (rc != MOKMORE)) 3420Sstevel@tonic-gate continue; 3430Sstevel@tonic-gate /* 3440Sstevel@tonic-gate * at this point, we should check to see if the job matches the 3450Sstevel@tonic-gate * selection criterion defined in "type_mask". 3460Sstevel@tonic-gate */ 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate /* too many yet? */ 3490Sstevel@tonic-gate if ((max_num_jobs != 0) && (count++ > max_num_jobs)) 3500Sstevel@tonic-gate continue; 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate if ((job = calloc(1, sizeof (*job))) == NULL) 3530Sstevel@tonic-gate continue; 3540Sstevel@tonic-gate 355*1676Sjpk job_status_to_attributes(job, req_id, owner, slabel, size, 356*1676Sjpk date, state, dest, form, charset, rank, file); 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate if ((ptr = strrchr(file, '-')) != NULL) { 3590Sstevel@tonic-gate *++ptr = '0'; 3600Sstevel@tonic-gate *++ptr = NULL; 3610Sstevel@tonic-gate } 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate lpsched_read_job_configuration(svc, job, file); 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate list_append(jobs, job); 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate } while (rc == MOKMORE); 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate if (rc == MNOINFO) /* If no jobs are found, it's still ok */ 3700Sstevel@tonic-gate rc = MOK; 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate return (lpsched_status_to_papi_status(rc)); 3730Sstevel@tonic-gate } 3740Sstevel@tonic-gate 3750Sstevel@tonic-gate papi_attribute_t ** 3760Sstevel@tonic-gate papiPrinterGetAttributeList(papi_printer_t printer) 3770Sstevel@tonic-gate { 3780Sstevel@tonic-gate printer_t *tmp = printer; 3790Sstevel@tonic-gate 3800Sstevel@tonic-gate if (tmp == NULL) 3810Sstevel@tonic-gate return (NULL); 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate return (tmp->attributes); 3840Sstevel@tonic-gate } 385