PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/usr/src/lib/print/libpapi-lpd/common/lpd-query.c

https://github.com/richlowe/illumos-gate
C | 507 lines | 290 code | 79 blank | 138 comment | 108 complexity | 6243560a85edabd0778ea6c5f519f628 MD5 | raw file
  1. /*
  2. * CDDL HEADER START
  3. *
  4. * The contents of this file are subject to the terms of the
  5. * Common Development and Distribution License (the "License").
  6. * You may not use this file except in compliance with the License.
  7. *
  8. * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  9. * or http://www.opensolaris.org/os/licensing.
  10. * See the License for the specific language governing permissions
  11. * and limitations under the License.
  12. *
  13. * When distributing Covered Code, include this CDDL HEADER in each
  14. * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15. * If applicable, add the following below this CDDL HEADER, with the
  16. * fields enclosed by brackets "[]" replaced with your own identifying
  17. * information: Portions Copyright [yyyy] [name of copyright owner]
  18. *
  19. * CDDL HEADER END
  20. */
  21. /*
  22. * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
  23. */
  24. /* $Id: lpd-query.c 155 2006-04-26 02:34:54Z ktou $ */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <sys/fcntl.h>
  31. #include <time.h>
  32. #include <ctype.h>
  33. #include <string.h>
  34. #include <stdarg.h>
  35. #include <regex.h>
  36. #include <papi_impl.h>
  37. /* The string is modified by this call */
  38. static char *
  39. regvalue(regmatch_t match, char *string)
  40. {
  41. char *result = NULL;
  42. if (match.rm_so != match.rm_eo) {
  43. result = string + match.rm_so;
  44. *(result + (match.rm_eo - match.rm_so)) = '\0';
  45. }
  46. return (result);
  47. }
  48. /*
  49. * Print job entries start with:
  50. * (user): (rank) [job (number) (...)]
  51. * (user) is the job-owner's user name
  52. * (rank) is the rank in queue. (active, 1st, 2nd, ...)
  53. * (number) is the job number
  54. * (...) is an optional hostname
  55. * some servers will use whitespace a little differently than is displayed
  56. * above. The regular expression below makes whitespace optional in some
  57. * places.
  58. */
  59. static char *job_expr = "^(.*[[:alnum:]]):[[:space:]]+([[:alnum:]]+)"\
  60. "[[:space:]]+[[][[:space:]]*job[[:space:]]*([[:digit:]]+)"\
  61. "[[:space:]]*(.*)]";
  62. static regex_t job_re;
  63. /*
  64. * Print job entries for remote windows printer start with:
  65. * Owner Status Jobname Job-Id Size Pages Priority
  66. * e.g:
  67. * Owner Status Jobname Job-Id Size Pages Priority
  68. * ------------------------------------------------------------
  69. * root (10.3. Waiting /etc/release 2 240 1 4
  70. *
  71. * Owner is the job-owner's user name
  72. * Status is the job-status (printing, waiting, error)
  73. * Jobname is the name of the job to be printed
  74. * Job-Id is the id of the job queued to be printed
  75. * Size is the size of the job in bytes
  76. * Pages is the number of pages of the job
  77. * Priority is the job-priority
  78. */
  79. static char *wjob_expr = "^([[:alnum:]]+)[[:space:]]*[(](.*)[)]*[[:space:]]"\
  80. "+([[:alnum:]]+)[[:space:]]+(.*)([[:alnum:]]+)(.*)[[:space:]]+"\
  81. "([[:digit:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:digit:]]+)"\
  82. "[[:space:]]+([[:digit:]]+)";
  83. static regex_t wjob_re;
  84. /*
  85. * Windows job header is in the following format
  86. * Owner Status Jobname Job-Id Size Pages Priority
  87. * --------------------------------------------------------------
  88. */
  89. static char *whjob_expr = "Owner Status Jobname Job-Id"\
  90. " Size Pages Priority";
  91. static regex_t whjob_re;
  92. static char *wline_expr = "----------";
  93. static regex_t wline_re;
  94. /*
  95. * status line(s) for "processing" printers will contain one of the following:
  96. * ready and printing
  97. * Printing
  98. * processing
  99. */
  100. static char *proc_expr = "(ready and printing|printing|processing)";
  101. static regex_t proc_re;
  102. /*
  103. * status line(s) for "idle" printers will contain one of the following:
  104. * no entries
  105. * (printer) is ready
  106. * idle
  107. */
  108. static char *idle_expr = "(no entries|is ready| idle)";
  109. static regex_t idle_re;
  110. /*
  111. * Printer state reason (For Windows remote printers)
  112. * Paused
  113. */
  114. static char *state_reason_expr = "(Paused)";
  115. static regex_t state_reason_re;
  116. /*
  117. * document line(s)
  118. * (copies) copies of (name) (size) bytes
  119. * (name) (size) bytes
  120. * document lines can be in either format above.
  121. * (copies) is the number of copies of the document to print
  122. * (name) is the name of the document: /etc/motd, ...
  123. * (size) is the number of bytes in the document data
  124. */
  125. static char *doc1_expr = "[[:space:]]+(([[:digit:]]+) copies of )"\
  126. "([^[:space:]]+)[[:space:]]*([[:digit:]]+) bytes";
  127. static char *doc2_expr = "[[:space:]]+()([^[:space:]]+)[[:space:]]*"\
  128. "([[:digit:]]+) bytes";
  129. static regex_t doc1_re;
  130. static regex_t doc2_re;
  131. /* Printer-state for Windows */
  132. static int win_state = 0x03; /* Idle */
  133. static void
  134. parse_lpd_job(service_t *svc, job_t **job, int fd, char *line, int len)
  135. {
  136. papi_attribute_t **attributes = NULL;
  137. regmatch_t matches[10];
  138. char *s;
  139. int octets = 0;
  140. int flag = 0;
  141. /*
  142. * job_re and wjob_re were compiled in the calling function
  143. * first check for solaris jobs
  144. * if there is no-match check for windows jobs
  145. */
  146. if (regexec(&job_re, line, (size_t)5, matches, 0) == REG_NOMATCH) {
  147. if (regexec(&wjob_re, line, (size_t)10, matches, 0)
  148. == REG_NOMATCH)
  149. return;
  150. else
  151. flag = 1;
  152. }
  153. if (flag == 1) {
  154. /* Windows job */
  155. /* first match is job-id */
  156. if ((s = regvalue(matches[1], line)) == NULL)
  157. s = "nobody";
  158. papiAttributeListAddString(&attributes, PAPI_ATTR_REPLACE,
  159. "job-originating-user-name", s);
  160. if ((s = regvalue(matches[4], line)) == NULL)
  161. s = "unknown";
  162. papiAttributeListAddString(&attributes, PAPI_ATTR_APPEND,
  163. "job-name", s);
  164. papiAttributeListAddString(&attributes, PAPI_ATTR_APPEND,
  165. "job-file-names", s);
  166. if ((s = regvalue(matches[7], line)) == NULL)
  167. s = "0";
  168. papiAttributeListAddInteger(&attributes, PAPI_ATTR_REPLACE,
  169. "job-id", atoi(s));
  170. if ((s = regvalue(matches[8], line)) == NULL)
  171. s = "0";
  172. octets = atoi(s);
  173. papiAttributeListAddInteger(&attributes,
  174. PAPI_ATTR_APPEND, "job-file-sizes", atoi(s));
  175. /*
  176. * Since a job has been found so the printer state is either
  177. * 'stopped' or 'processing'
  178. * By default it is "processing"
  179. */
  180. win_state = 0x04;
  181. } else {
  182. /* Solaris job */
  183. if ((s = regvalue(matches[1], line)) == NULL)
  184. s = "nobody";
  185. papiAttributeListAddString(&attributes, PAPI_ATTR_REPLACE,
  186. "job-originating-user-name", s);
  187. if ((s = regvalue(matches[2], line)) == NULL)
  188. s = "0";
  189. papiAttributeListAddInteger(&attributes, PAPI_ATTR_REPLACE,
  190. "number-of-intervening-jobs", atoi(s) - 1);
  191. if ((s = regvalue(matches[3], line)) == NULL)
  192. s = "0";
  193. papiAttributeListAddInteger(&attributes, PAPI_ATTR_REPLACE,
  194. "job-id", atoi(s));
  195. if ((s = regvalue(matches[4], line)) == NULL)
  196. s = svc->uri->host;
  197. papiAttributeListAddString(&attributes, PAPI_ATTR_REPLACE,
  198. "job-originating-host-name", s);
  199. }
  200. while ((fdgets(line, len, fd) != NULL) &&
  201. (regexec(&job_re, line, (size_t)0, NULL, 0) == REG_NOMATCH) &&
  202. (regexec(&wjob_re, line, (size_t)0, NULL, 0) == REG_NOMATCH)) {
  203. int size = 0, copies = 1;
  204. /* process copies/documents */
  205. /* doc1_re and doc2_re were compiled in the calling function */
  206. if ((regexec(&doc1_re, line, (size_t)4, matches, 0) != 0) &&
  207. (regexec(&doc2_re, line, (size_t)4, matches, 0) != 0))
  208. continue;
  209. if ((s = regvalue(matches[1], line)) == NULL)
  210. s = "1";
  211. if ((copies = atoi(s)) < 1)
  212. copies = 1;
  213. if ((s = regvalue(matches[2], line)) == NULL)
  214. s = "unknown";
  215. papiAttributeListAddString(&attributes,
  216. PAPI_ATTR_APPEND, "job-name", s);
  217. papiAttributeListAddString(&attributes,
  218. PAPI_ATTR_APPEND, "job-file-names", s);
  219. if ((s = regvalue(matches[3], line)) == NULL)
  220. s = "0";
  221. size = atoi(s);
  222. papiAttributeListAddInteger(&attributes,
  223. PAPI_ATTR_APPEND, "job-file-sizes", size);
  224. octets += (size * copies);
  225. }
  226. papiAttributeListAddInteger(&attributes, PAPI_ATTR_APPEND,
  227. "job-k-octets", octets/1024);
  228. papiAttributeListAddInteger(&attributes, PAPI_ATTR_APPEND,
  229. "job-octets", octets);
  230. papiAttributeListAddString(&attributes, PAPI_ATTR_APPEND,
  231. "printer-name", queue_name_from_uri(svc->uri));
  232. if ((*job = (job_t *)calloc(1, sizeof (**job))) != NULL)
  233. (*job)->attributes = attributes;
  234. }
  235. void
  236. parse_lpd_query(service_t *svc, int fd)
  237. {
  238. papi_attribute_t **attributes = NULL;
  239. cache_t *cache = NULL;
  240. int state = 0x03; /* idle */
  241. char line[128];
  242. char status[1024];
  243. char *s;
  244. int win_flag = 0;
  245. papiAttributeListAddString(&attributes, PAPI_ATTR_APPEND,
  246. "printer-name", queue_name_from_uri(svc->uri));
  247. if (uri_to_string(svc->uri, status, sizeof (status)) == 0)
  248. papiAttributeListAddString(&attributes, PAPI_ATTR_APPEND,
  249. "printer-uri-supported", status);
  250. /*
  251. * on most systems, status is a single line, but some appear to
  252. * return multi-line status messages. To get the "best" possible
  253. * printer-state-reason, we accumulate the text until we hit the
  254. * first print job entry.
  255. *
  256. * Print job entries start with:
  257. * user: rank [job number ...]
  258. */
  259. (void) regcomp(&job_re, job_expr, REG_EXTENDED|REG_ICASE);
  260. /*
  261. * For remote windows printers
  262. * Print job entries start with:
  263. * Owner Status Jobname Job-Id Size Pages Priority
  264. */
  265. (void) regcomp(&wjob_re, wjob_expr, REG_EXTENDED|REG_ICASE);
  266. (void) regcomp(&whjob_re, whjob_expr, REG_EXTENDED|REG_ICASE);
  267. (void) regcomp(&wline_re, wline_expr, REG_EXTENDED|REG_ICASE);
  268. status[0] = '\0';
  269. while ((fdgets(line, sizeof (line), fd) != NULL) &&
  270. (regexec(&job_re, line, (size_t)0, NULL, 0) == REG_NOMATCH) &&
  271. (regexec(&wjob_re, line, (size_t)0, NULL, 0) == REG_NOMATCH)) {
  272. /*
  273. * When windows job queue gets queried following header
  274. * should not get printed
  275. * Owner Status Jobname Job-Id Size Pages Priority
  276. * -----------------------------------------------
  277. */
  278. if ((regexec(&whjob_re, line, (size_t)0, NULL, 0)
  279. == REG_NOMATCH) &&
  280. (regexec(&wline_re, line, (size_t)0, NULL, 0)
  281. == REG_NOMATCH))
  282. strlcat(status, line, sizeof (status));
  283. }
  284. /* chop off trailing whitespace */
  285. s = status + strlen(status) - 1;
  286. while ((s > status) && (isspace(*s) != 0))
  287. *s-- = '\0';
  288. papiAttributeListAddString(&attributes, PAPI_ATTR_REPLACE,
  289. "printer-state-reasons", status);
  290. /* Check if this is for Windows remote printers */
  291. if (strstr(status, "Windows")) {
  292. /*
  293. * It is a remote windows printer
  294. * By default set the status as idle
  295. * Set the printer-state after call to "parse_lpd_job"
  296. */
  297. win_flag = 1;
  298. (void) regcomp(&state_reason_re, state_reason_expr,
  299. REG_EXTENDED|REG_ICASE);
  300. if (regexec(&state_reason_re, status, (size_t)0, NULL, 0) == 0)
  301. state = 0x05; /* stopped */
  302. } else {
  303. (void) regcomp(&proc_re, proc_expr, REG_EXTENDED|REG_ICASE);
  304. (void) regcomp(&idle_re, idle_expr, REG_EXTENDED|REG_ICASE);
  305. if (regexec(&proc_re, status, (size_t)0, NULL, 0) == 0)
  306. state = 0x04; /* processing */
  307. else if (regexec(&idle_re, status, (size_t)0, NULL, 0) == 0)
  308. state = 0x03; /* idle */
  309. else
  310. state = 0x05; /* stopped */
  311. papiAttributeListAddInteger(&attributes, PAPI_ATTR_REPLACE,
  312. "printer-state", state);
  313. }
  314. if ((cache = (cache_t *)calloc(1, sizeof (*cache))) == NULL)
  315. return;
  316. if ((cache->printer = (printer_t *)calloc(1, sizeof (*cache->printer)))
  317. == NULL)
  318. return;
  319. cache->printer->attributes = attributes;
  320. svc->cache = cache;
  321. (void) regcomp(&doc1_re, doc1_expr, REG_EXTENDED|REG_ICASE);
  322. (void) regcomp(&doc2_re, doc2_expr, REG_EXTENDED|REG_ICASE);
  323. /* process job related entries */
  324. while (line[0] != '\0') {
  325. job_t *job = NULL;
  326. parse_lpd_job(svc, &job, fd, line, sizeof (line));
  327. if (job == NULL)
  328. break;
  329. list_append(&cache->jobs, job);
  330. }
  331. /*
  332. * For remote windows printer set the printer-state
  333. * after parse_lpd_job
  334. */
  335. if (win_flag) {
  336. if (state == 0x05)
  337. win_state = state;
  338. papiAttributeListAddInteger(&attributes, PAPI_ATTR_REPLACE,
  339. "printer-state", win_state);
  340. }
  341. time(&cache->timestamp);
  342. }
  343. void
  344. cache_update(service_t *svc)
  345. {
  346. int fd;
  347. if (svc == NULL)
  348. return;
  349. if (svc->cache != NULL) { /* this should be time based */
  350. if (svc->cache->jobs == NULL) {
  351. free(svc->cache);
  352. svc->cache = NULL;
  353. } else
  354. return;
  355. }
  356. if ((fd = lpd_open(svc, 'q', NULL, 15)) < 0)
  357. return;
  358. parse_lpd_query(svc, fd);
  359. close(fd);
  360. }
  361. papi_status_t
  362. lpd_find_printer_info(service_t *svc, printer_t **printer)
  363. {
  364. papi_status_t result = PAPI_BAD_ARGUMENT;
  365. if ((svc == NULL) || (printer == NULL))
  366. return (PAPI_BAD_ARGUMENT);
  367. cache_update(svc);
  368. if (svc->cache != NULL) {
  369. *printer = svc->cache->printer;
  370. result = PAPI_OK;
  371. } else
  372. result = PAPI_NOT_FOUND;
  373. return (result);
  374. }
  375. papi_status_t
  376. lpd_find_jobs_info(service_t *svc, job_t ***jobs)
  377. {
  378. papi_status_t result = PAPI_BAD_ARGUMENT;
  379. if (svc != NULL) {
  380. cache_update(svc);
  381. if (svc->cache != NULL) {
  382. *jobs = svc->cache->jobs;
  383. result = PAPI_OK;
  384. }
  385. }
  386. /*
  387. * cache jobs is free()-ed in
  388. * libpapi-dynamic/common/printer.c -
  389. * papiPrinterListJobs() cache printer is
  390. * free()-ed by the caller of
  391. * lpd_find_printer_info Invalidate the
  392. * cache by freeing the cache.
  393. */
  394. free(svc->cache);
  395. svc->cache = NULL;
  396. return (result);
  397. }
  398. papi_status_t
  399. lpd_find_job_info(service_t *svc, int job_id, job_t **job)
  400. {
  401. papi_status_t result = PAPI_BAD_ARGUMENT;
  402. job_t **jobs;
  403. if ((lpd_find_jobs_info(svc, &jobs) == PAPI_OK) && (jobs != NULL)) {
  404. int i;
  405. *job = NULL;
  406. for (i = 0; ((*job == NULL) && (jobs[i] != NULL)); i++) {
  407. int id = -1;
  408. papiAttributeListGetInteger(jobs[i]->attributes, NULL,
  409. "job-id", &id);
  410. if (id == job_id)
  411. *job = jobs[i];
  412. }
  413. if (*job != NULL)
  414. result = PAPI_OK;
  415. }
  416. return (result);
  417. }
  418. void
  419. cache_free(cache_t *item)
  420. {
  421. if (item != NULL) {
  422. if (item->printer != NULL)
  423. papiPrinterFree((papi_printer_t *)item->printer);
  424. if (item->jobs != NULL)
  425. papiJobListFree((papi_job_t *)item->jobs);
  426. free(item);
  427. }
  428. }