2 Unix SMB/Netbios implementation.
4 printing backend routines
5 Copyright (C) Andrew Tridgell 1992-2000
6 Copyright (C) Jeremy Allison 2002
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 extern SIG_ATOMIC_T got_sig_term;
27 extern SIG_ATOMIC_T reload_after_sighup;
29 /* Current printer interface */
30 static BOOL remove_from_jobs_changed(int snum, uint32 jobid);
33 the printing backend revolves around a tdb database that stores the
34 SMB view of the print queue
36 The key for this database is a jobid - a internally generated number that
37 uniquely identifies a print job
39 reading the print queue involves two steps:
40 - possibly running lpq and updating the internal database from that
41 - reading entries from the database
43 jobids are assigned when a job starts spooling.
46 /***************************************************************************
47 Nightmare. LANMAN jobid's are 16 bit numbers..... We must map them to 32
48 bit RPC jobids.... JRA.
49 ***************************************************************************/
51 static TDB_CONTEXT *rap_tdb;
52 static uint16 next_rap_jobid;
54 uint16 pjobid_to_rap(int snum, uint32 jobid)
60 DEBUG(10,("pjobid_to_rap: called.\n"));
63 /* Create the in-memory tdb. */
64 rap_tdb = tdb_open_log(NULL, 0, TDB_INTERNAL, (O_RDWR|O_CREAT), 0644);
69 SIVAL(&jinfo,0,(int32)snum);
70 SIVAL(&jinfo,4,jobid);
72 key.dptr = (char *)&jinfo;
73 key.dsize = sizeof(jinfo);
74 data = tdb_fetch(rap_tdb, key);
75 if (data.dptr && data.dsize == sizeof(uint16)) {
76 rap_jobid = SVAL(data.dptr, 0);
78 DEBUG(10,("pjobid_to_rap: jobid %u maps to RAP jobid %u\n",
80 (unsigned int)rap_jobid));
84 /* Not found - create and store mapping. */
85 rap_jobid = ++next_rap_jobid;
87 rap_jobid = ++next_rap_jobid;
88 data.dptr = (char *)&rap_jobid;
89 data.dsize = sizeof(rap_jobid);
90 tdb_store(rap_tdb, key, data, TDB_REPLACE);
91 tdb_store(rap_tdb, data, key, TDB_REPLACE);
93 DEBUG(10,("pjobid_to_rap: created jobid %u maps to RAP jobid %u\n",
95 (unsigned int)rap_jobid));
99 BOOL rap_to_pjobid(uint16 rap_jobid, int *psnum, uint32 *pjobid)
103 DEBUG(10,("rap_to_pjobid called.\n"));
108 key.dptr = (char *)&rap_jobid;
109 key.dsize = sizeof(rap_jobid);
110 data = tdb_fetch(rap_tdb, key);
111 if (data.dptr && data.dsize == 8) {
112 *psnum = IVAL(data.dptr,0);
113 *pjobid = IVAL(data.dptr,4);
114 DEBUG(10,("rap_to_pjobid: jobid %u maps to RAP jobid %u\n",
115 (unsigned int)*pjobid,
116 (unsigned int)rap_jobid));
117 SAFE_FREE(data.dptr);
121 DEBUG(10,("rap_to_pjobid: Failed to lookup RAP jobid %u\n",
122 (unsigned int)rap_jobid));
123 SAFE_FREE(data.dptr);
127 static void rap_jobid_delete(int snum, uint32 jobid)
133 DEBUG(10,("rap_jobid_delete: called.\n"));
138 SIVAL(&jinfo,0,(int32)snum);
139 SIVAL(&jinfo,4,jobid);
141 key.dptr = (char *)&jinfo;
142 key.dsize = sizeof(jinfo);
143 data = tdb_fetch(rap_tdb, key);
144 if (!data.dptr || (data.dsize != sizeof(uint16))) {
145 DEBUG(10,("rap_jobid_delete: cannot find jobid %u\n",
146 (unsigned int)jobid ));
147 SAFE_FREE(data.dptr);
151 DEBUG(10,("rap_jobid_delete: deleting jobid %u\n",
152 (unsigned int)jobid ));
154 rap_jobid = SVAL(data.dptr, 0);
155 SAFE_FREE(data.dptr);
156 data.dptr = (char *)&rap_jobid;
157 data.dsize = sizeof(rap_jobid);
158 tdb_delete(rap_tdb, key);
159 tdb_delete(rap_tdb, data);
162 static int get_queue_status(int, print_status_struct *);
164 /****************************************************************************
165 Initialise the printing backend. Called once at startup before the fork().
166 ****************************************************************************/
168 BOOL print_backend_init(void)
170 const char *sversion = "INFO/version";
171 pstring printing_path;
172 int services = lp_numservices();
175 unlink(lock_path("printing.tdb"));
176 pstrcpy(printing_path,lock_path("printing"));
177 mkdir(printing_path,0755);
179 /* handle a Samba upgrade */
181 for (snum = 0; snum < services; snum++) {
182 struct tdb_print_db *pdb;
183 if (!lp_print_ok(snum))
186 pdb = get_print_db_byname(lp_const_servicename(snum));
189 if (tdb_lock_bystring(pdb->tdb, sversion, 0) == -1) {
190 DEBUG(0,("print_backend_init: Failed to open printer %s database\n", lp_const_servicename(snum) ));
191 release_print_db(pdb);
194 if (tdb_fetch_int32(pdb->tdb, sversion) != PRINT_DATABASE_VERSION) {
195 tdb_traverse(pdb->tdb, tdb_traverse_delete_fn, NULL);
196 tdb_store_int32(pdb->tdb, sversion, PRINT_DATABASE_VERSION);
198 tdb_unlock_bystring(pdb->tdb, sversion);
199 release_print_db(pdb);
202 close_all_print_db(); /* Don't leave any open. */
204 /* do NT print initialization... */
205 return nt_printing_init();
208 /****************************************************************************
209 Shut down printing backend. Called once at shutdown to close the tdb.
210 ****************************************************************************/
212 void printing_end(void)
214 close_all_print_db(); /* Don't leave any open. */
217 /****************************************************************************
218 Retrieve the set of printing functions for a given service. This allows
219 us to set the printer function table based on the value of the 'printing'
222 Use the generic interface as the default and only use cups interface only
223 when asked for (and only when supported)
224 ****************************************************************************/
226 static struct printif *get_printer_fns( int snum )
228 struct printif *printer_fns = &generic_printif;
231 if ( lp_printing(snum) == PRINT_CUPS ) {
232 printer_fns = &cups_printif;
234 #endif /* HAVE_CUPS */
239 /****************************************************************************
240 Useful function to generate a tdb key.
241 ****************************************************************************/
243 static TDB_DATA print_key(uint32 jobid)
249 ret.dptr = (void *)&j;
250 ret.dsize = sizeof(j);
254 /***********************************************************************
255 unpack a pjob from a tdb buffer
256 ***********************************************************************/
258 int unpack_pjob( char* buf, int buflen, struct printjob *pjob )
262 uint32 pjpid, pjsysjob, pjfd, pjstarttime, pjstatus;
263 uint32 pjsize, pjpage_count, pjspooled, pjsmbjob;
268 len += tdb_unpack(buf+len, buflen-len, "dddddddddffff",
286 if ( (used = unpack_devicemode(&pjob->nt_devmode, buf+len, buflen-len)) == -1 )
292 pjob->sysjob = pjsysjob;
294 pjob->starttime = pjstarttime;
295 pjob->status = pjstatus;
297 pjob->page_count = pjpage_count;
298 pjob->spooled = pjspooled;
299 pjob->smbjob = pjsmbjob;
305 /****************************************************************************
306 Useful function to find a print job in the database.
307 ****************************************************************************/
309 static struct printjob *print_job_find(int snum, uint32 jobid)
311 static struct printjob pjob;
313 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
319 ret = tdb_fetch(pdb->tdb, print_key(jobid));
320 release_print_db(pdb);
325 if ( pjob.nt_devmode )
326 free_nt_devicemode( &pjob.nt_devmode );
330 if ( unpack_pjob( ret.dptr, ret.dsize, &pjob ) == -1 ) {
339 /* Convert a unix jobid to a smb jobid */
341 static uint32 sysjob_to_jobid_value;
343 static int unixjob_traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key,
344 TDB_DATA data, void *state)
346 struct printjob *pjob;
347 int *sysjob = (int *)state;
349 if (!data.dptr || data.dsize == 0)
352 pjob = (struct printjob *)data.dptr;
353 if (key.dsize != sizeof(uint32))
356 if (*sysjob == pjob->sysjob) {
357 uint32 jobid = IVAL(key.dptr,0);
359 sysjob_to_jobid_value = jobid;
366 /****************************************************************************
367 This is a *horribly expensive call as we have to iterate through all the
368 current printer tdb's. Don't do this often ! JRA.
369 ****************************************************************************/
371 uint32 sysjob_to_jobid(int unix_jobid)
373 int services = lp_numservices();
376 sysjob_to_jobid_value = (uint32)-1;
378 for (snum = 0; snum < services; snum++) {
379 struct tdb_print_db *pdb;
380 if (!lp_print_ok(snum))
382 pdb = get_print_db_byname(lp_const_servicename(snum));
384 tdb_traverse(pdb->tdb, unixjob_traverse_fn, &unix_jobid);
385 release_print_db(pdb);
386 if (sysjob_to_jobid_value != (uint32)-1)
387 return sysjob_to_jobid_value;
392 /****************************************************************************
393 Send notifications based on what has changed after a pjob_store.
394 ****************************************************************************/
398 uint32 spoolss_status;
399 } lpq_to_spoolss_status_map[] = {
400 { LPQ_QUEUED, JOB_STATUS_QUEUED },
401 { LPQ_PAUSED, JOB_STATUS_PAUSED },
402 { LPQ_SPOOLING, JOB_STATUS_SPOOLING },
403 { LPQ_PRINTING, JOB_STATUS_PRINTING },
404 { LPQ_DELETING, JOB_STATUS_DELETING },
405 { LPQ_OFFLINE, JOB_STATUS_OFFLINE },
406 { LPQ_PAPEROUT, JOB_STATUS_PAPEROUT },
407 { LPQ_PRINTED, JOB_STATUS_PRINTED },
408 { LPQ_DELETED, JOB_STATUS_DELETED },
409 { LPQ_BLOCKED, JOB_STATUS_BLOCKED },
410 { LPQ_USER_INTERVENTION, JOB_STATUS_USER_INTERVENTION },
414 /* Convert a lpq status value stored in printing.tdb into the
415 appropriate win32 API constant. */
417 static uint32 map_to_spoolss_status(uint32 lpq_status)
421 while (lpq_to_spoolss_status_map[i].lpq_status != -1) {
422 if (lpq_to_spoolss_status_map[i].lpq_status == lpq_status)
423 return lpq_to_spoolss_status_map[i].spoolss_status;
430 static void pjob_store_notify(int snum, uint32 jobid, struct printjob *old_data,
431 struct printjob *new_data)
433 BOOL new_job = False;
438 /* Job attributes that can't be changed. We only send
439 notification for these on a new job. */
441 /* ACHTUNG! Due to a bug in Samba's spoolss parsing of the
442 NOTIFY_INFO_DATA buffer, we *have* to send the job submission
443 time first or else we'll end up with potential alignment
444 errors. I don't think the systemtime should be spooled as
445 a string, but this gets us around that error.
446 --jerry (i'll feel dirty for this) */
449 notify_job_submitted(snum, jobid, new_data->starttime);
450 notify_job_username(snum, jobid, new_data->user);
453 if (new_job || !strequal(old_data->jobname, new_data->jobname))
454 notify_job_name(snum, jobid, new_data->jobname);
456 /* Job attributes of a new job or attributes that can be
459 if (new_job || !strequal(old_data->jobname, new_data->jobname))
460 notify_job_name(snum, jobid, new_data->jobname);
462 if (new_job || old_data->status != new_data->status)
463 notify_job_status(snum, jobid, map_to_spoolss_status(new_data->status));
465 if (new_job || old_data->size != new_data->size)
466 notify_job_total_bytes(snum, jobid, new_data->size);
468 if (new_job || old_data->page_count != new_data->page_count)
469 notify_job_total_pages(snum, jobid, new_data->page_count);
472 /****************************************************************************
473 Store a job structure back to the database.
474 ****************************************************************************/
476 static BOOL pjob_store(int snum, uint32 jobid, struct printjob *pjob)
478 TDB_DATA old_data, new_data;
480 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
482 int len, newlen, buflen;
490 old_data = tdb_fetch(pdb->tdb, print_key(jobid));
492 /* Doh! Now we have to pack/unpack data since the NT_DEVICEMODE was added */
499 len += tdb_pack(buf+len, buflen-len, "dddddddddffff",
501 (uint32)pjob->sysjob,
503 (uint32)pjob->starttime,
504 (uint32)pjob->status,
506 (uint32)pjob->page_count,
507 (uint32)pjob->spooled,
508 (uint32)pjob->smbjob,
514 len += pack_devicemode(pjob->nt_devmode, buf+len, buflen-len);
519 tb = (char *)Realloc(buf, len);
521 DEBUG(0,("pjob_store: failed to enlarge buffer!\n"));
528 } while ( buflen != len );
534 new_data.dsize = len;
535 ret = (tdb_store(pdb->tdb, print_key(jobid), new_data, TDB_REPLACE) == 0);
537 release_print_db(pdb);
539 /* Send notify updates for what has changed */
542 struct printjob old_pjob;
544 if ( old_data.dsize )
546 if ( unpack_pjob( old_data.dptr, old_data.dsize, &old_pjob ) != -1 )
548 pjob_store_notify( snum, jobid, &old_pjob , pjob );
549 free_nt_devicemode( &old_pjob.nt_devmode );
554 pjob_store_notify( snum, jobid, NULL, pjob );
559 SAFE_FREE( old_data.dptr );
565 /****************************************************************************
566 Remove a job structure from the database.
567 ****************************************************************************/
569 void pjob_delete(int snum, uint32 jobid)
571 struct printjob *pjob = print_job_find(snum, jobid);
572 uint32 job_status = 0;
573 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
579 DEBUG(5, ("pjob_delete: we were asked to delete nonexistent job %u\n",
580 (unsigned int)jobid));
581 release_print_db(pdb);
585 /* We must cycle through JOB_STATUS_DELETING and
586 JOB_STATUS_DELETED for the port monitor to delete the job
589 job_status = JOB_STATUS_DELETING|JOB_STATUS_DELETED;
590 notify_job_status(snum, jobid, job_status);
592 /* Remove from printing.tdb */
594 tdb_delete(pdb->tdb, print_key(jobid));
595 remove_from_jobs_changed(snum, jobid);
596 release_print_db(pdb);
597 rap_jobid_delete(snum, jobid);
600 /****************************************************************************
601 Parse a file name from the system spooler to generate a jobid.
602 ****************************************************************************/
604 static uint32 print_parse_jobid(char *fname)
608 if (strncmp(fname,PRINT_SPOOL_PREFIX,strlen(PRINT_SPOOL_PREFIX)) != 0)
610 fname += strlen(PRINT_SPOOL_PREFIX);
616 return (uint32)jobid;
619 /****************************************************************************
620 List a unix job in the print database.
621 ****************************************************************************/
623 static void print_unix_job(int snum, print_queue_struct *q, uint32 jobid)
625 struct printjob pj, *old_pj;
627 if (jobid == (uint32)-1)
628 jobid = q->job + UNIX_JOB_START;
630 /* Preserve the timestamp on an existing unix print job */
632 old_pj = print_job_find(snum, jobid);
639 pj.starttime = old_pj ? old_pj->starttime : q->time;
640 pj.status = q->status;
643 fstrcpy(pj.filename, old_pj ? old_pj->filename : "");
644 if (jobid < UNIX_JOB_START) {
646 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : "Remote Downlevel Document");
649 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : q->fs_file);
651 fstrcpy(pj.user, old_pj ? old_pj->user : q->fs_user);
652 fstrcpy(pj.queuename, old_pj ? old_pj->queuename : lp_const_servicename(snum));
654 pjob_store(snum, jobid, &pj);
658 struct traverse_struct {
659 print_queue_struct *queue;
660 int qcount, snum, maxcount, total_jobs;
664 /****************************************************************************
665 Utility fn to delete any jobs that are no longer active.
666 ****************************************************************************/
668 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
670 struct traverse_struct *ts = (struct traverse_struct *)state;
671 struct printjob pjob;
675 if ( key.dsize != sizeof(jobid) )
678 jobid = IVAL(key.dptr, 0);
679 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
681 free_nt_devicemode( &pjob.nt_devmode );
684 if (ts->snum != lp_servicenumber(pjob.queuename)) {
685 /* this isn't for the queue we are looking at - this cannot happen with the split tdb's. JRA */
690 /* remove a unix job if it isn't in the system queue any more */
692 for (i=0;i<ts->qcount;i++) {
693 uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
694 if (jobid == u_jobid)
697 if (i == ts->qcount) {
698 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !smbjob\n",
699 (unsigned int)jobid ));
700 pjob_delete(ts->snum, jobid);
704 /* need to continue the the bottom of the function to
705 save the correct attributes */
708 /* maybe it hasn't been spooled yet */
710 /* if a job is not spooled and the process doesn't
711 exist then kill it. This cleans up after smbd
713 if (!process_exists(pjob.pid)) {
714 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !process_exists (%u)\n",
715 (unsigned int)jobid, (unsigned int)pjob.pid ));
716 pjob_delete(ts->snum, jobid);
722 /* this check only makes sense for jobs submitted from Windows clients */
725 for (i=0;i<ts->qcount;i++) {
726 uint32 curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
727 if (jobid == curr_jobid)
732 /* The job isn't in the system queue - we have to assume it has
733 completed, so delete the database entry. */
735 if (i == ts->qcount) {
737 /* A race can occur between the time a job is spooled and
738 when it appears in the lpq output. This happens when
739 the job is added to printing.tdb when another smbd
740 running print_queue_update() has completed a lpq and
741 is currently traversing the printing tdb and deleting jobs.
742 Don't delete the job if it was submitted after the lpq_time. */
744 if (pjob.starttime < ts->lpq_time) {
745 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to pjob.starttime (%u) < ts->lpq_time (%u)\n",
747 (unsigned int)pjob.starttime,
748 (unsigned int)ts->lpq_time ));
749 pjob_delete(ts->snum, jobid);
755 /* Save the pjob attributes we will store. */
756 /* FIXME!!! This is the only place where queue->job
757 represents the SMB jobid --jerry */
758 ts->queue[i].job = jobid;
759 ts->queue[i].size = pjob.size;
760 ts->queue[i].page_count = pjob.page_count;
761 ts->queue[i].status = pjob.status;
762 ts->queue[i].priority = 1;
763 ts->queue[i].time = pjob.starttime;
764 fstrcpy(ts->queue[i].fs_user, pjob.user);
765 fstrcpy(ts->queue[i].fs_file, pjob.jobname);
772 /****************************************************************************
773 Check if the print queue has been updated recently enough.
774 ****************************************************************************/
776 static void print_cache_flush(int snum)
779 const char *printername = lp_const_servicename(snum);
780 struct tdb_print_db *pdb = get_print_db_byname(printername);
784 slprintf(key, sizeof(key)-1, "CACHE/%s", printername);
785 tdb_store_int32(pdb->tdb, key, -1);
786 release_print_db(pdb);
789 /****************************************************************************
790 Check if someone already thinks they are doing the update.
791 ****************************************************************************/
793 static pid_t get_updating_pid(fstring printer_name)
798 struct tdb_print_db *pdb = get_print_db_byname(printer_name);
802 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
804 key.dsize = strlen(keystr);
806 data = tdb_fetch(pdb->tdb, key);
807 release_print_db(pdb);
808 if (!data.dptr || data.dsize != sizeof(pid_t)) {
809 SAFE_FREE(data.dptr);
813 updating_pid = IVAL(data.dptr, 0);
814 SAFE_FREE(data.dptr);
816 if (process_exists(updating_pid))
822 /****************************************************************************
823 Set the fact that we're doing the update, or have finished doing the update
825 ****************************************************************************/
827 static void set_updating_pid(const fstring printer_name, BOOL delete)
832 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 SIVAL( buffer, 0, updating_pid);
851 data.dptr = (void *)buffer;
852 data.dsize = 4; /* we always assume this is a 4 byte value */
854 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
855 release_print_db(pdb);
858 /****************************************************************************
859 Sort print jobs by submittal time.
860 ****************************************************************************/
862 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
873 /* Sort on job start time */
875 if (j1->time == j2->time)
877 return (j1->time > j2->time) ? 1 : -1;
880 /****************************************************************************
881 Store the sorted queue representation for later portmon retrieval.
882 ****************************************************************************/
884 static void store_queue_struct(struct tdb_print_db *pdb, struct traverse_struct *pts)
887 int max_reported_jobs = lp_max_reported_jobs(pts->snum);
888 print_queue_struct *queue = pts->queue;
893 if (max_reported_jobs && (max_reported_jobs < pts->qcount))
894 pts->qcount = max_reported_jobs;
895 qcount = pts->qcount;
897 /* Work out the size. */
899 data.dsize += tdb_pack(NULL, 0, "d", qcount);
901 for (i = 0; i < pts->qcount; i++) {
902 data.dsize += tdb_pack(NULL, 0, "ddddddff",
903 (uint32)queue[i].job,
904 (uint32)queue[i].size,
905 (uint32)queue[i].page_count,
906 (uint32)queue[i].status,
907 (uint32)queue[i].priority,
908 (uint32)queue[i].time,
913 if ((data.dptr = malloc(data.dsize)) == NULL)
917 len += tdb_pack(data.dptr + len, data.dsize - len, "d", qcount);
918 for (i = 0; i < pts->qcount; i++) {
919 len += tdb_pack(data.dptr + len, data.dsize - len, "ddddddff",
920 (uint32)queue[i].job,
921 (uint32)queue[i].size,
922 (uint32)queue[i].page_count,
923 (uint32)queue[i].status,
924 (uint32)queue[i].priority,
925 (uint32)queue[i].time,
930 key.dptr = "INFO/linear_queue_array";
931 key.dsize = strlen(key.dptr);
932 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
933 SAFE_FREE(data.dptr);
937 static TDB_DATA get_jobs_changed_data(struct tdb_print_db *pdb)
941 key.dptr = "INFO/jobs_changed";
942 key.dsize = strlen(key.dptr);
945 data = tdb_fetch(pdb->tdb, key);
946 if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0)) {
947 SAFE_FREE(data.dptr);
954 static void check_job_changed(int snum, TDB_DATA data, uint32 jobid)
957 unsigned int job_count = data.dsize / 4;
959 for (i = 0; i < job_count; i++) {
962 ch_jobid = IVAL(data.dptr, i*4);
963 if (ch_jobid == jobid)
964 remove_from_jobs_changed(snum, jobid);
968 /****************************************************************************
969 Update the internal database from the system print queue for a queue.
970 ****************************************************************************/
972 static void print_queue_update_internal(int snum)
975 print_queue_struct *queue = NULL;
976 print_status_struct status;
977 print_status_struct old_status;
978 struct printjob *pjob;
979 struct traverse_struct tstruct;
980 fstring keystr, printer_name, cachestr;
983 struct tdb_print_db *pdb;
984 struct printif *current_printif = get_printer_fns( snum );
986 fstrcpy(printer_name, lp_const_servicename(snum));
987 pdb = get_print_db_byname(printer_name);
992 * Check to see if someone else is doing this update.
993 * This is essentially a mutex on the update.
996 if (get_updating_pid(printer_name) != -1) {
997 release_print_db(pdb);
1001 /* Lock the queue for the database update */
1003 slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", printer_name);
1004 /* Only wait 10 seconds for this. */
1005 if (tdb_lock_bystring(pdb->tdb, keystr, 10) == -1) {
1006 DEBUG(0,("print_queue_update: Failed to lock printer %s database\n", printer_name));
1007 release_print_db(pdb);
1012 * Ensure that no one else got in here.
1013 * If the updating pid is still -1 then we are
1017 if (get_updating_pid(printer_name) != -1) {
1019 * Someone else is doing the update, exit.
1021 tdb_unlock_bystring(pdb->tdb, keystr);
1022 release_print_db(pdb);
1027 * We're going to do the update ourselves.
1030 /* Tell others we're doing the update. */
1031 set_updating_pid(printer_name, False);
1034 * Allow others to enter and notice we're doing
1038 tdb_unlock_bystring(pdb->tdb, keystr);
1041 * Update the cache time FIRST ! Stops others even
1042 * attempting to get the lock and doing this
1043 * if the lpq takes a long time.
1046 slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", printer_name);
1047 tdb_store_int32(pdb->tdb, cachestr, (int)time(NULL));
1049 /* get the current queue using the appropriate interface */
1050 ZERO_STRUCT(status);
1052 qcount = (*(current_printif->queue_get))(snum, &queue, &status);
1054 DEBUG(3, ("%d job%s in queue for %s\n", qcount, (qcount != 1) ?
1055 "s" : "", printer_name));
1057 /* Sort the queue by submission time otherwise they are displayed
1060 qsort(queue, qcount, sizeof(print_queue_struct),
1061 QSORT_CAST(printjob_comp));
1064 any job in the internal database that is marked as spooled
1065 and doesn't exist in the system queue is considered finished
1066 and removed from the database
1068 any job in the system database but not in the internal database
1069 is added as a unix job
1071 fill in any system job numbers as we go
1074 jcdata = get_jobs_changed_data(pdb);
1076 for (i=0; i<qcount; i++) {
1077 uint32 jobid = print_parse_jobid(queue[i].fs_file);
1079 if (jobid == (uint32)-1) {
1080 /* assume its a unix print job */
1081 print_unix_job(snum, &queue[i], jobid);
1085 /* we have an active SMB print job - update its status */
1086 pjob = print_job_find(snum, jobid);
1088 /* err, somethings wrong. Probably smbd was restarted
1089 with jobs in the queue. All we can do is treat them
1090 like unix jobs. Pity. */
1091 print_unix_job(snum, &queue[i], jobid);
1095 pjob->sysjob = queue[i].job;
1096 pjob->status = queue[i].status;
1097 pjob_store(snum, jobid, pjob);
1098 check_job_changed(snum, jcdata, jobid);
1101 SAFE_FREE(jcdata.dptr);
1103 /* now delete any queued entries that don't appear in the
1105 tstruct.queue = queue;
1106 tstruct.qcount = qcount;
1107 tstruct.snum = snum;
1108 tstruct.total_jobs = 0;
1109 tstruct.lpq_time = time(NULL);
1111 tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
1113 /* Store the linearised queue, max jobs only. */
1114 store_queue_struct(pdb, &tstruct);
1116 SAFE_FREE(tstruct.queue);
1118 DEBUG(10,("print_queue_update: printer %s INFO/total_jobs = %d\n",
1119 printer_name, tstruct.total_jobs ));
1121 tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
1123 get_queue_status(snum, &old_status);
1124 if (old_status.qcount != qcount)
1125 DEBUG(10,("print_queue_update: queue status change %d jobs -> %d jobs for printer %s\n",
1126 old_status.qcount, qcount, printer_name ));
1128 /* store the new queue status structure */
1129 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printer_name);
1131 key.dsize = strlen(keystr);
1133 status.qcount = qcount;
1134 data.dptr = (void *)&status;
1135 data.dsize = sizeof(status);
1136 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
1139 * Update the cache time again. We want to do this call
1140 * as little as possible...
1143 slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", printer_name);
1144 tdb_store_int32(pdb->tdb, keystr, (int32)time(NULL));
1146 /* Delete our pid from the db. */
1147 set_updating_pid(printer_name, True);
1148 release_print_db(pdb);
1151 /****************************************************************************
1152 this is the receive function of the background lpq updater
1153 ****************************************************************************/
1154 static void print_queue_receive(int msg_type, pid_t src, void *buf, size_t len)
1158 print_queue_update_internal(snum);
1161 static pid_t background_lpq_updater_pid = -1;
1163 /****************************************************************************
1164 main thread of the background lpq updater
1165 ****************************************************************************/
1166 void start_background_queue(void)
1168 DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
1169 background_lpq_updater_pid = sys_fork();
1171 if (background_lpq_updater_pid == -1) {
1172 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
1176 if(background_lpq_updater_pid == 0) {
1178 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
1180 claim_connection( NULL, "smbd lpq backend", 0, False,
1181 FLAG_MSG_GENERAL|FLAG_MSG_SMBD|FLAG_MSG_PRINT_GENERAL);
1183 if (!locking_init(0)) {
1187 if (!print_backend_init()) {
1191 message_register(MSG_PRINTER_UPDATE, print_queue_receive);
1193 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
1197 /* check for some essential signals first */
1200 exit_server("Caught TERM signal");
1203 if (reload_after_sighup) {
1204 change_to_root_user();
1205 DEBUG(1,("Reloading services after SIGHUP\n"));
1206 reload_services(False);
1207 reload_after_sighup = 0;
1210 /* now check for messages */
1212 DEBUG(10,("start_background_queue: background LPQ thread got a message\n"));
1218 /****************************************************************************
1219 update the internal database from the system print queue for a queue
1220 ****************************************************************************/
1221 static void print_queue_update(int snum)
1224 * Make sure that the backgroup queueu process exists.
1225 * Otherwise just do the update ourselves
1228 if ( background_lpq_updater_pid != -1 ) {
1230 message_send_pid(background_lpq_updater_pid,
1231 MSG_PRINTER_UPDATE, &snum, sizeof(snum),
1235 print_queue_update_internal( snum );
1238 /****************************************************************************
1239 Create/Update an entry in the print tdb that will allow us to send notify
1240 updates only to interested smbd's.
1241 ****************************************************************************/
1243 BOOL print_notify_register_pid(int snum)
1246 struct tdb_print_db *pdb = NULL;
1247 TDB_CONTEXT *tdb = NULL;
1248 const char *printername;
1249 uint32 mypid = (uint32)sys_getpid();
1253 /* if (snum == -1), then the change notify request was
1254 on a print server handle and we need to register on
1259 int num_services = lp_numservices();
1262 for ( idx=0; idx<num_services; idx++ ) {
1263 if (lp_snum_ok(idx) && lp_print_ok(idx) )
1264 print_notify_register_pid(idx);
1269 else /* register for a specific printer */
1271 printername = lp_const_servicename(snum);
1272 pdb = get_print_db_byname(printername);
1278 if (tdb_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1279 DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n",
1282 release_print_db(pdb);
1286 data = get_printer_notify_pid_list( tdb, printername, True );
1288 /* Add ourselves and increase the refcount. */
1290 for (i = 0; i < data.dsize; i += 8) {
1291 if (IVAL(data.dptr,i) == mypid) {
1292 uint32 new_refcount = IVAL(data.dptr, i+4) + 1;
1293 SIVAL(data.dptr, i+4, new_refcount);
1298 if (i == data.dsize) {
1299 /* We weren't in the list. Realloc. */
1300 data.dptr = Realloc(data.dptr, data.dsize + 8);
1302 DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n",
1307 SIVAL(data.dptr,data.dsize - 8,mypid);
1308 SIVAL(data.dptr,data.dsize - 4,1); /* Refcount. */
1311 /* Store back the record. */
1312 if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1313 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1314 list for printer %s\n", printername));
1322 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1324 release_print_db(pdb);
1325 SAFE_FREE(data.dptr);
1329 /****************************************************************************
1330 Update an entry in the print tdb that will allow us to send notify
1331 updates only to interested smbd's.
1332 ****************************************************************************/
1334 BOOL print_notify_deregister_pid(int snum)
1337 struct tdb_print_db *pdb = NULL;
1338 TDB_CONTEXT *tdb = NULL;
1339 const char *printername;
1340 uint32 mypid = (uint32)sys_getpid();
1344 /* if ( snum == -1 ), we are deregister a print server handle
1345 which means to deregister on all print queues */
1349 int num_services = lp_numservices();
1352 for ( idx=0; idx<num_services; idx++ ) {
1353 if ( lp_snum_ok(idx) && lp_print_ok(idx) )
1354 print_notify_deregister_pid(idx);
1359 else /* deregister a specific printer */
1361 printername = lp_const_servicename(snum);
1362 pdb = get_print_db_byname(printername);
1368 if (tdb_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1369 DEBUG(0,("print_notify_register_pid: Failed to lock \
1370 printer %s database\n", printername));
1372 release_print_db(pdb);
1376 data = get_printer_notify_pid_list( tdb, printername, True );
1378 /* Reduce refcount. Remove ourselves if zero. */
1380 for (i = 0; i < data.dsize; ) {
1381 if (IVAL(data.dptr,i) == mypid) {
1382 uint32 refcount = IVAL(data.dptr, i+4);
1386 if (refcount == 0) {
1387 if (data.dsize - i > 8)
1388 memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
1392 SIVAL(data.dptr, i+4, refcount);
1398 if (data.dsize == 0)
1399 SAFE_FREE(data.dptr);
1401 /* Store back the record. */
1402 if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1403 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1404 list for printer %s\n", printername));
1412 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1414 release_print_db(pdb);
1415 SAFE_FREE(data.dptr);
1419 /****************************************************************************
1420 Check if a jobid is valid. It is valid if it exists in the database.
1421 ****************************************************************************/
1423 BOOL print_job_exists(int snum, uint32 jobid)
1425 struct tdb_print_db *pdb = get_print_db_byname(lp_const_servicename(snum));
1430 ret = tdb_exists(pdb->tdb, print_key(jobid));
1431 release_print_db(pdb);
1435 /****************************************************************************
1436 Give the fd used for a jobid.
1437 ****************************************************************************/
1439 int print_job_fd(int snum, uint32 jobid)
1441 struct printjob *pjob = print_job_find(snum, jobid);
1444 /* don't allow another process to get this info - it is meaningless */
1445 if (pjob->pid != sys_getpid())
1450 /****************************************************************************
1451 Give the filename used for a jobid.
1452 Only valid for the process doing the spooling and when the job
1453 has not been spooled.
1454 ****************************************************************************/
1456 char *print_job_fname(int snum, uint32 jobid)
1458 struct printjob *pjob = print_job_find(snum, jobid);
1459 if (!pjob || pjob->spooled || pjob->pid != sys_getpid())
1461 return pjob->filename;
1465 /****************************************************************************
1466 Give the filename used for a jobid.
1467 Only valid for the process doing the spooling and when the job
1468 has not been spooled.
1469 ****************************************************************************/
1471 NT_DEVICEMODE *print_job_devmode(int snum, uint32 jobid)
1473 struct printjob *pjob = print_job_find(snum, jobid);
1478 return pjob->nt_devmode;
1481 /****************************************************************************
1482 Set the place in the queue for a job.
1483 ****************************************************************************/
1485 BOOL print_job_set_place(int snum, uint32 jobid, int place)
1487 DEBUG(2,("print_job_set_place not implemented yet\n"));
1491 /****************************************************************************
1492 Set the name of a job. Only possible for owner.
1493 ****************************************************************************/
1495 BOOL print_job_set_name(int snum, uint32 jobid, char *name)
1497 struct printjob *pjob = print_job_find(snum, jobid);
1498 if (!pjob || pjob->pid != sys_getpid())
1501 fstrcpy(pjob->jobname, name);
1502 return pjob_store(snum, jobid, pjob);
1505 /***************************************************************************
1506 Remove a jobid from the 'jobs changed' list.
1507 ***************************************************************************/
1509 static BOOL remove_from_jobs_changed(int snum, uint32 jobid)
1511 const char *printername = lp_const_servicename(snum);
1512 struct tdb_print_db *pdb = get_print_db_byname(printername);
1514 size_t job_count, i;
1516 BOOL gotlock = False;
1518 key.dptr = "INFO/jobs_changed";
1519 key.dsize = strlen(key.dptr);
1522 if (tdb_chainlock_with_timeout(pdb->tdb, key, 5) == -1)
1527 data = tdb_fetch(pdb->tdb, key);
1529 if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0))
1532 job_count = data.dsize / 4;
1533 for (i = 0; i < job_count; i++) {
1536 ch_jobid = IVAL(data.dptr, i*4);
1537 if (ch_jobid == jobid) {
1538 if (i < job_count -1 )
1539 memmove(data.dptr + (i*4), data.dptr + (i*4) + 4, (job_count - i - 1)*4 );
1541 if (tdb_store(pdb->tdb, key, data, TDB_REPLACE) == -1)
1551 tdb_chainunlock(pdb->tdb, key);
1552 SAFE_FREE(data.dptr);
1553 release_print_db(pdb);
1555 DEBUG(10,("remove_from_jobs_changed: removed jobid %u\n", (unsigned int)jobid ));
1557 DEBUG(10,("remove_from_jobs_changed: Failed to remove jobid %u\n", (unsigned int)jobid ));
1561 /****************************************************************************
1562 Delete a print job - don't update queue.
1563 ****************************************************************************/
1565 static BOOL print_job_delete1(int snum, uint32 jobid)
1567 struct printjob *pjob = print_job_find(snum, jobid);
1569 struct printif *current_printif = get_printer_fns( snum );
1575 * If already deleting just return.
1578 if (pjob->status == LPQ_DELETING)
1581 /* Hrm - we need to be able to cope with deleting a job before it
1582 has reached the spooler. */
1584 if (pjob->sysjob == -1) {
1585 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid));
1588 /* Set the tdb entry to be deleting. */
1590 pjob->status = LPQ_DELETING;
1591 pjob_store(snum, jobid, pjob);
1593 if (pjob->spooled && pjob->sysjob != -1)
1594 result = (*(current_printif->job_delete))(snum, pjob);
1596 /* Delete the tdb entry if the delete succeeded or the job hasn't
1600 const char *printername = lp_const_servicename(snum);
1601 struct tdb_print_db *pdb = get_print_db_byname(printername);
1606 pjob_delete(snum, jobid);
1607 /* Ensure we keep a rough count of the number of total jobs... */
1608 tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, -1);
1609 release_print_db(pdb);
1612 return (result == 0);
1615 /****************************************************************************
1616 Return true if the current user owns the print job.
1617 ****************************************************************************/
1619 static BOOL is_owner(struct current_user *user, int snum, uint32 jobid)
1621 struct printjob *pjob = print_job_find(snum, jobid);
1627 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1628 return strequal(pjob->user, vuser->user.smb_name);
1630 return strequal(pjob->user, uidtoname(user->uid));
1634 /****************************************************************************
1636 ****************************************************************************/
1638 BOOL print_job_delete(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1640 BOOL owner, deleted;
1645 owner = is_owner(user, snum, jobid);
1647 /* Check access against security descriptor or whether the user
1651 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1652 DEBUG(3, ("delete denied by security descriptor\n"));
1653 *errcode = WERR_ACCESS_DENIED;
1655 /* BEGIN_ADMIN_LOG */
1656 sys_adminlog( LOG_ERR,
1657 "Permission denied-- user not allowed to delete, \
1658 pause, or resume print job. User name: %s. Printer name: %s.",
1659 uidtoname(user->uid), PRINTERNAME(snum) );
1666 * get the spooled filename of the print job
1667 * if this works, then the file has not been spooled
1668 * to the underlying print system. Just delete the
1669 * spool file & return.
1672 if ( (fname = print_job_fname( snum, jobid )) != NULL )
1674 /* remove the spool file */
1675 DEBUG(10,("print_job_delete: Removing spool file [%s]\n", fname ));
1676 if ( unlink( fname ) == -1 ) {
1677 *errcode = map_werror_from_unix(errno);
1682 if (!print_job_delete1(snum, jobid)) {
1683 *errcode = WERR_ACCESS_DENIED;
1687 /* force update the database and say the delete failed if the
1690 print_queue_update(snum);
1692 deleted = !print_job_exists(snum, jobid);
1694 *errcode = WERR_ACCESS_DENIED;
1699 /****************************************************************************
1701 ****************************************************************************/
1703 BOOL print_job_pause(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1705 struct printjob *pjob = print_job_find(snum, jobid);
1707 struct printif *current_printif = get_printer_fns( snum );
1712 if (!pjob->spooled || pjob->sysjob == -1)
1715 if (!is_owner(user, snum, jobid) &&
1716 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1717 DEBUG(3, ("pause denied by security descriptor\n"));
1719 /* BEGIN_ADMIN_LOG */
1720 sys_adminlog( LOG_ERR,
1721 "Permission denied-- user not allowed to delete, \
1722 pause, or resume print job. User name: %s. Printer name: %s.",
1723 uidtoname(user->uid), PRINTERNAME(snum) );
1726 *errcode = WERR_ACCESS_DENIED;
1730 /* need to pause the spooled entry */
1731 ret = (*(current_printif->job_pause))(snum, pjob);
1734 *errcode = WERR_INVALID_PARAM;
1738 /* force update the database */
1739 print_cache_flush(snum);
1741 /* Send a printer notify message */
1743 notify_job_status(snum, jobid, JOB_STATUS_PAUSED);
1745 /* how do we tell if this succeeded? */
1750 /****************************************************************************
1752 ****************************************************************************/
1754 BOOL print_job_resume(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1756 struct printjob *pjob = print_job_find(snum, jobid);
1758 struct printif *current_printif = get_printer_fns( snum );
1763 if (!pjob->spooled || pjob->sysjob == -1)
1766 if (!is_owner(user, snum, jobid) &&
1767 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1768 DEBUG(3, ("resume denied by security descriptor\n"));
1769 *errcode = WERR_ACCESS_DENIED;
1771 /* BEGIN_ADMIN_LOG */
1772 sys_adminlog( LOG_ERR,
1773 "Permission denied-- user not allowed to delete, \
1774 pause, or resume print job. User name: %s. Printer name: %s.",
1775 uidtoname(user->uid), PRINTERNAME(snum) );
1780 ret = (*(current_printif->job_resume))(snum, pjob);
1783 *errcode = WERR_INVALID_PARAM;
1787 /* force update the database */
1788 print_cache_flush(snum);
1790 /* Send a printer notify message */
1792 notify_job_status(snum, jobid, JOB_STATUS_QUEUED);
1797 /****************************************************************************
1798 Write to a print file.
1799 ****************************************************************************/
1801 int print_job_write(int snum, uint32 jobid, const char *buf, int size)
1804 struct printjob *pjob = print_job_find(snum, jobid);
1808 /* don't allow another process to get this info - it is meaningless */
1809 if (pjob->pid != sys_getpid())
1812 return_code = write(pjob->fd, buf, size);
1813 if (return_code>0) {
1815 pjob_store(snum, jobid, pjob);
1820 /****************************************************************************
1821 Check if the print queue has been updated recently enough.
1822 ****************************************************************************/
1824 static BOOL print_cache_expired(int snum)
1827 time_t last_qscan_time, time_now = time(NULL);
1828 const char *printername = lp_const_servicename(snum);
1829 struct tdb_print_db *pdb = get_print_db_byname(printername);
1834 slprintf(key, sizeof(key), "CACHE/%s", printername);
1835 last_qscan_time = (time_t)tdb_fetch_int32(pdb->tdb, key);
1838 * Invalidate the queue for 3 reasons.
1839 * (1). last queue scan time == -1.
1840 * (2). Current time - last queue scan time > allowed cache time.
1841 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1842 * This last test picks up machines for which the clock has been moved
1843 * forward, an lpq scan done and then the clock moved back. Otherwise
1844 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1847 if (last_qscan_time == ((time_t)-1) || (time_now - last_qscan_time) >= lp_lpqcachetime() ||
1848 last_qscan_time > (time_now + MAX_CACHE_VALID_TIME)) {
1849 DEBUG(3, ("print cache expired for queue %s \
1850 (last_qscan_time = %d, time now = %d, qcachetime = %d)\n", printername,
1851 (int)last_qscan_time, (int)time_now, (int)lp_lpqcachetime() ));
1852 release_print_db(pdb);
1855 release_print_db(pdb);
1859 /****************************************************************************
1860 Get the queue status - do not update if db is out of date.
1861 ****************************************************************************/
1863 static int get_queue_status(int snum, print_status_struct *status)
1867 const char *printername = lp_const_servicename(snum);
1868 struct tdb_print_db *pdb = get_print_db_byname(printername);
1875 ZERO_STRUCTP(status);
1876 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
1878 key.dsize = strlen(keystr);
1879 data = tdb_fetch(pdb->tdb, key);
1881 if (data.dsize == sizeof(print_status_struct))
1882 /* this memcpy is ok since the status struct was
1883 not packed before storing it in the tdb */
1884 memcpy(status, data.dptr, sizeof(print_status_struct));
1885 SAFE_FREE(data.dptr);
1888 len = tdb_fetch_int32(pdb->tdb, "INFO/total_jobs");
1889 release_print_db(pdb);
1890 return (len == -1 ? 0 : len);
1893 /****************************************************************************
1894 Determine the number of jobs in a queue.
1895 ****************************************************************************/
1897 int print_queue_length(int snum, print_status_struct *pstatus)
1899 print_status_struct status;
1902 /* make sure the database is up to date */
1903 if (print_cache_expired(snum))
1904 print_queue_update(snum);
1906 /* also fetch the queue status */
1907 memset(&status, 0, sizeof(status));
1908 len = get_queue_status(snum, &status);
1916 /***************************************************************************
1917 Allocate a jobid. Hold the lock for as short a time as possible.
1918 ***************************************************************************/
1920 static BOOL allocate_print_jobid(struct tdb_print_db *pdb, int snum, const char *printername, uint32 *pjobid)
1925 *pjobid = (uint32)-1;
1927 for (i = 0; i < 3; i++) {
1928 /* Lock the database - only wait 20 seconds. */
1929 if (tdb_lock_bystring(pdb->tdb, "INFO/nextjob", 20) == -1) {
1930 DEBUG(0,("allocate_print_jobid: failed to lock printing database %s\n", printername ));
1934 if (!tdb_fetch_uint32(pdb->tdb, "INFO/nextjob", &jobid)) {
1935 if (tdb_error(pdb->tdb) != TDB_ERR_NOEXIST) {
1936 DEBUG(0, ("allocate_print_jobid: failed to fetch INFO/nextjob for print queue %s\n",
1943 jobid = NEXT_JOBID(jobid);
1945 if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
1946 DEBUG(3, ("allocate_print_jobid: failed to store INFO/nextjob.\n"));
1947 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1951 /* We've finished with the INFO/nextjob lock. */
1952 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
1954 if (!print_job_exists(snum, jobid))
1959 DEBUG(0, ("allocate_print_jobid: failed to allocate a print job for queue %s\n",
1961 /* Probably full... */
1966 /* Store a dummy placeholder. */
1971 if (tdb_store(pdb->tdb, print_key(jobid), dum, TDB_INSERT) == -1) {
1972 DEBUG(3, ("allocate_print_jobid: jobid (%d) failed to store placeholder.\n",
1982 /***************************************************************************
1983 Append a jobid to the 'jobs changed' list.
1984 ***************************************************************************/
1986 static BOOL add_to_jobs_changed(struct tdb_print_db *pdb, uint32 jobid)
1991 key.dptr = "INFO/jobs_changed";
1992 key.dsize = strlen(key.dptr);
1993 SIVAL(&store_jobid, 0, jobid);
1994 data.dptr = (char *)&store_jobid;
1997 DEBUG(10,("add_to_jobs_changed: Added jobid %u\n", (unsigned int)jobid ));
1999 return (tdb_append(pdb->tdb, key, data) == 0);
2002 /***************************************************************************
2003 Start spooling a job - return the jobid.
2004 ***************************************************************************/
2006 uint32 print_job_start(struct current_user *user, int snum, char *jobname, NT_DEVICEMODE *nt_devmode )
2010 struct printjob pjob;
2012 const char *printername = lp_const_servicename(snum);
2013 struct tdb_print_db *pdb = get_print_db_byname(printername);
2021 if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
2022 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
2023 release_print_db(pdb);
2027 if (!print_time_access_check(snum)) {
2028 DEBUG(3, ("print_job_start: job start denied by time check\n"));
2029 release_print_db(pdb);
2033 path = lp_pathname(snum);
2035 /* see if we have sufficient disk space */
2036 if (lp_minprintspace(snum)) {
2037 SMB_BIG_UINT dspace, dsize;
2038 if (sys_fsusage(path, &dspace, &dsize) == 0 &&
2039 dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
2040 DEBUG(3, ("print_job_start: disk space check failed.\n"));
2041 release_print_db(pdb);
2047 /* for autoloaded printers, check that the printcap entry still exists */
2048 if (lp_autoloaded(snum) && !pcap_printername_ok(lp_const_servicename(snum), NULL)) {
2049 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum) ));
2050 release_print_db(pdb);
2055 /* Insure the maximum queue size is not violated */
2056 if ((njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
2057 DEBUG(3, ("print_job_start: Queue %s number of jobs (%d) larger than max printjobs per queue (%d).\n",
2058 printername, njobs, lp_maxprintjobs(snum) ));
2059 release_print_db(pdb);
2064 DEBUG(10,("print_job_start: Queue %s number of jobs (%d), max printjobs = %d\n",
2065 printername, njobs, lp_maxprintjobs(snum) ));
2067 if (!allocate_print_jobid(pdb, snum, printername, &jobid))
2070 /* create the database entry */
2074 pjob.pid = sys_getpid();
2077 pjob.starttime = time(NULL);
2078 pjob.status = LPQ_SPOOLING;
2080 pjob.spooled = False;
2082 pjob.nt_devmode = nt_devmode;
2084 fstrcpy(pjob.jobname, jobname);
2086 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
2087 fstrcpy(pjob.user, vuser->user.smb_name);
2089 fstrcpy(pjob.user, uidtoname(user->uid));
2092 fstrcpy(pjob.queuename, lp_const_servicename(snum));
2094 /* we have a job entry - now create the spool file */
2095 slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.8u.XXXXXX",
2096 path, PRINT_SPOOL_PREFIX, (unsigned int)jobid);
2097 pjob.fd = smb_mkstemp(pjob.filename);
2099 if (pjob.fd == -1) {
2100 if (errno == EACCES) {
2101 /* Common setup error, force a report. */
2102 DEBUG(0, ("print_job_start: insufficient permissions \
2103 to open spool file %s.\n", pjob.filename));
2105 /* Normal case, report at level 3 and above. */
2106 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
2107 DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
2112 pjob_store(snum, jobid, &pjob);
2114 /* Update the 'jobs changed' entry used by print_queue_status. */
2115 add_to_jobs_changed(pdb, jobid);
2117 /* Ensure we keep a rough count of the number of total jobs... */
2118 tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, 1);
2120 release_print_db(pdb);
2126 pjob_delete(snum, jobid);
2128 release_print_db(pdb);
2130 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
2134 /****************************************************************************
2135 Update the number of pages spooled to jobid
2136 ****************************************************************************/
2138 void print_job_endpage(int snum, uint32 jobid)
2140 struct printjob *pjob = print_job_find(snum, jobid);
2143 /* don't allow another process to get this info - it is meaningless */
2144 if (pjob->pid != sys_getpid())
2148 pjob_store(snum, jobid, pjob);
2151 /****************************************************************************
2152 Print a file - called on closing the file. This spools the job.
2153 If normal close is false then we're tearing down the jobs - treat as an
2155 ****************************************************************************/
2157 BOOL print_job_end(int snum, uint32 jobid, BOOL normal_close)
2159 struct printjob *pjob = print_job_find(snum, jobid);
2161 SMB_STRUCT_STAT sbuf;
2162 struct printif *current_printif = get_printer_fns( snum );
2167 if (pjob->spooled || pjob->pid != sys_getpid())
2170 if (normal_close && (sys_fstat(pjob->fd, &sbuf) == 0)) {
2171 pjob->size = sbuf.st_size;
2177 * Not a normal close or we couldn't stat the job file,
2178 * so something has gone wrong. Cleanup.
2182 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
2186 /* Technically, this is not quite right. If the printer has a separator
2187 * page turned on, the NT spooler prints the separator page even if the
2188 * print job is 0 bytes. 010215 JRR */
2189 if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
2190 /* don't bother spooling empty files or something being deleted. */
2191 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
2192 pjob->filename, pjob->size ? "deleted" : "zero length" ));
2193 unlink(pjob->filename);
2194 pjob_delete(snum, jobid);
2198 pjob->smbjob = jobid;
2200 ret = (*(current_printif->job_submit))(snum, pjob);
2205 /* The print job has been sucessfully handed over to the back-end */
2207 pjob->spooled = True;
2208 pjob->status = LPQ_QUEUED;
2209 pjob_store(snum, jobid, pjob);
2211 /* make sure the database is up to date */
2212 if (print_cache_expired(snum))
2213 print_queue_update(snum);
2219 /* The print job was not succesfully started. Cleanup */
2220 /* Still need to add proper error return propagation! 010122:JRR */
2221 unlink(pjob->filename);
2222 pjob_delete(snum, jobid);
2226 /****************************************************************************
2227 Get a snapshot of jobs in the system without traversing.
2228 ****************************************************************************/
2230 static BOOL get_stored_queue_info(struct tdb_print_db *pdb, int snum, int *pcount, print_queue_struct **ppqueue)
2232 TDB_DATA data, key, cgdata;
2233 print_queue_struct *queue = NULL;
2235 uint32 extra_count = 0;
2236 int total_count = 0;
2239 int max_reported_jobs = lp_max_reported_jobs(snum);
2242 /* make sure the database is up to date */
2243 if (print_cache_expired(snum))
2244 print_queue_update(snum);
2250 ZERO_STRUCT(cgdata);
2251 key.dptr = "INFO/linear_queue_array";
2252 key.dsize = strlen(key.dptr);
2254 /* Get the stored queue data. */
2255 data = tdb_fetch(pdb->tdb, key);
2257 if (data.dptr && data.dsize >= sizeof(qcount))
2258 len += tdb_unpack(data.dptr + len, data.dsize - len, "d", &qcount);
2260 /* Get the changed jobs list. */
2261 key.dptr = "INFO/jobs_changed";
2262 key.dsize = strlen(key.dptr);
2264 cgdata = tdb_fetch(pdb->tdb, key);
2265 if (cgdata.dptr != NULL && (cgdata.dsize % 4 == 0))
2266 extra_count = cgdata.dsize/4;
2268 DEBUG(5,("get_stored_queue_info: qcount = %u, extra_count = %u\n", (unsigned int)qcount, (unsigned int)extra_count));
2270 /* Allocate the queue size. */
2271 if (qcount == 0 && extra_count == 0)
2274 if ((queue = (print_queue_struct *)malloc(sizeof(print_queue_struct)*(qcount + extra_count))) == NULL)
2277 /* Retrieve the linearised queue data. */
2279 for( i = 0; i < qcount; i++) {
2280 uint32 qjob, qsize, qpage_count, qstatus, qpriority, qtime;
2281 len += tdb_unpack(data.dptr + len, data.dsize - len, "ddddddff",
2290 queue[i].job = qjob;
2291 queue[i].size = qsize;
2292 queue[i].page_count = qpage_count;
2293 queue[i].status = qstatus;
2294 queue[i].priority = qpriority;
2295 queue[i].time = qtime;
2298 total_count = qcount;
2300 /* Add in the changed jobids. */
2301 for( i = 0; i < extra_count; i++) {
2303 struct printjob *pjob;
2305 jobid = IVAL(cgdata.dptr, i*4);
2306 DEBUG(5,("get_stored_queue_info: changed job = %u\n", (unsigned int)jobid));
2307 pjob = print_job_find(snum, jobid);
2309 DEBUG(5,("get_stored_queue_info: failed to find changed job = %u\n", (unsigned int)jobid));
2310 remove_from_jobs_changed(snum, jobid);
2314 queue[total_count].job = jobid;
2315 queue[total_count].size = pjob->size;
2316 queue[total_count].page_count = pjob->page_count;
2317 queue[total_count].status = pjob->status;
2318 queue[total_count].priority = 1;
2319 queue[total_count].time = pjob->starttime;
2320 fstrcpy(queue[total_count].fs_user, pjob->user);
2321 fstrcpy(queue[total_count].fs_file, pjob->jobname);
2325 /* Sort the queue by submission time otherwise they are displayed
2328 qsort(queue, total_count, sizeof(print_queue_struct), QSORT_CAST(printjob_comp));
2330 DEBUG(5,("get_stored_queue_info: total_count = %u\n", (unsigned int)total_count));
2332 if (max_reported_jobs && total_count > max_reported_jobs)
2333 total_count = max_reported_jobs;
2336 *pcount = total_count;
2342 SAFE_FREE(data.dptr);
2343 SAFE_FREE(cgdata.dptr);
2347 /****************************************************************************
2348 Get a printer queue listing.
2349 set queue = NULL and status = NULL if you just want to update the cache
2350 ****************************************************************************/
2352 int print_queue_status(int snum,
2353 print_queue_struct **ppqueue,
2354 print_status_struct *status)
2358 const char *printername;
2359 struct tdb_print_db *pdb;
2362 /* make sure the database is up to date */
2364 if (print_cache_expired(snum))
2365 print_queue_update(snum);
2367 /* return if we are done */
2368 if ( !ppqueue || !status )
2372 printername = lp_const_servicename(snum);
2373 pdb = get_print_db_byname(printername);
2379 * Fetch the queue status. We must do this first, as there may
2380 * be no jobs in the queue.
2383 ZERO_STRUCTP(status);
2384 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printername);
2386 key.dsize = strlen(keystr);
2387 data = tdb_fetch(pdb->tdb, key);
2389 if (data.dsize == sizeof(*status)) {
2390 /* this memcpy is ok since the status struct was
2391 not packed before storing it in the tdb */
2392 memcpy(status, data.dptr, sizeof(*status));
2394 SAFE_FREE(data.dptr);
2398 * Now, fetch the print queue information. We first count the number
2399 * of entries, and then only retrieve the queue if necessary.
2402 if (!get_stored_queue_info(pdb, snum, &count, ppqueue)) {
2403 release_print_db(pdb);
2407 release_print_db(pdb);
2411 /****************************************************************************
2413 ****************************************************************************/
2415 BOOL print_queue_pause(struct current_user *user, int snum, WERROR *errcode)
2418 struct printif *current_printif = get_printer_fns( snum );
2420 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2421 *errcode = WERR_ACCESS_DENIED;
2425 ret = (*(current_printif->queue_pause))(snum);
2428 *errcode = WERR_INVALID_PARAM;
2432 /* force update the database */
2433 print_cache_flush(snum);
2435 /* Send a printer notify message */
2437 notify_printer_status(snum, PRINTER_STATUS_PAUSED);
2442 /****************************************************************************
2444 ****************************************************************************/
2446 BOOL print_queue_resume(struct current_user *user, int snum, WERROR *errcode)
2449 struct printif *current_printif = get_printer_fns( snum );
2451 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2452 *errcode = WERR_ACCESS_DENIED;
2456 ret = (*(current_printif->queue_resume))(snum);
2459 *errcode = WERR_INVALID_PARAM;
2463 /* make sure the database is up to date */
2464 if (print_cache_expired(snum))
2465 print_queue_update(snum);
2467 /* Send a printer notify message */
2469 notify_printer_status(snum, PRINTER_STATUS_OK);
2474 /****************************************************************************
2475 Purge a queue - implemented by deleting all jobs that we can delete.
2476 ****************************************************************************/
2478 BOOL print_queue_purge(struct current_user *user, int snum, WERROR *errcode)
2480 print_queue_struct *queue;
2481 print_status_struct status;
2485 /* Force and update so the count is accurate (i.e. not a cached count) */
2486 print_queue_update(snum);
2488 can_job_admin = print_access_check(user, snum, JOB_ACCESS_ADMINISTER);
2489 njobs = print_queue_status(snum, &queue, &status);
2491 for (i=0;i<njobs;i++) {
2492 BOOL owner = is_owner(user, snum, queue[i].job);
2494 if (owner || can_job_admin) {
2495 print_job_delete1(snum, queue[i].job);