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 /* Ensure the list terminates... JRA. */
160 SMB_ASSERT(p->next != print_db_head);
162 if (p->tdb && strequal(p->printer_name, printername)) {
163 DLIST_PROMOTE(print_db_head, p);
172 if (num_open >= MAX_PRINT_DBS_OPEN) {
173 /* Try and recycle the last entry. */
174 DLIST_PROMOTE(print_db_head, last_entry);
176 for (p = print_db_head; p; p = p->next) {
180 if (tdb_close(print_db_head->tdb)) {
181 DEBUG(0,("get_print_db: Failed to close tdb for printer %s\n",
182 print_db_head->printer_name ));
188 memset(p->printer_name, '\0', sizeof(p->printer_name));
192 DLIST_PROMOTE(print_db_head, p);
199 p = (struct tdb_print_db *)malloc(sizeof(struct tdb_print_db));
201 DEBUG(0,("get_print_db: malloc fail !\n"));
205 DLIST_ADD(print_db_head, p);
208 pstrcpy(printdb_path, lock_path("printing/"));
209 pstrcat(printdb_path, printername);
210 pstrcat(printdb_path, ".tdb");
213 p->tdb = tdb_open_log(printdb_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
217 DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
219 DLIST_REMOVE(print_db_head, p);
223 fstrcpy(p->printer_name, printername);
228 static void release_print_db( struct tdb_print_db *pdb)
231 SMB_ASSERT(pdb->ref_count >= 0);
234 /****************************************************************************
235 Initialise the printing backend. Called once at startup.
236 Does not survive a fork
237 ****************************************************************************/
239 BOOL print_backend_init(void)
241 char *sversion = "INFO/version";
242 pstring printing_path;
243 int services = lp_numservices();
246 if (local_pid == sys_getpid())
249 unlink(lock_path("printing.tdb"));
250 pstrcpy(printing_path,lock_path("printing"));
251 mkdir(printing_path,0755);
253 local_pid = sys_getpid();
255 /* handle a Samba upgrade */
257 for (snum = 0; snum < services; snum++) {
258 struct tdb_print_db *pdb;
259 if (!lp_print_ok(snum))
262 pdb = get_print_db_byname(lp_const_servicename(snum));
265 if (tdb_lock_bystring(pdb->tdb, sversion, 0) == -1) {
266 DEBUG(0,("print_backend_init: Failed to open printer %s database\n", lp_const_servicename(snum) ));
269 if (tdb_fetch_int32(pdb->tdb, sversion) != PRINT_DATABASE_VERSION) {
270 tdb_traverse(pdb->tdb, tdb_traverse_delete_fn, NULL);
271 tdb_store_int32(pdb->tdb, sversion, PRINT_DATABASE_VERSION);
273 tdb_unlock_bystring(pdb->tdb, sversion);
276 /* select the appropriate printing interface... */
278 if (strcmp(lp_printcapname(), "cups") == 0)
279 current_printif = &cups_printif;
280 #endif /* HAVE_CUPS */
282 /* do NT print initialization... */
283 return nt_printing_init();
286 /****************************************************************************
287 Shut down printing backend. Called once at shutdown to close the tdb.
288 ****************************************************************************/
290 void printing_end(void)
292 struct tdb_print_db *p;
294 for (p = print_db_head; p; ) {
295 struct tdb_print_db *next_p = p->next;
298 DLIST_REMOVE(print_db_head, p);
304 /****************************************************************************
305 Useful function to generate a tdb key.
306 ****************************************************************************/
308 static TDB_DATA print_key(uint32 jobid)
314 ret.dptr = (void *)&j;
315 ret.dsize = sizeof(j);
319 /***********************************************************************
320 unpack a pjob from a tdb buffer
321 ***********************************************************************/
323 int unpack_pjob( char* buf, int buflen, struct printjob *pjob )
331 len += tdb_unpack(buf+len, buflen-len, "dddddddddffff",
349 if ( (used = unpack_devicemode(&pjob->nt_devmode, buf+len, buflen-len)) == -1 )
358 /****************************************************************************
359 Useful function to find a print job in the database.
360 ****************************************************************************/
362 static struct printjob *print_job_find(int snum, uint32 jobid)
364 static struct printjob pjob;
366 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
372 ret = tdb_fetch(pdb->tdb, print_key(jobid));
373 release_print_db(pdb);
378 if ( pjob.nt_devmode )
379 free_nt_devicemode( &pjob.nt_devmode );
383 if ( unpack_pjob( ret.dptr, ret.dsize, &pjob ) == -1 )
390 /* Convert a unix jobid to a smb jobid */
392 static uint32 sysjob_to_jobid_value;
394 static int unixjob_traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key,
395 TDB_DATA data, void *state)
397 struct printjob *pjob;
398 int *sysjob = (int *)state;
400 if (!data.dptr || data.dsize == 0)
403 pjob = (struct printjob *)data.dptr;
404 if (key.dsize != sizeof(uint32))
407 if (*sysjob == pjob->sysjob) {
408 uint32 *jobid = (uint32 *)key.dptr;
410 sysjob_to_jobid_value = *jobid;
417 /****************************************************************************
418 This is a *horribly expensive call as we have to iterate through all the
419 current printer tdb's. Don't do this often ! JRA.
420 ****************************************************************************/
422 uint32 sysjob_to_jobid(int unix_jobid)
424 int services = lp_numservices();
427 sysjob_to_jobid_value = (uint32)-1;
429 for (snum = 0; snum < services; snum++) {
430 struct tdb_print_db *pdb;
431 if (!lp_print_ok(snum))
433 pdb = get_print_db_byname(lp_const_servicename(snum));
435 tdb_traverse(pdb->tdb, unixjob_traverse_fn, &unix_jobid);
436 release_print_db(pdb);
437 if (sysjob_to_jobid_value != (uint32)-1)
438 return sysjob_to_jobid_value;
443 /****************************************************************************
444 Send notifications based on what has changed after a pjob_store.
445 ****************************************************************************/
449 uint32 spoolss_status;
450 } lpq_to_spoolss_status_map[] = {
451 { LPQ_QUEUED, JOB_STATUS_QUEUED },
452 { LPQ_PAUSED, JOB_STATUS_PAUSED },
453 { LPQ_SPOOLING, JOB_STATUS_SPOOLING },
454 { LPQ_PRINTING, JOB_STATUS_PRINTING },
455 { LPQ_DELETING, JOB_STATUS_DELETING },
456 { LPQ_OFFLINE, JOB_STATUS_OFFLINE },
457 { LPQ_PAPEROUT, JOB_STATUS_PAPEROUT },
458 { LPQ_PRINTED, JOB_STATUS_PRINTED },
459 { LPQ_DELETED, JOB_STATUS_DELETED },
460 { LPQ_BLOCKED, JOB_STATUS_BLOCKED },
461 { LPQ_USER_INTERVENTION, JOB_STATUS_USER_INTERVENTION },
465 /* Convert a lpq status value stored in printing.tdb into the
466 appropriate win32 API constant. */
468 static uint32 map_to_spoolss_status(uint32 lpq_status)
472 while (lpq_to_spoolss_status_map[i].lpq_status != -1) {
473 if (lpq_to_spoolss_status_map[i].lpq_status == lpq_status)
474 return lpq_to_spoolss_status_map[i].spoolss_status;
481 static void pjob_store_notify(int snum, uint32 jobid, struct printjob *old_data,
482 struct printjob *new_data)
484 BOOL new_job = False;
489 /* Notify the job name first */
491 if (new_job || !strequal(old_data->jobname, new_data->jobname))
492 notify_job_name(snum, jobid, new_data->jobname);
494 /* Job attributes that can't be changed. We only send
495 notification for these on a new job. */
498 notify_job_submitted(snum, jobid, new_data->starttime);
499 notify_job_username(snum, jobid, new_data->user);
502 /* Job attributes of a new job or attributes that can be
505 if (new_job || old_data->status != new_data->status)
506 notify_job_status(snum, jobid, map_to_spoolss_status(new_data->status));
508 if (new_job || old_data->size != new_data->size)
509 notify_job_total_bytes(snum, jobid, new_data->size);
511 if (new_job || old_data->page_count != new_data->page_count)
512 notify_job_total_pages(snum, jobid, new_data->page_count);
515 /****************************************************************************
516 Store a job structure back to the database.
517 ****************************************************************************/
519 static BOOL pjob_store(int snum, uint32 jobid, struct printjob *pjob)
521 TDB_DATA old_data, new_data;
523 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
525 int len, newlen, buflen;
533 old_data = tdb_fetch(pdb->tdb, print_key(jobid));
535 /* Doh! Now we have to pack/unpack data since the NT_DEVICEMODE was added */
542 len += tdb_pack(buf+len, buflen-len, "dddddddddffff",
557 len += pack_devicemode(pjob->nt_devmode, buf+len, buflen-len);
563 tb = (char *)Realloc(buf, len);
565 DEBUG(0,("pjob_store: failed to enlarge buffer!\n"));
573 while ( buflen != len );
579 new_data.dsize = len;
580 ret = (tdb_store(pdb->tdb, print_key(jobid), new_data, TDB_REPLACE) == 0);
582 release_print_db(pdb);
584 /* Send notify updates for what has changed */
586 if ( ret && (old_data.dsize == 0 || old_data.dsize == sizeof(*pjob)) )
587 pjob_store_notify( snum, jobid, (struct printjob *)old_data.dptr, pjob );
590 SAFE_FREE( old_data.dptr );
596 /****************************************************************************
597 Remove a job structure from the database.
598 ****************************************************************************/
600 static void pjob_delete(int snum, uint32 jobid)
602 struct printjob *pjob = print_job_find(snum, jobid);
603 uint32 job_status = 0;
604 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
610 DEBUG(5, ("pjob_delete(): we were asked to delete nonexistent job %u\n",
611 (unsigned int)jobid));
612 release_print_db(pdb);
616 /* Send a notification that a job has been deleted */
618 job_status = map_to_spoolss_status(pjob->status);
620 /* We must cycle through JOB_STATUS_DELETING and
621 JOB_STATUS_DELETED for the port monitor to delete the job
624 job_status |= JOB_STATUS_DELETING;
625 notify_job_status(snum, jobid, job_status);
627 job_status |= JOB_STATUS_DELETED;
628 notify_job_status(snum, jobid, job_status);
630 /* Remove from printing.tdb */
632 tdb_delete(pdb->tdb, print_key(jobid));
633 release_print_db(pdb);
634 rap_jobid_delete(snum, jobid);
637 /****************************************************************************
638 Parse a file name from the system spooler to generate a jobid.
639 ****************************************************************************/
641 static uint32 print_parse_jobid(char *fname)
645 if (strncmp(fname,PRINT_SPOOL_PREFIX,strlen(PRINT_SPOOL_PREFIX)) != 0)
647 fname += strlen(PRINT_SPOOL_PREFIX);
653 return (uint32)jobid;
656 /****************************************************************************
657 List a unix job in the print database.
658 ****************************************************************************/
660 static void print_unix_job(int snum, print_queue_struct *q)
662 uint32 jobid = q->job + UNIX_JOB_START;
663 struct printjob pj, *old_pj;
665 /* Preserve the timestamp on an existing unix print job */
667 old_pj = print_job_find(snum, jobid);
674 pj.starttime = old_pj ? old_pj->starttime : q->time;
675 pj.status = q->status;
679 fstrcpy(pj.filename, "");
680 fstrcpy(pj.jobname, q->fs_file);
681 fstrcpy(pj.user, q->fs_user);
682 fstrcpy(pj.queuename, lp_const_servicename(snum));
684 pjob_store(snum, jobid, &pj);
688 struct traverse_struct {
689 print_queue_struct *queue;
690 int qcount, snum, maxcount, total_jobs;
693 /****************************************************************************
694 Utility fn to delete any jobs that are no longer active.
695 ****************************************************************************/
697 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
699 struct traverse_struct *ts = (struct traverse_struct *)state;
700 struct printjob pjob;
704 if ( key.dsize != sizeof(jobid) )
707 memcpy(&jobid, key.dptr, sizeof(jobid));
708 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
710 free_nt_devicemode( &pjob.nt_devmode );
713 if (ts->snum != lp_servicenumber(pjob.queuename)) {
714 /* this isn't for the queue we are looking at - this cannot happen with the split tdb's. JRA */
719 /* remove a unix job if it isn't in the system queue any more */
721 for (i=0;i<ts->qcount;i++) {
722 uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
723 if (jobid == u_jobid)
727 pjob_delete(ts->snum, jobid);
733 /* maybe it hasn't been spooled yet */
735 /* if a job is not spooled and the process doesn't
736 exist then kill it. This cleans up after smbd
738 if (!process_exists(pjob.pid))
739 pjob_delete(ts->snum, jobid);
745 for (i=0;i<ts->qcount;i++) {
746 uint32 curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
747 if (jobid == curr_jobid)
751 /* The job isn't in the system queue - we have to assume it has
752 completed, so delete the database entry. */
754 if (i == ts->qcount) {
755 time_t cur_t = time(NULL);
757 /* A race can occur between the time a job is spooled and
758 when it appears in the lpq output. This happens when
759 the job is added to printing.tdb when another smbd
760 running print_queue_update() has completed a lpq and
761 is currently traversing the printing tdb and deleting jobs.
762 A workaround is to not delete the job if it has been
763 submitted less than lp_lpqcachetime() seconds ago. */
765 if ((cur_t - pjob.starttime) > lp_lpqcachetime())
766 pjob_delete(ts->snum, jobid);
776 /****************************************************************************
777 Check if the print queue has been updated recently enough.
778 ****************************************************************************/
780 static void print_cache_flush(int snum)
783 const char *printername = lp_const_servicename(snum);
784 struct tdb_print_db *pdb = get_print_db_byname(printername);
788 slprintf(key, sizeof(key)-1, "CACHE/%s", printername);
789 tdb_store_int32(pdb->tdb, key, -1);
790 release_print_db(pdb);
793 /****************************************************************************
794 Check if someone already thinks they are doing the update.
795 ****************************************************************************/
797 static pid_t get_updating_pid(fstring printer_name)
802 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
806 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
808 key.dsize = strlen(keystr);
810 data = tdb_fetch(pdb->tdb, key);
811 release_print_db(pdb);
812 if (!data.dptr || data.dsize != sizeof(pid_t))
815 memcpy(&updating_pid, data.dptr, sizeof(pid_t));
816 SAFE_FREE(data.dptr);
818 if (process_exists(updating_pid))
824 /****************************************************************************
825 Set the fact that we're doing the update, or have finished doing the update
827 ****************************************************************************/
829 static void set_updating_pid(const fstring printer_name, BOOL delete)
834 pid_t updating_pid = sys_getpid();
835 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
840 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
842 key.dsize = strlen(keystr);
845 tdb_delete(pdb->tdb, key);
846 release_print_db(pdb);
850 data.dptr = (void *)&updating_pid;
851 data.dsize = sizeof(pid_t);
853 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
854 release_print_db(pdb);
857 /****************************************************************************
858 Update the internal database from the system print queue for a queue.
859 ****************************************************************************/
861 static void print_queue_update(int snum)
864 print_queue_struct *queue = NULL;
865 print_status_struct status;
866 print_status_struct old_status;
867 struct printjob *pjob;
868 struct traverse_struct tstruct;
869 fstring keystr, printer_name, cachestr;
871 struct tdb_print_db *pdb;
873 fstrcpy(printer_name, lp_const_servicename(snum));
874 pdb = get_print_db_byname(printer_name);
879 * Check to see if someone else is doing this update.
880 * This is essentially a mutex on the update.
883 if (get_updating_pid(printer_name) != -1) {
884 release_print_db(pdb);
888 /* Lock the queue for the database update */
890 slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", printer_name);
891 /* Only wait 10 seconds for this. */
892 if (tdb_lock_bystring(pdb->tdb, keystr, 10) == -1) {
893 DEBUG(0,("print_queue_update: Failed to lock printer %s database\n", printer_name));
894 release_print_db(pdb);
899 * Ensure that no one else got in here.
900 * If the updating pid is still -1 then we are
904 if (get_updating_pid(printer_name) != -1) {
906 * Someone else is doing the update, exit.
908 tdb_unlock_bystring(pdb->tdb, keystr);
909 release_print_db(pdb);
914 * We're going to do the update ourselves.
917 /* Tell others we're doing the update. */
918 set_updating_pid(printer_name, False);
921 * Allow others to enter and notice we're doing
925 tdb_unlock_bystring(pdb->tdb, keystr);
928 * Update the cache time FIRST ! Stops others even
929 * attempting to get the lock and doing this
930 * if the lpq takes a long time.
933 slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", printer_name);
934 tdb_store_int32(pdb->tdb, cachestr, (int)time(NULL));
936 /* get the current queue using the appropriate interface */
939 qcount = (*(current_printif->queue_get))(snum, &queue, &status);
941 DEBUG(3, ("%d job%s in queue for %s\n", qcount, (qcount != 1) ?
942 "s" : "", printer_name));
945 any job in the internal database that is marked as spooled
946 and doesn't exist in the system queue is considered finished
947 and removed from the database
949 any job in the system database but not in the internal database
950 is added as a unix job
952 fill in any system job numbers as we go
954 for (i=0; i<qcount; i++) {
955 uint32 jobid = print_parse_jobid(queue[i].fs_file);
957 if (jobid == (uint32)-1) {
958 /* assume its a unix print job */
959 print_unix_job(snum, &queue[i]);
963 /* we have an active SMB print job - update its status */
964 pjob = print_job_find(snum, jobid);
966 /* err, somethings wrong. Probably smbd was restarted
967 with jobs in the queue. All we can do is treat them
968 like unix jobs. Pity. */
969 print_unix_job(snum, &queue[i]);
973 pjob->sysjob = queue[i].job;
974 pjob->status = queue[i].status;
976 pjob_store(snum, jobid, pjob);
979 /* now delete any queued entries that don't appear in the
981 tstruct.queue = queue;
982 tstruct.qcount = qcount;
984 tstruct.total_jobs = 0;
986 tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
988 SAFE_FREE(tstruct.queue);
990 tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
992 if( qcount != get_queue_status(snum, &old_status))
993 DEBUG(10,("print_queue_update: queue status change %d jobs -> %d jobs for printer %s\n",
994 old_status.qcount, qcount, printer_name ));
996 /* store the new queue status structure */
997 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printer_name);
999 key.dsize = strlen(keystr);
1001 status.qcount = qcount;
1002 data.dptr = (void *)&status;
1003 data.dsize = sizeof(status);
1004 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
1007 * Update the cache time again. We want to do this call
1008 * as little as possible...
1011 slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", printer_name);
1012 tdb_store_int32(pdb->tdb, keystr, (int32)time(NULL));
1014 /* Delete our pid from the db. */
1015 set_updating_pid(printer_name, True);
1016 release_print_db(pdb);
1019 /****************************************************************************
1020 Check if a jobid is valid. It is valid if it exists in the database.
1021 ****************************************************************************/
1023 BOOL print_job_exists(int snum, uint32 jobid)
1025 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
1030 ret = tdb_exists(pdb->tdb, print_key(jobid));
1031 release_print_db(pdb);
1035 /****************************************************************************
1036 Give the fd used for a jobid.
1037 ****************************************************************************/
1039 int print_job_fd(int snum, uint32 jobid)
1041 struct printjob *pjob = print_job_find(snum, jobid);
1044 /* don't allow another process to get this info - it is meaningless */
1045 if (pjob->pid != local_pid)
1050 /****************************************************************************
1051 Give the filename used for a jobid.
1052 Only valid for the process doing the spooling and when the job
1053 has not been spooled.
1054 ****************************************************************************/
1056 char *print_job_fname(int snum, uint32 jobid)
1058 struct printjob *pjob = print_job_find(snum, jobid);
1059 if (!pjob || pjob->spooled || pjob->pid != local_pid)
1061 return pjob->filename;
1065 /****************************************************************************
1066 Give the filename used for a jobid.
1067 Only valid for the process doing the spooling and when the job
1068 has not been spooled.
1069 ****************************************************************************/
1071 NT_DEVICEMODE *print_job_devmode(int snum, uint32 jobid)
1073 struct printjob *pjob = print_job_find(snum, jobid);
1078 return pjob->nt_devmode;
1081 /****************************************************************************
1082 Set the place in the queue for a job.
1083 ****************************************************************************/
1085 BOOL print_job_set_place(int snum, uint32 jobid, int place)
1087 DEBUG(2,("print_job_set_place not implemented yet\n"));
1091 /****************************************************************************
1092 Set the name of a job. Only possible for owner.
1093 ****************************************************************************/
1095 BOOL print_job_set_name(int snum, uint32 jobid, char *name)
1097 struct printjob *pjob = print_job_find(snum, jobid);
1098 if (!pjob || pjob->pid != local_pid)
1101 fstrcpy(pjob->jobname, name);
1102 return pjob_store(snum, jobid, pjob);
1105 /****************************************************************************
1106 Delete a print job - don't update queue.
1107 ****************************************************************************/
1109 static BOOL print_job_delete1(int snum, uint32 jobid)
1111 struct printjob *pjob = print_job_find(snum, jobid);
1118 * If already deleting just return.
1121 if (pjob->status == LPQ_DELETING)
1124 /* Hrm - we need to be able to cope with deleting a job before it
1125 has reached the spooler. */
1127 if (pjob->sysjob == -1) {
1128 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid));
1131 /* Set the tdb entry to be deleting. */
1133 pjob->status = LPQ_DELETING;
1134 pjob_store(snum, jobid, pjob);
1136 if (pjob->spooled && pjob->sysjob != -1)
1137 result = (*(current_printif->job_delete))(snum, pjob);
1139 /* Delete the tdb entry if the delete suceeded or the job hasn't
1143 pjob_delete(snum, jobid);
1145 return (result == 0);
1148 /****************************************************************************
1149 Return true if the current user owns the print job.
1150 ****************************************************************************/
1152 static BOOL is_owner(struct current_user *user, int snum, uint32 jobid)
1154 struct printjob *pjob = print_job_find(snum, jobid);
1160 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1161 return strequal(pjob->user, vuser->user.smb_name);
1163 return strequal(pjob->user, uidtoname(user->uid));
1167 /****************************************************************************
1169 ****************************************************************************/
1171 BOOL print_job_delete(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1173 BOOL owner, deleted;
1178 owner = is_owner(user, snum, jobid);
1180 /* Check access against security descriptor or whether the user
1184 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1185 DEBUG(3, ("delete denied by security descriptor\n"));
1186 *errcode = WERR_ACCESS_DENIED;
1191 * get the spooled filename of the print job
1192 * if this works, then the file has not been spooled
1193 * to the underlying print system. Just delete the
1194 * spool file & return.
1197 if ( (fname = print_job_fname( snum, jobid )) != NULL )
1199 /* remove the spool file */
1200 DEBUG(10,("print_job_delete: Removing spool file [%s]\n", fname ));
1201 if ( unlink( fname ) == -1 ) {
1202 *errcode = map_werror_from_unix(errno);
1209 if (!print_job_delete1(snum, jobid)) {
1210 *errcode = WERR_ACCESS_DENIED;
1214 /* force update the database and say the delete failed if the
1217 print_queue_update(snum);
1219 deleted = !print_job_exists(snum, jobid);
1221 *errcode = WERR_ACCESS_DENIED;
1226 /****************************************************************************
1228 ****************************************************************************/
1230 BOOL print_job_pause(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1232 struct printjob *pjob = print_job_find(snum, jobid);
1238 if (!pjob->spooled || pjob->sysjob == -1)
1241 if (!is_owner(user, snum, jobid) &&
1242 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1243 DEBUG(3, ("pause denied by security descriptor\n"));
1244 *errcode = WERR_ACCESS_DENIED;
1248 /* need to pause the spooled entry */
1249 ret = (*(current_printif->job_pause))(snum, pjob);
1252 *errcode = WERR_INVALID_PARAM;
1256 /* force update the database */
1257 print_cache_flush(snum);
1259 /* Send a printer notify message */
1261 notify_job_status(snum, jobid, JOB_STATUS_PAUSED);
1263 /* how do we tell if this succeeded? */
1268 /****************************************************************************
1270 ****************************************************************************/
1272 BOOL print_job_resume(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1274 struct printjob *pjob = print_job_find(snum, jobid);
1280 if (!pjob->spooled || pjob->sysjob == -1)
1283 if (!is_owner(user, snum, jobid) &&
1284 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1285 DEBUG(3, ("resume denied by security descriptor\n"));
1286 *errcode = WERR_ACCESS_DENIED;
1290 ret = (*(current_printif->job_resume))(snum, pjob);
1293 *errcode = WERR_INVALID_PARAM;
1297 /* force update the database */
1298 print_cache_flush(snum);
1300 /* Send a printer notify message */
1302 notify_job_status(snum, jobid, JOB_STATUS_QUEUED);
1307 /****************************************************************************
1308 Write to a print file.
1309 ****************************************************************************/
1311 int print_job_write(int snum, uint32 jobid, const char *buf, int size)
1314 struct printjob *pjob = print_job_find(snum, jobid);
1318 /* don't allow another process to get this info - it is meaningless */
1319 if (pjob->pid != local_pid)
1322 return_code = write(pjob->fd, buf, size);
1323 if (return_code>0) {
1325 pjob_store(snum, jobid, pjob);
1330 /****************************************************************************
1331 Check if the print queue has been updated recently enough.
1332 ****************************************************************************/
1334 static BOOL print_cache_expired(int snum)
1337 time_t last_qscan_time, time_now = time(NULL);
1338 const char *printername = lp_const_servicename(snum);
1339 struct tdb_print_db *pdb = get_print_db_byname(printername);
1344 slprintf(key, sizeof(key), "CACHE/%s", printername);
1345 last_qscan_time = (time_t)tdb_fetch_int32(pdb->tdb, key);
1348 * Invalidate the queue for 3 reasons.
1349 * (1). last queue scan time == -1.
1350 * (2). Current time - last queue scan time > allowed cache time.
1351 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1352 * This last test picks up machines for which the clock has been moved
1353 * forward, an lpq scan done and then the clock moved back. Otherwise
1354 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1357 if (last_qscan_time == ((time_t)-1) || (time_now - last_qscan_time) >= lp_lpqcachetime() ||
1358 last_qscan_time > (time_now + MAX_CACHE_VALID_TIME)) {
1359 DEBUG(3, ("print cache expired for queue %s \
1360 (last_qscan_time = %d, time now = %d, qcachetime = %d)\n", printername,
1361 (int)last_qscan_time, (int)time_now, (int)lp_lpqcachetime() ));
1362 release_print_db(pdb);
1365 release_print_db(pdb);
1369 /****************************************************************************
1370 Get the queue status - do not update if db is out of date.
1371 ****************************************************************************/
1373 static int get_queue_status(int snum, print_status_struct *status)
1377 const char *printername = lp_const_servicename(snum);
1378 struct tdb_print_db *pdb = get_print_db_byname(printername);
1382 ZERO_STRUCTP(status);
1383 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
1385 key.dsize = strlen(keystr);
1386 data = tdb_fetch(pdb->tdb, key);
1387 release_print_db(pdb);
1389 if (data.dsize == sizeof(print_status_struct))
1390 memcpy(status, data.dptr, sizeof(print_status_struct));
1391 SAFE_FREE(data.dptr);
1393 return status->qcount;
1396 /****************************************************************************
1397 Determine the number of jobs in a queue.
1398 ****************************************************************************/
1400 int print_queue_length(int snum, print_status_struct *pstatus)
1402 print_status_struct status;
1405 /* make sure the database is up to date */
1406 if (print_cache_expired(snum))
1407 print_queue_update(snum);
1409 /* also fetch the queue status */
1410 memset(&status, 0, sizeof(status));
1411 len = get_queue_status(snum, &status);
1417 /***************************************************************************
1418 Start spooling a job - return the jobid.
1419 ***************************************************************************/
1421 uint32 print_job_start(struct current_user *user, int snum, char *jobname, NT_DEVICEMODE *nt_devmode )
1425 struct printjob pjob;
1429 const char *printername = lp_const_servicename(snum);
1430 struct tdb_print_db *pdb = get_print_db_byname(printername);
1431 BOOL pdb_locked = False;
1438 if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
1439 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
1440 release_print_db(pdb);
1444 if (!print_time_access_check(snum)) {
1445 DEBUG(3, ("print_job_start: job start denied by time check\n"));
1446 release_print_db(pdb);
1450 path = lp_pathname(snum);
1452 /* see if we have sufficient disk space */
1453 if (lp_minprintspace(snum)) {
1454 SMB_BIG_UINT dspace, dsize;
1455 if (sys_fsusage(path, &dspace, &dsize) == 0 &&
1456 dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
1457 DEBUG(3, ("print_job_start: disk space check failed.\n"));
1458 release_print_db(pdb);
1464 /* for autoloaded printers, check that the printcap entry still exists */
1465 if (lp_autoloaded(snum) && !pcap_printername_ok(lp_const_servicename(snum), NULL)) {
1466 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum) ));
1467 release_print_db(pdb);
1472 /* Insure the maximum queue size is not violated */
1473 if ((njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
1474 DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per queue (%d).\n",
1475 njobs, lp_maxprintjobs(snum) ));
1476 release_print_db(pdb);
1481 /* Lock the database - only wait 20 seconds. */
1482 if (tdb_lock_bystring(pdb->tdb, "INFO/nextjob", 20) == -1) {
1483 DEBUG(0,("print_job_start: failed to lock printing database %s\n", printername ));
1484 release_print_db(pdb);
1490 next_jobid = tdb_fetch_int32(pdb->tdb, "INFO/nextjob");
1491 if (next_jobid == -1)
1494 for (jobid = NEXT_JOBID(next_jobid); jobid != next_jobid; jobid = NEXT_JOBID(jobid)) {
1495 if (!print_job_exists(snum, jobid))
1499 if (jobid == next_jobid) {
1500 DEBUG(3, ("print_job_start: jobid (%d)==next_jobid(%d).\n",
1501 jobid, next_jobid ));
1506 /* Store a dummy placeholder. This must be quick as we have the lock. */
1511 if (tdb_store(pdb->tdb, print_key(jobid), dum, TDB_INSERT) == -1) {
1512 DEBUG(3, ("print_job_start: jobid (%d) failed to store placeholder.\n",
1519 if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
1520 DEBUG(3, ("print_job_start: failed to store INFO/nextjob.\n"));
1525 /* We've finished with the INFO/nextjob lock. */
1526 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1529 /* create the database entry */
1533 pjob.pid = local_pid;
1536 pjob.starttime = time(NULL);
1537 pjob.status = LPQ_SPOOLING;
1539 pjob.spooled = False;
1541 pjob.nt_devmode = nt_devmode;
1543 fstrcpy(pjob.jobname, jobname);
1545 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1546 fstrcpy(pjob.user, vuser->user.smb_name);
1548 fstrcpy(pjob.user, uidtoname(user->uid));
1551 fstrcpy(pjob.queuename, lp_const_servicename(snum));
1553 /* we have a job entry - now create the spool file */
1554 slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.8u.XXXXXX",
1555 path, PRINT_SPOOL_PREFIX, (unsigned int)jobid);
1556 pjob.fd = smb_mkstemp(pjob.filename);
1558 if (pjob.fd == -1) {
1559 if (errno == EACCES) {
1560 /* Common setup error, force a report. */
1561 DEBUG(0, ("print_job_start: insufficient permissions \
1562 to open spool file %s.\n", pjob.filename));
1564 /* Normal case, report at level 3 and above. */
1565 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
1566 DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
1571 pjob_store(snum, jobid, &pjob);
1573 release_print_db(pdb);
1579 pjob_delete(snum, jobid);
1582 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1583 release_print_db(pdb);
1585 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
1589 /****************************************************************************
1590 Update the number of pages spooled to jobid
1591 ****************************************************************************/
1593 void print_job_endpage(int snum, uint32 jobid)
1595 struct printjob *pjob = print_job_find(snum, jobid);
1598 /* don't allow another process to get this info - it is meaningless */
1599 if (pjob->pid != local_pid)
1603 pjob_store(snum, jobid, pjob);
1606 /****************************************************************************
1607 Print a file - called on closing the file. This spools the job.
1608 If normal close is false then we're tearing down the jobs - treat as an
1610 ****************************************************************************/
1612 BOOL print_job_end(int snum, uint32 jobid, BOOL normal_close)
1614 struct printjob *pjob = print_job_find(snum, jobid);
1616 SMB_STRUCT_STAT sbuf;
1621 if (pjob->spooled || pjob->pid != local_pid)
1624 if (normal_close && (sys_fstat(pjob->fd, &sbuf) == 0)) {
1625 pjob->size = sbuf.st_size;
1631 * Not a normal close or we couldn't stat the job file,
1632 * so something has gone wrong. Cleanup.
1636 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
1640 /* Technically, this is not quite right. If the printer has a separator
1641 * page turned on, the NT spooler prints the separator page even if the
1642 * print job is 0 bytes. 010215 JRR */
1643 if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
1644 /* don't bother spooling empty files or something being deleted. */
1645 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
1646 pjob->filename, pjob->size ? "deleted" : "zero length" ));
1647 unlink(pjob->filename);
1648 pjob_delete(snum, jobid);
1652 ret = (*(current_printif->job_submit))(snum, pjob);
1657 /* The print job has been sucessfully handed over to the back-end */
1659 pjob->spooled = True;
1660 pjob->status = LPQ_QUEUED;
1661 pjob_store(snum, jobid, pjob);
1663 /* make sure the database is up to date */
1664 if (print_cache_expired(snum))
1665 print_queue_update(snum);
1671 /* The print job was not succesfully started. Cleanup */
1672 /* Still need to add proper error return propagation! 010122:JRR */
1673 unlink(pjob->filename);
1674 pjob_delete(snum, jobid);
1678 /****************************************************************************
1679 Utility fn to enumerate the print queue.
1680 ****************************************************************************/
1682 static int traverse_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
1684 struct traverse_struct *ts = (struct traverse_struct *)state;
1685 struct printjob pjob;
1691 if ( key.dsize != sizeof(jobid) )
1694 memcpy(&jobid, key.dptr, sizeof(jobid));
1696 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
1698 free_nt_devicemode( &pjob.nt_devmode );
1700 /* maybe it isn't for this queue */
1701 if (ts->snum != lp_servicenumber(pjob.queuename))
1704 if (ts->qcount >= ts->maxcount)
1709 ts->queue[i].job = jobid;
1710 ts->queue[i].size = pjob.size;
1711 ts->queue[i].page_count = pjob.page_count;
1712 ts->queue[i].status = pjob.status;
1713 ts->queue[i].priority = 1;
1714 ts->queue[i].time = pjob.starttime;
1715 fstrcpy(ts->queue[i].fs_user, pjob.user);
1716 fstrcpy(ts->queue[i].fs_file, pjob.jobname);
1723 struct traverse_count_struct {
1727 /****************************************************************************
1728 Utility fn to count the number of entries in the print queue.
1729 ****************************************************************************/
1731 static int traverse_count_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
1733 struct traverse_count_struct *ts = (struct traverse_count_struct *)state;
1734 struct printjob pjob;
1739 if ( key.dsize != sizeof(jobid) )
1742 memcpy(&jobid, key.dptr, sizeof(jobid));
1744 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
1747 free_nt_devicemode( &pjob.nt_devmode );
1749 /* maybe it isn't for this queue - this cannot happen with the tdb/printer code. JRA */
1750 if (ts->snum != lp_servicenumber(pjob.queuename))
1758 /****************************************************************************
1759 Sort print jobs by submittal time.
1760 ****************************************************************************/
1762 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
1773 /* Sort on job start time */
1775 if (j1->time == j2->time)
1777 return (j1->time > j2->time) ? 1 : -1;
1780 /****************************************************************************
1781 Get a printer queue listing.
1782 ****************************************************************************/
1784 int print_queue_status(int snum,
1785 print_queue_struct **queue,
1786 print_status_struct *status)
1788 struct traverse_struct tstruct;
1789 struct traverse_count_struct tsc;
1792 const char *printername = lp_const_servicename(snum);
1793 struct tdb_print_db *pdb = get_print_db_byname(printername);
1800 /* make sure the database is up to date */
1801 if (print_cache_expired(snum))
1802 print_queue_update(snum);
1805 * Fetch the queue status. We must do this first, as there may
1806 * be no jobs in the queue.
1808 ZERO_STRUCTP(status);
1809 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
1811 key.dsize = strlen(keystr);
1812 data = tdb_fetch(pdb->tdb, key);
1814 if (data.dsize == sizeof(*status)) {
1815 memcpy(status, data.dptr, sizeof(*status));
1817 SAFE_FREE(data.dptr);
1821 * Now, fetch the print queue information. We first count the number
1822 * of entries, and then only retrieve the queue if necessary.
1827 tdb_traverse(pdb->tdb, traverse_count_fn_queue, (void *)&tsc);
1829 if (tsc.count == 0) {
1830 release_print_db(pdb);
1834 /* Allocate the queue size. */
1835 if ((tstruct.queue = (print_queue_struct *)
1836 malloc(sizeof(print_queue_struct)*tsc.count)) == NULL) {
1837 release_print_db(pdb);
1842 * Fill in the queue.
1843 * We need maxcount as the queue size may have changed between
1844 * the two calls to tdb_traverse.
1847 tstruct.maxcount = tsc.count;
1848 tstruct.snum = snum;
1850 tdb_traverse(pdb->tdb, traverse_fn_queue, (void *)&tstruct);
1851 release_print_db(pdb);
1853 /* Sort the queue by submission time otherwise they are displayed
1856 qsort(tstruct.queue, tstruct.qcount, sizeof(print_queue_struct),
1857 QSORT_CAST(printjob_comp));
1859 *queue = tstruct.queue;
1860 return tstruct.qcount;
1863 /****************************************************************************
1864 Turn a queue name into a snum.
1865 ****************************************************************************/
1867 int print_queue_snum(const char *qname)
1869 int snum = lp_servicenumber(qname);
1870 if (snum == -1 || !lp_print_ok(snum))
1875 /****************************************************************************
1877 ****************************************************************************/
1879 BOOL print_queue_pause(struct current_user *user, int snum, WERROR *errcode)
1883 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
1884 *errcode = WERR_ACCESS_DENIED;
1888 ret = (*(current_printif->queue_pause))(snum);
1891 *errcode = WERR_INVALID_PARAM;
1895 /* force update the database */
1896 print_cache_flush(snum);
1898 /* Send a printer notify message */
1900 notify_printer_status(snum, PRINTER_STATUS_PAUSED);
1905 /****************************************************************************
1907 ****************************************************************************/
1909 BOOL print_queue_resume(struct current_user *user, int snum, WERROR *errcode)
1913 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
1914 *errcode = WERR_ACCESS_DENIED;
1918 ret = (*(current_printif->queue_resume))(snum);
1921 *errcode = WERR_INVALID_PARAM;
1925 /* make sure the database is up to date */
1926 if (print_cache_expired(snum))
1927 print_queue_update(snum);
1929 /* Send a printer notify message */
1931 notify_printer_status(snum, PRINTER_STATUS_OK);
1936 /****************************************************************************
1937 Purge a queue - implemented by deleting all jobs that we can delete.
1938 ****************************************************************************/
1940 BOOL print_queue_purge(struct current_user *user, int snum, WERROR *errcode)
1942 print_queue_struct *queue;
1943 print_status_struct status;
1947 /* Force and update so the count is accurate (i.e. not a cached count) */
1948 print_queue_update(snum);
1950 can_job_admin = print_access_check(user, snum, JOB_ACCESS_ADMINISTER);
1951 njobs = print_queue_status(snum, &queue, &status);
1953 for (i=0;i<njobs;i++) {
1954 BOOL owner = is_owner(user, snum, queue[i].job);
1956 if (owner || can_job_admin) {
1957 print_job_delete1(snum, queue[i].job);