12264Sjacobs /* 22264Sjacobs * CDDL HEADER START 32264Sjacobs * 42264Sjacobs * The contents of this file are subject to the terms of the 52264Sjacobs * Common Development and Distribution License (the "License"). 62264Sjacobs * You may not use this file except in compliance with the License. 72264Sjacobs * 82264Sjacobs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 92264Sjacobs * or http://www.opensolaris.org/os/licensing. 102264Sjacobs * See the License for the specific language governing permissions 112264Sjacobs * and limitations under the License. 122264Sjacobs * 132264Sjacobs * When distributing Covered Code, include this CDDL HEADER in each 142264Sjacobs * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 152264Sjacobs * If applicable, add the following below this CDDL HEADER, with the 162264Sjacobs * fields enclosed by brackets "[]" replaced with your own identifying 172264Sjacobs * information: Portions Copyright [yyyy] [name of copyright owner] 182264Sjacobs * 192264Sjacobs * CDDL HEADER END 202264Sjacobs */ 212264Sjacobs 222264Sjacobs /* 23*5825Sjacobs * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 242264Sjacobs * Use is subject to license terms. 252264Sjacobs * 262264Sjacobs */ 272264Sjacobs 282264Sjacobs /* $Id: lpd-job.c 157 2006-04-26 15:07:55Z ktou $ */ 292264Sjacobs 302264Sjacobs #pragma ident "%Z%%M% %I% %E% SMI" 312264Sjacobs 322264Sjacobs #define __EXTENSIONS__ /* for strtok_r() */ 332264Sjacobs #include <stdio.h> 342264Sjacobs #include <stdlib.h> 352264Sjacobs #include <unistd.h> 362264Sjacobs #include <errno.h> 372264Sjacobs #include <limits.h> 382264Sjacobs #include <sys/types.h> 392264Sjacobs #include <sys/stat.h> 402264Sjacobs #include <fcntl.h> 412264Sjacobs #include <string.h> 422264Sjacobs #include <pwd.h> 432264Sjacobs #include <libintl.h> 442264Sjacobs #include <papi_impl.h> 452264Sjacobs 462264Sjacobs enum { LPD_RFC, LPD_SVR4 }; 472264Sjacobs 482264Sjacobs static char 492264Sjacobs mime_type_to_rfc1179_type(char *mime) 502264Sjacobs { 512264Sjacobs static struct { char *mime; char rfc; } cvt[] = { 523125Sjacobs { "text/plain", 'f' }, 532264Sjacobs { "application/octet-stream", 'l' }, 542264Sjacobs { "application/postscript", 'f' }, /* rfc incorrectly has 'o' */ 552264Sjacobs { "application/x-pr", 'p' }, 562264Sjacobs { "application/x-cif", 'c' }, 572264Sjacobs { "application/x-dvi", 'd' }, 582264Sjacobs { "application/x-fortran", 'r' }, 592264Sjacobs { "application/x-plot", 'g' }, 602264Sjacobs { "application/x-ditroff", 'n' }, 612264Sjacobs { "application/x-troff", 't' }, 622264Sjacobs { "application/x-raster", 'v' }, 632264Sjacobs { NULL, 0} 642264Sjacobs }; 652264Sjacobs char result = '\0'; 662264Sjacobs 672264Sjacobs if (mime != NULL) { 682264Sjacobs int i; 692264Sjacobs 702264Sjacobs for (i = 0; cvt[i].mime != NULL; i++) 712264Sjacobs if (strcasecmp(cvt[i].mime, mime) == 0) { 722264Sjacobs result = cvt[i].rfc; 732264Sjacobs break; 742264Sjacobs } 752264Sjacobs } 762264Sjacobs 772264Sjacobs return (result); 782264Sjacobs } 792264Sjacobs 802264Sjacobs static papi_status_t 812264Sjacobs add_lpd_control_line(char **metadata, char code, char *value) 822264Sjacobs { 832264Sjacobs size_t size = 0; 842264Sjacobs char line[BUFSIZ]; 852264Sjacobs 862264Sjacobs if ((metadata == NULL) || (value == NULL)) 872264Sjacobs return (PAPI_BAD_REQUEST); 882264Sjacobs 892264Sjacobs if (*metadata != NULL) 902264Sjacobs size = strlen(*metadata); 912264Sjacobs size += strlen(value) + 3; 922264Sjacobs 932264Sjacobs if (*metadata == NULL) { 942264Sjacobs *metadata = (char *)calloc(1, size); 952264Sjacobs } else { 962264Sjacobs void *tmp; 972264Sjacobs tmp = realloc(*metadata, size); 982264Sjacobs if (tmp) 992264Sjacobs *metadata = (char *)tmp; 1002264Sjacobs else 1012264Sjacobs return (PAPI_TEMPORARY_ERROR); 1022264Sjacobs } 1032264Sjacobs 1042264Sjacobs snprintf(line, sizeof (line), "%c%s\n", code, value); 1052264Sjacobs strlcat(*metadata, line, size); 1062264Sjacobs 1072264Sjacobs return (PAPI_OK); 1082264Sjacobs } 1092264Sjacobs 1102264Sjacobs static papi_status_t 1112264Sjacobs add_svr4_control_line(char **metadata, char code, char *value) 1122264Sjacobs { 1132264Sjacobs 1142264Sjacobs char line[BUFSIZ]; 1152264Sjacobs 1162264Sjacobs if ((metadata == NULL) || (value == NULL)) 1172264Sjacobs return (PAPI_BAD_REQUEST); 1182264Sjacobs 1192264Sjacobs snprintf(line, sizeof (line), "%c%s", code, value); 1202264Sjacobs 1212264Sjacobs return (add_lpd_control_line(metadata, '5', line)); 1222264Sjacobs } 1232264Sjacobs 1242264Sjacobs static papi_status_t 1252264Sjacobs add_hpux_control_line(char **metadata, char *value) 1262264Sjacobs { 1272264Sjacobs 1282264Sjacobs char line[BUFSIZ]; 1292264Sjacobs 1302264Sjacobs if ((metadata == NULL) || (value == NULL)) 1312264Sjacobs return (PAPI_BAD_REQUEST); 1322264Sjacobs 1332264Sjacobs snprintf(line, sizeof (line), " O%s", value); 1342264Sjacobs 1352264Sjacobs return (add_lpd_control_line(metadata, 'N', line)); 1362264Sjacobs } 1372264Sjacobs 1382264Sjacobs static papi_status_t 1392264Sjacobs add_int_control_line(char **metadata, char code, int value, int flag) 1402264Sjacobs { 1412264Sjacobs char buf[16]; 1422264Sjacobs 1432264Sjacobs snprintf(buf, sizeof (buf), "%d", value); 1442264Sjacobs 1452264Sjacobs if (flag == LPD_SVR4) 1462264Sjacobs return (add_svr4_control_line(metadata, code, buf)); 1472264Sjacobs else 1482264Sjacobs return (add_lpd_control_line(metadata, code, buf)); 1492264Sjacobs } 1502264Sjacobs 1512264Sjacobs static papi_status_t 1522264Sjacobs lpd_add_rfc1179_attributes(service_t *svc, papi_attribute_t **attributes, 1532264Sjacobs char **metadata, papi_attribute_t ***used) 1542264Sjacobs { 1552264Sjacobs papi_status_t status = PAPI_OK; 1562264Sjacobs char *s; 1572264Sjacobs int integer; 1582264Sjacobs char bool; 1592264Sjacobs char host[BUFSIZ]; 1602264Sjacobs char *user = "nobody"; 1612264Sjacobs uid_t uid = getuid(); 1622264Sjacobs struct passwd *pw; 1632264Sjacobs 1642264Sjacobs if (svc == NULL) 1652264Sjacobs return (PAPI_BAD_REQUEST); 1662264Sjacobs 1672264Sjacobs /* There is nothing to do */ 1682264Sjacobs if (attributes == NULL) 1692264Sjacobs return (PAPI_OK); 1702264Sjacobs 1712264Sjacobs gethostname(host, sizeof (host)); 1722264Sjacobs add_lpd_control_line(metadata, 'H', host); 1732264Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL, 1742264Sjacobs "job-originating-host-name", host); 1752264Sjacobs 1762264Sjacobs if ((pw = getpwuid(uid)) != NULL) 1772264Sjacobs user = pw->pw_name; 1782264Sjacobs if (uid == 0) 1792264Sjacobs papiAttributeListGetString(svc->attributes, NULL, "username", 1802264Sjacobs &user); 1812264Sjacobs add_lpd_control_line(metadata, 'P', user); 1822264Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL, 1832264Sjacobs "job-originating-user-name", user); 1842264Sjacobs 1852264Sjacobs /* Class for Banner Page */ 1862264Sjacobs s = NULL; 1872264Sjacobs papiAttributeListGetString(attributes, NULL, "rfc-1179-class", &s); 1882264Sjacobs if (s != NULL) { 1892264Sjacobs add_lpd_control_line(metadata, 'C', s); 1902264Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL, 1912264Sjacobs "rfc-1179-class", s); 1922264Sjacobs } 1932264Sjacobs 1942264Sjacobs /* Print Banner Page */ 1952264Sjacobs s = NULL; 1962264Sjacobs papiAttributeListGetString(attributes, NULL, "job-sheets", &s); 1972264Sjacobs if ((s != NULL) && (strcmp(s, "standard") == 0)) { 1982264Sjacobs add_lpd_control_line(metadata, 'L', user); 1992264Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL, 2002264Sjacobs "job-sheets", s); 2012264Sjacobs } 2022264Sjacobs 2032264Sjacobs /* Jobname */ 2042264Sjacobs s = NULL; 2052264Sjacobs papiAttributeListGetString(attributes, NULL, "job-name", &s); 2062264Sjacobs if (s != NULL) { 2072264Sjacobs add_lpd_control_line(metadata, 'J', s); 2082264Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL, 2092264Sjacobs "job-name", s); 2102264Sjacobs } 2112264Sjacobs 2122264Sjacobs /* User to mail when job is done - lpr -m */ 2132264Sjacobs bool = PAPI_FALSE; 2142264Sjacobs papiAttributeListGetBoolean(attributes, NULL, "rfc-1179-mail", &bool); 2152264Sjacobs if (bool == PAPI_TRUE) { 2162264Sjacobs add_lpd_control_line(metadata, 'M', user); 2172264Sjacobs papiAttributeListAddBoolean(used, PAPI_ATTR_EXCL, 2182264Sjacobs "rfc-1179-mail", bool); 2192264Sjacobs } 2202264Sjacobs 2212264Sjacobs /* Title for pr */ 2222264Sjacobs s = NULL; 2232264Sjacobs papiAttributeListGetString(attributes, NULL, "pr-title", &s); 2242264Sjacobs if (s != NULL) { 2252264Sjacobs add_lpd_control_line(metadata, 'T', s); 2262264Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL, 2272264Sjacobs "pr-title", s); 2282264Sjacobs } 2292264Sjacobs 2302264Sjacobs /* Indent - used with pr filter */ 2312264Sjacobs integer = 0; 2322264Sjacobs papiAttributeListGetInteger(attributes, NULL, "pr-indent", &integer); 2332264Sjacobs if (integer >= 1) { 2342264Sjacobs add_int_control_line(metadata, 'I', integer, LPD_RFC); 2352264Sjacobs papiAttributeListAddInteger(used, PAPI_ATTR_EXCL, 2362264Sjacobs "pr-indent", integer); 2372264Sjacobs } 2382264Sjacobs 2392264Sjacobs /* Width - used with pr filter */ 2402264Sjacobs integer = 0; 2412264Sjacobs papiAttributeListGetInteger(attributes, NULL, "pr-width", &integer); 2422264Sjacobs if (integer >= 1) { 2432264Sjacobs add_int_control_line(metadata, 'W', integer, LPD_RFC); 2442264Sjacobs papiAttributeListAddInteger(used, PAPI_ATTR_EXCL, 2452264Sjacobs "pr-width", integer); 2462264Sjacobs } 2472264Sjacobs 2482264Sjacobs /* file with Times Roman font lpr -1 */ 2492264Sjacobs s = NULL; 2502264Sjacobs papiAttributeListGetString(attributes, NULL, "rfc-1179-font-r", &s); 2512264Sjacobs if (s != NULL) { 2522264Sjacobs add_lpd_control_line(metadata, '1', s); 2532264Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL, 2542264Sjacobs "rfc-1179-font-r", s); 2552264Sjacobs } 2562264Sjacobs 2572264Sjacobs /* file with Times Roman font lpr -2 */ 2582264Sjacobs s = NULL; 2592264Sjacobs papiAttributeListGetString(attributes, NULL, "rfc-1179-font-i", &s); 2602264Sjacobs if (s != NULL) { 2612264Sjacobs add_lpd_control_line(metadata, '2', s); 2622264Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL, 2632264Sjacobs "rfc-1179-font-i", s); 2642264Sjacobs } 2652264Sjacobs 2662264Sjacobs /* file with Times Roman font lpr -3 */ 2672264Sjacobs s = NULL; 2682264Sjacobs papiAttributeListGetString(attributes, NULL, "rfc-1179-font-b", &s); 2692264Sjacobs if (s != NULL) { 2702264Sjacobs add_lpd_control_line(metadata, '3', s); 2712264Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL, 2722264Sjacobs "rfc-1179-font-b", s); 2732264Sjacobs } 2742264Sjacobs 2752264Sjacobs /* file with Times Roman font lpr -4 */ 2762264Sjacobs s = NULL; 2772264Sjacobs papiAttributeListGetString(attributes, NULL, "rfc-1179-font-s", &s); 2782264Sjacobs if (s != NULL) { 2792264Sjacobs add_lpd_control_line(metadata, '4', s); 2802264Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL, 2812264Sjacobs "rfc-1179-font-s", s); 2822264Sjacobs } 2832264Sjacobs 2842264Sjacobs return (status); 2852264Sjacobs } 2862264Sjacobs 287*5825Sjacobs static char * 288*5825Sjacobs unused_attributes(papi_attribute_t **list, papi_attribute_t **used) 289*5825Sjacobs { 290*5825Sjacobs char *result = NULL; 291*5825Sjacobs char **names = NULL; 292*5825Sjacobs int i; 293*5825Sjacobs 294*5825Sjacobs if ((list == NULL) || (used == NULL)) 295*5825Sjacobs return (NULL); 296*5825Sjacobs 297*5825Sjacobs for (i = 0; used[i] != NULL; i++) 298*5825Sjacobs list_append(&names, used[i]->name); 299*5825Sjacobs 300*5825Sjacobs if (names != NULL) { 301*5825Sjacobs papi_attribute_t **unused = NULL; 302*5825Sjacobs 303*5825Sjacobs /* add these to the list of things to ignore */ 304*5825Sjacobs list_append(&names, "document-format"); 305*5825Sjacobs list_append(&names, "copies"); 306*5825Sjacobs 307*5825Sjacobs split_and_copy_attributes(names, list, NULL, &unused); 308*5825Sjacobs if (unused != NULL) { 309*5825Sjacobs size_t size = 0; 310*5825Sjacobs 311*5825Sjacobs do { 312*5825Sjacobs size += 1024; 313*5825Sjacobs if (result != NULL) 314*5825Sjacobs free(result); 315*5825Sjacobs result = calloc(1, size); 316*5825Sjacobs } while (papiAttributeListToString(unused, " ", 317*5825Sjacobs result, size) != PAPI_OK); 318*5825Sjacobs papiAttributeListFree(unused); 319*5825Sjacobs } 320*5825Sjacobs free(names); 321*5825Sjacobs } 322*5825Sjacobs 323*5825Sjacobs return (result); 324*5825Sjacobs } 325*5825Sjacobs 3262264Sjacobs /* 3272264Sjacobs * lpd_add_svr4_attributes 3282264Sjacobs * Solaris 2.x LP - BSD protocol extensions 3292264Sjacobs */ 3302264Sjacobs static papi_status_t 3312264Sjacobs lpd_add_svr4_attributes(service_t *svc, papi_attribute_t **attributes, 3322264Sjacobs char **metadata, papi_attribute_t ***used) 3332264Sjacobs { 3342264Sjacobs char *s; 3352264Sjacobs int integer; 3362264Sjacobs 3372264Sjacobs if (svc == NULL) 3382264Sjacobs return (PAPI_BAD_REQUEST); 3392264Sjacobs 3402264Sjacobs /* media */ 3412264Sjacobs s = NULL; 3422264Sjacobs papiAttributeListGetString(attributes, NULL, "media", &s); 3432264Sjacobs if (s != NULL) { 3442264Sjacobs add_svr4_control_line(metadata, 'f', s); 3452264Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL, 3462264Sjacobs "media", s); 3472264Sjacobs } 3482264Sjacobs 3492264Sjacobs /* Handling */ 3502264Sjacobs s = NULL; 3512264Sjacobs papiAttributeListGetString(attributes, NULL, "job_hold_until", &s); 3522264Sjacobs if ((s != NULL) && (strcmp(s, "indefinite"))) { 3532264Sjacobs add_svr4_control_line(metadata, 'H', "hold"); 3542264Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL, 3552264Sjacobs "media", "hold"); 3562264Sjacobs } else if ((s != NULL) && (strcmp(s, "no-hold"))) { 3572264Sjacobs add_svr4_control_line(metadata, 'H', "release"); 3582264Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL, 3592264Sjacobs "media", "release"); 3602264Sjacobs } else if ((s != NULL) && (strcmp(s, "immediate"))) { 3612264Sjacobs add_int_control_line(metadata, 'q', 0, LPD_SVR4); 3622264Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL, 3632264Sjacobs "media", "immediate"); 3642264Sjacobs } 3652264Sjacobs 3662264Sjacobs /* Pages */ 3672264Sjacobs s = NULL; 3682264Sjacobs papiAttributeListGetString(attributes, NULL, "page-ranges", &s); 3692264Sjacobs if (s != NULL) { 3702264Sjacobs add_svr4_control_line(metadata, 'P', s); 3712264Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL, 3722264Sjacobs "page-ranges", s); 3732264Sjacobs } 3742264Sjacobs 3752264Sjacobs /* Priority : lp -q */ 3762264Sjacobs integer = -1; 3772264Sjacobs papiAttributeListGetInteger(attributes, NULL, "priority", &integer); 3782264Sjacobs if (integer != -1) { 3792264Sjacobs add_int_control_line(metadata, 'q', integer, LPD_SVR4); 3802264Sjacobs papiAttributeListAddInteger(used, PAPI_ATTR_EXCL, 3812264Sjacobs "priority", integer); 3822264Sjacobs } 3832264Sjacobs 3842264Sjacobs /* Charset : lp -S */ 3852264Sjacobs s = NULL; 3862264Sjacobs papiAttributeListGetString(attributes, NULL, "lp-charset", &s); 3872264Sjacobs if (s != NULL) { 3882264Sjacobs add_svr4_control_line(metadata, 'S', s); 3892264Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL, 3902264Sjacobs "lp-charset", s); 3912264Sjacobs } 3922264Sjacobs 3932264Sjacobs /* Type : done when adding file */ 3942264Sjacobs 3952264Sjacobs /* Mode : lp -y */ 3962264Sjacobs s = NULL; 3972264Sjacobs papiAttributeListGetString(attributes, NULL, "lp-modes", &s); 3982264Sjacobs if (s != NULL) { 3992264Sjacobs add_svr4_control_line(metadata, 'y', s); 4002264Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL, 4012264Sjacobs "lp-modes", s); 4022264Sjacobs } 4032264Sjacobs 404*5825Sjacobs /* Options lp -o are handled elsewhere */ 405*5825Sjacobs if ((s = unused_attributes(attributes, *used)) != NULL) { 406*5825Sjacobs add_lpd_control_line(metadata, 'O', s); 407*5825Sjacobs free(s); 4082264Sjacobs } 4092264Sjacobs 4102264Sjacobs return (PAPI_OK); 4112264Sjacobs } 4122264Sjacobs 4132264Sjacobs papi_status_t 4142264Sjacobs lpd_add_hpux_attributes(service_t *svc, papi_attribute_t **attributes, 4152264Sjacobs char **metadata, papi_attribute_t ***used) 4162264Sjacobs { 4172264Sjacobs char *s = NULL; 4182264Sjacobs 4192264Sjacobs /* Options lp -o */ 420*5825Sjacobs if ((s = unused_attributes(attributes, *used)) != NULL) { 4212264Sjacobs add_hpux_control_line(metadata, s); 422*5825Sjacobs free(s); 4232264Sjacobs } 4242264Sjacobs 4252264Sjacobs return (PAPI_OK); 4262264Sjacobs } 4272264Sjacobs 4282264Sjacobs papi_status_t 4292264Sjacobs lpd_job_add_attributes(service_t *svc, papi_attribute_t **attributes, 4302264Sjacobs char **metadata, papi_attribute_t ***used) 4312264Sjacobs { 4322264Sjacobs if ((svc == NULL) || (metadata == NULL)) 4332264Sjacobs return (PAPI_BAD_REQUEST); 4342264Sjacobs 4352264Sjacobs lpd_add_rfc1179_attributes(svc, attributes, metadata, used); 4362264Sjacobs 437*5825Sjacobs /* add protocol extensions if applicable */ 4382264Sjacobs if (svc->uri->fragment != NULL) { 4392264Sjacobs if ((strcasecmp(svc->uri->fragment, "solaris") == 0) || 4402264Sjacobs (strcasecmp(svc->uri->fragment, "svr4") == 0)) 4412264Sjacobs lpd_add_svr4_attributes(svc, attributes, metadata, 4422264Sjacobs used); 4432264Sjacobs else if (strcasecmp(svc->uri->fragment, "hpux") == 0) 4442264Sjacobs lpd_add_hpux_attributes(svc, attributes, metadata, 4452264Sjacobs used); 4462264Sjacobs /* 4472264Sjacobs * others could be added here: 4482264Sjacobs * lprng, sco, aix, digital unix, xerox, ... 4492264Sjacobs */ 4502264Sjacobs } 4512264Sjacobs 4522264Sjacobs return (PAPI_OK); 4532264Sjacobs } 4542264Sjacobs 4552264Sjacobs papi_status_t 4562264Sjacobs lpd_job_add_files(service_t *svc, papi_attribute_t **attributes, 4572264Sjacobs char **files, char **metadata, papi_attribute_t ***used) 4582264Sjacobs { 4593125Sjacobs char *format = "text/plain"; 4602264Sjacobs char rfc_fmt = 'l'; 4612264Sjacobs int copies = 1; 4622264Sjacobs char host[BUFSIZ]; 4632264Sjacobs int i; 4642264Sjacobs 4652264Sjacobs if ((svc == NULL) || (attributes == NULL) || (files == NULL) || 4662264Sjacobs (metadata == NULL)) 4672264Sjacobs return (PAPI_BAD_ARGUMENT); 4682264Sjacobs 4692264Sjacobs papiAttributeListGetString(attributes, NULL, "document-format", 4702264Sjacobs &format); 4712264Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL, 4722264Sjacobs "document-format", format); 4732264Sjacobs if ((rfc_fmt = mime_type_to_rfc1179_type(format)) == '\0') { 4742264Sjacobs if ((svc->uri->fragment != NULL) && 4752264Sjacobs ((strcasecmp(svc->uri->fragment, "solaris") == 0) || 4762264Sjacobs (strcasecmp(svc->uri->fragment, "svr4") == 0))) 4772264Sjacobs add_svr4_control_line(metadata, 'T', format); 4782264Sjacobs rfc_fmt = 'l'; 4792264Sjacobs } 4802264Sjacobs 4812264Sjacobs papiAttributeListGetInteger(attributes, NULL, "copies", &copies); 4822264Sjacobs if (copies < 1) 4832264Sjacobs copies = 1; 4842264Sjacobs papiAttributeListAddInteger(used, PAPI_ATTR_EXCL, "copies", copies); 4852264Sjacobs 4862264Sjacobs gethostname(host, sizeof (host)); 4872264Sjacobs 4882264Sjacobs for (i = 0; files[i] != NULL; i++) { 4892264Sjacobs char name[BUFSIZ]; 4902264Sjacobs char key; 4912264Sjacobs int j; 4922264Sjacobs 4932264Sjacobs if ((strcmp("standard input", files[i]) != 0) && 4942264Sjacobs (access(files[i], R_OK) < 0)) { 4952264Sjacobs detailed_error(svc, gettext("aborting request, %s: %s"), 4962264Sjacobs files[i], strerror(errno)); 4972264Sjacobs return (PAPI_NOT_AUTHORIZED); 4982264Sjacobs } 4992264Sjacobs 5002264Sjacobs if (i < 26) 5012264Sjacobs key = 'A' + i; 5022264Sjacobs else if (i < 52) 5032264Sjacobs key = 'a' + (i - 26); 5042264Sjacobs else if (i < 62) 5052264Sjacobs key = '0' + (i - 52); 5062264Sjacobs else { 5072264Sjacobs detailed_error(svc, 5082264Sjacobs gettext("too many files, truncated at 62")); 5092264Sjacobs return (PAPI_OK_SUBST); 5102264Sjacobs } 5112264Sjacobs 5122264Sjacobs snprintf(name, sizeof (name), "df%cXXX%s", key, host); 5132264Sjacobs 5142264Sjacobs for (j = 0; j < copies; j++) 5152264Sjacobs add_lpd_control_line(metadata, rfc_fmt, name); 5162264Sjacobs add_lpd_control_line(metadata, 'U', name); 5172264Sjacobs add_lpd_control_line(metadata, 'N', (char *)files[i]); 5182264Sjacobs } 5192264Sjacobs 5202264Sjacobs return (PAPI_OK); 5212264Sjacobs } 5222264Sjacobs 5232264Sjacobs papi_status_t 5242264Sjacobs lpd_submit_job(service_t *svc, char *metadata, papi_attribute_t ***attributes, 5252264Sjacobs int *ofd) 5262264Sjacobs { 5272264Sjacobs papi_status_t status = PAPI_INTERNAL_ERROR; 5282264Sjacobs int fd; 5292264Sjacobs char path[32]; 5302264Sjacobs char *list[2]; 5312264Sjacobs 5322264Sjacobs if ((svc == NULL) || (metadata == NULL)) 5332264Sjacobs return (PAPI_BAD_ARGUMENT); 5342264Sjacobs 5352264Sjacobs strcpy(path, "/tmp/lpd-job-XXXXXX"); 5362264Sjacobs fd = mkstemp(path); 5372264Sjacobs write(fd, metadata, strlen(metadata)); 5382264Sjacobs close(fd); 5392264Sjacobs 5402264Sjacobs list[0] = path; 5412264Sjacobs list[1] = NULL; 5422264Sjacobs 5433915Sceastha if (((fd = lpd_open(svc, 's', list, 15)) < 0) && (errno != EBADMSG)) { 5442264Sjacobs switch (errno) { 5452264Sjacobs case ENOSPC: 5462264Sjacobs status = PAPI_TEMPORARY_ERROR; 5472264Sjacobs break; 5482264Sjacobs case EIO: 5492264Sjacobs status = PAPI_TEMPORARY_ERROR; 5502264Sjacobs break; 5512264Sjacobs case ECONNREFUSED: 5522264Sjacobs status = PAPI_SERVICE_UNAVAILABLE; 5532264Sjacobs break; 5542264Sjacobs case ENOENT: 5552264Sjacobs status = PAPI_NOT_ACCEPTING; 5562264Sjacobs break; 5572264Sjacobs case EBADMSG: 5582264Sjacobs case EBADF: 5592264Sjacobs status = PAPI_OK; 5602264Sjacobs break; 5612264Sjacobs default: 5622264Sjacobs status = PAPI_TIMEOUT; 5632264Sjacobs break; 5642264Sjacobs } 5652264Sjacobs } else 5662264Sjacobs status = PAPI_OK; 5672264Sjacobs 5682264Sjacobs if (ofd != NULL) 5692264Sjacobs *ofd = fd; 5702264Sjacobs else 5712264Sjacobs close(fd); 5722264Sjacobs 5732264Sjacobs /* read the ID and add it to to the job */ 5742264Sjacobs if ((fd = open(path, O_RDONLY)) >= 0) { 5752264Sjacobs int job_id = 0; 5762264Sjacobs read(fd, &job_id, sizeof (job_id)); 5772264Sjacobs papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE, 5782264Sjacobs "job-id", job_id); 5792264Sjacobs close(fd); 5802264Sjacobs } 5812264Sjacobs 5822264Sjacobs unlink(path); 5832264Sjacobs 5842264Sjacobs return (status); 5852264Sjacobs } 586