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 /* There can be this many printing tdb's open, plus any locked ones. */
136 #define MAX_PRINT_DBS_OPEN 1
138 struct tdb_print_db {
139 struct tdb_print_db *next, *prev;
142 fstring printer_name;
145 static struct tdb_print_db *print_db_head;
147 /****************************************************************************
148 Function to find or create the printer specific job tdb given a printername.
149 Limits the number of tdb's open to MAX_PRINT_DBS_OPEN.
150 ****************************************************************************/
152 static struct tdb_print_db *get_print_db_byname(const char *printername)
154 struct tdb_print_db *p = NULL, *last_entry = NULL;
156 pstring printdb_path;
158 for (p = print_db_head, last_entry = print_db_head; p; p = p->next) {
159 if (p->tdb && strequal(p->printer_name, printername)) {
160 DLIST_PROMOTE(print_db_head, p);
169 if (num_open >= MAX_PRINT_DBS_OPEN) {
170 /* Try and recycle the last entry. */
171 DLIST_PROMOTE(print_db_head, last_entry);
173 for (p = print_db_head; p; p = p->next) {
177 if (tdb_close(print_db_head->tdb)) {
178 DEBUG(0,("get_print_db: Failed to close tdb for printer %s\n",
179 print_db_head->printer_name ));
187 DLIST_PROMOTE(print_db_head, p);
194 p = (struct tdb_print_db *)malloc(sizeof(struct tdb_print_db));
196 DEBUG(0,("get_print_db: malloc fail !\n"));
200 DLIST_ADD(print_db_head, p);
203 pstrcpy(printdb_path, lock_path("printing/"));
204 pstrcat(printdb_path, printername);
205 pstrcat(printdb_path, ".tdb");
208 p->tdb = tdb_open_log(printdb_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
212 DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
214 DLIST_REMOVE(print_db_head, p);
218 fstrcpy(p->printer_name, printername);
223 static void release_print_db( struct tdb_print_db *pdb)
226 SMB_ASSERT(pdb->ref_count >= 0);
229 /****************************************************************************
230 Initialise the printing backend. Called once at startup.
231 Does not survive a fork
232 ****************************************************************************/
234 BOOL print_backend_init(void)
236 char *sversion = "INFO/version";
237 pstring printing_path;
238 int services = lp_numservices();
241 if (local_pid == sys_getpid())
244 unlink(lock_path("printing.tdb"));
245 pstrcpy(printing_path,lock_path("printing"));
246 mkdir(printing_path,0755);
248 local_pid = sys_getpid();
250 /* handle a Samba upgrade */
252 for (snum = 0; snum < services; snum++) {
253 struct tdb_print_db *pdb;
254 if (!lp_print_ok(snum))
257 pdb = get_print_db_byname(lp_const_servicename(snum));
260 if (tdb_lock_bystring(pdb->tdb, sversion) == -1) {
261 DEBUG(0,("print_backend_init: Failed to open printer %s database\n", lp_const_servicename(snum) ));
264 if (tdb_fetch_int32(pdb->tdb, sversion) != PRINT_DATABASE_VERSION) {
265 tdb_traverse(pdb->tdb, tdb_traverse_delete_fn, NULL);
266 tdb_store_int32(pdb->tdb, sversion, PRINT_DATABASE_VERSION);
268 tdb_unlock_bystring(pdb->tdb, sversion);
271 /* select the appropriate printing interface... */
273 if (strcmp(lp_printcapname(), "cups") == 0)
274 current_printif = &cups_printif;
275 #endif /* HAVE_CUPS */
277 /* do NT print initialization... */
278 return nt_printing_init();
281 /****************************************************************************
282 Shut down printing backend. Called once at shutdown to close the tdb.
283 ****************************************************************************/
285 void printing_end(void)
287 struct tdb_print_db *p;
289 for (p = print_db_head; p; ) {
290 struct tdb_print_db *next_p = p->next;
293 DLIST_REMOVE(print_db_head, p);
299 /****************************************************************************
300 Useful function to generate a tdb key.
301 ****************************************************************************/
303 static TDB_DATA print_key(uint32 jobid)
309 ret.dptr = (void *)&j;
310 ret.dsize = sizeof(j);
314 /****************************************************************************
315 Useful function to find a print job in the database.
316 ****************************************************************************/
318 static struct printjob *print_job_find(int snum, uint32 jobid)
320 static struct printjob pjob;
322 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
327 ret = tdb_fetch(pdb->tdb, print_key(jobid));
328 release_print_db(pdb);
330 if (!ret.dptr || ret.dsize != sizeof(pjob))
333 memcpy(&pjob, ret.dptr, sizeof(pjob));
338 /* Convert a unix jobid to a smb jobid */
340 static uint32 sysjob_to_jobid_value;
342 static int unixjob_traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key,
343 TDB_DATA data, void *state)
345 struct printjob *pjob = (struct printjob *)data.dptr;
346 int *sysjob = (int *)state;
348 if (key.dsize != sizeof(uint32))
351 if (*sysjob == pjob->sysjob) {
352 uint32 *jobid = (uint32 *)key.dptr;
354 sysjob_to_jobid_value = *jobid;
361 /****************************************************************************
362 This is a *horribly expensive call as we have to iterate through all the
363 current printer tdb's. Don't do this often ! JRA.
364 ****************************************************************************/
366 uint32 sysjob_to_jobid(int unix_jobid)
368 int services = lp_numservices();
371 sysjob_to_jobid_value = (uint32)-1;
373 for (snum = 0; snum < services; snum++) {
374 struct tdb_print_db *pdb;
375 if (!lp_print_ok(snum))
377 pdb = get_print_db_byname(lp_const_servicename(snum));
379 tdb_traverse(pdb->tdb, unixjob_traverse_fn, &unix_jobid);
380 release_print_db(pdb);
381 if (sysjob_to_jobid_value != (uint32)-1)
382 return sysjob_to_jobid_value;
387 /****************************************************************************
388 Send notifications based on what has changed after a pjob_store.
389 ****************************************************************************/
393 uint32 spoolss_status;
394 } lpq_to_spoolss_status_map[] = {
395 { LPQ_QUEUED, JOB_STATUS_QUEUED },
396 { LPQ_PAUSED, JOB_STATUS_PAUSED },
397 { LPQ_SPOOLING, JOB_STATUS_SPOOLING },
398 { LPQ_PRINTING, JOB_STATUS_PRINTING },
399 { LPQ_DELETING, JOB_STATUS_DELETING },
400 { LPQ_OFFLINE, JOB_STATUS_OFFLINE },
401 { LPQ_PAPEROUT, JOB_STATUS_PAPEROUT },
402 { LPQ_PRINTED, JOB_STATUS_PRINTED },
403 { LPQ_DELETED, JOB_STATUS_DELETED },
404 { LPQ_BLOCKED, JOB_STATUS_BLOCKED },
405 { LPQ_USER_INTERVENTION, JOB_STATUS_USER_INTERVENTION },
409 /* Convert a lpq status value stored in printing.tdb into the
410 appropriate win32 API constant. */
412 static uint32 map_to_spoolss_status(uint32 lpq_status)
416 while (lpq_to_spoolss_status_map[i].lpq_status != -1) {
417 if (lpq_to_spoolss_status_map[i].lpq_status == lpq_status)
418 return lpq_to_spoolss_status_map[i].spoolss_status;
425 static void pjob_store_notify(int snum, uint32 jobid, struct printjob *old_data,
426 struct printjob *new_data)
428 BOOL new_job = False;
433 /* Notify the job name first */
435 if (new_job || !strequal(old_data->jobname, new_data->jobname))
436 notify_job_name(snum, jobid, new_data->jobname);
438 /* Job attributes that can't be changed. We only send
439 notification for these on a new job. */
442 notify_job_submitted(snum, jobid, new_data->starttime);
443 notify_job_username(snum, jobid, new_data->user);
446 /* Job attributes of a new job or attributes that can be
449 if (new_job || old_data->status != new_data->status)
450 notify_job_status(snum, jobid, map_to_spoolss_status(new_data->status));
452 if (new_job || old_data->size != new_data->size)
453 notify_job_total_bytes(snum, jobid, new_data->size);
455 if (new_job || old_data->page_count != new_data->page_count)
456 notify_job_total_pages(snum, jobid, new_data->page_count);
459 /****************************************************************************
460 Store a job structure back to the database.
461 ****************************************************************************/
463 static BOOL pjob_store(int snum, uint32 jobid, struct printjob *pjob)
465 TDB_DATA old_data, new_data;
467 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
474 old_data = tdb_fetch(pdb->tdb, print_key(jobid));
478 new_data.dptr = (void *)pjob;
479 new_data.dsize = sizeof(*pjob);
480 ret = (tdb_store(pdb->tdb, print_key(jobid), new_data, TDB_REPLACE) == 0);
482 release_print_db(pdb);
484 /* Send notify updates for what has changed */
486 if (ret && (old_data.dsize == 0 || old_data.dsize == sizeof(*pjob))) {
488 snum, jobid, (struct printjob *)old_data.dptr,
489 (struct printjob *)new_data.dptr);
496 /****************************************************************************
497 Remove a job structure from the database.
498 ****************************************************************************/
500 static void pjob_delete(int snum, uint32 jobid)
502 struct printjob *pjob = print_job_find(snum, jobid);
503 uint32 job_status = 0;
504 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
510 DEBUG(5, ("pjob_delete(): we were asked to delete nonexistent job %u\n",
511 (unsigned int)jobid));
512 release_print_db(pdb);
516 /* Send a notification that a job has been deleted */
518 job_status = map_to_spoolss_status(pjob->status);
520 /* We must cycle through JOB_STATUS_DELETING and
521 JOB_STATUS_DELETED for the port monitor to delete the job
524 job_status |= JOB_STATUS_DELETING;
525 notify_job_status(snum, jobid, job_status);
527 job_status |= JOB_STATUS_DELETED;
528 notify_job_status(snum, jobid, job_status);
530 /* Remove from printing.tdb */
532 tdb_delete(pdb->tdb, print_key(jobid));
533 release_print_db(pdb);
534 rap_jobid_delete(snum, jobid);
537 /****************************************************************************
538 Parse a file name from the system spooler to generate a jobid.
539 ****************************************************************************/
541 static uint32 print_parse_jobid(char *fname)
545 if (strncmp(fname,PRINT_SPOOL_PREFIX,strlen(PRINT_SPOOL_PREFIX)) != 0)
547 fname += strlen(PRINT_SPOOL_PREFIX);
553 return (uint32)jobid;
556 /****************************************************************************
557 List a unix job in the print database.
558 ****************************************************************************/
560 static void print_unix_job(int snum, print_queue_struct *q)
562 uint32 jobid = q->job + UNIX_JOB_START;
563 struct printjob pj, *old_pj;
565 /* Preserve the timestamp on an existing unix print job */
567 old_pj = print_job_find(snum, jobid);
574 pj.starttime = old_pj ? old_pj->starttime : q->time;
575 pj.status = q->status;
579 fstrcpy(pj.filename, "");
580 fstrcpy(pj.jobname, q->fs_file);
581 fstrcpy(pj.user, q->fs_user);
582 fstrcpy(pj.queuename, lp_const_servicename(snum));
584 pjob_store(snum, jobid, &pj);
588 struct traverse_struct {
589 print_queue_struct *queue;
590 int qcount, snum, maxcount, total_jobs;
593 /****************************************************************************
594 Utility fn to delete any jobs that are no longer active.
595 ****************************************************************************/
597 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
599 struct traverse_struct *ts = (struct traverse_struct *)state;
600 struct printjob pjob;
604 if (data.dsize != sizeof(pjob) || key.dsize != sizeof(jobid))
606 memcpy(&jobid, key.dptr, sizeof(jobid));
607 memcpy(&pjob, data.dptr, sizeof(pjob));
609 if (ts->snum != lp_servicenumber(pjob.queuename)) {
610 /* this isn't for the queue we are looking at - this cannot happen with the split tdb's. JRA */
615 /* remove a unix job if it isn't in the system queue any more */
617 for (i=0;i<ts->qcount;i++) {
618 uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
619 if (jobid == u_jobid)
623 pjob_delete(ts->snum, jobid);
629 /* maybe it hasn't been spooled yet */
631 /* if a job is not spooled and the process doesn't
632 exist then kill it. This cleans up after smbd
634 if (!process_exists(pjob.pid))
635 pjob_delete(ts->snum, jobid);
641 for (i=0;i<ts->qcount;i++) {
642 uint32 curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
643 if (jobid == curr_jobid)
647 /* The job isn't in the system queue - we have to assume it has
648 completed, so delete the database entry. */
650 if (i == ts->qcount) {
651 time_t cur_t = time(NULL);
653 /* A race can occur between the time a job is spooled and
654 when it appears in the lpq output. This happens when
655 the job is added to printing.tdb when another smbd
656 running print_queue_update() has completed a lpq and
657 is currently traversing the printing tdb and deleting jobs.
658 A workaround is to not delete the job if it has been
659 submitted less than lp_lpqcachetime() seconds ago. */
661 if ((cur_t - pjob.starttime) > lp_lpqcachetime())
662 pjob_delete(ts->snum, jobid);
672 /****************************************************************************
673 Check if the print queue has been updated recently enough.
674 ****************************************************************************/
676 static void print_cache_flush(int snum)
679 const char *printername = lp_const_servicename(snum);
680 struct tdb_print_db *pdb = get_print_db_byname(printername);
684 slprintf(key, sizeof(key)-1, "CACHE/%s", printername);
685 tdb_store_int32(pdb->tdb, key, -1);
686 release_print_db(pdb);
689 /****************************************************************************
690 Check if someone already thinks they are doing the update.
691 ****************************************************************************/
693 static pid_t get_updating_pid(fstring printer_name)
698 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
702 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
704 key.dsize = strlen(keystr);
706 data = tdb_fetch(pdb->tdb, key);
707 release_print_db(pdb);
708 if (!data.dptr || data.dsize != sizeof(pid_t))
711 memcpy(&updating_pid, data.dptr, sizeof(pid_t));
712 SAFE_FREE(data.dptr);
714 if (process_exists(updating_pid))
720 /****************************************************************************
721 Set the fact that we're doing the update, or have finished doing the update
723 ****************************************************************************/
725 static void set_updating_pid(const fstring printer_name, BOOL delete)
730 pid_t updating_pid = sys_getpid();
731 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
736 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
738 key.dsize = strlen(keystr);
741 tdb_delete(pdb->tdb, key);
742 release_print_db(pdb);
746 data.dptr = (void *)&updating_pid;
747 data.dsize = sizeof(pid_t);
749 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
750 release_print_db(pdb);
753 /****************************************************************************
754 Update the internal database from the system print queue for a queue.
755 ****************************************************************************/
757 static void print_queue_update(int snum)
760 print_queue_struct *queue = NULL;
761 print_status_struct status;
762 print_status_struct old_status;
763 struct printjob *pjob;
764 struct traverse_struct tstruct;
765 fstring keystr, printer_name, cachestr;
767 struct tdb_print_db *pdb;
769 fstrcpy(printer_name, lp_const_servicename(snum));
770 pdb = get_print_db_byname(printer_name);
775 * Check to see if someone else is doing this update.
776 * This is essentially a mutex on the update.
779 if (get_updating_pid(printer_name) != -1) {
780 release_print_db(pdb);
784 /* Lock the queue for the database update */
786 slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", printer_name);
787 if (tdb_lock_bystring(pdb->tdb, keystr) == -1) {
788 DEBUG(0,("print_queue_update: Failed to lock printer %s database\n", printer_name));
789 release_print_db(pdb);
794 * Ensure that no one else got in here.
795 * If the updating pid is still -1 then we are
799 if (get_updating_pid(printer_name) != -1) {
801 * Someone else is doing the update, exit.
803 tdb_unlock_bystring(pdb->tdb, keystr);
804 release_print_db(pdb);
809 * We're going to do the update ourselves.
812 /* Tell others we're doing the update. */
813 set_updating_pid(printer_name, False);
816 * Allow others to enter and notice we're doing
820 tdb_unlock_bystring(pdb->tdb, keystr);
823 * Update the cache time FIRST ! Stops others even
824 * attempting to get the lock and doing this
825 * if the lpq takes a long time.
828 slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", printer_name);
829 tdb_store_int32(pdb->tdb, cachestr, (int)time(NULL));
831 /* get the current queue using the appropriate interface */
834 qcount = (*(current_printif->queue_get))(snum, &queue, &status);
836 DEBUG(3, ("%d job%s in queue for %s\n", qcount, (qcount != 1) ?
837 "s" : "", printer_name));
840 any job in the internal database that is marked as spooled
841 and doesn't exist in the system queue is considered finished
842 and removed from the database
844 any job in the system database but not in the internal database
845 is added as a unix job
847 fill in any system job numbers as we go
849 for (i=0; i<qcount; i++) {
850 uint32 jobid = print_parse_jobid(queue[i].fs_file);
852 if (jobid == (uint32)-1) {
853 /* assume its a unix print job */
854 print_unix_job(snum, &queue[i]);
858 /* we have an active SMB print job - update its status */
859 pjob = print_job_find(snum, jobid);
861 /* err, somethings wrong. Probably smbd was restarted
862 with jobs in the queue. All we can do is treat them
863 like unix jobs. Pity. */
864 print_unix_job(snum, &queue[i]);
868 pjob->sysjob = queue[i].job;
869 pjob->status = queue[i].status;
871 pjob_store(snum, jobid, pjob);
874 /* now delete any queued entries that don't appear in the
876 tstruct.queue = queue;
877 tstruct.qcount = qcount;
879 tstruct.total_jobs = 0;
881 tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
883 SAFE_FREE(tstruct.queue);
885 tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
887 if( qcount != get_queue_status(snum, &old_status))
888 DEBUG(10,("print_queue_update: queue status change %d jobs -> %d jobs for printer %s\n",
889 old_status.qcount, qcount, printer_name ));
891 /* store the new queue status structure */
892 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printer_name);
894 key.dsize = strlen(keystr);
896 status.qcount = qcount;
897 data.dptr = (void *)&status;
898 data.dsize = sizeof(status);
899 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
902 * Update the cache time again. We want to do this call
903 * as little as possible...
906 slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", printer_name);
907 tdb_store_int32(pdb->tdb, keystr, (int32)time(NULL));
909 /* Delete our pid from the db. */
910 set_updating_pid(printer_name, True);
911 release_print_db(pdb);
914 /****************************************************************************
915 Check if a jobid is valid. It is valid if it exists in the database.
916 ****************************************************************************/
918 BOOL print_job_exists(int snum, uint32 jobid)
920 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
925 ret = tdb_exists(pdb->tdb, print_key(jobid));
926 release_print_db(pdb);
930 /****************************************************************************
931 Give the fd used for a jobid.
932 ****************************************************************************/
934 int print_job_fd(int snum, uint32 jobid)
936 struct printjob *pjob = print_job_find(snum, jobid);
939 /* don't allow another process to get this info - it is meaningless */
940 if (pjob->pid != local_pid)
945 /****************************************************************************
946 Give the filename used for a jobid.
947 Only valid for the process doing the spooling and when the job
948 has not been spooled.
949 ****************************************************************************/
951 char *print_job_fname(int snum, uint32 jobid)
953 struct printjob *pjob = print_job_find(snum, jobid);
954 if (!pjob || pjob->spooled || pjob->pid != local_pid)
956 return pjob->filename;
959 /****************************************************************************
960 Set the place in the queue for a job.
961 ****************************************************************************/
963 BOOL print_job_set_place(int snum, uint32 jobid, int place)
965 DEBUG(2,("print_job_set_place not implemented yet\n"));
969 /****************************************************************************
970 Set the name of a job. Only possible for owner.
971 ****************************************************************************/
973 BOOL print_job_set_name(int snum, uint32 jobid, char *name)
975 struct printjob *pjob = print_job_find(snum, jobid);
976 if (!pjob || pjob->pid != local_pid)
979 fstrcpy(pjob->jobname, name);
980 return pjob_store(snum, jobid, pjob);
983 /****************************************************************************
984 Delete a print job - don't update queue.
985 ****************************************************************************/
987 static BOOL print_job_delete1(int snum, uint32 jobid)
989 struct printjob *pjob = print_job_find(snum, jobid);
996 * If already deleting just return.
999 if (pjob->status == LPQ_DELETING)
1002 /* Hrm - we need to be able to cope with deleting a job before it
1003 has reached the spooler. */
1005 if (pjob->sysjob == -1) {
1006 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid));
1009 /* Set the tdb entry to be deleting. */
1011 pjob->status = LPQ_DELETING;
1012 pjob_store(snum, jobid, pjob);
1014 if (pjob->spooled && pjob->sysjob != -1)
1015 result = (*(current_printif->job_delete))(snum, pjob);
1017 /* Delete the tdb entry if the delete suceeded or the job hasn't
1021 pjob_delete(snum, jobid);
1023 return (result == 0);
1026 /****************************************************************************
1027 Return true if the current user owns the print job.
1028 ****************************************************************************/
1030 static BOOL is_owner(struct current_user *user, int snum, uint32 jobid)
1032 struct printjob *pjob = print_job_find(snum, jobid);
1038 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1039 return strequal(pjob->user, vuser->user.smb_name);
1041 return strequal(pjob->user, uidtoname(user->uid));
1045 /****************************************************************************
1047 ****************************************************************************/
1049 BOOL print_job_delete(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1053 owner = is_owner(user, snum, jobid);
1055 /* Check access against security descriptor or whether the user
1059 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1060 DEBUG(3, ("delete denied by security descriptor\n"));
1061 *errcode = WERR_ACCESS_DENIED;
1065 if (!print_job_delete1(snum, jobid))
1068 /* force update the database and say the delete failed if the
1071 print_queue_update(snum);
1073 return !print_job_exists(snum, jobid);
1076 /****************************************************************************
1078 ****************************************************************************/
1080 BOOL print_job_pause(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1082 struct printjob *pjob = print_job_find(snum, jobid);
1088 if (!pjob->spooled || pjob->sysjob == -1)
1091 if (!is_owner(user, snum, jobid) &&
1092 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1093 DEBUG(3, ("pause denied by security descriptor\n"));
1094 *errcode = WERR_ACCESS_DENIED;
1098 /* need to pause the spooled entry */
1099 ret = (*(current_printif->job_pause))(snum, pjob);
1102 *errcode = WERR_INVALID_PARAM;
1106 /* force update the database */
1107 print_cache_flush(snum);
1109 /* Send a printer notify message */
1111 notify_job_status(snum, jobid, JOB_STATUS_PAUSED);
1113 /* how do we tell if this succeeded? */
1118 /****************************************************************************
1120 ****************************************************************************/
1122 BOOL print_job_resume(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1124 struct printjob *pjob = print_job_find(snum, jobid);
1130 if (!pjob->spooled || pjob->sysjob == -1)
1133 if (!is_owner(user, snum, jobid) &&
1134 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1135 DEBUG(3, ("resume denied by security descriptor\n"));
1136 *errcode = WERR_ACCESS_DENIED;
1140 ret = (*(current_printif->job_resume))(snum, pjob);
1143 *errcode = WERR_INVALID_PARAM;
1147 /* force update the database */
1148 print_cache_flush(snum);
1150 /* Send a printer notify message */
1152 notify_job_status(snum, jobid, JOB_STATUS_QUEUED);
1157 /****************************************************************************
1158 Write to a print file.
1159 ****************************************************************************/
1161 int print_job_write(int snum, uint32 jobid, const char *buf, int size)
1164 struct printjob *pjob = print_job_find(snum, jobid);
1168 /* don't allow another process to get this info - it is meaningless */
1169 if (pjob->pid != local_pid)
1172 return_code = write(pjob->fd, buf, size);
1173 if (return_code>0) {
1175 pjob_store(snum, jobid, pjob);
1180 /****************************************************************************
1181 Check if the print queue has been updated recently enough.
1182 ****************************************************************************/
1184 static BOOL print_cache_expired(int snum)
1187 time_t last_qscan_time, time_now = time(NULL);
1188 const char *printername = lp_const_servicename(snum);
1189 struct tdb_print_db *pdb = get_print_db_byname(printername);
1194 slprintf(key, sizeof(key), "CACHE/%s", printername);
1195 last_qscan_time = (time_t)tdb_fetch_int32(pdb->tdb, key);
1198 * Invalidate the queue for 3 reasons.
1199 * (1). last queue scan time == -1.
1200 * (2). Current time - last queue scan time > allowed cache time.
1201 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1202 * This last test picks up machines for which the clock has been moved
1203 * forward, an lpq scan done and then the clock moved back. Otherwise
1204 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1207 if (last_qscan_time == ((time_t)-1) || (time_now - last_qscan_time) >= lp_lpqcachetime() ||
1208 last_qscan_time > (time_now + MAX_CACHE_VALID_TIME)) {
1209 DEBUG(3, ("print cache expired for queue %s \
1210 (last_qscan_time = %d, time now = %d, qcachetime = %d)\n", printername,
1211 (int)last_qscan_time, (int)time_now, (int)lp_lpqcachetime() ));
1212 release_print_db(pdb);
1215 release_print_db(pdb);
1219 /****************************************************************************
1220 Get the queue status - do not update if db is out of date.
1221 ****************************************************************************/
1223 static int get_queue_status(int snum, print_status_struct *status)
1227 const char *printername = lp_const_servicename(snum);
1228 struct tdb_print_db *pdb = get_print_db_byname(printername);
1232 ZERO_STRUCTP(status);
1233 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
1235 key.dsize = strlen(keystr);
1236 data = tdb_fetch(pdb->tdb, key);
1237 release_print_db(pdb);
1239 if (data.dsize == sizeof(print_status_struct)) {
1240 memcpy(status, data.dptr, sizeof(print_status_struct));
1242 SAFE_FREE(data.dptr);
1244 return status->qcount;
1247 /****************************************************************************
1248 Determine the number of jobs in a queue.
1249 ****************************************************************************/
1251 int print_queue_length(int snum, print_status_struct *pstatus)
1253 print_status_struct status;
1256 /* make sure the database is up to date */
1257 if (print_cache_expired(snum))
1258 print_queue_update(snum);
1260 /* also fetch the queue status */
1261 memset(&status, 0, sizeof(status));
1262 len = get_queue_status(snum, &status);
1268 /***************************************************************************
1269 Start spooling a job - return the jobid.
1270 ***************************************************************************/
1272 uint32 print_job_start(struct current_user *user, int snum, char *jobname)
1276 struct printjob pjob;
1280 const char *printername = lp_const_servicename(snum);
1281 struct tdb_print_db *pdb = get_print_db_byname(printername);
1288 if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
1289 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
1290 release_print_db(pdb);
1294 if (!print_time_access_check(snum)) {
1295 DEBUG(3, ("print_job_start: job start denied by time check\n"));
1296 release_print_db(pdb);
1300 path = lp_pathname(snum);
1302 /* see if we have sufficient disk space */
1303 if (lp_minprintspace(snum)) {
1304 SMB_BIG_UINT dspace, dsize;
1305 if (sys_fsusage(path, &dspace, &dsize) == 0 &&
1306 dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
1307 DEBUG(3, ("print_job_start: disk space check failed.\n"));
1308 release_print_db(pdb);
1314 /* for autoloaded printers, check that the printcap entry still exists */
1315 if (lp_autoloaded(snum) && !pcap_printername_ok(lp_const_servicename(snum), NULL)) {
1316 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum) ));
1317 release_print_db(pdb);
1322 /* Insure the maximum queue size is not violated */
1323 if (lp_maxprintjobs(snum) && (njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
1324 DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per queue (%d).\n",
1325 njobs, lp_maxprintjobs(snum) ));
1326 release_print_db(pdb);
1331 /* create the database entry */
1333 pjob.pid = local_pid;
1336 pjob.starttime = time(NULL);
1337 pjob.status = LPQ_SPOOLING;
1339 pjob.spooled = False;
1342 fstrcpy(pjob.jobname, jobname);
1344 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1345 fstrcpy(pjob.user, vuser->user.smb_name);
1347 fstrcpy(pjob.user, uidtoname(user->uid));
1350 fstrcpy(pjob.queuename, lp_const_servicename(snum));
1352 /* lock the database */
1353 if (tdb_lock_bystring(pdb->tdb, "INFO/nextjob") == -1) {
1354 DEBUG(0,("print_job_start: failed to lock printing database %s\n", printername ));
1355 release_print_db(pdb);
1359 next_jobid = tdb_fetch_int32(pdb->tdb, "INFO/nextjob");
1360 if (next_jobid == -1)
1363 for (jobid = NEXT_JOBID(next_jobid); jobid != next_jobid; jobid = NEXT_JOBID(jobid)) {
1364 if (!print_job_exists(snum, jobid))
1367 if (jobid == next_jobid || !pjob_store(snum, jobid, &pjob)) {
1368 DEBUG(3, ("print_job_start: either jobid (%d)==next_jobid(%d) or pjob_store failed.\n",
1369 jobid, next_jobid ));
1374 if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
1375 DEBUG(3, ("print_job_start: failed to store INFO/nextjob.\n"));
1380 /* We've finished with the INFO/nextjob lock. */
1381 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1383 /* we have a job entry - now create the spool file */
1384 slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.8u.XXXXXX",
1385 path, PRINT_SPOOL_PREFIX, (unsigned int)jobid);
1386 pjob.fd = smb_mkstemp(pjob.filename);
1388 if (pjob.fd == -1) {
1389 if (errno == EACCES) {
1390 /* Common setup error, force a report. */
1391 DEBUG(0, ("print_job_start: insufficient permissions \
1392 to open spool file %s.\n", pjob.filename));
1394 /* Normal case, report at level 3 and above. */
1395 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
1396 DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
1401 pjob_store(snum, jobid, &pjob);
1403 release_print_db(pdb);
1406 * If the printer is marked as postscript output a leading
1407 * file identifier to ensure the file is treated as a raw
1409 * This has a similar effect as CtrlD=0 in WIN.INI file.
1410 * tim@fsg.com 09/06/94
1412 if (lp_postscript(snum)) {
1413 print_job_write(snum, jobid, "%!\n",3);
1420 pjob_delete(snum, jobid);
1422 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1423 release_print_db(pdb);
1425 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
1429 /****************************************************************************
1430 Update the number of pages spooled to jobid
1431 ****************************************************************************/
1433 void print_job_endpage(int snum, uint32 jobid)
1435 struct printjob *pjob = print_job_find(snum, jobid);
1438 /* don't allow another process to get this info - it is meaningless */
1439 if (pjob->pid != local_pid)
1443 pjob_store(snum, jobid, pjob);
1446 /****************************************************************************
1447 Print a file - called on closing the file. This spools the job.
1448 If normal close is false then we're tearing down the jobs - treat as an
1450 ****************************************************************************/
1452 BOOL print_job_end(int snum, uint32 jobid, BOOL normal_close)
1454 struct printjob *pjob = print_job_find(snum, jobid);
1456 SMB_STRUCT_STAT sbuf;
1461 if (pjob->spooled || pjob->pid != local_pid)
1464 if (normal_close && (sys_fstat(pjob->fd, &sbuf) == 0)) {
1465 pjob->size = sbuf.st_size;
1471 * Not a normal close or we couldn't stat the job file,
1472 * so something has gone wrong. Cleanup.
1476 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
1480 /* Technically, this is not quite right. If the printer has a separator
1481 * page turned on, the NT spooler prints the separator page even if the
1482 * print job is 0 bytes. 010215 JRR */
1483 if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
1484 /* don't bother spooling empty files or something being deleted. */
1485 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
1486 pjob->filename, pjob->size ? "deleted" : "zero length" ));
1487 unlink(pjob->filename);
1488 pjob_delete(snum, jobid);
1492 ret = (*(current_printif->job_submit))(snum, pjob);
1497 /* The print job has been sucessfully handed over to the back-end */
1499 pjob->spooled = True;
1500 pjob->status = LPQ_QUEUED;
1501 pjob_store(snum, jobid, pjob);
1503 /* make sure the database is up to date */
1504 if (print_cache_expired(snum))
1505 print_queue_update(snum);
1511 /* The print job was not succesfully started. Cleanup */
1512 /* Still need to add proper error return propagation! 010122:JRR */
1513 unlink(pjob->filename);
1514 pjob_delete(snum, jobid);
1518 /****************************************************************************
1519 Utility fn to enumerate the print queue.
1520 ****************************************************************************/
1522 static int traverse_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
1524 struct traverse_struct *ts = (struct traverse_struct *)state;
1525 struct printjob pjob;
1529 if (data.dsize != sizeof(pjob) || key.dsize != sizeof(int))
1531 memcpy(&jobid, key.dptr, sizeof(jobid));
1532 memcpy(&pjob, data.dptr, sizeof(pjob));
1534 /* maybe it isn't for this queue */
1535 if (ts->snum != lp_servicenumber(pjob.queuename))
1538 if (ts->qcount >= ts->maxcount)
1543 ts->queue[i].job = jobid;
1544 ts->queue[i].size = pjob.size;
1545 ts->queue[i].page_count = pjob.page_count;
1546 ts->queue[i].status = pjob.status;
1547 ts->queue[i].priority = 1;
1548 ts->queue[i].time = pjob.starttime;
1549 fstrcpy(ts->queue[i].fs_user, pjob.user);
1550 fstrcpy(ts->queue[i].fs_file, pjob.jobname);
1557 struct traverse_count_struct {
1561 /****************************************************************************
1562 Utility fn to count the number of entries in the print queue.
1563 ****************************************************************************/
1565 static int traverse_count_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
1567 struct traverse_count_struct *ts = (struct traverse_count_struct *)state;
1568 struct printjob pjob;
1571 if (data.dsize != sizeof(pjob) || key.dsize != sizeof(int))
1573 memcpy(&jobid, key.dptr, sizeof(jobid));
1574 memcpy(&pjob, data.dptr, sizeof(pjob));
1576 /* maybe it isn't for this queue - this cannot happen with the tdb/printer code. JRA */
1577 if (ts->snum != lp_servicenumber(pjob.queuename))
1585 /****************************************************************************
1586 Sort print jobs by submittal time.
1587 ****************************************************************************/
1589 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
1600 /* Sort on job start time */
1602 if (j1->time == j2->time)
1604 return (j1->time > j2->time) ? 1 : -1;
1607 /****************************************************************************
1608 Get a printer queue listing.
1609 ****************************************************************************/
1611 int print_queue_status(int snum,
1612 print_queue_struct **queue,
1613 print_status_struct *status)
1615 struct traverse_struct tstruct;
1616 struct traverse_count_struct tsc;
1619 const char *printername = lp_const_servicename(snum);
1620 struct tdb_print_db *pdb = get_print_db_byname(printername);
1627 /* make sure the database is up to date */
1628 if (print_cache_expired(snum))
1629 print_queue_update(snum);
1632 * Fetch the queue status. We must do this first, as there may
1633 * be no jobs in the queue.
1635 ZERO_STRUCTP(status);
1636 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
1638 key.dsize = strlen(keystr);
1639 data = tdb_fetch(pdb->tdb, key);
1641 if (data.dsize == sizeof(*status)) {
1642 memcpy(status, data.dptr, sizeof(*status));
1644 SAFE_FREE(data.dptr);
1648 * Now, fetch the print queue information. We first count the number
1649 * of entries, and then only retrieve the queue if necessary.
1654 tdb_traverse(pdb->tdb, traverse_count_fn_queue, (void *)&tsc);
1656 if (tsc.count == 0) {
1657 release_print_db(pdb);
1661 /* Allocate the queue size. */
1662 if ((tstruct.queue = (print_queue_struct *)
1663 malloc(sizeof(print_queue_struct)*tsc.count)) == NULL) {
1664 release_print_db(pdb);
1669 * Fill in the queue.
1670 * We need maxcount as the queue size may have changed between
1671 * the two calls to tdb_traverse.
1674 tstruct.maxcount = tsc.count;
1675 tstruct.snum = snum;
1677 tdb_traverse(pdb->tdb, traverse_fn_queue, (void *)&tstruct);
1678 release_print_db(pdb);
1680 /* Sort the queue by submission time otherwise they are displayed
1683 qsort(tstruct.queue, tstruct.qcount, sizeof(print_queue_struct),
1684 QSORT_CAST(printjob_comp));
1686 *queue = tstruct.queue;
1687 return tstruct.qcount;
1690 /****************************************************************************
1691 Turn a queue name into a snum.
1692 ****************************************************************************/
1694 int print_queue_snum(const char *qname)
1696 int snum = lp_servicenumber(qname);
1697 if (snum == -1 || !lp_print_ok(snum))
1702 /****************************************************************************
1704 ****************************************************************************/
1706 BOOL print_queue_pause(struct current_user *user, int snum, WERROR *errcode)
1710 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
1711 *errcode = WERR_ACCESS_DENIED;
1715 ret = (*(current_printif->queue_pause))(snum);
1718 *errcode = WERR_INVALID_PARAM;
1722 /* force update the database */
1723 print_cache_flush(snum);
1725 /* Send a printer notify message */
1727 notify_printer_status(snum, PRINTER_STATUS_PAUSED);
1732 /****************************************************************************
1734 ****************************************************************************/
1736 BOOL print_queue_resume(struct current_user *user, int snum, WERROR *errcode)
1740 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
1741 *errcode = WERR_ACCESS_DENIED;
1745 ret = (*(current_printif->queue_resume))(snum);
1748 *errcode = WERR_INVALID_PARAM;
1752 /* make sure the database is up to date */
1753 if (print_cache_expired(snum))
1754 print_queue_update(snum);
1756 /* Send a printer notify message */
1758 notify_printer_status(snum, PRINTER_STATUS_OK);
1763 /****************************************************************************
1764 Purge a queue - implemented by deleting all jobs that we can delete.
1765 ****************************************************************************/
1767 BOOL print_queue_purge(struct current_user *user, int snum, WERROR *errcode)
1769 print_queue_struct *queue;
1770 print_status_struct status;
1774 /* Force and update so the count is accurate (i.e. not a cached count) */
1775 print_queue_update(snum);
1777 can_job_admin = print_access_check(user, snum, JOB_ACCESS_ADMINISTER);
1778 njobs = print_queue_status(snum, &queue, &status);
1780 for (i=0;i<njobs;i++) {
1781 BOOL owner = is_owner(user, snum, queue[i].job);
1783 if (owner || can_job_admin) {
1784 print_job_delete1(snum, queue[i].job);