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(struct tevent_context *ev,
561 struct messaging_context *msg_ctx,
562 const char* sharename, uint32 jobid,
563 struct printjob *old_data,
564 struct printjob *new_data)
566 bool new_job = False;
571 /* Job attributes that can't be changed. We only send
572 notification for these on a new job. */
574 /* ACHTUNG! Due to a bug in Samba's spoolss parsing of the
575 NOTIFY_INFO_DATA buffer, we *have* to send the job submission
576 time first or else we'll end up with potential alignment
577 errors. I don't think the systemtime should be spooled as
578 a string, but this gets us around that error.
579 --jerry (i'll feel dirty for this) */
582 notify_job_submitted(ev, msg_ctx,
583 sharename, jobid, new_data->starttime);
584 notify_job_username(ev, msg_ctx,
585 sharename, jobid, new_data->user);
588 if (new_job || !strequal(old_data->jobname, new_data->jobname))
589 notify_job_name(ev, msg_ctx,
590 sharename, jobid, new_data->jobname);
592 /* Job attributes of a new job or attributes that can be
595 if (new_job || !strequal(old_data->jobname, new_data->jobname))
596 notify_job_name(ev, msg_ctx,
597 sharename, jobid, new_data->jobname);
599 if (new_job || old_data->status != new_data->status)
600 notify_job_status(ev, msg_ctx,
602 map_to_spoolss_status(new_data->status));
604 if (new_job || old_data->size != new_data->size)
605 notify_job_total_bytes(ev, msg_ctx,
606 sharename, jobid, new_data->size);
608 if (new_job || old_data->page_count != new_data->page_count)
609 notify_job_total_pages(ev, msg_ctx,
610 sharename, jobid, new_data->page_count);
613 /****************************************************************************
614 Store a job structure back to the database.
615 ****************************************************************************/
617 static bool pjob_store(struct tevent_context *ev,
618 struct messaging_context *msg_ctx,
619 const char* sharename, uint32 jobid,
620 struct printjob *pjob)
623 TDB_DATA old_data, new_data;
625 struct tdb_print_db *pdb = get_print_db_byname(sharename);
627 int len, newlen, buflen;
635 old_data = tdb_fetch(pdb->tdb, print_key(jobid, &tmp));
637 /* Doh! Now we have to pack/unpack data since the NT_DEVICEMODE was added */
644 len += tdb_pack(buf+len, buflen-len, "dddddddddffff",
646 (uint32)pjob->sysjob,
648 (uint32)pjob->starttime,
649 (uint32)pjob->status,
651 (uint32)pjob->page_count,
652 (uint32)pjob->spooled,
653 (uint32)pjob->smbjob,
659 len += pack_devicemode(pjob->devmode, buf+len, buflen-len);
662 buf = (uint8 *)SMB_REALLOC(buf, len);
664 DEBUG(0,("pjob_store: failed to enlarge buffer!\n"));
669 } while ( buflen != len );
675 new_data.dsize = len;
676 ret = (tdb_store(pdb->tdb, print_key(jobid, &tmp), new_data,
679 release_print_db(pdb);
681 /* Send notify updates for what has changed */
684 struct printjob old_pjob;
686 if ( old_data.dsize )
688 if ( unpack_pjob( old_data.dptr, old_data.dsize, &old_pjob ) != -1 )
690 pjob_store_notify(server_event_context(),
692 sharename, jobid, &old_pjob,
694 talloc_free(old_pjob.devmode);
699 pjob_store_notify(server_event_context(), msg_ctx,
700 sharename, jobid, NULL, pjob);
705 SAFE_FREE( old_data.dptr );
711 /****************************************************************************
712 Remove a job structure from the database.
713 ****************************************************************************/
715 static void pjob_delete(struct tevent_context *ev,
716 struct messaging_context *msg_ctx,
717 const char* sharename, uint32 jobid)
720 struct printjob *pjob;
721 uint32 job_status = 0;
722 struct tdb_print_db *pdb;
724 pdb = get_print_db_byname( sharename );
729 pjob = print_job_find( sharename, jobid );
732 DEBUG(5, ("pjob_delete: we were asked to delete nonexistent job %u\n",
733 (unsigned int)jobid));
734 release_print_db(pdb);
738 /* We must cycle through JOB_STATUS_DELETING and
739 JOB_STATUS_DELETED for the port monitor to delete the job
742 job_status = JOB_STATUS_DELETING|JOB_STATUS_DELETED;
743 notify_job_status(ev, msg_ctx, sharename, jobid, job_status);
745 /* Remove from printing.tdb */
747 tdb_delete(pdb->tdb, print_key(jobid, &tmp));
748 remove_from_jobs_changed(sharename, jobid);
749 release_print_db( pdb );
750 rap_jobid_delete(sharename, jobid);
753 /****************************************************************************
754 List a unix job in the print database.
755 ****************************************************************************/
757 static void print_unix_job(struct tevent_context *ev,
758 struct messaging_context *msg_ctx,
759 const char *sharename, print_queue_struct *q,
762 struct printjob pj, *old_pj;
764 if (jobid == (uint32)-1)
765 jobid = q->job + UNIX_JOB_START;
767 /* Preserve the timestamp on an existing unix print job */
769 old_pj = print_job_find(sharename, jobid);
776 pj.starttime = old_pj ? old_pj->starttime : q->time;
777 pj.status = q->status;
780 fstrcpy(pj.filename, old_pj ? old_pj->filename : "");
781 if (jobid < UNIX_JOB_START) {
783 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : "Remote Downlevel Document");
786 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : q->fs_file);
788 fstrcpy(pj.user, old_pj ? old_pj->user : q->fs_user);
789 fstrcpy(pj.queuename, old_pj ? old_pj->queuename : sharename );
791 pjob_store(ev, msg_ctx, sharename, jobid, &pj);
795 struct traverse_struct {
796 print_queue_struct *queue;
797 int qcount, snum, maxcount, total_jobs;
798 const char *sharename;
800 const char *lprm_command;
801 struct printif *print_if;
802 struct tevent_context *ev;
803 struct messaging_context *msg_ctx;
806 /****************************************************************************
807 Utility fn to delete any jobs that are no longer active.
808 ****************************************************************************/
810 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
812 struct traverse_struct *ts = (struct traverse_struct *)state;
813 struct printjob pjob;
817 if ( key.dsize != sizeof(jobid) )
820 jobid = IVAL(key.dptr, 0);
821 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
823 talloc_free(pjob.devmode);
827 /* remove a unix job if it isn't in the system queue any more */
829 for (i=0;i<ts->qcount;i++) {
830 uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
831 if (jobid == u_jobid)
834 if (i == ts->qcount) {
835 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !smbjob\n",
836 (unsigned int)jobid ));
837 pjob_delete(ts->ev, ts->msg_ctx,
838 ts->sharename, jobid);
842 /* need to continue the the bottom of the function to
843 save the correct attributes */
846 /* maybe it hasn't been spooled yet */
848 /* if a job is not spooled and the process doesn't
849 exist then kill it. This cleans up after smbd
851 if (!process_exists_by_pid(pjob.pid)) {
852 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !process_exists (%u)\n",
853 (unsigned int)jobid, (unsigned int)pjob.pid ));
854 pjob_delete(ts->ev, ts->msg_ctx,
855 ts->sharename, jobid);
861 /* this check only makes sense for jobs submitted from Windows clients */
864 for (i=0;i<ts->qcount;i++) {
867 if ( pjob.status == LPQ_DELETED )
870 curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
872 if (jobid == curr_jobid) {
874 /* try to clean up any jobs that need to be deleted */
876 if ( pjob.status == LPQ_DELETING ) {
879 result = (*(ts->print_if->job_delete))(
880 ts->sharename, ts->lprm_command, &pjob );
883 /* if we can't delete, then reset the job status */
884 pjob.status = LPQ_QUEUED;
885 pjob_store(ts->ev, ts->msg_ctx,
886 ts->sharename, jobid, &pjob);
889 /* if we deleted the job, the remove the tdb record */
892 ts->sharename, jobid);
893 pjob.status = LPQ_DELETED;
903 /* The job isn't in the system queue - we have to assume it has
904 completed, so delete the database entry. */
906 if (i == ts->qcount) {
908 /* A race can occur between the time a job is spooled and
909 when it appears in the lpq output. This happens when
910 the job is added to printing.tdb when another smbd
911 running print_queue_update() has completed a lpq and
912 is currently traversing the printing tdb and deleting jobs.
913 Don't delete the job if it was submitted after the lpq_time. */
915 if (pjob.starttime < ts->lpq_time) {
916 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to pjob.starttime (%u) < ts->lpq_time (%u)\n",
918 (unsigned int)pjob.starttime,
919 (unsigned int)ts->lpq_time ));
920 pjob_delete(ts->ev, ts->msg_ctx,
921 ts->sharename, jobid);
927 /* Save the pjob attributes we will store.
928 FIXME!!! This is the only place where queue->job
929 represents the SMB jobid --jerry */
931 ts->queue[i].job = jobid;
932 ts->queue[i].size = pjob.size;
933 ts->queue[i].page_count = pjob.page_count;
934 ts->queue[i].status = pjob.status;
935 ts->queue[i].priority = 1;
936 ts->queue[i].time = pjob.starttime;
937 fstrcpy(ts->queue[i].fs_user, pjob.user);
938 fstrcpy(ts->queue[i].fs_file, pjob.jobname);
945 /****************************************************************************
946 Check if the print queue has been updated recently enough.
947 ****************************************************************************/
949 static void print_cache_flush(const char *sharename)
952 struct tdb_print_db *pdb = get_print_db_byname(sharename);
956 slprintf(key, sizeof(key)-1, "CACHE/%s", sharename);
957 tdb_store_int32(pdb->tdb, key, -1);
958 release_print_db(pdb);
961 /****************************************************************************
962 Check if someone already thinks they are doing the update.
963 ****************************************************************************/
965 static pid_t get_updating_pid(const char *sharename)
970 struct tdb_print_db *pdb = get_print_db_byname(sharename);
974 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", sharename);
975 key = string_tdb_data(keystr);
977 data = tdb_fetch(pdb->tdb, key);
978 release_print_db(pdb);
979 if (!data.dptr || data.dsize != sizeof(pid_t)) {
980 SAFE_FREE(data.dptr);
984 updating_pid = IVAL(data.dptr, 0);
985 SAFE_FREE(data.dptr);
987 if (process_exists_by_pid(updating_pid))
993 /****************************************************************************
994 Set the fact that we're doing the update, or have finished doing the update
996 ****************************************************************************/
998 static void set_updating_pid(const fstring sharename, bool updating)
1003 pid_t updating_pid = sys_getpid();
1006 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1011 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", sharename);
1012 key = string_tdb_data(keystr);
1014 DEBUG(5, ("set_updating_pid: %s updating lpq cache for print share %s\n",
1015 updating ? "" : "not ",
1019 tdb_delete(pdb->tdb, key);
1020 release_print_db(pdb);
1024 SIVAL( buffer, 0, updating_pid);
1026 data.dsize = 4; /* we always assume this is a 4 byte value */
1028 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
1029 release_print_db(pdb);
1032 /****************************************************************************
1033 Sort print jobs by submittal time.
1034 ****************************************************************************/
1036 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
1047 /* Sort on job start time */
1049 if (j1->time == j2->time)
1051 return (j1->time > j2->time) ? 1 : -1;
1054 /****************************************************************************
1055 Store the sorted queue representation for later portmon retrieval.
1057 ****************************************************************************/
1059 static void store_queue_struct(struct tdb_print_db *pdb, struct traverse_struct *pts)
1062 int max_reported_jobs = lp_max_reported_jobs(pts->snum);
1063 print_queue_struct *queue = pts->queue;
1066 unsigned int qcount;
1068 if (max_reported_jobs && (max_reported_jobs < pts->qcount))
1069 pts->qcount = max_reported_jobs;
1072 /* Work out the size. */
1074 data.dsize += tdb_pack(NULL, 0, "d", qcount);
1076 for (i = 0; i < pts->qcount; i++) {
1077 if ( queue[i].status == LPQ_DELETED )
1081 data.dsize += tdb_pack(NULL, 0, "ddddddff",
1082 (uint32)queue[i].job,
1083 (uint32)queue[i].size,
1084 (uint32)queue[i].page_count,
1085 (uint32)queue[i].status,
1086 (uint32)queue[i].priority,
1087 (uint32)queue[i].time,
1092 if ((data.dptr = (uint8 *)SMB_MALLOC(data.dsize)) == NULL)
1096 len += tdb_pack(data.dptr + len, data.dsize - len, "d", qcount);
1097 for (i = 0; i < pts->qcount; i++) {
1098 if ( queue[i].status == LPQ_DELETED )
1101 len += tdb_pack(data.dptr + len, data.dsize - len, "ddddddff",
1102 (uint32)queue[i].job,
1103 (uint32)queue[i].size,
1104 (uint32)queue[i].page_count,
1105 (uint32)queue[i].status,
1106 (uint32)queue[i].priority,
1107 (uint32)queue[i].time,
1112 tdb_store(pdb->tdb, string_tdb_data("INFO/linear_queue_array"), data,
1114 SAFE_FREE(data.dptr);
1118 static TDB_DATA get_jobs_changed_data(struct tdb_print_db *pdb)
1124 data = tdb_fetch(pdb->tdb, string_tdb_data("INFO/jobs_changed"));
1125 if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0)) {
1126 SAFE_FREE(data.dptr);
1133 static void check_job_changed(const char *sharename, TDB_DATA data, uint32 jobid)
1136 unsigned int job_count = data.dsize / 4;
1138 for (i = 0; i < job_count; i++) {
1141 ch_jobid = IVAL(data.dptr, i*4);
1142 if (ch_jobid == jobid)
1143 remove_from_jobs_changed(sharename, jobid);
1147 /****************************************************************************
1148 Check if the print queue has been updated recently enough.
1149 ****************************************************************************/
1151 static bool print_cache_expired(const char *sharename, bool check_pending)
1154 time_t last_qscan_time, time_now = time(NULL);
1155 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1156 bool result = False;
1161 snprintf(key, sizeof(key), "CACHE/%s", sharename);
1162 last_qscan_time = (time_t)tdb_fetch_int32(pdb->tdb, key);
1165 * Invalidate the queue for 3 reasons.
1166 * (1). last queue scan time == -1.
1167 * (2). Current time - last queue scan time > allowed cache time.
1168 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1169 * This last test picks up machines for which the clock has been moved
1170 * forward, an lpq scan done and then the clock moved back. Otherwise
1171 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1174 if (last_qscan_time == ((time_t)-1)
1175 || (time_now - last_qscan_time) >= lp_lpqcachetime()
1176 || last_qscan_time > (time_now + MAX_CACHE_VALID_TIME))
1179 time_t msg_pending_time;
1181 DEBUG(4, ("print_cache_expired: cache expired for queue %s "
1182 "(last_qscan_time = %d, time now = %d, qcachetime = %d)\n",
1183 sharename, (int)last_qscan_time, (int)time_now,
1184 (int)lp_lpqcachetime() ));
1186 /* check if another smbd has already sent a message to update the
1187 queue. Give the pending message one minute to clear and
1188 then send another message anyways. Make sure to check for
1189 clocks that have been run forward and then back again. */
1191 snprintf(key, sizeof(key), "MSG_PENDING/%s", sharename);
1194 && tdb_fetch_uint32( pdb->tdb, key, &u )
1195 && (msg_pending_time=u) > 0
1196 && msg_pending_time <= time_now
1197 && (time_now - msg_pending_time) < 60 )
1199 DEBUG(4,("print_cache_expired: message already pending for %s. Accepting cache\n",
1208 release_print_db(pdb);
1212 /****************************************************************************
1213 main work for updating the lpq cahe for a printer queue
1214 ****************************************************************************/
1216 static void print_queue_update_internal( struct tevent_context *ev,
1217 struct messaging_context *msg_ctx,
1218 const char *sharename,
1219 struct printif *current_printif,
1220 char *lpq_command, char *lprm_command )
1223 print_queue_struct *queue = NULL;
1224 print_status_struct status;
1225 print_status_struct old_status;
1226 struct printjob *pjob;
1227 struct traverse_struct tstruct;
1230 fstring keystr, cachestr;
1231 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1237 DEBUG(5,("print_queue_update_internal: printer = %s, type = %d, lpq command = [%s]\n",
1238 sharename, current_printif->type, lpq_command));
1241 * Update the cache time FIRST ! Stops others even
1242 * attempting to get the lock and doing this
1243 * if the lpq takes a long time.
1246 slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", sharename);
1247 tdb_store_int32(pdb->tdb, cachestr, (int)time(NULL));
1249 /* get the current queue using the appropriate interface */
1250 ZERO_STRUCT(status);
1252 qcount = (*(current_printif->queue_get))(sharename,
1253 current_printif->type,
1254 lpq_command, &queue, &status);
1256 DEBUG(3, ("print_queue_update_internal: %d job%s in queue for %s\n",
1257 qcount, (qcount != 1) ? "s" : "", sharename));
1259 /* Sort the queue by submission time otherwise they are displayed
1262 TYPESAFE_QSORT(queue, qcount, printjob_comp);
1265 any job in the internal database that is marked as spooled
1266 and doesn't exist in the system queue is considered finished
1267 and removed from the database
1269 any job in the system database but not in the internal database
1270 is added as a unix job
1272 fill in any system job numbers as we go
1275 jcdata = get_jobs_changed_data(pdb);
1277 for (i=0; i<qcount; i++) {
1278 uint32 jobid = print_parse_jobid(queue[i].fs_file);
1280 if (jobid == (uint32)-1) {
1281 /* assume its a unix print job */
1282 print_unix_job(ev, msg_ctx,
1283 sharename, &queue[i], jobid);
1287 /* we have an active SMB print job - update its status */
1288 pjob = print_job_find(sharename, jobid);
1290 /* err, somethings wrong. Probably smbd was restarted
1291 with jobs in the queue. All we can do is treat them
1292 like unix jobs. Pity. */
1293 print_unix_job(ev, msg_ctx,
1294 sharename, &queue[i], jobid);
1298 pjob->sysjob = queue[i].job;
1300 /* don't reset the status on jobs to be deleted */
1302 if ( pjob->status != LPQ_DELETING )
1303 pjob->status = queue[i].status;
1305 pjob_store(ev, msg_ctx, sharename, jobid, pjob);
1307 check_job_changed(sharename, jcdata, jobid);
1310 SAFE_FREE(jcdata.dptr);
1312 /* now delete any queued entries that don't appear in the
1314 tstruct.queue = queue;
1315 tstruct.qcount = qcount;
1317 tstruct.total_jobs = 0;
1318 tstruct.lpq_time = time(NULL);
1319 tstruct.sharename = sharename;
1320 tstruct.lprm_command = lprm_command;
1321 tstruct.print_if = current_printif;
1323 tstruct.msg_ctx = msg_ctx;
1325 tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
1327 /* Store the linearised queue, max jobs only. */
1328 store_queue_struct(pdb, &tstruct);
1330 SAFE_FREE(tstruct.queue);
1332 DEBUG(10,("print_queue_update_internal: printer %s INFO/total_jobs = %d\n",
1333 sharename, tstruct.total_jobs ));
1335 tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
1337 get_queue_status(sharename, &old_status);
1338 if (old_status.qcount != qcount)
1339 DEBUG(10,("print_queue_update_internal: queue status change %d jobs -> %d jobs for printer %s\n",
1340 old_status.qcount, qcount, sharename));
1342 /* store the new queue status structure */
1343 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", sharename);
1344 key = string_tdb_data(keystr);
1346 status.qcount = qcount;
1347 data.dptr = (uint8 *)&status;
1348 data.dsize = sizeof(status);
1349 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
1352 * Update the cache time again. We want to do this call
1353 * as little as possible...
1356 slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", sharename);
1357 tdb_store_int32(pdb->tdb, keystr, (int32)time(NULL));
1359 /* clear the msg pending record for this queue */
1361 snprintf(keystr, sizeof(keystr), "MSG_PENDING/%s", sharename);
1363 if ( !tdb_store_uint32( pdb->tdb, keystr, 0 ) ) {
1364 /* log a message but continue on */
1366 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1370 release_print_db( pdb );
1375 /****************************************************************************
1376 Update the internal database from the system print queue for a queue.
1377 obtain a lock on the print queue before proceeding (needed when mutiple
1378 smbd processes maytry to update the lpq cache concurrently).
1379 ****************************************************************************/
1381 static void print_queue_update_with_lock( const char *sharename,
1382 struct printif *current_printif,
1383 char *lpq_command, char *lprm_command )
1386 struct tdb_print_db *pdb;
1388 DEBUG(5,("print_queue_update_with_lock: printer share = %s\n", sharename));
1389 pdb = get_print_db_byname(sharename);
1393 if ( !print_cache_expired(sharename, False) ) {
1394 DEBUG(5,("print_queue_update_with_lock: print cache for %s is still ok\n", sharename));
1395 release_print_db(pdb);
1400 * Check to see if someone else is doing this update.
1401 * This is essentially a mutex on the update.
1404 if (get_updating_pid(sharename) != -1) {
1405 release_print_db(pdb);
1409 /* Lock the queue for the database update */
1411 slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", sharename);
1412 /* Only wait 10 seconds for this. */
1413 if (tdb_lock_bystring_with_timeout(pdb->tdb, keystr, 10) == -1) {
1414 DEBUG(0,("print_queue_update_with_lock: Failed to lock printer %s database\n", sharename));
1415 release_print_db(pdb);
1420 * Ensure that no one else got in here.
1421 * If the updating pid is still -1 then we are
1425 if (get_updating_pid(sharename) != -1) {
1427 * Someone else is doing the update, exit.
1429 tdb_unlock_bystring(pdb->tdb, keystr);
1430 release_print_db(pdb);
1435 * We're going to do the update ourselves.
1438 /* Tell others we're doing the update. */
1439 set_updating_pid(sharename, True);
1442 * Allow others to enter and notice we're doing
1446 tdb_unlock_bystring(pdb->tdb, keystr);
1448 /* do the main work now */
1450 print_queue_update_internal(server_event_context(),
1451 server_messaging_context(),
1452 sharename, current_printif,
1453 lpq_command, lprm_command);
1455 /* Delete our pid from the db. */
1456 set_updating_pid(sharename, False);
1457 release_print_db(pdb);
1460 /****************************************************************************
1461 this is the receive function of the background lpq updater
1462 ****************************************************************************/
1463 static void print_queue_receive(struct messaging_context *msg,
1466 struct server_id server_id,
1470 char *lpqcommand = NULL, *lprmcommand = NULL;
1474 len = tdb_unpack( (uint8 *)data->data, data->length, "fdPP",
1481 SAFE_FREE(lpqcommand);
1482 SAFE_FREE(lprmcommand);
1483 DEBUG(0,("print_queue_receive: Got invalid print queue update message\n"));
1487 print_queue_update_with_lock(sharename,
1488 get_printer_fns_from_type((enum printing_types)printing_type),
1489 lpqcommand, lprmcommand );
1491 SAFE_FREE(lpqcommand);
1492 SAFE_FREE(lprmcommand);
1496 static void printing_pause_fd_handler(struct tevent_context *ev,
1497 struct tevent_fd *fde,
1502 * If pause_pipe[1] is closed it means the parent smbd
1503 * and children exited or aborted.
1505 exit_server_cleanly(NULL);
1508 extern struct child_pid *children;
1509 extern int num_children;
1511 static void add_child_pid(pid_t pid)
1513 struct child_pid *child;
1515 child = SMB_MALLOC_P(struct child_pid);
1516 if (child == NULL) {
1517 DEBUG(0, ("Could not add child struct -- malloc failed\n"));
1521 DLIST_ADD(children, child);
1525 static pid_t background_lpq_updater_pid = -1;
1527 /****************************************************************************
1528 main thread of the background lpq updater
1529 ****************************************************************************/
1530 void start_background_queue(struct tevent_context *ev,
1531 struct messaging_context *msg_ctx)
1533 /* Use local variables for this as we don't
1534 * need to save the parent side of this, just
1535 * ensure it closes when the process exits.
1539 DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
1541 if (pipe(pause_pipe) == -1) {
1542 DEBUG(5,("start_background_queue: cannot create pipe. %s\n", strerror(errno) ));
1546 background_lpq_updater_pid = sys_fork();
1548 if (background_lpq_updater_pid == -1) {
1549 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
1553 /* Track the printing pid along with other smbd children */
1554 add_child_pid(background_lpq_updater_pid);
1556 if(background_lpq_updater_pid == 0) {
1557 struct tevent_fd *fde;
1562 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
1564 close(pause_pipe[0]);
1567 status = reinit_after_fork(msg_ctx, ev, procid_self(), true);
1569 if (!NT_STATUS_IS_OK(status)) {
1570 DEBUG(0,("reinit_after_fork() failed\n"));
1571 smb_panic("reinit_after_fork() failed");
1574 smbd_setup_sig_term_handler();
1575 smbd_setup_sig_hup_handler(ev, msg_ctx);
1577 if (!serverid_register(procid_self(),
1578 FLAG_MSG_GENERAL|FLAG_MSG_SMBD
1579 |FLAG_MSG_PRINT_GENERAL)) {
1583 if (!locking_init()) {
1587 messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
1588 print_queue_receive);
1590 fde = tevent_add_fd(ev, ev, pause_pipe[1], TEVENT_FD_READ,
1591 printing_pause_fd_handler,
1594 DEBUG(0,("tevent_add_fd() failed for pause_pipe\n"));
1595 smb_panic("tevent_add_fd() failed for pause_pipe");
1598 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
1599 ret = tevent_loop_wait(ev);
1600 /* should not be reached */
1601 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
1602 ret, (ret == 0) ? "out of events" : strerror(errno)));
1606 close(pause_pipe[1]);
1609 /****************************************************************************
1610 update the internal database from the system print queue for a queue
1611 ****************************************************************************/
1613 static void print_queue_update(struct messaging_context *msg_ctx,
1614 int snum, bool force)
1618 char *lpqcommand = NULL;
1619 char *lprmcommand = NULL;
1620 uint8 *buffer = NULL;
1623 struct tdb_print_db *pdb;
1625 struct printif *current_printif;
1626 TALLOC_CTX *ctx = talloc_tos();
1628 fstrcpy( sharename, lp_const_servicename(snum));
1630 /* don't strip out characters like '$' from the printername */
1632 lpqcommand = talloc_string_sub2(ctx,
1633 lp_lpqcommand(snum),
1635 lp_printername(snum),
1636 false, false, false);
1640 lpqcommand = talloc_sub_advanced(ctx,
1641 lp_servicename(snum),
1642 current_user_info.unix_name,
1644 current_user.ut.gid,
1645 get_current_username(),
1646 current_user_info.domain,
1652 lprmcommand = talloc_string_sub2(ctx,
1653 lp_lprmcommand(snum),
1655 lp_printername(snum),
1656 false, false, false);
1660 lprmcommand = talloc_sub_advanced(ctx,
1661 lp_servicename(snum),
1662 current_user_info.unix_name,
1664 current_user.ut.gid,
1665 get_current_username(),
1666 current_user_info.domain,
1673 * Make sure that the background queue process exists.
1674 * Otherwise just do the update ourselves
1677 if ( force || background_lpq_updater_pid == -1 ) {
1678 DEBUG(4,("print_queue_update: updating queue [%s] myself\n", sharename));
1679 current_printif = get_printer_fns( snum );
1680 print_queue_update_with_lock( sharename, current_printif, lpqcommand, lprmcommand );
1685 type = lp_printing(snum);
1687 /* get the length */
1689 len = tdb_pack( NULL, 0, "fdPP",
1695 buffer = SMB_XMALLOC_ARRAY( uint8, len );
1697 /* now pack the buffer */
1698 newlen = tdb_pack( buffer, len, "fdPP",
1704 SMB_ASSERT( newlen == len );
1706 DEBUG(10,("print_queue_update: Sending message -> printer = %s, "
1707 "type = %d, lpq command = [%s] lprm command = [%s]\n",
1708 sharename, type, lpqcommand, lprmcommand ));
1710 /* here we set a msg pending record for other smbd processes
1711 to throttle the number of duplicate print_queue_update msgs
1714 pdb = get_print_db_byname(sharename);
1720 snprintf(key, sizeof(key), "MSG_PENDING/%s", sharename);
1722 if ( !tdb_store_uint32( pdb->tdb, key, time(NULL) ) ) {
1723 /* log a message but continue on */
1725 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1729 release_print_db( pdb );
1731 /* finally send the message */
1733 messaging_send_buf(msg_ctx, pid_to_procid(background_lpq_updater_pid),
1734 MSG_PRINTER_UPDATE, (uint8 *)buffer, len);
1736 SAFE_FREE( buffer );
1741 /****************************************************************************
1742 Create/Update an entry in the print tdb that will allow us to send notify
1743 updates only to interested smbd's.
1744 ****************************************************************************/
1746 bool print_notify_register_pid(int snum)
1749 struct tdb_print_db *pdb = NULL;
1750 TDB_CONTEXT *tdb = NULL;
1751 const char *printername;
1752 uint32 mypid = (uint32)sys_getpid();
1756 /* if (snum == -1), then the change notify request was
1757 on a print server handle and we need to register on
1762 int num_services = lp_numservices();
1765 for ( idx=0; idx<num_services; idx++ ) {
1766 if (lp_snum_ok(idx) && lp_print_ok(idx) )
1767 print_notify_register_pid(idx);
1772 else /* register for a specific printer */
1774 printername = lp_const_servicename(snum);
1775 pdb = get_print_db_byname(printername);
1781 if (tdb_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1782 DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n",
1785 release_print_db(pdb);
1789 data = get_printer_notify_pid_list( tdb, printername, True );
1791 /* Add ourselves and increase the refcount. */
1793 for (i = 0; i < data.dsize; i += 8) {
1794 if (IVAL(data.dptr,i) == mypid) {
1795 uint32 new_refcount = IVAL(data.dptr, i+4) + 1;
1796 SIVAL(data.dptr, i+4, new_refcount);
1801 if (i == data.dsize) {
1802 /* We weren't in the list. Realloc. */
1803 data.dptr = (uint8 *)SMB_REALLOC(data.dptr, data.dsize + 8);
1805 DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n",
1810 SIVAL(data.dptr,data.dsize - 8,mypid);
1811 SIVAL(data.dptr,data.dsize - 4,1); /* Refcount. */
1814 /* Store back the record. */
1815 if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1816 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1817 list for printer %s\n", printername));
1825 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1827 release_print_db(pdb);
1828 SAFE_FREE(data.dptr);
1832 /****************************************************************************
1833 Update an entry in the print tdb that will allow us to send notify
1834 updates only to interested smbd's.
1835 ****************************************************************************/
1837 bool print_notify_deregister_pid(int snum)
1840 struct tdb_print_db *pdb = NULL;
1841 TDB_CONTEXT *tdb = NULL;
1842 const char *printername;
1843 uint32 mypid = (uint32)sys_getpid();
1847 /* if ( snum == -1 ), we are deregister a print server handle
1848 which means to deregister on all print queues */
1852 int num_services = lp_numservices();
1855 for ( idx=0; idx<num_services; idx++ ) {
1856 if ( lp_snum_ok(idx) && lp_print_ok(idx) )
1857 print_notify_deregister_pid(idx);
1862 else /* deregister a specific printer */
1864 printername = lp_const_servicename(snum);
1865 pdb = get_print_db_byname(printername);
1871 if (tdb_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1872 DEBUG(0,("print_notify_register_pid: Failed to lock \
1873 printer %s database\n", printername));
1875 release_print_db(pdb);
1879 data = get_printer_notify_pid_list( tdb, printername, True );
1881 /* Reduce refcount. Remove ourselves if zero. */
1883 for (i = 0; i < data.dsize; ) {
1884 if (IVAL(data.dptr,i) == mypid) {
1885 uint32 refcount = IVAL(data.dptr, i+4);
1889 if (refcount == 0) {
1890 if (data.dsize - i > 8)
1891 memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
1895 SIVAL(data.dptr, i+4, refcount);
1901 if (data.dsize == 0)
1902 SAFE_FREE(data.dptr);
1904 /* Store back the record. */
1905 if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1906 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1907 list for printer %s\n", printername));
1915 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1917 release_print_db(pdb);
1918 SAFE_FREE(data.dptr);
1922 /****************************************************************************
1923 Check if a jobid is valid. It is valid if it exists in the database.
1924 ****************************************************************************/
1926 bool print_job_exists(const char* sharename, uint32 jobid)
1928 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1934 ret = tdb_exists(pdb->tdb, print_key(jobid, &tmp));
1935 release_print_db(pdb);
1939 /****************************************************************************
1940 Give the filename used for a jobid.
1941 Only valid for the process doing the spooling and when the job
1942 has not been spooled.
1943 ****************************************************************************/
1945 char *print_job_fname(const char* sharename, uint32 jobid)
1947 struct printjob *pjob = print_job_find(sharename, jobid);
1948 if (!pjob || pjob->spooled || pjob->pid != sys_getpid())
1950 return pjob->filename;
1954 /****************************************************************************
1955 Give the filename used for a jobid.
1956 Only valid for the process doing the spooling and when the job
1957 has not been spooled.
1958 ****************************************************************************/
1960 struct spoolss_DeviceMode *print_job_devmode(const char* sharename, uint32 jobid)
1962 struct printjob *pjob = print_job_find(sharename, jobid);
1967 return pjob->devmode;
1970 /****************************************************************************
1971 Set the name of a job. Only possible for owner.
1972 ****************************************************************************/
1974 bool print_job_set_name(const char *sharename, uint32 jobid, const char *name)
1976 struct printjob *pjob;
1978 pjob = print_job_find(sharename, jobid);
1979 if (!pjob || pjob->pid != sys_getpid())
1982 fstrcpy(pjob->jobname, name);
1983 return pjob_store(server_event_context(), server_messaging_context(),
1984 sharename, jobid, pjob);
1987 /****************************************************************************
1988 Get the name of a job. Only possible for owner.
1989 ****************************************************************************/
1991 bool print_job_get_name(TALLOC_CTX *mem_ctx, const char *sharename, uint32_t jobid, char **name)
1993 struct printjob *pjob;
1995 pjob = print_job_find(sharename, jobid);
1996 if (!pjob || pjob->pid != sys_getpid()) {
2000 *name = talloc_strdup(mem_ctx, pjob->jobname);
2009 /***************************************************************************
2010 Remove a jobid from the 'jobs changed' list.
2011 ***************************************************************************/
2013 static bool remove_from_jobs_changed(const char* sharename, uint32 jobid)
2015 struct tdb_print_db *pdb = get_print_db_byname(sharename);
2017 size_t job_count, i;
2019 bool gotlock = False;
2027 key = string_tdb_data("INFO/jobs_changed");
2029 if (tdb_chainlock_with_timeout(pdb->tdb, key, 5) == -1)
2034 data = tdb_fetch(pdb->tdb, key);
2036 if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0))
2039 job_count = data.dsize / 4;
2040 for (i = 0; i < job_count; i++) {
2043 ch_jobid = IVAL(data.dptr, i*4);
2044 if (ch_jobid == jobid) {
2045 if (i < job_count -1 )
2046 memmove(data.dptr + (i*4), data.dptr + (i*4) + 4, (job_count - i - 1)*4 );
2048 if (tdb_store(pdb->tdb, key, data, TDB_REPLACE) == -1)
2058 tdb_chainunlock(pdb->tdb, key);
2059 SAFE_FREE(data.dptr);
2060 release_print_db(pdb);
2062 DEBUG(10,("remove_from_jobs_changed: removed jobid %u\n", (unsigned int)jobid ));
2064 DEBUG(10,("remove_from_jobs_changed: Failed to remove jobid %u\n", (unsigned int)jobid ));
2068 /****************************************************************************
2069 Delete a print job - don't update queue.
2070 ****************************************************************************/
2072 static bool print_job_delete1(int snum, uint32 jobid)
2074 const char* sharename = lp_const_servicename(snum);
2075 struct printjob *pjob = print_job_find(sharename, jobid);
2077 struct printif *current_printif = get_printer_fns( snum );
2083 * If already deleting just return.
2086 if (pjob->status == LPQ_DELETING)
2089 /* Hrm - we need to be able to cope with deleting a job before it
2090 has reached the spooler. Just mark it as LPQ_DELETING and
2091 let the print_queue_update() code rmeove the record */
2094 if (pjob->sysjob == -1) {
2095 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid));
2098 /* Set the tdb entry to be deleting. */
2100 pjob->status = LPQ_DELETING;
2101 pjob_store(server_event_context(), server_messaging_context(),
2102 sharename, jobid, pjob);
2104 if (pjob->spooled && pjob->sysjob != -1)
2106 result = (*(current_printif->job_delete))(
2107 lp_printername(snum),
2108 lp_lprmcommand(snum),
2111 /* Delete the tdb entry if the delete succeeded or the job hasn't
2115 struct tdb_print_db *pdb = get_print_db_byname(sharename);
2120 pjob_delete(server_event_context(),
2121 server_messaging_context(),
2123 /* Ensure we keep a rough count of the number of total jobs... */
2124 tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, -1);
2125 release_print_db(pdb);
2129 remove_from_jobs_changed( sharename, jobid );
2131 return (result == 0);
2134 /****************************************************************************
2135 Return true if the current user owns the print job.
2136 ****************************************************************************/
2138 static bool is_owner(struct auth_serversupplied_info *server_info,
2139 const char *servicename,
2142 struct printjob *pjob = print_job_find(servicename, jobid);
2144 if (!pjob || !server_info)
2147 return strequal(pjob->user, server_info->sanitized_username);
2150 /****************************************************************************
2152 ****************************************************************************/
2154 WERROR print_job_delete(struct auth_serversupplied_info *server_info,
2155 struct messaging_context *msg_ctx,
2156 int snum, uint32_t jobid)
2158 const char* sharename = lp_const_servicename(snum);
2159 struct printjob *pjob;
2163 owner = is_owner(server_info, lp_const_servicename(snum), jobid);
2165 /* Check access against security descriptor or whether the user
2169 !print_access_check(server_info, msg_ctx, snum,
2170 JOB_ACCESS_ADMINISTER)) {
2171 DEBUG(3, ("delete denied by security descriptor\n"));
2173 /* BEGIN_ADMIN_LOG */
2174 sys_adminlog( LOG_ERR,
2175 "Permission denied-- user not allowed to delete, \
2176 pause, or resume print job. User name: %s. Printer name: %s.",
2177 uidtoname(server_info->utok.uid),
2178 lp_printername(snum) );
2181 return WERR_ACCESS_DENIED;
2185 * get the spooled filename of the print job
2186 * if this works, then the file has not been spooled
2187 * to the underlying print system. Just delete the
2188 * spool file & return.
2191 fname = print_job_fname(sharename, jobid);
2192 if (fname != NULL) {
2193 /* remove the spool file */
2194 DEBUG(10, ("print_job_delete: "
2195 "Removing spool file [%s]\n", fname));
2196 if (unlink(fname) == -1) {
2197 return map_werror_from_unix(errno);
2201 if (!print_job_delete1(snum, jobid)) {
2202 return WERR_ACCESS_DENIED;
2205 /* force update the database and say the delete failed if the
2208 print_queue_update(msg_ctx, snum, True);
2210 pjob = print_job_find(sharename, jobid);
2211 if (pjob && (pjob->status != LPQ_DELETING)) {
2212 return WERR_ACCESS_DENIED;
2215 return WERR_PRINTER_HAS_JOBS_QUEUED;
2218 /****************************************************************************
2220 ****************************************************************************/
2222 bool print_job_pause(struct auth_serversupplied_info *server_info,
2223 struct messaging_context *msg_ctx,
2224 int snum, uint32 jobid, WERROR *errcode)
2226 const char* sharename = lp_const_servicename(snum);
2227 struct printjob *pjob;
2229 struct printif *current_printif = get_printer_fns( snum );
2231 pjob = print_job_find(sharename, jobid);
2233 if (!pjob || !server_info) {
2234 DEBUG(10, ("print_job_pause: no pjob or user for jobid %u\n",
2235 (unsigned int)jobid ));
2239 if (!pjob->spooled || pjob->sysjob == -1) {
2240 DEBUG(10, ("print_job_pause: not spooled or bad sysjob = %d for jobid %u\n",
2241 (int)pjob->sysjob, (unsigned int)jobid ));
2245 if (!is_owner(server_info, lp_const_servicename(snum), jobid) &&
2246 !print_access_check(server_info, msg_ctx, snum,
2247 JOB_ACCESS_ADMINISTER)) {
2248 DEBUG(3, ("pause denied by security descriptor\n"));
2250 /* BEGIN_ADMIN_LOG */
2251 sys_adminlog( LOG_ERR,
2252 "Permission denied-- user not allowed to delete, \
2253 pause, or resume print job. User name: %s. Printer name: %s.",
2254 uidtoname(server_info->utok.uid),
2255 lp_printername(snum) );
2258 *errcode = WERR_ACCESS_DENIED;
2262 /* need to pause the spooled entry */
2263 ret = (*(current_printif->job_pause))(snum, pjob);
2266 *errcode = WERR_INVALID_PARAM;
2270 /* force update the database */
2271 print_cache_flush(lp_const_servicename(snum));
2273 /* Send a printer notify message */
2275 notify_job_status(server_event_context(), msg_ctx, sharename, jobid,
2278 /* how do we tell if this succeeded? */
2283 /****************************************************************************
2285 ****************************************************************************/
2287 bool print_job_resume(struct auth_serversupplied_info *server_info,
2288 struct messaging_context *msg_ctx,
2289 int snum, uint32 jobid, WERROR *errcode)
2291 const char *sharename = lp_const_servicename(snum);
2292 struct printjob *pjob;
2294 struct printif *current_printif = get_printer_fns( snum );
2296 pjob = print_job_find(sharename, jobid);
2298 if (!pjob || !server_info) {
2299 DEBUG(10, ("print_job_resume: no pjob or user for jobid %u\n",
2300 (unsigned int)jobid ));
2304 if (!pjob->spooled || pjob->sysjob == -1) {
2305 DEBUG(10, ("print_job_resume: not spooled or bad sysjob = %d for jobid %u\n",
2306 (int)pjob->sysjob, (unsigned int)jobid ));
2310 if (!is_owner(server_info, lp_const_servicename(snum), jobid) &&
2311 !print_access_check(server_info, msg_ctx, snum,
2312 JOB_ACCESS_ADMINISTER)) {
2313 DEBUG(3, ("resume denied by security descriptor\n"));
2314 *errcode = WERR_ACCESS_DENIED;
2316 /* BEGIN_ADMIN_LOG */
2317 sys_adminlog( LOG_ERR,
2318 "Permission denied-- user not allowed to delete, \
2319 pause, or resume print job. User name: %s. Printer name: %s.",
2320 uidtoname(server_info->utok.uid),
2321 lp_printername(snum) );
2326 ret = (*(current_printif->job_resume))(snum, pjob);
2329 *errcode = WERR_INVALID_PARAM;
2333 /* force update the database */
2334 print_cache_flush(lp_const_servicename(snum));
2336 /* Send a printer notify message */
2338 notify_job_status(server_event_context(), msg_ctx, sharename, jobid,
2344 /****************************************************************************
2345 Write to a print file.
2346 ****************************************************************************/
2348 ssize_t print_job_write(int snum, uint32 jobid, const char *buf, SMB_OFF_T pos, size_t size)
2350 const char* sharename = lp_const_servicename(snum);
2351 ssize_t return_code;
2352 struct printjob *pjob;
2354 pjob = print_job_find(sharename, jobid);
2358 /* don't allow another process to get this info - it is meaningless */
2359 if (pjob->pid != sys_getpid())
2362 /* if SMBD is spooling this can't be allowed */
2363 if (pjob->status == PJOB_SMBD_SPOOLING) {
2367 return_code = write_data_at_offset(pjob->fd, buf, size, pos);
2369 if (return_code>0) {
2371 pjob_store(server_event_context(), server_messaging_context(),
2372 sharename, jobid, pjob);
2377 /****************************************************************************
2378 Get the queue status - do not update if db is out of date.
2379 ****************************************************************************/
2381 static int get_queue_status(const char* sharename, print_status_struct *status)
2385 struct tdb_print_db *pdb = get_print_db_byname(sharename);
2389 ZERO_STRUCTP(status);
2396 fstr_sprintf(keystr, "STATUS/%s", sharename);
2397 data = tdb_fetch(pdb->tdb, string_tdb_data(keystr));
2399 if (data.dsize == sizeof(print_status_struct))
2400 /* this memcpy is ok since the status struct was
2401 not packed before storing it in the tdb */
2402 memcpy(status, data.dptr, sizeof(print_status_struct));
2403 SAFE_FREE(data.dptr);
2406 len = tdb_fetch_int32(pdb->tdb, "INFO/total_jobs");
2407 release_print_db(pdb);
2408 return (len == -1 ? 0 : len);
2411 /****************************************************************************
2412 Determine the number of jobs in a queue.
2413 ****************************************************************************/
2415 int print_queue_length(struct messaging_context *msg_ctx, int snum,
2416 print_status_struct *pstatus)
2418 const char* sharename = lp_const_servicename( snum );
2419 print_status_struct status;
2422 ZERO_STRUCT( status );
2424 /* make sure the database is up to date */
2425 if (print_cache_expired(lp_const_servicename(snum), True))
2426 print_queue_update(msg_ctx, snum, False);
2428 /* also fetch the queue status */
2429 memset(&status, 0, sizeof(status));
2430 len = get_queue_status(sharename, &status);
2438 /***************************************************************************
2439 Allocate a jobid. Hold the lock for as short a time as possible.
2440 ***************************************************************************/
2442 static WERROR allocate_print_jobid(struct tdb_print_db *pdb, int snum,
2443 const char *sharename, uint32 *pjobid)
2447 enum TDB_ERROR terr;
2450 *pjobid = (uint32)-1;
2452 for (i = 0; i < 3; i++) {
2453 /* Lock the database - only wait 20 seconds. */
2454 ret = tdb_lock_bystring_with_timeout(pdb->tdb,
2455 "INFO/nextjob", 20);
2457 DEBUG(0, ("allocate_print_jobid: "
2458 "Failed to lock printing database %s\n",
2460 terr = tdb_error(pdb->tdb);
2461 return ntstatus_to_werror(map_nt_error_from_tdb(terr));
2464 if (!tdb_fetch_uint32(pdb->tdb, "INFO/nextjob", &jobid)) {
2465 terr = tdb_error(pdb->tdb);
2466 if (terr != TDB_ERR_NOEXIST) {
2467 DEBUG(0, ("allocate_print_jobid: "
2468 "Failed to fetch INFO/nextjob "
2469 "for print queue %s\n", sharename));
2470 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
2471 return ntstatus_to_werror(map_nt_error_from_tdb(terr));
2473 DEBUG(10, ("allocate_print_jobid: "
2474 "No existing jobid in %s\n", sharename));
2478 DEBUG(10, ("allocate_print_jobid: "
2479 "Read jobid %u from %s\n", jobid, sharename));
2481 jobid = NEXT_JOBID(jobid);
2483 ret = tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid);
2485 terr = tdb_error(pdb->tdb);
2486 DEBUG(3, ("allocate_print_jobid: "
2487 "Failed to store INFO/nextjob.\n"));
2488 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
2489 return ntstatus_to_werror(map_nt_error_from_tdb(terr));
2492 /* We've finished with the INFO/nextjob lock. */
2493 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
2495 if (!print_job_exists(sharename, jobid)) {
2498 DEBUG(10, ("allocate_print_jobid: "
2499 "Found jobid %u in %s\n", jobid, sharename));
2503 DEBUG(0, ("allocate_print_jobid: "
2504 "Failed to allocate a print job for queue %s\n",
2506 /* Probably full... */
2507 return WERR_NO_SPOOL_SPACE;
2510 /* Store a dummy placeholder. */
2516 if (tdb_store(pdb->tdb, print_key(jobid, &tmp), dum,
2517 TDB_INSERT) == -1) {
2518 DEBUG(3, ("allocate_print_jobid: "
2519 "jobid (%d) failed to store placeholder.\n",
2521 terr = tdb_error(pdb->tdb);
2522 return ntstatus_to_werror(map_nt_error_from_tdb(terr));
2530 /***************************************************************************
2531 Append a jobid to the 'jobs changed' list.
2532 ***************************************************************************/
2534 static bool add_to_jobs_changed(struct tdb_print_db *pdb, uint32 jobid)
2539 SIVAL(&store_jobid, 0, jobid);
2540 data.dptr = (uint8 *)&store_jobid;
2543 DEBUG(10,("add_to_jobs_changed: Added jobid %u\n", (unsigned int)jobid ));
2545 return (tdb_append(pdb->tdb, string_tdb_data("INFO/jobs_changed"),
2550 /***************************************************************************
2551 Do all checks needed to determine if we can start a job.
2552 ***************************************************************************/
2554 static WERROR print_job_checks(struct auth_serversupplied_info *server_info,
2555 struct messaging_context *msg_ctx,
2556 int snum, int *njobs)
2558 const char *sharename = lp_const_servicename(snum);
2559 uint64_t dspace, dsize;
2563 if (!print_access_check(server_info, msg_ctx, snum,
2564 PRINTER_ACCESS_USE)) {
2565 DEBUG(3, ("print_job_checks: "
2566 "job start denied by security descriptor\n"));
2567 return WERR_ACCESS_DENIED;
2570 if (!print_time_access_check(server_info, msg_ctx, sharename)) {
2571 DEBUG(3, ("print_job_checks: "
2572 "job start denied by time check\n"));
2573 return WERR_ACCESS_DENIED;
2576 /* see if we have sufficient disk space */
2577 if (lp_minprintspace(snum)) {
2578 minspace = lp_minprintspace(snum);
2579 ret = sys_fsusage(lp_pathname(snum), &dspace, &dsize);
2580 if (ret == 0 && dspace < 2*minspace) {
2581 DEBUG(3, ("print_job_checks: "
2582 "disk space check failed.\n"));
2583 return WERR_NO_SPOOL_SPACE;
2587 /* for autoloaded printers, check that the printcap entry still exists */
2588 if (lp_autoloaded(snum) && !pcap_printername_ok(sharename)) {
2589 DEBUG(3, ("print_job_checks: printer name %s check failed.\n",
2591 return WERR_ACCESS_DENIED;
2594 /* Insure the maximum queue size is not violated */
2595 *njobs = print_queue_length(msg_ctx, snum, NULL);
2596 if (*njobs > lp_maxprintjobs(snum)) {
2597 DEBUG(3, ("print_job_checks: Queue %s number of jobs (%d) "
2598 "larger than max printjobs per queue (%d).\n",
2599 sharename, *njobs, lp_maxprintjobs(snum)));
2600 return WERR_NO_SPOOL_SPACE;
2606 /***************************************************************************
2608 ***************************************************************************/
2610 static WERROR print_job_spool_file(int snum, uint32_t jobid,
2611 const char *output_file,
2612 struct printjob *pjob)
2619 /* if this file is within the printer path, it means that smbd
2620 * is spooling it and will pass us control when it is finished.
2621 * Verify that the file name is ok, within path, and it is
2622 * already already there */
2624 path = lp_pathname(snum);
2626 if (strncmp(output_file, path, len) == 0 &&
2627 (output_file[len - 1] == '/' || output_file[len] == '/')) {
2629 /* verify path is not too long */
2630 if (strlen(output_file) >= sizeof(pjob->filename)) {
2631 return WERR_INVALID_NAME;
2634 /* verify that the file exists */
2635 if (sys_stat(output_file, &st, false) != 0) {
2636 return WERR_INVALID_NAME;
2639 fstrcpy(pjob->filename, output_file);
2641 DEBUG(3, ("print_job_spool_file:"
2642 "External spooling activated"));
2644 /* we do not open the file until spooling is done */
2646 pjob->status = PJOB_SMBD_SPOOLING;
2652 slprintf(pjob->filename, sizeof(pjob->filename)-1,
2653 "%s/%s%.8u.XXXXXX", lp_pathname(snum),
2654 PRINT_SPOOL_PREFIX, (unsigned int)jobid);
2655 pjob->fd = mkstemp(pjob->filename);
2657 if (pjob->fd == -1) {
2658 werr = map_werror_from_unix(errno);
2659 if (W_ERROR_EQUAL(werr, WERR_ACCESS_DENIED)) {
2660 /* Common setup error, force a report. */
2661 DEBUG(0, ("print_job_spool_file: "
2662 "insufficient permissions to open spool "
2663 "file %s.\n", pjob->filename));
2665 /* Normal case, report at level 3 and above. */
2666 DEBUG(3, ("print_job_spool_file: "
2667 "can't open spool file %s\n",
2676 /***************************************************************************
2677 Start spooling a job - return the jobid.
2678 ***************************************************************************/
2680 WERROR print_job_start(struct auth_serversupplied_info *server_info,
2681 struct messaging_context *msg_ctx,
2682 int snum, const char *docname, const char *filename,
2683 struct spoolss_DeviceMode *devmode, uint32_t *_jobid)
2687 struct printjob pjob;
2688 const char *sharename = lp_const_servicename(snum);
2689 struct tdb_print_db *pdb = get_print_db_byname(sharename);
2694 return WERR_INTERNAL_DB_CORRUPTION;
2697 path = lp_pathname(snum);
2699 werr = print_job_checks(server_info, msg_ctx, snum, &njobs);
2700 if (!W_ERROR_IS_OK(werr)) {
2701 release_print_db(pdb);
2705 DEBUG(10, ("print_job_start: "
2706 "Queue %s number of jobs (%d), max printjobs = %d\n",
2707 sharename, njobs, lp_maxprintjobs(snum)));
2709 werr = allocate_print_jobid(pdb, snum, sharename, &jobid);
2710 if (!W_ERROR_IS_OK(werr)) {
2714 /* create the database entry */
2718 pjob.pid = sys_getpid();
2721 pjob.starttime = time(NULL);
2722 pjob.status = LPQ_SPOOLING;
2724 pjob.spooled = False;
2726 pjob.devmode = devmode;
2728 fstrcpy(pjob.jobname, docname);
2730 fstrcpy(pjob.user, lp_printjob_username(snum));
2731 standard_sub_advanced(sharename, server_info->sanitized_username,
2732 path, server_info->utok.gid,
2733 server_info->sanitized_username,
2734 server_info->info3->base.domain.string,
2735 pjob.user, sizeof(pjob.user)-1);
2736 /* ensure NULL termination */
2737 pjob.user[sizeof(pjob.user)-1] = '\0';
2739 fstrcpy(pjob.queuename, lp_const_servicename(snum));
2741 /* we have a job entry - now create the spool file */
2742 werr = print_job_spool_file(snum, jobid, filename, &pjob);
2743 if (!W_ERROR_IS_OK(werr)) {
2747 pjob_store(server_event_context(), msg_ctx, sharename, jobid, &pjob);
2749 /* Update the 'jobs changed' entry used by print_queue_status. */
2750 add_to_jobs_changed(pdb, jobid);
2752 /* Ensure we keep a rough count of the number of total jobs... */
2753 tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, 1);
2755 release_print_db(pdb);
2762 pjob_delete(server_event_context(), msg_ctx, sharename, jobid);
2765 release_print_db(pdb);
2767 DEBUG(3, ("print_job_start: returning fail. "
2768 "Error = %s\n", win_errstr(werr)));
2772 /****************************************************************************
2773 Update the number of pages spooled to jobid
2774 ****************************************************************************/
2776 void print_job_endpage(int snum, uint32 jobid)
2778 const char* sharename = lp_const_servicename(snum);
2779 struct printjob *pjob;
2781 pjob = print_job_find(sharename, jobid);
2784 /* don't allow another process to get this info - it is meaningless */
2785 if (pjob->pid != sys_getpid())
2789 pjob_store(server_event_context(), server_messaging_context(),
2790 sharename, jobid, pjob);
2793 /****************************************************************************
2794 Print a file - called on closing the file. This spools the job.
2795 If normal close is false then we're tearing down the jobs - treat as an
2797 ****************************************************************************/
2799 NTSTATUS print_job_end(struct messaging_context *msg_ctx, int snum,
2800 uint32 jobid, enum file_close_type close_type)
2802 const char* sharename = lp_const_servicename(snum);
2803 struct printjob *pjob;
2805 SMB_STRUCT_STAT sbuf;
2806 struct printif *current_printif = get_printer_fns( snum );
2807 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2809 pjob = print_job_find(sharename, jobid);
2812 return NT_STATUS_PRINT_CANCELLED;
2815 if (pjob->spooled || pjob->pid != sys_getpid()) {
2816 return NT_STATUS_ACCESS_DENIED;
2819 if (close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) {
2820 if (pjob->status == PJOB_SMBD_SPOOLING) {
2821 /* take over the file now, smbd is done */
2822 if (sys_stat(pjob->filename, &sbuf, false) != 0) {
2823 status = map_nt_error_from_unix(errno);
2824 DEBUG(3, ("print_job_end: "
2825 "stat file failed for jobid %d\n",
2830 pjob->status = LPQ_SPOOLING;
2834 if ((sys_fstat(pjob->fd, &sbuf, false) != 0)) {
2835 status = map_nt_error_from_unix(errno);
2837 DEBUG(3, ("print_job_end: "
2838 "stat file failed for jobid %d\n",
2846 pjob->size = sbuf.st_ex_size;
2850 * Not a normal close, something has gone wrong. Cleanup.
2852 if (pjob->fd != -1) {
2858 /* Technically, this is not quite right. If the printer has a separator
2859 * page turned on, the NT spooler prints the separator page even if the
2860 * print job is 0 bytes. 010215 JRR */
2861 if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
2862 /* don't bother spooling empty files or something being deleted. */
2863 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
2864 pjob->filename, pjob->size ? "deleted" : "zero length" ));
2865 unlink(pjob->filename);
2866 pjob_delete(server_event_context(), msg_ctx, sharename, jobid);
2867 return NT_STATUS_OK;
2870 ret = (*(current_printif->job_submit))(snum, pjob);
2873 status = NT_STATUS_PRINT_CANCELLED;
2877 /* The print job has been successfully handed over to the back-end */
2879 pjob->spooled = True;
2880 pjob->status = LPQ_QUEUED;
2881 pjob_store(server_event_context(), msg_ctx, sharename, jobid, pjob);
2883 /* make sure the database is up to date */
2884 if (print_cache_expired(lp_const_servicename(snum), True))
2885 print_queue_update(msg_ctx, snum, False);
2887 return NT_STATUS_OK;
2891 /* The print job was not successfully started. Cleanup */
2892 /* Still need to add proper error return propagation! 010122:JRR */
2894 unlink(pjob->filename);
2895 pjob_delete(server_event_context(), msg_ctx, sharename, jobid);
2899 /****************************************************************************
2900 Get a snapshot of jobs in the system without traversing.
2901 ****************************************************************************/
2903 static bool get_stored_queue_info(struct messaging_context *msg_ctx,
2904 struct tdb_print_db *pdb, int snum,
2905 int *pcount, print_queue_struct **ppqueue)
2907 TDB_DATA data, cgdata;
2908 print_queue_struct *queue = NULL;
2910 uint32 extra_count = 0;
2911 int total_count = 0;
2914 int max_reported_jobs = lp_max_reported_jobs(snum);
2916 const char* sharename = lp_servicename(snum);
2918 /* make sure the database is up to date */
2919 if (print_cache_expired(lp_const_servicename(snum), True))
2920 print_queue_update(msg_ctx, snum, False);
2926 ZERO_STRUCT(cgdata);
2928 /* Get the stored queue data. */
2929 data = tdb_fetch(pdb->tdb, string_tdb_data("INFO/linear_queue_array"));
2931 if (data.dptr && data.dsize >= sizeof(qcount))
2932 len += tdb_unpack(data.dptr + len, data.dsize - len, "d", &qcount);
2934 /* Get the changed jobs list. */
2935 cgdata = tdb_fetch(pdb->tdb, string_tdb_data("INFO/jobs_changed"));
2936 if (cgdata.dptr != NULL && (cgdata.dsize % 4 == 0))
2937 extra_count = cgdata.dsize/4;
2939 DEBUG(5,("get_stored_queue_info: qcount = %u, extra_count = %u\n", (unsigned int)qcount, (unsigned int)extra_count));
2941 /* Allocate the queue size. */
2942 if (qcount == 0 && extra_count == 0)
2945 if ((queue = SMB_MALLOC_ARRAY(print_queue_struct, qcount + extra_count)) == NULL)
2948 /* Retrieve the linearised queue data. */
2950 for( i = 0; i < qcount; i++) {
2951 uint32 qjob, qsize, qpage_count, qstatus, qpriority, qtime;
2952 len += tdb_unpack(data.dptr + len, data.dsize - len, "ddddddff",
2961 queue[i].job = qjob;
2962 queue[i].size = qsize;
2963 queue[i].page_count = qpage_count;
2964 queue[i].status = qstatus;
2965 queue[i].priority = qpriority;
2966 queue[i].time = qtime;
2969 total_count = qcount;
2971 /* Add in the changed jobids. */
2972 for( i = 0; i < extra_count; i++) {
2974 struct printjob *pjob;
2976 jobid = IVAL(cgdata.dptr, i*4);
2977 DEBUG(5,("get_stored_queue_info: changed job = %u\n", (unsigned int)jobid));
2978 pjob = print_job_find(lp_const_servicename(snum), jobid);
2980 DEBUG(5,("get_stored_queue_info: failed to find changed job = %u\n", (unsigned int)jobid));
2981 remove_from_jobs_changed(sharename, jobid);
2985 queue[total_count].job = jobid;
2986 queue[total_count].size = pjob->size;
2987 queue[total_count].page_count = pjob->page_count;
2988 queue[total_count].status = pjob->status;
2989 queue[total_count].priority = 1;
2990 queue[total_count].time = pjob->starttime;
2991 fstrcpy(queue[total_count].fs_user, pjob->user);
2992 fstrcpy(queue[total_count].fs_file, pjob->jobname);
2996 /* Sort the queue by submission time otherwise they are displayed
2999 TYPESAFE_QSORT(queue, total_count, printjob_comp);
3001 DEBUG(5,("get_stored_queue_info: total_count = %u\n", (unsigned int)total_count));
3003 if (max_reported_jobs && total_count > max_reported_jobs)
3004 total_count = max_reported_jobs;
3007 *pcount = total_count;
3013 SAFE_FREE(data.dptr);
3014 SAFE_FREE(cgdata.dptr);
3018 /****************************************************************************
3019 Get a printer queue listing.
3020 set queue = NULL and status = NULL if you just want to update the cache
3021 ****************************************************************************/
3023 int print_queue_status(struct messaging_context *msg_ctx, int snum,
3024 print_queue_struct **ppqueue,
3025 print_status_struct *status)
3029 const char *sharename;
3030 struct tdb_print_db *pdb;
3033 /* make sure the database is up to date */
3035 if (print_cache_expired(lp_const_servicename(snum), True))
3036 print_queue_update(msg_ctx, snum, False);
3038 /* return if we are done */
3039 if ( !ppqueue || !status )
3043 sharename = lp_const_servicename(snum);
3044 pdb = get_print_db_byname(sharename);
3050 * Fetch the queue status. We must do this first, as there may
3051 * be no jobs in the queue.
3054 ZERO_STRUCTP(status);
3055 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", sharename);
3056 key = string_tdb_data(keystr);
3058 data = tdb_fetch(pdb->tdb, key);
3060 if (data.dsize == sizeof(*status)) {
3061 /* this memcpy is ok since the status struct was
3062 not packed before storing it in the tdb */
3063 memcpy(status, data.dptr, sizeof(*status));
3065 SAFE_FREE(data.dptr);
3069 * Now, fetch the print queue information. We first count the number
3070 * of entries, and then only retrieve the queue if necessary.
3073 if (!get_stored_queue_info(msg_ctx, pdb, snum, &count, ppqueue)) {
3074 release_print_db(pdb);
3078 release_print_db(pdb);
3082 /****************************************************************************
3084 ****************************************************************************/
3086 WERROR print_queue_pause(struct auth_serversupplied_info *server_info,
3087 struct messaging_context *msg_ctx, int snum)
3090 struct printif *current_printif = get_printer_fns( snum );
3092 if (!print_access_check(server_info, msg_ctx, snum,
3093 PRINTER_ACCESS_ADMINISTER)) {
3094 return WERR_ACCESS_DENIED;
3100 ret = (*(current_printif->queue_pause))(snum);
3105 return WERR_INVALID_PARAM;
3108 /* force update the database */
3109 print_cache_flush(lp_const_servicename(snum));
3111 /* Send a printer notify message */
3113 notify_printer_status(server_event_context(), msg_ctx, snum,
3114 PRINTER_STATUS_PAUSED);
3119 /****************************************************************************
3121 ****************************************************************************/
3123 WERROR print_queue_resume(struct auth_serversupplied_info *server_info,
3124 struct messaging_context *msg_ctx, int snum)
3127 struct printif *current_printif = get_printer_fns( snum );
3129 if (!print_access_check(server_info, msg_ctx, snum,
3130 PRINTER_ACCESS_ADMINISTER)) {
3131 return WERR_ACCESS_DENIED;
3136 ret = (*(current_printif->queue_resume))(snum);
3141 return WERR_INVALID_PARAM;
3144 /* make sure the database is up to date */
3145 if (print_cache_expired(lp_const_servicename(snum), True))
3146 print_queue_update(msg_ctx, snum, True);
3148 /* Send a printer notify message */
3150 notify_printer_status(server_event_context(), msg_ctx, snum,
3156 /****************************************************************************
3157 Purge a queue - implemented by deleting all jobs that we can delete.
3158 ****************************************************************************/
3160 WERROR print_queue_purge(struct auth_serversupplied_info *server_info,
3161 struct messaging_context *msg_ctx, int snum)
3163 print_queue_struct *queue;
3164 print_status_struct status;
3168 /* Force and update so the count is accurate (i.e. not a cached count) */
3169 print_queue_update(msg_ctx, snum, True);
3171 can_job_admin = print_access_check(server_info,
3174 JOB_ACCESS_ADMINISTER);
3175 njobs = print_queue_status(msg_ctx, snum, &queue, &status);
3177 if ( can_job_admin )
3180 for (i=0;i<njobs;i++) {
3181 bool owner = is_owner(server_info, lp_const_servicename(snum),
3184 if (owner || can_job_admin) {
3185 print_job_delete1(snum, queue[i].job);
3189 if ( can_job_admin )
3192 /* update the cache */
3193 print_queue_update(msg_ctx, snum, True);