Add bcast_msg_flags to connection struct. Allows sender to filter when
[Samba.git] / source / printing / printing.c
blob0dacfe820cf55594d406fdb6d0b8278cae4a5675
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 /* There can be this many printing tdb's open, plus any locked ones. */
136 #define MAX_PRINT_DBS_OPEN 1
138 struct tdb_print_db {
139 struct tdb_print_db *next, *prev;
140 TDB_CONTEXT *tdb;
141 int ref_count;
142 fstring printer_name;
145 static struct tdb_print_db *print_db_head;
147 /****************************************************************************
148 Function to find or create the printer specific job tdb given a printername.
149 Limits the number of tdb's open to MAX_PRINT_DBS_OPEN.
150 ****************************************************************************/
152 static struct tdb_print_db *get_print_db_byname(const char *printername)
154 struct tdb_print_db *p = NULL, *last_entry = NULL;
155 int num_open = 0;
156 pstring printdb_path;
158 for (p = print_db_head, last_entry = print_db_head; p; p = p->next) {
159 if (p->tdb && strequal(p->printer_name, printername)) {
160 DLIST_PROMOTE(print_db_head, p);
161 p->ref_count++;
162 return p;
164 num_open++;
165 last_entry = p;
168 /* Not found. */
169 if (num_open >= MAX_PRINT_DBS_OPEN) {
170 /* Try and recycle the last entry. */
171 DLIST_PROMOTE(print_db_head, last_entry);
173 for (p = print_db_head; p; p = p->next) {
174 if (p->ref_count)
175 continue;
176 if (p->tdb) {
177 if (tdb_close(print_db_head->tdb)) {
178 DEBUG(0,("get_print_db: Failed to close tdb for printer %s\n",
179 print_db_head->printer_name ));
180 return NULL;
183 ZERO_STRUCTP(p);
184 break;
186 if (p) {
187 DLIST_PROMOTE(print_db_head, p);
188 p = print_db_head;
192 if (!p) {
193 /* Create one. */
194 p = (struct tdb_print_db *)malloc(sizeof(struct tdb_print_db));
195 if (!p) {
196 DEBUG(0,("get_print_db: malloc fail !\n"));
197 return NULL;
199 ZERO_STRUCTP(p);
200 DLIST_ADD(print_db_head, p);
203 pstrcpy(printdb_path, lock_path("printing/"));
204 pstrcat(printdb_path, printername);
205 pstrcat(printdb_path, ".tdb");
207 become_root();
208 p->tdb = tdb_open_log(printdb_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
209 unbecome_root();
211 if (!p->tdb) {
212 DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
213 printdb_path ));
214 DLIST_REMOVE(print_db_head, p);
215 SAFE_FREE(p);
216 return NULL;
218 fstrcpy(p->printer_name, printername);
219 p->ref_count++;
220 return p;
223 static void release_print_db( struct tdb_print_db *pdb)
225 pdb->ref_count--;
226 SMB_ASSERT(pdb->ref_count >= 0);
229 /****************************************************************************
230 Initialise the printing backend. Called once at startup.
231 Does not survive a fork
232 ****************************************************************************/
234 BOOL print_backend_init(void)
236 char *sversion = "INFO/version";
237 pstring printing_path;
238 int services = lp_numservices();
239 int snum;
241 if (local_pid == sys_getpid())
242 return True;
244 unlink(lock_path("printing.tdb"));
245 pstrcpy(printing_path,lock_path("printing"));
246 mkdir(printing_path,0755);
248 local_pid = sys_getpid();
250 /* handle a Samba upgrade */
252 for (snum = 0; snum < services; snum++) {
253 struct tdb_print_db *pdb;
254 if (!lp_print_ok(snum))
255 continue;
257 pdb = get_print_db_byname(lp_const_servicename(snum));
258 if (!pdb)
259 continue;
260 if (tdb_lock_bystring(pdb->tdb, sversion) == -1) {
261 DEBUG(0,("print_backend_init: Failed to open printer %s database\n", lp_const_servicename(snum) ));
262 return False;
264 if (tdb_fetch_int32(pdb->tdb, sversion) != PRINT_DATABASE_VERSION) {
265 tdb_traverse(pdb->tdb, tdb_traverse_delete_fn, NULL);
266 tdb_store_int32(pdb->tdb, sversion, PRINT_DATABASE_VERSION);
268 tdb_unlock_bystring(pdb->tdb, sversion);
271 /* select the appropriate printing interface... */
272 #ifdef HAVE_CUPS
273 if (strcmp(lp_printcapname(), "cups") == 0)
274 current_printif = &cups_printif;
275 #endif /* HAVE_CUPS */
277 /* do NT print initialization... */
278 return nt_printing_init();
281 /****************************************************************************
282 Shut down printing backend. Called once at shutdown to close the tdb.
283 ****************************************************************************/
285 void printing_end(void)
287 struct tdb_print_db *p;
289 for (p = print_db_head; p; ) {
290 struct tdb_print_db *next_p = p->next;
291 if (p->tdb)
292 tdb_close(p->tdb);
293 DLIST_REMOVE(print_db_head, p);
294 SAFE_FREE(p);
295 p = next_p;
299 /****************************************************************************
300 Useful function to generate a tdb key.
301 ****************************************************************************/
303 static TDB_DATA print_key(uint32 jobid)
305 static uint32 j;
306 TDB_DATA ret;
308 j = jobid;
309 ret.dptr = (void *)&j;
310 ret.dsize = sizeof(j);
311 return ret;
314 /****************************************************************************
315 Useful function to find a print job in the database.
316 ****************************************************************************/
318 static struct printjob *print_job_find(int snum, uint32 jobid)
320 static struct printjob pjob;
321 TDB_DATA ret;
322 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
324 if (!pdb)
325 return NULL;
327 ret = tdb_fetch(pdb->tdb, print_key(jobid));
328 release_print_db(pdb);
330 if (!ret.dptr || ret.dsize != sizeof(pjob))
331 return NULL;
333 memcpy(&pjob, ret.dptr, sizeof(pjob));
334 SAFE_FREE(ret.dptr);
335 return &pjob;
338 /* Convert a unix jobid to a smb jobid */
340 static uint32 sysjob_to_jobid_value;
342 static int unixjob_traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key,
343 TDB_DATA data, void *state)
345 struct printjob *pjob = (struct printjob *)data.dptr;
346 int *sysjob = (int *)state;
348 if (key.dsize != sizeof(uint32))
349 return 0;
351 if (*sysjob == pjob->sysjob) {
352 uint32 *jobid = (uint32 *)key.dptr;
354 sysjob_to_jobid_value = *jobid;
355 return 1;
358 return 0;
361 /****************************************************************************
362 This is a *horribly expensive call as we have to iterate through all the
363 current printer tdb's. Don't do this often ! JRA.
364 ****************************************************************************/
366 uint32 sysjob_to_jobid(int unix_jobid)
368 int services = lp_numservices();
369 int snum;
371 sysjob_to_jobid_value = (uint32)-1;
373 for (snum = 0; snum < services; snum++) {
374 struct tdb_print_db *pdb;
375 if (!lp_print_ok(snum))
376 continue;
377 pdb = get_print_db_byname(lp_const_servicename(snum));
378 if (pdb)
379 tdb_traverse(pdb->tdb, unixjob_traverse_fn, &unix_jobid);
380 release_print_db(pdb);
381 if (sysjob_to_jobid_value != (uint32)-1)
382 return sysjob_to_jobid_value;
384 return (uint32)-1;
387 /****************************************************************************
388 Send notifications based on what has changed after a pjob_store.
389 ****************************************************************************/
391 static struct {
392 uint32 lpq_status;
393 uint32 spoolss_status;
394 } lpq_to_spoolss_status_map[] = {
395 { LPQ_QUEUED, JOB_STATUS_QUEUED },
396 { LPQ_PAUSED, JOB_STATUS_PAUSED },
397 { LPQ_SPOOLING, JOB_STATUS_SPOOLING },
398 { LPQ_PRINTING, JOB_STATUS_PRINTING },
399 { LPQ_DELETING, JOB_STATUS_DELETING },
400 { LPQ_OFFLINE, JOB_STATUS_OFFLINE },
401 { LPQ_PAPEROUT, JOB_STATUS_PAPEROUT },
402 { LPQ_PRINTED, JOB_STATUS_PRINTED },
403 { LPQ_DELETED, JOB_STATUS_DELETED },
404 { LPQ_BLOCKED, JOB_STATUS_BLOCKED },
405 { LPQ_USER_INTERVENTION, JOB_STATUS_USER_INTERVENTION },
406 { -1, 0 }
409 /* Convert a lpq status value stored in printing.tdb into the
410 appropriate win32 API constant. */
412 static uint32 map_to_spoolss_status(uint32 lpq_status)
414 int i = 0;
416 while (lpq_to_spoolss_status_map[i].lpq_status != -1) {
417 if (lpq_to_spoolss_status_map[i].lpq_status == lpq_status)
418 return lpq_to_spoolss_status_map[i].spoolss_status;
419 i++;
422 return 0;
425 static void pjob_store_notify(int snum, uint32 jobid, struct printjob *old_data,
426 struct printjob *new_data)
428 BOOL new_job = False;
430 if (!old_data)
431 new_job = True;
433 /* Notify the job name first */
435 if (new_job || !strequal(old_data->jobname, new_data->jobname))
436 notify_job_name(snum, jobid, new_data->jobname);
438 /* Job attributes that can't be changed. We only send
439 notification for these on a new job. */
441 if (new_job) {
442 notify_job_submitted(snum, jobid, new_data->starttime);
443 notify_job_username(snum, jobid, new_data->user);
446 /* Job attributes of a new job or attributes that can be
447 modified. */
449 if (new_job || old_data->status != new_data->status)
450 notify_job_status(snum, jobid, map_to_spoolss_status(new_data->status));
452 if (new_job || old_data->size != new_data->size)
453 notify_job_total_bytes(snum, jobid, new_data->size);
455 if (new_job || old_data->page_count != new_data->page_count)
456 notify_job_total_pages(snum, jobid, new_data->page_count);
459 /****************************************************************************
460 Store a job structure back to the database.
461 ****************************************************************************/
463 static BOOL pjob_store(int snum, uint32 jobid, struct printjob *pjob, BOOL donotify)
465 TDB_DATA old_data, new_data;
466 BOOL ret;
467 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
469 if (!pdb)
470 return False;
472 /* Get old data */
474 old_data = tdb_fetch(pdb->tdb, print_key(jobid));
476 /* Store new data */
478 new_data.dptr = (void *)pjob;
479 new_data.dsize = sizeof(*pjob);
480 ret = (tdb_store(pdb->tdb, print_key(jobid), new_data, TDB_REPLACE) == 0);
482 release_print_db(pdb);
484 /* Send notify updates for what has changed */
486 if (donotify && ret && (old_data.dsize == 0 || old_data.dsize == sizeof(*pjob))) {
487 pjob_store_notify(
488 snum, jobid, (struct printjob *)old_data.dptr,
489 (struct printjob *)new_data.dptr);
490 free(old_data.dptr);
493 return ret;
496 /****************************************************************************
497 Remove a job structure from the database.
498 ****************************************************************************/
500 static void pjob_delete(int snum, uint32 jobid)
502 struct printjob *pjob = print_job_find(snum, jobid);
503 uint32 job_status = 0;
504 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
506 if (!pdb)
507 return;
509 if (!pjob) {
510 DEBUG(5, ("pjob_delete(): we were asked to delete nonexistent job %u\n",
511 (unsigned int)jobid));
512 release_print_db(pdb);
513 return;
516 /* Send a notification that a job has been deleted */
518 job_status = map_to_spoolss_status(pjob->status);
520 /* We must cycle through JOB_STATUS_DELETING and
521 JOB_STATUS_DELETED for the port monitor to delete the job
522 properly. */
524 job_status |= JOB_STATUS_DELETING;
525 notify_job_status(snum, jobid, job_status);
527 job_status |= JOB_STATUS_DELETED;
528 notify_job_status(snum, jobid, job_status);
530 /* Remove from printing.tdb */
532 tdb_delete(pdb->tdb, print_key(jobid));
533 release_print_db(pdb);
534 rap_jobid_delete(snum, jobid);
537 /****************************************************************************
538 Parse a file name from the system spooler to generate a jobid.
539 ****************************************************************************/
541 static uint32 print_parse_jobid(char *fname)
543 int jobid;
545 if (strncmp(fname,PRINT_SPOOL_PREFIX,strlen(PRINT_SPOOL_PREFIX)) != 0)
546 return (uint32)-1;
547 fname += strlen(PRINT_SPOOL_PREFIX);
549 jobid = atoi(fname);
550 if (jobid <= 0)
551 return (uint32)-1;
553 return (uint32)jobid;
556 /****************************************************************************
557 List a unix job in the print database.
558 ****************************************************************************/
560 static void print_unix_job(int snum, print_queue_struct *q)
562 uint32 jobid = q->job + UNIX_JOB_START;
563 struct printjob pj, *old_pj;
565 /* Preserve the timestamp on an existing unix print job */
567 old_pj = print_job_find(snum, jobid);
569 ZERO_STRUCT(pj);
571 pj.pid = (pid_t)-1;
572 pj.sysjob = q->job;
573 pj.fd = -1;
574 pj.starttime = old_pj ? old_pj->starttime : q->time;
575 pj.status = q->status;
576 pj.size = q->size;
577 pj.spooled = True;
578 pj.smbjob = False;
579 fstrcpy(pj.filename, "");
580 fstrcpy(pj.jobname, q->fs_file);
581 fstrcpy(pj.user, q->fs_user);
582 fstrcpy(pj.queuename, lp_const_servicename(snum));
584 pjob_store(snum, jobid, &pj, True);
588 struct traverse_struct {
589 print_queue_struct *queue;
590 int qcount, snum, maxcount, total_jobs;
593 /****************************************************************************
594 Utility fn to delete any jobs that are no longer active.
595 ****************************************************************************/
597 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
599 struct traverse_struct *ts = (struct traverse_struct *)state;
600 struct printjob pjob;
601 uint32 jobid;
602 int i;
604 if (data.dsize != sizeof(pjob) || key.dsize != sizeof(jobid))
605 return 0;
606 memcpy(&jobid, key.dptr, sizeof(jobid));
607 memcpy(&pjob, data.dptr, sizeof(pjob));
609 if (ts->snum != lp_servicenumber(pjob.queuename)) {
610 /* this isn't for the queue we are looking at - this cannot happen with the split tdb's. JRA */
611 return 0;
614 if (!pjob.smbjob) {
615 /* remove a unix job if it isn't in the system queue any more */
617 for (i=0;i<ts->qcount;i++) {
618 uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
619 if (jobid == u_jobid)
620 break;
622 if (i == ts->qcount)
623 pjob_delete(ts->snum, jobid);
624 else
625 ts->total_jobs++;
626 return 0;
629 /* maybe it hasn't been spooled yet */
630 if (!pjob.spooled) {
631 /* if a job is not spooled and the process doesn't
632 exist then kill it. This cleans up after smbd
633 deaths */
634 if (!process_exists(pjob.pid))
635 pjob_delete(ts->snum, jobid);
636 else
637 ts->total_jobs++;
638 return 0;
641 for (i=0;i<ts->qcount;i++) {
642 uint32 curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
643 if (jobid == curr_jobid)
644 break;
647 /* The job isn't in the system queue - we have to assume it has
648 completed, so delete the database entry. */
650 if (i == ts->qcount) {
651 time_t cur_t = time(NULL);
653 /* A race can occur between the time a job is spooled and
654 when it appears in the lpq output. This happens when
655 the job is added to printing.tdb when another smbd
656 running print_queue_update() has completed a lpq and
657 is currently traversing the printing tdb and deleting jobs.
658 A workaround is to not delete the job if it has been
659 submitted less than lp_lpqcachetime() seconds ago. */
661 if ((cur_t - pjob.starttime) > lp_lpqcachetime())
662 pjob_delete(ts->snum, jobid);
663 else
664 ts->total_jobs++;
666 else
667 ts->total_jobs++;
669 return 0;
672 /****************************************************************************
673 Check if the print queue has been updated recently enough.
674 ****************************************************************************/
676 static void print_cache_flush(int snum)
678 fstring key;
679 const char *printername = lp_const_servicename(snum);
680 struct tdb_print_db *pdb = get_print_db_byname(printername);
682 if (!pdb)
683 return;
684 slprintf(key, sizeof(key)-1, "CACHE/%s", printername);
685 tdb_store_int32(pdb->tdb, key, -1);
686 release_print_db(pdb);
689 /****************************************************************************
690 Check if someone already thinks they are doing the update.
691 ****************************************************************************/
693 static pid_t get_updating_pid(fstring printer_name)
695 fstring keystr;
696 TDB_DATA data, key;
697 pid_t updating_pid;
698 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
700 if (!pdb)
701 return (pid_t)-1;
702 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
703 key.dptr = keystr;
704 key.dsize = strlen(keystr);
706 data = tdb_fetch(pdb->tdb, key);
707 release_print_db(pdb);
708 if (!data.dptr || data.dsize != sizeof(pid_t))
709 return (pid_t)-1;
711 memcpy(&updating_pid, data.dptr, sizeof(pid_t));
712 SAFE_FREE(data.dptr);
714 if (process_exists(updating_pid))
715 return updating_pid;
717 return (pid_t)-1;
720 /****************************************************************************
721 Set the fact that we're doing the update, or have finished doing the update
722 in the tdb.
723 ****************************************************************************/
725 static void set_updating_pid(const fstring printer_name, BOOL delete)
727 fstring keystr;
728 TDB_DATA key;
729 TDB_DATA data;
730 pid_t updating_pid = sys_getpid();
731 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
733 if (!pdb)
734 return;
736 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
737 key.dptr = keystr;
738 key.dsize = strlen(keystr);
740 if (delete) {
741 tdb_delete(pdb->tdb, key);
742 release_print_db(pdb);
743 return;
746 data.dptr = (void *)&updating_pid;
747 data.dsize = sizeof(pid_t);
749 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
750 release_print_db(pdb);
753 /****************************************************************************
754 Update the internal database from the system print queue for a queue.
755 ****************************************************************************/
757 static void print_queue_update(int snum)
759 int i, qcount;
760 print_queue_struct *queue = NULL;
761 print_status_struct status;
762 print_status_struct old_status;
763 struct printjob *pjob;
764 struct traverse_struct tstruct;
765 fstring keystr, printer_name, cachestr;
766 TDB_DATA data, key;
767 struct tdb_print_db *pdb;
769 fstrcpy(printer_name, lp_const_servicename(snum));
770 pdb = get_print_db_byname(printer_name);
771 if (!pdb)
772 return;
775 * Check to see if someone else is doing this update.
776 * This is essentially a mutex on the update.
779 if (get_updating_pid(printer_name) != -1) {
780 release_print_db(pdb);
781 return;
784 /* Lock the queue for the database update */
786 slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", printer_name);
787 if (tdb_lock_bystring(pdb->tdb, keystr) == -1) {
788 DEBUG(0,("print_queue_update: Failed to lock printer %s database\n", printer_name));
789 release_print_db(pdb);
790 return;
794 * Ensure that no one else got in here.
795 * If the updating pid is still -1 then we are
796 * the winner.
799 if (get_updating_pid(printer_name) != -1) {
801 * Someone else is doing the update, exit.
803 tdb_unlock_bystring(pdb->tdb, keystr);
804 release_print_db(pdb);
805 return;
809 * We're going to do the update ourselves.
812 /* Tell others we're doing the update. */
813 set_updating_pid(printer_name, False);
816 * Allow others to enter and notice we're doing
817 * the update.
820 tdb_unlock_bystring(pdb->tdb, keystr);
823 * Update the cache time FIRST ! Stops others even
824 * attempting to get the lock and doing this
825 * if the lpq takes a long time.
828 slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", printer_name);
829 tdb_store_int32(pdb->tdb, cachestr, (int)time(NULL));
831 /* get the current queue using the appropriate interface */
832 ZERO_STRUCT(status);
834 qcount = (*(current_printif->queue_get))(snum, &queue, &status);
836 DEBUG(3, ("%d job%s in queue for %s\n", qcount, (qcount != 1) ?
837 "s" : "", printer_name));
840 any job in the internal database that is marked as spooled
841 and doesn't exist in the system queue is considered finished
842 and removed from the database
844 any job in the system database but not in the internal database
845 is added as a unix job
847 fill in any system job numbers as we go
849 for (i=0; i<qcount; i++) {
850 uint32 jobid = print_parse_jobid(queue[i].fs_file);
852 if (jobid == (uint32)-1) {
853 /* assume its a unix print job */
854 print_unix_job(snum, &queue[i]);
855 continue;
858 /* we have an active SMB print job - update its status */
859 pjob = print_job_find(snum, jobid);
860 if (!pjob) {
861 /* err, somethings wrong. Probably smbd was restarted
862 with jobs in the queue. All we can do is treat them
863 like unix jobs. Pity. */
864 print_unix_job(snum, &queue[i]);
865 continue;
868 pjob->sysjob = queue[i].job;
869 pjob->status = queue[i].status;
871 pjob_store(snum, jobid, pjob, True);
874 /* now delete any queued entries that don't appear in the
875 system queue */
876 tstruct.queue = queue;
877 tstruct.qcount = qcount;
878 tstruct.snum = snum;
879 tstruct.total_jobs = 0;
881 tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
883 SAFE_FREE(tstruct.queue);
885 tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
887 if( qcount != get_queue_status(snum, &old_status))
888 DEBUG(10,("print_queue_update: queue status change %d jobs -> %d jobs for printer %s\n",
889 old_status.qcount, qcount, printer_name ));
891 /* store the new queue status structure */
892 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printer_name);
893 key.dptr = keystr;
894 key.dsize = strlen(keystr);
896 status.qcount = qcount;
897 data.dptr = (void *)&status;
898 data.dsize = sizeof(status);
899 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
902 * Update the cache time again. We want to do this call
903 * as little as possible...
906 slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", printer_name);
907 tdb_store_int32(pdb->tdb, keystr, (int32)time(NULL));
909 /* Delete our pid from the db. */
910 set_updating_pid(printer_name, True);
911 release_print_db(pdb);
914 /****************************************************************************
915 Check if a jobid is valid. It is valid if it exists in the database.
916 ****************************************************************************/
918 BOOL print_job_exists(int snum, uint32 jobid)
920 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
921 BOOL ret;
923 if (!pdb)
924 return False;
925 ret = tdb_exists(pdb->tdb, print_key(jobid));
926 release_print_db(pdb);
927 return ret;
930 /****************************************************************************
931 Give the fd used for a jobid.
932 ****************************************************************************/
934 int print_job_fd(int snum, uint32 jobid)
936 struct printjob *pjob = print_job_find(snum, jobid);
937 if (!pjob)
938 return -1;
939 /* don't allow another process to get this info - it is meaningless */
940 if (pjob->pid != local_pid)
941 return -1;
942 return pjob->fd;
945 /****************************************************************************
946 Give the filename used for a jobid.
947 Only valid for the process doing the spooling and when the job
948 has not been spooled.
949 ****************************************************************************/
951 char *print_job_fname(int snum, uint32 jobid)
953 struct printjob *pjob = print_job_find(snum, jobid);
954 if (!pjob || pjob->spooled || pjob->pid != local_pid)
955 return NULL;
956 return pjob->filename;
959 /****************************************************************************
960 Set the place in the queue for a job.
961 ****************************************************************************/
963 BOOL print_job_set_place(int snum, uint32 jobid, int place)
965 DEBUG(2,("print_job_set_place not implemented yet\n"));
966 return False;
969 /****************************************************************************
970 Set the name of a job. Only possible for owner.
971 ****************************************************************************/
973 BOOL print_job_set_name(int snum, uint32 jobid, char *name)
975 struct printjob *pjob = print_job_find(snum, jobid);
976 if (!pjob || pjob->pid != local_pid)
977 return False;
979 fstrcpy(pjob->jobname, name);
980 return pjob_store(snum, jobid, pjob, True);
983 /****************************************************************************
984 Delete a print job - don't update queue.
985 ****************************************************************************/
987 static BOOL print_job_delete1(int snum, uint32 jobid)
989 struct printjob *pjob = print_job_find(snum, jobid);
990 int result = 0;
992 if (!pjob)
993 return False;
996 * If already deleting just return.
999 if (pjob->status == LPQ_DELETING)
1000 return True;
1002 /* Hrm - we need to be able to cope with deleting a job before it
1003 has reached the spooler. */
1005 if (pjob->sysjob == -1) {
1006 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid));
1009 /* Set the tdb entry to be deleting. */
1011 pjob->status = LPQ_DELETING;
1012 pjob_store(snum, jobid, pjob, True);
1014 if (pjob->spooled && pjob->sysjob != -1)
1015 result = (*(current_printif->job_delete))(snum, pjob);
1017 /* Delete the tdb entry if the delete suceeded or the job hasn't
1018 been spooled. */
1020 if (result == 0)
1021 pjob_delete(snum, jobid);
1023 return (result == 0);
1026 /****************************************************************************
1027 Return true if the current user owns the print job.
1028 ****************************************************************************/
1030 static BOOL is_owner(struct current_user *user, int snum, uint32 jobid)
1032 struct printjob *pjob = print_job_find(snum, jobid);
1033 user_struct *vuser;
1035 if (!pjob || !user)
1036 return False;
1038 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1039 return strequal(pjob->user, vuser->user.smb_name);
1040 } else {
1041 return strequal(pjob->user, uidtoname(user->uid));
1045 /****************************************************************************
1046 Delete a print job.
1047 ****************************************************************************/
1049 BOOL print_job_delete(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1051 BOOL owner;
1053 owner = is_owner(user, snum, jobid);
1055 /* Check access against security descriptor or whether the user
1056 owns their job. */
1058 if (!owner &&
1059 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1060 DEBUG(3, ("delete denied by security descriptor\n"));
1061 *errcode = WERR_ACCESS_DENIED;
1062 return False;
1065 if (!print_job_delete1(snum, jobid))
1066 return False;
1068 /* force update the database and say the delete failed if the
1069 job still exists */
1071 print_queue_update(snum);
1073 return !print_job_exists(snum, jobid);
1076 /****************************************************************************
1077 Pause a job.
1078 ****************************************************************************/
1080 BOOL print_job_pause(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1082 struct printjob *pjob = print_job_find(snum, jobid);
1083 int ret = -1;
1085 if (!pjob || !user)
1086 return False;
1088 if (!pjob->spooled || pjob->sysjob == -1)
1089 return False;
1091 if (!is_owner(user, snum, jobid) &&
1092 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1093 DEBUG(3, ("pause denied by security descriptor\n"));
1094 *errcode = WERR_ACCESS_DENIED;
1095 return False;
1098 /* need to pause the spooled entry */
1099 ret = (*(current_printif->job_pause))(snum, pjob);
1101 if (ret != 0) {
1102 *errcode = WERR_INVALID_PARAM;
1103 return False;
1106 /* force update the database */
1107 print_cache_flush(snum);
1109 /* Send a printer notify message */
1111 notify_job_status(snum, jobid, JOB_STATUS_PAUSED);
1113 /* how do we tell if this succeeded? */
1115 return True;
1118 /****************************************************************************
1119 Resume a job.
1120 ****************************************************************************/
1122 BOOL print_job_resume(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1124 struct printjob *pjob = print_job_find(snum, jobid);
1125 int ret;
1127 if (!pjob || !user)
1128 return False;
1130 if (!pjob->spooled || pjob->sysjob == -1)
1131 return False;
1133 if (!is_owner(user, snum, jobid) &&
1134 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1135 DEBUG(3, ("resume denied by security descriptor\n"));
1136 *errcode = WERR_ACCESS_DENIED;
1137 return False;
1140 ret = (*(current_printif->job_resume))(snum, pjob);
1142 if (ret != 0) {
1143 *errcode = WERR_INVALID_PARAM;
1144 return False;
1147 /* force update the database */
1148 print_cache_flush(snum);
1150 /* Send a printer notify message */
1152 notify_job_status(snum, jobid, JOB_STATUS_QUEUED);
1154 return True;
1157 /****************************************************************************
1158 Write to a print file.
1159 ****************************************************************************/
1161 int print_job_write(int snum, uint32 jobid, const char *buf, int size)
1163 int return_code;
1164 struct printjob *pjob = print_job_find(snum, jobid);
1166 if (!pjob)
1167 return -1;
1168 /* don't allow another process to get this info - it is meaningless */
1169 if (pjob->pid != local_pid)
1170 return -1;
1172 return_code = write(pjob->fd, buf, size);
1173 if (return_code>0) {
1174 pjob->size += size;
1175 pjob_store(snum, jobid, pjob, True);
1177 return return_code;
1180 /****************************************************************************
1181 Check if the print queue has been updated recently enough.
1182 ****************************************************************************/
1184 static BOOL print_cache_expired(int snum)
1186 fstring key;
1187 time_t last_qscan_time, time_now = time(NULL);
1188 const char *printername = lp_const_servicename(snum);
1189 struct tdb_print_db *pdb = get_print_db_byname(printername);
1191 if (!pdb)
1192 return False;
1194 slprintf(key, sizeof(key), "CACHE/%s", printername);
1195 last_qscan_time = (time_t)tdb_fetch_int32(pdb->tdb, key);
1198 * Invalidate the queue for 3 reasons.
1199 * (1). last queue scan time == -1.
1200 * (2). Current time - last queue scan time > allowed cache time.
1201 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1202 * This last test picks up machines for which the clock has been moved
1203 * forward, an lpq scan done and then the clock moved back. Otherwise
1204 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1207 if (last_qscan_time == ((time_t)-1) || (time_now - last_qscan_time) >= lp_lpqcachetime() ||
1208 last_qscan_time > (time_now + MAX_CACHE_VALID_TIME)) {
1209 DEBUG(3, ("print cache expired for queue %s \
1210 (last_qscan_time = %d, time now = %d, qcachetime = %d)\n", printername,
1211 (int)last_qscan_time, (int)time_now, (int)lp_lpqcachetime() ));
1212 release_print_db(pdb);
1213 return True;
1215 release_print_db(pdb);
1216 return False;
1219 /****************************************************************************
1220 Get the queue status - do not update if db is out of date.
1221 ****************************************************************************/
1223 static int get_queue_status(int snum, print_status_struct *status)
1225 fstring keystr;
1226 TDB_DATA data, key;
1227 const char *printername = lp_const_servicename(snum);
1228 struct tdb_print_db *pdb = get_print_db_byname(printername);
1229 if (!pdb)
1230 return 0;
1232 ZERO_STRUCTP(status);
1233 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
1234 key.dptr = keystr;
1235 key.dsize = strlen(keystr);
1236 data = tdb_fetch(pdb->tdb, key);
1237 release_print_db(pdb);
1238 if (data.dptr) {
1239 if (data.dsize == sizeof(print_status_struct)) {
1240 memcpy(status, data.dptr, sizeof(print_status_struct));
1242 SAFE_FREE(data.dptr);
1244 return status->qcount;
1247 /****************************************************************************
1248 Determine the number of jobs in a queue.
1249 ****************************************************************************/
1251 int print_queue_length(int snum, print_status_struct *pstatus)
1253 print_status_struct status;
1254 int len;
1256 /* make sure the database is up to date */
1257 if (print_cache_expired(snum))
1258 print_queue_update(snum);
1260 /* also fetch the queue status */
1261 memset(&status, 0, sizeof(status));
1262 len = get_queue_status(snum, &status);
1263 if (pstatus)
1264 *pstatus = status;
1265 return len;
1268 #if 0 /* JRATEST */
1269 /****************************************************************************
1270 Determine the number of jobs in all queues. This is very expensive. Don't
1271 call ! JRA.
1272 ****************************************************************************/
1274 static int get_total_jobs(void)
1276 int total_jobs = 0;
1277 int snum;
1278 int services = lp_numservices();
1280 for (snum = 0; snum < services; snum++) {
1281 struct tdb_print_db *pdb;
1282 int jobs;
1284 if (!lp_print_ok(snum))
1285 continue;
1287 pdb = get_print_db_byname(lp_const_servicename(snum));
1288 if (!pdb)
1289 continue;
1291 /* make sure the database is up to date */
1292 if (print_cache_expired(snum))
1293 print_queue_update(snum);
1295 jobs = tdb_fetch_int32(pdb->tdb, "INFO/total_jobs");
1296 if (jobs > 0)
1297 total_jobs += jobs;
1298 release_print_db(pdb);
1300 return total_jobs;
1302 #endif /* JRATEST */
1304 /***************************************************************************
1305 Start spooling a job - return the jobid.
1306 ***************************************************************************/
1308 uint32 print_job_start(struct current_user *user, int snum, char *jobname)
1310 uint32 jobid;
1311 char *path;
1312 struct printjob pjob;
1313 int next_jobid;
1314 user_struct *vuser;
1315 int njobs = 0;
1316 const char *printername = lp_const_servicename(snum);
1317 struct tdb_print_db *pdb = get_print_db_byname(printername);
1319 errno = 0;
1321 if (!pdb)
1322 return (uint32)-1;
1324 if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
1325 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
1326 release_print_db(pdb);
1327 return (uint32)-1;
1330 if (!print_time_access_check(snum)) {
1331 DEBUG(3, ("print_job_start: job start denied by time check\n"));
1332 release_print_db(pdb);
1333 return (uint32)-1;
1336 path = lp_pathname(snum);
1338 /* see if we have sufficient disk space */
1339 if (lp_minprintspace(snum)) {
1340 SMB_BIG_UINT dspace, dsize;
1341 if (sys_fsusage(path, &dspace, &dsize) == 0 &&
1342 dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
1343 DEBUG(3, ("print_job_start: disk space check failed.\n"));
1344 release_print_db(pdb);
1345 errno = ENOSPC;
1346 return (uint32)-1;
1350 /* for autoloaded printers, check that the printcap entry still exists */
1351 if (lp_autoloaded(snum) && !pcap_printername_ok(lp_const_servicename(snum), NULL)) {
1352 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum) ));
1353 release_print_db(pdb);
1354 errno = ENOENT;
1355 return (uint32)-1;
1358 /* Insure the maximum queue size is not violated */
1359 if (lp_maxprintjobs(snum) && (njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
1360 DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per queue (%d).\n",
1361 njobs, lp_maxprintjobs(snum) ));
1362 release_print_db(pdb);
1363 errno = ENOSPC;
1364 return (uint32)-1;
1367 #if 0 /* JRATEST */
1368 /* Insure the maximum print jobs in the system is not violated */
1369 if (lp_totalprintjobs() && get_total_jobs() > lp_totalprintjobs()) {
1370 DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per system (%d).\n",
1371 njobs, lp_totalprintjobs() ));
1372 release_print_db(pdb);
1373 errno = ENOSPC;
1374 return (uint32)-1;
1376 #endif /* JRATEST */
1378 /* create the database entry */
1379 ZERO_STRUCT(pjob);
1380 pjob.pid = local_pid;
1381 pjob.sysjob = -1;
1382 pjob.fd = -1;
1383 pjob.starttime = time(NULL);
1384 pjob.status = LPQ_SPOOLING;
1385 pjob.size = 0;
1386 pjob.spooled = False;
1387 pjob.smbjob = True;
1389 fstrcpy(pjob.jobname, jobname);
1391 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1392 fstrcpy(pjob.user, vuser->user.smb_name);
1393 } else {
1394 fstrcpy(pjob.user, uidtoname(user->uid));
1397 fstrcpy(pjob.queuename, lp_const_servicename(snum));
1399 /* lock the database */
1400 if (tdb_lock_bystring(pdb->tdb, "INFO/nextjob") == -1) {
1401 DEBUG(0,("print_job_start: failed to lock printing database %s\n", printername ));
1402 release_print_db(pdb);
1403 return (uint32)-1;
1406 next_jobid = tdb_fetch_int32(pdb->tdb, "INFO/nextjob");
1407 if (next_jobid == -1)
1408 next_jobid = 1;
1410 for (jobid = NEXT_JOBID(next_jobid); jobid != next_jobid; jobid = NEXT_JOBID(jobid)) {
1411 if (!print_job_exists(snum, jobid))
1412 break;
1414 if (jobid == next_jobid || !pjob_store(snum, jobid, &pjob, False)) {
1415 DEBUG(3, ("print_job_start: either jobid (%d)==next_jobid(%d) or pjob_store failed.\n",
1416 jobid, next_jobid ));
1417 jobid = -1;
1418 goto fail;
1421 if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
1422 DEBUG(3, ("print_job_start: failed to store INFO/nextjob.\n"));
1423 jobid = -1;
1424 goto fail;
1427 /* We've finished with the INFO/nextjob lock. */
1428 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1430 /* we have a job entry - now create the spool file */
1431 slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.8u.XXXXXX",
1432 path, PRINT_SPOOL_PREFIX, (unsigned int)jobid);
1433 pjob.fd = smb_mkstemp(pjob.filename);
1435 if (pjob.fd == -1) {
1436 if (errno == EACCES) {
1437 /* Common setup error, force a report. */
1438 DEBUG(0, ("print_job_start: insufficient permissions \
1439 to open spool file %s.\n", pjob.filename));
1440 } else {
1441 /* Normal case, report at level 3 and above. */
1442 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
1443 DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
1445 goto fail;
1448 pjob_store(snum, jobid, &pjob, False);
1450 release_print_db(pdb);
1453 * If the printer is marked as postscript output a leading
1454 * file identifier to ensure the file is treated as a raw
1455 * postscript file.
1456 * This has a similar effect as CtrlD=0 in WIN.INI file.
1457 * tim@fsg.com 09/06/94
1459 if (lp_postscript(snum)) {
1460 print_job_write(snum, jobid, "%!\n",3);
1463 return jobid;
1465 fail:
1466 if (jobid != -1)
1467 pjob_delete(snum, jobid);
1469 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1470 release_print_db(pdb);
1472 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
1473 return -1;
1476 /****************************************************************************
1477 Update the number of pages spooled to jobid
1478 ****************************************************************************/
1480 void print_job_endpage(int snum, uint32 jobid)
1482 struct printjob *pjob = print_job_find(snum, jobid);
1483 if (!pjob)
1484 return;
1485 /* don't allow another process to get this info - it is meaningless */
1486 if (pjob->pid != local_pid)
1487 return;
1489 pjob->page_count++;
1490 pjob_store(snum, jobid, pjob, True);
1493 /****************************************************************************
1494 Print a file - called on closing the file. This spools the job.
1495 If normal close is false then we're tearing down the jobs - treat as an
1496 error.
1497 ****************************************************************************/
1499 BOOL print_job_end(int snum, uint32 jobid, BOOL normal_close)
1501 struct printjob *pjob = print_job_find(snum, jobid);
1502 int ret;
1503 SMB_STRUCT_STAT sbuf;
1505 if (!pjob)
1506 return False;
1508 if (pjob->spooled || pjob->pid != local_pid)
1509 return False;
1511 if (normal_close && (sys_fstat(pjob->fd, &sbuf) == 0)) {
1512 pjob->size = sbuf.st_size;
1513 close(pjob->fd);
1514 pjob->fd = -1;
1515 } else {
1518 * Not a normal close or we couldn't stat the job file,
1519 * so something has gone wrong. Cleanup.
1521 close(pjob->fd);
1522 pjob->fd = -1;
1523 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
1524 goto fail;
1527 /* Technically, this is not quite right. If the printer has a separator
1528 * page turned on, the NT spooler prints the separator page even if the
1529 * print job is 0 bytes. 010215 JRR */
1530 if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
1531 /* don't bother spooling empty files or something being deleted. */
1532 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
1533 pjob->filename, pjob->size ? "deleted" : "zero length" ));
1534 unlink(pjob->filename);
1535 pjob_delete(snum, jobid);
1536 return True;
1539 ret = (*(current_printif->job_submit))(snum, pjob);
1541 if (ret)
1542 goto fail;
1544 /* The print job has been sucessfully handed over to the back-end */
1546 pjob->spooled = True;
1547 pjob->status = LPQ_QUEUED;
1548 pjob_store(snum, jobid, pjob, True);
1550 /* make sure the database is up to date */
1551 if (print_cache_expired(snum))
1552 print_queue_update(snum);
1554 return True;
1556 fail:
1558 /* The print job was not succesfully started. Cleanup */
1559 /* Still need to add proper error return propagation! 010122:JRR */
1560 unlink(pjob->filename);
1561 pjob_delete(snum, jobid);
1562 return False;
1565 /****************************************************************************
1566 Utility fn to enumerate the print queue.
1567 ****************************************************************************/
1569 static int traverse_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
1571 struct traverse_struct *ts = (struct traverse_struct *)state;
1572 struct printjob pjob;
1573 int i;
1574 uint32 jobid;
1576 if (data.dsize != sizeof(pjob) || key.dsize != sizeof(int))
1577 return 0;
1578 memcpy(&jobid, key.dptr, sizeof(jobid));
1579 memcpy(&pjob, data.dptr, sizeof(pjob));
1581 /* maybe it isn't for this queue */
1582 if (ts->snum != lp_servicenumber(pjob.queuename))
1583 return 0;
1585 if (ts->qcount >= ts->maxcount)
1586 return 0;
1588 i = ts->qcount;
1590 ts->queue[i].job = jobid;
1591 ts->queue[i].size = pjob.size;
1592 ts->queue[i].page_count = pjob.page_count;
1593 ts->queue[i].status = pjob.status;
1594 ts->queue[i].priority = 1;
1595 ts->queue[i].time = pjob.starttime;
1596 fstrcpy(ts->queue[i].fs_user, pjob.user);
1597 fstrcpy(ts->queue[i].fs_file, pjob.jobname);
1599 ts->qcount++;
1601 return 0;
1604 struct traverse_count_struct {
1605 int snum, count;
1608 /****************************************************************************
1609 Utility fn to count the number of entries in the print queue.
1610 ****************************************************************************/
1612 static int traverse_count_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
1614 struct traverse_count_struct *ts = (struct traverse_count_struct *)state;
1615 struct printjob pjob;
1616 uint32 jobid;
1618 if (data.dsize != sizeof(pjob) || key.dsize != sizeof(int))
1619 return 0;
1620 memcpy(&jobid, key.dptr, sizeof(jobid));
1621 memcpy(&pjob, data.dptr, sizeof(pjob));
1623 /* maybe it isn't for this queue - this cannot happen with the tdb/printer code. JRA */
1624 if (ts->snum != lp_servicenumber(pjob.queuename))
1625 return 0;
1627 ts->count++;
1629 return 0;
1632 /****************************************************************************
1633 Sort print jobs by submittal time.
1634 ****************************************************************************/
1636 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
1638 /* Silly cases */
1640 if (!j1 && !j2)
1641 return 0;
1642 if (!j1)
1643 return -1;
1644 if (!j2)
1645 return 1;
1647 /* Sort on job start time */
1649 if (j1->time == j2->time)
1650 return 0;
1651 return (j1->time > j2->time) ? 1 : -1;
1654 /****************************************************************************
1655 Get a printer queue listing.
1656 ****************************************************************************/
1658 int print_queue_status(int snum,
1659 print_queue_struct **queue,
1660 print_status_struct *status)
1662 struct traverse_struct tstruct;
1663 struct traverse_count_struct tsc;
1664 fstring keystr;
1665 TDB_DATA data, key;
1666 const char *printername = lp_const_servicename(snum);
1667 struct tdb_print_db *pdb = get_print_db_byname(printername);
1669 *queue = NULL;
1671 if (!pdb)
1672 return 0;
1674 /* make sure the database is up to date */
1675 if (print_cache_expired(snum))
1676 print_queue_update(snum);
1679 * Fetch the queue status. We must do this first, as there may
1680 * be no jobs in the queue.
1682 ZERO_STRUCTP(status);
1683 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
1684 key.dptr = keystr;
1685 key.dsize = strlen(keystr);
1686 data = tdb_fetch(pdb->tdb, key);
1687 if (data.dptr) {
1688 if (data.dsize == sizeof(*status)) {
1689 memcpy(status, data.dptr, sizeof(*status));
1691 SAFE_FREE(data.dptr);
1695 * Now, fetch the print queue information. We first count the number
1696 * of entries, and then only retrieve the queue if necessary.
1698 tsc.count = 0;
1699 tsc.snum = snum;
1701 tdb_traverse(pdb->tdb, traverse_count_fn_queue, (void *)&tsc);
1703 if (tsc.count == 0) {
1704 release_print_db(pdb);
1705 return 0;
1708 /* Allocate the queue size. */
1709 if ((tstruct.queue = (print_queue_struct *)
1710 malloc(sizeof(print_queue_struct)*tsc.count)) == NULL) {
1711 release_print_db(pdb);
1712 return 0;
1716 * Fill in the queue.
1717 * We need maxcount as the queue size may have changed between
1718 * the two calls to tdb_traverse.
1720 tstruct.qcount = 0;
1721 tstruct.maxcount = tsc.count;
1722 tstruct.snum = snum;
1724 tdb_traverse(pdb->tdb, traverse_fn_queue, (void *)&tstruct);
1725 release_print_db(pdb);
1727 /* Sort the queue by submission time otherwise they are displayed
1728 in hash order. */
1730 qsort(tstruct.queue, tstruct.qcount, sizeof(print_queue_struct),
1731 QSORT_CAST(printjob_comp));
1733 *queue = tstruct.queue;
1734 return tstruct.qcount;
1737 /****************************************************************************
1738 Turn a queue name into a snum.
1739 ****************************************************************************/
1741 int print_queue_snum(const char *qname)
1743 int snum = lp_servicenumber(qname);
1744 if (snum == -1 || !lp_print_ok(snum))
1745 return -1;
1746 return snum;
1749 /****************************************************************************
1750 Pause a queue.
1751 ****************************************************************************/
1753 BOOL print_queue_pause(struct current_user *user, int snum, WERROR *errcode)
1755 int ret;
1757 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
1758 *errcode = WERR_ACCESS_DENIED;
1759 return False;
1762 ret = (*(current_printif->queue_pause))(snum);
1764 if (ret != 0) {
1765 *errcode = WERR_INVALID_PARAM;
1766 return False;
1769 /* force update the database */
1770 print_cache_flush(snum);
1772 /* Send a printer notify message */
1774 notify_printer_status(snum, PRINTER_STATUS_PAUSED);
1776 return True;
1779 /****************************************************************************
1780 Resume a queue.
1781 ****************************************************************************/
1783 BOOL print_queue_resume(struct current_user *user, int snum, WERROR *errcode)
1785 int ret;
1787 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
1788 *errcode = WERR_ACCESS_DENIED;
1789 return False;
1792 ret = (*(current_printif->queue_resume))(snum);
1794 if (ret != 0) {
1795 *errcode = WERR_INVALID_PARAM;
1796 return False;
1799 /* make sure the database is up to date */
1800 if (print_cache_expired(snum))
1801 print_queue_update(snum);
1803 /* Send a printer notify message */
1805 notify_printer_status(snum, PRINTER_STATUS_OK);
1807 return True;
1810 /****************************************************************************
1811 Purge a queue - implemented by deleting all jobs that we can delete.
1812 ****************************************************************************/
1814 BOOL print_queue_purge(struct current_user *user, int snum, WERROR *errcode)
1816 print_queue_struct *queue;
1817 print_status_struct status;
1818 int njobs, i;
1819 BOOL can_job_admin;
1821 /* Force and update so the count is accurate (i.e. not a cached count) */
1822 print_queue_update(snum);
1824 can_job_admin = print_access_check(user, snum, JOB_ACCESS_ADMINISTER);
1825 njobs = print_queue_status(snum, &queue, &status);
1827 for (i=0;i<njobs;i++) {
1828 BOOL owner = is_owner(user, snum, queue[i].job);
1830 if (owner || can_job_admin) {
1831 print_job_delete1(snum, queue[i].job);
1835 SAFE_FREE(queue);
1837 return True;