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.
25 /* Current printer interface */
26 static struct printif *current_printif = &generic_printif;
29 the printing backend revolves around a tdb database that stores the
30 SMB view of the print queue
32 The key for this database is a jobid - a internally generated number that
33 uniquely identifies a print job
35 reading the print queue involves two steps:
36 - possibly running lpq and updating the internal database from that
37 - reading entries from the database
39 jobids are assigned when a job starts spooling.
42 /***************************************************************************
43 Nightmare. LANMAN jobid's are 16 bit numbers..... We must map them to 32
44 bit RPC jobids.... JRA.
45 ***************************************************************************/
47 static TDB_CONTEXT *rap_tdb;
48 static uint16 next_rap_jobid;
50 uint16 pjobid_to_rap(int snum, uint32 jobid)
57 /* Create the in-memory tdb. */
58 rap_tdb = tdb_open_log(NULL, 0, TDB_INTERNAL, (O_RDWR|O_CREAT), 0644);
63 SIVAL(&jinfo,0,(int32)snum);
64 SIVAL(&jinfo,4,jobid);
66 key.dptr = (char *)&jinfo;
67 key.dsize = sizeof(jinfo);
68 data = tdb_fetch(rap_tdb, key);
69 if (data.dptr && data.dsize == sizeof(uint16)) {
70 memcpy(&rap_jobid, data.dptr, sizeof(uint16));
74 /* Not found - create and store mapping. */
75 rap_jobid = ++next_rap_jobid;
77 rap_jobid = ++next_rap_jobid;
78 data.dptr = (char *)&rap_jobid;
79 data.dsize = sizeof(rap_jobid);
80 tdb_store(rap_tdb, key, data, TDB_REPLACE);
81 tdb_store(rap_tdb, data, key, TDB_REPLACE);
85 BOOL rap_to_pjobid(uint16 rap_jobid, int *psnum, uint32 *pjobid)
93 key.dptr = (char *)&rap_jobid;
94 key.dsize = sizeof(rap_jobid);
95 data = tdb_fetch(rap_tdb, key);
96 if (data.dptr && data.dsize == sizeof(jinfo)) {
97 *psnum = IVAL(&jinfo,0);
98 *pjobid = IVAL(&jinfo,4);
105 static void rap_jobid_delete(int snum, uint32 jobid)
114 SIVAL(&jinfo,0,(int32)snum);
115 SIVAL(&jinfo,4,jobid);
117 key.dptr = (char *)&jinfo;
118 key.dsize = sizeof(jinfo);
119 data = tdb_fetch(rap_tdb, key);
120 if (!data.dptr || (data.dsize != sizeof(uint16)))
123 memcpy(&rap_jobid, data.dptr, sizeof(uint16));
124 SAFE_FREE(data.dptr);
125 data.dptr = (char *)&rap_jobid;
126 data.dsize = sizeof(rap_jobid);
127 tdb_delete(rap_tdb, key);
128 tdb_delete(rap_tdb, data);
131 static pid_t local_pid;
133 static int get_queue_status(int, print_status_struct *);
135 #define MAX_PRINT_DBS_OPEN 1
137 struct tdb_print_db {
138 struct tdb_print_db *next, *prev;
140 fstring printer_name;
143 static struct tdb_print_db *print_db_head;
145 /****************************************************************************
146 Function to find or create the printer specific job tdb given a printername.
147 Limits the number of tdb's open to MAX_PRINT_DBS_OPEN.
148 ****************************************************************************/
150 static struct tdb_print_db *get_print_db_byname(const char *printername)
152 struct tdb_print_db *p, *last_entry;
154 pstring printdb_path;
156 for (p = print_db_head, last_entry = print_db_head; p; p = p->next) {
157 if (p->tdb && strequal(p->printer_name, printername)) {
158 DLIST_PROMOTE(print_db_head, p);
165 if (num_open >= MAX_PRINT_DBS_OPEN) {
166 /* Recycle the last entry. */
167 DLIST_PROMOTE(print_db_head, last_entry);
168 if (print_db_head->tdb) {
169 if (tdb_close(print_db_head->tdb)) {
170 DEBUG(0,("get_print_db: Failed to close tdb for printer %s\n",
171 print_db_head->printer_name ));
179 p = (struct tdb_print_db *)malloc(sizeof(struct tdb_print_db));
181 DEBUG(0,("get_print_db: malloc fail !\n"));
185 DLIST_ADD(print_db_head, p);
188 pstrcpy(printdb_path, lock_path("printing/"));
189 pstrcat(printdb_path, printername);
190 pstrcat(printdb_path, ".tdb");
191 p->tdb = tdb_open_log(printdb_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
193 DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
195 DLIST_REMOVE(print_db_head, p);
199 fstrcpy(p->printer_name, printername);
203 /****************************************************************************
204 Initialise the printing backend. Called once at startup.
205 Does not survive a fork
206 ****************************************************************************/
208 BOOL print_backend_init(void)
210 char *sversion = "INFO/version";
211 pstring printing_path;
212 int services = lp_numservices();
215 if (local_pid == sys_getpid())
218 unlink(lock_path("printing.tdb"));
219 pstrcpy(printing_path,lock_path("printing"));
220 mkdir(printing_path,0755);
222 local_pid = sys_getpid();
224 /* handle a Samba upgrade */
226 for (snum = 0; snum < services; snum++) {
227 struct tdb_print_db *pdb;
228 if (!lp_print_ok(snum))
231 pdb = get_print_db_byname(lp_const_servicename(snum));
234 tdb_lock_bystring(pdb->tdb, sversion);
235 if (tdb_fetch_int32(pdb->tdb, sversion) != PRINT_DATABASE_VERSION) {
236 tdb_traverse(pdb->tdb, tdb_traverse_delete_fn, NULL);
237 tdb_store_int32(pdb->tdb, sversion, PRINT_DATABASE_VERSION);
239 tdb_unlock_bystring(pdb->tdb, sversion);
242 /* select the appropriate printing interface... */
244 if (strcmp(lp_printcapname(), "cups") == 0)
245 current_printif = &cups_printif;
246 #endif /* HAVE_CUPS */
248 /* do NT print initialization... */
249 return nt_printing_init();
252 /****************************************************************************
253 Shut down printing backend. Called once at shutdown to close the tdb.
254 ****************************************************************************/
256 void printing_end(void)
258 struct tdb_print_db *p;
260 for (p = print_db_head; p; ) {
261 struct tdb_print_db *next_p = p->next;
264 DLIST_REMOVE(print_db_head, p);
270 /****************************************************************************
271 Useful function to generate a tdb key.
272 ****************************************************************************/
274 static TDB_DATA print_key(uint32 jobid)
280 ret.dptr = (void *)&j;
281 ret.dsize = sizeof(j);
285 /****************************************************************************
286 Useful function to find a print job in the database.
287 ****************************************************************************/
289 static struct printjob *print_job_find(int snum, uint32 jobid)
291 static struct printjob pjob;
293 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
298 ret = tdb_fetch(pdb->tdb, print_key(jobid));
299 if (!ret.dptr || ret.dsize != sizeof(pjob))
302 memcpy(&pjob, ret.dptr, sizeof(pjob));
307 /* Convert a unix jobid to a smb jobid */
309 static uint32 sysjob_to_jobid_value;
311 static int unixjob_traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key,
312 TDB_DATA data, void *state)
314 struct printjob *pjob = (struct printjob *)data.dptr;
315 int *sysjob = (int *)state;
317 if (key.dsize != sizeof(uint32))
320 if (*sysjob == pjob->sysjob) {
321 uint32 *jobid = (uint32 *)key.dptr;
323 sysjob_to_jobid_value = *jobid;
330 /****************************************************************************
331 This is a *horribly expensive call as we have to iterate through all the
332 current printer tdb's. Don't do this often ! JRA.
333 ****************************************************************************/
335 uint32 sysjob_to_jobid(int unix_jobid)
337 int services = lp_numservices();
340 sysjob_to_jobid_value = (uint32)-1;
342 for (snum = 0; snum < services; snum++) {
343 struct tdb_print_db *pdb;
344 if (!lp_print_ok(snum))
346 pdb = get_print_db_byname(lp_const_servicename(snum));
348 tdb_traverse(pdb->tdb, unixjob_traverse_fn, &unix_jobid);
349 if (sysjob_to_jobid_value != (uint32)-1)
350 return sysjob_to_jobid_value;
355 /****************************************************************************
356 Send notifications based on what has changed after a pjob_store.
357 ****************************************************************************/
361 uint32 spoolss_status;
362 } lpq_to_spoolss_status_map[] = {
363 { LPQ_QUEUED, JOB_STATUS_QUEUED },
364 { LPQ_PAUSED, JOB_STATUS_PAUSED },
365 { LPQ_SPOOLING, JOB_STATUS_SPOOLING },
366 { LPQ_PRINTING, JOB_STATUS_PRINTING },
367 { LPQ_DELETING, JOB_STATUS_DELETING },
368 { LPQ_OFFLINE, JOB_STATUS_OFFLINE },
369 { LPQ_PAPEROUT, JOB_STATUS_PAPEROUT },
370 { LPQ_PRINTED, JOB_STATUS_PRINTED },
371 { LPQ_DELETED, JOB_STATUS_DELETED },
372 { LPQ_BLOCKED, JOB_STATUS_BLOCKED },
373 { LPQ_USER_INTERVENTION, JOB_STATUS_USER_INTERVENTION },
377 /* Convert a lpq status value stored in printing.tdb into the
378 appropriate win32 API constant. */
380 static uint32 map_to_spoolss_status(uint32 lpq_status)
384 while (lpq_to_spoolss_status_map[i].lpq_status != -1) {
385 if (lpq_to_spoolss_status_map[i].lpq_status == lpq_status)
386 return lpq_to_spoolss_status_map[i].spoolss_status;
393 static void pjob_store_notify(int snum, uint32 jobid, struct printjob *old_data,
394 struct printjob *new_data)
396 BOOL new_job = False;
401 /* Notify the job name first */
403 if (new_job || !strequal(old_data->jobname, new_data->jobname))
404 notify_job_name(snum, jobid, new_data->jobname);
406 /* Job attributes that can't be changed. We only send
407 notification for these on a new job. */
410 notify_job_submitted(snum, jobid, new_data->starttime);
411 notify_job_username(snum, jobid, new_data->user);
414 /* Job attributes of a new job or attributes that can be
417 if (new_job || old_data->status != new_data->status)
418 notify_job_status(snum, jobid, map_to_spoolss_status(new_data->status));
420 if (new_job || old_data->size != new_data->size)
421 notify_job_total_bytes(snum, jobid, new_data->size);
423 if (new_job || old_data->page_count != new_data->page_count)
424 notify_job_total_pages(snum, jobid, new_data->page_count);
427 /****************************************************************************
428 Store a job structure back to the database.
429 ****************************************************************************/
431 static BOOL pjob_store(int snum, uint32 jobid, struct printjob *pjob)
433 TDB_DATA old_data, new_data;
435 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
442 old_data = tdb_fetch(pdb->tdb, print_key(jobid));
446 new_data.dptr = (void *)pjob;
447 new_data.dsize = sizeof(*pjob);
448 ret = (tdb_store(pdb->tdb, print_key(jobid), new_data, TDB_REPLACE) == 0);
450 /* Send notify updates for what has changed */
452 if (ret && (old_data.dsize == 0 || old_data.dsize == sizeof(*pjob))) {
454 snum, jobid, (struct printjob *)old_data.dptr,
455 (struct printjob *)new_data.dptr);
462 /****************************************************************************
463 Remove a job structure from the database.
464 ****************************************************************************/
466 static void pjob_delete(int snum, uint32 jobid)
468 struct printjob *pjob = print_job_find(snum, jobid);
469 uint32 job_status = 0;
470 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
476 DEBUG(5, ("pjob_delete(): we were asked to delete nonexistent job %u\n",
477 (unsigned int)jobid));
481 /* Send a notification that a job has been deleted */
483 job_status = map_to_spoolss_status(pjob->status);
485 /* We must cycle through JOB_STATUS_DELETING and
486 JOB_STATUS_DELETED for the port monitor to delete the job
489 job_status |= JOB_STATUS_DELETING;
490 notify_job_status(snum, jobid, job_status);
492 job_status |= JOB_STATUS_DELETED;
493 notify_job_status(snum, jobid, job_status);
495 /* Remove from printing.tdb */
497 tdb_delete(pdb->tdb, print_key(jobid));
498 rap_jobid_delete(snum, jobid);
501 /****************************************************************************
502 Parse a file name from the system spooler to generate a jobid.
503 ****************************************************************************/
505 static uint32 print_parse_jobid(char *fname)
509 if (strncmp(fname,PRINT_SPOOL_PREFIX,strlen(PRINT_SPOOL_PREFIX)) != 0)
511 fname += strlen(PRINT_SPOOL_PREFIX);
517 return (uint32)jobid;
520 /****************************************************************************
521 List a unix job in the print database.
522 ****************************************************************************/
524 static void print_unix_job(int snum, print_queue_struct *q)
526 uint32 jobid = q->job + UNIX_JOB_START;
527 struct printjob pj, *old_pj;
529 /* Preserve the timestamp on an existing unix print job */
531 old_pj = print_job_find(snum, jobid);
538 pj.starttime = old_pj ? old_pj->starttime : q->time;
539 pj.status = q->status;
543 fstrcpy(pj.filename, "");
544 fstrcpy(pj.jobname, q->fs_file);
545 fstrcpy(pj.user, q->fs_user);
546 fstrcpy(pj.queuename, lp_const_servicename(snum));
548 pjob_store(snum, jobid, &pj);
552 struct traverse_struct {
553 print_queue_struct *queue;
554 int qcount, snum, maxcount, total_jobs;
557 /****************************************************************************
558 Utility fn to delete any jobs that are no longer active.
559 ****************************************************************************/
561 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
563 struct traverse_struct *ts = (struct traverse_struct *)state;
564 struct printjob pjob;
568 if (data.dsize != sizeof(pjob) || key.dsize != sizeof(jobid))
570 memcpy(&jobid, key.dptr, sizeof(jobid));
571 memcpy(&pjob, data.dptr, sizeof(pjob));
573 if (ts->snum != lp_servicenumber(pjob.queuename)) {
574 /* this isn't for the queue we are looking at - this cannot happen with the split tdb's. JRA */
579 /* remove a unix job if it isn't in the system queue any more */
581 for (i=0;i<ts->qcount;i++) {
582 uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
583 if (jobid == u_jobid)
587 pjob_delete(ts->snum, jobid);
593 /* maybe it hasn't been spooled yet */
595 /* if a job is not spooled and the process doesn't
596 exist then kill it. This cleans up after smbd
598 if (!process_exists(pjob.pid))
599 pjob_delete(ts->snum, jobid);
605 for (i=0;i<ts->qcount;i++) {
606 uint32 curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
607 if (jobid == curr_jobid)
611 /* The job isn't in the system queue - we have to assume it has
612 completed, so delete the database entry. */
614 if (i == ts->qcount) {
615 time_t cur_t = time(NULL);
617 /* A race can occur between the time a job is spooled and
618 when it appears in the lpq output. This happens when
619 the job is added to printing.tdb when another smbd
620 running print_queue_update() has completed a lpq and
621 is currently traversing the printing tdb and deleting jobs.
622 A workaround is to not delete the job if it has been
623 submitted less than lp_lpqcachetime() seconds ago. */
625 if ((cur_t - pjob.starttime) > lp_lpqcachetime())
626 pjob_delete(ts->snum, jobid);
636 /****************************************************************************
637 Check if the print queue has been updated recently enough.
638 ****************************************************************************/
640 static void print_cache_flush(int snum)
643 const char *printername = lp_const_servicename(snum);
644 struct tdb_print_db *pdb = get_print_db_byname(printername);
648 slprintf(key, sizeof(key)-1, "CACHE/%s", printername);
649 tdb_store_int32(pdb->tdb, key, -1);
652 /****************************************************************************
653 Check if someone already thinks they are doing the update.
654 ****************************************************************************/
656 static pid_t get_updating_pid(fstring printer_name)
661 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
665 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
667 key.dsize = strlen(keystr);
669 data = tdb_fetch(pdb->tdb, key);
670 if (!data.dptr || data.dsize != sizeof(pid_t))
673 memcpy(&updating_pid, data.dptr, sizeof(pid_t));
674 SAFE_FREE(data.dptr);
676 if (process_exists(updating_pid))
682 /****************************************************************************
683 Set the fact that we're doing the update, or have finished doing the update
685 ****************************************************************************/
687 static void set_updating_pid(const fstring printer_name, BOOL delete)
692 pid_t updating_pid = sys_getpid();
693 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
698 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
700 key.dsize = strlen(keystr);
703 tdb_delete(pdb->tdb, key);
707 data.dptr = (void *)&updating_pid;
708 data.dsize = sizeof(pid_t);
710 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
713 /****************************************************************************
714 Update the internal database from the system print queue for a queue.
715 ****************************************************************************/
717 static void print_queue_update(int snum)
720 print_queue_struct *queue = NULL;
721 print_status_struct status;
722 print_status_struct old_status;
723 struct printjob *pjob;
724 struct traverse_struct tstruct;
725 fstring keystr, printer_name, cachestr;
727 struct tdb_print_db *pdb;
729 fstrcpy(printer_name, lp_const_servicename(snum));
730 pdb = get_print_db_byname(printer_name);
735 * Check to see if someone else is doing this update.
736 * This is essentially a mutex on the update.
739 if (get_updating_pid(printer_name) != -1)
742 /* Lock the queue for the database update */
744 slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", printer_name);
745 tdb_lock_bystring(pdb->tdb, keystr);
748 * Ensure that no one else got in here.
749 * If the updating pid is still -1 then we are
753 if (get_updating_pid(printer_name) != -1) {
755 * Someone else is doing the update, exit.
757 tdb_unlock_bystring(pdb->tdb, keystr);
762 * We're going to do the update ourselves.
765 /* Tell others we're doing the update. */
766 set_updating_pid(printer_name, False);
769 * Allow others to enter and notice we're doing
773 tdb_unlock_bystring(pdb->tdb, keystr);
776 * Update the cache time FIRST ! Stops others even
777 * attempting to get the lock and doing this
778 * if the lpq takes a long time.
781 slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", printer_name);
782 tdb_store_int32(pdb->tdb, cachestr, (int)time(NULL));
784 /* get the current queue using the appropriate interface */
787 qcount = (*(current_printif->queue_get))(snum, &queue, &status);
789 DEBUG(3, ("%d job%s in queue for %s\n", qcount, (qcount != 1) ?
790 "s" : "", printer_name));
793 any job in the internal database that is marked as spooled
794 and doesn't exist in the system queue is considered finished
795 and removed from the database
797 any job in the system database but not in the internal database
798 is added as a unix job
800 fill in any system job numbers as we go
802 for (i=0; i<qcount; i++) {
803 uint32 jobid = print_parse_jobid(queue[i].fs_file);
805 if (jobid == (uint32)-1) {
806 /* assume its a unix print job */
807 print_unix_job(snum, &queue[i]);
811 /* we have an active SMB print job - update its status */
812 pjob = print_job_find(snum, jobid);
814 /* err, somethings wrong. Probably smbd was restarted
815 with jobs in the queue. All we can do is treat them
816 like unix jobs. Pity. */
817 print_unix_job(snum, &queue[i]);
821 pjob->sysjob = queue[i].job;
822 pjob->status = queue[i].status;
824 pjob_store(snum, jobid, pjob);
827 /* now delete any queued entries that don't appear in the
829 tstruct.queue = queue;
830 tstruct.qcount = qcount;
832 tstruct.total_jobs = 0;
834 tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
836 SAFE_FREE(tstruct.queue);
838 tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
840 if( qcount != get_queue_status(snum, &old_status))
841 DEBUG(10,("print_queue_update: queue status change %d jobs -> %d jobs for printer %s\n",
842 old_status.qcount, qcount, printer_name ));
844 /* store the new queue status structure */
845 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printer_name);
847 key.dsize = strlen(keystr);
849 status.qcount = qcount;
850 data.dptr = (void *)&status;
851 data.dsize = sizeof(status);
852 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
855 * Update the cache time again. We want to do this call
856 * as little as possible...
859 slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", printer_name);
860 tdb_store_int32(pdb->tdb, keystr, (int32)time(NULL));
862 /* Delete our pid from the db. */
863 set_updating_pid(printer_name, True);
866 /****************************************************************************
867 Check if a jobid is valid. It is valid if it exists in the database.
868 ****************************************************************************/
870 BOOL print_job_exists(int snum, uint32 jobid)
872 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
875 return tdb_exists(pdb->tdb, print_key(jobid));
878 /****************************************************************************
879 Give the fd used for a jobid.
880 ****************************************************************************/
882 int print_job_fd(int snum, uint32 jobid)
884 struct printjob *pjob = print_job_find(snum, jobid);
887 /* don't allow another process to get this info - it is meaningless */
888 if (pjob->pid != local_pid)
893 /****************************************************************************
894 Give the filename used for a jobid.
895 Only valid for the process doing the spooling and when the job
896 has not been spooled.
897 ****************************************************************************/
899 char *print_job_fname(int snum, uint32 jobid)
901 struct printjob *pjob = print_job_find(snum, jobid);
902 if (!pjob || pjob->spooled || pjob->pid != local_pid)
904 return pjob->filename;
907 /****************************************************************************
908 Set the place in the queue for a job.
909 ****************************************************************************/
911 BOOL print_job_set_place(int snum, uint32 jobid, int place)
913 DEBUG(2,("print_job_set_place not implemented yet\n"));
917 /****************************************************************************
918 Set the name of a job. Only possible for owner.
919 ****************************************************************************/
921 BOOL print_job_set_name(int snum, uint32 jobid, char *name)
923 struct printjob *pjob = print_job_find(snum, jobid);
924 if (!pjob || pjob->pid != local_pid)
927 fstrcpy(pjob->jobname, name);
928 return pjob_store(snum, jobid, pjob);
931 /****************************************************************************
932 Delete a print job - don't update queue.
933 ****************************************************************************/
935 static BOOL print_job_delete1(int snum, uint32 jobid)
937 struct printjob *pjob = print_job_find(snum, jobid);
944 * If already deleting just return.
947 if (pjob->status == LPQ_DELETING)
950 /* Hrm - we need to be able to cope with deleting a job before it
951 has reached the spooler. */
953 if (pjob->sysjob == -1) {
954 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid));
957 /* Set the tdb entry to be deleting. */
959 pjob->status = LPQ_DELETING;
960 pjob_store(snum, jobid, pjob);
962 if (pjob->spooled && pjob->sysjob != -1)
963 result = (*(current_printif->job_delete))(snum, pjob);
965 /* Delete the tdb entry if the delete suceeded or the job hasn't
969 pjob_delete(snum, jobid);
971 return (result == 0);
974 /****************************************************************************
975 Return true if the current user owns the print job.
976 ****************************************************************************/
978 static BOOL is_owner(struct current_user *user, int snum, uint32 jobid)
980 struct printjob *pjob = print_job_find(snum, jobid);
986 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
987 return strequal(pjob->user, vuser->user.smb_name);
989 return strequal(pjob->user, uidtoname(user->uid));
993 /****************************************************************************
995 ****************************************************************************/
997 BOOL print_job_delete(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1001 owner = is_owner(user, snum, jobid);
1003 /* Check access against security descriptor or whether the user
1007 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1008 DEBUG(3, ("delete denied by security descriptor\n"));
1009 *errcode = WERR_ACCESS_DENIED;
1013 if (!print_job_delete1(snum, jobid))
1016 /* force update the database and say the delete failed if the
1019 print_queue_update(snum);
1021 return !print_job_exists(snum, jobid);
1024 /****************************************************************************
1026 ****************************************************************************/
1028 BOOL print_job_pause(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1030 struct printjob *pjob = print_job_find(snum, jobid);
1036 if (!pjob->spooled || pjob->sysjob == -1)
1039 if (!is_owner(user, snum, jobid) &&
1040 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1041 DEBUG(3, ("pause denied by security descriptor\n"));
1042 *errcode = WERR_ACCESS_DENIED;
1046 /* need to pause the spooled entry */
1047 ret = (*(current_printif->job_pause))(snum, pjob);
1050 *errcode = WERR_INVALID_PARAM;
1054 /* force update the database */
1055 print_cache_flush(snum);
1057 /* Send a printer notify message */
1059 notify_job_status(snum, jobid, JOB_STATUS_PAUSED);
1061 /* how do we tell if this succeeded? */
1066 /****************************************************************************
1068 ****************************************************************************/
1070 BOOL print_job_resume(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1072 struct printjob *pjob = print_job_find(snum, jobid);
1078 if (!pjob->spooled || pjob->sysjob == -1)
1081 if (!is_owner(user, snum, jobid) &&
1082 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1083 DEBUG(3, ("resume denied by security descriptor\n"));
1084 *errcode = WERR_ACCESS_DENIED;
1088 ret = (*(current_printif->job_resume))(snum, pjob);
1091 *errcode = WERR_INVALID_PARAM;
1095 /* force update the database */
1096 print_cache_flush(snum);
1098 /* Send a printer notify message */
1100 notify_job_status(snum, jobid, JOB_STATUS_QUEUED);
1105 /****************************************************************************
1106 Write to a print file.
1107 ****************************************************************************/
1109 int print_job_write(int snum, uint32 jobid, const char *buf, int size)
1112 struct printjob *pjob = print_job_find(snum, jobid);
1116 /* don't allow another process to get this info - it is meaningless */
1117 if (pjob->pid != local_pid)
1120 return_code = write(pjob->fd, buf, size);
1121 if (return_code>0) {
1123 pjob_store(snum, jobid, pjob);
1128 /****************************************************************************
1129 Check if the print queue has been updated recently enough.
1130 ****************************************************************************/
1132 static BOOL print_cache_expired(int snum)
1135 time_t last_qscan_time, time_now = time(NULL);
1136 const char *printername = lp_const_servicename(snum);
1137 struct tdb_print_db *pdb = get_print_db_byname(printername);
1142 slprintf(key, sizeof(key), "CACHE/%s", printername);
1143 last_qscan_time = (time_t)tdb_fetch_int32(pdb->tdb, key);
1146 * Invalidate the queue for 3 reasons.
1147 * (1). last queue scan time == -1.
1148 * (2). Current time - last queue scan time > allowed cache time.
1149 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1150 * This last test picks up machines for which the clock has been moved
1151 * forward, an lpq scan done and then the clock moved back. Otherwise
1152 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1155 if (last_qscan_time == ((time_t)-1) || (time_now - last_qscan_time) >= lp_lpqcachetime() ||
1156 last_qscan_time > (time_now + MAX_CACHE_VALID_TIME)) {
1157 DEBUG(3, ("print cache expired for queue %s \
1158 (last_qscan_time = %d, time now = %d, qcachetime = %d)\n", printername,
1159 (int)last_qscan_time, (int)time_now, (int)lp_lpqcachetime() ));
1165 /****************************************************************************
1166 Get the queue status - do not update if db is out of date.
1167 ****************************************************************************/
1169 static int get_queue_status(int snum, print_status_struct *status)
1173 const char *printername = lp_const_servicename(snum);
1174 struct tdb_print_db *pdb = get_print_db_byname(printername);
1178 ZERO_STRUCTP(status);
1179 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
1181 key.dsize = strlen(keystr);
1182 data = tdb_fetch(pdb->tdb, key);
1184 if (data.dsize == sizeof(print_status_struct)) {
1185 memcpy(status, data.dptr, sizeof(print_status_struct));
1187 SAFE_FREE(data.dptr);
1189 return status->qcount;
1192 /****************************************************************************
1193 Determine the number of jobs in a queue.
1194 ****************************************************************************/
1196 int print_queue_length(int snum, print_status_struct *pstatus)
1198 print_status_struct status;
1201 /* make sure the database is up to date */
1202 if (print_cache_expired(snum))
1203 print_queue_update(snum);
1205 /* also fetch the queue status */
1206 memset(&status, 0, sizeof(status));
1207 len = get_queue_status(snum, &status);
1213 /****************************************************************************
1214 Determine the number of jobs in all queues. This is very expensive. Don't
1216 ****************************************************************************/
1218 static int get_total_jobs(void)
1222 int services = lp_numservices();
1224 for (snum = 0; snum < services; snum++) {
1225 struct tdb_print_db *pdb;
1228 if (!lp_print_ok(snum))
1231 pdb = get_print_db_byname(lp_const_servicename(snum));
1235 /* make sure the database is up to date */
1236 if (print_cache_expired(snum))
1237 print_queue_update(snum);
1239 jobs = tdb_fetch_int32(pdb->tdb, "INFO/total_jobs");
1246 /***************************************************************************
1247 Start spooling a job - return the jobid.
1248 ***************************************************************************/
1250 uint32 print_job_start(struct current_user *user, int snum, char *jobname)
1254 struct printjob pjob;
1258 const char *printername = lp_const_servicename(snum);
1259 struct tdb_print_db *pdb = get_print_db_byname(printername);
1266 if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
1267 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
1271 if (!print_time_access_check(snum)) {
1272 DEBUG(3, ("print_job_start: job start denied by time check\n"));
1276 path = lp_pathname(snum);
1278 /* see if we have sufficient disk space */
1279 if (lp_minprintspace(snum)) {
1280 SMB_BIG_UINT dspace, dsize;
1281 if (sys_fsusage(path, &dspace, &dsize) == 0 &&
1282 dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
1283 DEBUG(3, ("print_job_start: disk space check failed.\n"));
1289 /* for autoloaded printers, check that the printcap entry still exists */
1290 if (lp_autoloaded(snum) && !pcap_printername_ok(lp_const_servicename(snum), NULL)) {
1291 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum) ));
1296 /* Insure the maximum queue size is not violated */
1297 if (lp_maxprintjobs(snum) && (njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
1298 DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per queue (%d).\n",
1299 njobs, lp_maxprintjobs(snum) ));
1304 /* Insure the maximum print jobs in the system is not violated */
1305 if (lp_totalprintjobs() && get_total_jobs() > lp_totalprintjobs()) {
1306 DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per system (%d).\n",
1307 njobs, lp_totalprintjobs() ));
1312 /* create the database entry */
1314 pjob.pid = local_pid;
1317 pjob.starttime = time(NULL);
1318 pjob.status = LPQ_SPOOLING;
1320 pjob.spooled = False;
1323 fstrcpy(pjob.jobname, jobname);
1325 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1326 fstrcpy(pjob.user, vuser->user.smb_name);
1328 fstrcpy(pjob.user, uidtoname(user->uid));
1331 fstrcpy(pjob.queuename, lp_const_servicename(snum));
1333 /* lock the database */
1334 tdb_lock_bystring(pdb->tdb, "INFO/nextjob");
1336 next_jobid = tdb_fetch_int32(pdb->tdb, "INFO/nextjob");
1337 if (next_jobid == -1)
1340 for (jobid = NEXT_JOBID(next_jobid); jobid != next_jobid; jobid = NEXT_JOBID(jobid)) {
1341 if (!print_job_exists(snum, jobid))
1344 if (jobid == next_jobid || !pjob_store(snum, jobid, &pjob)) {
1345 DEBUG(3, ("print_job_start: either jobid (%d)==next_jobid(%d) or pjob_store failed.\n",
1346 jobid, next_jobid ));
1351 if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
1352 DEBUG(3, ("print_job_start: failed to store INFO/nextjob.\n"));
1357 /* we have a job entry - now create the spool file */
1358 slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.8u.XXXXXX",
1359 path, PRINT_SPOOL_PREFIX, (unsigned int)jobid);
1360 pjob.fd = smb_mkstemp(pjob.filename);
1362 if (pjob.fd == -1) {
1363 if (errno == EACCES) {
1364 /* Common setup error, force a report. */
1365 DEBUG(0, ("print_job_start: insufficient permissions \
1366 to open spool file %s.\n", pjob.filename));
1368 /* Normal case, report at level 3 and above. */
1369 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
1370 DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
1375 pjob_store(snum, jobid, &pjob);
1377 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1380 * If the printer is marked as postscript output a leading
1381 * file identifier to ensure the file is treated as a raw
1383 * This has a similar effect as CtrlD=0 in WIN.INI file.
1384 * tim@fsg.com 09/06/94
1386 if (lp_postscript(snum)) {
1387 print_job_write(snum, jobid, "%!\n",3);
1394 pjob_delete(snum, jobid);
1396 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1398 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
1402 /****************************************************************************
1403 Update the number of pages spooled to jobid
1404 ****************************************************************************/
1406 void print_job_endpage(int snum, uint32 jobid)
1408 struct printjob *pjob = print_job_find(snum, jobid);
1411 /* don't allow another process to get this info - it is meaningless */
1412 if (pjob->pid != local_pid)
1416 pjob_store(snum, jobid, pjob);
1419 /****************************************************************************
1420 Print a file - called on closing the file. This spools the job.
1421 If normal close is false then we're tearing down the jobs - treat as an
1423 ****************************************************************************/
1425 BOOL print_job_end(int snum, uint32 jobid, BOOL normal_close)
1427 struct printjob *pjob = print_job_find(snum, jobid);
1429 SMB_STRUCT_STAT sbuf;
1434 if (pjob->spooled || pjob->pid != local_pid)
1437 if (normal_close && (sys_fstat(pjob->fd, &sbuf) == 0)) {
1438 pjob->size = sbuf.st_size;
1444 * Not a normal close or we couldn't stat the job file,
1445 * so something has gone wrong. Cleanup.
1449 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
1453 /* Technically, this is not quite right. If the printer has a separator
1454 * page turned on, the NT spooler prints the separator page even if the
1455 * print job is 0 bytes. 010215 JRR */
1456 if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
1457 /* don't bother spooling empty files or something being deleted. */
1458 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
1459 pjob->filename, pjob->size ? "deleted" : "zero length" ));
1460 unlink(pjob->filename);
1461 pjob_delete(snum, jobid);
1465 ret = (*(current_printif->job_submit))(snum, pjob);
1470 /* The print job has been sucessfully handed over to the back-end */
1472 pjob->spooled = True;
1473 pjob->status = LPQ_QUEUED;
1474 pjob_store(snum, jobid, pjob);
1476 /* make sure the database is up to date */
1477 if (print_cache_expired(snum))
1478 print_queue_update(snum);
1484 /* The print job was not succesfully started. Cleanup */
1485 /* Still need to add proper error return propagation! 010122:JRR */
1486 unlink(pjob->filename);
1487 pjob_delete(snum, jobid);
1491 /****************************************************************************
1492 Utility fn to enumerate the print queue.
1493 ****************************************************************************/
1495 static int traverse_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
1497 struct traverse_struct *ts = (struct traverse_struct *)state;
1498 struct printjob pjob;
1502 if (data.dsize != sizeof(pjob) || key.dsize != sizeof(int))
1504 memcpy(&jobid, key.dptr, sizeof(jobid));
1505 memcpy(&pjob, data.dptr, sizeof(pjob));
1507 /* maybe it isn't for this queue */
1508 if (ts->snum != lp_servicenumber(pjob.queuename))
1511 if (ts->qcount >= ts->maxcount)
1516 ts->queue[i].job = jobid;
1517 ts->queue[i].size = pjob.size;
1518 ts->queue[i].page_count = pjob.page_count;
1519 ts->queue[i].status = pjob.status;
1520 ts->queue[i].priority = 1;
1521 ts->queue[i].time = pjob.starttime;
1522 fstrcpy(ts->queue[i].fs_user, pjob.user);
1523 fstrcpy(ts->queue[i].fs_file, pjob.jobname);
1530 struct traverse_count_struct {
1534 /****************************************************************************
1535 Utility fn to count the number of entries in the print queue.
1536 ****************************************************************************/
1538 static int traverse_count_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
1540 struct traverse_count_struct *ts = (struct traverse_count_struct *)state;
1541 struct printjob pjob;
1544 if (data.dsize != sizeof(pjob) || key.dsize != sizeof(int))
1546 memcpy(&jobid, key.dptr, sizeof(jobid));
1547 memcpy(&pjob, data.dptr, sizeof(pjob));
1549 /* maybe it isn't for this queue - this cannot happen with the tdb/printer code. JRA */
1550 if (ts->snum != lp_servicenumber(pjob.queuename))
1558 /****************************************************************************
1559 Sort print jobs by submittal time.
1560 ****************************************************************************/
1562 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
1573 /* Sort on job start time */
1575 if (j1->time == j2->time)
1577 return (j1->time > j2->time) ? 1 : -1;
1580 /****************************************************************************
1581 Get a printer queue listing.
1582 ****************************************************************************/
1584 int print_queue_status(int snum,
1585 print_queue_struct **queue,
1586 print_status_struct *status)
1588 struct traverse_struct tstruct;
1589 struct traverse_count_struct tsc;
1592 const char *printername = lp_const_servicename(snum);
1593 struct tdb_print_db *pdb = get_print_db_byname(printername);
1600 /* make sure the database is up to date */
1601 if (print_cache_expired(snum))
1602 print_queue_update(snum);
1605 * Fetch the queue status. We must do this first, as there may
1606 * be no jobs in the queue.
1608 ZERO_STRUCTP(status);
1609 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
1611 key.dsize = strlen(keystr);
1612 data = tdb_fetch(pdb->tdb, key);
1614 if (data.dsize == sizeof(*status)) {
1615 memcpy(status, data.dptr, sizeof(*status));
1617 SAFE_FREE(data.dptr);
1621 * Now, fetch the print queue information. We first count the number
1622 * of entries, and then only retrieve the queue if necessary.
1627 tdb_traverse(pdb->tdb, traverse_count_fn_queue, (void *)&tsc);
1632 /* Allocate the queue size. */
1633 if ((tstruct.queue = (print_queue_struct *)
1634 malloc(sizeof(print_queue_struct)*tsc.count)) == NULL)
1638 * Fill in the queue.
1639 * We need maxcount as the queue size may have changed between
1640 * the two calls to tdb_traverse.
1643 tstruct.maxcount = tsc.count;
1644 tstruct.snum = snum;
1646 tdb_traverse(pdb->tdb, traverse_fn_queue, (void *)&tstruct);
1648 /* Sort the queue by submission time otherwise they are displayed
1651 qsort(tstruct.queue, tstruct.qcount, sizeof(print_queue_struct),
1652 QSORT_CAST(printjob_comp));
1654 *queue = tstruct.queue;
1655 return tstruct.qcount;
1658 /****************************************************************************
1659 Turn a queue name into a snum.
1660 ****************************************************************************/
1662 int print_queue_snum(const char *qname)
1664 int snum = lp_servicenumber(qname);
1665 if (snum == -1 || !lp_print_ok(snum))
1670 /****************************************************************************
1672 ****************************************************************************/
1674 BOOL print_queue_pause(struct current_user *user, int snum, WERROR *errcode)
1678 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
1679 *errcode = WERR_ACCESS_DENIED;
1683 ret = (*(current_printif->queue_pause))(snum);
1686 *errcode = WERR_INVALID_PARAM;
1690 /* force update the database */
1691 print_cache_flush(snum);
1693 /* Send a printer notify message */
1695 notify_printer_status(snum, PRINTER_STATUS_PAUSED);
1700 /****************************************************************************
1702 ****************************************************************************/
1704 BOOL print_queue_resume(struct current_user *user, int snum, WERROR *errcode)
1708 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
1709 *errcode = WERR_ACCESS_DENIED;
1713 ret = (*(current_printif->queue_resume))(snum);
1716 *errcode = WERR_INVALID_PARAM;
1720 /* make sure the database is up to date */
1721 if (print_cache_expired(snum))
1722 print_queue_update(snum);
1724 /* Send a printer notify message */
1726 notify_printer_status(snum, PRINTER_STATUS_OK);
1731 /****************************************************************************
1732 Purge a queue - implemented by deleting all jobs that we can delete.
1733 ****************************************************************************/
1735 BOOL print_queue_purge(struct current_user *user, int snum, WERROR *errcode)
1737 print_queue_struct *queue;
1738 print_status_struct status;
1742 /* Force and update so the count is accurate (i.e. not a cached count) */
1743 print_queue_update(snum);
1745 can_job_admin = print_access_check(user, snum, JOB_ACCESS_ADMINISTER);
1746 njobs = print_queue_status(snum, &queue, &status);
1748 for (i=0;i<njobs;i++) {
1749 BOOL owner = is_owner(user, snum, queue[i].job);
1751 if (owner || can_job_admin) {
1752 print_job_delete1(snum, queue[i].job);