2 Unix SMB/Netbios implementation.
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.
25 /* Current printer interface */
26 static struct printif
*current_printif
= &generic_printif
;
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
)
57 /* Create the in-memory tdb. */
58 rap_tdb
= tdb_open_log(NULL
, 0, TDB_INTERNAL
, (O_RDWR
|O_CREAT
), 0644);
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
));
74 /* Not found - create and store mapping. */
75 rap_jobid
= ++next_rap_jobid
;
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
);
85 BOOL
rap_to_pjobid(uint16 rap_jobid
, int *psnum
, uint32
*pjobid
)
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);
105 static void rap_jobid_delete(int snum
, uint32 jobid
)
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
)))
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
;
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
;
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
);
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
) {
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
));
187 DLIST_PROMOTE(print_db_head
, p
);
194 p
= (struct tdb_print_db
*)malloc(sizeof(struct tdb_print_db
));
196 DEBUG(0,("get_print_db: malloc fail !\n"));
200 DLIST_ADD(print_db_head
, p
);
203 pstrcpy(printdb_path
, lock_path("printing/"));
204 pstrcat(printdb_path
, printername
);
205 pstrcat(printdb_path
, ".tdb");
208 p
->tdb
= tdb_open_log(printdb_path
, 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
212 DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
214 DLIST_REMOVE(print_db_head
, p
);
218 fstrcpy(p
->printer_name
, printername
);
223 static void release_print_db( struct tdb_print_db
*pdb
)
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();
241 if (local_pid
== sys_getpid())
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
))
257 pdb
= get_print_db_byname(lp_const_servicename(snum
));
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
) ));
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... */
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
;
293 DLIST_REMOVE(print_db_head
, p
);
299 /****************************************************************************
300 Useful function to generate a tdb key.
301 ****************************************************************************/
303 static TDB_DATA
print_key(uint32 jobid
)
309 ret
.dptr
= (void *)&j
;
310 ret
.dsize
= sizeof(j
);
314 /***********************************************************************
315 unpack a pjob from a tdb buffer
316 ***********************************************************************/
318 int unpack_pjob( char* buf
, int buflen
, struct printjob
*pjob
)
326 len
+= tdb_unpack(buf
+len
, buflen
-len
, "dddddddddffff",
344 if ( (used
= unpack_devicemode(&pjob
->nt_devmode
, buf
+len
, buflen
-len
)) == -1 )
353 /****************************************************************************
354 Useful function to find a print job in the database.
355 ****************************************************************************/
357 static struct printjob
*print_job_find(int snum
, uint32 jobid
)
359 static struct printjob pjob
;
361 struct tdb_print_db
*pdb
= get_print_db_byname(lp_const_servicename(snum
));
367 ret
= tdb_fetch(pdb
->tdb
, print_key(jobid
));
368 release_print_db(pdb
);
373 if ( pjob
.nt_devmode
)
374 free_nt_devicemode( &pjob
.nt_devmode
);
378 if ( unpack_pjob( ret
.dptr
, ret
.dsize
, &pjob
) == -1 )
385 /* Convert a unix jobid to a smb jobid */
387 static uint32 sysjob_to_jobid_value
;
389 static int unixjob_traverse_fn(TDB_CONTEXT
*the_tdb
, TDB_DATA key
,
390 TDB_DATA data
, void *state
)
392 struct printjob
*pjob
;
393 int *sysjob
= (int *)state
;
395 if (!data
.dptr
|| data
.dsize
== 0)
398 pjob
= (struct printjob
*)data
.dptr
;
399 if (key
.dsize
!= sizeof(uint32
))
402 if (*sysjob
== pjob
->sysjob
) {
403 uint32
*jobid
= (uint32
*)key
.dptr
;
405 sysjob_to_jobid_value
= *jobid
;
412 /****************************************************************************
413 This is a *horribly expensive call as we have to iterate through all the
414 current printer tdb's. Don't do this often ! JRA.
415 ****************************************************************************/
417 uint32
sysjob_to_jobid(int unix_jobid
)
419 int services
= lp_numservices();
422 sysjob_to_jobid_value
= (uint32
)-1;
424 for (snum
= 0; snum
< services
; snum
++) {
425 struct tdb_print_db
*pdb
;
426 if (!lp_print_ok(snum
))
428 pdb
= get_print_db_byname(lp_const_servicename(snum
));
430 tdb_traverse(pdb
->tdb
, unixjob_traverse_fn
, &unix_jobid
);
431 release_print_db(pdb
);
432 if (sysjob_to_jobid_value
!= (uint32
)-1)
433 return sysjob_to_jobid_value
;
438 /****************************************************************************
439 Send notifications based on what has changed after a pjob_store.
440 ****************************************************************************/
444 uint32 spoolss_status
;
445 } lpq_to_spoolss_status_map
[] = {
446 { LPQ_QUEUED
, JOB_STATUS_QUEUED
},
447 { LPQ_PAUSED
, JOB_STATUS_PAUSED
},
448 { LPQ_SPOOLING
, JOB_STATUS_SPOOLING
},
449 { LPQ_PRINTING
, JOB_STATUS_PRINTING
},
450 { LPQ_DELETING
, JOB_STATUS_DELETING
},
451 { LPQ_OFFLINE
, JOB_STATUS_OFFLINE
},
452 { LPQ_PAPEROUT
, JOB_STATUS_PAPEROUT
},
453 { LPQ_PRINTED
, JOB_STATUS_PRINTED
},
454 { LPQ_DELETED
, JOB_STATUS_DELETED
},
455 { LPQ_BLOCKED
, JOB_STATUS_BLOCKED
},
456 { LPQ_USER_INTERVENTION
, JOB_STATUS_USER_INTERVENTION
},
460 /* Convert a lpq status value stored in printing.tdb into the
461 appropriate win32 API constant. */
463 static uint32
map_to_spoolss_status(uint32 lpq_status
)
467 while (lpq_to_spoolss_status_map
[i
].lpq_status
!= -1) {
468 if (lpq_to_spoolss_status_map
[i
].lpq_status
== lpq_status
)
469 return lpq_to_spoolss_status_map
[i
].spoolss_status
;
476 static void pjob_store_notify(int snum
, uint32 jobid
, struct printjob
*old_data
,
477 struct printjob
*new_data
)
479 BOOL new_job
= False
;
484 /* Notify the job name first */
486 if (new_job
|| !strequal(old_data
->jobname
, new_data
->jobname
))
487 notify_job_name(snum
, jobid
, new_data
->jobname
);
489 /* Job attributes that can't be changed. We only send
490 notification for these on a new job. */
493 notify_job_submitted(snum
, jobid
, new_data
->starttime
);
494 notify_job_username(snum
, jobid
, new_data
->user
);
497 /* Job attributes of a new job or attributes that can be
500 if (new_job
|| old_data
->status
!= new_data
->status
)
501 notify_job_status(snum
, jobid
, map_to_spoolss_status(new_data
->status
));
503 if (new_job
|| old_data
->size
!= new_data
->size
)
504 notify_job_total_bytes(snum
, jobid
, new_data
->size
);
506 if (new_job
|| old_data
->page_count
!= new_data
->page_count
)
507 notify_job_total_pages(snum
, jobid
, new_data
->page_count
);
510 /****************************************************************************
511 Store a job structure back to the database.
512 ****************************************************************************/
514 static BOOL
pjob_store(int snum
, uint32 jobid
, struct printjob
*pjob
)
516 TDB_DATA old_data
, new_data
;
518 struct tdb_print_db
*pdb
= get_print_db_byname(lp_const_servicename(snum
));
520 int len
, newlen
, buflen
;
528 old_data
= tdb_fetch(pdb
->tdb
, print_key(jobid
));
530 /* Doh! Now we have to pack/unpack data since the NT_DEVICEMODE was added */
537 len
+= tdb_pack(buf
+len
, buflen
-len
, "dddddddddffff",
552 len
+= pack_devicemode(pjob
->nt_devmode
, buf
+len
, buflen
-len
);
558 tb
= (char *)Realloc(buf
, len
);
560 DEBUG(0,("pjob_store: failed to enlarge buffer!\n"));
568 while ( buflen
!= len
);
574 new_data
.dsize
= len
;
575 ret
= (tdb_store(pdb
->tdb
, print_key(jobid
), new_data
, TDB_REPLACE
) == 0);
577 release_print_db(pdb
);
579 /* Send notify updates for what has changed */
581 if ( ret
&& (old_data
.dsize
== 0 || old_data
.dsize
== sizeof(*pjob
)) )
582 pjob_store_notify( snum
, jobid
, (struct printjob
*)old_data
.dptr
, pjob
);
585 SAFE_FREE( old_data
.dptr
);
591 /****************************************************************************
592 Remove a job structure from the database.
593 ****************************************************************************/
595 static void pjob_delete(int snum
, uint32 jobid
)
597 struct printjob
*pjob
= print_job_find(snum
, jobid
);
598 uint32 job_status
= 0;
599 struct tdb_print_db
*pdb
= get_print_db_byname(lp_const_servicename(snum
));
605 DEBUG(5, ("pjob_delete(): we were asked to delete nonexistent job %u\n",
606 (unsigned int)jobid
));
607 release_print_db(pdb
);
611 /* Send a notification that a job has been deleted */
613 job_status
= map_to_spoolss_status(pjob
->status
);
615 /* We must cycle through JOB_STATUS_DELETING and
616 JOB_STATUS_DELETED for the port monitor to delete the job
619 job_status
|= JOB_STATUS_DELETING
;
620 notify_job_status(snum
, jobid
, job_status
);
622 job_status
|= JOB_STATUS_DELETED
;
623 notify_job_status(snum
, jobid
, job_status
);
625 /* Remove from printing.tdb */
627 tdb_delete(pdb
->tdb
, print_key(jobid
));
628 release_print_db(pdb
);
629 rap_jobid_delete(snum
, jobid
);
632 /****************************************************************************
633 Parse a file name from the system spooler to generate a jobid.
634 ****************************************************************************/
636 static uint32
print_parse_jobid(char *fname
)
640 if (strncmp(fname
,PRINT_SPOOL_PREFIX
,strlen(PRINT_SPOOL_PREFIX
)) != 0)
642 fname
+= strlen(PRINT_SPOOL_PREFIX
);
648 return (uint32
)jobid
;
651 /****************************************************************************
652 List a unix job in the print database.
653 ****************************************************************************/
655 static void print_unix_job(int snum
, print_queue_struct
*q
)
657 uint32 jobid
= q
->job
+ UNIX_JOB_START
;
658 struct printjob pj
, *old_pj
;
660 /* Preserve the timestamp on an existing unix print job */
662 old_pj
= print_job_find(snum
, jobid
);
669 pj
.starttime
= old_pj
? old_pj
->starttime
: q
->time
;
670 pj
.status
= q
->status
;
674 fstrcpy(pj
.filename
, "");
675 fstrcpy(pj
.jobname
, q
->fs_file
);
676 fstrcpy(pj
.user
, q
->fs_user
);
677 fstrcpy(pj
.queuename
, lp_const_servicename(snum
));
679 pjob_store(snum
, jobid
, &pj
);
683 struct traverse_struct
{
684 print_queue_struct
*queue
;
685 int qcount
, snum
, maxcount
, total_jobs
;
688 /****************************************************************************
689 Utility fn to delete any jobs that are no longer active.
690 ****************************************************************************/
692 static int traverse_fn_delete(TDB_CONTEXT
*t
, TDB_DATA key
, TDB_DATA data
, void *state
)
694 struct traverse_struct
*ts
= (struct traverse_struct
*)state
;
695 struct printjob pjob
;
699 if ( key
.dsize
!= sizeof(jobid
) )
702 memcpy(&jobid
, key
.dptr
, sizeof(jobid
));
703 if ( unpack_pjob( data
.dptr
, data
.dsize
, &pjob
) == -1 )
705 free_nt_devicemode( &pjob
.nt_devmode
);
708 if (ts
->snum
!= lp_servicenumber(pjob
.queuename
)) {
709 /* this isn't for the queue we are looking at - this cannot happen with the split tdb's. JRA */
714 /* remove a unix job if it isn't in the system queue any more */
716 for (i
=0;i
<ts
->qcount
;i
++) {
717 uint32 u_jobid
= (ts
->queue
[i
].job
+ UNIX_JOB_START
);
718 if (jobid
== u_jobid
)
722 pjob_delete(ts
->snum
, jobid
);
728 /* maybe it hasn't been spooled yet */
730 /* if a job is not spooled and the process doesn't
731 exist then kill it. This cleans up after smbd
733 if (!process_exists(pjob
.pid
))
734 pjob_delete(ts
->snum
, jobid
);
740 for (i
=0;i
<ts
->qcount
;i
++) {
741 uint32 curr_jobid
= print_parse_jobid(ts
->queue
[i
].fs_file
);
742 if (jobid
== curr_jobid
)
746 /* The job isn't in the system queue - we have to assume it has
747 completed, so delete the database entry. */
749 if (i
== ts
->qcount
) {
750 time_t cur_t
= time(NULL
);
752 /* A race can occur between the time a job is spooled and
753 when it appears in the lpq output. This happens when
754 the job is added to printing.tdb when another smbd
755 running print_queue_update() has completed a lpq and
756 is currently traversing the printing tdb and deleting jobs.
757 A workaround is to not delete the job if it has been
758 submitted less than lp_lpqcachetime() seconds ago. */
760 if ((cur_t
- pjob
.starttime
) > lp_lpqcachetime())
761 pjob_delete(ts
->snum
, jobid
);
771 /****************************************************************************
772 Check if the print queue has been updated recently enough.
773 ****************************************************************************/
775 static void print_cache_flush(int snum
)
778 const char *printername
= lp_const_servicename(snum
);
779 struct tdb_print_db
*pdb
= get_print_db_byname(printername
);
783 slprintf(key
, sizeof(key
)-1, "CACHE/%s", printername
);
784 tdb_store_int32(pdb
->tdb
, key
, -1);
785 release_print_db(pdb
);
788 /****************************************************************************
789 Check if someone already thinks they are doing the update.
790 ****************************************************************************/
792 static pid_t
get_updating_pid(fstring printer_name
)
797 struct tdb_print_db
*pdb
= get_print_db_byname(printer_name
);
801 slprintf(keystr
, sizeof(keystr
)-1, "UPDATING/%s", printer_name
);
803 key
.dsize
= strlen(keystr
);
805 data
= tdb_fetch(pdb
->tdb
, key
);
806 release_print_db(pdb
);
807 if (!data
.dptr
|| data
.dsize
!= sizeof(pid_t
))
810 memcpy(&updating_pid
, data
.dptr
, sizeof(pid_t
));
811 SAFE_FREE(data
.dptr
);
813 if (process_exists(updating_pid
))
819 /****************************************************************************
820 Set the fact that we're doing the update, or have finished doing the update
822 ****************************************************************************/
824 static void set_updating_pid(const fstring printer_name
, BOOL
delete)
829 pid_t updating_pid
= sys_getpid();
830 struct tdb_print_db
*pdb
= get_print_db_byname(printer_name
);
835 slprintf(keystr
, sizeof(keystr
)-1, "UPDATING/%s", printer_name
);
837 key
.dsize
= strlen(keystr
);
840 tdb_delete(pdb
->tdb
, key
);
841 release_print_db(pdb
);
845 data
.dptr
= (void *)&updating_pid
;
846 data
.dsize
= sizeof(pid_t
);
848 tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
);
849 release_print_db(pdb
);
852 /****************************************************************************
853 Update the internal database from the system print queue for a queue.
854 ****************************************************************************/
856 static void print_queue_update(int snum
)
859 print_queue_struct
*queue
= NULL
;
860 print_status_struct status
;
861 print_status_struct old_status
;
862 struct printjob
*pjob
;
863 struct traverse_struct tstruct
;
864 fstring keystr
, printer_name
, cachestr
;
866 struct tdb_print_db
*pdb
;
868 fstrcpy(printer_name
, lp_const_servicename(snum
));
869 pdb
= get_print_db_byname(printer_name
);
874 * Check to see if someone else is doing this update.
875 * This is essentially a mutex on the update.
878 if (get_updating_pid(printer_name
) != -1) {
879 release_print_db(pdb
);
883 /* Lock the queue for the database update */
885 slprintf(keystr
, sizeof(keystr
) - 1, "LOCK/%s", printer_name
);
886 if (tdb_lock_bystring(pdb
->tdb
, keystr
) == -1) {
887 DEBUG(0,("print_queue_update: Failed to lock printer %s database\n", printer_name
));
888 release_print_db(pdb
);
893 * Ensure that no one else got in here.
894 * If the updating pid is still -1 then we are
898 if (get_updating_pid(printer_name
) != -1) {
900 * Someone else is doing the update, exit.
902 tdb_unlock_bystring(pdb
->tdb
, keystr
);
903 release_print_db(pdb
);
908 * We're going to do the update ourselves.
911 /* Tell others we're doing the update. */
912 set_updating_pid(printer_name
, False
);
915 * Allow others to enter and notice we're doing
919 tdb_unlock_bystring(pdb
->tdb
, keystr
);
922 * Update the cache time FIRST ! Stops others even
923 * attempting to get the lock and doing this
924 * if the lpq takes a long time.
927 slprintf(cachestr
, sizeof(cachestr
)-1, "CACHE/%s", printer_name
);
928 tdb_store_int32(pdb
->tdb
, cachestr
, (int)time(NULL
));
930 /* get the current queue using the appropriate interface */
933 qcount
= (*(current_printif
->queue_get
))(snum
, &queue
, &status
);
935 DEBUG(3, ("%d job%s in queue for %s\n", qcount
, (qcount
!= 1) ?
936 "s" : "", printer_name
));
939 any job in the internal database that is marked as spooled
940 and doesn't exist in the system queue is considered finished
941 and removed from the database
943 any job in the system database but not in the internal database
944 is added as a unix job
946 fill in any system job numbers as we go
948 for (i
=0; i
<qcount
; i
++) {
949 uint32 jobid
= print_parse_jobid(queue
[i
].fs_file
);
951 if (jobid
== (uint32
)-1) {
952 /* assume its a unix print job */
953 print_unix_job(snum
, &queue
[i
]);
957 /* we have an active SMB print job - update its status */
958 pjob
= print_job_find(snum
, jobid
);
960 /* err, somethings wrong. Probably smbd was restarted
961 with jobs in the queue. All we can do is treat them
962 like unix jobs. Pity. */
963 print_unix_job(snum
, &queue
[i
]);
967 pjob
->sysjob
= queue
[i
].job
;
968 pjob
->status
= queue
[i
].status
;
970 pjob_store(snum
, jobid
, pjob
);
973 /* now delete any queued entries that don't appear in the
975 tstruct
.queue
= queue
;
976 tstruct
.qcount
= qcount
;
978 tstruct
.total_jobs
= 0;
980 tdb_traverse(pdb
->tdb
, traverse_fn_delete
, (void *)&tstruct
);
982 SAFE_FREE(tstruct
.queue
);
984 tdb_store_int32(pdb
->tdb
, "INFO/total_jobs", tstruct
.total_jobs
);
986 if( qcount
!= get_queue_status(snum
, &old_status
))
987 DEBUG(10,("print_queue_update: queue status change %d jobs -> %d jobs for printer %s\n",
988 old_status
.qcount
, qcount
, printer_name
));
990 /* store the new queue status structure */
991 slprintf(keystr
, sizeof(keystr
)-1, "STATUS/%s", printer_name
);
993 key
.dsize
= strlen(keystr
);
995 status
.qcount
= qcount
;
996 data
.dptr
= (void *)&status
;
997 data
.dsize
= sizeof(status
);
998 tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
);
1001 * Update the cache time again. We want to do this call
1002 * as little as possible...
1005 slprintf(keystr
, sizeof(keystr
)-1, "CACHE/%s", printer_name
);
1006 tdb_store_int32(pdb
->tdb
, keystr
, (int32
)time(NULL
));
1008 /* Delete our pid from the db. */
1009 set_updating_pid(printer_name
, True
);
1010 release_print_db(pdb
);
1013 /****************************************************************************
1014 Check if a jobid is valid. It is valid if it exists in the database.
1015 ****************************************************************************/
1017 BOOL
print_job_exists(int snum
, uint32 jobid
)
1019 struct tdb_print_db
*pdb
= get_print_db_byname(lp_const_servicename(snum
));
1024 ret
= tdb_exists(pdb
->tdb
, print_key(jobid
));
1025 release_print_db(pdb
);
1029 /****************************************************************************
1030 Give the fd used for a jobid.
1031 ****************************************************************************/
1033 int print_job_fd(int snum
, uint32 jobid
)
1035 struct printjob
*pjob
= print_job_find(snum
, jobid
);
1038 /* don't allow another process to get this info - it is meaningless */
1039 if (pjob
->pid
!= local_pid
)
1044 /****************************************************************************
1045 Give the filename used for a jobid.
1046 Only valid for the process doing the spooling and when the job
1047 has not been spooled.
1048 ****************************************************************************/
1050 char *print_job_fname(int snum
, uint32 jobid
)
1052 struct printjob
*pjob
= print_job_find(snum
, jobid
);
1053 if (!pjob
|| pjob
->spooled
|| pjob
->pid
!= local_pid
)
1055 return pjob
->filename
;
1059 /****************************************************************************
1060 Give the filename used for a jobid.
1061 Only valid for the process doing the spooling and when the job
1062 has not been spooled.
1063 ****************************************************************************/
1065 NT_DEVICEMODE
*print_job_devmode(int snum
, uint32 jobid
)
1067 struct printjob
*pjob
= print_job_find(snum
, jobid
);
1072 return pjob
->nt_devmode
;
1075 /****************************************************************************
1076 Set the place in the queue for a job.
1077 ****************************************************************************/
1079 BOOL
print_job_set_place(int snum
, uint32 jobid
, int place
)
1081 DEBUG(2,("print_job_set_place not implemented yet\n"));
1085 /****************************************************************************
1086 Set the name of a job. Only possible for owner.
1087 ****************************************************************************/
1089 BOOL
print_job_set_name(int snum
, uint32 jobid
, char *name
)
1091 struct printjob
*pjob
= print_job_find(snum
, jobid
);
1092 if (!pjob
|| pjob
->pid
!= local_pid
)
1095 fstrcpy(pjob
->jobname
, name
);
1096 return pjob_store(snum
, jobid
, pjob
);
1099 /****************************************************************************
1100 Delete a print job - don't update queue.
1101 ****************************************************************************/
1103 static BOOL
print_job_delete1(int snum
, uint32 jobid
)
1105 struct printjob
*pjob
= print_job_find(snum
, jobid
);
1112 * If already deleting just return.
1115 if (pjob
->status
== LPQ_DELETING
)
1118 /* Hrm - we need to be able to cope with deleting a job before it
1119 has reached the spooler. */
1121 if (pjob
->sysjob
== -1) {
1122 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid
));
1125 /* Set the tdb entry to be deleting. */
1127 pjob
->status
= LPQ_DELETING
;
1128 pjob_store(snum
, jobid
, pjob
);
1130 if (pjob
->spooled
&& pjob
->sysjob
!= -1)
1131 result
= (*(current_printif
->job_delete
))(snum
, pjob
);
1133 /* Delete the tdb entry if the delete suceeded or the job hasn't
1137 pjob_delete(snum
, jobid
);
1139 return (result
== 0);
1142 /****************************************************************************
1143 Return true if the current user owns the print job.
1144 ****************************************************************************/
1146 static BOOL
is_owner(struct current_user
*user
, int snum
, uint32 jobid
)
1148 struct printjob
*pjob
= print_job_find(snum
, jobid
);
1154 if ((vuser
= get_valid_user_struct(user
->vuid
)) != NULL
) {
1155 return strequal(pjob
->user
, vuser
->user
.smb_name
);
1157 return strequal(pjob
->user
, uidtoname(user
->uid
));
1161 /****************************************************************************
1163 ****************************************************************************/
1165 BOOL
print_job_delete(struct current_user
*user
, int snum
, uint32 jobid
, WERROR
*errcode
)
1167 BOOL owner
, deleted
;
1172 owner
= is_owner(user
, snum
, jobid
);
1174 /* Check access against security descriptor or whether the user
1178 !print_access_check(user
, snum
, JOB_ACCESS_ADMINISTER
)) {
1179 DEBUG(3, ("delete denied by security descriptor\n"));
1180 *errcode
= WERR_ACCESS_DENIED
;
1185 * get the spooled filename of the print job
1186 * if this works, then the file has not been spooled
1187 * to the underlying print system. Just delete the
1188 * spool file & return.
1191 if ( (fname
= print_job_fname( snum
, jobid
)) != NULL
)
1193 /* remove the spool file */
1194 DEBUG(10,("print_job_delete: Removing spool file [%s]\n", fname
));
1195 if ( unlink( fname
) == -1 ) {
1196 *errcode
= map_werror_from_unix(errno
);
1203 if (!print_job_delete1(snum
, jobid
)) {
1204 *errcode
= WERR_ACCESS_DENIED
;
1208 /* force update the database and say the delete failed if the
1211 print_queue_update(snum
);
1213 deleted
= !print_job_exists(snum
, jobid
);
1215 *errcode
= WERR_ACCESS_DENIED
;
1220 /****************************************************************************
1222 ****************************************************************************/
1224 BOOL
print_job_pause(struct current_user
*user
, int snum
, uint32 jobid
, WERROR
*errcode
)
1226 struct printjob
*pjob
= print_job_find(snum
, jobid
);
1232 if (!pjob
->spooled
|| pjob
->sysjob
== -1)
1235 if (!is_owner(user
, snum
, jobid
) &&
1236 !print_access_check(user
, snum
, JOB_ACCESS_ADMINISTER
)) {
1237 DEBUG(3, ("pause denied by security descriptor\n"));
1238 *errcode
= WERR_ACCESS_DENIED
;
1242 /* need to pause the spooled entry */
1243 ret
= (*(current_printif
->job_pause
))(snum
, pjob
);
1246 *errcode
= WERR_INVALID_PARAM
;
1250 /* force update the database */
1251 print_cache_flush(snum
);
1253 /* Send a printer notify message */
1255 notify_job_status(snum
, jobid
, JOB_STATUS_PAUSED
);
1257 /* how do we tell if this succeeded? */
1262 /****************************************************************************
1264 ****************************************************************************/
1266 BOOL
print_job_resume(struct current_user
*user
, int snum
, uint32 jobid
, WERROR
*errcode
)
1268 struct printjob
*pjob
= print_job_find(snum
, jobid
);
1274 if (!pjob
->spooled
|| pjob
->sysjob
== -1)
1277 if (!is_owner(user
, snum
, jobid
) &&
1278 !print_access_check(user
, snum
, JOB_ACCESS_ADMINISTER
)) {
1279 DEBUG(3, ("resume denied by security descriptor\n"));
1280 *errcode
= WERR_ACCESS_DENIED
;
1284 ret
= (*(current_printif
->job_resume
))(snum
, pjob
);
1287 *errcode
= WERR_INVALID_PARAM
;
1291 /* force update the database */
1292 print_cache_flush(snum
);
1294 /* Send a printer notify message */
1296 notify_job_status(snum
, jobid
, JOB_STATUS_QUEUED
);
1301 /****************************************************************************
1302 Write to a print file.
1303 ****************************************************************************/
1305 int print_job_write(int snum
, uint32 jobid
, const char *buf
, int size
)
1308 struct printjob
*pjob
= print_job_find(snum
, jobid
);
1312 /* don't allow another process to get this info - it is meaningless */
1313 if (pjob
->pid
!= local_pid
)
1316 return_code
= write(pjob
->fd
, buf
, size
);
1317 if (return_code
>0) {
1319 pjob_store(snum
, jobid
, pjob
);
1324 /****************************************************************************
1325 Check if the print queue has been updated recently enough.
1326 ****************************************************************************/
1328 static BOOL
print_cache_expired(int snum
)
1331 time_t last_qscan_time
, time_now
= time(NULL
);
1332 const char *printername
= lp_const_servicename(snum
);
1333 struct tdb_print_db
*pdb
= get_print_db_byname(printername
);
1338 slprintf(key
, sizeof(key
), "CACHE/%s", printername
);
1339 last_qscan_time
= (time_t)tdb_fetch_int32(pdb
->tdb
, key
);
1342 * Invalidate the queue for 3 reasons.
1343 * (1). last queue scan time == -1.
1344 * (2). Current time - last queue scan time > allowed cache time.
1345 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1346 * This last test picks up machines for which the clock has been moved
1347 * forward, an lpq scan done and then the clock moved back. Otherwise
1348 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1351 if (last_qscan_time
== ((time_t)-1) || (time_now
- last_qscan_time
) >= lp_lpqcachetime() ||
1352 last_qscan_time
> (time_now
+ MAX_CACHE_VALID_TIME
)) {
1353 DEBUG(3, ("print cache expired for queue %s \
1354 (last_qscan_time = %d, time now = %d, qcachetime = %d)\n", printername
,
1355 (int)last_qscan_time
, (int)time_now
, (int)lp_lpqcachetime() ));
1356 release_print_db(pdb
);
1359 release_print_db(pdb
);
1363 /****************************************************************************
1364 Get the queue status - do not update if db is out of date.
1365 ****************************************************************************/
1367 static int get_queue_status(int snum
, print_status_struct
*status
)
1371 const char *printername
= lp_const_servicename(snum
);
1372 struct tdb_print_db
*pdb
= get_print_db_byname(printername
);
1376 ZERO_STRUCTP(status
);
1377 slprintf(keystr
, sizeof(keystr
)-1, "STATUS/%s", printername
);
1379 key
.dsize
= strlen(keystr
);
1380 data
= tdb_fetch(pdb
->tdb
, key
);
1381 release_print_db(pdb
);
1383 if (data
.dsize
== sizeof(print_status_struct
)) {
1384 memcpy(status
, data
.dptr
, sizeof(print_status_struct
));
1386 SAFE_FREE(data
.dptr
);
1388 return status
->qcount
;
1391 /****************************************************************************
1392 Determine the number of jobs in a queue.
1393 ****************************************************************************/
1395 int print_queue_length(int snum
, print_status_struct
*pstatus
)
1397 print_status_struct status
;
1400 /* make sure the database is up to date */
1401 if (print_cache_expired(snum
))
1402 print_queue_update(snum
);
1404 /* also fetch the queue status */
1405 memset(&status
, 0, sizeof(status
));
1406 len
= get_queue_status(snum
, &status
);
1412 /***************************************************************************
1413 Start spooling a job - return the jobid.
1414 ***************************************************************************/
1416 uint32
print_job_start(struct current_user
*user
, int snum
, char *jobname
, NT_DEVICEMODE
*nt_devmode
)
1420 struct printjob pjob
;
1424 const char *printername
= lp_const_servicename(snum
);
1425 struct tdb_print_db
*pdb
= get_print_db_byname(printername
);
1426 BOOL pdb_locked
= False
;
1433 if (!print_access_check(user
, snum
, PRINTER_ACCESS_USE
)) {
1434 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
1435 release_print_db(pdb
);
1439 if (!print_time_access_check(snum
)) {
1440 DEBUG(3, ("print_job_start: job start denied by time check\n"));
1441 release_print_db(pdb
);
1445 path
= lp_pathname(snum
);
1447 /* see if we have sufficient disk space */
1448 if (lp_minprintspace(snum
)) {
1449 SMB_BIG_UINT dspace
, dsize
;
1450 if (sys_fsusage(path
, &dspace
, &dsize
) == 0 &&
1451 dspace
< 2*(SMB_BIG_UINT
)lp_minprintspace(snum
)) {
1452 DEBUG(3, ("print_job_start: disk space check failed.\n"));
1453 release_print_db(pdb
);
1459 /* for autoloaded printers, check that the printcap entry still exists */
1460 if (lp_autoloaded(snum
) && !pcap_printername_ok(lp_const_servicename(snum
), NULL
)) {
1461 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum
) ));
1462 release_print_db(pdb
);
1467 /* Insure the maximum queue size is not violated */
1468 if (lp_maxprintjobs(snum
) && (njobs
= print_queue_length(snum
,NULL
)) > lp_maxprintjobs(snum
)) {
1469 DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per queue (%d).\n",
1470 njobs
, lp_maxprintjobs(snum
) ));
1471 release_print_db(pdb
);
1476 /* Lock the database */
1477 if (tdb_lock_bystring(pdb
->tdb
, "INFO/nextjob") == -1) {
1478 DEBUG(0,("print_job_start: failed to lock printing database %s\n", printername
));
1479 release_print_db(pdb
);
1485 next_jobid
= tdb_fetch_int32(pdb
->tdb
, "INFO/nextjob");
1486 if (next_jobid
== -1)
1489 for (jobid
= NEXT_JOBID(next_jobid
); jobid
!= next_jobid
; jobid
= NEXT_JOBID(jobid
)) {
1490 if (!print_job_exists(snum
, jobid
))
1494 if (jobid
== next_jobid
) {
1495 DEBUG(3, ("print_job_start: jobid (%d)==next_jobid(%d).\n",
1496 jobid
, next_jobid
));
1501 /* Store a dummy placeholder. This must be quick as we have the lock. */
1506 if (tdb_store(pdb
->tdb
, print_key(jobid
), dum
, TDB_INSERT
) == -1) {
1507 DEBUG(3, ("print_job_start: jobid (%d) failed to store placeholder.\n",
1514 if (tdb_store_int32(pdb
->tdb
, "INFO/nextjob", jobid
)==-1) {
1515 DEBUG(3, ("print_job_start: failed to store INFO/nextjob.\n"));
1520 /* We've finished with the INFO/nextjob lock. */
1521 tdb_unlock_bystring(pdb
->tdb
, "INFO/nextjob");
1524 /* create the database entry */
1528 pjob
.pid
= local_pid
;
1531 pjob
.starttime
= time(NULL
);
1532 pjob
.status
= LPQ_SPOOLING
;
1534 pjob
.spooled
= False
;
1536 pjob
.nt_devmode
= nt_devmode
;
1538 fstrcpy(pjob
.jobname
, jobname
);
1540 if ((vuser
= get_valid_user_struct(user
->vuid
)) != NULL
) {
1541 fstrcpy(pjob
.user
, vuser
->user
.smb_name
);
1543 fstrcpy(pjob
.user
, uidtoname(user
->uid
));
1546 fstrcpy(pjob
.queuename
, lp_const_servicename(snum
));
1548 /* we have a job entry - now create the spool file */
1549 slprintf(pjob
.filename
, sizeof(pjob
.filename
)-1, "%s/%s%.8u.XXXXXX",
1550 path
, PRINT_SPOOL_PREFIX
, (unsigned int)jobid
);
1551 pjob
.fd
= smb_mkstemp(pjob
.filename
);
1553 if (pjob
.fd
== -1) {
1554 if (errno
== EACCES
) {
1555 /* Common setup error, force a report. */
1556 DEBUG(0, ("print_job_start: insufficient permissions \
1557 to open spool file %s.\n", pjob
.filename
));
1559 /* Normal case, report at level 3 and above. */
1560 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob
.filename
));
1561 DEBUGADD(3, ("errno = %d (%s).\n", errno
, strerror(errno
)));
1566 pjob_store(snum
, jobid
, &pjob
);
1568 release_print_db(pdb
);
1571 * If the printer is marked as postscript output a leading
1572 * file identifier to ensure the file is treated as a raw
1574 * This has a similar effect as CtrlD=0 in WIN.INI file.
1575 * tim@fsg.com 09/06/94
1577 if (lp_postscript(snum
)) {
1578 print_job_write(snum
, jobid
, "%!\n",3);
1585 pjob_delete(snum
, jobid
);
1588 tdb_unlock_bystring(pdb
->tdb
, "INFO/nextjob");
1589 release_print_db(pdb
);
1591 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno
) ));
1595 /****************************************************************************
1596 Update the number of pages spooled to jobid
1597 ****************************************************************************/
1599 void print_job_endpage(int snum
, uint32 jobid
)
1601 struct printjob
*pjob
= print_job_find(snum
, jobid
);
1604 /* don't allow another process to get this info - it is meaningless */
1605 if (pjob
->pid
!= local_pid
)
1609 pjob_store(snum
, jobid
, pjob
);
1612 /****************************************************************************
1613 Print a file - called on closing the file. This spools the job.
1614 If normal close is false then we're tearing down the jobs - treat as an
1616 ****************************************************************************/
1618 BOOL
print_job_end(int snum
, uint32 jobid
, BOOL normal_close
)
1620 struct printjob
*pjob
= print_job_find(snum
, jobid
);
1622 SMB_STRUCT_STAT sbuf
;
1627 if (pjob
->spooled
|| pjob
->pid
!= local_pid
)
1630 if (normal_close
&& (sys_fstat(pjob
->fd
, &sbuf
) == 0)) {
1631 pjob
->size
= sbuf
.st_size
;
1637 * Not a normal close or we couldn't stat the job file,
1638 * so something has gone wrong. Cleanup.
1642 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid
));
1646 /* Technically, this is not quite right. If the printer has a separator
1647 * page turned on, the NT spooler prints the separator page even if the
1648 * print job is 0 bytes. 010215 JRR */
1649 if (pjob
->size
== 0 || pjob
->status
== LPQ_DELETING
) {
1650 /* don't bother spooling empty files or something being deleted. */
1651 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
1652 pjob
->filename
, pjob
->size
? "deleted" : "zero length" ));
1653 unlink(pjob
->filename
);
1654 pjob_delete(snum
, jobid
);
1658 ret
= (*(current_printif
->job_submit
))(snum
, pjob
);
1663 /* The print job has been sucessfully handed over to the back-end */
1665 pjob
->spooled
= True
;
1666 pjob
->status
= LPQ_QUEUED
;
1667 pjob_store(snum
, jobid
, pjob
);
1669 /* make sure the database is up to date */
1670 if (print_cache_expired(snum
))
1671 print_queue_update(snum
);
1677 /* The print job was not succesfully started. Cleanup */
1678 /* Still need to add proper error return propagation! 010122:JRR */
1679 unlink(pjob
->filename
);
1680 pjob_delete(snum
, jobid
);
1684 /****************************************************************************
1685 Utility fn to enumerate the print queue.
1686 ****************************************************************************/
1688 static int traverse_fn_queue(TDB_CONTEXT
*t
, TDB_DATA key
, TDB_DATA data
, void *state
)
1690 struct traverse_struct
*ts
= (struct traverse_struct
*)state
;
1691 struct printjob pjob
;
1697 if ( key
.dsize
!= sizeof(jobid
) )
1700 memcpy(&jobid
, key
.dptr
, sizeof(jobid
));
1702 if ( unpack_pjob( data
.dptr
, data
.dsize
, &pjob
) == -1 )
1704 free_nt_devicemode( &pjob
.nt_devmode
);
1706 /* maybe it isn't for this queue */
1707 if (ts
->snum
!= lp_servicenumber(pjob
.queuename
))
1710 if (ts
->qcount
>= ts
->maxcount
)
1715 ts
->queue
[i
].job
= jobid
;
1716 ts
->queue
[i
].size
= pjob
.size
;
1717 ts
->queue
[i
].page_count
= pjob
.page_count
;
1718 ts
->queue
[i
].status
= pjob
.status
;
1719 ts
->queue
[i
].priority
= 1;
1720 ts
->queue
[i
].time
= pjob
.starttime
;
1721 fstrcpy(ts
->queue
[i
].fs_user
, pjob
.user
);
1722 fstrcpy(ts
->queue
[i
].fs_file
, pjob
.jobname
);
1729 struct traverse_count_struct
{
1733 /****************************************************************************
1734 Utility fn to count the number of entries in the print queue.
1735 ****************************************************************************/
1737 static int traverse_count_fn_queue(TDB_CONTEXT
*t
, TDB_DATA key
, TDB_DATA data
, void *state
)
1739 struct traverse_count_struct
*ts
= (struct traverse_count_struct
*)state
;
1740 struct printjob pjob
;
1745 if ( key
.dsize
!= sizeof(jobid
) )
1748 memcpy(&jobid
, key
.dptr
, sizeof(jobid
));
1750 if ( unpack_pjob( data
.dptr
, data
.dsize
, &pjob
) == -1 )
1753 free_nt_devicemode( &pjob
.nt_devmode
);
1755 /* maybe it isn't for this queue - this cannot happen with the tdb/printer code. JRA */
1756 if (ts
->snum
!= lp_servicenumber(pjob
.queuename
))
1764 /****************************************************************************
1765 Sort print jobs by submittal time.
1766 ****************************************************************************/
1768 static int printjob_comp(print_queue_struct
*j1
, print_queue_struct
*j2
)
1779 /* Sort on job start time */
1781 if (j1
->time
== j2
->time
)
1783 return (j1
->time
> j2
->time
) ? 1 : -1;
1786 /****************************************************************************
1787 Get a printer queue listing.
1788 ****************************************************************************/
1790 int print_queue_status(int snum
,
1791 print_queue_struct
**queue
,
1792 print_status_struct
*status
)
1794 struct traverse_struct tstruct
;
1795 struct traverse_count_struct tsc
;
1798 const char *printername
= lp_const_servicename(snum
);
1799 struct tdb_print_db
*pdb
= get_print_db_byname(printername
);
1806 /* make sure the database is up to date */
1807 if (print_cache_expired(snum
))
1808 print_queue_update(snum
);
1811 * Fetch the queue status. We must do this first, as there may
1812 * be no jobs in the queue.
1814 ZERO_STRUCTP(status
);
1815 slprintf(keystr
, sizeof(keystr
)-1, "STATUS/%s", printername
);
1817 key
.dsize
= strlen(keystr
);
1818 data
= tdb_fetch(pdb
->tdb
, key
);
1820 if (data
.dsize
== sizeof(*status
)) {
1821 memcpy(status
, data
.dptr
, sizeof(*status
));
1823 SAFE_FREE(data
.dptr
);
1827 * Now, fetch the print queue information. We first count the number
1828 * of entries, and then only retrieve the queue if necessary.
1833 tdb_traverse(pdb
->tdb
, traverse_count_fn_queue
, (void *)&tsc
);
1835 if (tsc
.count
== 0) {
1836 release_print_db(pdb
);
1840 /* Allocate the queue size. */
1841 if ((tstruct
.queue
= (print_queue_struct
*)
1842 malloc(sizeof(print_queue_struct
)*tsc
.count
)) == NULL
) {
1843 release_print_db(pdb
);
1848 * Fill in the queue.
1849 * We need maxcount as the queue size may have changed between
1850 * the two calls to tdb_traverse.
1853 tstruct
.maxcount
= tsc
.count
;
1854 tstruct
.snum
= snum
;
1856 tdb_traverse(pdb
->tdb
, traverse_fn_queue
, (void *)&tstruct
);
1857 release_print_db(pdb
);
1859 /* Sort the queue by submission time otherwise they are displayed
1862 qsort(tstruct
.queue
, tstruct
.qcount
, sizeof(print_queue_struct
),
1863 QSORT_CAST(printjob_comp
));
1865 *queue
= tstruct
.queue
;
1866 return tstruct
.qcount
;
1869 /****************************************************************************
1870 Turn a queue name into a snum.
1871 ****************************************************************************/
1873 int print_queue_snum(const char *qname
)
1875 int snum
= lp_servicenumber(qname
);
1876 if (snum
== -1 || !lp_print_ok(snum
))
1881 /****************************************************************************
1883 ****************************************************************************/
1885 BOOL
print_queue_pause(struct current_user
*user
, int snum
, WERROR
*errcode
)
1889 if (!print_access_check(user
, snum
, PRINTER_ACCESS_ADMINISTER
)) {
1890 *errcode
= WERR_ACCESS_DENIED
;
1894 ret
= (*(current_printif
->queue_pause
))(snum
);
1897 *errcode
= WERR_INVALID_PARAM
;
1901 /* force update the database */
1902 print_cache_flush(snum
);
1904 /* Send a printer notify message */
1906 notify_printer_status(snum
, PRINTER_STATUS_PAUSED
);
1911 /****************************************************************************
1913 ****************************************************************************/
1915 BOOL
print_queue_resume(struct current_user
*user
, int snum
, WERROR
*errcode
)
1919 if (!print_access_check(user
, snum
, PRINTER_ACCESS_ADMINISTER
)) {
1920 *errcode
= WERR_ACCESS_DENIED
;
1924 ret
= (*(current_printif
->queue_resume
))(snum
);
1927 *errcode
= WERR_INVALID_PARAM
;
1931 /* make sure the database is up to date */
1932 if (print_cache_expired(snum
))
1933 print_queue_update(snum
);
1935 /* Send a printer notify message */
1937 notify_printer_status(snum
, PRINTER_STATUS_OK
);
1942 /****************************************************************************
1943 Purge a queue - implemented by deleting all jobs that we can delete.
1944 ****************************************************************************/
1946 BOOL
print_queue_purge(struct current_user
*user
, int snum
, WERROR
*errcode
)
1948 print_queue_struct
*queue
;
1949 print_status_struct status
;
1953 /* Force and update so the count is accurate (i.e. not a cached count) */
1954 print_queue_update(snum
);
1956 can_job_admin
= print_access_check(user
, snum
, JOB_ACCESS_ADMINISTER
);
1957 njobs
= print_queue_status(snum
, &queue
, &status
);
1959 for (i
=0;i
<njobs
;i
++) {
1960 BOOL owner
= is_owner(user
, snum
, queue
[i
].job
);
1962 if (owner
|| can_job_admin
) {
1963 print_job_delete1(snum
, queue
[i
].job
);