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 3 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, see <http://www.gnu.org/licenses/>.
25 extern SIG_ATOMIC_T got_sig_term
;
26 extern SIG_ATOMIC_T reload_after_sighup
;
27 extern struct current_user current_user
;
28 extern userdom_struct current_user_info
;
30 /* Current printer interface */
31 static bool remove_from_jobs_changed(const char* sharename
, uint32 jobid
);
34 the printing backend revolves around a tdb database that stores the
35 SMB view of the print queue
37 The key for this database is a jobid - a internally generated number that
38 uniquely identifies a print job
40 reading the print queue involves two steps:
41 - possibly running lpq and updating the internal database from that
42 - reading entries from the database
44 jobids are assigned when a job starts spooling.
47 static TDB_CONTEXT
*rap_tdb
;
48 static uint16 next_rap_jobid
;
49 struct rap_jobid_key
{
54 /***************************************************************************
55 Nightmare. LANMAN jobid's are 16 bit numbers..... We must map them to 32
56 bit RPC jobids.... JRA.
57 ***************************************************************************/
59 uint16
pjobid_to_rap(const char* sharename
, uint32 jobid
)
63 struct rap_jobid_key jinfo
;
66 DEBUG(10,("pjobid_to_rap: called.\n"));
69 /* Create the in-memory tdb. */
70 rap_tdb
= tdb_open_log(NULL
, 0, TDB_INTERNAL
, (O_RDWR
|O_CREAT
), 0644);
76 fstrcpy( jinfo
.sharename
, sharename
);
78 key
.dptr
= (uint8
*)&jinfo
;
79 key
.dsize
= sizeof(jinfo
);
81 data
= tdb_fetch(rap_tdb
, key
);
82 if (data
.dptr
&& data
.dsize
== sizeof(uint16
)) {
83 rap_jobid
= SVAL(data
.dptr
, 0);
85 DEBUG(10,("pjobid_to_rap: jobid %u maps to RAP jobid %u\n",
86 (unsigned int)jobid
, (unsigned int)rap_jobid
));
90 /* Not found - create and store mapping. */
91 rap_jobid
= ++next_rap_jobid
;
93 rap_jobid
= ++next_rap_jobid
;
94 SSVAL(buf
,0,rap_jobid
);
96 data
.dsize
= sizeof(rap_jobid
);
97 tdb_store(rap_tdb
, key
, data
, TDB_REPLACE
);
98 tdb_store(rap_tdb
, data
, key
, TDB_REPLACE
);
100 DEBUG(10,("pjobid_to_rap: created jobid %u maps to RAP jobid %u\n",
101 (unsigned int)jobid
, (unsigned int)rap_jobid
));
105 bool rap_to_pjobid(uint16 rap_jobid
, fstring sharename
, uint32
*pjobid
)
110 DEBUG(10,("rap_to_pjobid called.\n"));
115 SSVAL(buf
,0,rap_jobid
);
117 key
.dsize
= sizeof(rap_jobid
);
118 data
= tdb_fetch(rap_tdb
, key
);
119 if ( data
.dptr
&& data
.dsize
== sizeof(struct rap_jobid_key
) )
121 struct rap_jobid_key
*jinfo
= (struct rap_jobid_key
*)data
.dptr
;
122 fstrcpy( sharename
, jinfo
->sharename
);
123 *pjobid
= jinfo
->jobid
;
124 DEBUG(10,("rap_to_pjobid: jobid %u maps to RAP jobid %u\n",
125 (unsigned int)*pjobid
, (unsigned int)rap_jobid
));
126 SAFE_FREE(data
.dptr
);
130 DEBUG(10,("rap_to_pjobid: Failed to lookup RAP jobid %u\n",
131 (unsigned int)rap_jobid
));
132 SAFE_FREE(data
.dptr
);
136 static void rap_jobid_delete(const char* sharename
, uint32 jobid
)
140 struct rap_jobid_key jinfo
;
143 DEBUG(10,("rap_jobid_delete: called.\n"));
148 ZERO_STRUCT( jinfo
);
149 fstrcpy( jinfo
.sharename
, sharename
);
151 key
.dptr
= (uint8
*)&jinfo
;
152 key
.dsize
= sizeof(jinfo
);
154 data
= tdb_fetch(rap_tdb
, key
);
155 if (!data
.dptr
|| (data
.dsize
!= sizeof(uint16
))) {
156 DEBUG(10,("rap_jobid_delete: cannot find jobid %u\n",
157 (unsigned int)jobid
));
158 SAFE_FREE(data
.dptr
);
162 DEBUG(10,("rap_jobid_delete: deleting jobid %u\n",
163 (unsigned int)jobid
));
165 rap_jobid
= SVAL(data
.dptr
, 0);
166 SAFE_FREE(data
.dptr
);
167 SSVAL(buf
,0,rap_jobid
);
169 data
.dsize
= sizeof(rap_jobid
);
170 tdb_delete(rap_tdb
, key
);
171 tdb_delete(rap_tdb
, data
);
174 static int get_queue_status(const char* sharename
, print_status_struct
*);
176 /****************************************************************************
177 Initialise the printing backend. Called once at startup before the fork().
178 ****************************************************************************/
180 bool print_backend_init(struct messaging_context
*msg_ctx
)
182 const char *sversion
= "INFO/version";
183 int services
= lp_numservices();
186 unlink(lock_path("printing.tdb"));
187 mkdir(lock_path("printing"),0755);
189 /* handle a Samba upgrade */
191 for (snum
= 0; snum
< services
; snum
++) {
192 struct tdb_print_db
*pdb
;
193 if (!lp_print_ok(snum
))
196 pdb
= get_print_db_byname(lp_const_servicename(snum
));
199 if (tdb_lock_bystring(pdb
->tdb
, sversion
) == -1) {
200 DEBUG(0,("print_backend_init: Failed to open printer %s database\n", lp_const_servicename(snum
) ));
201 release_print_db(pdb
);
204 if (tdb_fetch_int32(pdb
->tdb
, sversion
) != PRINT_DATABASE_VERSION
) {
205 tdb_wipe_all(pdb
->tdb
);
206 tdb_store_int32(pdb
->tdb
, sversion
, PRINT_DATABASE_VERSION
);
208 tdb_unlock_bystring(pdb
->tdb
, sversion
);
209 release_print_db(pdb
);
212 close_all_print_db(); /* Don't leave any open. */
214 /* do NT print initialization... */
215 return nt_printing_init(msg_ctx
);
218 /****************************************************************************
219 Shut down printing backend. Called once at shutdown to close the tdb.
220 ****************************************************************************/
222 void printing_end(void)
224 close_all_print_db(); /* Don't leave any open. */
227 /****************************************************************************
228 Retrieve the set of printing functions for a given service. This allows
229 us to set the printer function table based on the value of the 'printing'
232 Use the generic interface as the default and only use cups interface only
233 when asked for (and only when supported)
234 ****************************************************************************/
236 static struct printif
*get_printer_fns_from_type( enum printing_types type
)
238 struct printif
*printer_fns
= &generic_printif
;
241 if ( type
== PRINT_CUPS
) {
242 printer_fns
= &cups_printif
;
244 #endif /* HAVE_CUPS */
247 if ( type
== PRINT_IPRINT
) {
248 printer_fns
= &iprint_printif
;
250 #endif /* HAVE_IPRINT */
252 printer_fns
->type
= type
;
257 static struct printif
*get_printer_fns( int snum
)
259 return get_printer_fns_from_type( (enum printing_types
)lp_printing(snum
) );
263 /****************************************************************************
264 Useful function to generate a tdb key.
265 ****************************************************************************/
267 static TDB_DATA
print_key(uint32 jobid
, uint32
*tmp
)
271 SIVAL(tmp
, 0, jobid
);
272 ret
.dptr
= (uint8
*)tmp
;
273 ret
.dsize
= sizeof(*tmp
);
277 /***********************************************************************
278 unpack a pjob from a tdb buffer
279 ***********************************************************************/
281 int unpack_pjob( uint8
*buf
, int buflen
, struct printjob
*pjob
)
285 uint32 pjpid
, pjsysjob
, pjfd
, pjstarttime
, pjstatus
;
286 uint32 pjsize
, pjpage_count
, pjspooled
, pjsmbjob
;
291 len
+= tdb_unpack(buf
+len
, buflen
-len
, "dddddddddffff",
309 if ( (used
= unpack_devicemode(&pjob
->nt_devmode
, buf
+len
, buflen
-len
)) == -1 )
315 pjob
->sysjob
= pjsysjob
;
317 pjob
->starttime
= pjstarttime
;
318 pjob
->status
= pjstatus
;
320 pjob
->page_count
= pjpage_count
;
321 pjob
->spooled
= pjspooled
;
322 pjob
->smbjob
= pjsmbjob
;
328 /****************************************************************************
329 Useful function to find a print job in the database.
330 ****************************************************************************/
332 static struct printjob
*print_job_find(const char *sharename
, uint32 jobid
)
334 static struct printjob pjob
;
337 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
339 DEBUG(10,("print_job_find: looking up job %u for share %s\n",
340 (unsigned int)jobid
, sharename
));
346 ret
= tdb_fetch(pdb
->tdb
, print_key(jobid
, &tmp
));
347 release_print_db(pdb
);
350 DEBUG(10,("print_job_find: failed to find jobid %u.\n", (unsigned int)jobid
));
354 if ( pjob
.nt_devmode
) {
355 free_nt_devicemode( &pjob
.nt_devmode
);
360 if ( unpack_pjob( ret
.dptr
, ret
.dsize
, &pjob
) == -1 ) {
361 DEBUG(10,("print_job_find: failed to unpack jobid %u.\n", (unsigned int)jobid
));
368 DEBUG(10,("print_job_find: returning system job %d for jobid %u.\n",
369 (int)pjob
.sysjob
, (unsigned int)jobid
));
374 /* Convert a unix jobid to a smb jobid */
376 struct unixjob_traverse_state
{
378 uint32 sysjob_to_jobid_value
;
381 static int unixjob_traverse_fn(TDB_CONTEXT
*the_tdb
, TDB_DATA key
,
382 TDB_DATA data
, void *private_data
)
384 struct printjob
*pjob
;
385 struct unixjob_traverse_state
*state
=
386 (struct unixjob_traverse_state
*)private_data
;
388 if (!data
.dptr
|| data
.dsize
== 0)
391 pjob
= (struct printjob
*)data
.dptr
;
392 if (key
.dsize
!= sizeof(uint32
))
395 if (state
->sysjob
== pjob
->sysjob
) {
396 uint32 jobid
= IVAL(key
.dptr
,0);
398 state
->sysjob_to_jobid_value
= jobid
;
405 /****************************************************************************
406 This is a *horribly expensive call as we have to iterate through all the
407 current printer tdb's. Don't do this often ! JRA.
408 ****************************************************************************/
410 uint32
sysjob_to_jobid(int unix_jobid
)
412 int services
= lp_numservices();
414 struct unixjob_traverse_state state
;
416 state
.sysjob
= unix_jobid
;
417 state
.sysjob_to_jobid_value
= (uint32
)-1;
419 for (snum
= 0; snum
< services
; snum
++) {
420 struct tdb_print_db
*pdb
;
421 if (!lp_print_ok(snum
))
423 pdb
= get_print_db_byname(lp_const_servicename(snum
));
427 tdb_traverse(pdb
->tdb
, unixjob_traverse_fn
, &state
);
428 release_print_db(pdb
);
429 if (state
.sysjob_to_jobid_value
!= (uint32
)-1)
430 return state
.sysjob_to_jobid_value
;
435 /****************************************************************************
436 Send notifications based on what has changed after a pjob_store.
437 ****************************************************************************/
439 static const struct {
441 uint32 spoolss_status
;
442 } lpq_to_spoolss_status_map
[] = {
443 { LPQ_QUEUED
, JOB_STATUS_QUEUED
},
444 { LPQ_PAUSED
, JOB_STATUS_PAUSED
},
445 { LPQ_SPOOLING
, JOB_STATUS_SPOOLING
},
446 { LPQ_PRINTING
, JOB_STATUS_PRINTING
},
447 { LPQ_DELETING
, JOB_STATUS_DELETING
},
448 { LPQ_OFFLINE
, JOB_STATUS_OFFLINE
},
449 { LPQ_PAPEROUT
, JOB_STATUS_PAPEROUT
},
450 { LPQ_PRINTED
, JOB_STATUS_PRINTED
},
451 { LPQ_DELETED
, JOB_STATUS_DELETED
},
452 { LPQ_BLOCKED
, JOB_STATUS_BLOCKED
},
453 { LPQ_USER_INTERVENTION
, JOB_STATUS_USER_INTERVENTION
},
457 /* Convert a lpq status value stored in printing.tdb into the
458 appropriate win32 API constant. */
460 static uint32
map_to_spoolss_status(uint32 lpq_status
)
464 while (lpq_to_spoolss_status_map
[i
].lpq_status
!= -1) {
465 if (lpq_to_spoolss_status_map
[i
].lpq_status
== lpq_status
)
466 return lpq_to_spoolss_status_map
[i
].spoolss_status
;
473 static void pjob_store_notify(const char* sharename
, uint32 jobid
, struct printjob
*old_data
,
474 struct printjob
*new_data
)
476 bool new_job
= False
;
481 /* Job attributes that can't be changed. We only send
482 notification for these on a new job. */
484 /* ACHTUNG! Due to a bug in Samba's spoolss parsing of the
485 NOTIFY_INFO_DATA buffer, we *have* to send the job submission
486 time first or else we'll end up with potential alignment
487 errors. I don't think the systemtime should be spooled as
488 a string, but this gets us around that error.
489 --jerry (i'll feel dirty for this) */
492 notify_job_submitted(sharename
, jobid
, new_data
->starttime
);
493 notify_job_username(sharename
, jobid
, new_data
->user
);
496 if (new_job
|| !strequal(old_data
->jobname
, new_data
->jobname
))
497 notify_job_name(sharename
, jobid
, new_data
->jobname
);
499 /* Job attributes of a new job or attributes that can be
502 if (new_job
|| !strequal(old_data
->jobname
, new_data
->jobname
))
503 notify_job_name(sharename
, jobid
, new_data
->jobname
);
505 if (new_job
|| old_data
->status
!= new_data
->status
)
506 notify_job_status(sharename
, jobid
, map_to_spoolss_status(new_data
->status
));
508 if (new_job
|| old_data
->size
!= new_data
->size
)
509 notify_job_total_bytes(sharename
, jobid
, new_data
->size
);
511 if (new_job
|| old_data
->page_count
!= new_data
->page_count
)
512 notify_job_total_pages(sharename
, jobid
, new_data
->page_count
);
515 /****************************************************************************
516 Store a job structure back to the database.
517 ****************************************************************************/
519 static bool pjob_store(const char* sharename
, uint32 jobid
, struct printjob
*pjob
)
522 TDB_DATA old_data
, new_data
;
524 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
526 int len
, newlen
, buflen
;
534 old_data
= tdb_fetch(pdb
->tdb
, print_key(jobid
, &tmp
));
536 /* Doh! Now we have to pack/unpack data since the NT_DEVICEMODE was added */
543 len
+= tdb_pack(buf
+len
, buflen
-len
, "dddddddddffff",
545 (uint32
)pjob
->sysjob
,
547 (uint32
)pjob
->starttime
,
548 (uint32
)pjob
->status
,
550 (uint32
)pjob
->page_count
,
551 (uint32
)pjob
->spooled
,
552 (uint32
)pjob
->smbjob
,
558 len
+= pack_devicemode(pjob
->nt_devmode
, buf
+len
, buflen
-len
);
561 buf
= (uint8
*)SMB_REALLOC(buf
, len
);
563 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
, &tmp
), new_data
,
578 release_print_db(pdb
);
580 /* Send notify updates for what has changed */
583 struct printjob old_pjob
;
585 if ( old_data
.dsize
)
587 if ( unpack_pjob( old_data
.dptr
, old_data
.dsize
, &old_pjob
) != -1 )
589 pjob_store_notify( sharename
, jobid
, &old_pjob
, pjob
);
590 free_nt_devicemode( &old_pjob
.nt_devmode
);
595 pjob_store_notify( sharename
, jobid
, NULL
, pjob
);
600 SAFE_FREE( old_data
.dptr
);
606 /****************************************************************************
607 Remove a job structure from the database.
608 ****************************************************************************/
610 void pjob_delete(const char* sharename
, uint32 jobid
)
613 struct printjob
*pjob
;
614 uint32 job_status
= 0;
615 struct tdb_print_db
*pdb
;
617 pdb
= get_print_db_byname( sharename
);
622 pjob
= print_job_find( sharename
, jobid
);
625 DEBUG(5, ("pjob_delete: we were asked to delete nonexistent job %u\n",
626 (unsigned int)jobid
));
627 release_print_db(pdb
);
631 /* We must cycle through JOB_STATUS_DELETING and
632 JOB_STATUS_DELETED for the port monitor to delete the job
635 job_status
= JOB_STATUS_DELETING
|JOB_STATUS_DELETED
;
636 notify_job_status(sharename
, jobid
, job_status
);
638 /* Remove from printing.tdb */
640 tdb_delete(pdb
->tdb
, print_key(jobid
, &tmp
));
641 remove_from_jobs_changed(sharename
, jobid
);
642 release_print_db( pdb
);
643 rap_jobid_delete(sharename
, jobid
);
646 /****************************************************************************
647 Parse a file name from the system spooler to generate a jobid.
648 ****************************************************************************/
650 static uint32
print_parse_jobid(char *fname
)
654 if (strncmp(fname
,PRINT_SPOOL_PREFIX
,strlen(PRINT_SPOOL_PREFIX
)) != 0)
656 fname
+= strlen(PRINT_SPOOL_PREFIX
);
662 return (uint32
)jobid
;
665 /****************************************************************************
666 List a unix job in the print database.
667 ****************************************************************************/
669 static void print_unix_job(const char *sharename
, print_queue_struct
*q
, uint32 jobid
)
671 struct printjob pj
, *old_pj
;
673 if (jobid
== (uint32
)-1)
674 jobid
= q
->job
+ UNIX_JOB_START
;
676 /* Preserve the timestamp on an existing unix print job */
678 old_pj
= print_job_find(sharename
, jobid
);
685 pj
.starttime
= old_pj
? old_pj
->starttime
: q
->time
;
686 pj
.status
= q
->status
;
689 fstrcpy(pj
.filename
, old_pj
? old_pj
->filename
: "");
690 if (jobid
< UNIX_JOB_START
) {
692 fstrcpy(pj
.jobname
, old_pj
? old_pj
->jobname
: "Remote Downlevel Document");
695 fstrcpy(pj
.jobname
, old_pj
? old_pj
->jobname
: q
->fs_file
);
697 fstrcpy(pj
.user
, old_pj
? old_pj
->user
: q
->fs_user
);
698 fstrcpy(pj
.queuename
, old_pj
? old_pj
->queuename
: sharename
);
700 pjob_store(sharename
, jobid
, &pj
);
704 struct traverse_struct
{
705 print_queue_struct
*queue
;
706 int qcount
, snum
, maxcount
, total_jobs
;
707 const char *sharename
;
709 const char *lprm_command
;
710 struct printif
*print_if
;
713 /****************************************************************************
714 Utility fn to delete any jobs that are no longer active.
715 ****************************************************************************/
717 static int traverse_fn_delete(TDB_CONTEXT
*t
, TDB_DATA key
, TDB_DATA data
, void *state
)
719 struct traverse_struct
*ts
= (struct traverse_struct
*)state
;
720 struct printjob pjob
;
724 if ( key
.dsize
!= sizeof(jobid
) )
727 jobid
= IVAL(key
.dptr
, 0);
728 if ( unpack_pjob( data
.dptr
, data
.dsize
, &pjob
) == -1 )
730 free_nt_devicemode( &pjob
.nt_devmode
);
734 /* remove a unix job if it isn't in the system queue any more */
736 for (i
=0;i
<ts
->qcount
;i
++) {
737 uint32 u_jobid
= (ts
->queue
[i
].job
+ UNIX_JOB_START
);
738 if (jobid
== u_jobid
)
741 if (i
== ts
->qcount
) {
742 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !smbjob\n",
743 (unsigned int)jobid
));
744 pjob_delete(ts
->sharename
, jobid
);
748 /* need to continue the the bottom of the function to
749 save the correct attributes */
752 /* maybe it hasn't been spooled yet */
754 /* if a job is not spooled and the process doesn't
755 exist then kill it. This cleans up after smbd
757 if (!process_exists_by_pid(pjob
.pid
)) {
758 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !process_exists (%u)\n",
759 (unsigned int)jobid
, (unsigned int)pjob
.pid
));
760 pjob_delete(ts
->sharename
, jobid
);
766 /* this check only makes sense for jobs submitted from Windows clients */
769 for (i
=0;i
<ts
->qcount
;i
++) {
772 if ( pjob
.status
== LPQ_DELETED
)
775 curr_jobid
= print_parse_jobid(ts
->queue
[i
].fs_file
);
777 if (jobid
== curr_jobid
) {
779 /* try to clean up any jobs that need to be deleted */
781 if ( pjob
.status
== LPQ_DELETING
) {
784 result
= (*(ts
->print_if
->job_delete
))(
785 ts
->sharename
, ts
->lprm_command
, &pjob
);
788 /* if we can't delete, then reset the job status */
789 pjob
.status
= LPQ_QUEUED
;
790 pjob_store(ts
->sharename
, jobid
, &pjob
);
793 /* if we deleted the job, the remove the tdb record */
794 pjob_delete(ts
->sharename
, jobid
);
795 pjob
.status
= LPQ_DELETED
;
805 /* The job isn't in the system queue - we have to assume it has
806 completed, so delete the database entry. */
808 if (i
== ts
->qcount
) {
810 /* A race can occur between the time a job is spooled and
811 when it appears in the lpq output. This happens when
812 the job is added to printing.tdb when another smbd
813 running print_queue_update() has completed a lpq and
814 is currently traversing the printing tdb and deleting jobs.
815 Don't delete the job if it was submitted after the lpq_time. */
817 if (pjob
.starttime
< ts
->lpq_time
) {
818 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to pjob.starttime (%u) < ts->lpq_time (%u)\n",
820 (unsigned int)pjob
.starttime
,
821 (unsigned int)ts
->lpq_time
));
822 pjob_delete(ts
->sharename
, jobid
);
828 /* Save the pjob attributes we will store.
829 FIXME!!! This is the only place where queue->job
830 represents the SMB jobid --jerry */
832 ts
->queue
[i
].job
= jobid
;
833 ts
->queue
[i
].size
= pjob
.size
;
834 ts
->queue
[i
].page_count
= pjob
.page_count
;
835 ts
->queue
[i
].status
= pjob
.status
;
836 ts
->queue
[i
].priority
= 1;
837 ts
->queue
[i
].time
= pjob
.starttime
;
838 fstrcpy(ts
->queue
[i
].fs_user
, pjob
.user
);
839 fstrcpy(ts
->queue
[i
].fs_file
, pjob
.jobname
);
846 /****************************************************************************
847 Check if the print queue has been updated recently enough.
848 ****************************************************************************/
850 static void print_cache_flush(const char *sharename
)
853 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
857 slprintf(key
, sizeof(key
)-1, "CACHE/%s", sharename
);
858 tdb_store_int32(pdb
->tdb
, key
, -1);
859 release_print_db(pdb
);
862 /****************************************************************************
863 Check if someone already thinks they are doing the update.
864 ****************************************************************************/
866 static pid_t
get_updating_pid(const char *sharename
)
871 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
875 slprintf(keystr
, sizeof(keystr
)-1, "UPDATING/%s", sharename
);
876 key
= string_tdb_data(keystr
);
878 data
= tdb_fetch(pdb
->tdb
, key
);
879 release_print_db(pdb
);
880 if (!data
.dptr
|| data
.dsize
!= sizeof(pid_t
)) {
881 SAFE_FREE(data
.dptr
);
885 updating_pid
= IVAL(data
.dptr
, 0);
886 SAFE_FREE(data
.dptr
);
888 if (process_exists_by_pid(updating_pid
))
894 /****************************************************************************
895 Set the fact that we're doing the update, or have finished doing the update
897 ****************************************************************************/
899 static void set_updating_pid(const fstring sharename
, bool updating
)
904 pid_t updating_pid
= sys_getpid();
907 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
912 slprintf(keystr
, sizeof(keystr
)-1, "UPDATING/%s", sharename
);
913 key
= string_tdb_data(keystr
);
915 DEBUG(5, ("set_updating_pid: %s updating lpq cache for print share %s\n",
916 updating
? "" : "not ",
920 tdb_delete(pdb
->tdb
, key
);
921 release_print_db(pdb
);
925 SIVAL( buffer
, 0, updating_pid
);
927 data
.dsize
= 4; /* we always assume this is a 4 byte value */
929 tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
);
930 release_print_db(pdb
);
933 /****************************************************************************
934 Sort print jobs by submittal time.
935 ****************************************************************************/
937 static int printjob_comp(print_queue_struct
*j1
, print_queue_struct
*j2
)
948 /* Sort on job start time */
950 if (j1
->time
== j2
->time
)
952 return (j1
->time
> j2
->time
) ? 1 : -1;
955 /****************************************************************************
956 Store the sorted queue representation for later portmon retrieval.
958 ****************************************************************************/
960 static void store_queue_struct(struct tdb_print_db
*pdb
, struct traverse_struct
*pts
)
963 int max_reported_jobs
= lp_max_reported_jobs(pts
->snum
);
964 print_queue_struct
*queue
= pts
->queue
;
969 if (max_reported_jobs
&& (max_reported_jobs
< pts
->qcount
))
970 pts
->qcount
= max_reported_jobs
;
973 /* Work out the size. */
975 data
.dsize
+= tdb_pack(NULL
, 0, "d", qcount
);
977 for (i
= 0; i
< pts
->qcount
; i
++) {
978 if ( queue
[i
].status
== LPQ_DELETED
)
982 data
.dsize
+= tdb_pack(NULL
, 0, "ddddddff",
983 (uint32
)queue
[i
].job
,
984 (uint32
)queue
[i
].size
,
985 (uint32
)queue
[i
].page_count
,
986 (uint32
)queue
[i
].status
,
987 (uint32
)queue
[i
].priority
,
988 (uint32
)queue
[i
].time
,
993 if ((data
.dptr
= (uint8
*)SMB_MALLOC(data
.dsize
)) == NULL
)
997 len
+= tdb_pack(data
.dptr
+ len
, data
.dsize
- len
, "d", qcount
);
998 for (i
= 0; i
< pts
->qcount
; i
++) {
999 if ( queue
[i
].status
== LPQ_DELETED
)
1002 len
+= tdb_pack(data
.dptr
+ len
, data
.dsize
- len
, "ddddddff",
1003 (uint32
)queue
[i
].job
,
1004 (uint32
)queue
[i
].size
,
1005 (uint32
)queue
[i
].page_count
,
1006 (uint32
)queue
[i
].status
,
1007 (uint32
)queue
[i
].priority
,
1008 (uint32
)queue
[i
].time
,
1013 tdb_store(pdb
->tdb
, string_tdb_data("INFO/linear_queue_array"), data
,
1015 SAFE_FREE(data
.dptr
);
1019 static TDB_DATA
get_jobs_changed_data(struct tdb_print_db
*pdb
)
1025 data
= tdb_fetch(pdb
->tdb
, string_tdb_data("INFO/jobs_changed"));
1026 if (data
.dptr
== NULL
|| data
.dsize
== 0 || (data
.dsize
% 4 != 0)) {
1027 SAFE_FREE(data
.dptr
);
1034 static void check_job_changed(const char *sharename
, TDB_DATA data
, uint32 jobid
)
1037 unsigned int job_count
= data
.dsize
/ 4;
1039 for (i
= 0; i
< job_count
; i
++) {
1042 ch_jobid
= IVAL(data
.dptr
, i
*4);
1043 if (ch_jobid
== jobid
)
1044 remove_from_jobs_changed(sharename
, jobid
);
1048 /****************************************************************************
1049 Check if the print queue has been updated recently enough.
1050 ****************************************************************************/
1052 static bool print_cache_expired(const char *sharename
, bool check_pending
)
1055 time_t last_qscan_time
, time_now
= time(NULL
);
1056 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1057 bool result
= False
;
1062 snprintf(key
, sizeof(key
), "CACHE/%s", sharename
);
1063 last_qscan_time
= (time_t)tdb_fetch_int32(pdb
->tdb
, key
);
1066 * Invalidate the queue for 3 reasons.
1067 * (1). last queue scan time == -1.
1068 * (2). Current time - last queue scan time > allowed cache time.
1069 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1070 * This last test picks up machines for which the clock has been moved
1071 * forward, an lpq scan done and then the clock moved back. Otherwise
1072 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1075 if (last_qscan_time
== ((time_t)-1)
1076 || (time_now
- last_qscan_time
) >= lp_lpqcachetime()
1077 || last_qscan_time
> (time_now
+ MAX_CACHE_VALID_TIME
))
1080 time_t msg_pending_time
;
1082 DEBUG(4, ("print_cache_expired: cache expired for queue %s "
1083 "(last_qscan_time = %d, time now = %d, qcachetime = %d)\n",
1084 sharename
, (int)last_qscan_time
, (int)time_now
,
1085 (int)lp_lpqcachetime() ));
1087 /* check if another smbd has already sent a message to update the
1088 queue. Give the pending message one minute to clear and
1089 then send another message anyways. Make sure to check for
1090 clocks that have been run forward and then back again. */
1092 snprintf(key
, sizeof(key
), "MSG_PENDING/%s", sharename
);
1095 && tdb_fetch_uint32( pdb
->tdb
, key
, &u
)
1096 && (msg_pending_time
=u
) > 0
1097 && msg_pending_time
<= time_now
1098 && (time_now
- msg_pending_time
) < 60 )
1100 DEBUG(4,("print_cache_expired: message already pending for %s. Accepting cache\n",
1109 release_print_db(pdb
);
1113 /****************************************************************************
1114 main work for updating the lpq cahe for a printer queue
1115 ****************************************************************************/
1117 static void print_queue_update_internal( const char *sharename
,
1118 struct printif
*current_printif
,
1119 char *lpq_command
, char *lprm_command
)
1122 print_queue_struct
*queue
= NULL
;
1123 print_status_struct status
;
1124 print_status_struct old_status
;
1125 struct printjob
*pjob
;
1126 struct traverse_struct tstruct
;
1129 fstring keystr
, cachestr
;
1130 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1136 DEBUG(5,("print_queue_update_internal: printer = %s, type = %d, lpq command = [%s]\n",
1137 sharename
, current_printif
->type
, lpq_command
));
1140 * Update the cache time FIRST ! Stops others even
1141 * attempting to get the lock and doing this
1142 * if the lpq takes a long time.
1145 slprintf(cachestr
, sizeof(cachestr
)-1, "CACHE/%s", sharename
);
1146 tdb_store_int32(pdb
->tdb
, cachestr
, (int)time(NULL
));
1148 /* get the current queue using the appropriate interface */
1149 ZERO_STRUCT(status
);
1151 qcount
= (*(current_printif
->queue_get
))(sharename
,
1152 current_printif
->type
,
1153 lpq_command
, &queue
, &status
);
1155 DEBUG(3, ("print_queue_update_internal: %d job%s in queue for %s\n",
1156 qcount
, (qcount
!= 1) ? "s" : "", sharename
));
1158 /* Sort the queue by submission time otherwise they are displayed
1161 qsort(queue
, qcount
, sizeof(print_queue_struct
),
1162 QSORT_CAST(printjob_comp
));
1165 any job in the internal database that is marked as spooled
1166 and doesn't exist in the system queue is considered finished
1167 and removed from the database
1169 any job in the system database but not in the internal database
1170 is added as a unix job
1172 fill in any system job numbers as we go
1175 jcdata
= get_jobs_changed_data(pdb
);
1177 for (i
=0; i
<qcount
; i
++) {
1178 uint32 jobid
= print_parse_jobid(queue
[i
].fs_file
);
1180 if (jobid
== (uint32
)-1) {
1181 /* assume its a unix print job */
1182 print_unix_job(sharename
, &queue
[i
], jobid
);
1186 /* we have an active SMB print job - update its status */
1187 pjob
= print_job_find(sharename
, jobid
);
1189 /* err, somethings wrong. Probably smbd was restarted
1190 with jobs in the queue. All we can do is treat them
1191 like unix jobs. Pity. */
1192 print_unix_job(sharename
, &queue
[i
], jobid
);
1196 pjob
->sysjob
= queue
[i
].job
;
1198 /* don't reset the status on jobs to be deleted */
1200 if ( pjob
->status
!= LPQ_DELETING
)
1201 pjob
->status
= queue
[i
].status
;
1203 pjob_store(sharename
, jobid
, pjob
);
1205 check_job_changed(sharename
, jcdata
, jobid
);
1208 SAFE_FREE(jcdata
.dptr
);
1210 /* now delete any queued entries that don't appear in the
1212 tstruct
.queue
= queue
;
1213 tstruct
.qcount
= qcount
;
1215 tstruct
.total_jobs
= 0;
1216 tstruct
.lpq_time
= time(NULL
);
1217 tstruct
.sharename
= sharename
;
1218 tstruct
.lprm_command
= lprm_command
;
1219 tstruct
.print_if
= current_printif
;
1221 tdb_traverse(pdb
->tdb
, traverse_fn_delete
, (void *)&tstruct
);
1223 /* Store the linearised queue, max jobs only. */
1224 store_queue_struct(pdb
, &tstruct
);
1226 SAFE_FREE(tstruct
.queue
);
1228 DEBUG(10,("print_queue_update_internal: printer %s INFO/total_jobs = %d\n",
1229 sharename
, tstruct
.total_jobs
));
1231 tdb_store_int32(pdb
->tdb
, "INFO/total_jobs", tstruct
.total_jobs
);
1233 get_queue_status(sharename
, &old_status
);
1234 if (old_status
.qcount
!= qcount
)
1235 DEBUG(10,("print_queue_update_internal: queue status change %d jobs -> %d jobs for printer %s\n",
1236 old_status
.qcount
, qcount
, sharename
));
1238 /* store the new queue status structure */
1239 slprintf(keystr
, sizeof(keystr
)-1, "STATUS/%s", sharename
);
1240 key
= string_tdb_data(keystr
);
1242 status
.qcount
= qcount
;
1243 data
.dptr
= (uint8
*)&status
;
1244 data
.dsize
= sizeof(status
);
1245 tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
);
1248 * Update the cache time again. We want to do this call
1249 * as little as possible...
1252 slprintf(keystr
, sizeof(keystr
)-1, "CACHE/%s", sharename
);
1253 tdb_store_int32(pdb
->tdb
, keystr
, (int32
)time(NULL
));
1255 /* clear the msg pending record for this queue */
1257 snprintf(keystr
, sizeof(keystr
), "MSG_PENDING/%s", sharename
);
1259 if ( !tdb_store_uint32( pdb
->tdb
, keystr
, 0 ) ) {
1260 /* log a message but continue on */
1262 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1266 release_print_db( pdb
);
1271 /****************************************************************************
1272 Update the internal database from the system print queue for a queue.
1273 obtain a lock on the print queue before proceeding (needed when mutiple
1274 smbd processes maytry to update the lpq cache concurrently).
1275 ****************************************************************************/
1277 static void print_queue_update_with_lock( const char *sharename
,
1278 struct printif
*current_printif
,
1279 char *lpq_command
, char *lprm_command
)
1282 struct tdb_print_db
*pdb
;
1284 DEBUG(5,("print_queue_update_with_lock: printer share = %s\n", sharename
));
1285 pdb
= get_print_db_byname(sharename
);
1289 if ( !print_cache_expired(sharename
, False
) ) {
1290 DEBUG(5,("print_queue_update_with_lock: print cache for %s is still ok\n", sharename
));
1291 release_print_db(pdb
);
1296 * Check to see if someone else is doing this update.
1297 * This is essentially a mutex on the update.
1300 if (get_updating_pid(sharename
) != -1) {
1301 release_print_db(pdb
);
1305 /* Lock the queue for the database update */
1307 slprintf(keystr
, sizeof(keystr
) - 1, "LOCK/%s", sharename
);
1308 /* Only wait 10 seconds for this. */
1309 if (tdb_lock_bystring_with_timeout(pdb
->tdb
, keystr
, 10) == -1) {
1310 DEBUG(0,("print_queue_update_with_lock: Failed to lock printer %s database\n", sharename
));
1311 release_print_db(pdb
);
1316 * Ensure that no one else got in here.
1317 * If the updating pid is still -1 then we are
1321 if (get_updating_pid(sharename
) != -1) {
1323 * Someone else is doing the update, exit.
1325 tdb_unlock_bystring(pdb
->tdb
, keystr
);
1326 release_print_db(pdb
);
1331 * We're going to do the update ourselves.
1334 /* Tell others we're doing the update. */
1335 set_updating_pid(sharename
, True
);
1338 * Allow others to enter and notice we're doing
1342 tdb_unlock_bystring(pdb
->tdb
, keystr
);
1344 /* do the main work now */
1346 print_queue_update_internal( sharename
, current_printif
,
1347 lpq_command
, lprm_command
);
1349 /* Delete our pid from the db. */
1350 set_updating_pid(sharename
, False
);
1351 release_print_db(pdb
);
1354 /****************************************************************************
1355 this is the receive function of the background lpq updater
1356 ****************************************************************************/
1357 static void print_queue_receive(struct messaging_context
*msg
,
1360 struct server_id server_id
,
1364 char *lpqcommand
= NULL
, *lprmcommand
= NULL
;
1368 len
= tdb_unpack( (uint8
*)data
->data
, data
->length
, "fdPP",
1375 SAFE_FREE(lpqcommand
);
1376 SAFE_FREE(lprmcommand
);
1377 DEBUG(0,("print_queue_receive: Got invalid print queue update message\n"));
1381 print_queue_update_with_lock(sharename
,
1382 get_printer_fns_from_type((enum printing_types
)printing_type
),
1383 lpqcommand
, lprmcommand
);
1385 SAFE_FREE(lpqcommand
);
1386 SAFE_FREE(lprmcommand
);
1390 static pid_t background_lpq_updater_pid
= -1;
1392 /****************************************************************************
1393 main thread of the background lpq updater
1394 ****************************************************************************/
1395 void start_background_queue(void)
1397 /* Use local variables for this as we don't
1398 * need to save the parent side of this, just
1399 * ensure it closes when the process exits.
1403 DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
1405 if (pipe(pause_pipe
) == -1) {
1406 DEBUG(5,("start_background_queue: cannot create pipe. %s\n", strerror(errno
) ));
1410 background_lpq_updater_pid
= sys_fork();
1412 if (background_lpq_updater_pid
== -1) {
1413 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno
) ));
1417 if(background_lpq_updater_pid
== 0) {
1419 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
1421 close(pause_pipe
[0]);
1424 if (!reinit_after_fork(smbd_messaging_context(), true)) {
1425 DEBUG(0,("reinit_after_fork() failed\n"));
1426 smb_panic("reinit_after_fork() failed");
1429 claim_connection( NULL
, "smbd lpq backend",
1430 FLAG_MSG_GENERAL
|FLAG_MSG_SMBD
|FLAG_MSG_PRINT_GENERAL
);
1432 if (!locking_init()) {
1436 messaging_register(smbd_messaging_context(), NULL
,
1437 MSG_PRINTER_UPDATE
, print_queue_receive
);
1439 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
1444 FD_ZERO(&pause_fds
);
1445 FD_SET(pause_pipe
[1], &pause_fds
);
1446 pause_select
= sys_select(pause_pipe
[1]+1, &pause_fds
, NULL
, NULL
, NULL
);
1447 /* If pause_pipe[0] is closed it means the parent smbd
1448 * and children exited or aborted. */
1449 if (pause_select
== 1) {
1450 exit_server_cleanly(NULL
);
1453 /* check for some essential signals first */
1456 exit_server_cleanly(NULL
);
1459 if (reload_after_sighup
) {
1460 change_to_root_user();
1461 DEBUG(1,("Reloading services after SIGHUP\n"));
1462 reload_services(False
);
1463 reload_after_sighup
= 0;
1466 /* now check for messages */
1468 DEBUG(10,("start_background_queue: background LPQ thread got a message\n"));
1469 message_dispatch(smbd_messaging_context());
1471 /* process any pending print change notify messages */
1473 print_notify_send_messages(smbd_messaging_context(),
1478 close(pause_pipe
[1]);
1481 /****************************************************************************
1482 update the internal database from the system print queue for a queue
1483 ****************************************************************************/
1485 static void print_queue_update(int snum
, bool force
)
1489 char *lpqcommand
= NULL
;
1490 char *lprmcommand
= NULL
;
1491 uint8
*buffer
= NULL
;
1494 struct tdb_print_db
*pdb
;
1496 struct printif
*current_printif
;
1497 TALLOC_CTX
*ctx
= talloc_tos();
1499 fstrcpy( sharename
, lp_const_servicename(snum
));
1501 /* don't strip out characters like '$' from the printername */
1503 lpqcommand
= talloc_string_sub2(ctx
,
1504 lp_lpqcommand(snum
),
1507 false, false, false);
1511 lpqcommand
= talloc_sub_advanced(ctx
,
1512 lp_servicename(snum
),
1513 current_user_info
.unix_name
,
1515 current_user
.ut
.gid
,
1516 get_current_username(),
1517 current_user_info
.domain
,
1523 lprmcommand
= talloc_string_sub2(ctx
,
1524 lp_lprmcommand(snum
),
1527 false, false, false);
1531 lprmcommand
= talloc_sub_advanced(ctx
,
1532 lp_servicename(snum
),
1533 current_user_info
.unix_name
,
1535 current_user
.ut
.gid
,
1536 get_current_username(),
1537 current_user_info
.domain
,
1544 * Make sure that the background queue process exists.
1545 * Otherwise just do the update ourselves
1548 if ( force
|| background_lpq_updater_pid
== -1 ) {
1549 DEBUG(4,("print_queue_update: updating queue [%s] myself\n", sharename
));
1550 current_printif
= get_printer_fns( snum
);
1551 print_queue_update_with_lock( sharename
, current_printif
, lpqcommand
, lprmcommand
);
1556 type
= lp_printing(snum
);
1558 /* get the length */
1560 len
= tdb_pack( NULL
, 0, "fdPP",
1566 buffer
= SMB_XMALLOC_ARRAY( uint8
, len
);
1568 /* now pack the buffer */
1569 newlen
= tdb_pack( buffer
, len
, "fdPP",
1575 SMB_ASSERT( newlen
== len
);
1577 DEBUG(10,("print_queue_update: Sending message -> printer = %s, "
1578 "type = %d, lpq command = [%s] lprm command = [%s]\n",
1579 sharename
, type
, lpqcommand
, lprmcommand
));
1581 /* here we set a msg pending record for other smbd processes
1582 to throttle the number of duplicate print_queue_update msgs
1585 pdb
= get_print_db_byname(sharename
);
1591 snprintf(key
, sizeof(key
), "MSG_PENDING/%s", sharename
);
1593 if ( !tdb_store_uint32( pdb
->tdb
, key
, time(NULL
) ) ) {
1594 /* log a message but continue on */
1596 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1600 release_print_db( pdb
);
1602 /* finally send the message */
1604 messaging_send_buf(smbd_messaging_context(),
1605 pid_to_procid(background_lpq_updater_pid
),
1606 MSG_PRINTER_UPDATE
, (uint8
*)buffer
, len
);
1608 SAFE_FREE( buffer
);
1613 /****************************************************************************
1614 Create/Update an entry in the print tdb that will allow us to send notify
1615 updates only to interested smbd's.
1616 ****************************************************************************/
1618 bool print_notify_register_pid(int snum
)
1621 struct tdb_print_db
*pdb
= NULL
;
1622 TDB_CONTEXT
*tdb
= NULL
;
1623 const char *printername
;
1624 uint32 mypid
= (uint32
)sys_getpid();
1628 /* if (snum == -1), then the change notify request was
1629 on a print server handle and we need to register on
1634 int num_services
= lp_numservices();
1637 for ( idx
=0; idx
<num_services
; idx
++ ) {
1638 if (lp_snum_ok(idx
) && lp_print_ok(idx
) )
1639 print_notify_register_pid(idx
);
1644 else /* register for a specific printer */
1646 printername
= lp_const_servicename(snum
);
1647 pdb
= get_print_db_byname(printername
);
1653 if (tdb_lock_bystring_with_timeout(tdb
, NOTIFY_PID_LIST_KEY
, 10) == -1) {
1654 DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n",
1657 release_print_db(pdb
);
1661 data
= get_printer_notify_pid_list( tdb
, printername
, True
);
1663 /* Add ourselves and increase the refcount. */
1665 for (i
= 0; i
< data
.dsize
; i
+= 8) {
1666 if (IVAL(data
.dptr
,i
) == mypid
) {
1667 uint32 new_refcount
= IVAL(data
.dptr
, i
+4) + 1;
1668 SIVAL(data
.dptr
, i
+4, new_refcount
);
1673 if (i
== data
.dsize
) {
1674 /* We weren't in the list. Realloc. */
1675 data
.dptr
= (uint8
*)SMB_REALLOC(data
.dptr
, data
.dsize
+ 8);
1677 DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n",
1682 SIVAL(data
.dptr
,data
.dsize
- 8,mypid
);
1683 SIVAL(data
.dptr
,data
.dsize
- 4,1); /* Refcount. */
1686 /* Store back the record. */
1687 if (tdb_store_bystring(tdb
, NOTIFY_PID_LIST_KEY
, data
, TDB_REPLACE
) == -1) {
1688 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1689 list for printer %s\n", printername
));
1697 tdb_unlock_bystring(tdb
, NOTIFY_PID_LIST_KEY
);
1699 release_print_db(pdb
);
1700 SAFE_FREE(data
.dptr
);
1704 /****************************************************************************
1705 Update an entry in the print tdb that will allow us to send notify
1706 updates only to interested smbd's.
1707 ****************************************************************************/
1709 bool print_notify_deregister_pid(int snum
)
1712 struct tdb_print_db
*pdb
= NULL
;
1713 TDB_CONTEXT
*tdb
= NULL
;
1714 const char *printername
;
1715 uint32 mypid
= (uint32
)sys_getpid();
1719 /* if ( snum == -1 ), we are deregister a print server handle
1720 which means to deregister on all print queues */
1724 int num_services
= lp_numservices();
1727 for ( idx
=0; idx
<num_services
; idx
++ ) {
1728 if ( lp_snum_ok(idx
) && lp_print_ok(idx
) )
1729 print_notify_deregister_pid(idx
);
1734 else /* deregister a specific printer */
1736 printername
= lp_const_servicename(snum
);
1737 pdb
= get_print_db_byname(printername
);
1743 if (tdb_lock_bystring_with_timeout(tdb
, NOTIFY_PID_LIST_KEY
, 10) == -1) {
1744 DEBUG(0,("print_notify_register_pid: Failed to lock \
1745 printer %s database\n", printername
));
1747 release_print_db(pdb
);
1751 data
= get_printer_notify_pid_list( tdb
, printername
, True
);
1753 /* Reduce refcount. Remove ourselves if zero. */
1755 for (i
= 0; i
< data
.dsize
; ) {
1756 if (IVAL(data
.dptr
,i
) == mypid
) {
1757 uint32 refcount
= IVAL(data
.dptr
, i
+4);
1761 if (refcount
== 0) {
1762 if (data
.dsize
- i
> 8)
1763 memmove( &data
.dptr
[i
], &data
.dptr
[i
+8], data
.dsize
- i
- 8);
1767 SIVAL(data
.dptr
, i
+4, refcount
);
1773 if (data
.dsize
== 0)
1774 SAFE_FREE(data
.dptr
);
1776 /* Store back the record. */
1777 if (tdb_store_bystring(tdb
, NOTIFY_PID_LIST_KEY
, data
, TDB_REPLACE
) == -1) {
1778 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1779 list for printer %s\n", printername
));
1787 tdb_unlock_bystring(tdb
, NOTIFY_PID_LIST_KEY
);
1789 release_print_db(pdb
);
1790 SAFE_FREE(data
.dptr
);
1794 /****************************************************************************
1795 Check if a jobid is valid. It is valid if it exists in the database.
1796 ****************************************************************************/
1798 bool print_job_exists(const char* sharename
, uint32 jobid
)
1800 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1806 ret
= tdb_exists(pdb
->tdb
, print_key(jobid
, &tmp
));
1807 release_print_db(pdb
);
1811 /****************************************************************************
1812 Give the fd used for a jobid.
1813 ****************************************************************************/
1815 int print_job_fd(const char* sharename
, uint32 jobid
)
1817 struct printjob
*pjob
= print_job_find(sharename
, jobid
);
1820 /* don't allow another process to get this info - it is meaningless */
1821 if (pjob
->pid
!= sys_getpid())
1826 /****************************************************************************
1827 Give the filename used for a jobid.
1828 Only valid for the process doing the spooling and when the job
1829 has not been spooled.
1830 ****************************************************************************/
1832 char *print_job_fname(const char* sharename
, uint32 jobid
)
1834 struct printjob
*pjob
= print_job_find(sharename
, jobid
);
1835 if (!pjob
|| pjob
->spooled
|| pjob
->pid
!= sys_getpid())
1837 return pjob
->filename
;
1841 /****************************************************************************
1842 Give the filename used for a jobid.
1843 Only valid for the process doing the spooling and when the job
1844 has not been spooled.
1845 ****************************************************************************/
1847 NT_DEVICEMODE
*print_job_devmode(const char* sharename
, uint32 jobid
)
1849 struct printjob
*pjob
= print_job_find(sharename
, jobid
);
1854 return pjob
->nt_devmode
;
1857 /****************************************************************************
1858 Set the place in the queue for a job.
1859 ****************************************************************************/
1861 bool print_job_set_place(const char *sharename
, uint32 jobid
, int place
)
1863 DEBUG(2,("print_job_set_place not implemented yet\n"));
1867 /****************************************************************************
1868 Set the name of a job. Only possible for owner.
1869 ****************************************************************************/
1871 bool print_job_set_name(const char *sharename
, uint32 jobid
, char *name
)
1873 struct printjob
*pjob
;
1875 pjob
= print_job_find(sharename
, jobid
);
1876 if (!pjob
|| pjob
->pid
!= sys_getpid())
1879 fstrcpy(pjob
->jobname
, name
);
1880 return pjob_store(sharename
, jobid
, pjob
);
1883 /***************************************************************************
1884 Remove a jobid from the 'jobs changed' list.
1885 ***************************************************************************/
1887 static bool remove_from_jobs_changed(const char* sharename
, uint32 jobid
)
1889 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1891 size_t job_count
, i
;
1893 bool gotlock
= False
;
1901 key
= string_tdb_data("INFO/jobs_changed");
1903 if (tdb_chainlock_with_timeout(pdb
->tdb
, key
, 5) == -1)
1908 data
= tdb_fetch(pdb
->tdb
, key
);
1910 if (data
.dptr
== NULL
|| data
.dsize
== 0 || (data
.dsize
% 4 != 0))
1913 job_count
= data
.dsize
/ 4;
1914 for (i
= 0; i
< job_count
; i
++) {
1917 ch_jobid
= IVAL(data
.dptr
, i
*4);
1918 if (ch_jobid
== jobid
) {
1919 if (i
< job_count
-1 )
1920 memmove(data
.dptr
+ (i
*4), data
.dptr
+ (i
*4) + 4, (job_count
- i
- 1)*4 );
1922 if (tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
) == -1)
1932 tdb_chainunlock(pdb
->tdb
, key
);
1933 SAFE_FREE(data
.dptr
);
1934 release_print_db(pdb
);
1936 DEBUG(10,("remove_from_jobs_changed: removed jobid %u\n", (unsigned int)jobid
));
1938 DEBUG(10,("remove_from_jobs_changed: Failed to remove jobid %u\n", (unsigned int)jobid
));
1942 /****************************************************************************
1943 Delete a print job - don't update queue.
1944 ****************************************************************************/
1946 static bool print_job_delete1(int snum
, uint32 jobid
)
1948 const char* sharename
= lp_const_servicename(snum
);
1949 struct printjob
*pjob
= print_job_find(sharename
, jobid
);
1951 struct printif
*current_printif
= get_printer_fns( snum
);
1957 * If already deleting just return.
1960 if (pjob
->status
== LPQ_DELETING
)
1963 /* Hrm - we need to be able to cope with deleting a job before it
1964 has reached the spooler. Just mark it as LPQ_DELETING and
1965 let the print_queue_update() code rmeove the record */
1968 if (pjob
->sysjob
== -1) {
1969 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid
));
1972 /* Set the tdb entry to be deleting. */
1974 pjob
->status
= LPQ_DELETING
;
1975 pjob_store(sharename
, jobid
, pjob
);
1977 if (pjob
->spooled
&& pjob
->sysjob
!= -1)
1979 result
= (*(current_printif
->job_delete
))(
1981 lp_lprmcommand(snum
),
1984 /* Delete the tdb entry if the delete succeeded or the job hasn't
1988 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1993 pjob_delete(sharename
, jobid
);
1994 /* Ensure we keep a rough count of the number of total jobs... */
1995 tdb_change_int32_atomic(pdb
->tdb
, "INFO/total_jobs", &njobs
, -1);
1996 release_print_db(pdb
);
2000 remove_from_jobs_changed( sharename
, jobid
);
2002 return (result
== 0);
2005 /****************************************************************************
2006 Return true if the current user owns the print job.
2007 ****************************************************************************/
2009 static bool is_owner(struct auth_serversupplied_info
*server_info
,
2010 const char *servicename
,
2013 struct printjob
*pjob
= print_job_find(servicename
, jobid
);
2015 if (!pjob
|| !server_info
)
2018 return strequal(pjob
->user
, server_info
->sanitized_username
);
2021 /****************************************************************************
2023 ****************************************************************************/
2025 bool print_job_delete(struct auth_serversupplied_info
*server_info
, int snum
,
2026 uint32 jobid
, WERROR
*errcode
)
2028 const char* sharename
= lp_const_servicename( snum
);
2029 struct printjob
*pjob
;
2035 owner
= is_owner(server_info
, lp_const_servicename(snum
), jobid
);
2037 /* Check access against security descriptor or whether the user
2041 !print_access_check(server_info
, snum
, JOB_ACCESS_ADMINISTER
)) {
2042 DEBUG(3, ("delete denied by security descriptor\n"));
2043 *errcode
= WERR_ACCESS_DENIED
;
2045 /* BEGIN_ADMIN_LOG */
2046 sys_adminlog( LOG_ERR
,
2047 "Permission denied-- user not allowed to delete, \
2048 pause, or resume print job. User name: %s. Printer name: %s.",
2049 uidtoname(server_info
->utok
.uid
),
2050 PRINTERNAME(snum
) );
2057 * get the spooled filename of the print job
2058 * if this works, then the file has not been spooled
2059 * to the underlying print system. Just delete the
2060 * spool file & return.
2063 if ( (fname
= print_job_fname( sharename
, jobid
)) != NULL
)
2065 /* remove the spool file */
2066 DEBUG(10,("print_job_delete: Removing spool file [%s]\n", fname
));
2067 if ( unlink( fname
) == -1 ) {
2068 *errcode
= map_werror_from_unix(errno
);
2073 if (!print_job_delete1(snum
, jobid
)) {
2074 *errcode
= WERR_ACCESS_DENIED
;
2078 /* force update the database and say the delete failed if the
2081 print_queue_update(snum
, True
);
2083 pjob
= print_job_find(sharename
, jobid
);
2084 if ( pjob
&& (pjob
->status
!= LPQ_DELETING
) )
2085 *errcode
= WERR_ACCESS_DENIED
;
2087 return (pjob
== NULL
);
2090 /****************************************************************************
2092 ****************************************************************************/
2094 bool print_job_pause(struct auth_serversupplied_info
*server_info
, int snum
,
2095 uint32 jobid
, WERROR
*errcode
)
2097 const char* sharename
= lp_const_servicename(snum
);
2098 struct printjob
*pjob
;
2100 struct printif
*current_printif
= get_printer_fns( snum
);
2102 pjob
= print_job_find(sharename
, jobid
);
2104 if (!pjob
|| !server_info
) {
2105 DEBUG(10, ("print_job_pause: no pjob or user for jobid %u\n",
2106 (unsigned int)jobid
));
2110 if (!pjob
->spooled
|| pjob
->sysjob
== -1) {
2111 DEBUG(10, ("print_job_pause: not spooled or bad sysjob = %d for jobid %u\n",
2112 (int)pjob
->sysjob
, (unsigned int)jobid
));
2116 if (!is_owner(server_info
, lp_const_servicename(snum
), jobid
) &&
2117 !print_access_check(server_info
, snum
, JOB_ACCESS_ADMINISTER
)) {
2118 DEBUG(3, ("pause denied by security descriptor\n"));
2120 /* BEGIN_ADMIN_LOG */
2121 sys_adminlog( LOG_ERR
,
2122 "Permission denied-- user not allowed to delete, \
2123 pause, or resume print job. User name: %s. Printer name: %s.",
2124 uidtoname(server_info
->utok
.uid
),
2125 PRINTERNAME(snum
) );
2128 *errcode
= WERR_ACCESS_DENIED
;
2132 /* need to pause the spooled entry */
2133 ret
= (*(current_printif
->job_pause
))(snum
, pjob
);
2136 *errcode
= WERR_INVALID_PARAM
;
2140 /* force update the database */
2141 print_cache_flush(lp_const_servicename(snum
));
2143 /* Send a printer notify message */
2145 notify_job_status(sharename
, jobid
, JOB_STATUS_PAUSED
);
2147 /* how do we tell if this succeeded? */
2152 /****************************************************************************
2154 ****************************************************************************/
2156 bool print_job_resume(struct auth_serversupplied_info
*server_info
, int snum
,
2157 uint32 jobid
, WERROR
*errcode
)
2159 const char *sharename
= lp_const_servicename(snum
);
2160 struct printjob
*pjob
;
2162 struct printif
*current_printif
= get_printer_fns( snum
);
2164 pjob
= print_job_find(sharename
, jobid
);
2166 if (!pjob
|| !server_info
) {
2167 DEBUG(10, ("print_job_resume: no pjob or user for jobid %u\n",
2168 (unsigned int)jobid
));
2172 if (!pjob
->spooled
|| pjob
->sysjob
== -1) {
2173 DEBUG(10, ("print_job_resume: not spooled or bad sysjob = %d for jobid %u\n",
2174 (int)pjob
->sysjob
, (unsigned int)jobid
));
2178 if (!is_owner(server_info
, lp_const_servicename(snum
), jobid
) &&
2179 !print_access_check(server_info
, snum
, JOB_ACCESS_ADMINISTER
)) {
2180 DEBUG(3, ("resume denied by security descriptor\n"));
2181 *errcode
= WERR_ACCESS_DENIED
;
2183 /* BEGIN_ADMIN_LOG */
2184 sys_adminlog( LOG_ERR
,
2185 "Permission denied-- user not allowed to delete, \
2186 pause, or resume print job. User name: %s. Printer name: %s.",
2187 uidtoname(server_info
->utok
.uid
),
2188 PRINTERNAME(snum
) );
2193 ret
= (*(current_printif
->job_resume
))(snum
, pjob
);
2196 *errcode
= WERR_INVALID_PARAM
;
2200 /* force update the database */
2201 print_cache_flush(lp_const_servicename(snum
));
2203 /* Send a printer notify message */
2205 notify_job_status(sharename
, jobid
, JOB_STATUS_QUEUED
);
2210 /****************************************************************************
2211 Write to a print file.
2212 ****************************************************************************/
2214 ssize_t
print_job_write(int snum
, uint32 jobid
, const char *buf
, SMB_OFF_T pos
, size_t size
)
2216 const char* sharename
= lp_const_servicename(snum
);
2218 struct printjob
*pjob
;
2220 pjob
= print_job_find(sharename
, jobid
);
2224 /* don't allow another process to get this info - it is meaningless */
2225 if (pjob
->pid
!= sys_getpid())
2228 return_code
= write_data_at_offset(pjob
->fd
, buf
, size
, pos
);
2230 if (return_code
>0) {
2232 pjob_store(sharename
, jobid
, pjob
);
2237 /****************************************************************************
2238 Get the queue status - do not update if db is out of date.
2239 ****************************************************************************/
2241 static int get_queue_status(const char* sharename
, print_status_struct
*status
)
2245 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
2249 ZERO_STRUCTP(status
);
2256 fstr_sprintf(keystr
, "STATUS/%s", sharename
);
2257 data
= tdb_fetch(pdb
->tdb
, string_tdb_data(keystr
));
2259 if (data
.dsize
== sizeof(print_status_struct
))
2260 /* this memcpy is ok since the status struct was
2261 not packed before storing it in the tdb */
2262 memcpy(status
, data
.dptr
, sizeof(print_status_struct
));
2263 SAFE_FREE(data
.dptr
);
2266 len
= tdb_fetch_int32(pdb
->tdb
, "INFO/total_jobs");
2267 release_print_db(pdb
);
2268 return (len
== -1 ? 0 : len
);
2271 /****************************************************************************
2272 Determine the number of jobs in a queue.
2273 ****************************************************************************/
2275 int print_queue_length(int snum
, print_status_struct
*pstatus
)
2277 const char* sharename
= lp_const_servicename( snum
);
2278 print_status_struct status
;
2281 ZERO_STRUCT( status
);
2283 /* make sure the database is up to date */
2284 if (print_cache_expired(lp_const_servicename(snum
), True
))
2285 print_queue_update(snum
, False
);
2287 /* also fetch the queue status */
2288 memset(&status
, 0, sizeof(status
));
2289 len
= get_queue_status(sharename
, &status
);
2297 /***************************************************************************
2298 Allocate a jobid. Hold the lock for as short a time as possible.
2299 ***************************************************************************/
2301 static bool allocate_print_jobid(struct tdb_print_db
*pdb
, int snum
, const char *sharename
, uint32
*pjobid
)
2306 *pjobid
= (uint32
)-1;
2308 for (i
= 0; i
< 3; i
++) {
2309 /* Lock the database - only wait 20 seconds. */
2310 if (tdb_lock_bystring_with_timeout(pdb
->tdb
, "INFO/nextjob", 20) == -1) {
2311 DEBUG(0,("allocate_print_jobid: failed to lock printing database %s\n", sharename
));
2315 if (!tdb_fetch_uint32(pdb
->tdb
, "INFO/nextjob", &jobid
)) {
2316 if (tdb_error(pdb
->tdb
) != TDB_ERR_NOEXIST
) {
2317 DEBUG(0, ("allocate_print_jobid: failed to fetch INFO/nextjob for print queue %s\n",
2324 jobid
= NEXT_JOBID(jobid
);
2326 if (tdb_store_int32(pdb
->tdb
, "INFO/nextjob", jobid
)==-1) {
2327 DEBUG(3, ("allocate_print_jobid: failed to store INFO/nextjob.\n"));
2328 tdb_unlock_bystring(pdb
->tdb
, "INFO/nextjob");
2332 /* We've finished with the INFO/nextjob lock. */
2333 tdb_unlock_bystring(pdb
->tdb
, "INFO/nextjob");
2335 if (!print_job_exists(sharename
, jobid
))
2340 DEBUG(0, ("allocate_print_jobid: failed to allocate a print job for queue %s\n",
2342 /* Probably full... */
2347 /* Store a dummy placeholder. */
2353 if (tdb_store(pdb
->tdb
, print_key(jobid
, &tmp
), dum
,
2354 TDB_INSERT
) == -1) {
2355 DEBUG(3, ("allocate_print_jobid: jobid (%d) failed to store placeholder.\n",
2365 /***************************************************************************
2366 Append a jobid to the 'jobs changed' list.
2367 ***************************************************************************/
2369 static bool add_to_jobs_changed(struct tdb_print_db
*pdb
, uint32 jobid
)
2374 SIVAL(&store_jobid
, 0, jobid
);
2375 data
.dptr
= (uint8
*)&store_jobid
;
2378 DEBUG(10,("add_to_jobs_changed: Added jobid %u\n", (unsigned int)jobid
));
2380 return (tdb_append(pdb
->tdb
, string_tdb_data("INFO/jobs_changed"),
2384 /***************************************************************************
2385 Start spooling a job - return the jobid.
2386 ***************************************************************************/
2388 uint32
print_job_start(struct auth_serversupplied_info
*server_info
, int snum
,
2389 char *jobname
, NT_DEVICEMODE
*nt_devmode
)
2393 struct printjob pjob
;
2394 const char *sharename
= lp_const_servicename(snum
);
2395 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
2403 if (!print_access_check(server_info
, snum
, PRINTER_ACCESS_USE
)) {
2404 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
2405 release_print_db(pdb
);
2409 if (!print_time_access_check(lp_servicename(snum
))) {
2410 DEBUG(3, ("print_job_start: job start denied by time check\n"));
2411 release_print_db(pdb
);
2415 path
= lp_pathname(snum
);
2417 /* see if we have sufficient disk space */
2418 if (lp_minprintspace(snum
)) {
2419 uint64_t dspace
, dsize
;
2420 if (sys_fsusage(path
, &dspace
, &dsize
) == 0 &&
2421 dspace
< 2*(uint64_t)lp_minprintspace(snum
)) {
2422 DEBUG(3, ("print_job_start: disk space check failed.\n"));
2423 release_print_db(pdb
);
2429 /* for autoloaded printers, check that the printcap entry still exists */
2430 if (lp_autoloaded(snum
) && !pcap_printername_ok(lp_const_servicename(snum
))) {
2431 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum
) ));
2432 release_print_db(pdb
);
2437 /* Insure the maximum queue size is not violated */
2438 if ((njobs
= print_queue_length(snum
,NULL
)) > lp_maxprintjobs(snum
)) {
2439 DEBUG(3, ("print_job_start: Queue %s number of jobs (%d) larger than max printjobs per queue (%d).\n",
2440 sharename
, njobs
, lp_maxprintjobs(snum
) ));
2441 release_print_db(pdb
);
2446 DEBUG(10,("print_job_start: Queue %s number of jobs (%d), max printjobs = %d\n",
2447 sharename
, njobs
, lp_maxprintjobs(snum
) ));
2449 if (!allocate_print_jobid(pdb
, snum
, sharename
, &jobid
))
2452 /* create the database entry */
2456 pjob
.pid
= sys_getpid();
2459 pjob
.starttime
= time(NULL
);
2460 pjob
.status
= LPQ_SPOOLING
;
2462 pjob
.spooled
= False
;
2464 pjob
.nt_devmode
= nt_devmode
;
2466 fstrcpy(pjob
.jobname
, jobname
);
2468 fstrcpy(pjob
.user
, lp_printjob_username(snum
));
2469 standard_sub_advanced(sharename
, server_info
->sanitized_username
,
2470 path
, server_info
->utok
.gid
,
2471 server_info
->sanitized_username
,
2472 pdb_get_domain(server_info
->sam_account
),
2473 pjob
.user
, sizeof(pjob
.user
)-1);
2474 /* ensure NULL termination */
2475 pjob
.user
[sizeof(pjob
.user
)-1] = '\0';
2477 fstrcpy(pjob
.queuename
, lp_const_servicename(snum
));
2479 /* we have a job entry - now create the spool file */
2480 slprintf(pjob
.filename
, sizeof(pjob
.filename
)-1, "%s/%s%.8u.XXXXXX",
2481 path
, PRINT_SPOOL_PREFIX
, (unsigned int)jobid
);
2482 pjob
.fd
= smb_mkstemp(pjob
.filename
);
2484 if (pjob
.fd
== -1) {
2485 if (errno
== EACCES
) {
2486 /* Common setup error, force a report. */
2487 DEBUG(0, ("print_job_start: insufficient permissions \
2488 to open spool file %s.\n", pjob
.filename
));
2490 /* Normal case, report at level 3 and above. */
2491 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob
.filename
));
2492 DEBUGADD(3, ("errno = %d (%s).\n", errno
, strerror(errno
)));
2497 pjob_store(sharename
, jobid
, &pjob
);
2499 /* Update the 'jobs changed' entry used by print_queue_status. */
2500 add_to_jobs_changed(pdb
, jobid
);
2502 /* Ensure we keep a rough count of the number of total jobs... */
2503 tdb_change_int32_atomic(pdb
->tdb
, "INFO/total_jobs", &njobs
, 1);
2505 release_print_db(pdb
);
2511 pjob_delete(sharename
, jobid
);
2513 release_print_db(pdb
);
2515 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno
) ));
2519 /****************************************************************************
2520 Update the number of pages spooled to jobid
2521 ****************************************************************************/
2523 void print_job_endpage(int snum
, uint32 jobid
)
2525 const char* sharename
= lp_const_servicename(snum
);
2526 struct printjob
*pjob
;
2528 pjob
= print_job_find(sharename
, jobid
);
2531 /* don't allow another process to get this info - it is meaningless */
2532 if (pjob
->pid
!= sys_getpid())
2536 pjob_store(sharename
, jobid
, pjob
);
2539 /****************************************************************************
2540 Print a file - called on closing the file. This spools the job.
2541 If normal close is false then we're tearing down the jobs - treat as an
2543 ****************************************************************************/
2545 bool print_job_end(int snum
, uint32 jobid
, enum file_close_type close_type
)
2547 const char* sharename
= lp_const_servicename(snum
);
2548 struct printjob
*pjob
;
2550 SMB_STRUCT_STAT sbuf
;
2551 struct printif
*current_printif
= get_printer_fns( snum
);
2553 pjob
= print_job_find(sharename
, jobid
);
2558 if (pjob
->spooled
|| pjob
->pid
!= sys_getpid())
2561 if ((close_type
== NORMAL_CLOSE
|| close_type
== SHUTDOWN_CLOSE
) &&
2562 (sys_fstat(pjob
->fd
, &sbuf
) == 0)) {
2563 pjob
->size
= sbuf
.st_size
;
2569 * Not a normal close or we couldn't stat the job file,
2570 * so something has gone wrong. Cleanup.
2574 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid
));
2578 /* Technically, this is not quite right. If the printer has a separator
2579 * page turned on, the NT spooler prints the separator page even if the
2580 * print job is 0 bytes. 010215 JRR */
2581 if (pjob
->size
== 0 || pjob
->status
== LPQ_DELETING
) {
2582 /* don't bother spooling empty files or something being deleted. */
2583 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
2584 pjob
->filename
, pjob
->size
? "deleted" : "zero length" ));
2585 unlink(pjob
->filename
);
2586 pjob_delete(sharename
, jobid
);
2590 pjob
->smbjob
= jobid
;
2592 ret
= (*(current_printif
->job_submit
))(snum
, pjob
);
2597 /* The print job has been successfully handed over to the back-end */
2599 pjob
->spooled
= True
;
2600 pjob
->status
= LPQ_QUEUED
;
2601 pjob_store(sharename
, jobid
, pjob
);
2603 /* make sure the database is up to date */
2604 if (print_cache_expired(lp_const_servicename(snum
), True
))
2605 print_queue_update(snum
, False
);
2611 /* The print job was not successfully started. Cleanup */
2612 /* Still need to add proper error return propagation! 010122:JRR */
2613 unlink(pjob
->filename
);
2614 pjob_delete(sharename
, jobid
);
2618 /****************************************************************************
2619 Get a snapshot of jobs in the system without traversing.
2620 ****************************************************************************/
2622 static bool get_stored_queue_info(struct tdb_print_db
*pdb
, int snum
, int *pcount
, print_queue_struct
**ppqueue
)
2624 TDB_DATA data
, cgdata
;
2625 print_queue_struct
*queue
= NULL
;
2627 uint32 extra_count
= 0;
2628 int total_count
= 0;
2631 int max_reported_jobs
= lp_max_reported_jobs(snum
);
2633 const char* sharename
= lp_servicename(snum
);
2635 /* make sure the database is up to date */
2636 if (print_cache_expired(lp_const_servicename(snum
), True
))
2637 print_queue_update(snum
, False
);
2643 ZERO_STRUCT(cgdata
);
2645 /* Get the stored queue data. */
2646 data
= tdb_fetch(pdb
->tdb
, string_tdb_data("INFO/linear_queue_array"));
2648 if (data
.dptr
&& data
.dsize
>= sizeof(qcount
))
2649 len
+= tdb_unpack(data
.dptr
+ len
, data
.dsize
- len
, "d", &qcount
);
2651 /* Get the changed jobs list. */
2652 cgdata
= tdb_fetch(pdb
->tdb
, string_tdb_data("INFO/jobs_changed"));
2653 if (cgdata
.dptr
!= NULL
&& (cgdata
.dsize
% 4 == 0))
2654 extra_count
= cgdata
.dsize
/4;
2656 DEBUG(5,("get_stored_queue_info: qcount = %u, extra_count = %u\n", (unsigned int)qcount
, (unsigned int)extra_count
));
2658 /* Allocate the queue size. */
2659 if (qcount
== 0 && extra_count
== 0)
2662 if ((queue
= SMB_MALLOC_ARRAY(print_queue_struct
, qcount
+ extra_count
)) == NULL
)
2665 /* Retrieve the linearised queue data. */
2667 for( i
= 0; i
< qcount
; i
++) {
2668 uint32 qjob
, qsize
, qpage_count
, qstatus
, qpriority
, qtime
;
2669 len
+= tdb_unpack(data
.dptr
+ len
, data
.dsize
- len
, "ddddddff",
2678 queue
[i
].job
= qjob
;
2679 queue
[i
].size
= qsize
;
2680 queue
[i
].page_count
= qpage_count
;
2681 queue
[i
].status
= qstatus
;
2682 queue
[i
].priority
= qpriority
;
2683 queue
[i
].time
= qtime
;
2686 total_count
= qcount
;
2688 /* Add in the changed jobids. */
2689 for( i
= 0; i
< extra_count
; i
++) {
2691 struct printjob
*pjob
;
2693 jobid
= IVAL(cgdata
.dptr
, i
*4);
2694 DEBUG(5,("get_stored_queue_info: changed job = %u\n", (unsigned int)jobid
));
2695 pjob
= print_job_find(lp_const_servicename(snum
), jobid
);
2697 DEBUG(5,("get_stored_queue_info: failed to find changed job = %u\n", (unsigned int)jobid
));
2698 remove_from_jobs_changed(sharename
, jobid
);
2702 queue
[total_count
].job
= jobid
;
2703 queue
[total_count
].size
= pjob
->size
;
2704 queue
[total_count
].page_count
= pjob
->page_count
;
2705 queue
[total_count
].status
= pjob
->status
;
2706 queue
[total_count
].priority
= 1;
2707 queue
[total_count
].time
= pjob
->starttime
;
2708 fstrcpy(queue
[total_count
].fs_user
, pjob
->user
);
2709 fstrcpy(queue
[total_count
].fs_file
, pjob
->jobname
);
2713 /* Sort the queue by submission time otherwise they are displayed
2716 qsort(queue
, total_count
, sizeof(print_queue_struct
), QSORT_CAST(printjob_comp
));
2718 DEBUG(5,("get_stored_queue_info: total_count = %u\n", (unsigned int)total_count
));
2720 if (max_reported_jobs
&& total_count
> max_reported_jobs
)
2721 total_count
= max_reported_jobs
;
2724 *pcount
= total_count
;
2730 SAFE_FREE(data
.dptr
);
2731 SAFE_FREE(cgdata
.dptr
);
2735 /****************************************************************************
2736 Get a printer queue listing.
2737 set queue = NULL and status = NULL if you just want to update the cache
2738 ****************************************************************************/
2740 int print_queue_status(int snum
,
2741 print_queue_struct
**ppqueue
,
2742 print_status_struct
*status
)
2746 const char *sharename
;
2747 struct tdb_print_db
*pdb
;
2750 /* make sure the database is up to date */
2752 if (print_cache_expired(lp_const_servicename(snum
), True
))
2753 print_queue_update(snum
, False
);
2755 /* return if we are done */
2756 if ( !ppqueue
|| !status
)
2760 sharename
= lp_const_servicename(snum
);
2761 pdb
= get_print_db_byname(sharename
);
2767 * Fetch the queue status. We must do this first, as there may
2768 * be no jobs in the queue.
2771 ZERO_STRUCTP(status
);
2772 slprintf(keystr
, sizeof(keystr
)-1, "STATUS/%s", sharename
);
2773 key
= string_tdb_data(keystr
);
2775 data
= tdb_fetch(pdb
->tdb
, key
);
2777 if (data
.dsize
== sizeof(*status
)) {
2778 /* this memcpy is ok since the status struct was
2779 not packed before storing it in the tdb */
2780 memcpy(status
, data
.dptr
, sizeof(*status
));
2782 SAFE_FREE(data
.dptr
);
2786 * Now, fetch the print queue information. We first count the number
2787 * of entries, and then only retrieve the queue if necessary.
2790 if (!get_stored_queue_info(pdb
, snum
, &count
, ppqueue
)) {
2791 release_print_db(pdb
);
2795 release_print_db(pdb
);
2799 /****************************************************************************
2801 ****************************************************************************/
2803 bool print_queue_pause(struct auth_serversupplied_info
*server_info
, int snum
,
2807 struct printif
*current_printif
= get_printer_fns( snum
);
2809 if (!print_access_check(server_info
, snum
,
2810 PRINTER_ACCESS_ADMINISTER
)) {
2811 *errcode
= WERR_ACCESS_DENIED
;
2818 ret
= (*(current_printif
->queue_pause
))(snum
);
2823 *errcode
= WERR_INVALID_PARAM
;
2827 /* force update the database */
2828 print_cache_flush(lp_const_servicename(snum
));
2830 /* Send a printer notify message */
2832 notify_printer_status(snum
, PRINTER_STATUS_PAUSED
);
2837 /****************************************************************************
2839 ****************************************************************************/
2841 bool print_queue_resume(struct auth_serversupplied_info
*server_info
, int snum
,
2845 struct printif
*current_printif
= get_printer_fns( snum
);
2847 if (!print_access_check(server_info
, snum
,
2848 PRINTER_ACCESS_ADMINISTER
)) {
2849 *errcode
= WERR_ACCESS_DENIED
;
2855 ret
= (*(current_printif
->queue_resume
))(snum
);
2860 *errcode
= WERR_INVALID_PARAM
;
2864 /* make sure the database is up to date */
2865 if (print_cache_expired(lp_const_servicename(snum
), True
))
2866 print_queue_update(snum
, True
);
2868 /* Send a printer notify message */
2870 notify_printer_status(snum
, PRINTER_STATUS_OK
);
2875 /****************************************************************************
2876 Purge a queue - implemented by deleting all jobs that we can delete.
2877 ****************************************************************************/
2879 bool print_queue_purge(struct auth_serversupplied_info
*server_info
, int snum
,
2882 print_queue_struct
*queue
;
2883 print_status_struct status
;
2887 /* Force and update so the count is accurate (i.e. not a cached count) */
2888 print_queue_update(snum
, True
);
2890 can_job_admin
= print_access_check(server_info
, snum
,
2891 JOB_ACCESS_ADMINISTER
);
2892 njobs
= print_queue_status(snum
, &queue
, &status
);
2894 if ( can_job_admin
)
2897 for (i
=0;i
<njobs
;i
++) {
2898 bool owner
= is_owner(server_info
, lp_const_servicename(snum
),
2901 if (owner
|| can_job_admin
) {
2902 print_job_delete1(snum
, queue
[i
].job
);
2906 if ( can_job_admin
)
2909 /* update the cache */
2910 print_queue_update( snum
, True
);