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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 extern SIG_ATOMIC_T got_sig_term;
27 extern SIG_ATOMIC_T reload_after_sighup;
28 extern struct current_user current_user;
29 extern userdom_struct current_user_info;
31 /* Current printer interface */
32 static BOOL remove_from_jobs_changed(const char* sharename, uint32 jobid);
35 the printing backend revolves around a tdb database that stores the
36 SMB view of the print queue
38 The key for this database is a jobid - a internally generated number that
39 uniquely identifies a print job
41 reading the print queue involves two steps:
42 - possibly running lpq and updating the internal database from that
43 - reading entries from the database
45 jobids are assigned when a job starts spooling.
48 static TDB_CONTEXT *rap_tdb;
49 static uint16 next_rap_jobid;
50 struct rap_jobid_key {
55 /***************************************************************************
56 Nightmare. LANMAN jobid's are 16 bit numbers..... We must map them to 32
57 bit RPC jobids.... JRA.
58 ***************************************************************************/
60 uint16 pjobid_to_rap(const char* sharename, uint32 jobid)
64 struct rap_jobid_key jinfo;
67 DEBUG(10,("pjobid_to_rap: called.\n"));
70 /* Create the in-memory tdb. */
71 rap_tdb = tdb_open_log(NULL, 0, TDB_INTERNAL, (O_RDWR|O_CREAT), 0644);
77 fstrcpy( jinfo.sharename, sharename );
79 key.dptr = (uint8 *)&jinfo;
80 key.dsize = sizeof(jinfo);
82 data = tdb_fetch(rap_tdb, key);
83 if (data.dptr && data.dsize == sizeof(uint16)) {
84 rap_jobid = SVAL(data.dptr, 0);
86 DEBUG(10,("pjobid_to_rap: jobid %u maps to RAP jobid %u\n",
87 (unsigned int)jobid, (unsigned int)rap_jobid));
91 /* Not found - create and store mapping. */
92 rap_jobid = ++next_rap_jobid;
94 rap_jobid = ++next_rap_jobid;
95 SSVAL(buf,0,rap_jobid);
97 data.dsize = sizeof(rap_jobid);
98 tdb_store(rap_tdb, key, data, TDB_REPLACE);
99 tdb_store(rap_tdb, data, key, TDB_REPLACE);
101 DEBUG(10,("pjobid_to_rap: created jobid %u maps to RAP jobid %u\n",
102 (unsigned int)jobid, (unsigned int)rap_jobid));
106 BOOL rap_to_pjobid(uint16 rap_jobid, fstring sharename, uint32 *pjobid)
111 DEBUG(10,("rap_to_pjobid called.\n"));
116 SSVAL(buf,0,rap_jobid);
118 key.dsize = sizeof(rap_jobid);
119 data = tdb_fetch(rap_tdb, key);
120 if ( data.dptr && data.dsize == sizeof(struct rap_jobid_key) )
122 struct rap_jobid_key *jinfo = (struct rap_jobid_key*)data.dptr;
123 fstrcpy( sharename, jinfo->sharename );
124 *pjobid = jinfo->jobid;
125 DEBUG(10,("rap_to_pjobid: jobid %u maps to RAP jobid %u\n",
126 (unsigned int)*pjobid, (unsigned int)rap_jobid));
127 SAFE_FREE(data.dptr);
131 DEBUG(10,("rap_to_pjobid: Failed to lookup RAP jobid %u\n",
132 (unsigned int)rap_jobid));
133 SAFE_FREE(data.dptr);
137 static void rap_jobid_delete(const char* sharename, uint32 jobid)
141 struct rap_jobid_key jinfo;
144 DEBUG(10,("rap_jobid_delete: called.\n"));
149 ZERO_STRUCT( jinfo );
150 fstrcpy( jinfo.sharename, sharename );
152 key.dptr = (uint8 *)&jinfo;
153 key.dsize = sizeof(jinfo);
155 data = tdb_fetch(rap_tdb, key);
156 if (!data.dptr || (data.dsize != sizeof(uint16))) {
157 DEBUG(10,("rap_jobid_delete: cannot find jobid %u\n",
158 (unsigned int)jobid ));
159 SAFE_FREE(data.dptr);
163 DEBUG(10,("rap_jobid_delete: deleting jobid %u\n",
164 (unsigned int)jobid ));
166 rap_jobid = SVAL(data.dptr, 0);
167 SAFE_FREE(data.dptr);
168 SSVAL(buf,0,rap_jobid);
170 data.dsize = sizeof(rap_jobid);
171 tdb_delete(rap_tdb, key);
172 tdb_delete(rap_tdb, data);
175 static int get_queue_status(const char* sharename, print_status_struct *);
177 /****************************************************************************
178 Initialise the printing backend. Called once at startup before the fork().
179 ****************************************************************************/
181 BOOL print_backend_init(struct messaging_context *msg_ctx)
183 const char *sversion = "INFO/version";
184 pstring printing_path;
185 int services = lp_numservices();
188 unlink(lock_path("printing.tdb"));
189 pstrcpy(printing_path,lock_path("printing"));
190 mkdir(printing_path,0755);
192 /* handle a Samba upgrade */
194 for (snum = 0; snum < services; snum++) {
195 struct tdb_print_db *pdb;
196 if (!lp_print_ok(snum))
199 pdb = get_print_db_byname(lp_const_servicename(snum));
202 if (tdb_lock_bystring(pdb->tdb, sversion) == -1) {
203 DEBUG(0,("print_backend_init: Failed to open printer %s database\n", lp_const_servicename(snum) ));
204 release_print_db(pdb);
207 if (tdb_fetch_int32(pdb->tdb, sversion) != PRINT_DATABASE_VERSION) {
208 tdb_traverse(pdb->tdb, tdb_traverse_delete_fn, NULL);
209 tdb_store_int32(pdb->tdb, sversion, PRINT_DATABASE_VERSION);
211 tdb_unlock_bystring(pdb->tdb, sversion);
212 release_print_db(pdb);
215 close_all_print_db(); /* Don't leave any open. */
217 /* do NT print initialization... */
218 return nt_printing_init(msg_ctx);
221 /****************************************************************************
222 Shut down printing backend. Called once at shutdown to close the tdb.
223 ****************************************************************************/
225 void printing_end(void)
227 close_all_print_db(); /* Don't leave any open. */
230 /****************************************************************************
231 Retrieve the set of printing functions for a given service. This allows
232 us to set the printer function table based on the value of the 'printing'
235 Use the generic interface as the default and only use cups interface only
236 when asked for (and only when supported)
237 ****************************************************************************/
239 static struct printif *get_printer_fns_from_type( enum printing_types type )
241 struct printif *printer_fns = &generic_printif;
244 if ( type == PRINT_CUPS ) {
245 printer_fns = &cups_printif;
247 #endif /* HAVE_CUPS */
250 if ( type == PRINT_IPRINT ) {
251 printer_fns = &iprint_printif;
253 #endif /* HAVE_IPRINT */
255 printer_fns->type = type;
260 static struct printif *get_printer_fns( int snum )
262 return get_printer_fns_from_type( (enum printing_types)lp_printing(snum) );
266 /****************************************************************************
267 Useful function to generate a tdb key.
268 ****************************************************************************/
270 static TDB_DATA print_key(uint32 jobid)
276 ret.dptr = (uint8 *)&j;
277 ret.dsize = sizeof(j);
281 /***********************************************************************
282 unpack a pjob from a tdb buffer
283 ***********************************************************************/
285 int unpack_pjob( uint8 *buf, int buflen, struct printjob *pjob )
289 uint32 pjpid, pjsysjob, pjfd, pjstarttime, pjstatus;
290 uint32 pjsize, pjpage_count, pjspooled, pjsmbjob;
295 len += tdb_unpack(buf+len, buflen-len, "dddddddddffff",
313 if ( (used = unpack_devicemode(&pjob->nt_devmode, buf+len, buflen-len)) == -1 )
319 pjob->sysjob = pjsysjob;
321 pjob->starttime = pjstarttime;
322 pjob->status = pjstatus;
324 pjob->page_count = pjpage_count;
325 pjob->spooled = pjspooled;
326 pjob->smbjob = pjsmbjob;
332 /****************************************************************************
333 Useful function to find a print job in the database.
334 ****************************************************************************/
336 static struct printjob *print_job_find(const char *sharename, uint32 jobid)
338 static struct printjob pjob;
340 struct tdb_print_db *pdb = get_print_db_byname(sharename);
342 DEBUG(10,("print_job_find: looking up job %u for share %s\n",
343 (unsigned int)jobid, sharename ));
349 ret = tdb_fetch(pdb->tdb, print_key(jobid));
350 release_print_db(pdb);
353 DEBUG(10,("print_job_find: failed to find jobid %u.\n", (unsigned int)jobid ));
357 if ( pjob.nt_devmode ) {
358 free_nt_devicemode( &pjob.nt_devmode );
363 if ( unpack_pjob( ret.dptr, ret.dsize, &pjob ) == -1 ) {
364 DEBUG(10,("print_job_find: failed to unpack jobid %u.\n", (unsigned int)jobid ));
371 DEBUG(10,("print_job_find: returning system job %d for jobid %u.\n",
372 (int)pjob.sysjob, (unsigned int)jobid ));
377 /* Convert a unix jobid to a smb jobid */
379 static uint32 sysjob_to_jobid_value;
381 static int unixjob_traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key,
382 TDB_DATA data, void *state)
384 struct printjob *pjob;
385 int *sysjob = (int *)state;
387 if (!data.dptr || data.dsize == 0)
390 pjob = (struct printjob *)data.dptr;
391 if (key.dsize != sizeof(uint32))
394 if (*sysjob == pjob->sysjob) {
395 uint32 jobid = IVAL(key.dptr,0);
397 sysjob_to_jobid_value = jobid;
404 /****************************************************************************
405 This is a *horribly expensive call as we have to iterate through all the
406 current printer tdb's. Don't do this often ! JRA.
407 ****************************************************************************/
409 uint32 sysjob_to_jobid(int unix_jobid)
411 int services = lp_numservices();
414 sysjob_to_jobid_value = (uint32)-1;
416 for (snum = 0; snum < services; snum++) {
417 struct tdb_print_db *pdb;
418 if (!lp_print_ok(snum))
420 pdb = get_print_db_byname(lp_const_servicename(snum));
424 tdb_traverse(pdb->tdb, unixjob_traverse_fn, &unix_jobid);
425 release_print_db(pdb);
426 if (sysjob_to_jobid_value != (uint32)-1)
427 return sysjob_to_jobid_value;
432 /****************************************************************************
433 Send notifications based on what has changed after a pjob_store.
434 ****************************************************************************/
438 uint32 spoolss_status;
439 } lpq_to_spoolss_status_map[] = {
440 { LPQ_QUEUED, JOB_STATUS_QUEUED },
441 { LPQ_PAUSED, JOB_STATUS_PAUSED },
442 { LPQ_SPOOLING, JOB_STATUS_SPOOLING },
443 { LPQ_PRINTING, JOB_STATUS_PRINTING },
444 { LPQ_DELETING, JOB_STATUS_DELETING },
445 { LPQ_OFFLINE, JOB_STATUS_OFFLINE },
446 { LPQ_PAPEROUT, JOB_STATUS_PAPEROUT },
447 { LPQ_PRINTED, JOB_STATUS_PRINTED },
448 { LPQ_DELETED, JOB_STATUS_DELETED },
449 { LPQ_BLOCKED, JOB_STATUS_BLOCKED },
450 { LPQ_USER_INTERVENTION, JOB_STATUS_USER_INTERVENTION },
454 /* Convert a lpq status value stored in printing.tdb into the
455 appropriate win32 API constant. */
457 static uint32 map_to_spoolss_status(uint32 lpq_status)
461 while (lpq_to_spoolss_status_map[i].lpq_status != -1) {
462 if (lpq_to_spoolss_status_map[i].lpq_status == lpq_status)
463 return lpq_to_spoolss_status_map[i].spoolss_status;
470 static void pjob_store_notify(const char* sharename, uint32 jobid, struct printjob *old_data,
471 struct printjob *new_data)
473 BOOL new_job = False;
478 /* Job attributes that can't be changed. We only send
479 notification for these on a new job. */
481 /* ACHTUNG! Due to a bug in Samba's spoolss parsing of the
482 NOTIFY_INFO_DATA buffer, we *have* to send the job submission
483 time first or else we'll end up with potential alignment
484 errors. I don't think the systemtime should be spooled as
485 a string, but this gets us around that error.
486 --jerry (i'll feel dirty for this) */
489 notify_job_submitted(sharename, jobid, new_data->starttime);
490 notify_job_username(sharename, jobid, new_data->user);
493 if (new_job || !strequal(old_data->jobname, new_data->jobname))
494 notify_job_name(sharename, jobid, new_data->jobname);
496 /* Job attributes of a new job or attributes that can be
499 if (new_job || !strequal(old_data->jobname, new_data->jobname))
500 notify_job_name(sharename, jobid, new_data->jobname);
502 if (new_job || old_data->status != new_data->status)
503 notify_job_status(sharename, jobid, map_to_spoolss_status(new_data->status));
505 if (new_job || old_data->size != new_data->size)
506 notify_job_total_bytes(sharename, jobid, new_data->size);
508 if (new_job || old_data->page_count != new_data->page_count)
509 notify_job_total_pages(sharename, jobid, new_data->page_count);
512 /****************************************************************************
513 Store a job structure back to the database.
514 ****************************************************************************/
516 static BOOL pjob_store(const char* sharename, uint32 jobid, struct printjob *pjob)
518 TDB_DATA old_data, new_data;
520 struct tdb_print_db *pdb = get_print_db_byname(sharename);
522 int len, newlen, buflen;
530 old_data = tdb_fetch(pdb->tdb, print_key(jobid));
532 /* Doh! Now we have to pack/unpack data since the NT_DEVICEMODE was added */
539 len += tdb_pack(buf+len, buflen-len, "dddddddddffff",
541 (uint32)pjob->sysjob,
543 (uint32)pjob->starttime,
544 (uint32)pjob->status,
546 (uint32)pjob->page_count,
547 (uint32)pjob->spooled,
548 (uint32)pjob->smbjob,
554 len += pack_devicemode(pjob->nt_devmode, buf+len, buflen-len);
557 buf = (uint8 *)SMB_REALLOC(buf, len);
559 DEBUG(0,("pjob_store: failed to enlarge buffer!\n"));
564 } while ( buflen != len );
570 new_data.dsize = len;
571 ret = (tdb_store(pdb->tdb, print_key(jobid), new_data, TDB_REPLACE) == 0);
573 release_print_db(pdb);
575 /* Send notify updates for what has changed */
578 struct printjob old_pjob;
580 if ( old_data.dsize )
582 if ( unpack_pjob( old_data.dptr, old_data.dsize, &old_pjob ) != -1 )
584 pjob_store_notify( sharename, jobid, &old_pjob , pjob );
585 free_nt_devicemode( &old_pjob.nt_devmode );
590 pjob_store_notify( sharename, jobid, NULL, pjob );
595 SAFE_FREE( old_data.dptr );
601 /****************************************************************************
602 Remove a job structure from the database.
603 ****************************************************************************/
605 void pjob_delete(const char* sharename, uint32 jobid)
607 struct printjob *pjob;
608 uint32 job_status = 0;
609 struct tdb_print_db *pdb;
611 pdb = get_print_db_byname( sharename );
616 pjob = print_job_find( sharename, jobid );
619 DEBUG(5, ("pjob_delete: we were asked to delete nonexistent job %u\n",
620 (unsigned int)jobid));
621 release_print_db(pdb);
625 /* We must cycle through JOB_STATUS_DELETING and
626 JOB_STATUS_DELETED for the port monitor to delete the job
629 job_status = JOB_STATUS_DELETING|JOB_STATUS_DELETED;
630 notify_job_status(sharename, jobid, job_status);
632 /* Remove from printing.tdb */
634 tdb_delete(pdb->tdb, print_key(jobid));
635 remove_from_jobs_changed(sharename, jobid);
636 release_print_db( pdb );
637 rap_jobid_delete(sharename, jobid);
640 /****************************************************************************
641 Parse a file name from the system spooler to generate a jobid.
642 ****************************************************************************/
644 static uint32 print_parse_jobid(char *fname)
648 if (strncmp(fname,PRINT_SPOOL_PREFIX,strlen(PRINT_SPOOL_PREFIX)) != 0)
650 fname += strlen(PRINT_SPOOL_PREFIX);
656 return (uint32)jobid;
659 /****************************************************************************
660 List a unix job in the print database.
661 ****************************************************************************/
663 static void print_unix_job(const char *sharename, print_queue_struct *q, uint32 jobid)
665 struct printjob pj, *old_pj;
667 if (jobid == (uint32)-1)
668 jobid = q->job + UNIX_JOB_START;
670 /* Preserve the timestamp on an existing unix print job */
672 old_pj = print_job_find(sharename, jobid);
679 pj.starttime = old_pj ? old_pj->starttime : q->time;
680 pj.status = q->status;
683 fstrcpy(pj.filename, old_pj ? old_pj->filename : "");
684 if (jobid < UNIX_JOB_START) {
686 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : "Remote Downlevel Document");
689 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : q->fs_file);
691 fstrcpy(pj.user, old_pj ? old_pj->user : q->fs_user);
692 fstrcpy(pj.queuename, old_pj ? old_pj->queuename : sharename );
694 pjob_store(sharename, jobid, &pj);
698 struct traverse_struct {
699 print_queue_struct *queue;
700 int qcount, snum, maxcount, total_jobs;
701 const char *sharename;
703 const char *lprm_command;
704 struct printif *print_if;
707 /****************************************************************************
708 Utility fn to delete any jobs that are no longer active.
709 ****************************************************************************/
711 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
713 struct traverse_struct *ts = (struct traverse_struct *)state;
714 struct printjob pjob;
718 if ( key.dsize != sizeof(jobid) )
721 jobid = IVAL(key.dptr, 0);
722 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
724 free_nt_devicemode( &pjob.nt_devmode );
728 /* remove a unix job if it isn't in the system queue any more */
730 for (i=0;i<ts->qcount;i++) {
731 uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
732 if (jobid == u_jobid)
735 if (i == ts->qcount) {
736 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !smbjob\n",
737 (unsigned int)jobid ));
738 pjob_delete(ts->sharename, jobid);
742 /* need to continue the the bottom of the function to
743 save the correct attributes */
746 /* maybe it hasn't been spooled yet */
748 /* if a job is not spooled and the process doesn't
749 exist then kill it. This cleans up after smbd
751 if (!process_exists_by_pid(pjob.pid)) {
752 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !process_exists (%u)\n",
753 (unsigned int)jobid, (unsigned int)pjob.pid ));
754 pjob_delete(ts->sharename, jobid);
760 /* this check only makes sense for jobs submitted from Windows clients */
763 for (i=0;i<ts->qcount;i++) {
766 if ( pjob.status == LPQ_DELETED )
769 curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
771 if (jobid == curr_jobid) {
773 /* try to clean up any jobs that need to be deleted */
775 if ( pjob.status == LPQ_DELETING ) {
778 result = (*(ts->print_if->job_delete))(
779 ts->sharename, ts->lprm_command, &pjob );
782 /* if we can't delete, then reset the job status */
783 pjob.status = LPQ_QUEUED;
784 pjob_store(ts->sharename, jobid, &pjob);
787 /* if we deleted the job, the remove the tdb record */
788 pjob_delete(ts->sharename, jobid);
789 pjob.status = LPQ_DELETED;
799 /* The job isn't in the system queue - we have to assume it has
800 completed, so delete the database entry. */
802 if (i == ts->qcount) {
804 /* A race can occur between the time a job is spooled and
805 when it appears in the lpq output. This happens when
806 the job is added to printing.tdb when another smbd
807 running print_queue_update() has completed a lpq and
808 is currently traversing the printing tdb and deleting jobs.
809 Don't delete the job if it was submitted after the lpq_time. */
811 if (pjob.starttime < ts->lpq_time) {
812 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to pjob.starttime (%u) < ts->lpq_time (%u)\n",
814 (unsigned int)pjob.starttime,
815 (unsigned int)ts->lpq_time ));
816 pjob_delete(ts->sharename, jobid);
822 /* Save the pjob attributes we will store.
823 FIXME!!! This is the only place where queue->job
824 represents the SMB jobid --jerry */
826 ts->queue[i].job = jobid;
827 ts->queue[i].size = pjob.size;
828 ts->queue[i].page_count = pjob.page_count;
829 ts->queue[i].status = pjob.status;
830 ts->queue[i].priority = 1;
831 ts->queue[i].time = pjob.starttime;
832 fstrcpy(ts->queue[i].fs_user, pjob.user);
833 fstrcpy(ts->queue[i].fs_file, pjob.jobname);
840 /****************************************************************************
841 Check if the print queue has been updated recently enough.
842 ****************************************************************************/
844 static void print_cache_flush(const char *sharename)
847 struct tdb_print_db *pdb = get_print_db_byname(sharename);
851 slprintf(key, sizeof(key)-1, "CACHE/%s", sharename);
852 tdb_store_int32(pdb->tdb, key, -1);
853 release_print_db(pdb);
856 /****************************************************************************
857 Check if someone already thinks they are doing the update.
858 ****************************************************************************/
860 static pid_t get_updating_pid(const char *sharename)
865 struct tdb_print_db *pdb = get_print_db_byname(sharename);
869 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", sharename);
870 key = string_tdb_data(keystr);
872 data = tdb_fetch(pdb->tdb, key);
873 release_print_db(pdb);
874 if (!data.dptr || data.dsize != sizeof(pid_t)) {
875 SAFE_FREE(data.dptr);
879 updating_pid = IVAL(data.dptr, 0);
880 SAFE_FREE(data.dptr);
882 if (process_exists_by_pid(updating_pid))
888 /****************************************************************************
889 Set the fact that we're doing the update, or have finished doing the update
891 ****************************************************************************/
893 static void set_updating_pid(const fstring sharename, BOOL updating)
898 pid_t updating_pid = sys_getpid();
901 struct tdb_print_db *pdb = get_print_db_byname(sharename);
906 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", sharename);
907 key = string_tdb_data(keystr);
909 DEBUG(5, ("set_updating_pid: %s updating lpq cache for print share %s\n",
910 updating ? "" : "not ",
914 tdb_delete(pdb->tdb, key);
915 release_print_db(pdb);
919 SIVAL( buffer, 0, updating_pid);
921 data.dsize = 4; /* we always assume this is a 4 byte value */
923 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
924 release_print_db(pdb);
927 /****************************************************************************
928 Sort print jobs by submittal time.
929 ****************************************************************************/
931 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
942 /* Sort on job start time */
944 if (j1->time == j2->time)
946 return (j1->time > j2->time) ? 1 : -1;
949 /****************************************************************************
950 Store the sorted queue representation for later portmon retrieval.
952 ****************************************************************************/
954 static void store_queue_struct(struct tdb_print_db *pdb, struct traverse_struct *pts)
957 int max_reported_jobs = lp_max_reported_jobs(pts->snum);
958 print_queue_struct *queue = pts->queue;
963 if (max_reported_jobs && (max_reported_jobs < pts->qcount))
964 pts->qcount = max_reported_jobs;
967 /* Work out the size. */
969 data.dsize += tdb_pack(NULL, 0, "d", qcount);
971 for (i = 0; i < pts->qcount; i++) {
972 if ( queue[i].status == LPQ_DELETED )
976 data.dsize += tdb_pack(NULL, 0, "ddddddff",
977 (uint32)queue[i].job,
978 (uint32)queue[i].size,
979 (uint32)queue[i].page_count,
980 (uint32)queue[i].status,
981 (uint32)queue[i].priority,
982 (uint32)queue[i].time,
987 if ((data.dptr = (uint8 *)SMB_MALLOC(data.dsize)) == NULL)
991 len += tdb_pack(data.dptr + len, data.dsize - len, "d", qcount);
992 for (i = 0; i < pts->qcount; i++) {
993 if ( queue[i].status == LPQ_DELETED )
996 len += tdb_pack(data.dptr + len, data.dsize - len, "ddddddff",
997 (uint32)queue[i].job,
998 (uint32)queue[i].size,
999 (uint32)queue[i].page_count,
1000 (uint32)queue[i].status,
1001 (uint32)queue[i].priority,
1002 (uint32)queue[i].time,
1007 tdb_store(pdb->tdb, string_tdb_data("INFO/linear_queue_array"), data,
1009 SAFE_FREE(data.dptr);
1013 static TDB_DATA get_jobs_changed_data(struct tdb_print_db *pdb)
1019 data = tdb_fetch(pdb->tdb, string_tdb_data("INFO/jobs_changed"));
1020 if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0)) {
1021 SAFE_FREE(data.dptr);
1028 static void check_job_changed(const char *sharename, TDB_DATA data, uint32 jobid)
1031 unsigned int job_count = data.dsize / 4;
1033 for (i = 0; i < job_count; i++) {
1036 ch_jobid = IVAL(data.dptr, i*4);
1037 if (ch_jobid == jobid)
1038 remove_from_jobs_changed(sharename, jobid);
1042 /****************************************************************************
1043 Check if the print queue has been updated recently enough.
1044 ****************************************************************************/
1046 static BOOL print_cache_expired(const char *sharename, BOOL check_pending)
1049 time_t last_qscan_time, time_now = time(NULL);
1050 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1051 BOOL result = False;
1056 snprintf(key, sizeof(key), "CACHE/%s", sharename);
1057 last_qscan_time = (time_t)tdb_fetch_int32(pdb->tdb, key);
1060 * Invalidate the queue for 3 reasons.
1061 * (1). last queue scan time == -1.
1062 * (2). Current time - last queue scan time > allowed cache time.
1063 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1064 * This last test picks up machines for which the clock has been moved
1065 * forward, an lpq scan done and then the clock moved back. Otherwise
1066 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1069 if (last_qscan_time == ((time_t)-1)
1070 || (time_now - last_qscan_time) >= lp_lpqcachetime()
1071 || last_qscan_time > (time_now + MAX_CACHE_VALID_TIME))
1074 time_t msg_pending_time;
1076 DEBUG(4, ("print_cache_expired: cache expired for queue %s "
1077 "(last_qscan_time = %d, time now = %d, qcachetime = %d)\n",
1078 sharename, (int)last_qscan_time, (int)time_now,
1079 (int)lp_lpqcachetime() ));
1081 /* check if another smbd has already sent a message to update the
1082 queue. Give the pending message one minute to clear and
1083 then send another message anyways. Make sure to check for
1084 clocks that have been run forward and then back again. */
1086 snprintf(key, sizeof(key), "MSG_PENDING/%s", sharename);
1089 && tdb_fetch_uint32( pdb->tdb, key, &u )
1090 && (msg_pending_time=u) > 0
1091 && msg_pending_time <= time_now
1092 && (time_now - msg_pending_time) < 60 )
1094 DEBUG(4,("print_cache_expired: message already pending for %s. Accepting cache\n",
1103 release_print_db(pdb);
1107 /****************************************************************************
1108 main work for updating the lpq cahe for a printer queue
1109 ****************************************************************************/
1111 static void print_queue_update_internal( const char *sharename,
1112 struct printif *current_printif,
1113 char *lpq_command, char *lprm_command )
1116 print_queue_struct *queue = NULL;
1117 print_status_struct status;
1118 print_status_struct old_status;
1119 struct printjob *pjob;
1120 struct traverse_struct tstruct;
1123 fstring keystr, cachestr;
1124 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1130 DEBUG(5,("print_queue_update_internal: printer = %s, type = %d, lpq command = [%s]\n",
1131 sharename, current_printif->type, lpq_command));
1134 * Update the cache time FIRST ! Stops others even
1135 * attempting to get the lock and doing this
1136 * if the lpq takes a long time.
1139 slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", sharename);
1140 tdb_store_int32(pdb->tdb, cachestr, (int)time(NULL));
1142 /* get the current queue using the appropriate interface */
1143 ZERO_STRUCT(status);
1145 qcount = (*(current_printif->queue_get))(sharename,
1146 current_printif->type,
1147 lpq_command, &queue, &status);
1149 DEBUG(3, ("print_queue_update_internal: %d job%s in queue for %s\n",
1150 qcount, (qcount != 1) ? "s" : "", sharename));
1152 /* Sort the queue by submission time otherwise they are displayed
1155 qsort(queue, qcount, sizeof(print_queue_struct),
1156 QSORT_CAST(printjob_comp));
1159 any job in the internal database that is marked as spooled
1160 and doesn't exist in the system queue is considered finished
1161 and removed from the database
1163 any job in the system database but not in the internal database
1164 is added as a unix job
1166 fill in any system job numbers as we go
1169 jcdata = get_jobs_changed_data(pdb);
1171 for (i=0; i<qcount; i++) {
1172 uint32 jobid = print_parse_jobid(queue[i].fs_file);
1174 if (jobid == (uint32)-1) {
1175 /* assume its a unix print job */
1176 print_unix_job(sharename, &queue[i], jobid);
1180 /* we have an active SMB print job - update its status */
1181 pjob = print_job_find(sharename, jobid);
1183 /* err, somethings wrong. Probably smbd was restarted
1184 with jobs in the queue. All we can do is treat them
1185 like unix jobs. Pity. */
1186 print_unix_job(sharename, &queue[i], jobid);
1190 pjob->sysjob = queue[i].job;
1192 /* don't reset the status on jobs to be deleted */
1194 if ( pjob->status != LPQ_DELETING )
1195 pjob->status = queue[i].status;
1197 pjob_store(sharename, jobid, pjob);
1199 check_job_changed(sharename, jcdata, jobid);
1202 SAFE_FREE(jcdata.dptr);
1204 /* now delete any queued entries that don't appear in the
1206 tstruct.queue = queue;
1207 tstruct.qcount = qcount;
1209 tstruct.total_jobs = 0;
1210 tstruct.lpq_time = time(NULL);
1211 tstruct.sharename = sharename;
1212 tstruct.lprm_command = lprm_command;
1213 tstruct.print_if = current_printif;
1215 tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
1217 /* Store the linearised queue, max jobs only. */
1218 store_queue_struct(pdb, &tstruct);
1220 SAFE_FREE(tstruct.queue);
1222 DEBUG(10,("print_queue_update_internal: printer %s INFO/total_jobs = %d\n",
1223 sharename, tstruct.total_jobs ));
1225 tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
1227 get_queue_status(sharename, &old_status);
1228 if (old_status.qcount != qcount)
1229 DEBUG(10,("print_queue_update_internal: queue status change %d jobs -> %d jobs for printer %s\n",
1230 old_status.qcount, qcount, sharename));
1232 /* store the new queue status structure */
1233 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", sharename);
1234 key = string_tdb_data(keystr);
1236 status.qcount = qcount;
1237 data.dptr = (uint8 *)&status;
1238 data.dsize = sizeof(status);
1239 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
1242 * Update the cache time again. We want to do this call
1243 * as little as possible...
1246 slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", sharename);
1247 tdb_store_int32(pdb->tdb, keystr, (int32)time(NULL));
1249 /* clear the msg pending record for this queue */
1251 snprintf(keystr, sizeof(keystr), "MSG_PENDING/%s", sharename);
1253 if ( !tdb_store_uint32( pdb->tdb, keystr, 0 ) ) {
1254 /* log a message but continue on */
1256 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1260 release_print_db( pdb );
1265 /****************************************************************************
1266 Update the internal database from the system print queue for a queue.
1267 obtain a lock on the print queue before proceeding (needed when mutiple
1268 smbd processes maytry to update the lpq cache concurrently).
1269 ****************************************************************************/
1271 static void print_queue_update_with_lock( const char *sharename,
1272 struct printif *current_printif,
1273 char *lpq_command, char *lprm_command )
1276 struct tdb_print_db *pdb;
1278 DEBUG(5,("print_queue_update_with_lock: printer share = %s\n", sharename));
1279 pdb = get_print_db_byname(sharename);
1283 if ( !print_cache_expired(sharename, False) ) {
1284 DEBUG(5,("print_queue_update_with_lock: print cache for %s is still ok\n", sharename));
1285 release_print_db(pdb);
1290 * Check to see if someone else is doing this update.
1291 * This is essentially a mutex on the update.
1294 if (get_updating_pid(sharename) != -1) {
1295 release_print_db(pdb);
1299 /* Lock the queue for the database update */
1301 slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", sharename);
1302 /* Only wait 10 seconds for this. */
1303 if (tdb_lock_bystring_with_timeout(pdb->tdb, keystr, 10) == -1) {
1304 DEBUG(0,("print_queue_update_with_lock: Failed to lock printer %s database\n", sharename));
1305 release_print_db(pdb);
1310 * Ensure that no one else got in here.
1311 * If the updating pid is still -1 then we are
1315 if (get_updating_pid(sharename) != -1) {
1317 * Someone else is doing the update, exit.
1319 tdb_unlock_bystring(pdb->tdb, keystr);
1320 release_print_db(pdb);
1325 * We're going to do the update ourselves.
1328 /* Tell others we're doing the update. */
1329 set_updating_pid(sharename, True);
1332 * Allow others to enter and notice we're doing
1336 tdb_unlock_bystring(pdb->tdb, keystr);
1338 /* do the main work now */
1340 print_queue_update_internal( sharename, current_printif,
1341 lpq_command, lprm_command );
1343 /* Delete our pid from the db. */
1344 set_updating_pid(sharename, False);
1345 release_print_db(pdb);
1348 /****************************************************************************
1349 this is the receive function of the background lpq updater
1350 ****************************************************************************/
1351 static void print_queue_receive(struct messaging_context *msg,
1354 struct server_id server_id,
1358 pstring lpqcommand, lprmcommand;
1362 len = tdb_unpack( (uint8 *)data->data, data->length, "fdPP",
1369 DEBUG(0,("print_queue_receive: Got invalid print queue update message\n"));
1373 print_queue_update_with_lock(sharename,
1374 get_printer_fns_from_type((enum printing_types)printing_type),
1375 lpqcommand, lprmcommand );
1380 static pid_t background_lpq_updater_pid = -1;
1382 /****************************************************************************
1383 main thread of the background lpq updater
1384 ****************************************************************************/
1385 void start_background_queue(void)
1387 DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
1388 background_lpq_updater_pid = sys_fork();
1390 if (background_lpq_updater_pid == -1) {
1391 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
1395 if(background_lpq_updater_pid == 0) {
1397 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
1399 claim_connection( NULL, "smbd lpq backend",
1400 FLAG_MSG_GENERAL|FLAG_MSG_SMBD|FLAG_MSG_PRINT_GENERAL);
1402 if (!locking_init(0)) {
1406 messaging_register(smbd_messaging_context(), NULL,
1407 MSG_PRINTER_UPDATE, print_queue_receive);
1409 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
1413 /* check for some essential signals first */
1416 exit_server_cleanly(NULL);
1419 if (reload_after_sighup) {
1420 change_to_root_user();
1421 DEBUG(1,("Reloading services after SIGHUP\n"));
1422 reload_services(False);
1423 reload_after_sighup = 0;
1426 /* now check for messages */
1428 DEBUG(10,("start_background_queue: background LPQ thread got a message\n"));
1429 message_dispatch(smbd_messaging_context());
1431 /* process any pending print change notify messages */
1433 print_notify_send_messages(smbd_messaging_context(),
1439 /****************************************************************************
1440 update the internal database from the system print queue for a queue
1441 ****************************************************************************/
1443 static void print_queue_update(int snum, BOOL force)
1447 pstring lpqcommand, lprmcommand;
1448 uint8 *buffer = NULL;
1451 struct tdb_print_db *pdb;
1453 struct printif *current_printif;
1455 fstrcpy( sharename, lp_const_servicename(snum));
1457 /* don't strip out characters like '$' from the printername */
1459 pstrcpy( lpqcommand, lp_lpqcommand(snum));
1460 string_sub2( lpqcommand, "%p", PRINTERNAME(snum), sizeof(lpqcommand),
1461 False, False, False );
1462 standard_sub_advanced(lp_servicename(snum),
1463 current_user_info.unix_name, "",
1464 current_user.ut.gid,
1465 get_current_username(),
1466 current_user_info.domain,
1467 lpqcommand, sizeof(lpqcommand) );
1469 pstrcpy( lprmcommand, lp_lprmcommand(snum));
1470 string_sub2( lprmcommand, "%p", PRINTERNAME(snum), sizeof(lprmcommand),
1471 False, False, False );
1472 standard_sub_advanced(lp_servicename(snum),
1473 current_user_info.unix_name, "",
1474 current_user.ut.gid,
1475 get_current_username(),
1476 current_user_info.domain,
1477 lprmcommand, sizeof(lprmcommand) );
1480 * Make sure that the background queue process exists.
1481 * Otherwise just do the update ourselves
1484 if ( force || background_lpq_updater_pid == -1 ) {
1485 DEBUG(4,("print_queue_update: updating queue [%s] myself\n", sharename));
1486 current_printif = get_printer_fns( snum );
1487 print_queue_update_with_lock( sharename, current_printif, lpqcommand, lprmcommand );
1492 type = lp_printing(snum);
1494 /* get the length */
1496 len = tdb_pack( NULL, 0, "fdPP",
1502 buffer = SMB_XMALLOC_ARRAY( uint8, len );
1504 /* now pack the buffer */
1505 newlen = tdb_pack( buffer, len, "fdPP",
1511 SMB_ASSERT( newlen == len );
1513 DEBUG(10,("print_queue_update: Sending message -> printer = %s, "
1514 "type = %d, lpq command = [%s] lprm command = [%s]\n",
1515 sharename, type, lpqcommand, lprmcommand ));
1517 /* here we set a msg pending record for other smbd processes
1518 to throttle the number of duplicate print_queue_update msgs
1521 pdb = get_print_db_byname(sharename);
1527 snprintf(key, sizeof(key), "MSG_PENDING/%s", sharename);
1529 if ( !tdb_store_uint32( pdb->tdb, key, time(NULL) ) ) {
1530 /* log a message but continue on */
1532 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1536 release_print_db( pdb );
1538 /* finally send the message */
1540 messaging_send_buf(smbd_messaging_context(),
1541 pid_to_procid(background_lpq_updater_pid),
1542 MSG_PRINTER_UPDATE, (uint8 *)buffer, len);
1544 SAFE_FREE( buffer );
1549 /****************************************************************************
1550 Create/Update an entry in the print tdb that will allow us to send notify
1551 updates only to interested smbd's.
1552 ****************************************************************************/
1554 BOOL print_notify_register_pid(int snum)
1557 struct tdb_print_db *pdb = NULL;
1558 TDB_CONTEXT *tdb = NULL;
1559 const char *printername;
1560 uint32 mypid = (uint32)sys_getpid();
1564 /* if (snum == -1), then the change notify request was
1565 on a print server handle and we need to register on
1570 int num_services = lp_numservices();
1573 for ( idx=0; idx<num_services; idx++ ) {
1574 if (lp_snum_ok(idx) && lp_print_ok(idx) )
1575 print_notify_register_pid(idx);
1580 else /* register for a specific printer */
1582 printername = lp_const_servicename(snum);
1583 pdb = get_print_db_byname(printername);
1589 if (tdb_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1590 DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n",
1593 release_print_db(pdb);
1597 data = get_printer_notify_pid_list( tdb, printername, True );
1599 /* Add ourselves and increase the refcount. */
1601 for (i = 0; i < data.dsize; i += 8) {
1602 if (IVAL(data.dptr,i) == mypid) {
1603 uint32 new_refcount = IVAL(data.dptr, i+4) + 1;
1604 SIVAL(data.dptr, i+4, new_refcount);
1609 if (i == data.dsize) {
1610 /* We weren't in the list. Realloc. */
1611 data.dptr = (uint8 *)SMB_REALLOC(data.dptr, data.dsize + 8);
1613 DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n",
1618 SIVAL(data.dptr,data.dsize - 8,mypid);
1619 SIVAL(data.dptr,data.dsize - 4,1); /* Refcount. */
1622 /* Store back the record. */
1623 if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1624 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1625 list for printer %s\n", printername));
1633 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1635 release_print_db(pdb);
1636 SAFE_FREE(data.dptr);
1640 /****************************************************************************
1641 Update an entry in the print tdb that will allow us to send notify
1642 updates only to interested smbd's.
1643 ****************************************************************************/
1645 BOOL print_notify_deregister_pid(int snum)
1648 struct tdb_print_db *pdb = NULL;
1649 TDB_CONTEXT *tdb = NULL;
1650 const char *printername;
1651 uint32 mypid = (uint32)sys_getpid();
1655 /* if ( snum == -1 ), we are deregister a print server handle
1656 which means to deregister on all print queues */
1660 int num_services = lp_numservices();
1663 for ( idx=0; idx<num_services; idx++ ) {
1664 if ( lp_snum_ok(idx) && lp_print_ok(idx) )
1665 print_notify_deregister_pid(idx);
1670 else /* deregister a specific printer */
1672 printername = lp_const_servicename(snum);
1673 pdb = get_print_db_byname(printername);
1679 if (tdb_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1680 DEBUG(0,("print_notify_register_pid: Failed to lock \
1681 printer %s database\n", printername));
1683 release_print_db(pdb);
1687 data = get_printer_notify_pid_list( tdb, printername, True );
1689 /* Reduce refcount. Remove ourselves if zero. */
1691 for (i = 0; i < data.dsize; ) {
1692 if (IVAL(data.dptr,i) == mypid) {
1693 uint32 refcount = IVAL(data.dptr, i+4);
1697 if (refcount == 0) {
1698 if (data.dsize - i > 8)
1699 memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
1703 SIVAL(data.dptr, i+4, refcount);
1709 if (data.dsize == 0)
1710 SAFE_FREE(data.dptr);
1712 /* Store back the record. */
1713 if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1714 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1715 list for printer %s\n", printername));
1723 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1725 release_print_db(pdb);
1726 SAFE_FREE(data.dptr);
1730 /****************************************************************************
1731 Check if a jobid is valid. It is valid if it exists in the database.
1732 ****************************************************************************/
1734 BOOL print_job_exists(const char* sharename, uint32 jobid)
1736 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1741 ret = tdb_exists(pdb->tdb, print_key(jobid));
1742 release_print_db(pdb);
1746 /****************************************************************************
1747 Give the fd used for a jobid.
1748 ****************************************************************************/
1750 int print_job_fd(const char* sharename, uint32 jobid)
1752 struct printjob *pjob = print_job_find(sharename, jobid);
1755 /* don't allow another process to get this info - it is meaningless */
1756 if (pjob->pid != sys_getpid())
1761 /****************************************************************************
1762 Give the filename used for a jobid.
1763 Only valid for the process doing the spooling and when the job
1764 has not been spooled.
1765 ****************************************************************************/
1767 char *print_job_fname(const char* sharename, uint32 jobid)
1769 struct printjob *pjob = print_job_find(sharename, jobid);
1770 if (!pjob || pjob->spooled || pjob->pid != sys_getpid())
1772 return pjob->filename;
1776 /****************************************************************************
1777 Give the filename used for a jobid.
1778 Only valid for the process doing the spooling and when the job
1779 has not been spooled.
1780 ****************************************************************************/
1782 NT_DEVICEMODE *print_job_devmode(const char* sharename, uint32 jobid)
1784 struct printjob *pjob = print_job_find(sharename, jobid);
1789 return pjob->nt_devmode;
1792 /****************************************************************************
1793 Set the place in the queue for a job.
1794 ****************************************************************************/
1796 BOOL print_job_set_place(const char *sharename, uint32 jobid, int place)
1798 DEBUG(2,("print_job_set_place not implemented yet\n"));
1802 /****************************************************************************
1803 Set the name of a job. Only possible for owner.
1804 ****************************************************************************/
1806 BOOL print_job_set_name(const char *sharename, uint32 jobid, char *name)
1808 struct printjob *pjob;
1810 pjob = print_job_find(sharename, jobid);
1811 if (!pjob || pjob->pid != sys_getpid())
1814 fstrcpy(pjob->jobname, name);
1815 return pjob_store(sharename, jobid, pjob);
1818 /***************************************************************************
1819 Remove a jobid from the 'jobs changed' list.
1820 ***************************************************************************/
1822 static BOOL remove_from_jobs_changed(const char* sharename, uint32 jobid)
1824 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1826 size_t job_count, i;
1828 BOOL gotlock = False;
1836 key = string_tdb_data("INFO/jobs_changed");
1838 if (tdb_chainlock_with_timeout(pdb->tdb, key, 5) == -1)
1843 data = tdb_fetch(pdb->tdb, key);
1845 if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0))
1848 job_count = data.dsize / 4;
1849 for (i = 0; i < job_count; i++) {
1852 ch_jobid = IVAL(data.dptr, i*4);
1853 if (ch_jobid == jobid) {
1854 if (i < job_count -1 )
1855 memmove(data.dptr + (i*4), data.dptr + (i*4) + 4, (job_count - i - 1)*4 );
1857 if (tdb_store(pdb->tdb, key, data, TDB_REPLACE) == -1)
1867 tdb_chainunlock(pdb->tdb, key);
1868 SAFE_FREE(data.dptr);
1869 release_print_db(pdb);
1871 DEBUG(10,("remove_from_jobs_changed: removed jobid %u\n", (unsigned int)jobid ));
1873 DEBUG(10,("remove_from_jobs_changed: Failed to remove jobid %u\n", (unsigned int)jobid ));
1877 /****************************************************************************
1878 Delete a print job - don't update queue.
1879 ****************************************************************************/
1881 static BOOL print_job_delete1(int snum, uint32 jobid)
1883 const char* sharename = lp_const_servicename(snum);
1884 struct printjob *pjob = print_job_find(sharename, jobid);
1886 struct printif *current_printif = get_printer_fns( snum );
1892 * If already deleting just return.
1895 if (pjob->status == LPQ_DELETING)
1898 /* Hrm - we need to be able to cope with deleting a job before it
1899 has reached the spooler. Just mark it as LPQ_DELETING and
1900 let the print_queue_update() code rmeove the record */
1903 if (pjob->sysjob == -1) {
1904 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid));
1907 /* Set the tdb entry to be deleting. */
1909 pjob->status = LPQ_DELETING;
1910 pjob_store(sharename, jobid, pjob);
1912 if (pjob->spooled && pjob->sysjob != -1)
1914 result = (*(current_printif->job_delete))(
1916 lp_lprmcommand(snum),
1919 /* Delete the tdb entry if the delete succeeded or the job hasn't
1923 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1928 pjob_delete(sharename, jobid);
1929 /* Ensure we keep a rough count of the number of total jobs... */
1930 tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, -1);
1931 release_print_db(pdb);
1935 remove_from_jobs_changed( sharename, jobid );
1937 return (result == 0);
1940 /****************************************************************************
1941 Return true if the current user owns the print job.
1942 ****************************************************************************/
1944 static BOOL is_owner(struct current_user *user, const char *servicename,
1947 struct printjob *pjob = print_job_find(servicename, jobid);
1953 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1954 return strequal(pjob->user, vuser->user.smb_name);
1956 return strequal(pjob->user, uidtoname(user->ut.uid));
1960 /****************************************************************************
1962 ****************************************************************************/
1964 BOOL print_job_delete(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1966 const char* sharename = lp_const_servicename( snum );
1967 struct printjob *pjob;
1973 owner = is_owner(user, lp_const_servicename(snum), jobid);
1975 /* Check access against security descriptor or whether the user
1979 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1980 DEBUG(3, ("delete denied by security descriptor\n"));
1981 *errcode = WERR_ACCESS_DENIED;
1983 /* BEGIN_ADMIN_LOG */
1984 sys_adminlog( LOG_ERR,
1985 "Permission denied-- user not allowed to delete, \
1986 pause, or resume print job. User name: %s. Printer name: %s.",
1987 uidtoname(user->ut.uid), PRINTERNAME(snum) );
1994 * get the spooled filename of the print job
1995 * if this works, then the file has not been spooled
1996 * to the underlying print system. Just delete the
1997 * spool file & return.
2000 if ( (fname = print_job_fname( sharename, jobid )) != NULL )
2002 /* remove the spool file */
2003 DEBUG(10,("print_job_delete: Removing spool file [%s]\n", fname ));
2004 if ( unlink( fname ) == -1 ) {
2005 *errcode = map_werror_from_unix(errno);
2010 if (!print_job_delete1(snum, jobid)) {
2011 *errcode = WERR_ACCESS_DENIED;
2015 /* force update the database and say the delete failed if the
2018 print_queue_update(snum, True);
2020 pjob = print_job_find(sharename, jobid);
2021 if ( pjob && (pjob->status != LPQ_DELETING) )
2022 *errcode = WERR_ACCESS_DENIED;
2024 return (pjob == NULL );
2027 /****************************************************************************
2029 ****************************************************************************/
2031 BOOL print_job_pause(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
2033 const char* sharename = lp_const_servicename(snum);
2034 struct printjob *pjob;
2036 struct printif *current_printif = get_printer_fns( snum );
2038 pjob = print_job_find(sharename, jobid);
2040 if (!pjob || !user) {
2041 DEBUG(10, ("print_job_pause: no pjob or user for jobid %u\n",
2042 (unsigned int)jobid ));
2046 if (!pjob->spooled || pjob->sysjob == -1) {
2047 DEBUG(10, ("print_job_pause: not spooled or bad sysjob = %d for jobid %u\n",
2048 (int)pjob->sysjob, (unsigned int)jobid ));
2052 if (!is_owner(user, lp_const_servicename(snum), jobid) &&
2053 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
2054 DEBUG(3, ("pause denied by security descriptor\n"));
2056 /* BEGIN_ADMIN_LOG */
2057 sys_adminlog( LOG_ERR,
2058 "Permission denied-- user not allowed to delete, \
2059 pause, or resume print job. User name: %s. Printer name: %s.",
2060 uidtoname(user->ut.uid), PRINTERNAME(snum) );
2063 *errcode = WERR_ACCESS_DENIED;
2067 /* need to pause the spooled entry */
2068 ret = (*(current_printif->job_pause))(snum, pjob);
2071 *errcode = WERR_INVALID_PARAM;
2075 /* force update the database */
2076 print_cache_flush(lp_const_servicename(snum));
2078 /* Send a printer notify message */
2080 notify_job_status(sharename, jobid, JOB_STATUS_PAUSED);
2082 /* how do we tell if this succeeded? */
2087 /****************************************************************************
2089 ****************************************************************************/
2091 BOOL print_job_resume(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
2093 const char *sharename = lp_const_servicename(snum);
2094 struct printjob *pjob;
2096 struct printif *current_printif = get_printer_fns( snum );
2098 pjob = print_job_find(sharename, jobid);
2100 if (!pjob || !user) {
2101 DEBUG(10, ("print_job_resume: no pjob or user for jobid %u\n",
2102 (unsigned int)jobid ));
2106 if (!pjob->spooled || pjob->sysjob == -1) {
2107 DEBUG(10, ("print_job_resume: not spooled or bad sysjob = %d for jobid %u\n",
2108 (int)pjob->sysjob, (unsigned int)jobid ));
2112 if (!is_owner(user, lp_const_servicename(snum), jobid) &&
2113 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
2114 DEBUG(3, ("resume denied by security descriptor\n"));
2115 *errcode = WERR_ACCESS_DENIED;
2117 /* BEGIN_ADMIN_LOG */
2118 sys_adminlog( LOG_ERR,
2119 "Permission denied-- user not allowed to delete, \
2120 pause, or resume print job. User name: %s. Printer name: %s.",
2121 uidtoname(user->ut.uid), PRINTERNAME(snum) );
2126 ret = (*(current_printif->job_resume))(snum, pjob);
2129 *errcode = WERR_INVALID_PARAM;
2133 /* force update the database */
2134 print_cache_flush(lp_const_servicename(snum));
2136 /* Send a printer notify message */
2138 notify_job_status(sharename, jobid, JOB_STATUS_QUEUED);
2143 /****************************************************************************
2144 Write to a print file.
2145 ****************************************************************************/
2147 ssize_t print_job_write(int snum, uint32 jobid, const char *buf, SMB_OFF_T pos, size_t size)
2149 const char* sharename = lp_const_servicename(snum);
2151 struct printjob *pjob;
2153 pjob = print_job_find(sharename, jobid);
2157 /* don't allow another process to get this info - it is meaningless */
2158 if (pjob->pid != sys_getpid())
2161 return_code = write_data_at_offset(pjob->fd, buf, size, pos);
2163 if (return_code>0) {
2165 pjob_store(sharename, jobid, pjob);
2170 /****************************************************************************
2171 Get the queue status - do not update if db is out of date.
2172 ****************************************************************************/
2174 static int get_queue_status(const char* sharename, print_status_struct *status)
2178 struct tdb_print_db *pdb = get_print_db_byname(sharename);
2182 ZERO_STRUCTP(status);
2189 fstr_sprintf(keystr, "STATUS/%s", sharename);
2190 data = tdb_fetch(pdb->tdb, string_tdb_data(keystr));
2192 if (data.dsize == sizeof(print_status_struct))
2193 /* this memcpy is ok since the status struct was
2194 not packed before storing it in the tdb */
2195 memcpy(status, data.dptr, sizeof(print_status_struct));
2196 SAFE_FREE(data.dptr);
2199 len = tdb_fetch_int32(pdb->tdb, "INFO/total_jobs");
2200 release_print_db(pdb);
2201 return (len == -1 ? 0 : len);
2204 /****************************************************************************
2205 Determine the number of jobs in a queue.
2206 ****************************************************************************/
2208 int print_queue_length(int snum, print_status_struct *pstatus)
2210 const char* sharename = lp_const_servicename( snum );
2211 print_status_struct status;
2214 ZERO_STRUCT( status );
2216 /* make sure the database is up to date */
2217 if (print_cache_expired(lp_const_servicename(snum), True))
2218 print_queue_update(snum, False);
2220 /* also fetch the queue status */
2221 memset(&status, 0, sizeof(status));
2222 len = get_queue_status(sharename, &status);
2230 /***************************************************************************
2231 Allocate a jobid. Hold the lock for as short a time as possible.
2232 ***************************************************************************/
2234 static BOOL allocate_print_jobid(struct tdb_print_db *pdb, int snum, const char *sharename, uint32 *pjobid)
2239 *pjobid = (uint32)-1;
2241 for (i = 0; i < 3; i++) {
2242 /* Lock the database - only wait 20 seconds. */
2243 if (tdb_lock_bystring_with_timeout(pdb->tdb, "INFO/nextjob", 20) == -1) {
2244 DEBUG(0,("allocate_print_jobid: failed to lock printing database %s\n", sharename));
2248 if (!tdb_fetch_uint32(pdb->tdb, "INFO/nextjob", &jobid)) {
2249 if (tdb_error(pdb->tdb) != TDB_ERR_NOEXIST) {
2250 DEBUG(0, ("allocate_print_jobid: failed to fetch INFO/nextjob for print queue %s\n",
2257 jobid = NEXT_JOBID(jobid);
2259 if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
2260 DEBUG(3, ("allocate_print_jobid: failed to store INFO/nextjob.\n"));
2261 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
2265 /* We've finished with the INFO/nextjob lock. */
2266 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
2268 if (!print_job_exists(sharename, jobid))
2273 DEBUG(0, ("allocate_print_jobid: failed to allocate a print job for queue %s\n",
2275 /* Probably full... */
2280 /* Store a dummy placeholder. */
2285 if (tdb_store(pdb->tdb, print_key(jobid), dum, TDB_INSERT) == -1) {
2286 DEBUG(3, ("allocate_print_jobid: jobid (%d) failed to store placeholder.\n",
2296 /***************************************************************************
2297 Append a jobid to the 'jobs changed' list.
2298 ***************************************************************************/
2300 static BOOL add_to_jobs_changed(struct tdb_print_db *pdb, uint32 jobid)
2305 SIVAL(&store_jobid, 0, jobid);
2306 data.dptr = (uint8 *)&store_jobid;
2309 DEBUG(10,("add_to_jobs_changed: Added jobid %u\n", (unsigned int)jobid ));
2311 return (tdb_append(pdb->tdb, string_tdb_data("INFO/jobs_changed"),
2315 /***************************************************************************
2316 Start spooling a job - return the jobid.
2317 ***************************************************************************/
2319 uint32 print_job_start(struct current_user *user, int snum, char *jobname, NT_DEVICEMODE *nt_devmode )
2323 struct printjob pjob;
2325 const char *sharename = lp_const_servicename(snum);
2326 struct tdb_print_db *pdb = get_print_db_byname(sharename);
2334 if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
2335 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
2336 release_print_db(pdb);
2340 if (!print_time_access_check(lp_servicename(snum))) {
2341 DEBUG(3, ("print_job_start: job start denied by time check\n"));
2342 release_print_db(pdb);
2346 path = lp_pathname(snum);
2348 /* see if we have sufficient disk space */
2349 if (lp_minprintspace(snum)) {
2350 SMB_BIG_UINT dspace, dsize;
2351 if (sys_fsusage(path, &dspace, &dsize) == 0 &&
2352 dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
2353 DEBUG(3, ("print_job_start: disk space check failed.\n"));
2354 release_print_db(pdb);
2360 /* for autoloaded printers, check that the printcap entry still exists */
2361 if (lp_autoloaded(snum) && !pcap_printername_ok(lp_const_servicename(snum))) {
2362 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum) ));
2363 release_print_db(pdb);
2368 /* Insure the maximum queue size is not violated */
2369 if ((njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
2370 DEBUG(3, ("print_job_start: Queue %s number of jobs (%d) larger than max printjobs per queue (%d).\n",
2371 sharename, njobs, lp_maxprintjobs(snum) ));
2372 release_print_db(pdb);
2377 DEBUG(10,("print_job_start: Queue %s number of jobs (%d), max printjobs = %d\n",
2378 sharename, njobs, lp_maxprintjobs(snum) ));
2380 if (!allocate_print_jobid(pdb, snum, sharename, &jobid))
2383 /* create the database entry */
2387 pjob.pid = sys_getpid();
2390 pjob.starttime = time(NULL);
2391 pjob.status = LPQ_SPOOLING;
2393 pjob.spooled = False;
2395 pjob.nt_devmode = nt_devmode;
2397 fstrcpy(pjob.jobname, jobname);
2399 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
2400 fstrcpy(pjob.user, lp_printjob_username(snum));
2401 standard_sub_basic(vuser->user.smb_name, vuser->user.domain,
2402 pjob.user, sizeof(pjob.user)-1);
2403 /* ensure NULL termination */
2404 pjob.user[sizeof(pjob.user)-1] = '\0';
2406 fstrcpy(pjob.user, uidtoname(user->ut.uid));
2409 fstrcpy(pjob.queuename, lp_const_servicename(snum));
2411 /* we have a job entry - now create the spool file */
2412 slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.8u.XXXXXX",
2413 path, PRINT_SPOOL_PREFIX, (unsigned int)jobid);
2414 pjob.fd = smb_mkstemp(pjob.filename);
2416 if (pjob.fd == -1) {
2417 if (errno == EACCES) {
2418 /* Common setup error, force a report. */
2419 DEBUG(0, ("print_job_start: insufficient permissions \
2420 to open spool file %s.\n", pjob.filename));
2422 /* Normal case, report at level 3 and above. */
2423 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
2424 DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
2429 pjob_store(sharename, jobid, &pjob);
2431 /* Update the 'jobs changed' entry used by print_queue_status. */
2432 add_to_jobs_changed(pdb, jobid);
2434 /* Ensure we keep a rough count of the number of total jobs... */
2435 tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, 1);
2437 release_print_db(pdb);
2443 pjob_delete(sharename, jobid);
2445 release_print_db(pdb);
2447 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
2451 /****************************************************************************
2452 Update the number of pages spooled to jobid
2453 ****************************************************************************/
2455 void print_job_endpage(int snum, uint32 jobid)
2457 const char* sharename = lp_const_servicename(snum);
2458 struct printjob *pjob;
2460 pjob = print_job_find(sharename, jobid);
2463 /* don't allow another process to get this info - it is meaningless */
2464 if (pjob->pid != sys_getpid())
2468 pjob_store(sharename, jobid, pjob);
2471 /****************************************************************************
2472 Print a file - called on closing the file. This spools the job.
2473 If normal close is false then we're tearing down the jobs - treat as an
2475 ****************************************************************************/
2477 BOOL print_job_end(int snum, uint32 jobid, enum file_close_type close_type)
2479 const char* sharename = lp_const_servicename(snum);
2480 struct printjob *pjob;
2482 SMB_STRUCT_STAT sbuf;
2483 struct printif *current_printif = get_printer_fns( snum );
2485 pjob = print_job_find(sharename, jobid);
2490 if (pjob->spooled || pjob->pid != sys_getpid())
2493 if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
2494 (sys_fstat(pjob->fd, &sbuf) == 0)) {
2495 pjob->size = sbuf.st_size;
2501 * Not a normal close or we couldn't stat the job file,
2502 * so something has gone wrong. Cleanup.
2506 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
2510 /* Technically, this is not quite right. If the printer has a separator
2511 * page turned on, the NT spooler prints the separator page even if the
2512 * print job is 0 bytes. 010215 JRR */
2513 if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
2514 /* don't bother spooling empty files or something being deleted. */
2515 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
2516 pjob->filename, pjob->size ? "deleted" : "zero length" ));
2517 unlink(pjob->filename);
2518 pjob_delete(sharename, jobid);
2522 pjob->smbjob = jobid;
2524 ret = (*(current_printif->job_submit))(snum, pjob);
2529 /* The print job has been sucessfully handed over to the back-end */
2531 pjob->spooled = True;
2532 pjob->status = LPQ_QUEUED;
2533 pjob_store(sharename, jobid, pjob);
2535 /* make sure the database is up to date */
2536 if (print_cache_expired(lp_const_servicename(snum), True))
2537 print_queue_update(snum, False);
2543 /* The print job was not succesfully started. Cleanup */
2544 /* Still need to add proper error return propagation! 010122:JRR */
2545 unlink(pjob->filename);
2546 pjob_delete(sharename, jobid);
2550 /****************************************************************************
2551 Get a snapshot of jobs in the system without traversing.
2552 ****************************************************************************/
2554 static BOOL get_stored_queue_info(struct tdb_print_db *pdb, int snum, int *pcount, print_queue_struct **ppqueue)
2556 TDB_DATA data, cgdata;
2557 print_queue_struct *queue = NULL;
2559 uint32 extra_count = 0;
2560 int total_count = 0;
2563 int max_reported_jobs = lp_max_reported_jobs(snum);
2565 const char* sharename = lp_servicename(snum);
2567 /* make sure the database is up to date */
2568 if (print_cache_expired(lp_const_servicename(snum), True))
2569 print_queue_update(snum, False);
2575 ZERO_STRUCT(cgdata);
2577 /* Get the stored queue data. */
2578 data = tdb_fetch(pdb->tdb, string_tdb_data("INFO/linear_queue_array"));
2580 if (data.dptr && data.dsize >= sizeof(qcount))
2581 len += tdb_unpack(data.dptr + len, data.dsize - len, "d", &qcount);
2583 /* Get the changed jobs list. */
2584 cgdata = tdb_fetch(pdb->tdb, string_tdb_data("INFO/jobs_changed"));
2585 if (cgdata.dptr != NULL && (cgdata.dsize % 4 == 0))
2586 extra_count = cgdata.dsize/4;
2588 DEBUG(5,("get_stored_queue_info: qcount = %u, extra_count = %u\n", (unsigned int)qcount, (unsigned int)extra_count));
2590 /* Allocate the queue size. */
2591 if (qcount == 0 && extra_count == 0)
2594 if ((queue = SMB_MALLOC_ARRAY(print_queue_struct, qcount + extra_count)) == NULL)
2597 /* Retrieve the linearised queue data. */
2599 for( i = 0; i < qcount; i++) {
2600 uint32 qjob, qsize, qpage_count, qstatus, qpriority, qtime;
2601 len += tdb_unpack(data.dptr + len, data.dsize - len, "ddddddff",
2610 queue[i].job = qjob;
2611 queue[i].size = qsize;
2612 queue[i].page_count = qpage_count;
2613 queue[i].status = qstatus;
2614 queue[i].priority = qpriority;
2615 queue[i].time = qtime;
2618 total_count = qcount;
2620 /* Add in the changed jobids. */
2621 for( i = 0; i < extra_count; i++) {
2623 struct printjob *pjob;
2625 jobid = IVAL(cgdata.dptr, i*4);
2626 DEBUG(5,("get_stored_queue_info: changed job = %u\n", (unsigned int)jobid));
2627 pjob = print_job_find(lp_const_servicename(snum), jobid);
2629 DEBUG(5,("get_stored_queue_info: failed to find changed job = %u\n", (unsigned int)jobid));
2630 remove_from_jobs_changed(sharename, jobid);
2634 queue[total_count].job = jobid;
2635 queue[total_count].size = pjob->size;
2636 queue[total_count].page_count = pjob->page_count;
2637 queue[total_count].status = pjob->status;
2638 queue[total_count].priority = 1;
2639 queue[total_count].time = pjob->starttime;
2640 fstrcpy(queue[total_count].fs_user, pjob->user);
2641 fstrcpy(queue[total_count].fs_file, pjob->jobname);
2645 /* Sort the queue by submission time otherwise they are displayed
2648 qsort(queue, total_count, sizeof(print_queue_struct), QSORT_CAST(printjob_comp));
2650 DEBUG(5,("get_stored_queue_info: total_count = %u\n", (unsigned int)total_count));
2652 if (max_reported_jobs && total_count > max_reported_jobs)
2653 total_count = max_reported_jobs;
2656 *pcount = total_count;
2662 SAFE_FREE(data.dptr);
2663 SAFE_FREE(cgdata.dptr);
2667 /****************************************************************************
2668 Get a printer queue listing.
2669 set queue = NULL and status = NULL if you just want to update the cache
2670 ****************************************************************************/
2672 int print_queue_status(int snum,
2673 print_queue_struct **ppqueue,
2674 print_status_struct *status)
2678 const char *sharename;
2679 struct tdb_print_db *pdb;
2682 /* make sure the database is up to date */
2684 if (print_cache_expired(lp_const_servicename(snum), True))
2685 print_queue_update(snum, False);
2687 /* return if we are done */
2688 if ( !ppqueue || !status )
2692 sharename = lp_const_servicename(snum);
2693 pdb = get_print_db_byname(sharename);
2699 * Fetch the queue status. We must do this first, as there may
2700 * be no jobs in the queue.
2703 ZERO_STRUCTP(status);
2704 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", sharename);
2705 key = string_tdb_data(keystr);
2707 data = tdb_fetch(pdb->tdb, key);
2709 if (data.dsize == sizeof(*status)) {
2710 /* this memcpy is ok since the status struct was
2711 not packed before storing it in the tdb */
2712 memcpy(status, data.dptr, sizeof(*status));
2714 SAFE_FREE(data.dptr);
2718 * Now, fetch the print queue information. We first count the number
2719 * of entries, and then only retrieve the queue if necessary.
2722 if (!get_stored_queue_info(pdb, snum, &count, ppqueue)) {
2723 release_print_db(pdb);
2727 release_print_db(pdb);
2731 /****************************************************************************
2733 ****************************************************************************/
2735 BOOL print_queue_pause(struct current_user *user, int snum, WERROR *errcode)
2738 struct printif *current_printif = get_printer_fns( snum );
2740 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2741 *errcode = WERR_ACCESS_DENIED;
2748 ret = (*(current_printif->queue_pause))(snum);
2753 *errcode = WERR_INVALID_PARAM;
2757 /* force update the database */
2758 print_cache_flush(lp_const_servicename(snum));
2760 /* Send a printer notify message */
2762 notify_printer_status(snum, PRINTER_STATUS_PAUSED);
2767 /****************************************************************************
2769 ****************************************************************************/
2771 BOOL print_queue_resume(struct current_user *user, int snum, WERROR *errcode)
2774 struct printif *current_printif = get_printer_fns( snum );
2776 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2777 *errcode = WERR_ACCESS_DENIED;
2783 ret = (*(current_printif->queue_resume))(snum);
2788 *errcode = WERR_INVALID_PARAM;
2792 /* make sure the database is up to date */
2793 if (print_cache_expired(lp_const_servicename(snum), True))
2794 print_queue_update(snum, True);
2796 /* Send a printer notify message */
2798 notify_printer_status(snum, PRINTER_STATUS_OK);
2803 /****************************************************************************
2804 Purge a queue - implemented by deleting all jobs that we can delete.
2805 ****************************************************************************/
2807 BOOL print_queue_purge(struct current_user *user, int snum, WERROR *errcode)
2809 print_queue_struct *queue;
2810 print_status_struct status;
2814 /* Force and update so the count is accurate (i.e. not a cached count) */
2815 print_queue_update(snum, True);
2817 can_job_admin = print_access_check(user, snum, JOB_ACCESS_ADMINISTER);
2818 njobs = print_queue_status(snum, &queue, &status);
2820 if ( can_job_admin )
2823 for (i=0;i<njobs;i++) {
2824 BOOL owner = is_owner(user, lp_const_servicename(snum), queue[i].job);
2826 if (owner || can_job_admin) {
2827 print_job_delete1(snum, queue[i].job);
2831 if ( can_job_admin )
2834 /* update the cache */
2835 print_queue_update( snum, True );