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 #define MAX_PRINT_DBS_OPEN 1
137 struct tdb_print_db
{
138 struct tdb_print_db
*next
, *prev
;
140 fstring printer_name
;
143 static struct tdb_print_db
*print_db_head
;
145 /****************************************************************************
146 Function to find or create the printer specific job tdb given a printername.
147 Limits the number of tdb's open to MAX_PRINT_DBS_OPEN.
148 ****************************************************************************/
150 static struct tdb_print_db
*get_print_db_byname(const char *printername
)
152 struct tdb_print_db
*p
, *last_entry
;
154 pstring printdb_path
;
156 for (p
= print_db_head
, last_entry
= print_db_head
; p
; p
= p
->next
) {
157 if (p
->tdb
&& strequal(p
->printer_name
, printername
)) {
158 DLIST_PROMOTE(print_db_head
, p
);
165 if (num_open
>= MAX_PRINT_DBS_OPEN
) {
166 /* Recycle the last entry. */
167 DLIST_PROMOTE(print_db_head
, last_entry
);
168 if (print_db_head
->tdb
) {
169 if (tdb_close(print_db_head
->tdb
)) {
170 DEBUG(0,("get_print_db: Failed to close tdb for printer %s\n",
171 print_db_head
->printer_name
));
179 p
= (struct tdb_print_db
*)malloc(sizeof(struct tdb_print_db
));
181 DEBUG(0,("get_print_db: malloc fail !\n"));
185 DLIST_ADD(print_db_head
, p
);
188 pstrcpy(printdb_path
, lock_path("printing/"));
189 pstrcat(printdb_path
, printername
);
190 pstrcat(printdb_path
, ".tdb");
193 p
->tdb
= tdb_open_log(printdb_path
, 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
197 DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
199 DLIST_REMOVE(print_db_head
, p
);
203 fstrcpy(p
->printer_name
, printername
);
207 /****************************************************************************
208 Initialise the printing backend. Called once at startup.
209 Does not survive a fork
210 ****************************************************************************/
212 BOOL
print_backend_init(void)
214 char *sversion
= "INFO/version";
215 pstring printing_path
;
216 int services
= lp_numservices();
219 if (local_pid
== sys_getpid())
222 unlink(lock_path("printing.tdb"));
223 pstrcpy(printing_path
,lock_path("printing"));
224 mkdir(printing_path
,0755);
226 local_pid
= sys_getpid();
228 /* handle a Samba upgrade */
230 for (snum
= 0; snum
< services
; snum
++) {
231 struct tdb_print_db
*pdb
;
232 if (!lp_print_ok(snum
))
235 pdb
= get_print_db_byname(lp_const_servicename(snum
));
238 tdb_lock_bystring(pdb
->tdb
, sversion
);
239 if (tdb_fetch_int32(pdb
->tdb
, sversion
) != PRINT_DATABASE_VERSION
) {
240 tdb_traverse(pdb
->tdb
, tdb_traverse_delete_fn
, NULL
);
241 tdb_store_int32(pdb
->tdb
, sversion
, PRINT_DATABASE_VERSION
);
243 tdb_unlock_bystring(pdb
->tdb
, sversion
);
246 /* select the appropriate printing interface... */
248 if (strcmp(lp_printcapname(), "cups") == 0)
249 current_printif
= &cups_printif
;
250 #endif /* HAVE_CUPS */
252 /* do NT print initialization... */
253 return nt_printing_init();
256 /****************************************************************************
257 Shut down printing backend. Called once at shutdown to close the tdb.
258 ****************************************************************************/
260 void printing_end(void)
262 struct tdb_print_db
*p
;
264 for (p
= print_db_head
; p
; ) {
265 struct tdb_print_db
*next_p
= p
->next
;
268 DLIST_REMOVE(print_db_head
, p
);
274 /****************************************************************************
275 Useful function to generate a tdb key.
276 ****************************************************************************/
278 static TDB_DATA
print_key(uint32 jobid
)
284 ret
.dptr
= (void *)&j
;
285 ret
.dsize
= sizeof(j
);
289 /****************************************************************************
290 Useful function to find a print job in the database.
291 ****************************************************************************/
293 static struct printjob
*print_job_find(int snum
, uint32 jobid
)
295 static struct printjob pjob
;
297 struct tdb_print_db
*pdb
= get_print_db_byname(lp_const_servicename(snum
));
302 ret
= tdb_fetch(pdb
->tdb
, print_key(jobid
));
303 if (!ret
.dptr
|| ret
.dsize
!= sizeof(pjob
))
306 memcpy(&pjob
, ret
.dptr
, sizeof(pjob
));
311 /* Convert a unix jobid to a smb jobid */
313 static uint32 sysjob_to_jobid_value
;
315 static int unixjob_traverse_fn(TDB_CONTEXT
*the_tdb
, TDB_DATA key
,
316 TDB_DATA data
, void *state
)
318 struct printjob
*pjob
= (struct printjob
*)data
.dptr
;
319 int *sysjob
= (int *)state
;
321 if (key
.dsize
!= sizeof(uint32
))
324 if (*sysjob
== pjob
->sysjob
) {
325 uint32
*jobid
= (uint32
*)key
.dptr
;
327 sysjob_to_jobid_value
= *jobid
;
334 /****************************************************************************
335 This is a *horribly expensive call as we have to iterate through all the
336 current printer tdb's. Don't do this often ! JRA.
337 ****************************************************************************/
339 uint32
sysjob_to_jobid(int unix_jobid
)
341 int services
= lp_numservices();
344 sysjob_to_jobid_value
= (uint32
)-1;
346 for (snum
= 0; snum
< services
; snum
++) {
347 struct tdb_print_db
*pdb
;
348 if (!lp_print_ok(snum
))
350 pdb
= get_print_db_byname(lp_const_servicename(snum
));
352 tdb_traverse(pdb
->tdb
, unixjob_traverse_fn
, &unix_jobid
);
353 if (sysjob_to_jobid_value
!= (uint32
)-1)
354 return sysjob_to_jobid_value
;
359 /****************************************************************************
360 Send notifications based on what has changed after a pjob_store.
361 ****************************************************************************/
365 uint32 spoolss_status
;
366 } lpq_to_spoolss_status_map
[] = {
367 { LPQ_QUEUED
, JOB_STATUS_QUEUED
},
368 { LPQ_PAUSED
, JOB_STATUS_PAUSED
},
369 { LPQ_SPOOLING
, JOB_STATUS_SPOOLING
},
370 { LPQ_PRINTING
, JOB_STATUS_PRINTING
},
371 { LPQ_DELETING
, JOB_STATUS_DELETING
},
372 { LPQ_OFFLINE
, JOB_STATUS_OFFLINE
},
373 { LPQ_PAPEROUT
, JOB_STATUS_PAPEROUT
},
374 { LPQ_PRINTED
, JOB_STATUS_PRINTED
},
375 { LPQ_DELETED
, JOB_STATUS_DELETED
},
376 { LPQ_BLOCKED
, JOB_STATUS_BLOCKED
},
377 { LPQ_USER_INTERVENTION
, JOB_STATUS_USER_INTERVENTION
},
381 /* Convert a lpq status value stored in printing.tdb into the
382 appropriate win32 API constant. */
384 static uint32
map_to_spoolss_status(uint32 lpq_status
)
388 while (lpq_to_spoolss_status_map
[i
].lpq_status
!= -1) {
389 if (lpq_to_spoolss_status_map
[i
].lpq_status
== lpq_status
)
390 return lpq_to_spoolss_status_map
[i
].spoolss_status
;
397 static void pjob_store_notify(int snum
, uint32 jobid
, struct printjob
*old_data
,
398 struct printjob
*new_data
)
400 BOOL new_job
= False
;
405 /* Notify the job name first */
407 if (new_job
|| !strequal(old_data
->jobname
, new_data
->jobname
))
408 notify_job_name(snum
, jobid
, new_data
->jobname
);
410 /* Job attributes that can't be changed. We only send
411 notification for these on a new job. */
414 notify_job_submitted(snum
, jobid
, new_data
->starttime
);
415 notify_job_username(snum
, jobid
, new_data
->user
);
418 /* Job attributes of a new job or attributes that can be
421 if (new_job
|| old_data
->status
!= new_data
->status
)
422 notify_job_status(snum
, jobid
, map_to_spoolss_status(new_data
->status
));
424 if (new_job
|| old_data
->size
!= new_data
->size
)
425 notify_job_total_bytes(snum
, jobid
, new_data
->size
);
427 if (new_job
|| old_data
->page_count
!= new_data
->page_count
)
428 notify_job_total_pages(snum
, jobid
, new_data
->page_count
);
431 /****************************************************************************
432 Store a job structure back to the database.
433 ****************************************************************************/
435 static BOOL
pjob_store(int snum
, uint32 jobid
, struct printjob
*pjob
)
437 TDB_DATA old_data
, new_data
;
439 struct tdb_print_db
*pdb
= get_print_db_byname(lp_const_servicename(snum
));
446 old_data
= tdb_fetch(pdb
->tdb
, print_key(jobid
));
450 new_data
.dptr
= (void *)pjob
;
451 new_data
.dsize
= sizeof(*pjob
);
452 ret
= (tdb_store(pdb
->tdb
, print_key(jobid
), new_data
, TDB_REPLACE
) == 0);
454 /* Send notify updates for what has changed */
456 if (ret
&& (old_data
.dsize
== 0 || old_data
.dsize
== sizeof(*pjob
))) {
458 snum
, jobid
, (struct printjob
*)old_data
.dptr
,
459 (struct printjob
*)new_data
.dptr
);
466 /****************************************************************************
467 Remove a job structure from the database.
468 ****************************************************************************/
470 static void pjob_delete(int snum
, uint32 jobid
)
472 struct printjob
*pjob
= print_job_find(snum
, jobid
);
473 uint32 job_status
= 0;
474 struct tdb_print_db
*pdb
= get_print_db_byname(lp_const_servicename(snum
));
480 DEBUG(5, ("pjob_delete(): we were asked to delete nonexistent job %u\n",
481 (unsigned int)jobid
));
485 /* Send a notification that a job has been deleted */
487 job_status
= map_to_spoolss_status(pjob
->status
);
489 /* We must cycle through JOB_STATUS_DELETING and
490 JOB_STATUS_DELETED for the port monitor to delete the job
493 job_status
|= JOB_STATUS_DELETING
;
494 notify_job_status(snum
, jobid
, job_status
);
496 job_status
|= JOB_STATUS_DELETED
;
497 notify_job_status(snum
, jobid
, job_status
);
499 /* Remove from printing.tdb */
501 tdb_delete(pdb
->tdb
, print_key(jobid
));
502 rap_jobid_delete(snum
, jobid
);
505 /****************************************************************************
506 Parse a file name from the system spooler to generate a jobid.
507 ****************************************************************************/
509 static uint32
print_parse_jobid(char *fname
)
513 if (strncmp(fname
,PRINT_SPOOL_PREFIX
,strlen(PRINT_SPOOL_PREFIX
)) != 0)
515 fname
+= strlen(PRINT_SPOOL_PREFIX
);
521 return (uint32
)jobid
;
524 /****************************************************************************
525 List a unix job in the print database.
526 ****************************************************************************/
528 static void print_unix_job(int snum
, print_queue_struct
*q
)
530 uint32 jobid
= q
->job
+ UNIX_JOB_START
;
531 struct printjob pj
, *old_pj
;
533 /* Preserve the timestamp on an existing unix print job */
535 old_pj
= print_job_find(snum
, jobid
);
542 pj
.starttime
= old_pj
? old_pj
->starttime
: q
->time
;
543 pj
.status
= q
->status
;
547 fstrcpy(pj
.filename
, "");
548 fstrcpy(pj
.jobname
, q
->fs_file
);
549 fstrcpy(pj
.user
, q
->fs_user
);
550 fstrcpy(pj
.queuename
, lp_const_servicename(snum
));
552 pjob_store(snum
, jobid
, &pj
);
556 struct traverse_struct
{
557 print_queue_struct
*queue
;
558 int qcount
, snum
, maxcount
, total_jobs
;
561 /****************************************************************************
562 Utility fn to delete any jobs that are no longer active.
563 ****************************************************************************/
565 static int traverse_fn_delete(TDB_CONTEXT
*t
, TDB_DATA key
, TDB_DATA data
, void *state
)
567 struct traverse_struct
*ts
= (struct traverse_struct
*)state
;
568 struct printjob pjob
;
572 if (data
.dsize
!= sizeof(pjob
) || key
.dsize
!= sizeof(jobid
))
574 memcpy(&jobid
, key
.dptr
, sizeof(jobid
));
575 memcpy(&pjob
, data
.dptr
, sizeof(pjob
));
577 if (ts
->snum
!= lp_servicenumber(pjob
.queuename
)) {
578 /* this isn't for the queue we are looking at - this cannot happen with the split tdb's. JRA */
583 /* remove a unix job if it isn't in the system queue any more */
585 for (i
=0;i
<ts
->qcount
;i
++) {
586 uint32 u_jobid
= (ts
->queue
[i
].job
+ UNIX_JOB_START
);
587 if (jobid
== u_jobid
)
591 pjob_delete(ts
->snum
, jobid
);
597 /* maybe it hasn't been spooled yet */
599 /* if a job is not spooled and the process doesn't
600 exist then kill it. This cleans up after smbd
602 if (!process_exists(pjob
.pid
))
603 pjob_delete(ts
->snum
, jobid
);
609 for (i
=0;i
<ts
->qcount
;i
++) {
610 uint32 curr_jobid
= print_parse_jobid(ts
->queue
[i
].fs_file
);
611 if (jobid
== curr_jobid
)
615 /* The job isn't in the system queue - we have to assume it has
616 completed, so delete the database entry. */
618 if (i
== ts
->qcount
) {
619 time_t cur_t
= time(NULL
);
621 /* A race can occur between the time a job is spooled and
622 when it appears in the lpq output. This happens when
623 the job is added to printing.tdb when another smbd
624 running print_queue_update() has completed a lpq and
625 is currently traversing the printing tdb and deleting jobs.
626 A workaround is to not delete the job if it has been
627 submitted less than lp_lpqcachetime() seconds ago. */
629 if ((cur_t
- pjob
.starttime
) > lp_lpqcachetime())
630 pjob_delete(ts
->snum
, jobid
);
640 /****************************************************************************
641 Check if the print queue has been updated recently enough.
642 ****************************************************************************/
644 static void print_cache_flush(int snum
)
647 const char *printername
= lp_const_servicename(snum
);
648 struct tdb_print_db
*pdb
= get_print_db_byname(printername
);
652 slprintf(key
, sizeof(key
)-1, "CACHE/%s", printername
);
653 tdb_store_int32(pdb
->tdb
, key
, -1);
656 /****************************************************************************
657 Check if someone already thinks they are doing the update.
658 ****************************************************************************/
660 static pid_t
get_updating_pid(fstring printer_name
)
665 struct tdb_print_db
*pdb
= get_print_db_byname(printer_name
);
669 slprintf(keystr
, sizeof(keystr
)-1, "UPDATING/%s", printer_name
);
671 key
.dsize
= strlen(keystr
);
673 data
= tdb_fetch(pdb
->tdb
, key
);
674 if (!data
.dptr
|| data
.dsize
!= sizeof(pid_t
))
677 memcpy(&updating_pid
, data
.dptr
, sizeof(pid_t
));
678 SAFE_FREE(data
.dptr
);
680 if (process_exists(updating_pid
))
686 /****************************************************************************
687 Set the fact that we're doing the update, or have finished doing the update
689 ****************************************************************************/
691 static void set_updating_pid(const fstring printer_name
, BOOL
delete)
696 pid_t updating_pid
= sys_getpid();
697 struct tdb_print_db
*pdb
= get_print_db_byname(printer_name
);
702 slprintf(keystr
, sizeof(keystr
)-1, "UPDATING/%s", printer_name
);
704 key
.dsize
= strlen(keystr
);
707 tdb_delete(pdb
->tdb
, key
);
711 data
.dptr
= (void *)&updating_pid
;
712 data
.dsize
= sizeof(pid_t
);
714 tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
);
717 /****************************************************************************
718 Update the internal database from the system print queue for a queue.
719 ****************************************************************************/
721 static void print_queue_update(int snum
)
724 print_queue_struct
*queue
= NULL
;
725 print_status_struct status
;
726 print_status_struct old_status
;
727 struct printjob
*pjob
;
728 struct traverse_struct tstruct
;
729 fstring keystr
, printer_name
, cachestr
;
731 struct tdb_print_db
*pdb
;
733 fstrcpy(printer_name
, lp_const_servicename(snum
));
734 pdb
= get_print_db_byname(printer_name
);
739 * Check to see if someone else is doing this update.
740 * This is essentially a mutex on the update.
743 if (get_updating_pid(printer_name
) != -1)
746 /* Lock the queue for the database update */
748 slprintf(keystr
, sizeof(keystr
) - 1, "LOCK/%s", printer_name
);
749 tdb_lock_bystring(pdb
->tdb
, keystr
);
752 * Ensure that no one else got in here.
753 * If the updating pid is still -1 then we are
757 if (get_updating_pid(printer_name
) != -1) {
759 * Someone else is doing the update, exit.
761 tdb_unlock_bystring(pdb
->tdb
, keystr
);
766 * We're going to do the update ourselves.
769 /* Tell others we're doing the update. */
770 set_updating_pid(printer_name
, False
);
773 * Allow others to enter and notice we're doing
777 tdb_unlock_bystring(pdb
->tdb
, keystr
);
780 * Update the cache time FIRST ! Stops others even
781 * attempting to get the lock and doing this
782 * if the lpq takes a long time.
785 slprintf(cachestr
, sizeof(cachestr
)-1, "CACHE/%s", printer_name
);
786 tdb_store_int32(pdb
->tdb
, cachestr
, (int)time(NULL
));
788 /* get the current queue using the appropriate interface */
791 qcount
= (*(current_printif
->queue_get
))(snum
, &queue
, &status
);
793 DEBUG(3, ("%d job%s in queue for %s\n", qcount
, (qcount
!= 1) ?
794 "s" : "", printer_name
));
797 any job in the internal database that is marked as spooled
798 and doesn't exist in the system queue is considered finished
799 and removed from the database
801 any job in the system database but not in the internal database
802 is added as a unix job
804 fill in any system job numbers as we go
806 for (i
=0; i
<qcount
; i
++) {
807 uint32 jobid
= print_parse_jobid(queue
[i
].fs_file
);
809 if (jobid
== (uint32
)-1) {
810 /* assume its a unix print job */
811 print_unix_job(snum
, &queue
[i
]);
815 /* we have an active SMB print job - update its status */
816 pjob
= print_job_find(snum
, jobid
);
818 /* err, somethings wrong. Probably smbd was restarted
819 with jobs in the queue. All we can do is treat them
820 like unix jobs. Pity. */
821 print_unix_job(snum
, &queue
[i
]);
825 pjob
->sysjob
= queue
[i
].job
;
826 pjob
->status
= queue
[i
].status
;
828 pjob_store(snum
, jobid
, pjob
);
831 /* now delete any queued entries that don't appear in the
833 tstruct
.queue
= queue
;
834 tstruct
.qcount
= qcount
;
836 tstruct
.total_jobs
= 0;
838 tdb_traverse(pdb
->tdb
, traverse_fn_delete
, (void *)&tstruct
);
840 SAFE_FREE(tstruct
.queue
);
842 tdb_store_int32(pdb
->tdb
, "INFO/total_jobs", tstruct
.total_jobs
);
844 if( qcount
!= get_queue_status(snum
, &old_status
))
845 DEBUG(10,("print_queue_update: queue status change %d jobs -> %d jobs for printer %s\n",
846 old_status
.qcount
, qcount
, printer_name
));
848 /* store the new queue status structure */
849 slprintf(keystr
, sizeof(keystr
)-1, "STATUS/%s", printer_name
);
851 key
.dsize
= strlen(keystr
);
853 status
.qcount
= qcount
;
854 data
.dptr
= (void *)&status
;
855 data
.dsize
= sizeof(status
);
856 tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
);
859 * Update the cache time again. We want to do this call
860 * as little as possible...
863 slprintf(keystr
, sizeof(keystr
)-1, "CACHE/%s", printer_name
);
864 tdb_store_int32(pdb
->tdb
, keystr
, (int32
)time(NULL
));
866 /* Delete our pid from the db. */
867 set_updating_pid(printer_name
, True
);
870 /****************************************************************************
871 Check if a jobid is valid. It is valid if it exists in the database.
872 ****************************************************************************/
874 BOOL
print_job_exists(int snum
, uint32 jobid
)
876 struct tdb_print_db
*pdb
= get_print_db_byname(lp_const_servicename(snum
));
879 return tdb_exists(pdb
->tdb
, print_key(jobid
));
882 /****************************************************************************
883 Give the fd used for a jobid.
884 ****************************************************************************/
886 int print_job_fd(int snum
, uint32 jobid
)
888 struct printjob
*pjob
= print_job_find(snum
, jobid
);
891 /* don't allow another process to get this info - it is meaningless */
892 if (pjob
->pid
!= local_pid
)
897 /****************************************************************************
898 Give the filename used for a jobid.
899 Only valid for the process doing the spooling and when the job
900 has not been spooled.
901 ****************************************************************************/
903 char *print_job_fname(int snum
, uint32 jobid
)
905 struct printjob
*pjob
= print_job_find(snum
, jobid
);
906 if (!pjob
|| pjob
->spooled
|| pjob
->pid
!= local_pid
)
908 return pjob
->filename
;
911 /****************************************************************************
912 Set the place in the queue for a job.
913 ****************************************************************************/
915 BOOL
print_job_set_place(int snum
, uint32 jobid
, int place
)
917 DEBUG(2,("print_job_set_place not implemented yet\n"));
921 /****************************************************************************
922 Set the name of a job. Only possible for owner.
923 ****************************************************************************/
925 BOOL
print_job_set_name(int snum
, uint32 jobid
, char *name
)
927 struct printjob
*pjob
= print_job_find(snum
, jobid
);
928 if (!pjob
|| pjob
->pid
!= local_pid
)
931 fstrcpy(pjob
->jobname
, name
);
932 return pjob_store(snum
, jobid
, pjob
);
935 /****************************************************************************
936 Delete a print job - don't update queue.
937 ****************************************************************************/
939 static BOOL
print_job_delete1(int snum
, uint32 jobid
)
941 struct printjob
*pjob
= print_job_find(snum
, jobid
);
948 * If already deleting just return.
951 if (pjob
->status
== LPQ_DELETING
)
954 /* Hrm - we need to be able to cope with deleting a job before it
955 has reached the spooler. */
957 if (pjob
->sysjob
== -1) {
958 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid
));
961 /* Set the tdb entry to be deleting. */
963 pjob
->status
= LPQ_DELETING
;
964 pjob_store(snum
, jobid
, pjob
);
966 if (pjob
->spooled
&& pjob
->sysjob
!= -1)
967 result
= (*(current_printif
->job_delete
))(snum
, pjob
);
969 /* Delete the tdb entry if the delete suceeded or the job hasn't
973 pjob_delete(snum
, jobid
);
975 return (result
== 0);
978 /****************************************************************************
979 Return true if the current user owns the print job.
980 ****************************************************************************/
982 static BOOL
is_owner(struct current_user
*user
, int snum
, uint32 jobid
)
984 struct printjob
*pjob
= print_job_find(snum
, jobid
);
990 if ((vuser
= get_valid_user_struct(user
->vuid
)) != NULL
) {
991 return strequal(pjob
->user
, vuser
->user
.smb_name
);
993 return strequal(pjob
->user
, uidtoname(user
->uid
));
997 /****************************************************************************
999 ****************************************************************************/
1001 BOOL
print_job_delete(struct current_user
*user
, int snum
, uint32 jobid
, WERROR
*errcode
)
1005 owner
= is_owner(user
, snum
, jobid
);
1007 /* Check access against security descriptor or whether the user
1011 !print_access_check(user
, snum
, JOB_ACCESS_ADMINISTER
)) {
1012 DEBUG(3, ("delete denied by security descriptor\n"));
1013 *errcode
= WERR_ACCESS_DENIED
;
1017 if (!print_job_delete1(snum
, jobid
))
1020 /* force update the database and say the delete failed if the
1023 print_queue_update(snum
);
1025 return !print_job_exists(snum
, jobid
);
1028 /****************************************************************************
1030 ****************************************************************************/
1032 BOOL
print_job_pause(struct current_user
*user
, int snum
, uint32 jobid
, WERROR
*errcode
)
1034 struct printjob
*pjob
= print_job_find(snum
, jobid
);
1040 if (!pjob
->spooled
|| pjob
->sysjob
== -1)
1043 if (!is_owner(user
, snum
, jobid
) &&
1044 !print_access_check(user
, snum
, JOB_ACCESS_ADMINISTER
)) {
1045 DEBUG(3, ("pause denied by security descriptor\n"));
1046 *errcode
= WERR_ACCESS_DENIED
;
1050 /* need to pause the spooled entry */
1051 ret
= (*(current_printif
->job_pause
))(snum
, pjob
);
1054 *errcode
= WERR_INVALID_PARAM
;
1058 /* force update the database */
1059 print_cache_flush(snum
);
1061 /* Send a printer notify message */
1063 notify_job_status(snum
, jobid
, JOB_STATUS_PAUSED
);
1065 /* how do we tell if this succeeded? */
1070 /****************************************************************************
1072 ****************************************************************************/
1074 BOOL
print_job_resume(struct current_user
*user
, int snum
, uint32 jobid
, WERROR
*errcode
)
1076 struct printjob
*pjob
= print_job_find(snum
, jobid
);
1082 if (!pjob
->spooled
|| pjob
->sysjob
== -1)
1085 if (!is_owner(user
, snum
, jobid
) &&
1086 !print_access_check(user
, snum
, JOB_ACCESS_ADMINISTER
)) {
1087 DEBUG(3, ("resume denied by security descriptor\n"));
1088 *errcode
= WERR_ACCESS_DENIED
;
1092 ret
= (*(current_printif
->job_resume
))(snum
, pjob
);
1095 *errcode
= WERR_INVALID_PARAM
;
1099 /* force update the database */
1100 print_cache_flush(snum
);
1102 /* Send a printer notify message */
1104 notify_job_status(snum
, jobid
, JOB_STATUS_QUEUED
);
1109 /****************************************************************************
1110 Write to a print file.
1111 ****************************************************************************/
1113 int print_job_write(int snum
, uint32 jobid
, const char *buf
, int size
)
1116 struct printjob
*pjob
= print_job_find(snum
, jobid
);
1120 /* don't allow another process to get this info - it is meaningless */
1121 if (pjob
->pid
!= local_pid
)
1124 return_code
= write(pjob
->fd
, buf
, size
);
1125 if (return_code
>0) {
1127 pjob_store(snum
, jobid
, pjob
);
1132 /****************************************************************************
1133 Check if the print queue has been updated recently enough.
1134 ****************************************************************************/
1136 static BOOL
print_cache_expired(int snum
)
1139 time_t last_qscan_time
, time_now
= time(NULL
);
1140 const char *printername
= lp_const_servicename(snum
);
1141 struct tdb_print_db
*pdb
= get_print_db_byname(printername
);
1146 slprintf(key
, sizeof(key
), "CACHE/%s", printername
);
1147 last_qscan_time
= (time_t)tdb_fetch_int32(pdb
->tdb
, key
);
1150 * Invalidate the queue for 3 reasons.
1151 * (1). last queue scan time == -1.
1152 * (2). Current time - last queue scan time > allowed cache time.
1153 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1154 * This last test picks up machines for which the clock has been moved
1155 * forward, an lpq scan done and then the clock moved back. Otherwise
1156 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1159 if (last_qscan_time
== ((time_t)-1) || (time_now
- last_qscan_time
) >= lp_lpqcachetime() ||
1160 last_qscan_time
> (time_now
+ MAX_CACHE_VALID_TIME
)) {
1161 DEBUG(3, ("print cache expired for queue %s \
1162 (last_qscan_time = %d, time now = %d, qcachetime = %d)\n", printername
,
1163 (int)last_qscan_time
, (int)time_now
, (int)lp_lpqcachetime() ));
1169 /****************************************************************************
1170 Get the queue status - do not update if db is out of date.
1171 ****************************************************************************/
1173 static int get_queue_status(int snum
, print_status_struct
*status
)
1177 const char *printername
= lp_const_servicename(snum
);
1178 struct tdb_print_db
*pdb
= get_print_db_byname(printername
);
1182 ZERO_STRUCTP(status
);
1183 slprintf(keystr
, sizeof(keystr
)-1, "STATUS/%s", printername
);
1185 key
.dsize
= strlen(keystr
);
1186 data
= tdb_fetch(pdb
->tdb
, key
);
1188 if (data
.dsize
== sizeof(print_status_struct
)) {
1189 memcpy(status
, data
.dptr
, sizeof(print_status_struct
));
1191 SAFE_FREE(data
.dptr
);
1193 return status
->qcount
;
1196 /****************************************************************************
1197 Determine the number of jobs in a queue.
1198 ****************************************************************************/
1200 int print_queue_length(int snum
, print_status_struct
*pstatus
)
1202 print_status_struct status
;
1205 /* make sure the database is up to date */
1206 if (print_cache_expired(snum
))
1207 print_queue_update(snum
);
1209 /* also fetch the queue status */
1210 memset(&status
, 0, sizeof(status
));
1211 len
= get_queue_status(snum
, &status
);
1217 /****************************************************************************
1218 Determine the number of jobs in all queues. This is very expensive. Don't
1220 ****************************************************************************/
1222 static int get_total_jobs(void)
1226 int services
= lp_numservices();
1228 for (snum
= 0; snum
< services
; snum
++) {
1229 struct tdb_print_db
*pdb
;
1232 if (!lp_print_ok(snum
))
1235 pdb
= get_print_db_byname(lp_const_servicename(snum
));
1239 /* make sure the database is up to date */
1240 if (print_cache_expired(snum
))
1241 print_queue_update(snum
);
1243 jobs
= tdb_fetch_int32(pdb
->tdb
, "INFO/total_jobs");
1250 /***************************************************************************
1251 Start spooling a job - return the jobid.
1252 ***************************************************************************/
1254 uint32
print_job_start(struct current_user
*user
, int snum
, char *jobname
)
1258 struct printjob pjob
;
1262 const char *printername
= lp_const_servicename(snum
);
1263 struct tdb_print_db
*pdb
= get_print_db_byname(printername
);
1270 if (!print_access_check(user
, snum
, PRINTER_ACCESS_USE
)) {
1271 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
1275 if (!print_time_access_check(snum
)) {
1276 DEBUG(3, ("print_job_start: job start denied by time check\n"));
1280 path
= lp_pathname(snum
);
1282 /* see if we have sufficient disk space */
1283 if (lp_minprintspace(snum
)) {
1284 SMB_BIG_UINT dspace
, dsize
;
1285 if (sys_fsusage(path
, &dspace
, &dsize
) == 0 &&
1286 dspace
< 2*(SMB_BIG_UINT
)lp_minprintspace(snum
)) {
1287 DEBUG(3, ("print_job_start: disk space check failed.\n"));
1293 /* for autoloaded printers, check that the printcap entry still exists */
1294 if (lp_autoloaded(snum
) && !pcap_printername_ok(lp_const_servicename(snum
), NULL
)) {
1295 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum
) ));
1300 /* Insure the maximum queue size is not violated */
1301 if (lp_maxprintjobs(snum
) && (njobs
= print_queue_length(snum
,NULL
)) > lp_maxprintjobs(snum
)) {
1302 DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per queue (%d).\n",
1303 njobs
, lp_maxprintjobs(snum
) ));
1308 /* Insure the maximum print jobs in the system is not violated */
1309 if (lp_totalprintjobs() && get_total_jobs() > lp_totalprintjobs()) {
1310 DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per system (%d).\n",
1311 njobs
, lp_totalprintjobs() ));
1316 /* create the database entry */
1318 pjob
.pid
= local_pid
;
1321 pjob
.starttime
= time(NULL
);
1322 pjob
.status
= LPQ_SPOOLING
;
1324 pjob
.spooled
= False
;
1327 fstrcpy(pjob
.jobname
, jobname
);
1329 if ((vuser
= get_valid_user_struct(user
->vuid
)) != NULL
) {
1330 fstrcpy(pjob
.user
, vuser
->user
.smb_name
);
1332 fstrcpy(pjob
.user
, uidtoname(user
->uid
));
1335 fstrcpy(pjob
.queuename
, lp_const_servicename(snum
));
1337 /* lock the database */
1338 tdb_lock_bystring(pdb
->tdb
, "INFO/nextjob");
1340 next_jobid
= tdb_fetch_int32(pdb
->tdb
, "INFO/nextjob");
1341 if (next_jobid
== -1)
1344 for (jobid
= NEXT_JOBID(next_jobid
); jobid
!= next_jobid
; jobid
= NEXT_JOBID(jobid
)) {
1345 if (!print_job_exists(snum
, jobid
))
1348 if (jobid
== next_jobid
|| !pjob_store(snum
, jobid
, &pjob
)) {
1349 DEBUG(3, ("print_job_start: either jobid (%d)==next_jobid(%d) or pjob_store failed.\n",
1350 jobid
, next_jobid
));
1355 if (tdb_store_int32(pdb
->tdb
, "INFO/nextjob", jobid
)==-1) {
1356 DEBUG(3, ("print_job_start: failed to store INFO/nextjob.\n"));
1361 /* we have a job entry - now create the spool file */
1362 slprintf(pjob
.filename
, sizeof(pjob
.filename
)-1, "%s/%s%.8u.XXXXXX",
1363 path
, PRINT_SPOOL_PREFIX
, (unsigned int)jobid
);
1364 pjob
.fd
= smb_mkstemp(pjob
.filename
);
1366 if (pjob
.fd
== -1) {
1367 if (errno
== EACCES
) {
1368 /* Common setup error, force a report. */
1369 DEBUG(0, ("print_job_start: insufficient permissions \
1370 to open spool file %s.\n", pjob
.filename
));
1372 /* Normal case, report at level 3 and above. */
1373 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob
.filename
));
1374 DEBUGADD(3, ("errno = %d (%s).\n", errno
, strerror(errno
)));
1379 pjob_store(snum
, jobid
, &pjob
);
1381 tdb_unlock_bystring(pdb
->tdb
, "INFO/nextjob");
1384 * If the printer is marked as postscript output a leading
1385 * file identifier to ensure the file is treated as a raw
1387 * This has a similar effect as CtrlD=0 in WIN.INI file.
1388 * tim@fsg.com 09/06/94
1390 if (lp_postscript(snum
)) {
1391 print_job_write(snum
, jobid
, "%!\n",3);
1398 pjob_delete(snum
, jobid
);
1400 tdb_unlock_bystring(pdb
->tdb
, "INFO/nextjob");
1402 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno
) ));
1406 /****************************************************************************
1407 Update the number of pages spooled to jobid
1408 ****************************************************************************/
1410 void print_job_endpage(int snum
, uint32 jobid
)
1412 struct printjob
*pjob
= print_job_find(snum
, jobid
);
1415 /* don't allow another process to get this info - it is meaningless */
1416 if (pjob
->pid
!= local_pid
)
1420 pjob_store(snum
, jobid
, pjob
);
1423 /****************************************************************************
1424 Print a file - called on closing the file. This spools the job.
1425 If normal close is false then we're tearing down the jobs - treat as an
1427 ****************************************************************************/
1429 BOOL
print_job_end(int snum
, uint32 jobid
, BOOL normal_close
)
1431 struct printjob
*pjob
= print_job_find(snum
, jobid
);
1433 SMB_STRUCT_STAT sbuf
;
1438 if (pjob
->spooled
|| pjob
->pid
!= local_pid
)
1441 if (normal_close
&& (sys_fstat(pjob
->fd
, &sbuf
) == 0)) {
1442 pjob
->size
= sbuf
.st_size
;
1448 * Not a normal close or we couldn't stat the job file,
1449 * so something has gone wrong. Cleanup.
1453 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid
));
1457 /* Technically, this is not quite right. If the printer has a separator
1458 * page turned on, the NT spooler prints the separator page even if the
1459 * print job is 0 bytes. 010215 JRR */
1460 if (pjob
->size
== 0 || pjob
->status
== LPQ_DELETING
) {
1461 /* don't bother spooling empty files or something being deleted. */
1462 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
1463 pjob
->filename
, pjob
->size
? "deleted" : "zero length" ));
1464 unlink(pjob
->filename
);
1465 pjob_delete(snum
, jobid
);
1469 ret
= (*(current_printif
->job_submit
))(snum
, pjob
);
1474 /* The print job has been sucessfully handed over to the back-end */
1476 pjob
->spooled
= True
;
1477 pjob
->status
= LPQ_QUEUED
;
1478 pjob_store(snum
, jobid
, pjob
);
1480 /* make sure the database is up to date */
1481 if (print_cache_expired(snum
))
1482 print_queue_update(snum
);
1488 /* The print job was not succesfully started. Cleanup */
1489 /* Still need to add proper error return propagation! 010122:JRR */
1490 unlink(pjob
->filename
);
1491 pjob_delete(snum
, jobid
);
1495 /****************************************************************************
1496 Utility fn to enumerate the print queue.
1497 ****************************************************************************/
1499 static int traverse_fn_queue(TDB_CONTEXT
*t
, TDB_DATA key
, TDB_DATA data
, void *state
)
1501 struct traverse_struct
*ts
= (struct traverse_struct
*)state
;
1502 struct printjob pjob
;
1506 if (data
.dsize
!= sizeof(pjob
) || key
.dsize
!= sizeof(int))
1508 memcpy(&jobid
, key
.dptr
, sizeof(jobid
));
1509 memcpy(&pjob
, data
.dptr
, sizeof(pjob
));
1511 /* maybe it isn't for this queue */
1512 if (ts
->snum
!= lp_servicenumber(pjob
.queuename
))
1515 if (ts
->qcount
>= ts
->maxcount
)
1520 ts
->queue
[i
].job
= jobid
;
1521 ts
->queue
[i
].size
= pjob
.size
;
1522 ts
->queue
[i
].page_count
= pjob
.page_count
;
1523 ts
->queue
[i
].status
= pjob
.status
;
1524 ts
->queue
[i
].priority
= 1;
1525 ts
->queue
[i
].time
= pjob
.starttime
;
1526 fstrcpy(ts
->queue
[i
].fs_user
, pjob
.user
);
1527 fstrcpy(ts
->queue
[i
].fs_file
, pjob
.jobname
);
1534 struct traverse_count_struct
{
1538 /****************************************************************************
1539 Utility fn to count the number of entries in the print queue.
1540 ****************************************************************************/
1542 static int traverse_count_fn_queue(TDB_CONTEXT
*t
, TDB_DATA key
, TDB_DATA data
, void *state
)
1544 struct traverse_count_struct
*ts
= (struct traverse_count_struct
*)state
;
1545 struct printjob pjob
;
1548 if (data
.dsize
!= sizeof(pjob
) || key
.dsize
!= sizeof(int))
1550 memcpy(&jobid
, key
.dptr
, sizeof(jobid
));
1551 memcpy(&pjob
, data
.dptr
, sizeof(pjob
));
1553 /* maybe it isn't for this queue - this cannot happen with the tdb/printer code. JRA */
1554 if (ts
->snum
!= lp_servicenumber(pjob
.queuename
))
1562 /****************************************************************************
1563 Sort print jobs by submittal time.
1564 ****************************************************************************/
1566 static int printjob_comp(print_queue_struct
*j1
, print_queue_struct
*j2
)
1577 /* Sort on job start time */
1579 if (j1
->time
== j2
->time
)
1581 return (j1
->time
> j2
->time
) ? 1 : -1;
1584 /****************************************************************************
1585 Get a printer queue listing.
1586 ****************************************************************************/
1588 int print_queue_status(int snum
,
1589 print_queue_struct
**queue
,
1590 print_status_struct
*status
)
1592 struct traverse_struct tstruct
;
1593 struct traverse_count_struct tsc
;
1596 const char *printername
= lp_const_servicename(snum
);
1597 struct tdb_print_db
*pdb
= get_print_db_byname(printername
);
1604 /* make sure the database is up to date */
1605 if (print_cache_expired(snum
))
1606 print_queue_update(snum
);
1609 * Fetch the queue status. We must do this first, as there may
1610 * be no jobs in the queue.
1612 ZERO_STRUCTP(status
);
1613 slprintf(keystr
, sizeof(keystr
)-1, "STATUS/%s", printername
);
1615 key
.dsize
= strlen(keystr
);
1616 data
= tdb_fetch(pdb
->tdb
, key
);
1618 if (data
.dsize
== sizeof(*status
)) {
1619 memcpy(status
, data
.dptr
, sizeof(*status
));
1621 SAFE_FREE(data
.dptr
);
1625 * Now, fetch the print queue information. We first count the number
1626 * of entries, and then only retrieve the queue if necessary.
1631 tdb_traverse(pdb
->tdb
, traverse_count_fn_queue
, (void *)&tsc
);
1636 /* Allocate the queue size. */
1637 if ((tstruct
.queue
= (print_queue_struct
*)
1638 malloc(sizeof(print_queue_struct
)*tsc
.count
)) == NULL
)
1642 * Fill in the queue.
1643 * We need maxcount as the queue size may have changed between
1644 * the two calls to tdb_traverse.
1647 tstruct
.maxcount
= tsc
.count
;
1648 tstruct
.snum
= snum
;
1650 tdb_traverse(pdb
->tdb
, traverse_fn_queue
, (void *)&tstruct
);
1652 /* Sort the queue by submission time otherwise they are displayed
1655 qsort(tstruct
.queue
, tstruct
.qcount
, sizeof(print_queue_struct
),
1656 QSORT_CAST(printjob_comp
));
1658 *queue
= tstruct
.queue
;
1659 return tstruct
.qcount
;
1662 /****************************************************************************
1663 Turn a queue name into a snum.
1664 ****************************************************************************/
1666 int print_queue_snum(const char *qname
)
1668 int snum
= lp_servicenumber(qname
);
1669 if (snum
== -1 || !lp_print_ok(snum
))
1674 /****************************************************************************
1676 ****************************************************************************/
1678 BOOL
print_queue_pause(struct current_user
*user
, int snum
, WERROR
*errcode
)
1682 if (!print_access_check(user
, snum
, PRINTER_ACCESS_ADMINISTER
)) {
1683 *errcode
= WERR_ACCESS_DENIED
;
1687 ret
= (*(current_printif
->queue_pause
))(snum
);
1690 *errcode
= WERR_INVALID_PARAM
;
1694 /* force update the database */
1695 print_cache_flush(snum
);
1697 /* Send a printer notify message */
1699 notify_printer_status(snum
, PRINTER_STATUS_PAUSED
);
1704 /****************************************************************************
1706 ****************************************************************************/
1708 BOOL
print_queue_resume(struct current_user
*user
, int snum
, WERROR
*errcode
)
1712 if (!print_access_check(user
, snum
, PRINTER_ACCESS_ADMINISTER
)) {
1713 *errcode
= WERR_ACCESS_DENIED
;
1717 ret
= (*(current_printif
->queue_resume
))(snum
);
1720 *errcode
= WERR_INVALID_PARAM
;
1724 /* make sure the database is up to date */
1725 if (print_cache_expired(snum
))
1726 print_queue_update(snum
);
1728 /* Send a printer notify message */
1730 notify_printer_status(snum
, PRINTER_STATUS_OK
);
1735 /****************************************************************************
1736 Purge a queue - implemented by deleting all jobs that we can delete.
1737 ****************************************************************************/
1739 BOOL
print_queue_purge(struct current_user
*user
, int snum
, WERROR
*errcode
)
1741 print_queue_struct
*queue
;
1742 print_status_struct status
;
1746 /* Force and update so the count is accurate (i.e. not a cached count) */
1747 print_queue_update(snum
);
1749 can_job_admin
= print_access_check(user
, snum
, JOB_ACCESS_ADMINISTER
);
1750 njobs
= print_queue_status(snum
, &queue
, &status
);
1752 for (i
=0;i
<njobs
;i
++) {
1753 BOOL owner
= is_owner(user
, snum
, queue
[i
].job
);
1755 if (owner
|| can_job_admin
) {
1756 print_job_delete1(snum
, queue
[i
].job
);