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 pj.smbjob = (old_pj != NULL ? True : False);
617 fstrcpy(pj.filename, old_pj ? old_pj->filename : "");
618 if (jobid < UNIX_JOB_START)
619 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : "Remote Downlevel Document");
621 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : q->fs_file);
622 fstrcpy(pj.user, old_pj ? old_pj->user : q->fs_user);
623 fstrcpy(pj.queuename, old_pj ? old_pj->queuename : lp_const_servicename(snum));
625 pjob_store(snum, jobid, &pj);
629 struct traverse_struct {
630 print_queue_struct *queue;
631 int qcount, snum, maxcount, total_jobs;
635 /****************************************************************************
636 Utility fn to delete any jobs that are no longer active.
637 ****************************************************************************/
639 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
641 struct traverse_struct *ts = (struct traverse_struct *)state;
642 struct printjob pjob;
646 if ( key.dsize != sizeof(jobid) )
649 memcpy(&jobid, key.dptr, sizeof(jobid));
650 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
652 free_nt_devicemode( &pjob.nt_devmode );
655 if (ts->snum != lp_servicenumber(pjob.queuename)) {
656 /* this isn't for the queue we are looking at - this cannot happen with the split tdb's. JRA */
661 /* remove a unix job if it isn't in the system queue any more */
663 for (i=0;i<ts->qcount;i++) {
664 uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
665 if (jobid == u_jobid)
668 if (i == ts->qcount) {
669 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !smbjob\n",
670 (unsigned int)jobid ));
671 pjob_delete(ts->snum, jobid);
677 /* maybe it hasn't been spooled yet */
679 /* if a job is not spooled and the process doesn't
680 exist then kill it. This cleans up after smbd
682 if (!process_exists(pjob.pid)) {
683 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !process_exists (%u)\n",
684 (unsigned int)jobid, (unsigned int)pjob.pid ));
685 pjob_delete(ts->snum, jobid);
691 for (i=0;i<ts->qcount;i++) {
692 uint32 curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
693 if (jobid == curr_jobid)
697 /* The job isn't in the system queue - we have to assume it has
698 completed, so delete the database entry. */
700 if (i == ts->qcount) {
702 /* A race can occur between the time a job is spooled and
703 when it appears in the lpq output. This happens when
704 the job is added to printing.tdb when another smbd
705 running print_queue_update() has completed a lpq and
706 is currently traversing the printing tdb and deleting jobs.
707 Don't delete the job if it was submitted after the lpq_time. */
709 if (pjob.starttime < ts->lpq_time) {
710 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to pjob.starttime (%u) < ts->lpq_time (%u)\n",
712 (unsigned int)pjob.starttime,
713 (unsigned int)ts->lpq_time ));
714 pjob_delete(ts->snum, jobid);
720 /* Save the pjob attributes we will store. */
721 ts->queue[i].job = jobid;
722 ts->queue[i].size = pjob.size;
723 ts->queue[i].page_count = pjob.page_count;
724 ts->queue[i].status = pjob.status;
725 ts->queue[i].priority = 1;
726 ts->queue[i].time = pjob.starttime;
727 fstrcpy(ts->queue[i].fs_user, pjob.user);
728 fstrcpy(ts->queue[i].fs_file, pjob.jobname);
735 /****************************************************************************
736 Check if the print queue has been updated recently enough.
737 ****************************************************************************/
739 static void print_cache_flush(int snum)
742 const char *printername = lp_const_servicename(snum);
743 struct tdb_print_db *pdb = get_print_db_byname(printername);
747 slprintf(key, sizeof(key)-1, "CACHE/%s", printername);
748 tdb_store_int32(pdb->tdb, key, -1);
749 release_print_db(pdb);
752 /****************************************************************************
753 Check if someone already thinks they are doing the update.
754 ****************************************************************************/
756 static pid_t get_updating_pid(fstring printer_name)
761 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
765 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
767 key.dsize = strlen(keystr);
769 data = tdb_fetch(pdb->tdb, key);
770 release_print_db(pdb);
771 if (!data.dptr || data.dsize != sizeof(pid_t)) {
772 SAFE_FREE(data.dptr);
776 memcpy(&updating_pid, data.dptr, sizeof(pid_t));
777 SAFE_FREE(data.dptr);
779 if (process_exists(updating_pid))
785 /****************************************************************************
786 Set the fact that we're doing the update, or have finished doing the update
788 ****************************************************************************/
790 static void set_updating_pid(const fstring printer_name, BOOL delete)
795 pid_t updating_pid = sys_getpid();
796 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
801 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
803 key.dsize = strlen(keystr);
806 tdb_delete(pdb->tdb, key);
807 release_print_db(pdb);
811 data.dptr = (void *)&updating_pid;
812 data.dsize = sizeof(pid_t);
814 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
815 release_print_db(pdb);
818 /****************************************************************************
819 Sort print jobs by submittal time.
820 ****************************************************************************/
822 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
833 /* Sort on job start time */
835 if (j1->time == j2->time)
837 return (j1->time > j2->time) ? 1 : -1;
840 /****************************************************************************
841 Store the sorted queue representation for later portmon retrieval.
842 ****************************************************************************/
844 static void store_queue_struct(struct tdb_print_db *pdb, struct traverse_struct *pts)
847 int max_reported_jobs = lp_max_reported_jobs(pts->snum);
848 print_queue_struct *queue = pts->queue;
853 if (max_reported_jobs < pts->qcount)
854 pts->qcount = max_reported_jobs;
855 qcount = pts->qcount;
857 /* Work out the size. */
859 data.dsize += tdb_pack(NULL, 0, "d", qcount);
861 for (i = 0; i < pts->qcount; i++) {
862 data.dsize += tdb_pack(NULL, 0, "ddddddff",
863 (uint32)queue[i].job,
864 (uint32)queue[i].size,
865 (uint32)queue[i].page_count,
866 (uint32)queue[i].status,
867 (uint32)queue[i].priority,
868 (uint32)queue[i].time,
873 if ((data.dptr = malloc(data.dsize)) == NULL)
877 len += tdb_pack(data.dptr + len, data.dsize - len, "d", qcount);
878 for (i = 0; i < pts->qcount; i++) {
879 len += tdb_pack(data.dptr + len, data.dsize - len, "ddddddff",
880 (uint32)queue[i].job,
881 (uint32)queue[i].size,
882 (uint32)queue[i].page_count,
883 (uint32)queue[i].status,
884 (uint32)queue[i].priority,
885 (uint32)queue[i].time,
890 key.dptr = "INFO/linear_queue_array";
891 key.dsize = strlen(key.dptr);
892 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
893 SAFE_FREE(data.dptr);
897 static TDB_DATA get_jobs_changed_data(struct tdb_print_db *pdb)
901 key.dptr = "INFO/jobs_changed";
902 key.dsize = strlen(key.dptr);
905 data = tdb_fetch(pdb->tdb, key);
906 if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0)) {
907 SAFE_FREE(data.dptr);
914 static void check_job_changed(int snum, TDB_DATA data, uint32 jobid)
917 unsigned int job_count = data.dsize / 4;
919 for (i = 0; i < job_count; i++) {
922 memcpy(&ch_jobid, data.dptr + (i*4), 4);
923 if (ch_jobid == jobid)
924 remove_from_jobs_changed(snum, jobid);
928 /****************************************************************************
929 Update the internal database from the system print queue for a queue.
930 ****************************************************************************/
932 static void print_queue_update(int snum)
935 print_queue_struct *queue = NULL;
936 print_status_struct status;
937 print_status_struct old_status;
938 struct printjob *pjob;
939 struct traverse_struct tstruct;
940 fstring keystr, printer_name, cachestr;
943 struct tdb_print_db *pdb;
945 fstrcpy(printer_name, lp_const_servicename(snum));
946 pdb = get_print_db_byname(printer_name);
951 * Check to see if someone else is doing this update.
952 * This is essentially a mutex on the update.
955 if (get_updating_pid(printer_name) != -1) {
956 release_print_db(pdb);
960 /* Lock the queue for the database update */
962 slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", printer_name);
963 /* Only wait 10 seconds for this. */
964 if (tdb_lock_bystring(pdb->tdb, keystr, 10) == -1) {
965 DEBUG(0,("print_queue_update: Failed to lock printer %s database\n", printer_name));
966 release_print_db(pdb);
971 * Ensure that no one else got in here.
972 * If the updating pid is still -1 then we are
976 if (get_updating_pid(printer_name) != -1) {
978 * Someone else is doing the update, exit.
980 tdb_unlock_bystring(pdb->tdb, keystr);
981 release_print_db(pdb);
986 * We're going to do the update ourselves.
989 /* Tell others we're doing the update. */
990 set_updating_pid(printer_name, False);
993 * Allow others to enter and notice we're doing
997 tdb_unlock_bystring(pdb->tdb, keystr);
1000 * Update the cache time FIRST ! Stops others even
1001 * attempting to get the lock and doing this
1002 * if the lpq takes a long time.
1005 slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", printer_name);
1006 tdb_store_int32(pdb->tdb, cachestr, (int)time(NULL));
1008 /* get the current queue using the appropriate interface */
1009 ZERO_STRUCT(status);
1011 qcount = (*(current_printif->queue_get))(snum, &queue, &status);
1013 DEBUG(3, ("%d job%s in queue for %s\n", qcount, (qcount != 1) ?
1014 "s" : "", printer_name));
1016 /* Sort the queue by submission time otherwise they are displayed
1019 qsort(queue, qcount, sizeof(print_queue_struct),
1020 QSORT_CAST(printjob_comp));
1023 any job in the internal database that is marked as spooled
1024 and doesn't exist in the system queue is considered finished
1025 and removed from the database
1027 any job in the system database but not in the internal database
1028 is added as a unix job
1030 fill in any system job numbers as we go
1033 jcdata = get_jobs_changed_data(pdb);
1035 for (i=0; i<qcount; i++) {
1036 uint32 jobid = print_parse_jobid(queue[i].fs_file);
1038 if (jobid == (uint32)-1) {
1039 /* assume its a unix print job */
1040 print_unix_job(snum, &queue[i], jobid);
1044 /* we have an active SMB print job - update its status */
1045 pjob = print_job_find(snum, jobid);
1047 /* err, somethings wrong. Probably smbd was restarted
1048 with jobs in the queue. All we can do is treat them
1049 like unix jobs. Pity. */
1050 print_unix_job(snum, &queue[i], jobid);
1054 pjob->sysjob = queue[i].job;
1055 pjob->status = queue[i].status;
1056 pjob_store(snum, jobid, pjob);
1057 check_job_changed(snum, jcdata, jobid);
1060 SAFE_FREE(jcdata.dptr);
1062 /* now delete any queued entries that don't appear in the
1064 tstruct.queue = queue;
1065 tstruct.qcount = qcount;
1066 tstruct.snum = snum;
1067 tstruct.total_jobs = 0;
1068 tstruct.lpq_time = time(NULL);
1070 tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
1072 /* Store the linearised queue, max jobs only. */
1073 store_queue_struct(pdb, &tstruct);
1075 SAFE_FREE(tstruct.queue);
1077 DEBUG(10,("print_queue_update: printer %s INFO/total_jobs = %d\n",
1078 printer_name, tstruct.total_jobs ));
1080 tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
1082 get_queue_status(snum, &old_status);
1083 if (old_status.qcount != qcount)
1084 DEBUG(10,("print_queue_update: queue status change %d jobs -> %d jobs for printer %s\n",
1085 old_status.qcount, qcount, printer_name ));
1087 /* store the new queue status structure */
1088 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printer_name);
1090 key.dsize = strlen(keystr);
1092 status.qcount = qcount;
1093 data.dptr = (void *)&status;
1094 data.dsize = sizeof(status);
1095 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
1098 * Update the cache time again. We want to do this call
1099 * as little as possible...
1102 slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", printer_name);
1103 tdb_store_int32(pdb->tdb, keystr, (int32)time(NULL));
1105 /* Delete our pid from the db. */
1106 set_updating_pid(printer_name, True);
1107 release_print_db(pdb);
1110 /****************************************************************************
1111 Create/Update an entry in the print tdb that will allow us to send notify
1112 updates only to interested smbd's.
1113 ****************************************************************************/
1115 BOOL print_notify_register_pid(int snum)
1118 struct tdb_print_db *pdb = NULL;
1119 TDB_CONTEXT *tdb = NULL;
1120 const char *printername;
1121 uint32 mypid = (uint32)sys_getpid();
1125 /* if (snum == -1), then the change notify request was
1126 on a print server handle and we need to register on
1131 int num_services = lp_numservices();
1134 for ( idx=0; idx<num_services; idx++ ) {
1135 if (lp_snum_ok(idx) && lp_print_ok(idx) )
1136 print_notify_register_pid(idx);
1141 else /* register for a specific printer */
1143 printername = lp_const_servicename(snum);
1144 pdb = get_print_db_byname(printername);
1150 if (tdb_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1151 DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n",
1154 release_print_db(pdb);
1158 data = get_printer_notify_pid_list( tdb, printername, True );
1160 /* Add ourselves and increase the refcount. */
1162 for (i = 0; i < data.dsize; i += 8) {
1163 if (IVAL(data.dptr,i) == mypid) {
1164 uint32 new_refcount = IVAL(data.dptr, i+4) + 1;
1165 SIVAL(data.dptr, i+4, new_refcount);
1170 if (i == data.dsize) {
1171 /* We weren't in the list. Realloc. */
1172 data.dptr = Realloc(data.dptr, data.dsize + 8);
1174 DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n",
1179 SIVAL(data.dptr,data.dsize - 8,mypid);
1180 SIVAL(data.dptr,data.dsize - 4,1); /* Refcount. */
1183 /* Store back the record. */
1184 if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1185 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1186 list for printer %s\n", printername));
1194 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1196 release_print_db(pdb);
1197 SAFE_FREE(data.dptr);
1201 /****************************************************************************
1202 Update an entry in the print tdb that will allow us to send notify
1203 updates only to interested smbd's.
1204 ****************************************************************************/
1206 BOOL print_notify_deregister_pid(int snum)
1209 struct tdb_print_db *pdb = NULL;
1210 TDB_CONTEXT *tdb = NULL;
1211 const char *printername;
1212 uint32 mypid = (uint32)sys_getpid();
1216 /* if ( snum == -1 ), we are deregister a print server handle
1217 which means to deregister on all print queues */
1221 int num_services = lp_numservices();
1224 for ( idx=0; idx<num_services; idx++ ) {
1225 if ( lp_snum_ok(idx) && lp_print_ok(idx) )
1226 print_notify_deregister_pid(idx);
1231 else /* deregister a specific printer */
1233 printername = lp_const_servicename(snum);
1234 pdb = get_print_db_byname(printername);
1240 if (tdb_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1241 DEBUG(0,("print_notify_register_pid: Failed to lock \
1242 printer %s database\n", printername));
1244 release_print_db(pdb);
1248 data = get_printer_notify_pid_list( tdb, printername, True );
1250 /* Reduce refcount. Remove ourselves if zero. */
1252 for (i = 0; i < data.dsize; ) {
1253 if (IVAL(data.dptr,i) == mypid) {
1254 uint32 refcount = IVAL(data.dptr, i+4);
1258 if (refcount == 0) {
1259 if (data.dsize - i > 8)
1260 memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
1264 SIVAL(data.dptr, i+4, refcount);
1270 if (data.dsize == 0)
1271 SAFE_FREE(data.dptr);
1273 /* Store back the record. */
1274 if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1275 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1276 list for printer %s\n", printername));
1284 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1286 release_print_db(pdb);
1287 SAFE_FREE(data.dptr);
1291 /****************************************************************************
1292 Check if a jobid is valid. It is valid if it exists in the database.
1293 ****************************************************************************/
1295 BOOL print_job_exists(int snum, uint32 jobid)
1297 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
1302 ret = tdb_exists(pdb->tdb, print_key(jobid));
1303 release_print_db(pdb);
1307 /****************************************************************************
1308 Give the fd used for a jobid.
1309 ****************************************************************************/
1311 int print_job_fd(int snum, uint32 jobid)
1313 struct printjob *pjob = print_job_find(snum, jobid);
1316 /* don't allow another process to get this info - it is meaningless */
1317 if (pjob->pid != local_pid)
1322 /****************************************************************************
1323 Give the filename used for a jobid.
1324 Only valid for the process doing the spooling and when the job
1325 has not been spooled.
1326 ****************************************************************************/
1328 char *print_job_fname(int snum, uint32 jobid)
1330 struct printjob *pjob = print_job_find(snum, jobid);
1331 if (!pjob || pjob->spooled || pjob->pid != local_pid)
1333 return pjob->filename;
1337 /****************************************************************************
1338 Give the filename used for a jobid.
1339 Only valid for the process doing the spooling and when the job
1340 has not been spooled.
1341 ****************************************************************************/
1343 NT_DEVICEMODE *print_job_devmode(int snum, uint32 jobid)
1345 struct printjob *pjob = print_job_find(snum, jobid);
1350 return pjob->nt_devmode;
1353 /****************************************************************************
1354 Set the place in the queue for a job.
1355 ****************************************************************************/
1357 BOOL print_job_set_place(int snum, uint32 jobid, int place)
1359 DEBUG(2,("print_job_set_place not implemented yet\n"));
1363 /****************************************************************************
1364 Set the name of a job. Only possible for owner.
1365 ****************************************************************************/
1367 BOOL print_job_set_name(int snum, uint32 jobid, char *name)
1369 struct printjob *pjob = print_job_find(snum, jobid);
1370 if (!pjob || pjob->pid != local_pid)
1373 fstrcpy(pjob->jobname, name);
1374 return pjob_store(snum, jobid, pjob);
1377 /***************************************************************************
1378 Remove a jobid from the 'jobs changed' list.
1379 ***************************************************************************/
1381 static BOOL remove_from_jobs_changed(int snum, uint32 jobid)
1383 const char *printername = lp_const_servicename(snum);
1384 struct tdb_print_db *pdb = get_print_db_byname(printername);
1386 size_t job_count, i;
1388 BOOL gotlock = False;
1390 key.dptr = "INFO/jobs_changed";
1391 key.dsize = strlen(key.dptr);
1394 if (tdb_chainlock_with_timeout(pdb->tdb, key, 5) == -1)
1399 data = tdb_fetch(pdb->tdb, key);
1401 if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0))
1404 job_count = data.dsize / 4;
1405 for (i = 0; i < job_count; i++) {
1408 memcpy(&ch_jobid, data.dptr + (i*4), 4);
1409 if (ch_jobid == jobid) {
1410 if (i < job_count -1 )
1411 memmove(data.dptr + (i*4), data.dptr + (i*4) + 4, (job_count - i - 1)*4 );
1413 if (tdb_store(pdb->tdb, key, data, TDB_REPLACE) == -1)
1423 tdb_chainunlock(pdb->tdb, key);
1424 SAFE_FREE(data.dptr);
1425 release_print_db(pdb);
1427 DEBUG(10,("remove_from_jobs_changed: removed jobid %u\n", (unsigned int)jobid ));
1429 DEBUG(10,("remove_from_jobs_changed: Failed to remove jobid %u\n", (unsigned int)jobid ));
1433 /****************************************************************************
1434 Delete a print job - don't update queue.
1435 ****************************************************************************/
1437 static BOOL print_job_delete1(int snum, uint32 jobid)
1439 struct printjob *pjob = print_job_find(snum, jobid);
1446 * If already deleting just return.
1449 if (pjob->status == LPQ_DELETING)
1452 /* Hrm - we need to be able to cope with deleting a job before it
1453 has reached the spooler. */
1455 if (pjob->sysjob == -1) {
1456 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid));
1459 /* Set the tdb entry to be deleting. */
1461 pjob->status = LPQ_DELETING;
1462 pjob_store(snum, jobid, pjob);
1464 if (pjob->spooled && pjob->sysjob != -1)
1465 result = (*(current_printif->job_delete))(snum, pjob);
1467 remove_from_jobs_changed(snum, jobid);
1469 /* Delete the tdb entry if the delete succeeded or the job hasn't
1473 const char *printername = lp_const_servicename(snum);
1474 struct tdb_print_db *pdb = get_print_db_byname(printername);
1479 pjob_delete(snum, jobid);
1480 /* Ensure we keep a rough count of the number of total jobs... */
1481 tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, -1);
1482 release_print_db(pdb);
1485 return (result == 0);
1488 /****************************************************************************
1489 Return true if the current user owns the print job.
1490 ****************************************************************************/
1492 static BOOL is_owner(struct current_user *user, int snum, uint32 jobid)
1494 struct printjob *pjob = print_job_find(snum, jobid);
1500 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1501 return strequal(pjob->user, vuser->user.smb_name);
1503 return strequal(pjob->user, uidtoname(user->uid));
1507 /****************************************************************************
1509 ****************************************************************************/
1511 BOOL print_job_delete(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1513 BOOL owner, deleted;
1518 owner = is_owner(user, snum, jobid);
1520 /* Check access against security descriptor or whether the user
1524 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1525 DEBUG(3, ("delete denied by security descriptor\n"));
1526 *errcode = WERR_ACCESS_DENIED;
1528 /* BEGIN_ADMIN_LOG */
1529 sys_adminlog( LOG_ERR,
1530 "Permission denied-- user not allowed to delete, \
1531 pause, or resume print job. User name: %s. Printer name: %s.",
1532 uidtoname(user->uid), PRINTERNAME(snum) );
1539 * get the spooled filename of the print job
1540 * if this works, then the file has not been spooled
1541 * to the underlying print system. Just delete the
1542 * spool file & return.
1545 if ( (fname = print_job_fname( snum, jobid )) != NULL )
1547 /* remove the spool file */
1548 DEBUG(10,("print_job_delete: Removing spool file [%s]\n", fname ));
1549 if ( unlink( fname ) == -1 ) {
1550 *errcode = map_werror_from_unix(errno);
1557 if (!print_job_delete1(snum, jobid)) {
1558 *errcode = WERR_ACCESS_DENIED;
1562 /* force update the database and say the delete failed if the
1565 print_queue_update(snum);
1567 deleted = !print_job_exists(snum, jobid);
1569 *errcode = WERR_ACCESS_DENIED;
1574 /****************************************************************************
1576 ****************************************************************************/
1578 BOOL print_job_pause(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1580 struct printjob *pjob = print_job_find(snum, jobid);
1586 if (!pjob->spooled || pjob->sysjob == -1)
1589 if (!is_owner(user, snum, jobid) &&
1590 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1591 DEBUG(3, ("pause denied by security descriptor\n"));
1593 /* BEGIN_ADMIN_LOG */
1594 sys_adminlog( LOG_ERR,
1595 "Permission denied-- user not allowed to delete, \
1596 pause, or resume print job. User name: %s. Printer name: %s.",
1597 uidtoname(user->uid), PRINTERNAME(snum) );
1600 *errcode = WERR_ACCESS_DENIED;
1604 /* need to pause the spooled entry */
1605 ret = (*(current_printif->job_pause))(snum, pjob);
1608 *errcode = WERR_INVALID_PARAM;
1612 /* force update the database */
1613 print_cache_flush(snum);
1615 /* Send a printer notify message */
1617 notify_job_status(snum, jobid, JOB_STATUS_PAUSED);
1619 /* how do we tell if this succeeded? */
1624 /****************************************************************************
1626 ****************************************************************************/
1628 BOOL print_job_resume(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1630 struct printjob *pjob = print_job_find(snum, jobid);
1636 if (!pjob->spooled || pjob->sysjob == -1)
1639 if (!is_owner(user, snum, jobid) &&
1640 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1641 DEBUG(3, ("resume denied by security descriptor\n"));
1642 *errcode = WERR_ACCESS_DENIED;
1644 /* BEGIN_ADMIN_LOG */
1645 sys_adminlog( LOG_ERR,
1646 "Permission denied-- user not allowed to delete, \
1647 pause, or resume print job. User name: %s. Printer name: %s.",
1648 uidtoname(user->uid), PRINTERNAME(snum) );
1653 ret = (*(current_printif->job_resume))(snum, pjob);
1656 *errcode = WERR_INVALID_PARAM;
1660 /* force update the database */
1661 print_cache_flush(snum);
1663 /* Send a printer notify message */
1665 notify_job_status(snum, jobid, JOB_STATUS_QUEUED);
1670 /****************************************************************************
1671 Write to a print file.
1672 ****************************************************************************/
1674 int print_job_write(int snum, uint32 jobid, const char *buf, int size)
1677 struct printjob *pjob = print_job_find(snum, jobid);
1681 /* don't allow another process to get this info - it is meaningless */
1682 if (pjob->pid != local_pid)
1685 return_code = write(pjob->fd, buf, size);
1686 if (return_code>0) {
1688 pjob_store(snum, jobid, pjob);
1693 /****************************************************************************
1694 Check if the print queue has been updated recently enough.
1695 ****************************************************************************/
1697 static BOOL print_cache_expired(int snum)
1700 time_t last_qscan_time, time_now = time(NULL);
1701 const char *printername = lp_const_servicename(snum);
1702 struct tdb_print_db *pdb = get_print_db_byname(printername);
1707 slprintf(key, sizeof(key), "CACHE/%s", printername);
1708 last_qscan_time = (time_t)tdb_fetch_int32(pdb->tdb, key);
1711 * Invalidate the queue for 3 reasons.
1712 * (1). last queue scan time == -1.
1713 * (2). Current time - last queue scan time > allowed cache time.
1714 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1715 * This last test picks up machines for which the clock has been moved
1716 * forward, an lpq scan done and then the clock moved back. Otherwise
1717 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1720 if (last_qscan_time == ((time_t)-1) || (time_now - last_qscan_time) >= lp_lpqcachetime() ||
1721 last_qscan_time > (time_now + MAX_CACHE_VALID_TIME)) {
1722 DEBUG(3, ("print cache expired for queue %s \
1723 (last_qscan_time = %d, time now = %d, qcachetime = %d)\n", printername,
1724 (int)last_qscan_time, (int)time_now, (int)lp_lpqcachetime() ));
1725 release_print_db(pdb);
1728 release_print_db(pdb);
1732 /****************************************************************************
1733 Get the queue status - do not update if db is out of date.
1734 ****************************************************************************/
1736 static int get_queue_status(int snum, print_status_struct *status)
1740 const char *printername = lp_const_servicename(snum);
1741 struct tdb_print_db *pdb = get_print_db_byname(printername);
1748 ZERO_STRUCTP(status);
1749 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
1751 key.dsize = strlen(keystr);
1752 data = tdb_fetch(pdb->tdb, key);
1754 if (data.dsize == sizeof(print_status_struct))
1755 memcpy(status, data.dptr, sizeof(print_status_struct));
1756 SAFE_FREE(data.dptr);
1759 len = tdb_fetch_int32(pdb->tdb, "INFO/total_jobs");
1760 release_print_db(pdb);
1761 return (len == -1 ? 0 : len);
1764 /****************************************************************************
1765 Determine the number of jobs in a queue.
1766 ****************************************************************************/
1768 int print_queue_length(int snum, print_status_struct *pstatus)
1770 print_status_struct status;
1773 /* make sure the database is up to date */
1774 if (print_cache_expired(snum))
1775 print_queue_update(snum);
1777 /* also fetch the queue status */
1778 memset(&status, 0, sizeof(status));
1779 len = get_queue_status(snum, &status);
1787 /***************************************************************************
1788 Allocate a jobid. Hold the lock for as short a time as possible.
1789 ***************************************************************************/
1791 static BOOL allocate_print_jobid(struct tdb_print_db *pdb, int snum, const char *printername, uint32 *pjobid)
1796 *pjobid = (uint32)-1;
1798 for (i = 0; i < 3; i++) {
1799 /* Lock the database - only wait 20 seconds. */
1800 if (tdb_lock_bystring(pdb->tdb, "INFO/nextjob", 20) == -1) {
1801 DEBUG(0,("allocate_print_jobid: failed to lock printing database %s\n", printername ));
1805 if (!tdb_fetch_uint32(pdb->tdb, "INFO/nextjob", &jobid)) {
1806 if (tdb_error(pdb->tdb) != TDB_ERR_NOEXIST) {
1807 DEBUG(0, ("allocate_print_jobid: failed to fetch INFO/nextjob for print queue %s\n",
1814 jobid = NEXT_JOBID(jobid);
1816 if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
1817 DEBUG(3, ("allocate_print_jobid: failed to store INFO/nextjob.\n"));
1818 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1822 /* We've finished with the INFO/nextjob lock. */
1823 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1825 if (!print_job_exists(snum, jobid))
1830 DEBUG(0, ("allocate_print_jobid: failed to allocate a print job for queue %s\n",
1832 /* Probably full... */
1837 /* Store a dummy placeholder. */
1842 if (tdb_store(pdb->tdb, print_key(jobid), dum, TDB_INSERT) == -1) {
1843 DEBUG(3, ("allocate_print_jobid: jobid (%d) failed to store placeholder.\n",
1853 /***************************************************************************
1854 Append a jobid to the 'jobs changed' list.
1855 ***************************************************************************/
1857 static BOOL add_to_jobs_changed(struct tdb_print_db *pdb, uint32 jobid)
1861 key.dptr = "INFO/jobs_changed";
1862 key.dsize = strlen(key.dptr);
1863 data.dptr = (char *)&jobid;
1866 DEBUG(10,("add_to_jobs_changed: Added jobid %u\n", (unsigned int)jobid ));
1868 return (tdb_append(pdb->tdb, key, data) == 0);
1871 /***************************************************************************
1872 Start spooling a job - return the jobid.
1873 ***************************************************************************/
1875 uint32 print_job_start(struct current_user *user, int snum, char *jobname, NT_DEVICEMODE *nt_devmode )
1879 struct printjob pjob;
1881 const char *printername = lp_const_servicename(snum);
1882 struct tdb_print_db *pdb = get_print_db_byname(printername);
1890 if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
1891 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
1892 release_print_db(pdb);
1896 if (!print_time_access_check(snum)) {
1897 DEBUG(3, ("print_job_start: job start denied by time check\n"));
1898 release_print_db(pdb);
1902 path = lp_pathname(snum);
1904 /* see if we have sufficient disk space */
1905 if (lp_minprintspace(snum)) {
1906 SMB_BIG_UINT dspace, dsize;
1907 if (sys_fsusage(path, &dspace, &dsize) == 0 &&
1908 dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
1909 DEBUG(3, ("print_job_start: disk space check failed.\n"));
1910 release_print_db(pdb);
1916 /* for autoloaded printers, check that the printcap entry still exists */
1917 if (lp_autoloaded(snum) && !pcap_printername_ok(lp_const_servicename(snum), NULL)) {
1918 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum) ));
1919 release_print_db(pdb);
1924 /* Insure the maximum queue size is not violated */
1925 if ((njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
1926 DEBUG(3, ("print_job_start: Queue %s number of jobs (%d) larger than max printjobs per queue (%d).\n",
1927 printername, njobs, lp_maxprintjobs(snum) ));
1928 release_print_db(pdb);
1933 DEBUG(10,("print_job_start: Queue %s number of jobs (%d), max printjobs = %d\n",
1934 printername, njobs, lp_maxprintjobs(snum) ));
1936 if (!allocate_print_jobid(pdb, snum, printername, &jobid))
1939 /* create the database entry */
1943 pjob.pid = local_pid;
1946 pjob.starttime = time(NULL);
1947 pjob.status = LPQ_SPOOLING;
1949 pjob.spooled = False;
1951 pjob.nt_devmode = nt_devmode;
1953 fstrcpy(pjob.jobname, jobname);
1955 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1956 fstrcpy(pjob.user, vuser->user.smb_name);
1958 fstrcpy(pjob.user, uidtoname(user->uid));
1961 fstrcpy(pjob.queuename, lp_const_servicename(snum));
1963 /* we have a job entry - now create the spool file */
1964 slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.8u.XXXXXX",
1965 path, PRINT_SPOOL_PREFIX, (unsigned int)jobid);
1966 pjob.fd = smb_mkstemp(pjob.filename);
1968 if (pjob.fd == -1) {
1969 if (errno == EACCES) {
1970 /* Common setup error, force a report. */
1971 DEBUG(0, ("print_job_start: insufficient permissions \
1972 to open spool file %s.\n", pjob.filename));
1974 /* Normal case, report at level 3 and above. */
1975 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
1976 DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
1981 pjob_store(snum, jobid, &pjob);
1983 /* Update the 'jobs changed' entry used by print_queue_status. */
1984 add_to_jobs_changed(pdb, jobid);
1986 /* Ensure we keep a rough count of the number of total jobs... */
1987 tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, 1);
1989 release_print_db(pdb);
1995 pjob_delete(snum, jobid);
1997 release_print_db(pdb);
1999 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
2003 /****************************************************************************
2004 Update the number of pages spooled to jobid
2005 ****************************************************************************/
2007 void print_job_endpage(int snum, uint32 jobid)
2009 struct printjob *pjob = print_job_find(snum, jobid);
2012 /* don't allow another process to get this info - it is meaningless */
2013 if (pjob->pid != local_pid)
2017 pjob_store(snum, jobid, pjob);
2020 /****************************************************************************
2021 Print a file - called on closing the file. This spools the job.
2022 If normal close is false then we're tearing down the jobs - treat as an
2024 ****************************************************************************/
2026 BOOL print_job_end(int snum, uint32 jobid, BOOL normal_close)
2028 struct printjob *pjob = print_job_find(snum, jobid);
2030 SMB_STRUCT_STAT sbuf;
2035 if (pjob->spooled || pjob->pid != local_pid)
2038 if (normal_close && (sys_fstat(pjob->fd, &sbuf) == 0)) {
2039 pjob->size = sbuf.st_size;
2045 * Not a normal close or we couldn't stat the job file,
2046 * so something has gone wrong. Cleanup.
2050 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
2054 /* Technically, this is not quite right. If the printer has a separator
2055 * page turned on, the NT spooler prints the separator page even if the
2056 * print job is 0 bytes. 010215 JRR */
2057 if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
2058 /* don't bother spooling empty files or something being deleted. */
2059 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
2060 pjob->filename, pjob->size ? "deleted" : "zero length" ));
2061 unlink(pjob->filename);
2062 pjob_delete(snum, jobid);
2066 ret = (*(current_printif->job_submit))(snum, pjob);
2071 /* The print job has been sucessfully handed over to the back-end */
2073 pjob->spooled = True;
2074 pjob->status = LPQ_QUEUED;
2075 pjob_store(snum, jobid, pjob);
2077 /* make sure the database is up to date */
2078 if (print_cache_expired(snum))
2079 print_queue_update(snum);
2085 /* The print job was not succesfully started. Cleanup */
2086 /* Still need to add proper error return propagation! 010122:JRR */
2087 unlink(pjob->filename);
2088 pjob_delete(snum, jobid);
2089 remove_from_jobs_changed(snum, jobid);
2093 /****************************************************************************
2094 Get a snapshot of jobs in the system without traversing.
2095 ****************************************************************************/
2097 static BOOL get_stored_queue_info(struct tdb_print_db *pdb, int snum, int *pcount, print_queue_struct **ppqueue)
2099 TDB_DATA data, key, cgdata;
2100 print_queue_struct *queue = NULL;
2102 uint32 extra_count = 0;
2103 int total_count = 0;
2106 int max_reported_jobs = lp_max_reported_jobs(snum);
2109 /* make sure the database is up to date */
2110 if (print_cache_expired(snum))
2111 print_queue_update(snum);
2117 ZERO_STRUCT(cgdata);
2118 key.dptr = "INFO/linear_queue_array";
2119 key.dsize = strlen(key.dptr);
2121 /* Get the stored queue data. */
2122 data = tdb_fetch(pdb->tdb, key);
2124 if (data.dptr == NULL || data.dsize < 4)
2127 memcpy(&qcount, data.dptr, 4);
2129 /* Get the changed jobs list. */
2130 key.dptr = "INFO/jobs_changed";
2131 key.dsize = strlen(key.dptr);
2133 cgdata = tdb_fetch(pdb->tdb, key);
2134 if (cgdata.dptr != NULL && (cgdata.dsize % 4 == 0))
2135 extra_count = cgdata.dsize/4;
2137 DEBUG(5,("get_stored_queue_info: qcount = %u, extra_count = %u\n", (unsigned int)qcount, (unsigned int)extra_count));
2139 /* Allocate the queue size. */
2140 if (qcount == 0 && extra_count == 0)
2143 if ((queue = (print_queue_struct *)malloc(sizeof(print_queue_struct)*(qcount + extra_count))) == NULL)
2146 /* Retrieve the linearised queue data. */
2148 for( i = 0; i < qcount; i++) {
2149 uint32 qjob, qsize, qpage_count, qstatus, qpriority, qtime;
2150 len += tdb_unpack(data.dptr + 4 + len, data.dsize - len, NULL, "ddddddff",
2159 queue[i].job = qjob;
2160 queue[i].size = qsize;
2161 queue[i].page_count = qpage_count;
2162 queue[i].status = qstatus;
2163 queue[i].priority = qpriority;
2164 queue[i].time = qtime;
2167 total_count = qcount;
2169 /* Add in the changed jobids. */
2170 for( i = 0; i < extra_count; i++) {
2172 struct printjob *pjob;
2174 memcpy(&jobid, &cgdata.dptr[i*4], 4);
2175 DEBUG(5,("get_stored_queue_info: changed job = %u\n", (unsigned int)jobid));
2176 pjob = print_job_find(snum, jobid);
2178 DEBUG(5,("get_stored_queue_info: failed to find changed job = %u\n", (unsigned int)jobid));
2179 remove_from_jobs_changed(snum, jobid);
2183 queue[total_count].job = jobid;
2184 queue[total_count].size = pjob->size;
2185 queue[total_count].page_count = pjob->page_count;
2186 queue[total_count].status = pjob->status;
2187 queue[total_count].priority = 1;
2188 queue[total_count].time = pjob->starttime;
2189 fstrcpy(queue[total_count].fs_user, pjob->user);
2190 fstrcpy(queue[total_count].fs_file, pjob->jobname);
2194 /* Sort the queue by submission time otherwise they are displayed
2197 qsort(queue, total_count, sizeof(print_queue_struct), QSORT_CAST(printjob_comp));
2199 DEBUG(5,("get_stored_queue_info: total_count = %u\n", (unsigned int)total_count));
2201 if (max_reported_jobs && total_count > max_reported_jobs)
2202 total_count = max_reported_jobs;
2205 *pcount = total_count;
2211 SAFE_FREE(data.dptr);
2212 SAFE_FREE(cgdata.dptr);
2216 /****************************************************************************
2217 Get a printer queue listing.
2218 set queue = NULL and status = NULL if you just want to update the cache
2219 ****************************************************************************/
2221 int print_queue_status(int snum,
2222 print_queue_struct **ppqueue,
2223 print_status_struct *status)
2227 const char *printername;
2228 struct tdb_print_db *pdb;
2231 /* make sure the database is up to date */
2233 if (print_cache_expired(snum))
2234 print_queue_update(snum);
2236 /* return if we are done */
2237 if ( !ppqueue || !status )
2241 printername = lp_const_servicename(snum);
2242 pdb = get_print_db_byname(printername);
2248 * Fetch the queue status. We must do this first, as there may
2249 * be no jobs in the queue.
2252 ZERO_STRUCTP(status);
2253 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
2255 key.dsize = strlen(keystr);
2256 data = tdb_fetch(pdb->tdb, key);
2258 if (data.dsize == sizeof(*status)) {
2259 memcpy(status, data.dptr, sizeof(*status));
2261 SAFE_FREE(data.dptr);
2265 * Now, fetch the print queue information. We first count the number
2266 * of entries, and then only retrieve the queue if necessary.
2269 if (!get_stored_queue_info(pdb, snum, &count, ppqueue)) {
2270 release_print_db(pdb);
2274 release_print_db(pdb);
2278 /****************************************************************************
2280 ****************************************************************************/
2282 BOOL print_queue_pause(struct current_user *user, int snum, WERROR *errcode)
2286 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2287 *errcode = WERR_ACCESS_DENIED;
2291 ret = (*(current_printif->queue_pause))(snum);
2294 *errcode = WERR_INVALID_PARAM;
2298 /* force update the database */
2299 print_cache_flush(snum);
2301 /* Send a printer notify message */
2303 notify_printer_status(snum, PRINTER_STATUS_PAUSED);
2308 /****************************************************************************
2310 ****************************************************************************/
2312 BOOL print_queue_resume(struct current_user *user, int snum, WERROR *errcode)
2316 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2317 *errcode = WERR_ACCESS_DENIED;
2321 ret = (*(current_printif->queue_resume))(snum);
2324 *errcode = WERR_INVALID_PARAM;
2328 /* make sure the database is up to date */
2329 if (print_cache_expired(snum))
2330 print_queue_update(snum);
2332 /* Send a printer notify message */
2334 notify_printer_status(snum, PRINTER_STATUS_OK);
2339 /****************************************************************************
2340 Purge a queue - implemented by deleting all jobs that we can delete.
2341 ****************************************************************************/
2343 BOOL print_queue_purge(struct current_user *user, int snum, WERROR *errcode)
2345 print_queue_struct *queue;
2346 print_status_struct status;
2350 /* Force and update so the count is accurate (i.e. not a cached count) */
2351 print_queue_update(snum);
2353 can_job_admin = print_access_check(user, snum, JOB_ACCESS_ADMINISTER);
2354 njobs = print_queue_status(snum, &queue, &status);
2356 for (i=0;i<njobs;i++) {
2357 BOOL owner = is_owner(user, snum, queue[i].job);
2359 if (owner || can_job_admin) {
2360 print_job_delete1(snum, queue[i].job);