2 Unix SMB/Netbios implementation.
4 printing backend routines
5 Copyright (C) Andrew Tridgell 1992-2000
6 Copyright (C) Jeremy Allison 2002
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /* Current printer interface */
27 static struct printif *current_printif = &generic_printif;
28 static BOOL remove_from_jobs_changed(int snum, uint32 jobid);
31 the printing backend revolves around a tdb database that stores the
32 SMB view of the print queue
34 The key for this database is a jobid - a internally generated number that
35 uniquely identifies a print job
37 reading the print queue involves two steps:
38 - possibly running lpq and updating the internal database from that
39 - reading entries from the database
41 jobids are assigned when a job starts spooling.
44 /***************************************************************************
45 Nightmare. LANMAN jobid's are 16 bit numbers..... We must map them to 32
46 bit RPC jobids.... JRA.
47 ***************************************************************************/
49 static TDB_CONTEXT *rap_tdb;
50 static uint16 next_rap_jobid;
52 uint16 pjobid_to_rap(int snum, uint32 jobid)
58 DEBUG(10,("pjobid_to_rap: called.\n"));
61 /* Create the in-memory tdb. */
62 rap_tdb = tdb_open_log(NULL, 0, TDB_INTERNAL, (O_RDWR|O_CREAT), 0644);
67 SIVAL(&jinfo,0,(int32)snum);
68 SIVAL(&jinfo,4,jobid);
70 key.dptr = (char *)&jinfo;
71 key.dsize = sizeof(jinfo);
72 data = tdb_fetch(rap_tdb, key);
73 if (data.dptr && data.dsize == sizeof(uint16)) {
74 memcpy(&rap_jobid, data.dptr, sizeof(uint16));
76 DEBUG(10,("pjobid_to_rap: jobid %u maps to RAP jobid %u\n",
78 (unsigned int)rap_jobid));
82 /* Not found - create and store mapping. */
83 rap_jobid = ++next_rap_jobid;
85 rap_jobid = ++next_rap_jobid;
86 data.dptr = (char *)&rap_jobid;
87 data.dsize = sizeof(rap_jobid);
88 tdb_store(rap_tdb, key, data, TDB_REPLACE);
89 tdb_store(rap_tdb, data, key, TDB_REPLACE);
91 DEBUG(10,("pjobid_to_rap: created jobid %u maps to RAP jobid %u\n",
93 (unsigned int)rap_jobid));
97 BOOL rap_to_pjobid(uint16 rap_jobid, int *psnum, uint32 *pjobid)
101 DEBUG(10,("rap_to_pjobid called.\n"));
106 key.dptr = (char *)&rap_jobid;
107 key.dsize = sizeof(rap_jobid);
108 data = tdb_fetch(rap_tdb, key);
109 if (data.dptr && data.dsize == 8) {
110 *psnum = IVAL(data.dptr,0);
111 *pjobid = IVAL(data.dptr,4);
112 DEBUG(10,("rap_to_pjobid: jobid %u maps to RAP jobid %u\n",
113 (unsigned int)*pjobid,
114 (unsigned int)rap_jobid));
115 SAFE_FREE(data.dptr);
119 DEBUG(10,("rap_to_pjobid: Failed to lookup RAP jobid %u\n",
120 (unsigned int)rap_jobid));
121 SAFE_FREE(data.dptr);
125 static void rap_jobid_delete(int snum, uint32 jobid)
131 DEBUG(10,("rap_jobid_delete: called.\n"));
136 SIVAL(&jinfo,0,(int32)snum);
137 SIVAL(&jinfo,4,jobid);
139 key.dptr = (char *)&jinfo;
140 key.dsize = sizeof(jinfo);
141 data = tdb_fetch(rap_tdb, key);
142 if (!data.dptr || (data.dsize != sizeof(uint16))) {
143 DEBUG(10,("rap_jobid_delete: cannot find jobid %u\n",
144 (unsigned int)jobid ));
145 SAFE_FREE(data.dptr);
149 DEBUG(10,("rap_jobid_delete: deleting jobid %u\n",
150 (unsigned int)jobid ));
152 memcpy(&rap_jobid, data.dptr, sizeof(uint16));
153 SAFE_FREE(data.dptr);
154 data.dptr = (char *)&rap_jobid;
155 data.dsize = sizeof(rap_jobid);
156 tdb_delete(rap_tdb, key);
157 tdb_delete(rap_tdb, data);
160 static pid_t local_pid;
162 static int get_queue_status(int, print_status_struct *);
164 /****************************************************************************
165 Initialise the printing backend. Called once at startup before the fork().
166 ****************************************************************************/
168 BOOL print_backend_init(void)
170 const char *sversion = "INFO/version";
171 pstring printing_path;
172 int services = lp_numservices();
175 if (local_pid == sys_getpid())
178 unlink(lock_path("printing.tdb"));
179 pstrcpy(printing_path,lock_path("printing"));
180 mkdir(printing_path,0755);
182 local_pid = sys_getpid();
184 /* handle a Samba upgrade */
186 for (snum = 0; snum < services; snum++) {
187 struct tdb_print_db *pdb;
188 if (!lp_print_ok(snum))
191 pdb = get_print_db_byname(lp_const_servicename(snum));
194 if (tdb_lock_bystring(pdb->tdb, sversion, 0) == -1) {
195 DEBUG(0,("print_backend_init: Failed to open printer %s database\n", lp_const_servicename(snum) ));
196 release_print_db(pdb);
199 if (tdb_fetch_int32(pdb->tdb, sversion) != PRINT_DATABASE_VERSION) {
200 tdb_traverse(pdb->tdb, tdb_traverse_delete_fn, NULL);
201 tdb_store_int32(pdb->tdb, sversion, PRINT_DATABASE_VERSION);
203 tdb_unlock_bystring(pdb->tdb, sversion);
204 release_print_db(pdb);
207 close_all_print_db(); /* Don't leave any open. */
209 /* select the appropriate printing interface... */
211 if (strcmp(lp_printcapname(), "cups") == 0)
212 current_printif = &cups_printif;
213 #endif /* HAVE_CUPS */
215 /* do NT print initialization... */
216 return nt_printing_init();
219 /****************************************************************************
220 Shut down printing backend. Called once at shutdown to close the tdb.
221 ****************************************************************************/
223 void printing_end(void)
225 close_all_print_db(); /* Don't leave any open. */
228 /****************************************************************************
229 Useful function to generate a tdb key.
230 ****************************************************************************/
232 static TDB_DATA print_key(uint32 jobid)
238 ret.dptr = (void *)&j;
239 ret.dsize = sizeof(j);
243 /***********************************************************************
244 unpack a pjob from a tdb buffer
245 ***********************************************************************/
247 int unpack_pjob( char* buf, int buflen, struct printjob *pjob )
251 uint32 pjpid, pjsysjob, pjfd, pjstarttime, pjstatus;
252 uint32 pjsize, pjpage_count, pjspooled, pjsmbjob;
257 len += tdb_unpack(buf+len, buflen-len, "dddddddddffff",
275 if ( (used = unpack_devicemode(&pjob->nt_devmode, buf+len, buflen-len)) == -1 )
281 pjob->sysjob = pjsysjob;
283 pjob->starttime = pjstarttime;
284 pjob->status = pjstatus;
286 pjob->page_count = pjpage_count;
287 pjob->spooled = pjspooled;
288 pjob->smbjob = pjsmbjob;
294 /****************************************************************************
295 Useful function to find a print job in the database.
296 ****************************************************************************/
298 static struct printjob *print_job_find(int snum, uint32 jobid)
300 static struct printjob pjob;
302 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
308 ret = tdb_fetch(pdb->tdb, print_key(jobid));
309 release_print_db(pdb);
314 if ( pjob.nt_devmode )
315 free_nt_devicemode( &pjob.nt_devmode );
319 if ( unpack_pjob( ret.dptr, ret.dsize, &pjob ) == -1 ) {
328 /* Convert a unix jobid to a smb jobid */
330 static uint32 sysjob_to_jobid_value;
332 static int unixjob_traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key,
333 TDB_DATA data, void *state)
335 struct printjob *pjob;
336 int *sysjob = (int *)state;
338 if (!data.dptr || data.dsize == 0)
341 pjob = (struct printjob *)data.dptr;
342 if (key.dsize != sizeof(uint32))
345 if (*sysjob == pjob->sysjob) {
346 uint32 *jobid = (uint32 *)key.dptr;
348 sysjob_to_jobid_value = *jobid;
355 /****************************************************************************
356 This is a *horribly expensive call as we have to iterate through all the
357 current printer tdb's. Don't do this often ! JRA.
358 ****************************************************************************/
360 uint32 sysjob_to_jobid(int unix_jobid)
362 int services = lp_numservices();
365 sysjob_to_jobid_value = (uint32)-1;
367 for (snum = 0; snum < services; snum++) {
368 struct tdb_print_db *pdb;
369 if (!lp_print_ok(snum))
371 pdb = get_print_db_byname(lp_const_servicename(snum));
373 tdb_traverse(pdb->tdb, unixjob_traverse_fn, &unix_jobid);
374 release_print_db(pdb);
375 if (sysjob_to_jobid_value != (uint32)-1)
376 return sysjob_to_jobid_value;
381 /****************************************************************************
382 Send notifications based on what has changed after a pjob_store.
383 ****************************************************************************/
387 uint32 spoolss_status;
388 } lpq_to_spoolss_status_map[] = {
389 { LPQ_QUEUED, JOB_STATUS_QUEUED },
390 { LPQ_PAUSED, JOB_STATUS_PAUSED },
391 { LPQ_SPOOLING, JOB_STATUS_SPOOLING },
392 { LPQ_PRINTING, JOB_STATUS_PRINTING },
393 { LPQ_DELETING, JOB_STATUS_DELETING },
394 { LPQ_OFFLINE, JOB_STATUS_OFFLINE },
395 { LPQ_PAPEROUT, JOB_STATUS_PAPEROUT },
396 { LPQ_PRINTED, JOB_STATUS_PRINTED },
397 { LPQ_DELETED, JOB_STATUS_DELETED },
398 { LPQ_BLOCKED, JOB_STATUS_BLOCKED },
399 { LPQ_USER_INTERVENTION, JOB_STATUS_USER_INTERVENTION },
403 /* Convert a lpq status value stored in printing.tdb into the
404 appropriate win32 API constant. */
406 static uint32 map_to_spoolss_status(uint32 lpq_status)
410 while (lpq_to_spoolss_status_map[i].lpq_status != -1) {
411 if (lpq_to_spoolss_status_map[i].lpq_status == lpq_status)
412 return lpq_to_spoolss_status_map[i].spoolss_status;
419 static void pjob_store_notify(int snum, uint32 jobid, struct printjob *old_data,
420 struct printjob *new_data)
422 BOOL new_job = False;
427 /* Notify the job name first */
429 if (new_job || !strequal(old_data->jobname, new_data->jobname))
430 notify_job_name(snum, jobid, new_data->jobname);
432 /* Job attributes that can't be changed. We only send
433 notification for these on a new job. */
436 notify_job_submitted(snum, jobid, new_data->starttime);
437 notify_job_username(snum, jobid, new_data->user);
440 /* Job attributes of a new job or attributes that can be
443 if (new_job || old_data->status != new_data->status)
444 notify_job_status(snum, jobid, map_to_spoolss_status(new_data->status));
446 if (new_job || old_data->size != new_data->size)
447 notify_job_total_bytes(snum, jobid, new_data->size);
449 if (new_job || old_data->page_count != new_data->page_count)
450 notify_job_total_pages(snum, jobid, new_data->page_count);
453 /****************************************************************************
454 Store a job structure back to the database.
455 ****************************************************************************/
457 static BOOL pjob_store(int snum, uint32 jobid, struct printjob *pjob)
459 TDB_DATA old_data, new_data;
461 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
463 int len, newlen, buflen;
471 old_data = tdb_fetch(pdb->tdb, print_key(jobid));
473 /* Doh! Now we have to pack/unpack data since the NT_DEVICEMODE was added */
480 len += tdb_pack(buf+len, buflen-len, "dddddddddffff",
482 (uint32)pjob->sysjob,
484 (uint32)pjob->starttime,
485 (uint32)pjob->status,
487 (uint32)pjob->page_count,
488 (uint32)pjob->spooled,
489 (uint32)pjob->smbjob,
495 len += pack_devicemode(pjob->nt_devmode, buf+len, buflen-len);
500 tb = (char *)Realloc(buf, len);
502 DEBUG(0,("pjob_store: failed to enlarge buffer!\n"));
509 } while ( buflen != len );
515 new_data.dsize = len;
516 ret = (tdb_store(pdb->tdb, print_key(jobid), new_data, TDB_REPLACE) == 0);
518 release_print_db(pdb);
520 /* Send notify updates for what has changed */
522 if ( ret && (old_data.dsize == 0 || old_data.dsize == sizeof(*pjob)) )
523 pjob_store_notify( snum, jobid, (struct printjob *)old_data.dptr, pjob );
526 SAFE_FREE( old_data.dptr );
532 /****************************************************************************
533 Remove a job structure from the database.
534 ****************************************************************************/
536 void pjob_delete(int snum, uint32 jobid)
538 struct printjob *pjob = print_job_find(snum, jobid);
539 uint32 job_status = 0;
540 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
546 DEBUG(5, ("pjob_delete(): we were asked to delete nonexistent job %u\n",
547 (unsigned int)jobid));
548 release_print_db(pdb);
552 /* Send a notification that a job has been deleted */
554 job_status = map_to_spoolss_status(pjob->status);
556 /* We must cycle through JOB_STATUS_DELETING and
557 JOB_STATUS_DELETED for the port monitor to delete the job
560 job_status |= JOB_STATUS_DELETING;
561 notify_job_status(snum, jobid, job_status);
563 job_status |= JOB_STATUS_DELETED;
564 notify_job_status(snum, jobid, job_status);
566 /* Remove from printing.tdb */
568 tdb_delete(pdb->tdb, print_key(jobid));
569 release_print_db(pdb);
570 rap_jobid_delete(snum, jobid);
573 /****************************************************************************
574 Parse a file name from the system spooler to generate a jobid.
575 ****************************************************************************/
577 static uint32 print_parse_jobid(char *fname)
581 if (strncmp(fname,PRINT_SPOOL_PREFIX,strlen(PRINT_SPOOL_PREFIX)) != 0)
583 fname += strlen(PRINT_SPOOL_PREFIX);
589 return (uint32)jobid;
592 /****************************************************************************
593 List a unix job in the print database.
594 ****************************************************************************/
596 static void print_unix_job(int snum, print_queue_struct *q, uint32 jobid)
598 struct printjob pj, *old_pj;
600 if (jobid == (uint32)-1)
601 jobid = q->job + UNIX_JOB_START;
603 /* Preserve the timestamp on an existing unix print job */
605 old_pj = print_job_find(snum, jobid);
612 pj.starttime = old_pj ? old_pj->starttime : q->time;
613 pj.status = q->status;
616 fstrcpy(pj.filename, old_pj ? old_pj->filename : "");
617 if (jobid < UNIX_JOB_START) {
618 pj.smbjob = (old_pj != NULL ? True : False);
619 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : "Remote Downlevel Document");
622 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : q->fs_file);
624 fstrcpy(pj.user, old_pj ? old_pj->user : q->fs_user);
625 fstrcpy(pj.queuename, old_pj ? old_pj->queuename : lp_const_servicename(snum));
627 pjob_store(snum, jobid, &pj);
631 struct traverse_struct {
632 print_queue_struct *queue;
633 int qcount, snum, maxcount, total_jobs;
637 /****************************************************************************
638 Utility fn to delete any jobs that are no longer active.
639 ****************************************************************************/
641 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
643 struct traverse_struct *ts = (struct traverse_struct *)state;
644 struct printjob pjob;
648 if ( key.dsize != sizeof(jobid) )
651 memcpy(&jobid, key.dptr, sizeof(jobid));
652 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
654 free_nt_devicemode( &pjob.nt_devmode );
657 if (ts->snum != lp_servicenumber(pjob.queuename)) {
658 /* this isn't for the queue we are looking at - this cannot happen with the split tdb's. JRA */
663 /* remove a unix job if it isn't in the system queue any more */
665 for (i=0;i<ts->qcount;i++) {
666 uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
667 if (jobid == u_jobid)
670 if (i == ts->qcount) {
671 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !smbjob\n",
672 (unsigned int)jobid ));
673 pjob_delete(ts->snum, jobid);
679 /* maybe it hasn't been spooled yet */
681 /* if a job is not spooled and the process doesn't
682 exist then kill it. This cleans up after smbd
684 if (!process_exists(pjob.pid)) {
685 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !process_exists (%u)\n",
686 (unsigned int)jobid, (unsigned int)pjob.pid ));
687 pjob_delete(ts->snum, jobid);
693 for (i=0;i<ts->qcount;i++) {
694 uint32 curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
695 if (jobid == curr_jobid)
699 /* The job isn't in the system queue - we have to assume it has
700 completed, so delete the database entry. */
702 if (i == ts->qcount) {
704 /* A race can occur between the time a job is spooled and
705 when it appears in the lpq output. This happens when
706 the job is added to printing.tdb when another smbd
707 running print_queue_update() has completed a lpq and
708 is currently traversing the printing tdb and deleting jobs.
709 Don't delete the job if it was submitted after the lpq_time. */
711 if (pjob.starttime < ts->lpq_time) {
712 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to pjob.starttime (%u) < ts->lpq_time (%u)\n",
714 (unsigned int)pjob.starttime,
715 (unsigned int)ts->lpq_time ));
716 pjob_delete(ts->snum, jobid);
722 /* Save the pjob attributes we will store. */
723 ts->queue[i].job = jobid;
724 ts->queue[i].size = pjob.size;
725 ts->queue[i].page_count = pjob.page_count;
726 ts->queue[i].status = pjob.status;
727 ts->queue[i].priority = 1;
728 ts->queue[i].time = pjob.starttime;
729 fstrcpy(ts->queue[i].fs_user, pjob.user);
730 fstrcpy(ts->queue[i].fs_file, pjob.jobname);
737 /****************************************************************************
738 Check if the print queue has been updated recently enough.
739 ****************************************************************************/
741 static void print_cache_flush(int snum)
744 const char *printername = lp_const_servicename(snum);
745 struct tdb_print_db *pdb = get_print_db_byname(printername);
749 slprintf(key, sizeof(key)-1, "CACHE/%s", printername);
750 tdb_store_int32(pdb->tdb, key, -1);
751 release_print_db(pdb);
754 /****************************************************************************
755 Check if someone already thinks they are doing the update.
756 ****************************************************************************/
758 static pid_t get_updating_pid(fstring printer_name)
763 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
767 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
769 key.dsize = strlen(keystr);
771 data = tdb_fetch(pdb->tdb, key);
772 release_print_db(pdb);
773 if (!data.dptr || data.dsize != sizeof(pid_t)) {
774 SAFE_FREE(data.dptr);
778 memcpy(&updating_pid, data.dptr, sizeof(pid_t));
779 SAFE_FREE(data.dptr);
781 if (process_exists(updating_pid))
787 /****************************************************************************
788 Set the fact that we're doing the update, or have finished doing the update
790 ****************************************************************************/
792 static void set_updating_pid(const fstring printer_name, BOOL delete)
797 pid_t updating_pid = sys_getpid();
798 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
803 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
805 key.dsize = strlen(keystr);
808 tdb_delete(pdb->tdb, key);
809 release_print_db(pdb);
813 data.dptr = (void *)&updating_pid;
814 data.dsize = sizeof(pid_t);
816 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
817 release_print_db(pdb);
820 /****************************************************************************
821 Sort print jobs by submittal time.
822 ****************************************************************************/
824 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
835 /* Sort on job start time */
837 if (j1->time == j2->time)
839 return (j1->time > j2->time) ? 1 : -1;
842 /****************************************************************************
843 Store the sorted queue representation for later portmon retrieval.
844 ****************************************************************************/
846 static void store_queue_struct(struct tdb_print_db *pdb, struct traverse_struct *pts)
849 int max_reported_jobs = lp_max_reported_jobs(pts->snum);
850 print_queue_struct *queue = pts->queue;
855 if (max_reported_jobs && (max_reported_jobs < pts->qcount))
856 pts->qcount = max_reported_jobs;
857 qcount = pts->qcount;
859 /* Work out the size. */
861 data.dsize += tdb_pack(NULL, 0, "d", qcount);
863 for (i = 0; i < pts->qcount; i++) {
864 data.dsize += tdb_pack(NULL, 0, "ddddddff",
865 (uint32)queue[i].job,
866 (uint32)queue[i].size,
867 (uint32)queue[i].page_count,
868 (uint32)queue[i].status,
869 (uint32)queue[i].priority,
870 (uint32)queue[i].time,
875 if ((data.dptr = malloc(data.dsize)) == NULL)
879 len += tdb_pack(data.dptr + len, data.dsize - len, "d", qcount);
880 for (i = 0; i < pts->qcount; i++) {
881 len += tdb_pack(data.dptr + len, data.dsize - len, "ddddddff",
882 (uint32)queue[i].job,
883 (uint32)queue[i].size,
884 (uint32)queue[i].page_count,
885 (uint32)queue[i].status,
886 (uint32)queue[i].priority,
887 (uint32)queue[i].time,
892 key.dptr = "INFO/linear_queue_array";
893 key.dsize = strlen(key.dptr);
894 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
895 SAFE_FREE(data.dptr);
899 static TDB_DATA get_jobs_changed_data(struct tdb_print_db *pdb)
903 key.dptr = "INFO/jobs_changed";
904 key.dsize = strlen(key.dptr);
907 data = tdb_fetch(pdb->tdb, key);
908 if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0)) {
909 SAFE_FREE(data.dptr);
916 static void check_job_changed(int snum, TDB_DATA data, uint32 jobid)
919 unsigned int job_count = data.dsize / 4;
921 for (i = 0; i < job_count; i++) {
924 memcpy(&ch_jobid, data.dptr + (i*4), 4);
925 if (ch_jobid == jobid)
926 remove_from_jobs_changed(snum, jobid);
930 /****************************************************************************
931 Update the internal database from the system print queue for a queue.
932 ****************************************************************************/
934 static void print_queue_update(int snum)
937 print_queue_struct *queue = NULL;
938 print_status_struct status;
939 print_status_struct old_status;
940 struct printjob *pjob;
941 struct traverse_struct tstruct;
942 fstring keystr, printer_name, cachestr;
945 struct tdb_print_db *pdb;
947 fstrcpy(printer_name, lp_const_servicename(snum));
948 pdb = get_print_db_byname(printer_name);
953 * Check to see if someone else is doing this update.
954 * This is essentially a mutex on the update.
957 if (get_updating_pid(printer_name) != -1) {
958 release_print_db(pdb);
962 /* Lock the queue for the database update */
964 slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", printer_name);
965 /* Only wait 10 seconds for this. */
966 if (tdb_lock_bystring(pdb->tdb, keystr, 10) == -1) {
967 DEBUG(0,("print_queue_update: Failed to lock printer %s database\n", printer_name));
968 release_print_db(pdb);
973 * Ensure that no one else got in here.
974 * If the updating pid is still -1 then we are
978 if (get_updating_pid(printer_name) != -1) {
980 * Someone else is doing the update, exit.
982 tdb_unlock_bystring(pdb->tdb, keystr);
983 release_print_db(pdb);
988 * We're going to do the update ourselves.
991 /* Tell others we're doing the update. */
992 set_updating_pid(printer_name, False);
995 * Allow others to enter and notice we're doing
999 tdb_unlock_bystring(pdb->tdb, keystr);
1002 * Update the cache time FIRST ! Stops others even
1003 * attempting to get the lock and doing this
1004 * if the lpq takes a long time.
1007 slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", printer_name);
1008 tdb_store_int32(pdb->tdb, cachestr, (int)time(NULL));
1010 /* get the current queue using the appropriate interface */
1011 ZERO_STRUCT(status);
1013 qcount = (*(current_printif->queue_get))(snum, &queue, &status);
1015 DEBUG(3, ("%d job%s in queue for %s\n", qcount, (qcount != 1) ?
1016 "s" : "", printer_name));
1018 /* Sort the queue by submission time otherwise they are displayed
1021 qsort(queue, qcount, sizeof(print_queue_struct),
1022 QSORT_CAST(printjob_comp));
1025 any job in the internal database that is marked as spooled
1026 and doesn't exist in the system queue is considered finished
1027 and removed from the database
1029 any job in the system database but not in the internal database
1030 is added as a unix job
1032 fill in any system job numbers as we go
1035 jcdata = get_jobs_changed_data(pdb);
1037 for (i=0; i<qcount; i++) {
1038 uint32 jobid = print_parse_jobid(queue[i].fs_file);
1040 if (jobid == (uint32)-1) {
1041 /* assume its a unix print job */
1042 print_unix_job(snum, &queue[i], jobid);
1046 /* we have an active SMB print job - update its status */
1047 pjob = print_job_find(snum, jobid);
1049 /* err, somethings wrong. Probably smbd was restarted
1050 with jobs in the queue. All we can do is treat them
1051 like unix jobs. Pity. */
1052 print_unix_job(snum, &queue[i], jobid);
1056 pjob->sysjob = queue[i].job;
1057 pjob->status = queue[i].status;
1058 pjob_store(snum, jobid, pjob);
1059 check_job_changed(snum, jcdata, jobid);
1062 SAFE_FREE(jcdata.dptr);
1064 /* now delete any queued entries that don't appear in the
1066 tstruct.queue = queue;
1067 tstruct.qcount = qcount;
1068 tstruct.snum = snum;
1069 tstruct.total_jobs = 0;
1070 tstruct.lpq_time = time(NULL);
1072 tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
1074 /* Store the linearised queue, max jobs only. */
1075 store_queue_struct(pdb, &tstruct);
1077 SAFE_FREE(tstruct.queue);
1079 DEBUG(10,("print_queue_update: printer %s INFO/total_jobs = %d\n",
1080 printer_name, tstruct.total_jobs ));
1082 tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
1084 get_queue_status(snum, &old_status);
1085 if (old_status.qcount != qcount)
1086 DEBUG(10,("print_queue_update: queue status change %d jobs -> %d jobs for printer %s\n",
1087 old_status.qcount, qcount, printer_name ));
1089 /* store the new queue status structure */
1090 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printer_name);
1092 key.dsize = strlen(keystr);
1094 status.qcount = qcount;
1095 data.dptr = (void *)&status;
1096 data.dsize = sizeof(status);
1097 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
1100 * Update the cache time again. We want to do this call
1101 * as little as possible...
1104 slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", printer_name);
1105 tdb_store_int32(pdb->tdb, keystr, (int32)time(NULL));
1107 /* Delete our pid from the db. */
1108 set_updating_pid(printer_name, True);
1109 release_print_db(pdb);
1112 /****************************************************************************
1113 Create/Update an entry in the print tdb that will allow us to send notify
1114 updates only to interested smbd's.
1115 ****************************************************************************/
1117 BOOL print_notify_register_pid(int snum)
1120 struct tdb_print_db *pdb = NULL;
1121 TDB_CONTEXT *tdb = NULL;
1122 const char *printername;
1123 uint32 mypid = (uint32)sys_getpid();
1127 /* if (snum == -1), then the change notify request was
1128 on a print server handle and we need to register on
1133 int num_services = lp_numservices();
1136 for ( idx=0; idx<num_services; idx++ ) {
1137 if (lp_snum_ok(idx) && lp_print_ok(idx) )
1138 print_notify_register_pid(idx);
1143 else /* register for a specific printer */
1145 printername = lp_const_servicename(snum);
1146 pdb = get_print_db_byname(printername);
1152 if (tdb_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1153 DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n",
1156 release_print_db(pdb);
1160 data = get_printer_notify_pid_list( tdb, printername, True );
1162 /* Add ourselves and increase the refcount. */
1164 for (i = 0; i < data.dsize; i += 8) {
1165 if (IVAL(data.dptr,i) == mypid) {
1166 uint32 new_refcount = IVAL(data.dptr, i+4) + 1;
1167 SIVAL(data.dptr, i+4, new_refcount);
1172 if (i == data.dsize) {
1173 /* We weren't in the list. Realloc. */
1174 data.dptr = Realloc(data.dptr, data.dsize + 8);
1176 DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n",
1181 SIVAL(data.dptr,data.dsize - 8,mypid);
1182 SIVAL(data.dptr,data.dsize - 4,1); /* Refcount. */
1185 /* Store back the record. */
1186 if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1187 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1188 list for printer %s\n", printername));
1196 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1198 release_print_db(pdb);
1199 SAFE_FREE(data.dptr);
1203 /****************************************************************************
1204 Update an entry in the print tdb that will allow us to send notify
1205 updates only to interested smbd's.
1206 ****************************************************************************/
1208 BOOL print_notify_deregister_pid(int snum)
1211 struct tdb_print_db *pdb = NULL;
1212 TDB_CONTEXT *tdb = NULL;
1213 const char *printername;
1214 uint32 mypid = (uint32)sys_getpid();
1218 /* if ( snum == -1 ), we are deregister a print server handle
1219 which means to deregister on all print queues */
1223 int num_services = lp_numservices();
1226 for ( idx=0; idx<num_services; idx++ ) {
1227 if ( lp_snum_ok(idx) && lp_print_ok(idx) )
1228 print_notify_deregister_pid(idx);
1233 else /* deregister a specific printer */
1235 printername = lp_const_servicename(snum);
1236 pdb = get_print_db_byname(printername);
1242 if (tdb_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1243 DEBUG(0,("print_notify_register_pid: Failed to lock \
1244 printer %s database\n", printername));
1246 release_print_db(pdb);
1250 data = get_printer_notify_pid_list( tdb, printername, True );
1252 /* Reduce refcount. Remove ourselves if zero. */
1254 for (i = 0; i < data.dsize; ) {
1255 if (IVAL(data.dptr,i) == mypid) {
1256 uint32 refcount = IVAL(data.dptr, i+4);
1260 if (refcount == 0) {
1261 if (data.dsize - i > 8)
1262 memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
1266 SIVAL(data.dptr, i+4, refcount);
1272 if (data.dsize == 0)
1273 SAFE_FREE(data.dptr);
1275 /* Store back the record. */
1276 if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1277 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1278 list for printer %s\n", printername));
1286 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1288 release_print_db(pdb);
1289 SAFE_FREE(data.dptr);
1293 /****************************************************************************
1294 Check if a jobid is valid. It is valid if it exists in the database.
1295 ****************************************************************************/
1297 BOOL print_job_exists(int snum, uint32 jobid)
1299 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
1304 ret = tdb_exists(pdb->tdb, print_key(jobid));
1305 release_print_db(pdb);
1309 /****************************************************************************
1310 Give the fd used for a jobid.
1311 ****************************************************************************/
1313 int print_job_fd(int snum, uint32 jobid)
1315 struct printjob *pjob = print_job_find(snum, jobid);
1318 /* don't allow another process to get this info - it is meaningless */
1319 if (pjob->pid != local_pid)
1324 /****************************************************************************
1325 Give the filename used for a jobid.
1326 Only valid for the process doing the spooling and when the job
1327 has not been spooled.
1328 ****************************************************************************/
1330 char *print_job_fname(int snum, uint32 jobid)
1332 struct printjob *pjob = print_job_find(snum, jobid);
1333 if (!pjob || pjob->spooled || pjob->pid != local_pid)
1335 return pjob->filename;
1339 /****************************************************************************
1340 Give the filename used for a jobid.
1341 Only valid for the process doing the spooling and when the job
1342 has not been spooled.
1343 ****************************************************************************/
1345 NT_DEVICEMODE *print_job_devmode(int snum, uint32 jobid)
1347 struct printjob *pjob = print_job_find(snum, jobid);
1352 return pjob->nt_devmode;
1355 /****************************************************************************
1356 Set the place in the queue for a job.
1357 ****************************************************************************/
1359 BOOL print_job_set_place(int snum, uint32 jobid, int place)
1361 DEBUG(2,("print_job_set_place not implemented yet\n"));
1365 /****************************************************************************
1366 Set the name of a job. Only possible for owner.
1367 ****************************************************************************/
1369 BOOL print_job_set_name(int snum, uint32 jobid, char *name)
1371 struct printjob *pjob = print_job_find(snum, jobid);
1372 if (!pjob || pjob->pid != local_pid)
1375 fstrcpy(pjob->jobname, name);
1376 return pjob_store(snum, jobid, pjob);
1379 /***************************************************************************
1380 Remove a jobid from the 'jobs changed' list.
1381 ***************************************************************************/
1383 static BOOL remove_from_jobs_changed(int snum, uint32 jobid)
1385 const char *printername = lp_const_servicename(snum);
1386 struct tdb_print_db *pdb = get_print_db_byname(printername);
1388 size_t job_count, i;
1390 BOOL gotlock = False;
1392 key.dptr = "INFO/jobs_changed";
1393 key.dsize = strlen(key.dptr);
1396 if (tdb_chainlock_with_timeout(pdb->tdb, key, 5) == -1)
1401 data = tdb_fetch(pdb->tdb, key);
1403 if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0))
1406 job_count = data.dsize / 4;
1407 for (i = 0; i < job_count; i++) {
1410 memcpy(&ch_jobid, data.dptr + (i*4), 4);
1411 if (ch_jobid == jobid) {
1412 if (i < job_count -1 )
1413 memmove(data.dptr + (i*4), data.dptr + (i*4) + 4, (job_count - i - 1)*4 );
1415 if (tdb_store(pdb->tdb, key, data, TDB_REPLACE) == -1)
1425 tdb_chainunlock(pdb->tdb, key);
1426 SAFE_FREE(data.dptr);
1427 release_print_db(pdb);
1429 DEBUG(10,("remove_from_jobs_changed: removed jobid %u\n", (unsigned int)jobid ));
1431 DEBUG(10,("remove_from_jobs_changed: Failed to remove jobid %u\n", (unsigned int)jobid ));
1435 /****************************************************************************
1436 Delete a print job - don't update queue.
1437 ****************************************************************************/
1439 static BOOL print_job_delete1(int snum, uint32 jobid)
1441 struct printjob *pjob = print_job_find(snum, jobid);
1448 * If already deleting just return.
1451 if (pjob->status == LPQ_DELETING)
1454 /* Hrm - we need to be able to cope with deleting a job before it
1455 has reached the spooler. */
1457 if (pjob->sysjob == -1) {
1458 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid));
1461 /* Set the tdb entry to be deleting. */
1463 pjob->status = LPQ_DELETING;
1464 pjob_store(snum, jobid, pjob);
1466 if (pjob->spooled && pjob->sysjob != -1)
1467 result = (*(current_printif->job_delete))(snum, pjob);
1469 remove_from_jobs_changed(snum, jobid);
1471 /* Delete the tdb entry if the delete succeeded or the job hasn't
1475 const char *printername = lp_const_servicename(snum);
1476 struct tdb_print_db *pdb = get_print_db_byname(printername);
1481 pjob_delete(snum, jobid);
1482 /* Ensure we keep a rough count of the number of total jobs... */
1483 tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, -1);
1484 release_print_db(pdb);
1487 return (result == 0);
1490 /****************************************************************************
1491 Return true if the current user owns the print job.
1492 ****************************************************************************/
1494 static BOOL is_owner(struct current_user *user, int snum, uint32 jobid)
1496 struct printjob *pjob = print_job_find(snum, jobid);
1502 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1503 return strequal(pjob->user, vuser->user.smb_name);
1505 return strequal(pjob->user, uidtoname(user->uid));
1509 /****************************************************************************
1511 ****************************************************************************/
1513 BOOL print_job_delete(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1515 BOOL owner, deleted;
1520 owner = is_owner(user, snum, jobid);
1522 /* Check access against security descriptor or whether the user
1526 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1527 DEBUG(3, ("delete denied by security descriptor\n"));
1528 *errcode = WERR_ACCESS_DENIED;
1530 /* BEGIN_ADMIN_LOG */
1531 sys_adminlog( LOG_ERR,
1532 "Permission denied-- user not allowed to delete, \
1533 pause, or resume print job. User name: %s. Printer name: %s.",
1534 uidtoname(user->uid), PRINTERNAME(snum) );
1541 * get the spooled filename of the print job
1542 * if this works, then the file has not been spooled
1543 * to the underlying print system. Just delete the
1544 * spool file & return.
1547 if ( (fname = print_job_fname( snum, jobid )) != NULL )
1549 /* remove the spool file */
1550 DEBUG(10,("print_job_delete: Removing spool file [%s]\n", fname ));
1551 if ( unlink( fname ) == -1 ) {
1552 *errcode = map_werror_from_unix(errno);
1559 if (!print_job_delete1(snum, jobid)) {
1560 *errcode = WERR_ACCESS_DENIED;
1564 /* force update the database and say the delete failed if the
1567 print_queue_update(snum);
1569 deleted = !print_job_exists(snum, jobid);
1571 *errcode = WERR_ACCESS_DENIED;
1576 /****************************************************************************
1578 ****************************************************************************/
1580 BOOL print_job_pause(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1582 struct printjob *pjob = print_job_find(snum, jobid);
1588 if (!pjob->spooled || pjob->sysjob == -1)
1591 if (!is_owner(user, snum, jobid) &&
1592 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1593 DEBUG(3, ("pause denied by security descriptor\n"));
1595 /* BEGIN_ADMIN_LOG */
1596 sys_adminlog( LOG_ERR,
1597 "Permission denied-- user not allowed to delete, \
1598 pause, or resume print job. User name: %s. Printer name: %s.",
1599 uidtoname(user->uid), PRINTERNAME(snum) );
1602 *errcode = WERR_ACCESS_DENIED;
1606 /* need to pause the spooled entry */
1607 ret = (*(current_printif->job_pause))(snum, pjob);
1610 *errcode = WERR_INVALID_PARAM;
1614 /* force update the database */
1615 print_cache_flush(snum);
1617 /* Send a printer notify message */
1619 notify_job_status(snum, jobid, JOB_STATUS_PAUSED);
1621 /* how do we tell if this succeeded? */
1626 /****************************************************************************
1628 ****************************************************************************/
1630 BOOL print_job_resume(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1632 struct printjob *pjob = print_job_find(snum, jobid);
1638 if (!pjob->spooled || pjob->sysjob == -1)
1641 if (!is_owner(user, snum, jobid) &&
1642 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1643 DEBUG(3, ("resume denied by security descriptor\n"));
1644 *errcode = WERR_ACCESS_DENIED;
1646 /* BEGIN_ADMIN_LOG */
1647 sys_adminlog( LOG_ERR,
1648 "Permission denied-- user not allowed to delete, \
1649 pause, or resume print job. User name: %s. Printer name: %s.",
1650 uidtoname(user->uid), PRINTERNAME(snum) );
1655 ret = (*(current_printif->job_resume))(snum, pjob);
1658 *errcode = WERR_INVALID_PARAM;
1662 /* force update the database */
1663 print_cache_flush(snum);
1665 /* Send a printer notify message */
1667 notify_job_status(snum, jobid, JOB_STATUS_QUEUED);
1672 /****************************************************************************
1673 Write to a print file.
1674 ****************************************************************************/
1676 int print_job_write(int snum, uint32 jobid, const char *buf, int size)
1679 struct printjob *pjob = print_job_find(snum, jobid);
1683 /* don't allow another process to get this info - it is meaningless */
1684 if (pjob->pid != local_pid)
1687 return_code = write(pjob->fd, buf, size);
1688 if (return_code>0) {
1690 pjob_store(snum, jobid, pjob);
1695 /****************************************************************************
1696 Check if the print queue has been updated recently enough.
1697 ****************************************************************************/
1699 static BOOL print_cache_expired(int snum)
1702 time_t last_qscan_time, time_now = time(NULL);
1703 const char *printername = lp_const_servicename(snum);
1704 struct tdb_print_db *pdb = get_print_db_byname(printername);
1709 slprintf(key, sizeof(key), "CACHE/%s", printername);
1710 last_qscan_time = (time_t)tdb_fetch_int32(pdb->tdb, key);
1713 * Invalidate the queue for 3 reasons.
1714 * (1). last queue scan time == -1.
1715 * (2). Current time - last queue scan time > allowed cache time.
1716 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1717 * This last test picks up machines for which the clock has been moved
1718 * forward, an lpq scan done and then the clock moved back. Otherwise
1719 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1722 if (last_qscan_time == ((time_t)-1) || (time_now - last_qscan_time) >= lp_lpqcachetime() ||
1723 last_qscan_time > (time_now + MAX_CACHE_VALID_TIME)) {
1724 DEBUG(3, ("print cache expired for queue %s \
1725 (last_qscan_time = %d, time now = %d, qcachetime = %d)\n", printername,
1726 (int)last_qscan_time, (int)time_now, (int)lp_lpqcachetime() ));
1727 release_print_db(pdb);
1730 release_print_db(pdb);
1734 /****************************************************************************
1735 Get the queue status - do not update if db is out of date.
1736 ****************************************************************************/
1738 static int get_queue_status(int snum, print_status_struct *status)
1742 const char *printername = lp_const_servicename(snum);
1743 struct tdb_print_db *pdb = get_print_db_byname(printername);
1750 ZERO_STRUCTP(status);
1751 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
1753 key.dsize = strlen(keystr);
1754 data = tdb_fetch(pdb->tdb, key);
1756 if (data.dsize == sizeof(print_status_struct))
1757 memcpy(status, data.dptr, sizeof(print_status_struct));
1758 SAFE_FREE(data.dptr);
1761 len = tdb_fetch_int32(pdb->tdb, "INFO/total_jobs");
1762 release_print_db(pdb);
1763 return (len == -1 ? 0 : len);
1766 /****************************************************************************
1767 Determine the number of jobs in a queue.
1768 ****************************************************************************/
1770 int print_queue_length(int snum, print_status_struct *pstatus)
1772 print_status_struct status;
1775 /* make sure the database is up to date */
1776 if (print_cache_expired(snum))
1777 print_queue_update(snum);
1779 /* also fetch the queue status */
1780 memset(&status, 0, sizeof(status));
1781 len = get_queue_status(snum, &status);
1789 /***************************************************************************
1790 Allocate a jobid. Hold the lock for as short a time as possible.
1791 ***************************************************************************/
1793 static BOOL allocate_print_jobid(struct tdb_print_db *pdb, int snum, const char *printername, uint32 *pjobid)
1798 *pjobid = (uint32)-1;
1800 for (i = 0; i < 3; i++) {
1801 /* Lock the database - only wait 20 seconds. */
1802 if (tdb_lock_bystring(pdb->tdb, "INFO/nextjob", 20) == -1) {
1803 DEBUG(0,("allocate_print_jobid: failed to lock printing database %s\n", printername ));
1807 if (!tdb_fetch_uint32(pdb->tdb, "INFO/nextjob", &jobid)) {
1808 if (tdb_error(pdb->tdb) != TDB_ERR_NOEXIST) {
1809 DEBUG(0, ("allocate_print_jobid: failed to fetch INFO/nextjob for print queue %s\n",
1816 jobid = NEXT_JOBID(jobid);
1818 if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
1819 DEBUG(3, ("allocate_print_jobid: failed to store INFO/nextjob.\n"));
1820 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1824 /* We've finished with the INFO/nextjob lock. */
1825 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1827 if (!print_job_exists(snum, jobid))
1832 DEBUG(0, ("allocate_print_jobid: failed to allocate a print job for queue %s\n",
1834 /* Probably full... */
1839 /* Store a dummy placeholder. */
1844 if (tdb_store(pdb->tdb, print_key(jobid), dum, TDB_INSERT) == -1) {
1845 DEBUG(3, ("allocate_print_jobid: jobid (%d) failed to store placeholder.\n",
1855 /***************************************************************************
1856 Append a jobid to the 'jobs changed' list.
1857 ***************************************************************************/
1859 static BOOL add_to_jobs_changed(struct tdb_print_db *pdb, uint32 jobid)
1863 key.dptr = "INFO/jobs_changed";
1864 key.dsize = strlen(key.dptr);
1865 data.dptr = (char *)&jobid;
1868 DEBUG(10,("add_to_jobs_changed: Added jobid %u\n", (unsigned int)jobid ));
1870 return (tdb_append(pdb->tdb, key, data) == 0);
1873 /***************************************************************************
1874 Start spooling a job - return the jobid.
1875 ***************************************************************************/
1877 uint32 print_job_start(struct current_user *user, int snum, char *jobname, NT_DEVICEMODE *nt_devmode )
1881 struct printjob pjob;
1883 const char *printername = lp_const_servicename(snum);
1884 struct tdb_print_db *pdb = get_print_db_byname(printername);
1892 if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
1893 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
1894 release_print_db(pdb);
1898 if (!print_time_access_check(snum)) {
1899 DEBUG(3, ("print_job_start: job start denied by time check\n"));
1900 release_print_db(pdb);
1904 path = lp_pathname(snum);
1906 /* see if we have sufficient disk space */
1907 if (lp_minprintspace(snum)) {
1908 SMB_BIG_UINT dspace, dsize;
1909 if (sys_fsusage(path, &dspace, &dsize) == 0 &&
1910 dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
1911 DEBUG(3, ("print_job_start: disk space check failed.\n"));
1912 release_print_db(pdb);
1918 /* for autoloaded printers, check that the printcap entry still exists */
1919 if (lp_autoloaded(snum) && !pcap_printername_ok(lp_const_servicename(snum), NULL)) {
1920 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum) ));
1921 release_print_db(pdb);
1926 /* Insure the maximum queue size is not violated */
1927 if ((njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
1928 DEBUG(3, ("print_job_start: Queue %s number of jobs (%d) larger than max printjobs per queue (%d).\n",
1929 printername, njobs, lp_maxprintjobs(snum) ));
1930 release_print_db(pdb);
1935 DEBUG(10,("print_job_start: Queue %s number of jobs (%d), max printjobs = %d\n",
1936 printername, njobs, lp_maxprintjobs(snum) ));
1938 if (!allocate_print_jobid(pdb, snum, printername, &jobid))
1941 /* create the database entry */
1945 pjob.pid = local_pid;
1948 pjob.starttime = time(NULL);
1949 pjob.status = LPQ_SPOOLING;
1951 pjob.spooled = False;
1953 pjob.nt_devmode = nt_devmode;
1955 fstrcpy(pjob.jobname, jobname);
1957 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1958 fstrcpy(pjob.user, vuser->user.smb_name);
1960 fstrcpy(pjob.user, uidtoname(user->uid));
1963 fstrcpy(pjob.queuename, lp_const_servicename(snum));
1965 /* we have a job entry - now create the spool file */
1966 slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.8u.XXXXXX",
1967 path, PRINT_SPOOL_PREFIX, (unsigned int)jobid);
1968 pjob.fd = smb_mkstemp(pjob.filename);
1970 if (pjob.fd == -1) {
1971 if (errno == EACCES) {
1972 /* Common setup error, force a report. */
1973 DEBUG(0, ("print_job_start: insufficient permissions \
1974 to open spool file %s.\n", pjob.filename));
1976 /* Normal case, report at level 3 and above. */
1977 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
1978 DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
1983 pjob_store(snum, jobid, &pjob);
1985 /* Update the 'jobs changed' entry used by print_queue_status. */
1986 add_to_jobs_changed(pdb, jobid);
1988 /* Ensure we keep a rough count of the number of total jobs... */
1989 tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, 1);
1991 release_print_db(pdb);
1997 pjob_delete(snum, jobid);
1999 release_print_db(pdb);
2001 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
2005 /****************************************************************************
2006 Update the number of pages spooled to jobid
2007 ****************************************************************************/
2009 void print_job_endpage(int snum, uint32 jobid)
2011 struct printjob *pjob = print_job_find(snum, jobid);
2014 /* don't allow another process to get this info - it is meaningless */
2015 if (pjob->pid != local_pid)
2019 pjob_store(snum, jobid, pjob);
2022 /****************************************************************************
2023 Print a file - called on closing the file. This spools the job.
2024 If normal close is false then we're tearing down the jobs - treat as an
2026 ****************************************************************************/
2028 BOOL print_job_end(int snum, uint32 jobid, BOOL normal_close)
2030 struct printjob *pjob = print_job_find(snum, jobid);
2032 SMB_STRUCT_STAT sbuf;
2037 if (pjob->spooled || pjob->pid != local_pid)
2040 if (normal_close && (sys_fstat(pjob->fd, &sbuf) == 0)) {
2041 pjob->size = sbuf.st_size;
2047 * Not a normal close or we couldn't stat the job file,
2048 * so something has gone wrong. Cleanup.
2052 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
2056 /* Technically, this is not quite right. If the printer has a separator
2057 * page turned on, the NT spooler prints the separator page even if the
2058 * print job is 0 bytes. 010215 JRR */
2059 if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
2060 /* don't bother spooling empty files or something being deleted. */
2061 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
2062 pjob->filename, pjob->size ? "deleted" : "zero length" ));
2063 unlink(pjob->filename);
2064 pjob_delete(snum, jobid);
2068 pjob->smbjob = jobid;
2070 ret = (*(current_printif->job_submit))(snum, pjob);
2075 /* The print job has been sucessfully handed over to the back-end */
2077 pjob->spooled = True;
2078 pjob->status = LPQ_QUEUED;
2079 pjob_store(snum, jobid, pjob);
2081 /* make sure the database is up to date */
2082 if (print_cache_expired(snum))
2083 print_queue_update(snum);
2089 /* The print job was not succesfully started. Cleanup */
2090 /* Still need to add proper error return propagation! 010122:JRR */
2091 unlink(pjob->filename);
2092 pjob_delete(snum, jobid);
2093 remove_from_jobs_changed(snum, jobid);
2097 /****************************************************************************
2098 Get a snapshot of jobs in the system without traversing.
2099 ****************************************************************************/
2101 static BOOL get_stored_queue_info(struct tdb_print_db *pdb, int snum, int *pcount, print_queue_struct **ppqueue)
2103 TDB_DATA data, key, cgdata;
2104 print_queue_struct *queue = NULL;
2106 uint32 extra_count = 0;
2107 int total_count = 0;
2110 int max_reported_jobs = lp_max_reported_jobs(snum);
2113 /* make sure the database is up to date */
2114 if (print_cache_expired(snum))
2115 print_queue_update(snum);
2121 ZERO_STRUCT(cgdata);
2122 key.dptr = "INFO/linear_queue_array";
2123 key.dsize = strlen(key.dptr);
2125 /* Get the stored queue data. */
2126 data = tdb_fetch(pdb->tdb, key);
2128 if (data.dptr == NULL || data.dsize < 4)
2131 memcpy(&qcount, data.dptr, 4);
2133 /* Get the changed jobs list. */
2134 key.dptr = "INFO/jobs_changed";
2135 key.dsize = strlen(key.dptr);
2137 cgdata = tdb_fetch(pdb->tdb, key);
2138 if (cgdata.dptr != NULL && (cgdata.dsize % 4 == 0))
2139 extra_count = cgdata.dsize/4;
2141 DEBUG(5,("get_stored_queue_info: qcount = %u, extra_count = %u\n", (unsigned int)qcount, (unsigned int)extra_count));
2143 /* Allocate the queue size. */
2144 if (qcount == 0 && extra_count == 0)
2147 if ((queue = (print_queue_struct *)malloc(sizeof(print_queue_struct)*(qcount + extra_count))) == NULL)
2150 /* Retrieve the linearised queue data. */
2152 for( i = 0; i < qcount; i++) {
2153 uint32 qjob, qsize, qpage_count, qstatus, qpriority, qtime;
2154 len += tdb_unpack(data.dptr + 4 + len, data.dsize - len, "ddddddff",
2163 queue[i].job = qjob;
2164 queue[i].size = qsize;
2165 queue[i].page_count = qpage_count;
2166 queue[i].status = qstatus;
2167 queue[i].priority = qpriority;
2168 queue[i].time = qtime;
2171 total_count = qcount;
2173 /* Add in the changed jobids. */
2174 for( i = 0; i < extra_count; i++) {
2176 struct printjob *pjob;
2178 memcpy(&jobid, &cgdata.dptr[i*4], 4);
2179 DEBUG(5,("get_stored_queue_info: changed job = %u\n", (unsigned int)jobid));
2180 pjob = print_job_find(snum, jobid);
2182 DEBUG(5,("get_stored_queue_info: failed to find changed job = %u\n", (unsigned int)jobid));
2183 remove_from_jobs_changed(snum, jobid);
2187 queue[total_count].job = jobid;
2188 queue[total_count].size = pjob->size;
2189 queue[total_count].page_count = pjob->page_count;
2190 queue[total_count].status = pjob->status;
2191 queue[total_count].priority = 1;
2192 queue[total_count].time = pjob->starttime;
2193 fstrcpy(queue[total_count].fs_user, pjob->user);
2194 fstrcpy(queue[total_count].fs_file, pjob->jobname);
2198 /* Sort the queue by submission time otherwise they are displayed
2201 qsort(queue, total_count, sizeof(print_queue_struct), QSORT_CAST(printjob_comp));
2203 DEBUG(5,("get_stored_queue_info: total_count = %u\n", (unsigned int)total_count));
2205 if (max_reported_jobs && total_count > max_reported_jobs)
2206 total_count = max_reported_jobs;
2209 *pcount = total_count;
2215 SAFE_FREE(data.dptr);
2216 SAFE_FREE(cgdata.dptr);
2220 /****************************************************************************
2221 Get a printer queue listing.
2222 set queue = NULL and status = NULL if you just want to update the cache
2223 ****************************************************************************/
2225 int print_queue_status(int snum,
2226 print_queue_struct **ppqueue,
2227 print_status_struct *status)
2231 const char *printername;
2232 struct tdb_print_db *pdb;
2235 /* make sure the database is up to date */
2237 if (print_cache_expired(snum))
2238 print_queue_update(snum);
2240 /* return if we are done */
2241 if ( !ppqueue || !status )
2245 printername = lp_const_servicename(snum);
2246 pdb = get_print_db_byname(printername);
2252 * Fetch the queue status. We must do this first, as there may
2253 * be no jobs in the queue.
2256 ZERO_STRUCTP(status);
2257 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
2259 key.dsize = strlen(keystr);
2260 data = tdb_fetch(pdb->tdb, key);
2262 if (data.dsize == sizeof(*status)) {
2263 memcpy(status, data.dptr, sizeof(*status));
2265 SAFE_FREE(data.dptr);
2269 * Now, fetch the print queue information. We first count the number
2270 * of entries, and then only retrieve the queue if necessary.
2273 if (!get_stored_queue_info(pdb, snum, &count, ppqueue)) {
2274 release_print_db(pdb);
2278 release_print_db(pdb);
2282 /****************************************************************************
2284 ****************************************************************************/
2286 BOOL print_queue_pause(struct current_user *user, int snum, WERROR *errcode)
2290 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2291 *errcode = WERR_ACCESS_DENIED;
2295 ret = (*(current_printif->queue_pause))(snum);
2298 *errcode = WERR_INVALID_PARAM;
2302 /* force update the database */
2303 print_cache_flush(snum);
2305 /* Send a printer notify message */
2307 notify_printer_status(snum, PRINTER_STATUS_PAUSED);
2312 /****************************************************************************
2314 ****************************************************************************/
2316 BOOL print_queue_resume(struct current_user *user, int snum, WERROR *errcode)
2320 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2321 *errcode = WERR_ACCESS_DENIED;
2325 ret = (*(current_printif->queue_resume))(snum);
2328 *errcode = WERR_INVALID_PARAM;
2332 /* make sure the database is up to date */
2333 if (print_cache_expired(snum))
2334 print_queue_update(snum);
2336 /* Send a printer notify message */
2338 notify_printer_status(snum, PRINTER_STATUS_OK);
2343 /****************************************************************************
2344 Purge a queue - implemented by deleting all jobs that we can delete.
2345 ****************************************************************************/
2347 BOOL print_queue_purge(struct current_user *user, int snum, WERROR *errcode)
2349 print_queue_struct *queue;
2350 print_status_struct status;
2354 /* Force and update so the count is accurate (i.e. not a cached count) */
2355 print_queue_update(snum);
2357 can_job_admin = print_access_check(user, snum, JOB_ACCESS_ADMINISTER);
2358 njobs = print_queue_status(snum, &queue, &status);
2360 for (i=0;i<njobs;i++) {
2361 BOOL owner = is_owner(user, snum, queue[i].job);
2363 if (owner || can_job_admin) {
2364 print_job_delete1(snum, queue[i].job);