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/>.
24 #include "librpc/gen_ndr/messaging.h"
25 #include "../librpc/gen_ndr/ndr_spoolss.h"
26 #include "nt_printing.h"
27 #include "../librpc/gen_ndr/netlogon.h"
29 extern struct current_user current_user
;
30 extern userdom_struct current_user_info
;
32 /* Current printer interface */
33 static bool remove_from_jobs_changed(const char* sharename
, uint32 jobid
);
36 the printing backend revolves around a tdb database that stores the
37 SMB view of the print queue
39 The key for this database is a jobid - a internally generated number that
40 uniquely identifies a print job
42 reading the print queue involves two steps:
43 - possibly running lpq and updating the internal database from that
44 - reading entries from the database
46 jobids are assigned when a job starts spooling.
49 static TDB_CONTEXT
*rap_tdb
;
50 static uint16 next_rap_jobid
;
51 struct rap_jobid_key
{
56 /***************************************************************************
57 Nightmare. LANMAN jobid's are 16 bit numbers..... We must map them to 32
58 bit RPC jobids.... JRA.
59 ***************************************************************************/
61 uint16
pjobid_to_rap(const char* sharename
, uint32 jobid
)
65 struct rap_jobid_key jinfo
;
68 DEBUG(10,("pjobid_to_rap: called.\n"));
71 /* Create the in-memory tdb. */
72 rap_tdb
= tdb_open_log(NULL
, 0, TDB_INTERNAL
, (O_RDWR
|O_CREAT
), 0644);
78 fstrcpy( jinfo
.sharename
, sharename
);
80 key
.dptr
= (uint8
*)&jinfo
;
81 key
.dsize
= sizeof(jinfo
);
83 data
= tdb_fetch(rap_tdb
, key
);
84 if (data
.dptr
&& data
.dsize
== sizeof(uint16
)) {
85 rap_jobid
= SVAL(data
.dptr
, 0);
87 DEBUG(10,("pjobid_to_rap: jobid %u maps to RAP jobid %u\n",
88 (unsigned int)jobid
, (unsigned int)rap_jobid
));
92 /* Not found - create and store mapping. */
93 rap_jobid
= ++next_rap_jobid
;
95 rap_jobid
= ++next_rap_jobid
;
96 SSVAL(buf
,0,rap_jobid
);
98 data
.dsize
= sizeof(rap_jobid
);
99 tdb_store(rap_tdb
, key
, data
, TDB_REPLACE
);
100 tdb_store(rap_tdb
, data
, key
, TDB_REPLACE
);
102 DEBUG(10,("pjobid_to_rap: created jobid %u maps to RAP jobid %u\n",
103 (unsigned int)jobid
, (unsigned int)rap_jobid
));
107 bool rap_to_pjobid(uint16 rap_jobid
, fstring sharename
, uint32
*pjobid
)
112 DEBUG(10,("rap_to_pjobid called.\n"));
117 SSVAL(buf
,0,rap_jobid
);
119 key
.dsize
= sizeof(rap_jobid
);
120 data
= tdb_fetch(rap_tdb
, key
);
121 if ( data
.dptr
&& data
.dsize
== sizeof(struct rap_jobid_key
) )
123 struct rap_jobid_key
*jinfo
= (struct rap_jobid_key
*)data
.dptr
;
124 if (sharename
!= NULL
) {
125 fstrcpy( sharename
, jinfo
->sharename
);
127 *pjobid
= jinfo
->jobid
;
128 DEBUG(10,("rap_to_pjobid: jobid %u maps to RAP jobid %u\n",
129 (unsigned int)*pjobid
, (unsigned int)rap_jobid
));
130 SAFE_FREE(data
.dptr
);
134 DEBUG(10,("rap_to_pjobid: Failed to lookup RAP jobid %u\n",
135 (unsigned int)rap_jobid
));
136 SAFE_FREE(data
.dptr
);
140 void rap_jobid_delete(const char* sharename
, uint32 jobid
)
144 struct rap_jobid_key jinfo
;
147 DEBUG(10,("rap_jobid_delete: called.\n"));
152 ZERO_STRUCT( jinfo
);
153 fstrcpy( jinfo
.sharename
, sharename
);
155 key
.dptr
= (uint8
*)&jinfo
;
156 key
.dsize
= sizeof(jinfo
);
158 data
= tdb_fetch(rap_tdb
, key
);
159 if (!data
.dptr
|| (data
.dsize
!= sizeof(uint16
))) {
160 DEBUG(10,("rap_jobid_delete: cannot find jobid %u\n",
161 (unsigned int)jobid
));
162 SAFE_FREE(data
.dptr
);
166 DEBUG(10,("rap_jobid_delete: deleting jobid %u\n",
167 (unsigned int)jobid
));
169 rap_jobid
= SVAL(data
.dptr
, 0);
170 SAFE_FREE(data
.dptr
);
171 SSVAL(buf
,0,rap_jobid
);
173 data
.dsize
= sizeof(rap_jobid
);
174 tdb_delete(rap_tdb
, key
);
175 tdb_delete(rap_tdb
, data
);
178 static int get_queue_status(const char* sharename
, print_status_struct
*);
180 /****************************************************************************
181 Initialise the printing backend. Called once at startup before the fork().
182 ****************************************************************************/
184 bool print_backend_init(struct messaging_context
*msg_ctx
)
186 const char *sversion
= "INFO/version";
187 int services
= lp_numservices();
190 unlink(cache_path("printing.tdb"));
191 mkdir(cache_path("printing"),0755);
193 /* handle a Samba upgrade */
195 for (snum
= 0; snum
< services
; snum
++) {
196 struct tdb_print_db
*pdb
;
197 if (!lp_print_ok(snum
))
200 pdb
= get_print_db_byname(lp_const_servicename(snum
));
203 if (tdb_lock_bystring(pdb
->tdb
, sversion
) == -1) {
204 DEBUG(0,("print_backend_init: Failed to open printer %s database\n", lp_const_servicename(snum
) ));
205 release_print_db(pdb
);
208 if (tdb_fetch_int32(pdb
->tdb
, sversion
) != PRINT_DATABASE_VERSION
) {
209 tdb_wipe_all(pdb
->tdb
);
210 tdb_store_int32(pdb
->tdb
, sversion
, PRINT_DATABASE_VERSION
);
212 tdb_unlock_bystring(pdb
->tdb
, sversion
);
213 release_print_db(pdb
);
216 close_all_print_db(); /* Don't leave any open. */
218 /* do NT print initialization... */
219 return nt_printing_init(msg_ctx
);
222 /****************************************************************************
223 Shut down printing backend. Called once at shutdown to close the tdb.
224 ****************************************************************************/
226 void printing_end(void)
228 close_all_print_db(); /* Don't leave any open. */
231 /****************************************************************************
232 Retrieve the set of printing functions for a given service. This allows
233 us to set the printer function table based on the value of the 'printing'
236 Use the generic interface as the default and only use cups interface only
237 when asked for (and only when supported)
238 ****************************************************************************/
240 static struct printif
*get_printer_fns_from_type( enum printing_types type
)
242 struct printif
*printer_fns
= &generic_printif
;
245 if ( type
== PRINT_CUPS
) {
246 printer_fns
= &cups_printif
;
248 #endif /* HAVE_CUPS */
251 if ( type
== PRINT_IPRINT
) {
252 printer_fns
= &iprint_printif
;
254 #endif /* HAVE_IPRINT */
256 printer_fns
->type
= type
;
261 static struct printif
*get_printer_fns( int snum
)
263 return get_printer_fns_from_type( (enum printing_types
)lp_printing(snum
) );
267 /****************************************************************************
268 Useful function to generate a tdb key.
269 ****************************************************************************/
271 static TDB_DATA
print_key(uint32 jobid
, uint32
*tmp
)
275 SIVAL(tmp
, 0, jobid
);
276 ret
.dptr
= (uint8
*)tmp
;
277 ret
.dsize
= sizeof(*tmp
);
281 /****************************************************************************
282 Pack the devicemode to store it in a tdb.
283 ****************************************************************************/
284 static int pack_devicemode(struct spoolss_DeviceMode
*devmode
, uint8
*buf
, int buflen
)
286 enum ndr_err_code ndr_err
;
291 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(),
293 (ndr_push_flags_fn_t
)
294 ndr_push_spoolss_DeviceMode
);
295 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
296 DEBUG(10, ("pack_devicemode: "
297 "error encoding spoolss_DeviceMode\n"));
304 len
= tdb_pack(buf
, buflen
, "B", blob
.length
, blob
.data
);
307 DEBUG(8, ("Packed devicemode [%s]\n", devmode
->formname
));
314 /****************************************************************************
315 Unpack the devicemode to store it in a tdb.
316 ****************************************************************************/
317 static int unpack_devicemode(TALLOC_CTX
*mem_ctx
,
318 const uint8
*buf
, int buflen
,
319 struct spoolss_DeviceMode
**devmode
)
321 struct spoolss_DeviceMode
*dm
;
322 enum ndr_err_code ndr_err
;
330 len
= tdb_unpack(buf
, buflen
, "B", &data_len
, &data
);
335 dm
= talloc_zero(mem_ctx
, struct spoolss_DeviceMode
);
340 blob
= data_blob_const(data
, data_len
);
342 ndr_err
= ndr_pull_struct_blob(&blob
, dm
, dm
,
343 (ndr_pull_flags_fn_t
)ndr_pull_spoolss_DeviceMode
);
344 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
345 DEBUG(10, ("unpack_devicemode: "
346 "error parsing spoolss_DeviceMode\n"));
350 DEBUG(8, ("Unpacked devicemode [%s](%s)\n",
351 dm
->devicename
, dm
->formname
));
352 if (dm
->driverextra_data
.data
) {
353 DEBUG(8, ("with a private section of %d bytes\n",
354 dm
->__driverextra_length
));
364 /***********************************************************************
365 unpack a pjob from a tdb buffer
366 ***********************************************************************/
368 int unpack_pjob( uint8
*buf
, int buflen
, struct printjob
*pjob
)
372 uint32 pjpid
, pjsysjob
, pjfd
, pjstarttime
, pjstatus
;
373 uint32 pjsize
, pjpage_count
, pjspooled
, pjsmbjob
;
378 len
+= tdb_unpack(buf
+len
, buflen
-len
, "dddddddddffff",
396 used
= unpack_devicemode(NULL
, buf
+len
, buflen
-len
, &pjob
->devmode
);
404 pjob
->sysjob
= pjsysjob
;
406 pjob
->starttime
= pjstarttime
;
407 pjob
->status
= pjstatus
;
409 pjob
->page_count
= pjpage_count
;
410 pjob
->spooled
= pjspooled
;
411 pjob
->smbjob
= pjsmbjob
;
417 /****************************************************************************
418 Useful function to find a print job in the database.
419 ****************************************************************************/
421 static struct printjob
*print_job_find(const char *sharename
, uint32 jobid
)
423 static struct printjob pjob
;
426 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
428 DEBUG(10,("print_job_find: looking up job %u for share %s\n",
429 (unsigned int)jobid
, sharename
));
435 ret
= tdb_fetch(pdb
->tdb
, print_key(jobid
, &tmp
));
436 release_print_db(pdb
);
439 DEBUG(10,("print_job_find: failed to find jobid %u.\n", (unsigned int)jobid
));
443 talloc_free(pjob
.devmode
);
447 if ( unpack_pjob( ret
.dptr
, ret
.dsize
, &pjob
) == -1 ) {
448 DEBUG(10,("print_job_find: failed to unpack jobid %u.\n", (unsigned int)jobid
));
455 DEBUG(10,("print_job_find: returning system job %d for jobid %u.\n",
456 (int)pjob
.sysjob
, (unsigned int)jobid
));
461 /* Convert a unix jobid to a smb jobid */
463 struct unixjob_traverse_state
{
465 uint32 sysjob_to_jobid_value
;
468 static int unixjob_traverse_fn(TDB_CONTEXT
*the_tdb
, TDB_DATA key
,
469 TDB_DATA data
, void *private_data
)
471 struct printjob
*pjob
;
472 struct unixjob_traverse_state
*state
=
473 (struct unixjob_traverse_state
*)private_data
;
475 if (!data
.dptr
|| data
.dsize
== 0)
478 pjob
= (struct printjob
*)data
.dptr
;
479 if (key
.dsize
!= sizeof(uint32
))
482 if (state
->sysjob
== pjob
->sysjob
) {
483 uint32 jobid
= IVAL(key
.dptr
,0);
485 state
->sysjob_to_jobid_value
= jobid
;
492 /****************************************************************************
493 This is a *horribly expensive call as we have to iterate through all the
494 current printer tdb's. Don't do this often ! JRA.
495 ****************************************************************************/
497 uint32
sysjob_to_jobid(int unix_jobid
)
499 int services
= lp_numservices();
501 struct unixjob_traverse_state state
;
503 state
.sysjob
= unix_jobid
;
504 state
.sysjob_to_jobid_value
= (uint32
)-1;
506 for (snum
= 0; snum
< services
; snum
++) {
507 struct tdb_print_db
*pdb
;
508 if (!lp_print_ok(snum
))
510 pdb
= get_print_db_byname(lp_const_servicename(snum
));
514 tdb_traverse(pdb
->tdb
, unixjob_traverse_fn
, &state
);
515 release_print_db(pdb
);
516 if (state
.sysjob_to_jobid_value
!= (uint32
)-1)
517 return state
.sysjob_to_jobid_value
;
522 /****************************************************************************
523 Send notifications based on what has changed after a pjob_store.
524 ****************************************************************************/
526 static const struct {
528 uint32 spoolss_status
;
529 } lpq_to_spoolss_status_map
[] = {
530 { LPQ_QUEUED
, JOB_STATUS_QUEUED
},
531 { LPQ_PAUSED
, JOB_STATUS_PAUSED
},
532 { LPQ_SPOOLING
, JOB_STATUS_SPOOLING
},
533 { LPQ_PRINTING
, JOB_STATUS_PRINTING
},
534 { LPQ_DELETING
, JOB_STATUS_DELETING
},
535 { LPQ_OFFLINE
, JOB_STATUS_OFFLINE
},
536 { LPQ_PAPEROUT
, JOB_STATUS_PAPEROUT
},
537 { LPQ_PRINTED
, JOB_STATUS_PRINTED
},
538 { LPQ_DELETED
, JOB_STATUS_DELETED
},
539 { LPQ_BLOCKED
, JOB_STATUS_BLOCKED_DEVQ
},
540 { LPQ_USER_INTERVENTION
, JOB_STATUS_USER_INTERVENTION
},
544 /* Convert a lpq status value stored in printing.tdb into the
545 appropriate win32 API constant. */
547 static uint32
map_to_spoolss_status(uint32 lpq_status
)
551 while (lpq_to_spoolss_status_map
[i
].lpq_status
!= -1) {
552 if (lpq_to_spoolss_status_map
[i
].lpq_status
== lpq_status
)
553 return lpq_to_spoolss_status_map
[i
].spoolss_status
;
560 static void pjob_store_notify(const char* sharename
, uint32 jobid
, struct printjob
*old_data
,
561 struct printjob
*new_data
)
563 bool new_job
= False
;
568 /* Job attributes that can't be changed. We only send
569 notification for these on a new job. */
571 /* ACHTUNG! Due to a bug in Samba's spoolss parsing of the
572 NOTIFY_INFO_DATA buffer, we *have* to send the job submission
573 time first or else we'll end up with potential alignment
574 errors. I don't think the systemtime should be spooled as
575 a string, but this gets us around that error.
576 --jerry (i'll feel dirty for this) */
579 notify_job_submitted(sharename
, jobid
, new_data
->starttime
);
580 notify_job_username(sharename
, jobid
, new_data
->user
);
583 if (new_job
|| !strequal(old_data
->jobname
, new_data
->jobname
))
584 notify_job_name(sharename
, jobid
, new_data
->jobname
);
586 /* Job attributes of a new job or attributes that can be
589 if (new_job
|| !strequal(old_data
->jobname
, new_data
->jobname
))
590 notify_job_name(sharename
, jobid
, new_data
->jobname
);
592 if (new_job
|| old_data
->status
!= new_data
->status
)
593 notify_job_status(sharename
, jobid
, map_to_spoolss_status(new_data
->status
));
595 if (new_job
|| old_data
->size
!= new_data
->size
)
596 notify_job_total_bytes(sharename
, jobid
, new_data
->size
);
598 if (new_job
|| old_data
->page_count
!= new_data
->page_count
)
599 notify_job_total_pages(sharename
, jobid
, new_data
->page_count
);
602 /****************************************************************************
603 Store a job structure back to the database.
604 ****************************************************************************/
606 static bool pjob_store(const char* sharename
, uint32 jobid
, struct printjob
*pjob
)
609 TDB_DATA old_data
, new_data
;
611 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
613 int len
, newlen
, buflen
;
621 old_data
= tdb_fetch(pdb
->tdb
, print_key(jobid
, &tmp
));
623 /* Doh! Now we have to pack/unpack data since the NT_DEVICEMODE was added */
630 len
+= tdb_pack(buf
+len
, buflen
-len
, "dddddddddffff",
632 (uint32
)pjob
->sysjob
,
634 (uint32
)pjob
->starttime
,
635 (uint32
)pjob
->status
,
637 (uint32
)pjob
->page_count
,
638 (uint32
)pjob
->spooled
,
639 (uint32
)pjob
->smbjob
,
645 len
+= pack_devicemode(pjob
->devmode
, buf
+len
, buflen
-len
);
648 buf
= (uint8
*)SMB_REALLOC(buf
, len
);
650 DEBUG(0,("pjob_store: failed to enlarge buffer!\n"));
655 } while ( buflen
!= len
);
661 new_data
.dsize
= len
;
662 ret
= (tdb_store(pdb
->tdb
, print_key(jobid
, &tmp
), new_data
,
665 release_print_db(pdb
);
667 /* Send notify updates for what has changed */
670 struct printjob old_pjob
;
672 if ( old_data
.dsize
)
674 if ( unpack_pjob( old_data
.dptr
, old_data
.dsize
, &old_pjob
) != -1 )
676 pjob_store_notify( sharename
, jobid
, &old_pjob
, pjob
);
677 talloc_free(old_pjob
.devmode
);
682 pjob_store_notify( sharename
, jobid
, NULL
, pjob
);
687 SAFE_FREE( old_data
.dptr
);
693 /****************************************************************************
694 Remove a job structure from the database.
695 ****************************************************************************/
697 void pjob_delete(const char* sharename
, uint32 jobid
)
700 struct printjob
*pjob
;
701 uint32 job_status
= 0;
702 struct tdb_print_db
*pdb
;
704 pdb
= get_print_db_byname( sharename
);
709 pjob
= print_job_find( sharename
, jobid
);
712 DEBUG(5, ("pjob_delete: we were asked to delete nonexistent job %u\n",
713 (unsigned int)jobid
));
714 release_print_db(pdb
);
718 /* We must cycle through JOB_STATUS_DELETING and
719 JOB_STATUS_DELETED for the port monitor to delete the job
722 job_status
= JOB_STATUS_DELETING
|JOB_STATUS_DELETED
;
723 notify_job_status(sharename
, jobid
, job_status
);
725 /* Remove from printing.tdb */
727 tdb_delete(pdb
->tdb
, print_key(jobid
, &tmp
));
728 remove_from_jobs_changed(sharename
, jobid
);
729 release_print_db( pdb
);
730 rap_jobid_delete(sharename
, jobid
);
733 /****************************************************************************
734 List a unix job in the print database.
735 ****************************************************************************/
737 static void print_unix_job(const char *sharename
, print_queue_struct
*q
, uint32 jobid
)
739 struct printjob pj
, *old_pj
;
741 if (jobid
== (uint32
)-1)
742 jobid
= q
->job
+ UNIX_JOB_START
;
744 /* Preserve the timestamp on an existing unix print job */
746 old_pj
= print_job_find(sharename
, jobid
);
753 pj
.starttime
= old_pj
? old_pj
->starttime
: q
->time
;
754 pj
.status
= q
->status
;
757 fstrcpy(pj
.filename
, old_pj
? old_pj
->filename
: "");
758 if (jobid
< UNIX_JOB_START
) {
760 fstrcpy(pj
.jobname
, old_pj
? old_pj
->jobname
: "Remote Downlevel Document");
763 fstrcpy(pj
.jobname
, old_pj
? old_pj
->jobname
: q
->fs_file
);
765 fstrcpy(pj
.user
, old_pj
? old_pj
->user
: q
->fs_user
);
766 fstrcpy(pj
.queuename
, old_pj
? old_pj
->queuename
: sharename
);
768 pjob_store(sharename
, jobid
, &pj
);
772 struct traverse_struct
{
773 print_queue_struct
*queue
;
774 int qcount
, snum
, maxcount
, total_jobs
;
775 const char *sharename
;
777 const char *lprm_command
;
778 struct printif
*print_if
;
781 /****************************************************************************
782 Utility fn to delete any jobs that are no longer active.
783 ****************************************************************************/
785 static int traverse_fn_delete(TDB_CONTEXT
*t
, TDB_DATA key
, TDB_DATA data
, void *state
)
787 struct traverse_struct
*ts
= (struct traverse_struct
*)state
;
788 struct printjob pjob
;
792 if ( key
.dsize
!= sizeof(jobid
) )
795 jobid
= IVAL(key
.dptr
, 0);
796 if ( unpack_pjob( data
.dptr
, data
.dsize
, &pjob
) == -1 )
798 talloc_free(pjob
.devmode
);
802 /* remove a unix job if it isn't in the system queue any more */
804 for (i
=0;i
<ts
->qcount
;i
++) {
805 uint32 u_jobid
= (ts
->queue
[i
].job
+ UNIX_JOB_START
);
806 if (jobid
== u_jobid
)
809 if (i
== ts
->qcount
) {
810 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !smbjob\n",
811 (unsigned int)jobid
));
812 pjob_delete(ts
->sharename
, jobid
);
816 /* need to continue the the bottom of the function to
817 save the correct attributes */
820 /* maybe it hasn't been spooled yet */
822 /* if a job is not spooled and the process doesn't
823 exist then kill it. This cleans up after smbd
825 if (!process_exists_by_pid(pjob
.pid
)) {
826 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !process_exists (%u)\n",
827 (unsigned int)jobid
, (unsigned int)pjob
.pid
));
828 pjob_delete(ts
->sharename
, jobid
);
834 /* this check only makes sense for jobs submitted from Windows clients */
837 for (i
=0;i
<ts
->qcount
;i
++) {
840 if ( pjob
.status
== LPQ_DELETED
)
843 curr_jobid
= print_parse_jobid(ts
->queue
[i
].fs_file
);
845 if (jobid
== curr_jobid
) {
847 /* try to clean up any jobs that need to be deleted */
849 if ( pjob
.status
== LPQ_DELETING
) {
852 result
= (*(ts
->print_if
->job_delete
))(
853 ts
->sharename
, ts
->lprm_command
, &pjob
);
856 /* if we can't delete, then reset the job status */
857 pjob
.status
= LPQ_QUEUED
;
858 pjob_store(ts
->sharename
, jobid
, &pjob
);
861 /* if we deleted the job, the remove the tdb record */
862 pjob_delete(ts
->sharename
, jobid
);
863 pjob
.status
= LPQ_DELETED
;
873 /* The job isn't in the system queue - we have to assume it has
874 completed, so delete the database entry. */
876 if (i
== ts
->qcount
) {
878 /* A race can occur between the time a job is spooled and
879 when it appears in the lpq output. This happens when
880 the job is added to printing.tdb when another smbd
881 running print_queue_update() has completed a lpq and
882 is currently traversing the printing tdb and deleting jobs.
883 Don't delete the job if it was submitted after the lpq_time. */
885 if (pjob
.starttime
< ts
->lpq_time
) {
886 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to pjob.starttime (%u) < ts->lpq_time (%u)\n",
888 (unsigned int)pjob
.starttime
,
889 (unsigned int)ts
->lpq_time
));
890 pjob_delete(ts
->sharename
, jobid
);
896 /* Save the pjob attributes we will store.
897 FIXME!!! This is the only place where queue->job
898 represents the SMB jobid --jerry */
900 ts
->queue
[i
].job
= jobid
;
901 ts
->queue
[i
].size
= pjob
.size
;
902 ts
->queue
[i
].page_count
= pjob
.page_count
;
903 ts
->queue
[i
].status
= pjob
.status
;
904 ts
->queue
[i
].priority
= 1;
905 ts
->queue
[i
].time
= pjob
.starttime
;
906 fstrcpy(ts
->queue
[i
].fs_user
, pjob
.user
);
907 fstrcpy(ts
->queue
[i
].fs_file
, pjob
.jobname
);
914 /****************************************************************************
915 Check if the print queue has been updated recently enough.
916 ****************************************************************************/
918 static void print_cache_flush(const char *sharename
)
921 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
925 slprintf(key
, sizeof(key
)-1, "CACHE/%s", sharename
);
926 tdb_store_int32(pdb
->tdb
, key
, -1);
927 release_print_db(pdb
);
930 /****************************************************************************
931 Check if someone already thinks they are doing the update.
932 ****************************************************************************/
934 static pid_t
get_updating_pid(const char *sharename
)
939 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
943 slprintf(keystr
, sizeof(keystr
)-1, "UPDATING/%s", sharename
);
944 key
= string_tdb_data(keystr
);
946 data
= tdb_fetch(pdb
->tdb
, key
);
947 release_print_db(pdb
);
948 if (!data
.dptr
|| data
.dsize
!= sizeof(pid_t
)) {
949 SAFE_FREE(data
.dptr
);
953 updating_pid
= IVAL(data
.dptr
, 0);
954 SAFE_FREE(data
.dptr
);
956 if (process_exists_by_pid(updating_pid
))
962 /****************************************************************************
963 Set the fact that we're doing the update, or have finished doing the update
965 ****************************************************************************/
967 static void set_updating_pid(const fstring sharename
, bool updating
)
972 pid_t updating_pid
= sys_getpid();
975 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
980 slprintf(keystr
, sizeof(keystr
)-1, "UPDATING/%s", sharename
);
981 key
= string_tdb_data(keystr
);
983 DEBUG(5, ("set_updating_pid: %s updating lpq cache for print share %s\n",
984 updating
? "" : "not ",
988 tdb_delete(pdb
->tdb
, key
);
989 release_print_db(pdb
);
993 SIVAL( buffer
, 0, updating_pid
);
995 data
.dsize
= 4; /* we always assume this is a 4 byte value */
997 tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
);
998 release_print_db(pdb
);
1001 /****************************************************************************
1002 Sort print jobs by submittal time.
1003 ****************************************************************************/
1005 static int printjob_comp(print_queue_struct
*j1
, print_queue_struct
*j2
)
1016 /* Sort on job start time */
1018 if (j1
->time
== j2
->time
)
1020 return (j1
->time
> j2
->time
) ? 1 : -1;
1023 /****************************************************************************
1024 Store the sorted queue representation for later portmon retrieval.
1026 ****************************************************************************/
1028 static void store_queue_struct(struct tdb_print_db
*pdb
, struct traverse_struct
*pts
)
1031 int max_reported_jobs
= lp_max_reported_jobs(pts
->snum
);
1032 print_queue_struct
*queue
= pts
->queue
;
1035 unsigned int qcount
;
1037 if (max_reported_jobs
&& (max_reported_jobs
< pts
->qcount
))
1038 pts
->qcount
= max_reported_jobs
;
1041 /* Work out the size. */
1043 data
.dsize
+= tdb_pack(NULL
, 0, "d", qcount
);
1045 for (i
= 0; i
< pts
->qcount
; i
++) {
1046 if ( queue
[i
].status
== LPQ_DELETED
)
1050 data
.dsize
+= tdb_pack(NULL
, 0, "ddddddff",
1051 (uint32
)queue
[i
].job
,
1052 (uint32
)queue
[i
].size
,
1053 (uint32
)queue
[i
].page_count
,
1054 (uint32
)queue
[i
].status
,
1055 (uint32
)queue
[i
].priority
,
1056 (uint32
)queue
[i
].time
,
1061 if ((data
.dptr
= (uint8
*)SMB_MALLOC(data
.dsize
)) == NULL
)
1065 len
+= tdb_pack(data
.dptr
+ len
, data
.dsize
- len
, "d", qcount
);
1066 for (i
= 0; i
< pts
->qcount
; i
++) {
1067 if ( queue
[i
].status
== LPQ_DELETED
)
1070 len
+= tdb_pack(data
.dptr
+ len
, data
.dsize
- len
, "ddddddff",
1071 (uint32
)queue
[i
].job
,
1072 (uint32
)queue
[i
].size
,
1073 (uint32
)queue
[i
].page_count
,
1074 (uint32
)queue
[i
].status
,
1075 (uint32
)queue
[i
].priority
,
1076 (uint32
)queue
[i
].time
,
1081 tdb_store(pdb
->tdb
, string_tdb_data("INFO/linear_queue_array"), data
,
1083 SAFE_FREE(data
.dptr
);
1087 static TDB_DATA
get_jobs_changed_data(struct tdb_print_db
*pdb
)
1093 data
= tdb_fetch(pdb
->tdb
, string_tdb_data("INFO/jobs_changed"));
1094 if (data
.dptr
== NULL
|| data
.dsize
== 0 || (data
.dsize
% 4 != 0)) {
1095 SAFE_FREE(data
.dptr
);
1102 static void check_job_changed(const char *sharename
, TDB_DATA data
, uint32 jobid
)
1105 unsigned int job_count
= data
.dsize
/ 4;
1107 for (i
= 0; i
< job_count
; i
++) {
1110 ch_jobid
= IVAL(data
.dptr
, i
*4);
1111 if (ch_jobid
== jobid
)
1112 remove_from_jobs_changed(sharename
, jobid
);
1116 /****************************************************************************
1117 Check if the print queue has been updated recently enough.
1118 ****************************************************************************/
1120 static bool print_cache_expired(const char *sharename
, bool check_pending
)
1123 time_t last_qscan_time
, time_now
= time(NULL
);
1124 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1125 bool result
= False
;
1130 snprintf(key
, sizeof(key
), "CACHE/%s", sharename
);
1131 last_qscan_time
= (time_t)tdb_fetch_int32(pdb
->tdb
, key
);
1134 * Invalidate the queue for 3 reasons.
1135 * (1). last queue scan time == -1.
1136 * (2). Current time - last queue scan time > allowed cache time.
1137 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1138 * This last test picks up machines for which the clock has been moved
1139 * forward, an lpq scan done and then the clock moved back. Otherwise
1140 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1143 if (last_qscan_time
== ((time_t)-1)
1144 || (time_now
- last_qscan_time
) >= lp_lpqcachetime()
1145 || last_qscan_time
> (time_now
+ MAX_CACHE_VALID_TIME
))
1148 time_t msg_pending_time
;
1150 DEBUG(4, ("print_cache_expired: cache expired for queue %s "
1151 "(last_qscan_time = %d, time now = %d, qcachetime = %d)\n",
1152 sharename
, (int)last_qscan_time
, (int)time_now
,
1153 (int)lp_lpqcachetime() ));
1155 /* check if another smbd has already sent a message to update the
1156 queue. Give the pending message one minute to clear and
1157 then send another message anyways. Make sure to check for
1158 clocks that have been run forward and then back again. */
1160 snprintf(key
, sizeof(key
), "MSG_PENDING/%s", sharename
);
1163 && tdb_fetch_uint32( pdb
->tdb
, key
, &u
)
1164 && (msg_pending_time
=u
) > 0
1165 && msg_pending_time
<= time_now
1166 && (time_now
- msg_pending_time
) < 60 )
1168 DEBUG(4,("print_cache_expired: message already pending for %s. Accepting cache\n",
1177 release_print_db(pdb
);
1181 /****************************************************************************
1182 main work for updating the lpq cahe for a printer queue
1183 ****************************************************************************/
1185 static void print_queue_update_internal( const char *sharename
,
1186 struct printif
*current_printif
,
1187 char *lpq_command
, char *lprm_command
)
1190 print_queue_struct
*queue
= NULL
;
1191 print_status_struct status
;
1192 print_status_struct old_status
;
1193 struct printjob
*pjob
;
1194 struct traverse_struct tstruct
;
1197 fstring keystr
, cachestr
;
1198 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1204 DEBUG(5,("print_queue_update_internal: printer = %s, type = %d, lpq command = [%s]\n",
1205 sharename
, current_printif
->type
, lpq_command
));
1208 * Update the cache time FIRST ! Stops others even
1209 * attempting to get the lock and doing this
1210 * if the lpq takes a long time.
1213 slprintf(cachestr
, sizeof(cachestr
)-1, "CACHE/%s", sharename
);
1214 tdb_store_int32(pdb
->tdb
, cachestr
, (int)time(NULL
));
1216 /* get the current queue using the appropriate interface */
1217 ZERO_STRUCT(status
);
1219 qcount
= (*(current_printif
->queue_get
))(sharename
,
1220 current_printif
->type
,
1221 lpq_command
, &queue
, &status
);
1223 DEBUG(3, ("print_queue_update_internal: %d job%s in queue for %s\n",
1224 qcount
, (qcount
!= 1) ? "s" : "", sharename
));
1226 /* Sort the queue by submission time otherwise they are displayed
1229 TYPESAFE_QSORT(queue
, qcount
, printjob_comp
);
1232 any job in the internal database that is marked as spooled
1233 and doesn't exist in the system queue is considered finished
1234 and removed from the database
1236 any job in the system database but not in the internal database
1237 is added as a unix job
1239 fill in any system job numbers as we go
1242 jcdata
= get_jobs_changed_data(pdb
);
1244 for (i
=0; i
<qcount
; i
++) {
1245 uint32 jobid
= print_parse_jobid(queue
[i
].fs_file
);
1247 if (jobid
== (uint32
)-1) {
1248 /* assume its a unix print job */
1249 print_unix_job(sharename
, &queue
[i
], jobid
);
1253 /* we have an active SMB print job - update its status */
1254 pjob
= print_job_find(sharename
, jobid
);
1256 /* err, somethings wrong. Probably smbd was restarted
1257 with jobs in the queue. All we can do is treat them
1258 like unix jobs. Pity. */
1259 print_unix_job(sharename
, &queue
[i
], jobid
);
1263 pjob
->sysjob
= queue
[i
].job
;
1265 /* don't reset the status on jobs to be deleted */
1267 if ( pjob
->status
!= LPQ_DELETING
)
1268 pjob
->status
= queue
[i
].status
;
1270 pjob_store(sharename
, jobid
, pjob
);
1272 check_job_changed(sharename
, jcdata
, jobid
);
1275 SAFE_FREE(jcdata
.dptr
);
1277 /* now delete any queued entries that don't appear in the
1279 tstruct
.queue
= queue
;
1280 tstruct
.qcount
= qcount
;
1282 tstruct
.total_jobs
= 0;
1283 tstruct
.lpq_time
= time(NULL
);
1284 tstruct
.sharename
= sharename
;
1285 tstruct
.lprm_command
= lprm_command
;
1286 tstruct
.print_if
= current_printif
;
1288 tdb_traverse(pdb
->tdb
, traverse_fn_delete
, (void *)&tstruct
);
1290 /* Store the linearised queue, max jobs only. */
1291 store_queue_struct(pdb
, &tstruct
);
1293 SAFE_FREE(tstruct
.queue
);
1295 DEBUG(10,("print_queue_update_internal: printer %s INFO/total_jobs = %d\n",
1296 sharename
, tstruct
.total_jobs
));
1298 tdb_store_int32(pdb
->tdb
, "INFO/total_jobs", tstruct
.total_jobs
);
1300 get_queue_status(sharename
, &old_status
);
1301 if (old_status
.qcount
!= qcount
)
1302 DEBUG(10,("print_queue_update_internal: queue status change %d jobs -> %d jobs for printer %s\n",
1303 old_status
.qcount
, qcount
, sharename
));
1305 /* store the new queue status structure */
1306 slprintf(keystr
, sizeof(keystr
)-1, "STATUS/%s", sharename
);
1307 key
= string_tdb_data(keystr
);
1309 status
.qcount
= qcount
;
1310 data
.dptr
= (uint8
*)&status
;
1311 data
.dsize
= sizeof(status
);
1312 tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
);
1315 * Update the cache time again. We want to do this call
1316 * as little as possible...
1319 slprintf(keystr
, sizeof(keystr
)-1, "CACHE/%s", sharename
);
1320 tdb_store_int32(pdb
->tdb
, keystr
, (int32
)time(NULL
));
1322 /* clear the msg pending record for this queue */
1324 snprintf(keystr
, sizeof(keystr
), "MSG_PENDING/%s", sharename
);
1326 if ( !tdb_store_uint32( pdb
->tdb
, keystr
, 0 ) ) {
1327 /* log a message but continue on */
1329 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1333 release_print_db( pdb
);
1338 /****************************************************************************
1339 Update the internal database from the system print queue for a queue.
1340 obtain a lock on the print queue before proceeding (needed when mutiple
1341 smbd processes maytry to update the lpq cache concurrently).
1342 ****************************************************************************/
1344 static void print_queue_update_with_lock( const char *sharename
,
1345 struct printif
*current_printif
,
1346 char *lpq_command
, char *lprm_command
)
1349 struct tdb_print_db
*pdb
;
1351 DEBUG(5,("print_queue_update_with_lock: printer share = %s\n", sharename
));
1352 pdb
= get_print_db_byname(sharename
);
1356 if ( !print_cache_expired(sharename
, False
) ) {
1357 DEBUG(5,("print_queue_update_with_lock: print cache for %s is still ok\n", sharename
));
1358 release_print_db(pdb
);
1363 * Check to see if someone else is doing this update.
1364 * This is essentially a mutex on the update.
1367 if (get_updating_pid(sharename
) != -1) {
1368 release_print_db(pdb
);
1372 /* Lock the queue for the database update */
1374 slprintf(keystr
, sizeof(keystr
) - 1, "LOCK/%s", sharename
);
1375 /* Only wait 10 seconds for this. */
1376 if (tdb_lock_bystring_with_timeout(pdb
->tdb
, keystr
, 10) == -1) {
1377 DEBUG(0,("print_queue_update_with_lock: Failed to lock printer %s database\n", sharename
));
1378 release_print_db(pdb
);
1383 * Ensure that no one else got in here.
1384 * If the updating pid is still -1 then we are
1388 if (get_updating_pid(sharename
) != -1) {
1390 * Someone else is doing the update, exit.
1392 tdb_unlock_bystring(pdb
->tdb
, keystr
);
1393 release_print_db(pdb
);
1398 * We're going to do the update ourselves.
1401 /* Tell others we're doing the update. */
1402 set_updating_pid(sharename
, True
);
1405 * Allow others to enter and notice we're doing
1409 tdb_unlock_bystring(pdb
->tdb
, keystr
);
1411 /* do the main work now */
1413 print_queue_update_internal( sharename
, current_printif
,
1414 lpq_command
, lprm_command
);
1416 /* Delete our pid from the db. */
1417 set_updating_pid(sharename
, False
);
1418 release_print_db(pdb
);
1421 /****************************************************************************
1422 this is the receive function of the background lpq updater
1423 ****************************************************************************/
1424 static void print_queue_receive(struct messaging_context
*msg
,
1427 struct server_id server_id
,
1431 char *lpqcommand
= NULL
, *lprmcommand
= NULL
;
1435 len
= tdb_unpack( (uint8
*)data
->data
, data
->length
, "fdPP",
1442 SAFE_FREE(lpqcommand
);
1443 SAFE_FREE(lprmcommand
);
1444 DEBUG(0,("print_queue_receive: Got invalid print queue update message\n"));
1448 print_queue_update_with_lock(sharename
,
1449 get_printer_fns_from_type((enum printing_types
)printing_type
),
1450 lpqcommand
, lprmcommand
);
1452 SAFE_FREE(lpqcommand
);
1453 SAFE_FREE(lprmcommand
);
1457 static void printing_pause_fd_handler(struct tevent_context
*ev
,
1458 struct tevent_fd
*fde
,
1463 * If pause_pipe[1] is closed it means the parent smbd
1464 * and children exited or aborted.
1466 exit_server_cleanly(NULL
);
1469 extern struct child_pid
*children
;
1470 extern int num_children
;
1472 static void add_child_pid(pid_t pid
)
1474 struct child_pid
*child
;
1476 child
= SMB_MALLOC_P(struct child_pid
);
1477 if (child
== NULL
) {
1478 DEBUG(0, ("Could not add child struct -- malloc failed\n"));
1482 DLIST_ADD(children
, child
);
1486 static pid_t background_lpq_updater_pid
= -1;
1488 /****************************************************************************
1489 main thread of the background lpq updater
1490 ****************************************************************************/
1491 void start_background_queue(void)
1493 /* Use local variables for this as we don't
1494 * need to save the parent side of this, just
1495 * ensure it closes when the process exits.
1499 DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
1501 if (pipe(pause_pipe
) == -1) {
1502 DEBUG(5,("start_background_queue: cannot create pipe. %s\n", strerror(errno
) ));
1506 background_lpq_updater_pid
= sys_fork();
1508 if (background_lpq_updater_pid
== -1) {
1509 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno
) ));
1513 /* Track the printing pid along with other smbd children */
1514 add_child_pid(background_lpq_updater_pid
);
1516 if(background_lpq_updater_pid
== 0) {
1517 struct tevent_fd
*fde
;
1522 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
1524 close(pause_pipe
[0]);
1527 status
= reinit_after_fork(server_messaging_context(),
1528 server_event_context(),
1529 procid_self(), true);
1531 if (!NT_STATUS_IS_OK(status
)) {
1532 DEBUG(0,("reinit_after_fork() failed\n"));
1533 smb_panic("reinit_after_fork() failed");
1536 smbd_setup_sig_term_handler();
1537 smbd_setup_sig_hup_handler();
1539 if (!serverid_register(procid_self(),
1540 FLAG_MSG_GENERAL
|FLAG_MSG_SMBD
1541 |FLAG_MSG_PRINT_GENERAL
)) {
1545 if (!locking_init()) {
1549 messaging_register(server_messaging_context(), NULL
,
1550 MSG_PRINTER_UPDATE
, print_queue_receive
);
1552 fde
= tevent_add_fd(server_event_context(),
1553 server_event_context(),
1554 pause_pipe
[1], TEVENT_FD_READ
,
1555 printing_pause_fd_handler
,
1558 DEBUG(0,("tevent_add_fd() failed for pause_pipe\n"));
1559 smb_panic("tevent_add_fd() failed for pause_pipe");
1562 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
1563 ret
= tevent_loop_wait(server_event_context());
1564 /* should not be reached */
1565 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
1566 ret
, (ret
== 0) ? "out of events" : strerror(errno
)));
1570 close(pause_pipe
[1]);
1573 /****************************************************************************
1574 update the internal database from the system print queue for a queue
1575 ****************************************************************************/
1577 static void print_queue_update(int snum
, bool force
)
1581 char *lpqcommand
= NULL
;
1582 char *lprmcommand
= NULL
;
1583 uint8
*buffer
= NULL
;
1586 struct tdb_print_db
*pdb
;
1588 struct printif
*current_printif
;
1589 TALLOC_CTX
*ctx
= talloc_tos();
1591 fstrcpy( sharename
, lp_const_servicename(snum
));
1593 /* don't strip out characters like '$' from the printername */
1595 lpqcommand
= talloc_string_sub2(ctx
,
1596 lp_lpqcommand(snum
),
1598 lp_printername(snum
),
1599 false, false, false);
1603 lpqcommand
= talloc_sub_advanced(ctx
,
1604 lp_servicename(snum
),
1605 current_user_info
.unix_name
,
1607 current_user
.ut
.gid
,
1608 get_current_username(),
1609 current_user_info
.domain
,
1615 lprmcommand
= talloc_string_sub2(ctx
,
1616 lp_lprmcommand(snum
),
1618 lp_printername(snum
),
1619 false, false, false);
1623 lprmcommand
= talloc_sub_advanced(ctx
,
1624 lp_servicename(snum
),
1625 current_user_info
.unix_name
,
1627 current_user
.ut
.gid
,
1628 get_current_username(),
1629 current_user_info
.domain
,
1636 * Make sure that the background queue process exists.
1637 * Otherwise just do the update ourselves
1640 if ( force
|| background_lpq_updater_pid
== -1 ) {
1641 DEBUG(4,("print_queue_update: updating queue [%s] myself\n", sharename
));
1642 current_printif
= get_printer_fns( snum
);
1643 print_queue_update_with_lock( sharename
, current_printif
, lpqcommand
, lprmcommand
);
1648 type
= lp_printing(snum
);
1650 /* get the length */
1652 len
= tdb_pack( NULL
, 0, "fdPP",
1658 buffer
= SMB_XMALLOC_ARRAY( uint8
, len
);
1660 /* now pack the buffer */
1661 newlen
= tdb_pack( buffer
, len
, "fdPP",
1667 SMB_ASSERT( newlen
== len
);
1669 DEBUG(10,("print_queue_update: Sending message -> printer = %s, "
1670 "type = %d, lpq command = [%s] lprm command = [%s]\n",
1671 sharename
, type
, lpqcommand
, lprmcommand
));
1673 /* here we set a msg pending record for other smbd processes
1674 to throttle the number of duplicate print_queue_update msgs
1677 pdb
= get_print_db_byname(sharename
);
1683 snprintf(key
, sizeof(key
), "MSG_PENDING/%s", sharename
);
1685 if ( !tdb_store_uint32( pdb
->tdb
, key
, time(NULL
) ) ) {
1686 /* log a message but continue on */
1688 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1692 release_print_db( pdb
);
1694 /* finally send the message */
1696 messaging_send_buf(server_messaging_context(),
1697 pid_to_procid(background_lpq_updater_pid
),
1698 MSG_PRINTER_UPDATE
, (uint8
*)buffer
, len
);
1700 SAFE_FREE( buffer
);
1705 /****************************************************************************
1706 Create/Update an entry in the print tdb that will allow us to send notify
1707 updates only to interested smbd's.
1708 ****************************************************************************/
1710 bool print_notify_register_pid(int snum
)
1713 struct tdb_print_db
*pdb
= NULL
;
1714 TDB_CONTEXT
*tdb
= NULL
;
1715 const char *printername
;
1716 uint32 mypid
= (uint32
)sys_getpid();
1720 /* if (snum == -1), then the change notify request was
1721 on a print server handle and we need to register on
1726 int num_services
= lp_numservices();
1729 for ( idx
=0; idx
<num_services
; idx
++ ) {
1730 if (lp_snum_ok(idx
) && lp_print_ok(idx
) )
1731 print_notify_register_pid(idx
);
1736 else /* register for a specific printer */
1738 printername
= lp_const_servicename(snum
);
1739 pdb
= get_print_db_byname(printername
);
1745 if (tdb_lock_bystring_with_timeout(tdb
, NOTIFY_PID_LIST_KEY
, 10) == -1) {
1746 DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n",
1749 release_print_db(pdb
);
1753 data
= get_printer_notify_pid_list( tdb
, printername
, True
);
1755 /* Add ourselves and increase the refcount. */
1757 for (i
= 0; i
< data
.dsize
; i
+= 8) {
1758 if (IVAL(data
.dptr
,i
) == mypid
) {
1759 uint32 new_refcount
= IVAL(data
.dptr
, i
+4) + 1;
1760 SIVAL(data
.dptr
, i
+4, new_refcount
);
1765 if (i
== data
.dsize
) {
1766 /* We weren't in the list. Realloc. */
1767 data
.dptr
= (uint8
*)SMB_REALLOC(data
.dptr
, data
.dsize
+ 8);
1769 DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n",
1774 SIVAL(data
.dptr
,data
.dsize
- 8,mypid
);
1775 SIVAL(data
.dptr
,data
.dsize
- 4,1); /* Refcount. */
1778 /* Store back the record. */
1779 if (tdb_store_bystring(tdb
, NOTIFY_PID_LIST_KEY
, data
, TDB_REPLACE
) == -1) {
1780 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1781 list for printer %s\n", printername
));
1789 tdb_unlock_bystring(tdb
, NOTIFY_PID_LIST_KEY
);
1791 release_print_db(pdb
);
1792 SAFE_FREE(data
.dptr
);
1796 /****************************************************************************
1797 Update an entry in the print tdb that will allow us to send notify
1798 updates only to interested smbd's.
1799 ****************************************************************************/
1801 bool print_notify_deregister_pid(int snum
)
1804 struct tdb_print_db
*pdb
= NULL
;
1805 TDB_CONTEXT
*tdb
= NULL
;
1806 const char *printername
;
1807 uint32 mypid
= (uint32
)sys_getpid();
1811 /* if ( snum == -1 ), we are deregister a print server handle
1812 which means to deregister on all print queues */
1816 int num_services
= lp_numservices();
1819 for ( idx
=0; idx
<num_services
; idx
++ ) {
1820 if ( lp_snum_ok(idx
) && lp_print_ok(idx
) )
1821 print_notify_deregister_pid(idx
);
1826 else /* deregister a specific printer */
1828 printername
= lp_const_servicename(snum
);
1829 pdb
= get_print_db_byname(printername
);
1835 if (tdb_lock_bystring_with_timeout(tdb
, NOTIFY_PID_LIST_KEY
, 10) == -1) {
1836 DEBUG(0,("print_notify_register_pid: Failed to lock \
1837 printer %s database\n", printername
));
1839 release_print_db(pdb
);
1843 data
= get_printer_notify_pid_list( tdb
, printername
, True
);
1845 /* Reduce refcount. Remove ourselves if zero. */
1847 for (i
= 0; i
< data
.dsize
; ) {
1848 if (IVAL(data
.dptr
,i
) == mypid
) {
1849 uint32 refcount
= IVAL(data
.dptr
, i
+4);
1853 if (refcount
== 0) {
1854 if (data
.dsize
- i
> 8)
1855 memmove( &data
.dptr
[i
], &data
.dptr
[i
+8], data
.dsize
- i
- 8);
1859 SIVAL(data
.dptr
, i
+4, refcount
);
1865 if (data
.dsize
== 0)
1866 SAFE_FREE(data
.dptr
);
1868 /* Store back the record. */
1869 if (tdb_store_bystring(tdb
, NOTIFY_PID_LIST_KEY
, data
, TDB_REPLACE
) == -1) {
1870 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1871 list for printer %s\n", printername
));
1879 tdb_unlock_bystring(tdb
, NOTIFY_PID_LIST_KEY
);
1881 release_print_db(pdb
);
1882 SAFE_FREE(data
.dptr
);
1886 /****************************************************************************
1887 Check if a jobid is valid. It is valid if it exists in the database.
1888 ****************************************************************************/
1890 bool print_job_exists(const char* sharename
, uint32 jobid
)
1892 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1898 ret
= tdb_exists(pdb
->tdb
, print_key(jobid
, &tmp
));
1899 release_print_db(pdb
);
1903 /****************************************************************************
1904 Give the filename used for a jobid.
1905 Only valid for the process doing the spooling and when the job
1906 has not been spooled.
1907 ****************************************************************************/
1909 char *print_job_fname(const char* sharename
, uint32 jobid
)
1911 struct printjob
*pjob
= print_job_find(sharename
, jobid
);
1912 if (!pjob
|| pjob
->spooled
|| pjob
->pid
!= sys_getpid())
1914 return pjob
->filename
;
1918 /****************************************************************************
1919 Give the filename used for a jobid.
1920 Only valid for the process doing the spooling and when the job
1921 has not been spooled.
1922 ****************************************************************************/
1924 struct spoolss_DeviceMode
*print_job_devmode(const char* sharename
, uint32 jobid
)
1926 struct printjob
*pjob
= print_job_find(sharename
, jobid
);
1931 return pjob
->devmode
;
1934 /****************************************************************************
1935 Set the name of a job. Only possible for owner.
1936 ****************************************************************************/
1938 bool print_job_set_name(const char *sharename
, uint32 jobid
, const char *name
)
1940 struct printjob
*pjob
;
1942 pjob
= print_job_find(sharename
, jobid
);
1943 if (!pjob
|| pjob
->pid
!= sys_getpid())
1946 fstrcpy(pjob
->jobname
, name
);
1947 return pjob_store(sharename
, jobid
, pjob
);
1950 /****************************************************************************
1951 Get the name of a job. Only possible for owner.
1952 ****************************************************************************/
1954 bool print_job_get_name(TALLOC_CTX
*mem_ctx
, const char *sharename
, uint32_t jobid
, char **name
)
1956 struct printjob
*pjob
;
1958 pjob
= print_job_find(sharename
, jobid
);
1959 if (!pjob
|| pjob
->pid
!= sys_getpid()) {
1963 *name
= talloc_strdup(mem_ctx
, pjob
->jobname
);
1972 /***************************************************************************
1973 Remove a jobid from the 'jobs changed' list.
1974 ***************************************************************************/
1976 static bool remove_from_jobs_changed(const char* sharename
, uint32 jobid
)
1978 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1980 size_t job_count
, i
;
1982 bool gotlock
= False
;
1990 key
= string_tdb_data("INFO/jobs_changed");
1992 if (tdb_chainlock_with_timeout(pdb
->tdb
, key
, 5) == -1)
1997 data
= tdb_fetch(pdb
->tdb
, key
);
1999 if (data
.dptr
== NULL
|| data
.dsize
== 0 || (data
.dsize
% 4 != 0))
2002 job_count
= data
.dsize
/ 4;
2003 for (i
= 0; i
< job_count
; i
++) {
2006 ch_jobid
= IVAL(data
.dptr
, i
*4);
2007 if (ch_jobid
== jobid
) {
2008 if (i
< job_count
-1 )
2009 memmove(data
.dptr
+ (i
*4), data
.dptr
+ (i
*4) + 4, (job_count
- i
- 1)*4 );
2011 if (tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
) == -1)
2021 tdb_chainunlock(pdb
->tdb
, key
);
2022 SAFE_FREE(data
.dptr
);
2023 release_print_db(pdb
);
2025 DEBUG(10,("remove_from_jobs_changed: removed jobid %u\n", (unsigned int)jobid
));
2027 DEBUG(10,("remove_from_jobs_changed: Failed to remove jobid %u\n", (unsigned int)jobid
));
2031 /****************************************************************************
2032 Delete a print job - don't update queue.
2033 ****************************************************************************/
2035 static bool print_job_delete1(int snum
, uint32 jobid
)
2037 const char* sharename
= lp_const_servicename(snum
);
2038 struct printjob
*pjob
= print_job_find(sharename
, jobid
);
2040 struct printif
*current_printif
= get_printer_fns( snum
);
2046 * If already deleting just return.
2049 if (pjob
->status
== LPQ_DELETING
)
2052 /* Hrm - we need to be able to cope with deleting a job before it
2053 has reached the spooler. Just mark it as LPQ_DELETING and
2054 let the print_queue_update() code rmeove the record */
2057 if (pjob
->sysjob
== -1) {
2058 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid
));
2061 /* Set the tdb entry to be deleting. */
2063 pjob
->status
= LPQ_DELETING
;
2064 pjob_store(sharename
, jobid
, pjob
);
2066 if (pjob
->spooled
&& pjob
->sysjob
!= -1)
2068 result
= (*(current_printif
->job_delete
))(
2069 lp_printername(snum
),
2070 lp_lprmcommand(snum
),
2073 /* Delete the tdb entry if the delete succeeded or the job hasn't
2077 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
2082 pjob_delete(sharename
, jobid
);
2083 /* Ensure we keep a rough count of the number of total jobs... */
2084 tdb_change_int32_atomic(pdb
->tdb
, "INFO/total_jobs", &njobs
, -1);
2085 release_print_db(pdb
);
2089 remove_from_jobs_changed( sharename
, jobid
);
2091 return (result
== 0);
2094 /****************************************************************************
2095 Return true if the current user owns the print job.
2096 ****************************************************************************/
2098 static bool is_owner(struct auth_serversupplied_info
*server_info
,
2099 const char *servicename
,
2102 struct printjob
*pjob
= print_job_find(servicename
, jobid
);
2104 if (!pjob
|| !server_info
)
2107 return strequal(pjob
->user
, server_info
->sanitized_username
);
2110 /****************************************************************************
2112 ****************************************************************************/
2114 WERROR
print_job_delete(struct auth_serversupplied_info
*server_info
,
2115 int snum
, uint32_t jobid
)
2117 const char* sharename
= lp_const_servicename(snum
);
2118 struct printjob
*pjob
;
2122 owner
= is_owner(server_info
, lp_const_servicename(snum
), jobid
);
2124 /* Check access against security descriptor or whether the user
2128 !print_access_check(server_info
, smbd_messaging_context(), snum
,
2129 JOB_ACCESS_ADMINISTER
)) {
2130 DEBUG(3, ("delete denied by security descriptor\n"));
2132 /* BEGIN_ADMIN_LOG */
2133 sys_adminlog( LOG_ERR
,
2134 "Permission denied-- user not allowed to delete, \
2135 pause, or resume print job. User name: %s. Printer name: %s.",
2136 uidtoname(server_info
->utok
.uid
),
2137 lp_printername(snum
) );
2140 return WERR_ACCESS_DENIED
;
2144 * get the spooled filename of the print job
2145 * if this works, then the file has not been spooled
2146 * to the underlying print system. Just delete the
2147 * spool file & return.
2150 fname
= print_job_fname(sharename
, jobid
);
2151 if (fname
!= NULL
) {
2152 /* remove the spool file */
2153 DEBUG(10, ("print_job_delete: "
2154 "Removing spool file [%s]\n", fname
));
2155 if (unlink(fname
) == -1) {
2156 return map_werror_from_unix(errno
);
2160 if (!print_job_delete1(snum
, jobid
)) {
2161 return WERR_ACCESS_DENIED
;
2164 /* force update the database and say the delete failed if the
2167 print_queue_update(snum
, True
);
2169 pjob
= print_job_find(sharename
, jobid
);
2170 if (pjob
&& (pjob
->status
!= LPQ_DELETING
)) {
2171 return WERR_ACCESS_DENIED
;
2174 return WERR_PRINTER_HAS_JOBS_QUEUED
;
2177 /****************************************************************************
2179 ****************************************************************************/
2181 bool print_job_pause(struct auth_serversupplied_info
*server_info
, int snum
,
2182 uint32 jobid
, WERROR
*errcode
)
2184 const char* sharename
= lp_const_servicename(snum
);
2185 struct printjob
*pjob
;
2187 struct printif
*current_printif
= get_printer_fns( snum
);
2189 pjob
= print_job_find(sharename
, jobid
);
2191 if (!pjob
|| !server_info
) {
2192 DEBUG(10, ("print_job_pause: no pjob or user for jobid %u\n",
2193 (unsigned int)jobid
));
2197 if (!pjob
->spooled
|| pjob
->sysjob
== -1) {
2198 DEBUG(10, ("print_job_pause: not spooled or bad sysjob = %d for jobid %u\n",
2199 (int)pjob
->sysjob
, (unsigned int)jobid
));
2203 if (!is_owner(server_info
, lp_const_servicename(snum
), jobid
) &&
2204 !print_access_check(server_info
, smbd_messaging_context(), snum
,
2205 JOB_ACCESS_ADMINISTER
)) {
2206 DEBUG(3, ("pause denied by security descriptor\n"));
2208 /* BEGIN_ADMIN_LOG */
2209 sys_adminlog( LOG_ERR
,
2210 "Permission denied-- user not allowed to delete, \
2211 pause, or resume print job. User name: %s. Printer name: %s.",
2212 uidtoname(server_info
->utok
.uid
),
2213 lp_printername(snum
) );
2216 *errcode
= WERR_ACCESS_DENIED
;
2220 /* need to pause the spooled entry */
2221 ret
= (*(current_printif
->job_pause
))(snum
, pjob
);
2224 *errcode
= WERR_INVALID_PARAM
;
2228 /* force update the database */
2229 print_cache_flush(lp_const_servicename(snum
));
2231 /* Send a printer notify message */
2233 notify_job_status(sharename
, jobid
, JOB_STATUS_PAUSED
);
2235 /* how do we tell if this succeeded? */
2240 /****************************************************************************
2242 ****************************************************************************/
2244 bool print_job_resume(struct auth_serversupplied_info
*server_info
, int snum
,
2245 uint32 jobid
, WERROR
*errcode
)
2247 const char *sharename
= lp_const_servicename(snum
);
2248 struct printjob
*pjob
;
2250 struct printif
*current_printif
= get_printer_fns( snum
);
2252 pjob
= print_job_find(sharename
, jobid
);
2254 if (!pjob
|| !server_info
) {
2255 DEBUG(10, ("print_job_resume: no pjob or user for jobid %u\n",
2256 (unsigned int)jobid
));
2260 if (!pjob
->spooled
|| pjob
->sysjob
== -1) {
2261 DEBUG(10, ("print_job_resume: not spooled or bad sysjob = %d for jobid %u\n",
2262 (int)pjob
->sysjob
, (unsigned int)jobid
));
2266 if (!is_owner(server_info
, lp_const_servicename(snum
), jobid
) &&
2267 !print_access_check(server_info
, smbd_messaging_context(), snum
,
2268 JOB_ACCESS_ADMINISTER
)) {
2269 DEBUG(3, ("resume denied by security descriptor\n"));
2270 *errcode
= WERR_ACCESS_DENIED
;
2272 /* BEGIN_ADMIN_LOG */
2273 sys_adminlog( LOG_ERR
,
2274 "Permission denied-- user not allowed to delete, \
2275 pause, or resume print job. User name: %s. Printer name: %s.",
2276 uidtoname(server_info
->utok
.uid
),
2277 lp_printername(snum
) );
2282 ret
= (*(current_printif
->job_resume
))(snum
, pjob
);
2285 *errcode
= WERR_INVALID_PARAM
;
2289 /* force update the database */
2290 print_cache_flush(lp_const_servicename(snum
));
2292 /* Send a printer notify message */
2294 notify_job_status(sharename
, jobid
, JOB_STATUS_QUEUED
);
2299 /****************************************************************************
2300 Write to a print file.
2301 ****************************************************************************/
2303 ssize_t
print_job_write(int snum
, uint32 jobid
, const char *buf
, SMB_OFF_T pos
, size_t size
)
2305 const char* sharename
= lp_const_servicename(snum
);
2306 ssize_t return_code
;
2307 struct printjob
*pjob
;
2309 pjob
= print_job_find(sharename
, jobid
);
2313 /* don't allow another process to get this info - it is meaningless */
2314 if (pjob
->pid
!= sys_getpid())
2317 /* if SMBD is spooling this can't be allowed */
2318 if (pjob
->status
== PJOB_SMBD_SPOOLING
) {
2322 return_code
= write_data_at_offset(pjob
->fd
, buf
, size
, pos
);
2324 if (return_code
>0) {
2326 pjob_store(sharename
, jobid
, pjob
);
2331 /****************************************************************************
2332 Get the queue status - do not update if db is out of date.
2333 ****************************************************************************/
2335 static int get_queue_status(const char* sharename
, print_status_struct
*status
)
2339 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
2343 ZERO_STRUCTP(status
);
2350 fstr_sprintf(keystr
, "STATUS/%s", sharename
);
2351 data
= tdb_fetch(pdb
->tdb
, string_tdb_data(keystr
));
2353 if (data
.dsize
== sizeof(print_status_struct
))
2354 /* this memcpy is ok since the status struct was
2355 not packed before storing it in the tdb */
2356 memcpy(status
, data
.dptr
, sizeof(print_status_struct
));
2357 SAFE_FREE(data
.dptr
);
2360 len
= tdb_fetch_int32(pdb
->tdb
, "INFO/total_jobs");
2361 release_print_db(pdb
);
2362 return (len
== -1 ? 0 : len
);
2365 /****************************************************************************
2366 Determine the number of jobs in a queue.
2367 ****************************************************************************/
2369 int print_queue_length(int snum
, print_status_struct
*pstatus
)
2371 const char* sharename
= lp_const_servicename( snum
);
2372 print_status_struct status
;
2375 ZERO_STRUCT( status
);
2377 /* make sure the database is up to date */
2378 if (print_cache_expired(lp_const_servicename(snum
), True
))
2379 print_queue_update(snum
, False
);
2381 /* also fetch the queue status */
2382 memset(&status
, 0, sizeof(status
));
2383 len
= get_queue_status(sharename
, &status
);
2391 /***************************************************************************
2392 Allocate a jobid. Hold the lock for as short a time as possible.
2393 ***************************************************************************/
2395 static WERROR
allocate_print_jobid(struct tdb_print_db
*pdb
, int snum
,
2396 const char *sharename
, uint32
*pjobid
)
2400 enum TDB_ERROR terr
;
2403 *pjobid
= (uint32
)-1;
2405 for (i
= 0; i
< 3; i
++) {
2406 /* Lock the database - only wait 20 seconds. */
2407 ret
= tdb_lock_bystring_with_timeout(pdb
->tdb
,
2408 "INFO/nextjob", 20);
2410 DEBUG(0, ("allocate_print_jobid: "
2411 "Failed to lock printing database %s\n",
2413 terr
= tdb_error(pdb
->tdb
);
2414 return ntstatus_to_werror(map_nt_error_from_tdb(terr
));
2417 if (!tdb_fetch_uint32(pdb
->tdb
, "INFO/nextjob", &jobid
)) {
2418 terr
= tdb_error(pdb
->tdb
);
2419 if (terr
!= TDB_ERR_NOEXIST
) {
2420 DEBUG(0, ("allocate_print_jobid: "
2421 "Failed to fetch INFO/nextjob "
2422 "for print queue %s\n", sharename
));
2423 tdb_unlock_bystring(pdb
->tdb
, "INFO/nextjob");
2424 return ntstatus_to_werror(map_nt_error_from_tdb(terr
));
2426 DEBUG(10, ("allocate_print_jobid: "
2427 "No existing jobid in %s\n", sharename
));
2431 DEBUG(10, ("allocate_print_jobid: "
2432 "Read jobid %u from %s\n", jobid
, sharename
));
2434 jobid
= NEXT_JOBID(jobid
);
2436 ret
= tdb_store_int32(pdb
->tdb
, "INFO/nextjob", jobid
);
2438 terr
= tdb_error(pdb
->tdb
);
2439 DEBUG(3, ("allocate_print_jobid: "
2440 "Failed to store INFO/nextjob.\n"));
2441 tdb_unlock_bystring(pdb
->tdb
, "INFO/nextjob");
2442 return ntstatus_to_werror(map_nt_error_from_tdb(terr
));
2445 /* We've finished with the INFO/nextjob lock. */
2446 tdb_unlock_bystring(pdb
->tdb
, "INFO/nextjob");
2448 if (!print_job_exists(sharename
, jobid
)) {
2451 DEBUG(10, ("allocate_print_jobid: "
2452 "Found jobid %u in %s\n", jobid
, sharename
));
2456 DEBUG(0, ("allocate_print_jobid: "
2457 "Failed to allocate a print job for queue %s\n",
2459 /* Probably full... */
2460 return WERR_NO_SPOOL_SPACE
;
2463 /* Store a dummy placeholder. */
2469 if (tdb_store(pdb
->tdb
, print_key(jobid
, &tmp
), dum
,
2470 TDB_INSERT
) == -1) {
2471 DEBUG(3, ("allocate_print_jobid: "
2472 "jobid (%d) failed to store placeholder.\n",
2474 terr
= tdb_error(pdb
->tdb
);
2475 return ntstatus_to_werror(map_nt_error_from_tdb(terr
));
2483 /***************************************************************************
2484 Append a jobid to the 'jobs changed' list.
2485 ***************************************************************************/
2487 static bool add_to_jobs_changed(struct tdb_print_db
*pdb
, uint32 jobid
)
2492 SIVAL(&store_jobid
, 0, jobid
);
2493 data
.dptr
= (uint8
*)&store_jobid
;
2496 DEBUG(10,("add_to_jobs_changed: Added jobid %u\n", (unsigned int)jobid
));
2498 return (tdb_append(pdb
->tdb
, string_tdb_data("INFO/jobs_changed"),
2503 /***************************************************************************
2504 Do all checks needed to determine if we can start a job.
2505 ***************************************************************************/
2507 static WERROR
print_job_checks(struct auth_serversupplied_info
*server_info
,
2508 int snum
, int *njobs
)
2510 const char *sharename
= lp_const_servicename(snum
);
2511 uint64_t dspace
, dsize
;
2515 if (!print_access_check(server_info
, smbd_messaging_context(), snum
,
2516 PRINTER_ACCESS_USE
)) {
2517 DEBUG(3, ("print_job_checks: "
2518 "job start denied by security descriptor\n"));
2519 return WERR_ACCESS_DENIED
;
2522 if (!print_time_access_check(server_info
, smbd_messaging_context(),
2524 DEBUG(3, ("print_job_checks: "
2525 "job start denied by time check\n"));
2526 return WERR_ACCESS_DENIED
;
2529 /* see if we have sufficient disk space */
2530 if (lp_minprintspace(snum
)) {
2531 minspace
= lp_minprintspace(snum
);
2532 ret
= sys_fsusage(lp_pathname(snum
), &dspace
, &dsize
);
2533 if (ret
== 0 && dspace
< 2*minspace
) {
2534 DEBUG(3, ("print_job_checks: "
2535 "disk space check failed.\n"));
2536 return WERR_NO_SPOOL_SPACE
;
2540 /* for autoloaded printers, check that the printcap entry still exists */
2541 if (lp_autoloaded(snum
) && !pcap_printername_ok(sharename
)) {
2542 DEBUG(3, ("print_job_checks: printer name %s check failed.\n",
2544 return WERR_ACCESS_DENIED
;
2547 /* Insure the maximum queue size is not violated */
2548 *njobs
= print_queue_length(snum
, NULL
);
2549 if (*njobs
> lp_maxprintjobs(snum
)) {
2550 DEBUG(3, ("print_job_checks: Queue %s number of jobs (%d) "
2551 "larger than max printjobs per queue (%d).\n",
2552 sharename
, *njobs
, lp_maxprintjobs(snum
)));
2553 return WERR_NO_SPOOL_SPACE
;
2559 /***************************************************************************
2561 ***************************************************************************/
2563 static WERROR
print_job_spool_file(int snum
, uint32_t jobid
,
2564 const char *output_file
,
2565 struct printjob
*pjob
)
2572 /* if this file is within the printer path, it means that smbd
2573 * is spooling it and will pass us control when it is finished.
2574 * Verify that the file name is ok, within path, and it is
2575 * already already there */
2577 path
= lp_pathname(snum
);
2579 if (strncmp(output_file
, path
, len
) == 0 &&
2580 (output_file
[len
- 1] == '/' || output_file
[len
] == '/')) {
2582 /* verify path is not too long */
2583 if (strlen(output_file
) >= sizeof(pjob
->filename
)) {
2584 return WERR_INVALID_NAME
;
2587 /* verify that the file exists */
2588 if (sys_stat(output_file
, &st
, false) != 0) {
2589 return WERR_INVALID_NAME
;
2592 fstrcpy(pjob
->filename
, output_file
);
2594 DEBUG(3, ("print_job_spool_file:"
2595 "External spooling activated"));
2597 /* we do not open the file until spooling is done */
2599 pjob
->status
= PJOB_SMBD_SPOOLING
;
2605 slprintf(pjob
->filename
, sizeof(pjob
->filename
)-1,
2606 "%s/%s%.8u.XXXXXX", lp_pathname(snum
),
2607 PRINT_SPOOL_PREFIX
, (unsigned int)jobid
);
2608 pjob
->fd
= mkstemp(pjob
->filename
);
2610 if (pjob
->fd
== -1) {
2611 werr
= map_werror_from_unix(errno
);
2612 if (W_ERROR_EQUAL(werr
, WERR_ACCESS_DENIED
)) {
2613 /* Common setup error, force a report. */
2614 DEBUG(0, ("print_job_spool_file: "
2615 "insufficient permissions to open spool "
2616 "file %s.\n", pjob
->filename
));
2618 /* Normal case, report at level 3 and above. */
2619 DEBUG(3, ("print_job_spool_file: "
2620 "can't open spool file %s\n",
2629 /***************************************************************************
2630 Start spooling a job - return the jobid.
2631 ***************************************************************************/
2633 WERROR
print_job_start(struct auth_serversupplied_info
*server_info
,
2634 int snum
, const char *docname
, const char *filename
,
2635 struct spoolss_DeviceMode
*devmode
, uint32_t *_jobid
)
2639 struct printjob pjob
;
2640 const char *sharename
= lp_const_servicename(snum
);
2641 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
2646 return WERR_INTERNAL_DB_CORRUPTION
;
2649 path
= lp_pathname(snum
);
2651 werr
= print_job_checks(server_info
, snum
, &njobs
);
2652 if (!W_ERROR_IS_OK(werr
)) {
2653 release_print_db(pdb
);
2657 DEBUG(10, ("print_job_start: "
2658 "Queue %s number of jobs (%d), max printjobs = %d\n",
2659 sharename
, njobs
, lp_maxprintjobs(snum
)));
2661 werr
= allocate_print_jobid(pdb
, snum
, sharename
, &jobid
);
2662 if (!W_ERROR_IS_OK(werr
)) {
2666 /* create the database entry */
2670 pjob
.pid
= sys_getpid();
2673 pjob
.starttime
= time(NULL
);
2674 pjob
.status
= LPQ_SPOOLING
;
2676 pjob
.spooled
= False
;
2678 pjob
.devmode
= devmode
;
2680 fstrcpy(pjob
.jobname
, docname
);
2682 fstrcpy(pjob
.user
, lp_printjob_username(snum
));
2683 standard_sub_advanced(sharename
, server_info
->sanitized_username
,
2684 path
, server_info
->utok
.gid
,
2685 server_info
->sanitized_username
,
2686 server_info
->info3
->base
.domain
.string
,
2687 pjob
.user
, sizeof(pjob
.user
)-1);
2688 /* ensure NULL termination */
2689 pjob
.user
[sizeof(pjob
.user
)-1] = '\0';
2691 fstrcpy(pjob
.queuename
, lp_const_servicename(snum
));
2693 /* we have a job entry - now create the spool file */
2694 werr
= print_job_spool_file(snum
, jobid
, filename
, &pjob
);
2695 if (!W_ERROR_IS_OK(werr
)) {
2699 pjob_store(sharename
, jobid
, &pjob
);
2701 /* Update the 'jobs changed' entry used by print_queue_status. */
2702 add_to_jobs_changed(pdb
, jobid
);
2704 /* Ensure we keep a rough count of the number of total jobs... */
2705 tdb_change_int32_atomic(pdb
->tdb
, "INFO/total_jobs", &njobs
, 1);
2707 release_print_db(pdb
);
2714 pjob_delete(sharename
, jobid
);
2717 release_print_db(pdb
);
2719 DEBUG(3, ("print_job_start: returning fail. "
2720 "Error = %s\n", win_errstr(werr
)));
2724 /****************************************************************************
2725 Update the number of pages spooled to jobid
2726 ****************************************************************************/
2728 void print_job_endpage(int snum
, uint32 jobid
)
2730 const char* sharename
= lp_const_servicename(snum
);
2731 struct printjob
*pjob
;
2733 pjob
= print_job_find(sharename
, jobid
);
2736 /* don't allow another process to get this info - it is meaningless */
2737 if (pjob
->pid
!= sys_getpid())
2741 pjob_store(sharename
, jobid
, pjob
);
2744 /****************************************************************************
2745 Print a file - called on closing the file. This spools the job.
2746 If normal close is false then we're tearing down the jobs - treat as an
2748 ****************************************************************************/
2750 NTSTATUS
print_job_end(int snum
, uint32 jobid
, enum file_close_type close_type
)
2752 const char* sharename
= lp_const_servicename(snum
);
2753 struct printjob
*pjob
;
2755 SMB_STRUCT_STAT sbuf
;
2756 struct printif
*current_printif
= get_printer_fns( snum
);
2757 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2759 pjob
= print_job_find(sharename
, jobid
);
2762 return NT_STATUS_PRINT_CANCELLED
;
2765 if (pjob
->spooled
|| pjob
->pid
!= sys_getpid()) {
2766 return NT_STATUS_ACCESS_DENIED
;
2769 if (close_type
== NORMAL_CLOSE
|| close_type
== SHUTDOWN_CLOSE
) {
2770 if (pjob
->status
== PJOB_SMBD_SPOOLING
) {
2771 /* take over the file now, smbd is done */
2772 if (sys_stat(pjob
->filename
, &sbuf
, false) != 0) {
2773 status
= map_nt_error_from_unix(errno
);
2774 DEBUG(3, ("print_job_end: "
2775 "stat file failed for jobid %d\n",
2780 pjob
->status
= LPQ_SPOOLING
;
2784 if ((sys_fstat(pjob
->fd
, &sbuf
, false) != 0)) {
2785 status
= map_nt_error_from_unix(errno
);
2787 DEBUG(3, ("print_job_end: "
2788 "stat file failed for jobid %d\n",
2796 pjob
->size
= sbuf
.st_ex_size
;
2800 * Not a normal close, something has gone wrong. Cleanup.
2802 if (pjob
->fd
!= -1) {
2808 /* Technically, this is not quite right. If the printer has a separator
2809 * page turned on, the NT spooler prints the separator page even if the
2810 * print job is 0 bytes. 010215 JRR */
2811 if (pjob
->size
== 0 || pjob
->status
== LPQ_DELETING
) {
2812 /* don't bother spooling empty files or something being deleted. */
2813 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
2814 pjob
->filename
, pjob
->size
? "deleted" : "zero length" ));
2815 unlink(pjob
->filename
);
2816 pjob_delete(sharename
, jobid
);
2817 return NT_STATUS_OK
;
2820 ret
= (*(current_printif
->job_submit
))(snum
, pjob
);
2823 status
= NT_STATUS_PRINT_CANCELLED
;
2827 /* The print job has been successfully handed over to the back-end */
2829 pjob
->spooled
= True
;
2830 pjob
->status
= LPQ_QUEUED
;
2831 pjob_store(sharename
, jobid
, pjob
);
2833 /* make sure the database is up to date */
2834 if (print_cache_expired(lp_const_servicename(snum
), True
))
2835 print_queue_update(snum
, False
);
2837 return NT_STATUS_OK
;
2841 /* The print job was not successfully started. Cleanup */
2842 /* Still need to add proper error return propagation! 010122:JRR */
2844 unlink(pjob
->filename
);
2845 pjob_delete(sharename
, jobid
);
2849 /****************************************************************************
2850 Get a snapshot of jobs in the system without traversing.
2851 ****************************************************************************/
2853 static bool get_stored_queue_info(struct tdb_print_db
*pdb
, int snum
, int *pcount
, print_queue_struct
**ppqueue
)
2855 TDB_DATA data
, cgdata
;
2856 print_queue_struct
*queue
= NULL
;
2858 uint32 extra_count
= 0;
2859 int total_count
= 0;
2862 int max_reported_jobs
= lp_max_reported_jobs(snum
);
2864 const char* sharename
= lp_servicename(snum
);
2866 /* make sure the database is up to date */
2867 if (print_cache_expired(lp_const_servicename(snum
), True
))
2868 print_queue_update(snum
, False
);
2874 ZERO_STRUCT(cgdata
);
2876 /* Get the stored queue data. */
2877 data
= tdb_fetch(pdb
->tdb
, string_tdb_data("INFO/linear_queue_array"));
2879 if (data
.dptr
&& data
.dsize
>= sizeof(qcount
))
2880 len
+= tdb_unpack(data
.dptr
+ len
, data
.dsize
- len
, "d", &qcount
);
2882 /* Get the changed jobs list. */
2883 cgdata
= tdb_fetch(pdb
->tdb
, string_tdb_data("INFO/jobs_changed"));
2884 if (cgdata
.dptr
!= NULL
&& (cgdata
.dsize
% 4 == 0))
2885 extra_count
= cgdata
.dsize
/4;
2887 DEBUG(5,("get_stored_queue_info: qcount = %u, extra_count = %u\n", (unsigned int)qcount
, (unsigned int)extra_count
));
2889 /* Allocate the queue size. */
2890 if (qcount
== 0 && extra_count
== 0)
2893 if ((queue
= SMB_MALLOC_ARRAY(print_queue_struct
, qcount
+ extra_count
)) == NULL
)
2896 /* Retrieve the linearised queue data. */
2898 for( i
= 0; i
< qcount
; i
++) {
2899 uint32 qjob
, qsize
, qpage_count
, qstatus
, qpriority
, qtime
;
2900 len
+= tdb_unpack(data
.dptr
+ len
, data
.dsize
- len
, "ddddddff",
2909 queue
[i
].job
= qjob
;
2910 queue
[i
].size
= qsize
;
2911 queue
[i
].page_count
= qpage_count
;
2912 queue
[i
].status
= qstatus
;
2913 queue
[i
].priority
= qpriority
;
2914 queue
[i
].time
= qtime
;
2917 total_count
= qcount
;
2919 /* Add in the changed jobids. */
2920 for( i
= 0; i
< extra_count
; i
++) {
2922 struct printjob
*pjob
;
2924 jobid
= IVAL(cgdata
.dptr
, i
*4);
2925 DEBUG(5,("get_stored_queue_info: changed job = %u\n", (unsigned int)jobid
));
2926 pjob
= print_job_find(lp_const_servicename(snum
), jobid
);
2928 DEBUG(5,("get_stored_queue_info: failed to find changed job = %u\n", (unsigned int)jobid
));
2929 remove_from_jobs_changed(sharename
, jobid
);
2933 queue
[total_count
].job
= jobid
;
2934 queue
[total_count
].size
= pjob
->size
;
2935 queue
[total_count
].page_count
= pjob
->page_count
;
2936 queue
[total_count
].status
= pjob
->status
;
2937 queue
[total_count
].priority
= 1;
2938 queue
[total_count
].time
= pjob
->starttime
;
2939 fstrcpy(queue
[total_count
].fs_user
, pjob
->user
);
2940 fstrcpy(queue
[total_count
].fs_file
, pjob
->jobname
);
2944 /* Sort the queue by submission time otherwise they are displayed
2947 TYPESAFE_QSORT(queue
, total_count
, printjob_comp
);
2949 DEBUG(5,("get_stored_queue_info: total_count = %u\n", (unsigned int)total_count
));
2951 if (max_reported_jobs
&& total_count
> max_reported_jobs
)
2952 total_count
= max_reported_jobs
;
2955 *pcount
= total_count
;
2961 SAFE_FREE(data
.dptr
);
2962 SAFE_FREE(cgdata
.dptr
);
2966 /****************************************************************************
2967 Get a printer queue listing.
2968 set queue = NULL and status = NULL if you just want to update the cache
2969 ****************************************************************************/
2971 int print_queue_status(int snum
,
2972 print_queue_struct
**ppqueue
,
2973 print_status_struct
*status
)
2977 const char *sharename
;
2978 struct tdb_print_db
*pdb
;
2981 /* make sure the database is up to date */
2983 if (print_cache_expired(lp_const_servicename(snum
), True
))
2984 print_queue_update(snum
, False
);
2986 /* return if we are done */
2987 if ( !ppqueue
|| !status
)
2991 sharename
= lp_const_servicename(snum
);
2992 pdb
= get_print_db_byname(sharename
);
2998 * Fetch the queue status. We must do this first, as there may
2999 * be no jobs in the queue.
3002 ZERO_STRUCTP(status
);
3003 slprintf(keystr
, sizeof(keystr
)-1, "STATUS/%s", sharename
);
3004 key
= string_tdb_data(keystr
);
3006 data
= tdb_fetch(pdb
->tdb
, key
);
3008 if (data
.dsize
== sizeof(*status
)) {
3009 /* this memcpy is ok since the status struct was
3010 not packed before storing it in the tdb */
3011 memcpy(status
, data
.dptr
, sizeof(*status
));
3013 SAFE_FREE(data
.dptr
);
3017 * Now, fetch the print queue information. We first count the number
3018 * of entries, and then only retrieve the queue if necessary.
3021 if (!get_stored_queue_info(pdb
, snum
, &count
, ppqueue
)) {
3022 release_print_db(pdb
);
3026 release_print_db(pdb
);
3030 /****************************************************************************
3032 ****************************************************************************/
3034 WERROR
print_queue_pause(struct auth_serversupplied_info
*server_info
, int snum
)
3037 struct printif
*current_printif
= get_printer_fns( snum
);
3039 if (!print_access_check(server_info
, smbd_messaging_context(), snum
,
3040 PRINTER_ACCESS_ADMINISTER
)) {
3041 return WERR_ACCESS_DENIED
;
3047 ret
= (*(current_printif
->queue_pause
))(snum
);
3052 return WERR_INVALID_PARAM
;
3055 /* force update the database */
3056 print_cache_flush(lp_const_servicename(snum
));
3058 /* Send a printer notify message */
3060 notify_printer_status(snum
, PRINTER_STATUS_PAUSED
);
3065 /****************************************************************************
3067 ****************************************************************************/
3069 WERROR
print_queue_resume(struct auth_serversupplied_info
*server_info
, int snum
)
3072 struct printif
*current_printif
= get_printer_fns( snum
);
3074 if (!print_access_check(server_info
, smbd_messaging_context(), snum
,
3075 PRINTER_ACCESS_ADMINISTER
)) {
3076 return WERR_ACCESS_DENIED
;
3081 ret
= (*(current_printif
->queue_resume
))(snum
);
3086 return WERR_INVALID_PARAM
;
3089 /* make sure the database is up to date */
3090 if (print_cache_expired(lp_const_servicename(snum
), True
))
3091 print_queue_update(snum
, True
);
3093 /* Send a printer notify message */
3095 notify_printer_status(snum
, PRINTER_STATUS_OK
);
3100 /****************************************************************************
3101 Purge a queue - implemented by deleting all jobs that we can delete.
3102 ****************************************************************************/
3104 WERROR
print_queue_purge(struct auth_serversupplied_info
*server_info
, int snum
)
3106 print_queue_struct
*queue
;
3107 print_status_struct status
;
3111 /* Force and update so the count is accurate (i.e. not a cached count) */
3112 print_queue_update(snum
, True
);
3114 can_job_admin
= print_access_check(server_info
,
3115 smbd_messaging_context(),
3117 JOB_ACCESS_ADMINISTER
);
3118 njobs
= print_queue_status(snum
, &queue
, &status
);
3120 if ( can_job_admin
)
3123 for (i
=0;i
<njobs
;i
++) {
3124 bool owner
= is_owner(server_info
, lp_const_servicename(snum
),
3127 if (owner
|| can_job_admin
) {
3128 print_job_delete1(snum
, queue
[i
].job
);
3132 if ( can_job_admin
)
3135 /* update the cache */
3136 print_queue_update( snum
, True
);