[Openprinting-papi-svn] SF.net SVN: openprinting: [184] trunk/papi
Brought to you by:
njacobs,
shawn_pratt
From: <nj...@us...> - 2007-07-31 17:04:09
|
Revision: 184 http://openprinting.svn.sourceforge.net/openprinting/?rev=184&view=rev Author: njacobs Date: 2007-07-31 10:04:09 -0700 (Tue, 31 Jul 2007) Log Message: ----------- use gethostname() for linux, resync with Solaris Nevada Modified Paths: -------------- trunk/papi/ChangeLog trunk/papi/source/bsd-sysv-commands/in.lpd.c trunk/papi/source/bsd-sysv-commands/lpstat.c trunk/papi/source/libipp-core/read.c trunk/papi/source/libipp-listener/ipp-listener.c trunk/papi/source/libpapi-dynamic/nss.c trunk/papi/source/libpapi-ipp/ipp-support.c trunk/papi/source/libpapi-ipp/job.c trunk/papi/source/libpapi-ipp/papi_impl.h trunk/papi/source/libpapi-ipp/printer.c trunk/papi/source/libpapi-lpd/job.c trunk/papi/source/libpapi-lpd/lpd-cancel.c trunk/papi/source/libpapi-lpd/lpd-job.c trunk/papi/source/libpapi-lpd/lpd-misc.c trunk/papi/source/libpapi-lpd/lpd-port.c trunk/papi/source/libpapi-lpd/lpd-query.c trunk/papi/source/libpapi-lpd/printer.c Modified: trunk/papi/ChangeLog =================================================================== --- trunk/papi/ChangeLog 2007-02-03 02:35:26 UTC (rev 183) +++ trunk/papi/ChangeLog 2007-07-31 17:04:09 UTC (rev 184) @@ -1,3 +1,27 @@ +2007-07-31 Norm Jacobs <Norm.Jacobs@Sun.COM> + resync with Solaris Nevada, use gethostname() for linux + * source/libipp-listener/ipp-listener.c + * source/libpapi-dynamic/nss.c + gethostname() + * source/libipp-core/read.c + improper read of ipp request id + * source/libpapi-ipp/papi_impl.h + * source/libpapi-ipp/ipp-support.c + * source/libpapi-ipp/printer.c + * source/libpapi-ipp/job.c + IPP/1.1 compliance work + * source/bsd-sysv-commands/lpstat.c + fix regressions in '-lp' and output + * source/libpapi-lpd/lpd-port.c + * source/libpapi-lpd/lpd-cancel.c + * source/libpapi-lpd/printer.c + * source/libpapi-lpd/lpd-job.c + * source/libpapi-lpd/lpd-misc.c + * source/libpapi-lpd/job.c + lpd-port should use less risky interfaces + * source/libpapi-lpd/lpd-query.c + lpd-port should use less risky interfaces, better parsing + 2007-02-02 Norm Jacobs <Norm.Jacobs@Sun.COM> * source/libipp-listener/ipp-listener.c * source/libpapi-common/papi.h Modified: trunk/papi/source/bsd-sysv-commands/in.lpd.c =================================================================== --- trunk/papi/source/bsd-sysv-commands/in.lpd.c 2007-02-03 02:35:26 UTC (rev 183) +++ trunk/papi/source/bsd-sysv-commands/in.lpd.c 2007-07-31 17:04:09 UTC (rev 184) @@ -38,13 +38,13 @@ #include <libintl.h> #include <pwd.h> #include <grp.h> +#include <sys/param.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> -#include <sys/systeminfo.h> #include <papi.h> #include <uri.h> @@ -91,7 +91,7 @@ /* duplicate the name because gethostbyXXXX() is not reentrant */ hostname = strdup(hp->h_name); - (void) sysinfo(SI_HOSTNAME, myname, sizeof (myname)); + gethostname(myname, sizeof (myname)); /* is it from one of my addresses ? */ if ((hp = getipnodebyname(myname, AF_INET6, AI_ALL|AI_V4MAPPED, @@ -612,7 +612,7 @@ } /* is it the local host? */ - sysinfo(SI_HOSTNAME, buf, sizeof (buf)); + gethostname(buf, sizeof (buf)); if ((strcasecmp(uri->host, "localhost") != 0) && (strcasecmp(uri->host, buf) != 0)) { uri_free(uri); Modified: trunk/papi/source/bsd-sysv-commands/lpstat.c =================================================================== --- trunk/papi/source/bsd-sysv-commands/lpstat.c 2007-02-03 02:35:26 UTC (rev 183) +++ trunk/papi/source/bsd-sysv-commands/lpstat.c 2007-07-31 17:04:09 UTC (rev 184) @@ -33,6 +33,7 @@ #include <string.h> #include <locale.h> #include <libintl.h> +#include <ctype.h> #include <pwd.h> #include <papi.h> #include <uri.h> @@ -531,10 +532,14 @@ status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback, encryption, NULL); if (status != PAPI_OK) { - fprintf(stderr, gettext( - "Failed to contact service for %s: %s\n"), - name ? name : "(NULL)", - verbose_papi_message(svc, status)); + if (status == PAPI_NOT_FOUND) + fprintf(stderr, gettext("%s: unknown printer\n"), + name ? name : "(NULL)"); + else + fprintf(stderr, gettext( + "Failed to contact service for %s: %s\n"), + name ? name : "(NULL)", + verbose_papi_message(svc, status)); papiServiceDestroy(svc); return (-1); } @@ -857,8 +862,18 @@ (void) textdomain("SUNW_OST_OSCMD"); argv = (char **)calloc((ac + 1), sizeof (char *)); - for (c = 0; c < ac; c++) - argv[c] = av[c]; + for (c = 0; c < ac; c++) { + if ((av[c][0] == '-') && (av[c][1] == 'l') && + (isalpha(av[c][2]) != 0)) { + /* preserve old "-l[po...]" behavior */ + argv[c] = &av[c][1]; + argv[c][0] = '-'; + verbose = 1; + + } else + argv[c] = av[c]; + } + argv[c++] = "--"; ac = c; Modified: trunk/papi/source/libipp-core/read.c =================================================================== --- trunk/papi/source/libipp-core/read.c 2007-02-03 02:35:26 UTC (rev 183) +++ trunk/papi/source/libipp-core/read.c 2007-07-31 17:04:09 UTC (rev 184) @@ -621,7 +621,7 @@ (void) papiAttributeListAddInteger(message, PAPI_ATTR_REPLACE, attr_name, s); - memcpy(&s, &buf[4], 4); + memcpy(&i, &buf[4], 4); i = (uint32_t)ntohl(i); (void) papiAttributeListAddInteger(message, PAPI_ATTR_REPLACE, "request-id", i); Modified: trunk/papi/source/libipp-listener/ipp-listener.c =================================================================== --- trunk/papi/source/libipp-listener/ipp-listener.c 2007-02-03 02:35:26 UTC (rev 183) +++ trunk/papi/source/libipp-listener/ipp-listener.c 2007-07-31 17:04:09 UTC (rev 184) @@ -38,7 +38,6 @@ #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> -#include <sys/systeminfo.h> #include <papi.h> #include <ipp-listener.h> @@ -397,7 +396,7 @@ } /* does the host match up */ - sysinfo(SI_HOSTNAME, buf, sizeof (buf)); + gethostname(buf, sizeof (buf)); if ((strcasecmp(uri->host, "localhost") != 0) && (strcasecmp(uri->host, buf) != 0)) { uri_free(uri); Modified: trunk/papi/source/libpapi-dynamic/nss.c =================================================================== --- trunk/papi/source/libpapi-dynamic/nss.c 2007-02-03 02:35:26 UTC (rev 183) +++ trunk/papi/source/libpapi-dynamic/nss.c 2007-07-31 17:04:09 UTC (rev 184) @@ -44,9 +44,6 @@ #include <nss_dbdefs.h> #endif #include <config-site.h> -#if defined(__sun) && defined(__SVR4) -#include <sys/systeminfo.h> -#endif static char * @@ -197,7 +194,7 @@ return (1); /* is it the {nodename} */ - sysinfo(SI_HOSTNAME, hostname, sizeof (hostname)); + gethostname(hostname, sizeof (hostname)); if (strncasecmp(host, hostname, strlen(hostname)) == 0) return (1); Modified: trunk/papi/source/libpapi-ipp/ipp-support.c =================================================================== --- trunk/papi/source/libpapi-ipp/ipp-support.c 2007-02-03 02:35:26 UTC (rev 183) +++ trunk/papi/source/libpapi-ipp/ipp-support.c 2007-07-31 17:04:09 UTC (rev 184) @@ -40,6 +40,9 @@ #include <ipp.h> +static void ipp_add_printer_uri(service_t *svc, char *name, + papi_attribute_t ***op); + papi_status_t http_to_papi_status(http_status_t status) { @@ -161,7 +164,7 @@ void ipp_initialize_operational_attributes(service_t *svc, papi_attribute_t ***op, - papi_attribute_t **attributes) + char *printer, int job_id) { char *charset = "utf-8"; /* default to UTF-8 encoding */ char *language = setlocale(LC_ALL, ""); @@ -172,18 +175,22 @@ * All IPP requests must contain the following: * attributes-charset (UTF-8) * attributes-natural-language (our current locale) - * requesting-user-name (process user) + * (object identifier) printer-uri/job-id or job-uri + * requesting-user-name (process user or none) */ - papiAttributeListGetString(attributes, NULL, - "attributes-charset", &charset); papiAttributeListAddString(op, PAPI_ATTR_EXCL, "attributes-charset", charset); - papiAttributeListGetString(attributes, NULL, - "attributes-natural-language", &language); papiAttributeListAddString(op, PAPI_ATTR_EXCL, "attributes-natural-language", language); + if (printer != NULL) + ipp_add_printer_uri(svc, printer, op); + + if ((printer != NULL) && (job_id >= 0)) + papiAttributeListAddInteger(op, PAPI_ATTR_EXCL, + "job-id", job_id); + if ((pw = getpwuid(getuid())) != NULL) user = pw->pw_name; /* @@ -193,8 +200,6 @@ if (geteuid() == 0) { if (svc->user != NULL) user = svc->user; - papiAttributeListGetString(attributes, NULL, - "requesting-user-name", &user); } papiAttributeListAddString(op, PAPI_ATTR_REPLACE, "requesting-user-name", user); @@ -223,7 +228,7 @@ return (PAPI_TEMPORARY_ERROR); ipp_initialize_request(svc, &request, OPID_CUPS_GET_DEFAULT); - ipp_initialize_operational_attributes(svc, &op, NULL); + ipp_initialize_operational_attributes(svc, &op, NULL, -1); papiAttributeListAddString(&op, PAPI_ATTR_APPEND, "requested-attributes", "printer-uri-supported"); papiAttributeListAddCollection(&request, PAPI_ATTR_REPLACE, @@ -251,7 +256,7 @@ return (result); } -void +static void ipp_add_printer_uri(service_t *svc, char *name, papi_attribute_t ***op) { char *uri = name; Modified: trunk/papi/source/libpapi-ipp/job.c =================================================================== --- trunk/papi/source/libpapi-ipp/job.c 2007-02-03 02:35:26 UTC (rev 183) +++ trunk/papi/source/libpapi-ipp/job.c 2007-07-31 17:04:09 UTC (rev 184) @@ -123,8 +123,7 @@ ipp_initialize_request(svc, request, type); /* create an operational attributes group */ - ipp_initialize_operational_attributes(svc, &operational, NULL); - ipp_add_printer_uri(svc, printer, &operational); + ipp_initialize_operational_attributes(svc, &operational, printer, -1); /* split up the attributes into operational and job attributes */ split_and_copy_attributes(operational_names, attributes, @@ -154,11 +153,8 @@ ipp_initialize_request(svc, &request, type); /* create an operational attributes group */ - ipp_initialize_operational_attributes(svc, &op, NULL); - ipp_add_printer_uri(svc, printer, &op); + ipp_initialize_operational_attributes(svc, &op, printer, id); - papiAttributeListAddInteger(&op, PAPI_ATTR_REPLACE, "job-id", - id); papiAttributeListAddString(&op, PAPI_ATTR_REPLACE, "document-name", file); papiAttributeListAddBoolean(&op, PAPI_ATTR_REPLACE, "last-document", @@ -443,10 +439,8 @@ ipp_initialize_request(svc, &request, OPID_GET_JOB_ATTRIBUTES); - ipp_initialize_operational_attributes(svc, &op, NULL); - ipp_add_printer_uri(svc, printer, &op); + ipp_initialize_operational_attributes(svc, &op, printer, job_id); - papiAttributeListAddInteger(&op, PAPI_ATTR_REPLACE, "job-id", job_id); if (requested_attrs != NULL) { int i; @@ -489,10 +483,8 @@ ipp_initialize_request(svc, &request, type); - ipp_initialize_operational_attributes(svc, &op, NULL); - ipp_add_printer_uri(svc, printer, &op); + ipp_initialize_operational_attributes(svc, &op, printer, job_id); - papiAttributeListAddInteger(&op, PAPI_ATTR_REPLACE, "job-id", job_id); papiAttributeListAddCollection(&request, PAPI_ATTR_REPLACE, "operational-attributes-group", op); papiAttributeListFree(op); @@ -558,10 +550,8 @@ ipp_initialize_request(svc, &request, OPID_CUPS_MOVE_JOB); - ipp_initialize_operational_attributes(svc, &op, NULL); - ipp_add_printer_uri(svc, printer, &op); + ipp_initialize_operational_attributes(svc, &op, printer, job_id); - papiAttributeListAddInteger(&op, PAPI_ATTR_REPLACE, "job-id", job_id); papiAttributeListAddCollection(&request, PAPI_ATTR_REPLACE, "operational-attributes-group", op); papiAttributeListFree(op); @@ -603,10 +593,8 @@ ipp_initialize_request(svc, &request, OPID_SET_JOB_ATTRIBUTES); - ipp_initialize_operational_attributes(svc, &op, NULL); - ipp_add_printer_uri(svc, printer, &op); + ipp_initialize_operational_attributes(svc, &op, printer, job_id); - papiAttributeListAddInteger(&op, PAPI_ATTR_REPLACE, "job-id", job_id); papiAttributeListAddCollection(&request, PAPI_ATTR_REPLACE, "operational-attributes-group", op); papiAttributeListFree(op); Modified: trunk/papi/source/libpapi-ipp/papi_impl.h =================================================================== --- trunk/papi/source/libpapi-ipp/papi_impl.h 2007-02-03 02:35:26 UTC (rev 183) +++ trunk/papi/source/libpapi-ipp/papi_impl.h 2007-07-31 17:04:09 UTC (rev 184) @@ -94,13 +94,10 @@ papi_attribute_t ***request, uint16_t type); extern void ipp_initialize_operational_attributes(service_t *svc, papi_attribute_t ***op, - papi_attribute_t **attributes); + char *printer, int job_id); extern papi_status_t ipp_to_papi_status(uint16_t status); extern papi_status_t http_to_papi_status(http_status_t status); -extern void ipp_add_printer_uri(service_t *svc, char *name, - papi_attribute_t ***op); - /* service related interfaces */ extern void detailed_error(service_t *svc, char *fmt, ...); extern papi_status_t service_connect(service_t *svc, char *service_name); Modified: trunk/papi/source/libpapi-ipp/printer.c =================================================================== --- trunk/papi/source/libpapi-ipp/printer.c 2007-02-03 02:35:26 UTC (rev 183) +++ trunk/papi/source/libpapi-ipp/printer.c 2007-07-31 17:04:09 UTC (rev 184) @@ -94,7 +94,7 @@ return (result); ipp_initialize_request(svc, &request, OPID_CUPS_GET_PRINTERS); - ipp_initialize_operational_attributes(svc, &op, NULL); + ipp_initialize_operational_attributes(svc, &op, NULL, -1); if (requested_attrs != NULL) { int i; @@ -154,8 +154,7 @@ ipp_initialize_request(svc, &request, OPID_GET_PRINTER_ATTRIBUTES); - ipp_initialize_operational_attributes(svc, &op, NULL); - ipp_add_printer_uri(svc, name, &op); + ipp_initialize_operational_attributes(svc, &op, name, -1); if (requested_attrs != NULL) { int i; @@ -198,8 +197,7 @@ ipp_initialize_request(svc, &request, type); - ipp_initialize_operational_attributes(svc, &op, NULL); - ipp_add_printer_uri(svc, name, &op); + ipp_initialize_operational_attributes(svc, &op, name, -1); switch (type) { case OPID_DISABLE_PRINTER: @@ -286,8 +284,7 @@ ipp_initialize_request(svc, &request, OPID_SET_PRINTER_ATTRIBUTES); - ipp_initialize_operational_attributes(svc, &op, NULL); - ipp_add_printer_uri(svc, name, &op); + ipp_initialize_operational_attributes(svc, &op, name, -1); papiAttributeListAddCollection(&request, PAPI_ATTR_REPLACE, "operational-attributes-group", op); @@ -334,8 +331,7 @@ ipp_initialize_request(svc, &request, OPID_PURGE_JOBS); - ipp_initialize_operational_attributes(svc, &op, NULL); - ipp_add_printer_uri(svc, name, &op); + ipp_initialize_operational_attributes(svc, &op, name, -1); papiAttributeListAddCollection(&request, PAPI_ATTR_REPLACE, "operational-attributes-group", op); @@ -383,8 +379,7 @@ ipp_initialize_request(svc, &request, OPID_GET_JOBS); - ipp_initialize_operational_attributes(svc, &op, NULL); - ipp_add_printer_uri(svc, name, &op); + ipp_initialize_operational_attributes(svc, &op, name, -1); if (requested_attrs != NULL) { int i; Modified: trunk/papi/source/libpapi-lpd/job.c =================================================================== --- trunk/papi/source/libpapi-lpd/job.c 2007-02-03 02:35:26 UTC (rev 183) +++ trunk/papi/source/libpapi-lpd/job.c 2007-07-31 17:04:09 UTC (rev 184) @@ -58,7 +58,7 @@ if (job_ticket != NULL) { detailed_error(svc, - gettext("papiJobSubmit: job ticket not supported")); + gettext("papiJobSubmit: job ticket not supported")); return (PAPI_OPERATION_NOT_SUPPORTED); } @@ -67,16 +67,19 @@ if ((*job = j = (job_t *)calloc(1, sizeof (*j))) == NULL) { detailed_error(svc, - gettext("calloc() failed")); + gettext("calloc() failed")); return (PAPI_TEMPORARY_ERROR); } /* create a control file */ - status = lpd_job_add_attributes(svc, attributes, &metadata, - &j->attributes); - status = lpd_job_add_files(svc, attributes, files, &metadata, - &j->attributes); + (void) lpd_job_add_attributes(svc, attributes, &metadata, + &j->attributes); + if ((status = lpd_job_add_files(svc, attributes, files, &metadata, + &j->attributes)) != PAPI_OK) { + return (status); + } + /* send the job to the server */ status = lpd_submit_job(svc, metadata, &j->attributes, NULL); free(metadata); @@ -92,7 +95,7 @@ papi_job_ticket_t *job_ticket, char **files, papi_job_t *job) { return (papiJobSubmit(handle, name, job_attributes, - job_ticket, files, job)); + job_ticket, files, job)); } papi_status_t @@ -131,9 +134,9 @@ char *files[] = { "standard input", NULL }; lpd_job_add_files(svc, attributes, files, &metadata, - &(s->job->attributes)); + &(s->job->attributes)); status = lpd_submit_job(svc, metadata, &(s->job->attributes), - &s->fd); + &s->fd); } else { char dfname[18]; @@ -186,9 +189,9 @@ files[1] = NULL; lpd_job_add_files(svc, s->job->attributes, files, &s->metadata, - &(s->job->attributes)); + &(s->job->attributes)); status = lpd_submit_job(svc, s->metadata, - &(s->job->attributes), NULL); + &(s->job->attributes), NULL); unlink(s->dfname); free(s->dfname); } else @@ -252,7 +255,7 @@ if (j != NULL) papiAttributeListGetString(j->attributes, NULL, - "printer-name", &result); + "printer-name", &result); return (result); } @@ -265,7 +268,7 @@ if (j != NULL) papiAttributeListGetInteger(j->attributes, NULL, - "job-id", &result); + "job-id", &result); return (result); } Modified: trunk/papi/source/libpapi-lpd/lpd-cancel.c =================================================================== --- trunk/papi/source/libpapi-lpd/lpd-cancel.c 2007-02-03 02:35:26 UTC (rev 183) +++ trunk/papi/source/libpapi-lpd/lpd-cancel.c 2007-07-31 17:04:09 UTC (rev 184) @@ -49,7 +49,7 @@ list[0] = buf; list[1] = NULL; - if ((fd = lpd_open(svc, 'c', list, 3)) < 0) + if ((fd = lpd_open(svc, 'c', list, 15)) < 0) return (PAPI_INTERNAL_ERROR); memset(buf, 0, sizeof (buf)); @@ -80,7 +80,7 @@ if (svc == NULL) return (PAPI_BAD_ARGUMENT); - if ((fd = lpd_open(svc, 'c', NULL, 3)) < 0) + if ((fd = lpd_open(svc, 'c', NULL, 15)) < 0) return (PAPI_INTERNAL_ERROR); queue = queue_name_from_uri(svc->uri); Modified: trunk/papi/source/libpapi-lpd/lpd-job.c =================================================================== --- trunk/papi/source/libpapi-lpd/lpd-job.c 2007-02-03 02:35:26 UTC (rev 183) +++ trunk/papi/source/libpapi-lpd/lpd-job.c 2007-07-31 17:04:09 UTC (rev 184) @@ -504,7 +504,7 @@ list[0] = path; list[1] = NULL; - if (((fd = lpd_open(svc, 's', list, 3)) < 0) && (errno != EBADMSG)) { + if (((fd = lpd_open(svc, 's', list, 15)) < 0) && (errno != EBADMSG)) { switch (errno) { case ENOSPC: status = PAPI_TEMPORARY_ERROR; Modified: trunk/papi/source/libpapi-lpd/lpd-misc.c =================================================================== --- trunk/papi/source/libpapi-lpd/lpd-misc.c 2007-02-03 02:35:26 UTC (rev 183) +++ trunk/papi/source/libpapi-lpd/lpd-misc.c 2007-07-31 17:04:09 UTC (rev 184) @@ -127,7 +127,7 @@ { int ac, rc = -1, fds[2]; pid_t pid; - char *av[64], buf[BUFSIZ]; + char *av[64], *tmp, buf[BUFSIZ]; if ((svc == NULL) || (svc->uri == NULL)) return (-1); @@ -137,18 +137,30 @@ #endif av[0] = SUID_LPD_PORT; ac = 1; - uri_to_string(svc->uri, buf, sizeof (buf)); - av[ac++] = "-u"; - av[ac++] = strdup(buf); + /* server */ + av[ac++] = "-H"; + av[ac++] = svc->uri->host; + + /* timeout */ if (timeout > 0) { snprintf(buf, sizeof (buf), "%d", timeout); av[ac++] = "-t"; av[ac++] = strdup(buf); } + + /* operation */ snprintf(buf, sizeof (buf), "-%c", type); av[ac++] = buf; + /* queue */ + if ((tmp = strrchr(svc->uri->path, '/')) == NULL) + tmp = svc->uri->path; + else + tmp++; + av[ac++] = tmp; + + /* args */ if (args != NULL) while ((*args != NULL) && (ac < 62)) av[ac++] = *args++; Modified: trunk/papi/source/libpapi-lpd/lpd-port.c =================================================================== --- trunk/papi/source/libpapi-lpd/lpd-port.c 2007-02-03 02:35:26 UTC (rev 183) +++ trunk/papi/source/libpapi-lpd/lpd-port.c 2007-07-31 17:04:09 UTC (rev 184) @@ -50,11 +50,9 @@ #include <pwd.h> #include <sys/sendfile.h> #include <ctype.h> -#include <alloca.h> #ifdef HAVE_PRIV_H #include <priv.h> #endif -#include <papi_impl.h> #ifndef JOB_ID_FILE #define JOB_ID_FILE "/var/run/rfc-1179.seq" @@ -107,7 +105,7 @@ } static int -sock_connect(int sock, uri_t *uri, int timeout) +sock_connect(int sock, char *host, int timeout) { struct hostent *hp; struct servent *sp; @@ -120,13 +118,11 @@ int err, error_num; unsigned timo = 1; - int port = -1; - /* * Get the host address and port number to connect to. */ - if ((uri == NULL) || (uri->host == NULL)) { + if (host == NULL) { return (-1); } @@ -134,7 +130,7 @@ (void) memset((char *)&sin, (int)NULL, sizeof (sin)); #if defined(HAVE_GETIPNODEBYNAME) && defined(HAVE_RRESVPORT_AF) - if ((hp = getipnodebyname(uri->host, AF_INET6, AI_DEFAULT, + if ((hp = getipnodebyname(host, AF_INET6, AI_DEFAULT, &error_num)) == NULL) { errno = ENOENT; return (-1); @@ -142,7 +138,7 @@ (void) memcpy((caddr_t)&sin.sin6_addr, hp->h_addr, hp->h_length); sin.sin6_family = hp->h_addrtype; #else - if ((hp = gethostbyname(uri->host)) == NULL) { + if ((hp = gethostbyname(host)) == NULL) { errno = ENOENT; return (-1); } @@ -156,15 +152,10 @@ return (-1); } - if (uri->port != NULL) - port = atoi(uri->port); - if (port < 0) - port = sp->s_port; - #if defined(HAVE_GETIPNODEBYNAME) && defined(HAVE_RRESVPORT_AF) - sin.sin6_port = port; + sin.sin6_port = sp->s_port; #else - sin.sin_port = port; + sin.sin_port = sp->s_port; #endif retry: @@ -444,8 +435,9 @@ static int -submit_job(int sock, uri_t *uri, int job_id, char *path) +submit_job(int sock, char *printer, int job_id, char *path) { + struct stat st; int current = 0; off_t off = 0; char *metadata = NULL; @@ -454,30 +446,36 @@ int sent_files = 0; char buf[BUFSIZ]; size_t len; - char *printer = queue_name_from_uri(uri); - /* read in the control file */ - if ((fd = open(path, O_RDONLY)) >= 0) { - struct stat st; + /* open the control file */ + if ((fd = open(path, O_RDONLY)) < 0) { + syslog(LOG_ERR, "submit_job(%d, %s, %d, %s): open(): %m", + sock, printer, job_id, path); + return (-1); + } - if (fstat(fd, &st) < 0) { - close(fd); - return (-1); - } + /* get the size of the control file */ + if (fstat(fd, &st) < 0) { + syslog(LOG_ERR, "submit_job(%d, %s, %d, %s): fstat(): %m", + sock, printer, job_id, path); + close(fd); + return (-1); + } - metadata = alloca(st.st_size + 1); - memset(metadata, 0, st.st_size + 1); + /* allocate memory for the control file */ + if ((metadata = calloc(1, st.st_size + 1)) == NULL) { + syslog(LOG_ERR, "submit_job(%d, %s, %d, %s): calloc(): %m", + sock, printer, job_id, path); + close(fd); + return (-1); + } - if (read(fd, metadata, st.st_size) != st.st_size) { - close(fd); - free(metadata); - metadata = NULL; - return (-1); - } - - } else { - syslog(LOG_ERR, - "lpd-port:submit_job:open failed : %m path %s", path); + /* read in the control file */ + if (read(fd, metadata, st.st_size) != st.st_size) { + syslog(LOG_ERR, "submit_job(%d, %s, %d, %s): read(): %m", + sock, printer, job_id, path); + free(metadata); + close(fd); return (-1); } @@ -486,6 +484,9 @@ /* bad control data, dump the job */ syslog(LOG_ALERT, "bad control file, possible subversion attempt"); + free(metadata); + close(fd); + return (-1); } /* request to transfer the job */ @@ -545,13 +546,11 @@ return (0); } - static int -query(int fd, uri_t *uri, int ac, char **av) +query(int fd, char *printer, int ac, char **av) { char buf[BUFSIZ]; int rc, len; - char *printer = queue_name_from_uri(uri); /* build the request */ snprintf(buf, sizeof (buf), "\04%s", printer); @@ -569,11 +568,10 @@ } static int -cancel(int fd, uri_t *uri, int ac, char **av) +cancel(int fd, char *printer, int ac, char **av) { char buf[BUFSIZ]; int rc, len; - char *printer = queue_name_from_uri(uri); /* build the request */ snprintf(buf, sizeof (buf), "\05%s %s", printer, get_user_name()); @@ -602,12 +600,12 @@ else name++; - fprintf(stderr, "usage:\t%s -u uri [-t timeout] " - "[-s control ]\n", name); - fprintf(stderr, "\t%s -u uri [-t timeout] " - "[-c user|job ...]\n", name); - fprintf(stderr, "\t%s -u uri [-t timeout] " - "[-q user|job ...]\n", name); + fprintf(stderr, "usage:\t%s -H host [-t timeout] -s queue control ]\n", + name); + fprintf(stderr, "\t%s -H host [-t timeout] -c queue [user|job ...]\n", + name); + fprintf(stderr, "\t%s -H host [-t timeout] -q queue [user|job ...]\n", + name); exit(EINVAL); } @@ -626,7 +624,7 @@ { enum { OP_NONE, OP_SUBMIT, OP_QUERY, OP_CANCEL } operation = OP_NONE; int fd, c, timeout = 0, exit_code = 0; - uri_t *uri = NULL; + char *host = NULL, *queue = NULL; uid_t uid = getuid(); #ifdef PRIV_ALLSETS priv_set_t *saveset = NULL; @@ -670,44 +668,42 @@ seteuid(uid); #endif - - while ((c = getopt(ac, av, "cqst:u:")) != EOF) { + while ((c = getopt(ac, av, "H:t:c:q:s:")) != EOF) { switch (c) { + case 'H': + host = optarg; + break; + case 't': + timeout = atoi(optarg); + break; case 'c': if (operation != OP_NONE) usage(av[0]); operation = OP_CANCEL; + queue = optarg; break; case 'q': if (operation != OP_NONE) usage(av[0]); operation = OP_QUERY; + queue = optarg; break; case 's': if (operation != OP_NONE) usage(av[0]); operation = OP_SUBMIT; + queue = optarg; break; - case 't': - timeout = atoi(optarg); - break; - case 'u': - if (uri_from_string(optarg, &uri) < 0) - usage(av[0]); - break; default: usage(av[0]); /* does not return */ } } - if ((uri == NULL) || (timeout < 0) || (operation == OP_NONE)) + if ((host == NULL) || (queue == NULL) || (timeout < 0) || + (operation == OP_NONE)) usage(av[0]); - if ((strcasecmp(uri->scheme, "lpd") != 0) && - (strcasecmp(uri->scheme, "rfc-1179") != 0)) - usage(av[0]); - if (operation == OP_SUBMIT) /* get a job-id if we need it */ if ((c = next_job_id()) < 0) { syslog(LOG_ERR, "lpd_port:main:next_job_id fails"); @@ -726,23 +722,22 @@ setreuid(uid, uid); - /* connect to the print service */ - if ((fd = sock_connect(fd, uri, timeout)) < 0) + if ((fd = sock_connect(fd, host, timeout)) < 0) return (errno); /* perform the requested operation */ switch (operation) { case OP_SUBMIT: /* transfer the job, close the fd */ - if (submit_job(fd, uri, c, av[optind]) < 0) + if (submit_job(fd, queue, c, av[optind]) < 0) exit_code = errno; break; case OP_QUERY: /* send the query string, return the fd */ - if (query(fd, uri, ac - optind, &av[optind]) < 0) + if (query(fd, queue, ac - optind, &av[optind]) < 0) exit_code = errno; break; case OP_CANCEL: /* send the cancel string, return the fd */ - if (cancel(fd, uri, ac - optind, &av[optind]) < 0) + if (cancel(fd, queue, ac - optind, &av[optind]) < 0) exit_code = errno; break; default: /* This should never happen */ Modified: trunk/papi/source/libpapi-lpd/lpd-query.c =================================================================== --- trunk/papi/source/libpapi-lpd/lpd-query.c 2007-02-03 02:35:26 UTC (rev 183) +++ trunk/papi/source/libpapi-lpd/lpd-query.c 2007-07-31 17:04:09 UTC (rev 184) @@ -27,7 +27,6 @@ #pragma ident "$Id$" -#define __EXTENSIONS__ /* for strtok_r() */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> @@ -38,86 +37,130 @@ #include <ctype.h> #include <string.h> #include <stdarg.h> +#include <regex.h> #include <papi_impl.h> +/* The string is modified by this call */ +static char * +regvalue(regmatch_t match, char *string) +{ + char *result = NULL; + + if (match.rm_so != match.rm_eo) { + result = string + match.rm_so; + *(result + (match.rm_eo - match.rm_so)) = '\0'; + } + + return (result); +} + +/* + * Print job entries start with: + * (user): (rank) [job (number) (...)] + * (user) is the job-owner's user name + * (rank) is the rank in queue. (active, 1st, 2nd, ...) + * (number) is the job number + * (...) is an optional hostname + * some servers will use whitespace a little differently than is displayed + * above. The regular expression below makes whitespace optional in some + * places. + */ +static char *job_expr = "^(.*[[:alnum:]]):[[:space:]]+([[:alnum:]]+)[[:space:]]+[[][[:space:]]*job[[:space:]]*([[:digit:]]+)[[:space:]]*(.*)]"; +static regex_t job_re; + +/* + * status line(s) for "processing" printers will contain one of the following: + * ready and printing + * Printing + */ +static char *proc_expr = "(ready and printing|printing)"; +static regex_t proc_re; + +/* + * status line(s) for "idle" printers will contain one of the following: + * no entries + * (printer) is ready + * idle + */ +static char *idle_expr = "(no entries|is ready| idle)"; +static regex_t idle_re; + +/* + * document line(s) + * (copies) copies of (name) (size) bytes + * (name) (size) bytes + * document lines can be in either format above. + * (copies) is the number of copies of the document to print + * (name) is the name of the document: /etc/motd, ... + * (size) is the number of bytes in the document data + */ +static char *doc1_expr = "[[:space:]]+(([[:digit:]]+) copies of )([^[:space:]]+)[[:space:]]*([[:digit:]]+) bytes"; +static char *doc2_expr = "[[:space:]]+()([^[:space:]]+)[[:space:]]*([[:digit:]]+) bytes"; +static regex_t doc1_re; +static regex_t doc2_re; + static void -parse_lpd_job_entry(service_t *svc, int fd, job_t **job) +parse_lpd_job(service_t *svc, job_t **job, int fd, char *line, int len) { - char *iter = NULL; - char line[128]; papi_attribute_t **attributes = NULL; - char *p; + regmatch_t matches[5]; + char *s; int octets = 0; - *job = NULL; + /* job_re was compiled in the calling function */ + if (regexec(&job_re, line, (size_t)5, matches, 0) == REG_NOMATCH) + return; - if (fdgets(line, sizeof (line), fd) == NULL) - return; - /* - * 1st line... - * user: rank [job (ID)(host)]\n - */ - if ((p = strtok_r(line, ": ", &iter)) == NULL) /* user: ... */ - return; /* invalid format */ + if ((s = regvalue(matches[1], line)) == NULL) + s = "nobody"; papiAttributeListAddString(&attributes, PAPI_ATTR_REPLACE, - "job-originating-user-name", p); + "job-originating-user-name", s); - p = strtok_r(NULL, "\t ", &iter); /* ...rank... */ + if ((s = regvalue(matches[2], line)) == NULL) + s = "0"; papiAttributeListAddInteger(&attributes, PAPI_ATTR_REPLACE, - "number-of-intervening-jobs", atoi(p) - 1); - p = strtok_r(NULL, " ", &iter); /* ...[job ... */ - if ((p = strtok_r(NULL, "]\n", &iter)) == NULL) /* ...(id)(hostname)] */ - return; - while (isspace(*p)) p++; + "number-of-intervening-jobs", atoi(s) - 1); + + if ((s = regvalue(matches[3], line)) == NULL) + s = "0"; papiAttributeListAddInteger(&attributes, PAPI_ATTR_REPLACE, - "job-id", atoi(p)); - while (isdigit(*(++p))); - while (isspace(*p)) p++; + "job-id", atoi(s)); + + if ((s = regvalue(matches[4], line)) == NULL) + s = svc->uri->host; papiAttributeListAddString(&attributes, PAPI_ATTR_REPLACE, - "job-originating-host-name", p); + "job-originating-host-name", s); - /* - * rest-o-lines - * [(num) copies of ]file size bytes\n - */ - while ((fdgets(line, sizeof (line), fd) != NULL) && (line[0] != '\n')) { - int copies, size; - char *q; + while ((fdgets(line, len, fd) != NULL) && + (regexec(&job_re, line, (size_t)0, NULL, 0) == REG_NOMATCH)) { + int size = 0, copies = 1; + /* process copies/documents */ - /* find the number of copies */ - if ((p = strstr(line, "copies of")) != NULL) { - copies = atoi(line); - p += 9; - } else { + /* doc1_re and doc2_re were compiled in the calling function */ + if ((regexec(&doc1_re, line, (size_t)4, matches, 0) != 0) && + (regexec(&doc2_re, line, (size_t)4, matches, 0) != 0)) + continue; + + if ((s = regvalue(matches[1], line)) == NULL) + s = "1"; + if ((copies = atoi(s)) < 1) copies = 1; - p = line; - } - papiAttributeListAddInteger(&attributes, PAPI_ATTR_EXCL, - "copies", copies); - /* eat the leading whitespace */ - while (isspace(*p) != 0) - p++; - if ((q = strstr(p, " bytes\n")) != NULL) { - /* back up to the beginning of the size */ - do { q--; } while (isdigit(*q) != 0); + if ((s = regvalue(matches[2], line)) == NULL) + s = "unknown"; + papiAttributeListAddString(&attributes, + PAPI_ATTR_APPEND, "job-name", s); + papiAttributeListAddString(&attributes, + PAPI_ATTR_APPEND, "job-file-names", s); - /* seperate the name and size */ - *q = '\0'; - q++; - - size = atoi(q); - - papiAttributeListAddString(&attributes, - PAPI_ATTR_APPEND, "job-name", p); - papiAttributeListAddString(&attributes, - PAPI_ATTR_APPEND, "job-file-names", p); - papiAttributeListAddInteger(&attributes, + if ((s = regvalue(matches[3], line)) == NULL) + s = "0"; + size = atoi(s); + papiAttributeListAddInteger(&attributes, PAPI_ATTR_APPEND, "job-file-sizes", size); - octets += (size * copies); - } + octets += (size * copies); } papiAttributeListAddInteger(&attributes, PAPI_ATTR_APPEND, @@ -131,19 +174,6 @@ (*job)->attributes = attributes; } -static void -parse_lpd_job_entries(service_t *svc, int fd) -{ - job_t *job = NULL; - - do { - parse_lpd_job_entry(svc, fd, &job); - list_append(&svc->cache->jobs, job); - } while (job != NULL); - -} - - void parse_lpd_query(service_t *svc, int fd) { @@ -151,26 +181,45 @@ cache_t *cache = NULL; int state = 0x03; /* idle */ char line[128]; - char buf[1024]; + char status[1024]; + char *s; - /* get the status line */ - if (fdgets(line, sizeof (line), fd) == NULL) - return; /* this should not happen. */ - papiAttributeListAddString(&attributes, PAPI_ATTR_APPEND, "printer-name", queue_name_from_uri(svc->uri)); - if (uri_to_string(svc->uri, buf, sizeof (buf)) == 0) + if (uri_to_string(svc->uri, status, sizeof (status)) == 0) papiAttributeListAddString(&attributes, PAPI_ATTR_APPEND, - "printer-uri-supported", buf); + "printer-uri-supported", status); + /* + * on most systems, status is a single line, but some appear to + * return multi-line status messages. To get the "best" possible + * printer-state-reason, we accumulate the text until we hit the + * first print job entry. + * + * Print job entries start with: + * user: rank [job number ...] + */ + (void) regcomp(&job_re, job_expr, REG_EXTENDED|REG_ICASE); + + status[0] = '\0'; + while ((fdgets(line, sizeof (line), fd) != NULL) && + (regexec(&job_re, line, (size_t)0, NULL, 0) == REG_NOMATCH)) { + strlcat(status, line, sizeof (status)); + } + /* chop off trailing whitespace */ + s = status + strlen(status) - 1; + while ((s > status) && (isspace(*s) != 0)) + *s-- = '\0'; + papiAttributeListAddString(&attributes, PAPI_ATTR_REPLACE, - "printer-state-reasons", line); + "printer-state-reasons", status); - if (strstr(line, "ready and printing") != NULL) + (void) regcomp(&proc_re, proc_expr, REG_EXTENDED|REG_ICASE); + (void) regcomp(&idle_re, idle_expr, REG_EXTENDED|REG_ICASE); + if (regexec(&proc_re, status, (size_t)0, NULL, 0) == 0) state = 0x04; /* processing */ - else if ((strstr(line, "no entries") != NULL) || - (strstr(line, "is ready") != NULL)) + else if (regexec(&idle_re, status, (size_t)0, NULL, 0) == 0) state = 0x03; /* idle */ else state = 0x05; /* stopped */ @@ -188,9 +237,16 @@ cache->printer->attributes = attributes; svc->cache = cache; - if (fdgets(line, sizeof (line), fd) != NULL) { - /* get the jobs */ - parse_lpd_job_entries(svc, fd); + (void) regcomp(&doc1_re, doc1_expr, REG_EXTENDED|REG_ICASE); + (void) regcomp(&doc2_re, doc2_expr, REG_EXTENDED|REG_ICASE); + /* process job related entries */ + while (line[0] != '\0') { + job_t *job = NULL; + + parse_lpd_job(svc, &job, fd, line, sizeof (line)); + if (job == NULL) + break; + list_append(&cache->jobs, job); } time(&cache->timestamp); @@ -207,7 +263,7 @@ if (svc == NULL) return; - if ((fd = lpd_open(svc, 'q', NULL, 3)) < 0) + if ((fd = lpd_open(svc, 'q', NULL, 15)) < 0) return; parse_lpd_query(svc, fd); Modified: trunk/papi/source/libpapi-lpd/printer.c =================================================================== --- trunk/papi/source/libpapi-lpd/printer.c 2007-02-03 02:35:26 UTC (rev 183) +++ trunk/papi/source/libpapi-lpd/printer.c 2007-07-31 17:04:09 UTC (rev 184) @@ -81,6 +81,12 @@ PAPI_ATTR_APPEND, "printer-uri-supported", buf); } + /* Set printer accepting: mimic prepapi behavior */ + if ((p = *printer) != NULL) + papiAttributeListAddBoolean(&(p->attributes), + PAPI_ATTR_REPLACE, + "printer-is-accepting-jobs", PAPI_TRUE); + } return (status); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |