delete printer driver fix from APP_HEAD
[Samba.git] / source / printing / printing.c
blobcb689c05d6636bec15f554424db2c55f67009fe9
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
4 printing backend routines
5 Copyright (C) Andrew Tridgell 1992-2000
6 Copyright (C) Jeremy Allison 2002
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "printing.h"
25 /* Current printer interface */
26 static struct printif *current_printif = &generic_printif;
28 /*
29 the printing backend revolves around a tdb database that stores the
30 SMB view of the print queue
32 The key for this database is a jobid - a internally generated number that
33 uniquely identifies a print job
35 reading the print queue involves two steps:
36 - possibly running lpq and updating the internal database from that
37 - reading entries from the database
39 jobids are assigned when a job starts spooling.
42 /***************************************************************************
43 Nightmare. LANMAN jobid's are 16 bit numbers..... We must map them to 32
44 bit RPC jobids.... JRA.
45 ***************************************************************************/
47 static TDB_CONTEXT *rap_tdb;
48 static uint16 next_rap_jobid;
50 uint16 pjobid_to_rap(int snum, uint32 jobid)
52 uint16 rap_jobid;
53 TDB_DATA data, key;
54 char jinfo[8];
56 if (!rap_tdb) {
57 /* Create the in-memory tdb. */
58 rap_tdb = tdb_open_log(NULL, 0, TDB_INTERNAL, (O_RDWR|O_CREAT), 0644);
59 if (!rap_tdb)
60 return 0;
63 SIVAL(&jinfo,0,(int32)snum);
64 SIVAL(&jinfo,4,jobid);
66 key.dptr = (char *)&jinfo;
67 key.dsize = sizeof(jinfo);
68 data = tdb_fetch(rap_tdb, key);
69 if (data.dptr && data.dsize == sizeof(uint16)) {
70 memcpy(&rap_jobid, data.dptr, sizeof(uint16));
71 SAFE_FREE(data.dptr);
72 return rap_jobid;
74 /* Not found - create and store mapping. */
75 rap_jobid = ++next_rap_jobid;
76 if (rap_jobid == 0)
77 rap_jobid = ++next_rap_jobid;
78 data.dptr = (char *)&rap_jobid;
79 data.dsize = sizeof(rap_jobid);
80 tdb_store(rap_tdb, key, data, TDB_REPLACE);
81 tdb_store(rap_tdb, data, key, TDB_REPLACE);
82 return rap_jobid;
85 BOOL rap_to_pjobid(uint16 rap_jobid, int *psnum, uint32 *pjobid)
87 TDB_DATA data, key;
88 char jinfo[8];
90 if (!rap_tdb)
91 return False;
93 key.dptr = (char *)&rap_jobid;
94 key.dsize = sizeof(rap_jobid);
95 data = tdb_fetch(rap_tdb, key);
96 if (data.dptr && data.dsize == sizeof(jinfo)) {
97 *psnum = IVAL(&jinfo,0);
98 *pjobid = IVAL(&jinfo,4);
99 SAFE_FREE(data.dptr);
100 return True;
102 return False;
105 static void rap_jobid_delete(int snum, uint32 jobid)
107 TDB_DATA key, data;
108 uint16 rap_jobid;
109 char jinfo[8];
111 if (!rap_tdb)
112 return;
114 SIVAL(&jinfo,0,(int32)snum);
115 SIVAL(&jinfo,4,jobid);
117 key.dptr = (char *)&jinfo;
118 key.dsize = sizeof(jinfo);
119 data = tdb_fetch(rap_tdb, key);
120 if (!data.dptr || (data.dsize != sizeof(uint16)))
121 return;
123 memcpy(&rap_jobid, data.dptr, sizeof(uint16));
124 SAFE_FREE(data.dptr);
125 data.dptr = (char *)&rap_jobid;
126 data.dsize = sizeof(rap_jobid);
127 tdb_delete(rap_tdb, key);
128 tdb_delete(rap_tdb, data);
131 static pid_t local_pid;
133 static int get_queue_status(int, print_status_struct *);
135 #define MAX_PRINT_DBS_OPEN 1
137 struct tdb_print_db {
138 struct tdb_print_db *next, *prev;
139 TDB_CONTEXT *tdb;
140 fstring printer_name;
143 static struct tdb_print_db *print_db_head;
145 /****************************************************************************
146 Function to find or create the printer specific job tdb given a printername.
147 Limits the number of tdb's open to MAX_PRINT_DBS_OPEN.
148 ****************************************************************************/
150 static struct tdb_print_db *get_print_db_byname(const char *printername)
152 struct tdb_print_db *p, *last_entry;
153 int num_open = 0;
154 pstring printdb_path;
156 for (p = print_db_head, last_entry = print_db_head; p; p = p->next) {
157 if (p->tdb && strequal(p->printer_name, printername)) {
158 DLIST_PROMOTE(print_db_head, p);
159 return p;
161 num_open++;
162 last_entry = p;
164 /* Not found. */
165 if (num_open >= MAX_PRINT_DBS_OPEN) {
166 /* Recycle the last entry. */
167 DLIST_PROMOTE(print_db_head, last_entry);
168 if (print_db_head->tdb) {
169 if (tdb_close(print_db_head->tdb)) {
170 DEBUG(0,("get_print_db: Failed to close tdb for printer %s\n",
171 print_db_head->printer_name ));
172 return NULL;
175 p = print_db_head;
176 ZERO_STRUCTP(p);
177 } else {
178 /* Create one. */
179 p = (struct tdb_print_db *)malloc(sizeof(struct tdb_print_db));
180 if (!p) {
181 DEBUG(0,("get_print_db: malloc fail !\n"));
182 return NULL;
184 ZERO_STRUCTP(p);
185 DLIST_ADD(print_db_head, p);
188 pstrcpy(printdb_path, lock_path("printing/"));
189 pstrcat(printdb_path, printername);
190 pstrcat(printdb_path, ".tdb");
192 become_root();
193 p->tdb = tdb_open_log(printdb_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
194 unbecome_root();
196 if (!p->tdb) {
197 DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
198 printdb_path ));
199 DLIST_REMOVE(print_db_head, p);
200 SAFE_FREE(p);
201 return NULL;
203 fstrcpy(p->printer_name, printername);
204 return p;
207 /****************************************************************************
208 Initialise the printing backend. Called once at startup.
209 Does not survive a fork
210 ****************************************************************************/
212 BOOL print_backend_init(void)
214 char *sversion = "INFO/version";
215 pstring printing_path;
216 int services = lp_numservices();
217 int snum;
219 if (local_pid == sys_getpid())
220 return True;
222 unlink(lock_path("printing.tdb"));
223 pstrcpy(printing_path,lock_path("printing"));
224 mkdir(printing_path,0755);
226 local_pid = sys_getpid();
228 /* handle a Samba upgrade */
230 for (snum = 0; snum < services; snum++) {
231 struct tdb_print_db *pdb;
232 if (!lp_print_ok(snum))
233 continue;
235 pdb = get_print_db_byname(lp_const_servicename(snum));
236 if (!pdb)
237 continue;
238 tdb_lock_bystring(pdb->tdb, sversion);
239 if (tdb_fetch_int32(pdb->tdb, sversion) != PRINT_DATABASE_VERSION) {
240 tdb_traverse(pdb->tdb, tdb_traverse_delete_fn, NULL);
241 tdb_store_int32(pdb->tdb, sversion, PRINT_DATABASE_VERSION);
243 tdb_unlock_bystring(pdb->tdb, sversion);
246 /* select the appropriate printing interface... */
247 #ifdef HAVE_CUPS
248 if (strcmp(lp_printcapname(), "cups") == 0)
249 current_printif = &cups_printif;
250 #endif /* HAVE_CUPS */
252 /* do NT print initialization... */
253 return nt_printing_init();
256 /****************************************************************************
257 Shut down printing backend. Called once at shutdown to close the tdb.
258 ****************************************************************************/
260 void printing_end(void)
262 struct tdb_print_db *p;
264 for (p = print_db_head; p; ) {
265 struct tdb_print_db *next_p = p->next;
266 if (p->tdb)
267 tdb_close(p->tdb);
268 DLIST_REMOVE(print_db_head, p);
269 SAFE_FREE(p);
270 p = next_p;
274 /****************************************************************************
275 Useful function to generate a tdb key.
276 ****************************************************************************/
278 static TDB_DATA print_key(uint32 jobid)
280 static uint32 j;
281 TDB_DATA ret;
283 j = jobid;
284 ret.dptr = (void *)&j;
285 ret.dsize = sizeof(j);
286 return ret;
289 /****************************************************************************
290 Useful function to find a print job in the database.
291 ****************************************************************************/
293 static struct printjob *print_job_find(int snum, uint32 jobid)
295 static struct printjob pjob;
296 TDB_DATA ret;
297 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
299 if (!pdb)
300 return NULL;
302 ret = tdb_fetch(pdb->tdb, print_key(jobid));
303 if (!ret.dptr || ret.dsize != sizeof(pjob))
304 return NULL;
306 memcpy(&pjob, ret.dptr, sizeof(pjob));
307 SAFE_FREE(ret.dptr);
308 return &pjob;
311 /* Convert a unix jobid to a smb jobid */
313 static uint32 sysjob_to_jobid_value;
315 static int unixjob_traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key,
316 TDB_DATA data, void *state)
318 struct printjob *pjob = (struct printjob *)data.dptr;
319 int *sysjob = (int *)state;
321 if (key.dsize != sizeof(uint32))
322 return 0;
324 if (*sysjob == pjob->sysjob) {
325 uint32 *jobid = (uint32 *)key.dptr;
327 sysjob_to_jobid_value = *jobid;
328 return 1;
331 return 0;
334 /****************************************************************************
335 This is a *horribly expensive call as we have to iterate through all the
336 current printer tdb's. Don't do this often ! JRA.
337 ****************************************************************************/
339 uint32 sysjob_to_jobid(int unix_jobid)
341 int services = lp_numservices();
342 int snum;
344 sysjob_to_jobid_value = (uint32)-1;
346 for (snum = 0; snum < services; snum++) {
347 struct tdb_print_db *pdb;
348 if (!lp_print_ok(snum))
349 continue;
350 pdb = get_print_db_byname(lp_const_servicename(snum));
351 if (pdb)
352 tdb_traverse(pdb->tdb, unixjob_traverse_fn, &unix_jobid);
353 if (sysjob_to_jobid_value != (uint32)-1)
354 return sysjob_to_jobid_value;
356 return (uint32)-1;
359 /****************************************************************************
360 Send notifications based on what has changed after a pjob_store.
361 ****************************************************************************/
363 static struct {
364 uint32 lpq_status;
365 uint32 spoolss_status;
366 } lpq_to_spoolss_status_map[] = {
367 { LPQ_QUEUED, JOB_STATUS_QUEUED },
368 { LPQ_PAUSED, JOB_STATUS_PAUSED },
369 { LPQ_SPOOLING, JOB_STATUS_SPOOLING },
370 { LPQ_PRINTING, JOB_STATUS_PRINTING },
371 { LPQ_DELETING, JOB_STATUS_DELETING },
372 { LPQ_OFFLINE, JOB_STATUS_OFFLINE },
373 { LPQ_PAPEROUT, JOB_STATUS_PAPEROUT },
374 { LPQ_PRINTED, JOB_STATUS_PRINTED },
375 { LPQ_DELETED, JOB_STATUS_DELETED },
376 { LPQ_BLOCKED, JOB_STATUS_BLOCKED },
377 { LPQ_USER_INTERVENTION, JOB_STATUS_USER_INTERVENTION },
378 { -1, 0 }
381 /* Convert a lpq status value stored in printing.tdb into the
382 appropriate win32 API constant. */
384 static uint32 map_to_spoolss_status(uint32 lpq_status)
386 int i = 0;
388 while (lpq_to_spoolss_status_map[i].lpq_status != -1) {
389 if (lpq_to_spoolss_status_map[i].lpq_status == lpq_status)
390 return lpq_to_spoolss_status_map[i].spoolss_status;
391 i++;
394 return 0;
397 static void pjob_store_notify(int snum, uint32 jobid, struct printjob *old_data,
398 struct printjob *new_data)
400 BOOL new_job = False;
402 if (!old_data)
403 new_job = True;
405 /* Notify the job name first */
407 if (new_job || !strequal(old_data->jobname, new_data->jobname))
408 notify_job_name(snum, jobid, new_data->jobname);
410 /* Job attributes that can't be changed. We only send
411 notification for these on a new job. */
413 if (new_job) {
414 notify_job_submitted(snum, jobid, new_data->starttime);
415 notify_job_username(snum, jobid, new_data->user);
418 /* Job attributes of a new job or attributes that can be
419 modified. */
421 if (new_job || old_data->status != new_data->status)
422 notify_job_status(snum, jobid, map_to_spoolss_status(new_data->status));
424 if (new_job || old_data->size != new_data->size)
425 notify_job_total_bytes(snum, jobid, new_data->size);
427 if (new_job || old_data->page_count != new_data->page_count)
428 notify_job_total_pages(snum, jobid, new_data->page_count);
431 /****************************************************************************
432 Store a job structure back to the database.
433 ****************************************************************************/
435 static BOOL pjob_store(int snum, uint32 jobid, struct printjob *pjob)
437 TDB_DATA old_data, new_data;
438 BOOL ret;
439 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
441 if (!pdb)
442 return False;
444 /* Get old data */
446 old_data = tdb_fetch(pdb->tdb, print_key(jobid));
448 /* Store new data */
450 new_data.dptr = (void *)pjob;
451 new_data.dsize = sizeof(*pjob);
452 ret = (tdb_store(pdb->tdb, print_key(jobid), new_data, TDB_REPLACE) == 0);
454 /* Send notify updates for what has changed */
456 if (ret && (old_data.dsize == 0 || old_data.dsize == sizeof(*pjob))) {
457 pjob_store_notify(
458 snum, jobid, (struct printjob *)old_data.dptr,
459 (struct printjob *)new_data.dptr);
460 free(old_data.dptr);
463 return ret;
466 /****************************************************************************
467 Remove a job structure from the database.
468 ****************************************************************************/
470 static void pjob_delete(int snum, uint32 jobid)
472 struct printjob *pjob = print_job_find(snum, jobid);
473 uint32 job_status = 0;
474 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
476 if (!pdb)
477 return;
479 if (!pjob) {
480 DEBUG(5, ("pjob_delete(): we were asked to delete nonexistent job %u\n",
481 (unsigned int)jobid));
482 return;
485 /* Send a notification that a job has been deleted */
487 job_status = map_to_spoolss_status(pjob->status);
489 /* We must cycle through JOB_STATUS_DELETING and
490 JOB_STATUS_DELETED for the port monitor to delete the job
491 properly. */
493 job_status |= JOB_STATUS_DELETING;
494 notify_job_status(snum, jobid, job_status);
496 job_status |= JOB_STATUS_DELETED;
497 notify_job_status(snum, jobid, job_status);
499 /* Remove from printing.tdb */
501 tdb_delete(pdb->tdb, print_key(jobid));
502 rap_jobid_delete(snum, jobid);
505 /****************************************************************************
506 Parse a file name from the system spooler to generate a jobid.
507 ****************************************************************************/
509 static uint32 print_parse_jobid(char *fname)
511 int jobid;
513 if (strncmp(fname,PRINT_SPOOL_PREFIX,strlen(PRINT_SPOOL_PREFIX)) != 0)
514 return (uint32)-1;
515 fname += strlen(PRINT_SPOOL_PREFIX);
517 jobid = atoi(fname);
518 if (jobid <= 0)
519 return (uint32)-1;
521 return (uint32)jobid;
524 /****************************************************************************
525 List a unix job in the print database.
526 ****************************************************************************/
528 static void print_unix_job(int snum, print_queue_struct *q)
530 uint32 jobid = q->job + UNIX_JOB_START;
531 struct printjob pj, *old_pj;
533 /* Preserve the timestamp on an existing unix print job */
535 old_pj = print_job_find(snum, jobid);
537 ZERO_STRUCT(pj);
539 pj.pid = (pid_t)-1;
540 pj.sysjob = q->job;
541 pj.fd = -1;
542 pj.starttime = old_pj ? old_pj->starttime : q->time;
543 pj.status = q->status;
544 pj.size = q->size;
545 pj.spooled = True;
546 pj.smbjob = False;
547 fstrcpy(pj.filename, "");
548 fstrcpy(pj.jobname, q->fs_file);
549 fstrcpy(pj.user, q->fs_user);
550 fstrcpy(pj.queuename, lp_const_servicename(snum));
552 pjob_store(snum, jobid, &pj);
556 struct traverse_struct {
557 print_queue_struct *queue;
558 int qcount, snum, maxcount, total_jobs;
561 /****************************************************************************
562 Utility fn to delete any jobs that are no longer active.
563 ****************************************************************************/
565 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
567 struct traverse_struct *ts = (struct traverse_struct *)state;
568 struct printjob pjob;
569 uint32 jobid;
570 int i;
572 if (data.dsize != sizeof(pjob) || key.dsize != sizeof(jobid))
573 return 0;
574 memcpy(&jobid, key.dptr, sizeof(jobid));
575 memcpy(&pjob, data.dptr, sizeof(pjob));
577 if (ts->snum != lp_servicenumber(pjob.queuename)) {
578 /* this isn't for the queue we are looking at - this cannot happen with the split tdb's. JRA */
579 return 0;
582 if (!pjob.smbjob) {
583 /* remove a unix job if it isn't in the system queue any more */
585 for (i=0;i<ts->qcount;i++) {
586 uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
587 if (jobid == u_jobid)
588 break;
590 if (i == ts->qcount)
591 pjob_delete(ts->snum, jobid);
592 else
593 ts->total_jobs++;
594 return 0;
597 /* maybe it hasn't been spooled yet */
598 if (!pjob.spooled) {
599 /* if a job is not spooled and the process doesn't
600 exist then kill it. This cleans up after smbd
601 deaths */
602 if (!process_exists(pjob.pid))
603 pjob_delete(ts->snum, jobid);
604 else
605 ts->total_jobs++;
606 return 0;
609 for (i=0;i<ts->qcount;i++) {
610 uint32 curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
611 if (jobid == curr_jobid)
612 break;
615 /* The job isn't in the system queue - we have to assume it has
616 completed, so delete the database entry. */
618 if (i == ts->qcount) {
619 time_t cur_t = time(NULL);
621 /* A race can occur between the time a job is spooled and
622 when it appears in the lpq output. This happens when
623 the job is added to printing.tdb when another smbd
624 running print_queue_update() has completed a lpq and
625 is currently traversing the printing tdb and deleting jobs.
626 A workaround is to not delete the job if it has been
627 submitted less than lp_lpqcachetime() seconds ago. */
629 if ((cur_t - pjob.starttime) > lp_lpqcachetime())
630 pjob_delete(ts->snum, jobid);
631 else
632 ts->total_jobs++;
634 else
635 ts->total_jobs++;
637 return 0;
640 /****************************************************************************
641 Check if the print queue has been updated recently enough.
642 ****************************************************************************/
644 static void print_cache_flush(int snum)
646 fstring key;
647 const char *printername = lp_const_servicename(snum);
648 struct tdb_print_db *pdb = get_print_db_byname(printername);
650 if (!pdb)
651 return;
652 slprintf(key, sizeof(key)-1, "CACHE/%s", printername);
653 tdb_store_int32(pdb->tdb, key, -1);
656 /****************************************************************************
657 Check if someone already thinks they are doing the update.
658 ****************************************************************************/
660 static pid_t get_updating_pid(fstring printer_name)
662 fstring keystr;
663 TDB_DATA data, key;
664 pid_t updating_pid;
665 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
667 if (!pdb)
668 return (pid_t)-1;
669 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
670 key.dptr = keystr;
671 key.dsize = strlen(keystr);
673 data = tdb_fetch(pdb->tdb, key);
674 if (!data.dptr || data.dsize != sizeof(pid_t))
675 return (pid_t)-1;
677 memcpy(&updating_pid, data.dptr, sizeof(pid_t));
678 SAFE_FREE(data.dptr);
680 if (process_exists(updating_pid))
681 return updating_pid;
683 return (pid_t)-1;
686 /****************************************************************************
687 Set the fact that we're doing the update, or have finished doing the update
688 in the tdb.
689 ****************************************************************************/
691 static void set_updating_pid(const fstring printer_name, BOOL delete)
693 fstring keystr;
694 TDB_DATA key;
695 TDB_DATA data;
696 pid_t updating_pid = sys_getpid();
697 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
699 if (!pdb)
700 return;
702 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
703 key.dptr = keystr;
704 key.dsize = strlen(keystr);
706 if (delete) {
707 tdb_delete(pdb->tdb, key);
708 return;
711 data.dptr = (void *)&updating_pid;
712 data.dsize = sizeof(pid_t);
714 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
717 /****************************************************************************
718 Update the internal database from the system print queue for a queue.
719 ****************************************************************************/
721 static void print_queue_update(int snum)
723 int i, qcount;
724 print_queue_struct *queue = NULL;
725 print_status_struct status;
726 print_status_struct old_status;
727 struct printjob *pjob;
728 struct traverse_struct tstruct;
729 fstring keystr, printer_name, cachestr;
730 TDB_DATA data, key;
731 struct tdb_print_db *pdb;
733 fstrcpy(printer_name, lp_const_servicename(snum));
734 pdb = get_print_db_byname(printer_name);
735 if (!pdb)
736 return;
739 * Check to see if someone else is doing this update.
740 * This is essentially a mutex on the update.
743 if (get_updating_pid(printer_name) != -1)
744 return;
746 /* Lock the queue for the database update */
748 slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", printer_name);
749 tdb_lock_bystring(pdb->tdb, keystr);
752 * Ensure that no one else got in here.
753 * If the updating pid is still -1 then we are
754 * the winner.
757 if (get_updating_pid(printer_name) != -1) {
759 * Someone else is doing the update, exit.
761 tdb_unlock_bystring(pdb->tdb, keystr);
762 return;
766 * We're going to do the update ourselves.
769 /* Tell others we're doing the update. */
770 set_updating_pid(printer_name, False);
773 * Allow others to enter and notice we're doing
774 * the update.
777 tdb_unlock_bystring(pdb->tdb, keystr);
780 * Update the cache time FIRST ! Stops others even
781 * attempting to get the lock and doing this
782 * if the lpq takes a long time.
785 slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", printer_name);
786 tdb_store_int32(pdb->tdb, cachestr, (int)time(NULL));
788 /* get the current queue using the appropriate interface */
789 ZERO_STRUCT(status);
791 qcount = (*(current_printif->queue_get))(snum, &queue, &status);
793 DEBUG(3, ("%d job%s in queue for %s\n", qcount, (qcount != 1) ?
794 "s" : "", printer_name));
797 any job in the internal database that is marked as spooled
798 and doesn't exist in the system queue is considered finished
799 and removed from the database
801 any job in the system database but not in the internal database
802 is added as a unix job
804 fill in any system job numbers as we go
806 for (i=0; i<qcount; i++) {
807 uint32 jobid = print_parse_jobid(queue[i].fs_file);
809 if (jobid == (uint32)-1) {
810 /* assume its a unix print job */
811 print_unix_job(snum, &queue[i]);
812 continue;
815 /* we have an active SMB print job - update its status */
816 pjob = print_job_find(snum, jobid);
817 if (!pjob) {
818 /* err, somethings wrong. Probably smbd was restarted
819 with jobs in the queue. All we can do is treat them
820 like unix jobs. Pity. */
821 print_unix_job(snum, &queue[i]);
822 continue;
825 pjob->sysjob = queue[i].job;
826 pjob->status = queue[i].status;
828 pjob_store(snum, jobid, pjob);
831 /* now delete any queued entries that don't appear in the
832 system queue */
833 tstruct.queue = queue;
834 tstruct.qcount = qcount;
835 tstruct.snum = snum;
836 tstruct.total_jobs = 0;
838 tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
840 SAFE_FREE(tstruct.queue);
842 tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
844 if( qcount != get_queue_status(snum, &old_status))
845 DEBUG(10,("print_queue_update: queue status change %d jobs -> %d jobs for printer %s\n",
846 old_status.qcount, qcount, printer_name ));
848 /* store the new queue status structure */
849 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printer_name);
850 key.dptr = keystr;
851 key.dsize = strlen(keystr);
853 status.qcount = qcount;
854 data.dptr = (void *)&status;
855 data.dsize = sizeof(status);
856 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
859 * Update the cache time again. We want to do this call
860 * as little as possible...
863 slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", printer_name);
864 tdb_store_int32(pdb->tdb, keystr, (int32)time(NULL));
866 /* Delete our pid from the db. */
867 set_updating_pid(printer_name, True);
870 /****************************************************************************
871 Check if a jobid is valid. It is valid if it exists in the database.
872 ****************************************************************************/
874 BOOL print_job_exists(int snum, uint32 jobid)
876 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
877 if (!pdb)
878 return False;
879 return tdb_exists(pdb->tdb, print_key(jobid));
882 /****************************************************************************
883 Give the fd used for a jobid.
884 ****************************************************************************/
886 int print_job_fd(int snum, uint32 jobid)
888 struct printjob *pjob = print_job_find(snum, jobid);
889 if (!pjob)
890 return -1;
891 /* don't allow another process to get this info - it is meaningless */
892 if (pjob->pid != local_pid)
893 return -1;
894 return pjob->fd;
897 /****************************************************************************
898 Give the filename used for a jobid.
899 Only valid for the process doing the spooling and when the job
900 has not been spooled.
901 ****************************************************************************/
903 char *print_job_fname(int snum, uint32 jobid)
905 struct printjob *pjob = print_job_find(snum, jobid);
906 if (!pjob || pjob->spooled || pjob->pid != local_pid)
907 return NULL;
908 return pjob->filename;
911 /****************************************************************************
912 Set the place in the queue for a job.
913 ****************************************************************************/
915 BOOL print_job_set_place(int snum, uint32 jobid, int place)
917 DEBUG(2,("print_job_set_place not implemented yet\n"));
918 return False;
921 /****************************************************************************
922 Set the name of a job. Only possible for owner.
923 ****************************************************************************/
925 BOOL print_job_set_name(int snum, uint32 jobid, char *name)
927 struct printjob *pjob = print_job_find(snum, jobid);
928 if (!pjob || pjob->pid != local_pid)
929 return False;
931 fstrcpy(pjob->jobname, name);
932 return pjob_store(snum, jobid, pjob);
935 /****************************************************************************
936 Delete a print job - don't update queue.
937 ****************************************************************************/
939 static BOOL print_job_delete1(int snum, uint32 jobid)
941 struct printjob *pjob = print_job_find(snum, jobid);
942 int result = 0;
944 if (!pjob)
945 return False;
948 * If already deleting just return.
951 if (pjob->status == LPQ_DELETING)
952 return True;
954 /* Hrm - we need to be able to cope with deleting a job before it
955 has reached the spooler. */
957 if (pjob->sysjob == -1) {
958 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid));
961 /* Set the tdb entry to be deleting. */
963 pjob->status = LPQ_DELETING;
964 pjob_store(snum, jobid, pjob);
966 if (pjob->spooled && pjob->sysjob != -1)
967 result = (*(current_printif->job_delete))(snum, pjob);
969 /* Delete the tdb entry if the delete suceeded or the job hasn't
970 been spooled. */
972 if (result == 0)
973 pjob_delete(snum, jobid);
975 return (result == 0);
978 /****************************************************************************
979 Return true if the current user owns the print job.
980 ****************************************************************************/
982 static BOOL is_owner(struct current_user *user, int snum, uint32 jobid)
984 struct printjob *pjob = print_job_find(snum, jobid);
985 user_struct *vuser;
987 if (!pjob || !user)
988 return False;
990 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
991 return strequal(pjob->user, vuser->user.smb_name);
992 } else {
993 return strequal(pjob->user, uidtoname(user->uid));
997 /****************************************************************************
998 Delete a print job.
999 ****************************************************************************/
1001 BOOL print_job_delete(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1003 BOOL owner;
1005 owner = is_owner(user, snum, jobid);
1007 /* Check access against security descriptor or whether the user
1008 owns their job. */
1010 if (!owner &&
1011 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1012 DEBUG(3, ("delete denied by security descriptor\n"));
1013 *errcode = WERR_ACCESS_DENIED;
1014 return False;
1017 if (!print_job_delete1(snum, jobid))
1018 return False;
1020 /* force update the database and say the delete failed if the
1021 job still exists */
1023 print_queue_update(snum);
1025 return !print_job_exists(snum, jobid);
1028 /****************************************************************************
1029 Pause a job.
1030 ****************************************************************************/
1032 BOOL print_job_pause(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1034 struct printjob *pjob = print_job_find(snum, jobid);
1035 int ret = -1;
1037 if (!pjob || !user)
1038 return False;
1040 if (!pjob->spooled || pjob->sysjob == -1)
1041 return False;
1043 if (!is_owner(user, snum, jobid) &&
1044 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1045 DEBUG(3, ("pause denied by security descriptor\n"));
1046 *errcode = WERR_ACCESS_DENIED;
1047 return False;
1050 /* need to pause the spooled entry */
1051 ret = (*(current_printif->job_pause))(snum, pjob);
1053 if (ret != 0) {
1054 *errcode = WERR_INVALID_PARAM;
1055 return False;
1058 /* force update the database */
1059 print_cache_flush(snum);
1061 /* Send a printer notify message */
1063 notify_job_status(snum, jobid, JOB_STATUS_PAUSED);
1065 /* how do we tell if this succeeded? */
1067 return True;
1070 /****************************************************************************
1071 Resume a job.
1072 ****************************************************************************/
1074 BOOL print_job_resume(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1076 struct printjob *pjob = print_job_find(snum, jobid);
1077 int ret;
1079 if (!pjob || !user)
1080 return False;
1082 if (!pjob->spooled || pjob->sysjob == -1)
1083 return False;
1085 if (!is_owner(user, snum, jobid) &&
1086 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1087 DEBUG(3, ("resume denied by security descriptor\n"));
1088 *errcode = WERR_ACCESS_DENIED;
1089 return False;
1092 ret = (*(current_printif->job_resume))(snum, pjob);
1094 if (ret != 0) {
1095 *errcode = WERR_INVALID_PARAM;
1096 return False;
1099 /* force update the database */
1100 print_cache_flush(snum);
1102 /* Send a printer notify message */
1104 notify_job_status(snum, jobid, JOB_STATUS_QUEUED);
1106 return True;
1109 /****************************************************************************
1110 Write to a print file.
1111 ****************************************************************************/
1113 int print_job_write(int snum, uint32 jobid, const char *buf, int size)
1115 int return_code;
1116 struct printjob *pjob = print_job_find(snum, jobid);
1118 if (!pjob)
1119 return -1;
1120 /* don't allow another process to get this info - it is meaningless */
1121 if (pjob->pid != local_pid)
1122 return -1;
1124 return_code = write(pjob->fd, buf, size);
1125 if (return_code>0) {
1126 pjob->size += size;
1127 pjob_store(snum, jobid, pjob);
1129 return return_code;
1132 /****************************************************************************
1133 Check if the print queue has been updated recently enough.
1134 ****************************************************************************/
1136 static BOOL print_cache_expired(int snum)
1138 fstring key;
1139 time_t last_qscan_time, time_now = time(NULL);
1140 const char *printername = lp_const_servicename(snum);
1141 struct tdb_print_db *pdb = get_print_db_byname(printername);
1143 if (!pdb)
1144 return False;
1146 slprintf(key, sizeof(key), "CACHE/%s", printername);
1147 last_qscan_time = (time_t)tdb_fetch_int32(pdb->tdb, key);
1150 * Invalidate the queue for 3 reasons.
1151 * (1). last queue scan time == -1.
1152 * (2). Current time - last queue scan time > allowed cache time.
1153 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1154 * This last test picks up machines for which the clock has been moved
1155 * forward, an lpq scan done and then the clock moved back. Otherwise
1156 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1159 if (last_qscan_time == ((time_t)-1) || (time_now - last_qscan_time) >= lp_lpqcachetime() ||
1160 last_qscan_time > (time_now + MAX_CACHE_VALID_TIME)) {
1161 DEBUG(3, ("print cache expired for queue %s \
1162 (last_qscan_time = %d, time now = %d, qcachetime = %d)\n", printername,
1163 (int)last_qscan_time, (int)time_now, (int)lp_lpqcachetime() ));
1164 return True;
1166 return False;
1169 /****************************************************************************
1170 Get the queue status - do not update if db is out of date.
1171 ****************************************************************************/
1173 static int get_queue_status(int snum, print_status_struct *status)
1175 fstring keystr;
1176 TDB_DATA data, key;
1177 const char *printername = lp_const_servicename(snum);
1178 struct tdb_print_db *pdb = get_print_db_byname(printername);
1179 if (!pdb)
1180 return 0;
1182 ZERO_STRUCTP(status);
1183 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
1184 key.dptr = keystr;
1185 key.dsize = strlen(keystr);
1186 data = tdb_fetch(pdb->tdb, key);
1187 if (data.dptr) {
1188 if (data.dsize == sizeof(print_status_struct)) {
1189 memcpy(status, data.dptr, sizeof(print_status_struct));
1191 SAFE_FREE(data.dptr);
1193 return status->qcount;
1196 /****************************************************************************
1197 Determine the number of jobs in a queue.
1198 ****************************************************************************/
1200 int print_queue_length(int snum, print_status_struct *pstatus)
1202 print_status_struct status;
1203 int len;
1205 /* make sure the database is up to date */
1206 if (print_cache_expired(snum))
1207 print_queue_update(snum);
1209 /* also fetch the queue status */
1210 memset(&status, 0, sizeof(status));
1211 len = get_queue_status(snum, &status);
1212 if (pstatus)
1213 *pstatus = status;
1214 return len;
1217 /****************************************************************************
1218 Determine the number of jobs in all queues. This is very expensive. Don't
1219 call ! JRA.
1220 ****************************************************************************/
1222 static int get_total_jobs(void)
1224 int total_jobs = 0;
1225 int snum;
1226 int services = lp_numservices();
1228 for (snum = 0; snum < services; snum++) {
1229 struct tdb_print_db *pdb;
1230 int jobs;
1232 if (!lp_print_ok(snum))
1233 continue;
1235 pdb = get_print_db_byname(lp_const_servicename(snum));
1236 if (!pdb)
1237 continue;
1239 /* make sure the database is up to date */
1240 if (print_cache_expired(snum))
1241 print_queue_update(snum);
1243 jobs = tdb_fetch_int32(pdb->tdb, "INFO/total_jobs");
1244 if (jobs > 0)
1245 total_jobs += jobs;
1247 return total_jobs;
1250 /***************************************************************************
1251 Start spooling a job - return the jobid.
1252 ***************************************************************************/
1254 uint32 print_job_start(struct current_user *user, int snum, char *jobname)
1256 uint32 jobid;
1257 char *path;
1258 struct printjob pjob;
1259 int next_jobid;
1260 user_struct *vuser;
1261 int njobs = 0;
1262 const char *printername = lp_const_servicename(snum);
1263 struct tdb_print_db *pdb = get_print_db_byname(printername);
1265 errno = 0;
1267 if (!pdb)
1268 return (uint32)-1;
1270 if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
1271 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
1272 return (uint32)-1;
1275 if (!print_time_access_check(snum)) {
1276 DEBUG(3, ("print_job_start: job start denied by time check\n"));
1277 return (uint32)-1;
1280 path = lp_pathname(snum);
1282 /* see if we have sufficient disk space */
1283 if (lp_minprintspace(snum)) {
1284 SMB_BIG_UINT dspace, dsize;
1285 if (sys_fsusage(path, &dspace, &dsize) == 0 &&
1286 dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
1287 DEBUG(3, ("print_job_start: disk space check failed.\n"));
1288 errno = ENOSPC;
1289 return (uint32)-1;
1293 /* for autoloaded printers, check that the printcap entry still exists */
1294 if (lp_autoloaded(snum) && !pcap_printername_ok(lp_const_servicename(snum), NULL)) {
1295 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum) ));
1296 errno = ENOENT;
1297 return (uint32)-1;
1300 /* Insure the maximum queue size is not violated */
1301 if (lp_maxprintjobs(snum) && (njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
1302 DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per queue (%d).\n",
1303 njobs, lp_maxprintjobs(snum) ));
1304 errno = ENOSPC;
1305 return (uint32)-1;
1308 /* Insure the maximum print jobs in the system is not violated */
1309 if (lp_totalprintjobs() && get_total_jobs() > lp_totalprintjobs()) {
1310 DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per system (%d).\n",
1311 njobs, lp_totalprintjobs() ));
1312 errno = ENOSPC;
1313 return (uint32)-1;
1316 /* create the database entry */
1317 ZERO_STRUCT(pjob);
1318 pjob.pid = local_pid;
1319 pjob.sysjob = -1;
1320 pjob.fd = -1;
1321 pjob.starttime = time(NULL);
1322 pjob.status = LPQ_SPOOLING;
1323 pjob.size = 0;
1324 pjob.spooled = False;
1325 pjob.smbjob = True;
1327 fstrcpy(pjob.jobname, jobname);
1329 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1330 fstrcpy(pjob.user, vuser->user.smb_name);
1331 } else {
1332 fstrcpy(pjob.user, uidtoname(user->uid));
1335 fstrcpy(pjob.queuename, lp_const_servicename(snum));
1337 /* lock the database */
1338 tdb_lock_bystring(pdb->tdb, "INFO/nextjob");
1340 next_jobid = tdb_fetch_int32(pdb->tdb, "INFO/nextjob");
1341 if (next_jobid == -1)
1342 next_jobid = 1;
1344 for (jobid = NEXT_JOBID(next_jobid); jobid != next_jobid; jobid = NEXT_JOBID(jobid)) {
1345 if (!print_job_exists(snum, jobid))
1346 break;
1348 if (jobid == next_jobid || !pjob_store(snum, jobid, &pjob)) {
1349 DEBUG(3, ("print_job_start: either jobid (%d)==next_jobid(%d) or pjob_store failed.\n",
1350 jobid, next_jobid ));
1351 jobid = -1;
1352 goto fail;
1355 if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
1356 DEBUG(3, ("print_job_start: failed to store INFO/nextjob.\n"));
1357 jobid = -1;
1358 goto fail;
1361 /* we have a job entry - now create the spool file */
1362 slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.8u.XXXXXX",
1363 path, PRINT_SPOOL_PREFIX, (unsigned int)jobid);
1364 pjob.fd = smb_mkstemp(pjob.filename);
1366 if (pjob.fd == -1) {
1367 if (errno == EACCES) {
1368 /* Common setup error, force a report. */
1369 DEBUG(0, ("print_job_start: insufficient permissions \
1370 to open spool file %s.\n", pjob.filename));
1371 } else {
1372 /* Normal case, report at level 3 and above. */
1373 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
1374 DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
1376 goto fail;
1379 pjob_store(snum, jobid, &pjob);
1381 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1384 * If the printer is marked as postscript output a leading
1385 * file identifier to ensure the file is treated as a raw
1386 * postscript file.
1387 * This has a similar effect as CtrlD=0 in WIN.INI file.
1388 * tim@fsg.com 09/06/94
1390 if (lp_postscript(snum)) {
1391 print_job_write(snum, jobid, "%!\n",3);
1394 return jobid;
1396 fail:
1397 if (jobid != -1)
1398 pjob_delete(snum, jobid);
1400 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1402 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
1403 return -1;
1406 /****************************************************************************
1407 Update the number of pages spooled to jobid
1408 ****************************************************************************/
1410 void print_job_endpage(int snum, uint32 jobid)
1412 struct printjob *pjob = print_job_find(snum, jobid);
1413 if (!pjob)
1414 return;
1415 /* don't allow another process to get this info - it is meaningless */
1416 if (pjob->pid != local_pid)
1417 return;
1419 pjob->page_count++;
1420 pjob_store(snum, jobid, pjob);
1423 /****************************************************************************
1424 Print a file - called on closing the file. This spools the job.
1425 If normal close is false then we're tearing down the jobs - treat as an
1426 error.
1427 ****************************************************************************/
1429 BOOL print_job_end(int snum, uint32 jobid, BOOL normal_close)
1431 struct printjob *pjob = print_job_find(snum, jobid);
1432 int ret;
1433 SMB_STRUCT_STAT sbuf;
1435 if (!pjob)
1436 return False;
1438 if (pjob->spooled || pjob->pid != local_pid)
1439 return False;
1441 if (normal_close && (sys_fstat(pjob->fd, &sbuf) == 0)) {
1442 pjob->size = sbuf.st_size;
1443 close(pjob->fd);
1444 pjob->fd = -1;
1445 } else {
1448 * Not a normal close or we couldn't stat the job file,
1449 * so something has gone wrong. Cleanup.
1451 close(pjob->fd);
1452 pjob->fd = -1;
1453 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
1454 goto fail;
1457 /* Technically, this is not quite right. If the printer has a separator
1458 * page turned on, the NT spooler prints the separator page even if the
1459 * print job is 0 bytes. 010215 JRR */
1460 if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
1461 /* don't bother spooling empty files or something being deleted. */
1462 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
1463 pjob->filename, pjob->size ? "deleted" : "zero length" ));
1464 unlink(pjob->filename);
1465 pjob_delete(snum, jobid);
1466 return True;
1469 ret = (*(current_printif->job_submit))(snum, pjob);
1471 if (ret)
1472 goto fail;
1474 /* The print job has been sucessfully handed over to the back-end */
1476 pjob->spooled = True;
1477 pjob->status = LPQ_QUEUED;
1478 pjob_store(snum, jobid, pjob);
1480 /* make sure the database is up to date */
1481 if (print_cache_expired(snum))
1482 print_queue_update(snum);
1484 return True;
1486 fail:
1488 /* The print job was not succesfully started. Cleanup */
1489 /* Still need to add proper error return propagation! 010122:JRR */
1490 unlink(pjob->filename);
1491 pjob_delete(snum, jobid);
1492 return False;
1495 /****************************************************************************
1496 Utility fn to enumerate the print queue.
1497 ****************************************************************************/
1499 static int traverse_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
1501 struct traverse_struct *ts = (struct traverse_struct *)state;
1502 struct printjob pjob;
1503 int i;
1504 uint32 jobid;
1506 if (data.dsize != sizeof(pjob) || key.dsize != sizeof(int))
1507 return 0;
1508 memcpy(&jobid, key.dptr, sizeof(jobid));
1509 memcpy(&pjob, data.dptr, sizeof(pjob));
1511 /* maybe it isn't for this queue */
1512 if (ts->snum != lp_servicenumber(pjob.queuename))
1513 return 0;
1515 if (ts->qcount >= ts->maxcount)
1516 return 0;
1518 i = ts->qcount;
1520 ts->queue[i].job = jobid;
1521 ts->queue[i].size = pjob.size;
1522 ts->queue[i].page_count = pjob.page_count;
1523 ts->queue[i].status = pjob.status;
1524 ts->queue[i].priority = 1;
1525 ts->queue[i].time = pjob.starttime;
1526 fstrcpy(ts->queue[i].fs_user, pjob.user);
1527 fstrcpy(ts->queue[i].fs_file, pjob.jobname);
1529 ts->qcount++;
1531 return 0;
1534 struct traverse_count_struct {
1535 int snum, count;
1538 /****************************************************************************
1539 Utility fn to count the number of entries in the print queue.
1540 ****************************************************************************/
1542 static int traverse_count_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
1544 struct traverse_count_struct *ts = (struct traverse_count_struct *)state;
1545 struct printjob pjob;
1546 uint32 jobid;
1548 if (data.dsize != sizeof(pjob) || key.dsize != sizeof(int))
1549 return 0;
1550 memcpy(&jobid, key.dptr, sizeof(jobid));
1551 memcpy(&pjob, data.dptr, sizeof(pjob));
1553 /* maybe it isn't for this queue - this cannot happen with the tdb/printer code. JRA */
1554 if (ts->snum != lp_servicenumber(pjob.queuename))
1555 return 0;
1557 ts->count++;
1559 return 0;
1562 /****************************************************************************
1563 Sort print jobs by submittal time.
1564 ****************************************************************************/
1566 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
1568 /* Silly cases */
1570 if (!j1 && !j2)
1571 return 0;
1572 if (!j1)
1573 return -1;
1574 if (!j2)
1575 return 1;
1577 /* Sort on job start time */
1579 if (j1->time == j2->time)
1580 return 0;
1581 return (j1->time > j2->time) ? 1 : -1;
1584 /****************************************************************************
1585 Get a printer queue listing.
1586 ****************************************************************************/
1588 int print_queue_status(int snum,
1589 print_queue_struct **queue,
1590 print_status_struct *status)
1592 struct traverse_struct tstruct;
1593 struct traverse_count_struct tsc;
1594 fstring keystr;
1595 TDB_DATA data, key;
1596 const char *printername = lp_const_servicename(snum);
1597 struct tdb_print_db *pdb = get_print_db_byname(printername);
1599 *queue = NULL;
1601 if (!pdb)
1602 return 0;
1604 /* make sure the database is up to date */
1605 if (print_cache_expired(snum))
1606 print_queue_update(snum);
1609 * Fetch the queue status. We must do this first, as there may
1610 * be no jobs in the queue.
1612 ZERO_STRUCTP(status);
1613 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
1614 key.dptr = keystr;
1615 key.dsize = strlen(keystr);
1616 data = tdb_fetch(pdb->tdb, key);
1617 if (data.dptr) {
1618 if (data.dsize == sizeof(*status)) {
1619 memcpy(status, data.dptr, sizeof(*status));
1621 SAFE_FREE(data.dptr);
1625 * Now, fetch the print queue information. We first count the number
1626 * of entries, and then only retrieve the queue if necessary.
1628 tsc.count = 0;
1629 tsc.snum = snum;
1631 tdb_traverse(pdb->tdb, traverse_count_fn_queue, (void *)&tsc);
1633 if (tsc.count == 0)
1634 return 0;
1636 /* Allocate the queue size. */
1637 if ((tstruct.queue = (print_queue_struct *)
1638 malloc(sizeof(print_queue_struct)*tsc.count)) == NULL)
1639 return 0;
1642 * Fill in the queue.
1643 * We need maxcount as the queue size may have changed between
1644 * the two calls to tdb_traverse.
1646 tstruct.qcount = 0;
1647 tstruct.maxcount = tsc.count;
1648 tstruct.snum = snum;
1650 tdb_traverse(pdb->tdb, traverse_fn_queue, (void *)&tstruct);
1652 /* Sort the queue by submission time otherwise they are displayed
1653 in hash order. */
1655 qsort(tstruct.queue, tstruct.qcount, sizeof(print_queue_struct),
1656 QSORT_CAST(printjob_comp));
1658 *queue = tstruct.queue;
1659 return tstruct.qcount;
1662 /****************************************************************************
1663 Turn a queue name into a snum.
1664 ****************************************************************************/
1666 int print_queue_snum(const char *qname)
1668 int snum = lp_servicenumber(qname);
1669 if (snum == -1 || !lp_print_ok(snum))
1670 return -1;
1671 return snum;
1674 /****************************************************************************
1675 Pause a queue.
1676 ****************************************************************************/
1678 BOOL print_queue_pause(struct current_user *user, int snum, WERROR *errcode)
1680 int ret;
1682 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
1683 *errcode = WERR_ACCESS_DENIED;
1684 return False;
1687 ret = (*(current_printif->queue_pause))(snum);
1689 if (ret != 0) {
1690 *errcode = WERR_INVALID_PARAM;
1691 return False;
1694 /* force update the database */
1695 print_cache_flush(snum);
1697 /* Send a printer notify message */
1699 notify_printer_status(snum, PRINTER_STATUS_PAUSED);
1701 return True;
1704 /****************************************************************************
1705 Resume a queue.
1706 ****************************************************************************/
1708 BOOL print_queue_resume(struct current_user *user, int snum, WERROR *errcode)
1710 int ret;
1712 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
1713 *errcode = WERR_ACCESS_DENIED;
1714 return False;
1717 ret = (*(current_printif->queue_resume))(snum);
1719 if (ret != 0) {
1720 *errcode = WERR_INVALID_PARAM;
1721 return False;
1724 /* make sure the database is up to date */
1725 if (print_cache_expired(snum))
1726 print_queue_update(snum);
1728 /* Send a printer notify message */
1730 notify_printer_status(snum, PRINTER_STATUS_OK);
1732 return True;
1735 /****************************************************************************
1736 Purge a queue - implemented by deleting all jobs that we can delete.
1737 ****************************************************************************/
1739 BOOL print_queue_purge(struct current_user *user, int snum, WERROR *errcode)
1741 print_queue_struct *queue;
1742 print_status_struct status;
1743 int njobs, i;
1744 BOOL can_job_admin;
1746 /* Force and update so the count is accurate (i.e. not a cached count) */
1747 print_queue_update(snum);
1749 can_job_admin = print_access_check(user, snum, JOB_ACCESS_ADMINISTER);
1750 njobs = print_queue_status(snum, &queue, &status);
1752 for (i=0;i<njobs;i++) {
1753 BOOL owner = is_owner(user, snum, queue[i].job);
1755 if (owner || can_job_admin) {
1756 print_job_delete1(snum, queue[i].job);
1760 SAFE_FREE(queue);
1762 return True;