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(const char* sharename, 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 static TDB_CONTEXT *rap_tdb;
47 static uint16 next_rap_jobid;
48 struct rap_jobid_key {
53 /***************************************************************************
54 Nightmare. LANMAN jobid's are 16 bit numbers..... We must map them to 32
55 bit RPC jobids.... JRA.
56 ***************************************************************************/
58 uint16 pjobid_to_rap(const char* sharename, uint32 jobid)
62 struct rap_jobid_key jinfo;
65 DEBUG(10,("pjobid_to_rap: called.\n"));
68 /* Create the in-memory tdb. */
69 rap_tdb = tdb_open_log(NULL, 0, TDB_INTERNAL, (O_RDWR|O_CREAT), 0644);
75 fstrcpy( jinfo.sharename, sharename );
77 key.dptr = (char*)&jinfo;
78 key.dsize = sizeof(jinfo);
80 data = tdb_fetch(rap_tdb, key);
81 if (data.dptr && data.dsize == sizeof(uint16)) {
82 rap_jobid = SVAL(data.dptr, 0);
84 DEBUG(10,("pjobid_to_rap: jobid %u maps to RAP jobid %u\n",
85 (unsigned int)jobid, (unsigned int)rap_jobid));
89 /* Not found - create and store mapping. */
90 rap_jobid = ++next_rap_jobid;
92 rap_jobid = ++next_rap_jobid;
93 SSVAL(buf,0,rap_jobid);
95 data.dsize = sizeof(rap_jobid);
96 tdb_store(rap_tdb, key, data, TDB_REPLACE);
97 tdb_store(rap_tdb, data, key, TDB_REPLACE);
99 DEBUG(10,("pjobid_to_rap: created jobid %u maps to RAP jobid %u\n",
100 (unsigned int)jobid, (unsigned int)rap_jobid));
104 BOOL rap_to_pjobid(uint16 rap_jobid, fstring sharename, uint32 *pjobid)
109 DEBUG(10,("rap_to_pjobid called.\n"));
114 SSVAL(buf,0,rap_jobid);
116 key.dsize = sizeof(rap_jobid);
117 data = tdb_fetch(rap_tdb, key);
118 if ( data.dptr && data.dsize == sizeof(struct rap_jobid_key) )
120 struct rap_jobid_key *jinfo = (struct rap_jobid_key*)data.dptr;
121 fstrcpy( sharename, jinfo->sharename );
122 *pjobid = jinfo->jobid;
123 DEBUG(10,("rap_to_pjobid: jobid %u maps to RAP jobid %u\n",
124 (unsigned int)*pjobid, (unsigned int)rap_jobid));
125 SAFE_FREE(data.dptr);
129 DEBUG(10,("rap_to_pjobid: Failed to lookup RAP jobid %u\n",
130 (unsigned int)rap_jobid));
131 SAFE_FREE(data.dptr);
135 static void rap_jobid_delete(const char* sharename, uint32 jobid)
139 struct rap_jobid_key jinfo;
142 DEBUG(10,("rap_jobid_delete: called.\n"));
147 ZERO_STRUCT( jinfo );
148 fstrcpy( jinfo.sharename, sharename );
150 key.dptr = (char*)&jinfo;
151 key.dsize = sizeof(jinfo);
153 data = tdb_fetch(rap_tdb, key);
154 if (!data.dptr || (data.dsize != sizeof(uint16))) {
155 DEBUG(10,("rap_jobid_delete: cannot find jobid %u\n",
156 (unsigned int)jobid ));
157 SAFE_FREE(data.dptr);
161 DEBUG(10,("rap_jobid_delete: deleting jobid %u\n",
162 (unsigned int)jobid ));
164 rap_jobid = SVAL(data.dptr, 0);
165 SAFE_FREE(data.dptr);
166 SSVAL(buf,0,rap_jobid);
168 data.dsize = sizeof(rap_jobid);
169 tdb_delete(rap_tdb, key);
170 tdb_delete(rap_tdb, data);
173 static int get_queue_status(const char* sharename, print_status_struct *);
175 /****************************************************************************
176 Initialise the printing backend. Called once at startup before the fork().
177 ****************************************************************************/
179 BOOL print_backend_init(void)
181 const char *sversion = "INFO/version";
182 pstring printing_path;
183 int services = lp_numservices();
186 unlink(lock_path("printing.tdb"));
187 pstrcpy(printing_path,lock_path("printing"));
188 mkdir(printing_path,0755);
190 /* handle a Samba upgrade */
192 for (snum = 0; snum < services; snum++) {
193 struct tdb_print_db *pdb;
194 if (!lp_print_ok(snum))
197 pdb = get_print_db_byname(lp_const_servicename(snum));
200 if (tdb_lock_bystring(pdb->tdb, sversion, 0) == -1) {
201 DEBUG(0,("print_backend_init: Failed to open printer %s database\n", lp_const_servicename(snum) ));
202 release_print_db(pdb);
205 if (tdb_fetch_int32(pdb->tdb, sversion) != PRINT_DATABASE_VERSION) {
206 tdb_traverse(pdb->tdb, tdb_traverse_delete_fn, NULL);
207 tdb_store_int32(pdb->tdb, sversion, PRINT_DATABASE_VERSION);
209 tdb_unlock_bystring(pdb->tdb, sversion);
210 release_print_db(pdb);
213 close_all_print_db(); /* Don't leave any open. */
215 /* do NT print initialization... */
216 return nt_printing_init();
219 /****************************************************************************
220 Shut down printing backend. Called once at shutdown to close the tdb.
221 ****************************************************************************/
223 void printing_end(void)
225 close_all_print_db(); /* Don't leave any open. */
228 /****************************************************************************
229 Retrieve the set of printing functions for a given service. This allows
230 us to set the printer function table based on the value of the 'printing'
233 Use the generic interface as the default and only use cups interface only
234 when asked for (and only when supported)
235 ****************************************************************************/
237 static struct printif *get_printer_fns_from_type( int type )
239 struct printif *printer_fns = &generic_printif;
242 if ( type == PRINT_CUPS ) {
243 printer_fns = &cups_printif;
245 #endif /* HAVE_CUPS */
248 if ( type == PRINT_IPRINT ) {
249 printer_fns = &iprint_printif;
251 #endif /* HAVE_IPRINT */
253 printer_fns->type = type;
258 static struct printif *get_printer_fns( int snum )
260 return get_printer_fns_from_type( lp_printing(snum) );
264 /****************************************************************************
265 Useful function to generate a tdb key.
266 ****************************************************************************/
268 static TDB_DATA print_key(uint32 jobid)
274 ret.dptr = (void *)&j;
275 ret.dsize = sizeof(j);
279 /***********************************************************************
280 unpack a pjob from a tdb buffer
281 ***********************************************************************/
283 int unpack_pjob( char* buf, int buflen, struct printjob *pjob )
287 uint32 pjpid, pjsysjob, pjfd, pjstarttime, pjstatus;
288 uint32 pjsize, pjpage_count, pjspooled, pjsmbjob;
293 len += tdb_unpack(buf+len, buflen-len, "dddddddddffff",
311 if ( (used = unpack_devicemode(&pjob->nt_devmode, buf+len, buflen-len)) == -1 )
317 pjob->sysjob = pjsysjob;
319 pjob->starttime = pjstarttime;
320 pjob->status = pjstatus;
322 pjob->page_count = pjpage_count;
323 pjob->spooled = pjspooled;
324 pjob->smbjob = pjsmbjob;
330 /****************************************************************************
331 Useful function to find a print job in the database.
332 ****************************************************************************/
334 static struct printjob *print_job_find(const char *sharename, uint32 jobid)
336 static struct printjob pjob;
338 struct tdb_print_db *pdb = get_print_db_byname(sharename);
344 ret = tdb_fetch(pdb->tdb, print_key(jobid));
345 release_print_db(pdb);
350 if ( pjob.nt_devmode )
351 free_nt_devicemode( &pjob.nt_devmode );
355 if ( unpack_pjob( ret.dptr, ret.dsize, &pjob ) == -1 ) {
364 /* Convert a unix jobid to a smb jobid */
366 static uint32 sysjob_to_jobid_value;
368 static int unixjob_traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key,
369 TDB_DATA data, void *state)
371 struct printjob *pjob;
372 int *sysjob = (int *)state;
374 if (!data.dptr || data.dsize == 0)
377 pjob = (struct printjob *)data.dptr;
378 if (key.dsize != sizeof(uint32))
381 if (*sysjob == pjob->sysjob) {
382 uint32 jobid = IVAL(key.dptr,0);
384 sysjob_to_jobid_value = jobid;
391 /****************************************************************************
392 This is a *horribly expensive call as we have to iterate through all the
393 current printer tdb's. Don't do this often ! JRA.
394 ****************************************************************************/
396 uint32 sysjob_to_jobid(int unix_jobid)
398 int services = lp_numservices();
401 sysjob_to_jobid_value = (uint32)-1;
403 for (snum = 0; snum < services; snum++) {
404 struct tdb_print_db *pdb;
405 if (!lp_print_ok(snum))
407 pdb = get_print_db_byname(lp_const_servicename(snum));
411 tdb_traverse(pdb->tdb, unixjob_traverse_fn, &unix_jobid);
412 release_print_db(pdb);
413 if (sysjob_to_jobid_value != (uint32)-1)
414 return sysjob_to_jobid_value;
419 /****************************************************************************
420 Send notifications based on what has changed after a pjob_store.
421 ****************************************************************************/
425 uint32 spoolss_status;
426 } lpq_to_spoolss_status_map[] = {
427 { LPQ_QUEUED, JOB_STATUS_QUEUED },
428 { LPQ_PAUSED, JOB_STATUS_PAUSED },
429 { LPQ_SPOOLING, JOB_STATUS_SPOOLING },
430 { LPQ_PRINTING, JOB_STATUS_PRINTING },
431 { LPQ_DELETING, JOB_STATUS_DELETING },
432 { LPQ_OFFLINE, JOB_STATUS_OFFLINE },
433 { LPQ_PAPEROUT, JOB_STATUS_PAPEROUT },
434 { LPQ_PRINTED, JOB_STATUS_PRINTED },
435 { LPQ_DELETED, JOB_STATUS_DELETED },
436 { LPQ_BLOCKED, JOB_STATUS_BLOCKED },
437 { LPQ_USER_INTERVENTION, JOB_STATUS_USER_INTERVENTION },
441 /* Convert a lpq status value stored in printing.tdb into the
442 appropriate win32 API constant. */
444 static uint32 map_to_spoolss_status(uint32 lpq_status)
448 while (lpq_to_spoolss_status_map[i].lpq_status != -1) {
449 if (lpq_to_spoolss_status_map[i].lpq_status == lpq_status)
450 return lpq_to_spoolss_status_map[i].spoolss_status;
457 static void pjob_store_notify(const char* sharename, uint32 jobid, struct printjob *old_data,
458 struct printjob *new_data)
460 BOOL new_job = False;
465 /* Job attributes that can't be changed. We only send
466 notification for these on a new job. */
468 /* ACHTUNG! Due to a bug in Samba's spoolss parsing of the
469 NOTIFY_INFO_DATA buffer, we *have* to send the job submission
470 time first or else we'll end up with potential alignment
471 errors. I don't think the systemtime should be spooled as
472 a string, but this gets us around that error.
473 --jerry (i'll feel dirty for this) */
476 notify_job_submitted(sharename, jobid, new_data->starttime);
477 notify_job_username(sharename, jobid, new_data->user);
480 if (new_job || !strequal(old_data->jobname, new_data->jobname))
481 notify_job_name(sharename, jobid, new_data->jobname);
483 /* Job attributes of a new job or attributes that can be
486 if (new_job || !strequal(old_data->jobname, new_data->jobname))
487 notify_job_name(sharename, jobid, new_data->jobname);
489 if (new_job || old_data->status != new_data->status)
490 notify_job_status(sharename, jobid, map_to_spoolss_status(new_data->status));
492 if (new_job || old_data->size != new_data->size)
493 notify_job_total_bytes(sharename, jobid, new_data->size);
495 if (new_job || old_data->page_count != new_data->page_count)
496 notify_job_total_pages(sharename, jobid, new_data->page_count);
499 /****************************************************************************
500 Store a job structure back to the database.
501 ****************************************************************************/
503 static BOOL pjob_store(const char* sharename, uint32 jobid, struct printjob *pjob)
505 TDB_DATA old_data, new_data;
507 struct tdb_print_db *pdb = get_print_db_byname(sharename);
509 int len, newlen, buflen;
517 old_data = tdb_fetch(pdb->tdb, print_key(jobid));
519 /* Doh! Now we have to pack/unpack data since the NT_DEVICEMODE was added */
526 len += tdb_pack(buf+len, buflen-len, "dddddddddffff",
528 (uint32)pjob->sysjob,
530 (uint32)pjob->starttime,
531 (uint32)pjob->status,
533 (uint32)pjob->page_count,
534 (uint32)pjob->spooled,
535 (uint32)pjob->smbjob,
541 len += pack_devicemode(pjob->nt_devmode, buf+len, buflen-len);
546 tb = (char *)SMB_REALLOC(buf, len);
548 DEBUG(0,("pjob_store: failed to enlarge buffer!\n"));
555 } while ( buflen != len );
561 new_data.dsize = len;
562 ret = (tdb_store(pdb->tdb, print_key(jobid), new_data, TDB_REPLACE) == 0);
564 release_print_db(pdb);
566 /* Send notify updates for what has changed */
569 struct printjob old_pjob;
571 if ( old_data.dsize )
573 if ( unpack_pjob( old_data.dptr, old_data.dsize, &old_pjob ) != -1 )
575 pjob_store_notify( sharename, jobid, &old_pjob , pjob );
576 free_nt_devicemode( &old_pjob.nt_devmode );
581 pjob_store_notify( sharename, jobid, NULL, pjob );
586 SAFE_FREE( old_data.dptr );
592 /****************************************************************************
593 Remove a job structure from the database.
594 ****************************************************************************/
596 void pjob_delete(const char* sharename, uint32 jobid)
598 struct printjob *pjob;
599 uint32 job_status = 0;
600 struct tdb_print_db *pdb;
602 pdb = get_print_db_byname( sharename );
607 pjob = print_job_find( sharename, jobid );
610 DEBUG(5, ("pjob_delete: we were asked to delete nonexistent job %u\n",
611 (unsigned int)jobid));
612 release_print_db(pdb);
616 /* We must cycle through JOB_STATUS_DELETING and
617 JOB_STATUS_DELETED for the port monitor to delete the job
620 job_status = JOB_STATUS_DELETING|JOB_STATUS_DELETED;
621 notify_job_status(sharename, jobid, job_status);
623 /* Remove from printing.tdb */
625 tdb_delete(pdb->tdb, print_key(jobid));
626 remove_from_jobs_changed(sharename, jobid);
627 release_print_db( pdb );
628 rap_jobid_delete(sharename, jobid);
631 /****************************************************************************
632 Parse a file name from the system spooler to generate a jobid.
633 ****************************************************************************/
635 static uint32 print_parse_jobid(char *fname)
639 if (strncmp(fname,PRINT_SPOOL_PREFIX,strlen(PRINT_SPOOL_PREFIX)) != 0)
641 fname += strlen(PRINT_SPOOL_PREFIX);
647 return (uint32)jobid;
650 /****************************************************************************
651 List a unix job in the print database.
652 ****************************************************************************/
654 static void print_unix_job(const char *sharename, print_queue_struct *q, uint32 jobid)
656 struct printjob pj, *old_pj;
658 if (jobid == (uint32)-1)
659 jobid = q->job + UNIX_JOB_START;
661 /* Preserve the timestamp on an existing unix print job */
663 old_pj = print_job_find(sharename, jobid);
670 pj.starttime = old_pj ? old_pj->starttime : q->time;
671 pj.status = q->status;
674 fstrcpy(pj.filename, old_pj ? old_pj->filename : "");
675 if (jobid < UNIX_JOB_START) {
677 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : "Remote Downlevel Document");
680 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : q->fs_file);
682 fstrcpy(pj.user, old_pj ? old_pj->user : q->fs_user);
683 fstrcpy(pj.queuename, old_pj ? old_pj->queuename : sharename );
685 pjob_store(sharename, jobid, &pj);
689 struct traverse_struct {
690 print_queue_struct *queue;
691 int qcount, snum, maxcount, total_jobs;
692 const char *sharename;
696 /****************************************************************************
697 Utility fn to delete any jobs that are no longer active.
698 ****************************************************************************/
700 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
702 struct traverse_struct *ts = (struct traverse_struct *)state;
703 struct printjob pjob;
707 if ( key.dsize != sizeof(jobid) )
710 jobid = IVAL(key.dptr, 0);
711 if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
713 free_nt_devicemode( &pjob.nt_devmode );
717 /* remove a unix job if it isn't in the system queue any more */
719 for (i=0;i<ts->qcount;i++) {
720 uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
721 if (jobid == u_jobid)
724 if (i == ts->qcount) {
725 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !smbjob\n",
726 (unsigned int)jobid ));
727 pjob_delete(ts->sharename, jobid);
731 /* need to continue the the bottom of the function to
732 save the correct attributes */
735 /* maybe it hasn't been spooled yet */
737 /* if a job is not spooled and the process doesn't
738 exist then kill it. This cleans up after smbd
740 if (!process_exists(pjob.pid)) {
741 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !process_exists (%u)\n",
742 (unsigned int)jobid, (unsigned int)pjob.pid ));
743 pjob_delete(ts->sharename, jobid);
749 /* this check only makes sense for jobs submitted from Windows clients */
752 for (i=0;i<ts->qcount;i++) {
753 uint32 curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
754 if (jobid == curr_jobid)
759 /* The job isn't in the system queue - we have to assume it has
760 completed, so delete the database entry. */
762 if (i == ts->qcount) {
764 /* A race can occur between the time a job is spooled and
765 when it appears in the lpq output. This happens when
766 the job is added to printing.tdb when another smbd
767 running print_queue_update() has completed a lpq and
768 is currently traversing the printing tdb and deleting jobs.
769 Don't delete the job if it was submitted after the lpq_time. */
771 if (pjob.starttime < ts->lpq_time) {
772 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to pjob.starttime (%u) < ts->lpq_time (%u)\n",
774 (unsigned int)pjob.starttime,
775 (unsigned int)ts->lpq_time ));
776 pjob_delete(ts->sharename, jobid);
782 /* Save the pjob attributes we will store. */
783 /* FIXME!!! This is the only place where queue->job
784 represents the SMB jobid --jerry */
785 ts->queue[i].job = jobid;
786 ts->queue[i].size = pjob.size;
787 ts->queue[i].page_count = pjob.page_count;
788 ts->queue[i].status = pjob.status;
789 ts->queue[i].priority = 1;
790 ts->queue[i].time = pjob.starttime;
791 fstrcpy(ts->queue[i].fs_user, pjob.user);
792 fstrcpy(ts->queue[i].fs_file, pjob.jobname);
799 /****************************************************************************
800 Check if the print queue has been updated recently enough.
801 ****************************************************************************/
803 static void print_cache_flush(int snum)
806 const char *sharename = lp_const_servicename(snum);
807 struct tdb_print_db *pdb = get_print_db_byname(sharename);
811 slprintf(key, sizeof(key)-1, "CACHE/%s", sharename);
812 tdb_store_int32(pdb->tdb, key, -1);
813 release_print_db(pdb);
816 /****************************************************************************
817 Check if someone already thinks they are doing the update.
818 ****************************************************************************/
820 static pid_t get_updating_pid(const char *sharename)
825 struct tdb_print_db *pdb = get_print_db_byname(sharename);
829 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", sharename);
831 key.dsize = strlen(keystr);
833 data = tdb_fetch(pdb->tdb, key);
834 release_print_db(pdb);
835 if (!data.dptr || data.dsize != sizeof(pid_t)) {
836 SAFE_FREE(data.dptr);
840 updating_pid = IVAL(data.dptr, 0);
841 SAFE_FREE(data.dptr);
843 if (process_exists(updating_pid))
849 /****************************************************************************
850 Set the fact that we're doing the update, or have finished doing the update
852 ****************************************************************************/
854 static void set_updating_pid(const fstring sharename, BOOL updating)
859 pid_t updating_pid = sys_getpid();
862 struct tdb_print_db *pdb = get_print_db_byname(sharename);
867 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", sharename);
869 key.dsize = strlen(keystr);
871 DEBUG(5, ("set_updating_pid: %s updating lpq cache for print share %s\n",
872 updating ? "" : "not ",
876 tdb_delete(pdb->tdb, key);
877 release_print_db(pdb);
881 SIVAL( buffer, 0, updating_pid);
882 data.dptr = (void *)buffer;
883 data.dsize = 4; /* we always assume this is a 4 byte value */
885 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
886 release_print_db(pdb);
889 /****************************************************************************
890 Sort print jobs by submittal time.
891 ****************************************************************************/
893 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
904 /* Sort on job start time */
906 if (j1->time == j2->time)
908 return (j1->time > j2->time) ? 1 : -1;
911 /****************************************************************************
912 Store the sorted queue representation for later portmon retrieval.
913 ****************************************************************************/
915 static void store_queue_struct(struct tdb_print_db *pdb, struct traverse_struct *pts)
918 int max_reported_jobs = lp_max_reported_jobs(pts->snum);
919 print_queue_struct *queue = pts->queue;
924 if (max_reported_jobs && (max_reported_jobs < pts->qcount))
925 pts->qcount = max_reported_jobs;
926 qcount = pts->qcount;
928 /* Work out the size. */
930 data.dsize += tdb_pack(NULL, 0, "d", qcount);
932 for (i = 0; i < pts->qcount; i++) {
933 data.dsize += tdb_pack(NULL, 0, "ddddddff",
934 (uint32)queue[i].job,
935 (uint32)queue[i].size,
936 (uint32)queue[i].page_count,
937 (uint32)queue[i].status,
938 (uint32)queue[i].priority,
939 (uint32)queue[i].time,
944 if ((data.dptr = SMB_MALLOC(data.dsize)) == NULL)
948 len += tdb_pack(data.dptr + len, data.dsize - len, "d", qcount);
949 for (i = 0; i < pts->qcount; i++) {
950 len += tdb_pack(data.dptr + len, data.dsize - len, "ddddddff",
951 (uint32)queue[i].job,
952 (uint32)queue[i].size,
953 (uint32)queue[i].page_count,
954 (uint32)queue[i].status,
955 (uint32)queue[i].priority,
956 (uint32)queue[i].time,
961 tdb_store(pdb->tdb, string_tdb_data("INFO/linear_queue_array"), data,
963 SAFE_FREE(data.dptr);
967 static TDB_DATA get_jobs_changed_data(struct tdb_print_db *pdb)
973 data = tdb_fetch(pdb->tdb, string_tdb_data("INFO/jobs_changed"));
974 if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0)) {
975 SAFE_FREE(data.dptr);
982 static void check_job_changed(const char *sharename, TDB_DATA data, uint32 jobid)
985 unsigned int job_count = data.dsize / 4;
987 for (i = 0; i < job_count; i++) {
990 ch_jobid = IVAL(data.dptr, i*4);
991 if (ch_jobid == jobid)
992 remove_from_jobs_changed(sharename, jobid);
996 /****************************************************************************
997 Check if the print queue has been updated recently enough.
998 ****************************************************************************/
1000 static BOOL print_cache_expired(const char *sharename, BOOL check_pending)
1003 time_t last_qscan_time, time_now = time(NULL);
1004 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1005 BOOL result = False;
1010 snprintf(key, sizeof(key), "CACHE/%s", sharename);
1011 last_qscan_time = (time_t)tdb_fetch_int32(pdb->tdb, key);
1014 * Invalidate the queue for 3 reasons.
1015 * (1). last queue scan time == -1.
1016 * (2). Current time - last queue scan time > allowed cache time.
1017 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1018 * This last test picks up machines for which the clock has been moved
1019 * forward, an lpq scan done and then the clock moved back. Otherwise
1020 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1023 if (last_qscan_time == ((time_t)-1)
1024 || (time_now - last_qscan_time) >= lp_lpqcachetime()
1025 || last_qscan_time > (time_now + MAX_CACHE_VALID_TIME))
1027 time_t msg_pending_time;
1029 DEBUG(4, ("print_cache_expired: cache expired for queue %s "
1030 "(last_qscan_time = %d, time now = %d, qcachetime = %d)\n",
1031 sharename, (int)last_qscan_time, (int)time_now,
1032 (int)lp_lpqcachetime() ));
1034 /* check if another smbd has already sent a message to update the
1035 queue. Give the pending message one minute to clear and
1036 then send another message anyways. Make sure to check for
1037 clocks that have been run forward and then back again. */
1039 snprintf(key, sizeof(key), "MSG_PENDING/%s", sharename);
1042 && tdb_fetch_uint32( pdb->tdb, key, (uint32*)&msg_pending_time )
1043 && msg_pending_time > 0
1044 && msg_pending_time <= time_now
1045 && (time_now - msg_pending_time) < 60 )
1047 DEBUG(4,("print_cache_expired: message already pending for %s. Accepting cache\n",
1056 release_print_db(pdb);
1060 /****************************************************************************
1061 main work for updating the lpq cahe for a printer queue
1062 ****************************************************************************/
1064 static void print_queue_update_internal( const char *sharename,
1065 struct printif *current_printif,
1069 print_queue_struct *queue = NULL;
1070 print_status_struct status;
1071 print_status_struct old_status;
1072 struct printjob *pjob;
1073 struct traverse_struct tstruct;
1076 fstring keystr, cachestr;
1077 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1083 DEBUG(5,("print_queue_update_internal: printer = %s, type = %d, lpq command = [%s]\n",
1084 sharename, current_printif->type, lpq_command));
1087 * Update the cache time FIRST ! Stops others even
1088 * attempting to get the lock and doing this
1089 * if the lpq takes a long time.
1092 slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", sharename);
1093 tdb_store_int32(pdb->tdb, cachestr, (int)time(NULL));
1095 /* get the current queue using the appropriate interface */
1096 ZERO_STRUCT(status);
1098 qcount = (*(current_printif->queue_get))(sharename,
1099 current_printif->type,
1100 lpq_command, &queue, &status);
1102 DEBUG(3, ("print_queue_update_internal: %d job%s in queue for %s\n",
1103 qcount, (qcount != 1) ? "s" : "", sharename));
1105 /* Sort the queue by submission time otherwise they are displayed
1108 qsort(queue, qcount, sizeof(print_queue_struct),
1109 QSORT_CAST(printjob_comp));
1112 any job in the internal database that is marked as spooled
1113 and doesn't exist in the system queue is considered finished
1114 and removed from the database
1116 any job in the system database but not in the internal database
1117 is added as a unix job
1119 fill in any system job numbers as we go
1122 jcdata = get_jobs_changed_data(pdb);
1124 for (i=0; i<qcount; i++) {
1125 uint32 jobid = print_parse_jobid(queue[i].fs_file);
1127 if (jobid == (uint32)-1) {
1128 /* assume its a unix print job */
1129 print_unix_job(sharename, &queue[i], jobid);
1133 /* we have an active SMB print job - update its status */
1134 pjob = print_job_find(sharename, jobid);
1136 /* err, somethings wrong. Probably smbd was restarted
1137 with jobs in the queue. All we can do is treat them
1138 like unix jobs. Pity. */
1139 print_unix_job(sharename, &queue[i], jobid);
1143 pjob->sysjob = queue[i].job;
1144 pjob->status = queue[i].status;
1145 pjob_store(sharename, jobid, pjob);
1146 check_job_changed(sharename, jcdata, jobid);
1149 SAFE_FREE(jcdata.dptr);
1151 /* now delete any queued entries that don't appear in the
1153 tstruct.queue = queue;
1154 tstruct.qcount = qcount;
1156 tstruct.total_jobs = 0;
1157 tstruct.lpq_time = time(NULL);
1158 tstruct.sharename = sharename;
1160 tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
1162 /* Store the linearised queue, max jobs only. */
1163 store_queue_struct(pdb, &tstruct);
1165 SAFE_FREE(tstruct.queue);
1167 DEBUG(10,("print_queue_update_internal: printer %s INFO/total_jobs = %d\n",
1168 sharename, tstruct.total_jobs ));
1170 tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
1172 get_queue_status(sharename, &old_status);
1173 if (old_status.qcount != qcount)
1174 DEBUG(10,("print_queue_update_internal: queue status change %d jobs -> %d jobs for printer %s\n",
1175 old_status.qcount, qcount, sharename));
1177 /* store the new queue status structure */
1178 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", sharename);
1180 key.dsize = strlen(keystr);
1182 status.qcount = qcount;
1183 data.dptr = (void *)&status;
1184 data.dsize = sizeof(status);
1185 tdb_store(pdb->tdb, key, data, TDB_REPLACE);
1188 * Update the cache time again. We want to do this call
1189 * as little as possible...
1192 slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", sharename);
1193 tdb_store_int32(pdb->tdb, keystr, (int32)time(NULL));
1195 /* clear the msg pending record for this queue */
1197 snprintf(keystr, sizeof(keystr), "MSG_PENDING/%s", sharename);
1199 if ( !tdb_store_uint32( pdb->tdb, keystr, 0 ) ) {
1200 /* log a message but continue on */
1202 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1206 release_print_db( pdb );
1211 /****************************************************************************
1212 Update the internal database from the system print queue for a queue.
1213 obtain a lock on the print queue before proceeding (needed when mutiple
1214 smbd processes maytry to update the lpq cache concurrently).
1215 ****************************************************************************/
1217 static void print_queue_update_with_lock( const char *sharename,
1218 struct printif *current_printif,
1222 struct tdb_print_db *pdb;
1224 DEBUG(5,("print_queue_update_with_lock: printer share = %s\n", sharename));
1225 pdb = get_print_db_byname(sharename);
1229 if ( !print_cache_expired(sharename, False) ) {
1230 DEBUG(5,("print_queue_update_with_lock: print cache for %s is still ok\n", sharename));
1231 release_print_db(pdb);
1236 * Check to see if someone else is doing this update.
1237 * This is essentially a mutex on the update.
1240 if (get_updating_pid(sharename) != -1) {
1241 release_print_db(pdb);
1245 /* Lock the queue for the database update */
1247 slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", sharename);
1248 /* Only wait 10 seconds for this. */
1249 if (tdb_lock_bystring(pdb->tdb, keystr, 10) == -1) {
1250 DEBUG(0,("print_queue_update_with_lock: Failed to lock printer %s database\n", sharename));
1251 release_print_db(pdb);
1256 * Ensure that no one else got in here.
1257 * If the updating pid is still -1 then we are
1261 if (get_updating_pid(sharename) != -1) {
1263 * Someone else is doing the update, exit.
1265 tdb_unlock_bystring(pdb->tdb, keystr);
1266 release_print_db(pdb);
1271 * We're going to do the update ourselves.
1274 /* Tell others we're doing the update. */
1275 set_updating_pid(sharename, True);
1278 * Allow others to enter and notice we're doing
1282 tdb_unlock_bystring(pdb->tdb, keystr);
1284 /* do the main work now */
1286 print_queue_update_internal( sharename, current_printif, lpq_command );
1288 /* Delete our pid from the db. */
1289 set_updating_pid(sharename, False);
1290 release_print_db(pdb);
1293 /****************************************************************************
1294 this is the receive function of the background lpq updater
1295 ****************************************************************************/
1296 static void print_queue_receive(int msg_type, pid_t src, void *buf, size_t msglen)
1303 len = tdb_unpack( buf, msglen, "fdP",
1309 DEBUG(0,("print_queue_receive: Got invalid print queue update message\n"));
1313 print_queue_update_with_lock(sharename,
1314 get_printer_fns_from_type(printing_type),
1320 static pid_t background_lpq_updater_pid = -1;
1322 /****************************************************************************
1323 main thread of the background lpq updater
1324 ****************************************************************************/
1325 void start_background_queue(void)
1327 DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
1328 background_lpq_updater_pid = sys_fork();
1330 if (background_lpq_updater_pid == -1) {
1331 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
1335 if(background_lpq_updater_pid == 0) {
1337 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
1339 claim_connection( NULL, "smbd lpq backend", 0, False,
1340 FLAG_MSG_GENERAL|FLAG_MSG_SMBD|FLAG_MSG_PRINT_GENERAL);
1342 if (!locking_init(0)) {
1346 message_register(MSG_PRINTER_UPDATE, print_queue_receive);
1348 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
1352 /* check for some essential signals first */
1355 exit_server("Caught TERM signal");
1358 if (reload_after_sighup) {
1359 change_to_root_user();
1360 DEBUG(1,("Reloading services after SIGHUP\n"));
1361 reload_services(False);
1362 reload_after_sighup = 0;
1365 /* now check for messages */
1367 DEBUG(10,("start_background_queue: background LPQ thread got a message\n"));
1370 /* process any pending print change notify messages */
1372 print_notify_send_messages(0);
1377 /****************************************************************************
1378 update the internal database from the system print queue for a queue
1379 ****************************************************************************/
1381 static void print_queue_update(int snum, BOOL force)
1386 char *buffer = NULL;
1389 struct tdb_print_db *pdb;
1391 struct printif *current_printif;
1393 fstrcpy( sharename, lp_const_servicename(snum));
1395 /* don't strip out characters like '$' from the printername */
1397 pstrcpy( lpqcommand, lp_lpqcommand(snum));
1398 string_sub2( lpqcommand, "%p", PRINTERNAME(snum), sizeof(lpqcommand), False, False );
1399 standard_sub_snum( snum, lpqcommand, sizeof(lpqcommand) );
1402 * Make sure that the background queue process exists.
1403 * Otherwise just do the update ourselves
1406 if ( force || background_lpq_updater_pid == -1 ) {
1407 DEBUG(4,("print_queue_update: updating queue [%s] myself\n", sharename));
1408 current_printif = get_printer_fns( snum );
1409 print_queue_update_with_lock( sharename, current_printif, lpqcommand );
1414 type = lp_printing(snum);
1416 /* get the length */
1418 len = tdb_pack( buffer, len, "fdP",
1423 buffer = SMB_XMALLOC_ARRAY( char, len );
1425 /* now pack the buffer */
1426 newlen = tdb_pack( buffer, len, "fdP",
1431 SMB_ASSERT( newlen == len );
1433 DEBUG(10,("print_queue_update: Sending message -> printer = %s, "
1434 "type = %d, lpq command = [%s]\n", sharename, type, lpqcommand ));
1436 /* here we set a msg pending record for other smbd processes
1437 to throttle the number of duplicate print_queue_update msgs
1440 pdb = get_print_db_byname(sharename);
1446 snprintf(key, sizeof(key), "MSG_PENDING/%s", sharename);
1448 if ( !tdb_store_uint32( pdb->tdb, key, time(NULL) ) ) {
1449 /* log a message but continue on */
1451 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1455 release_print_db( pdb );
1457 /* finally send the message */
1460 message_send_pid(background_lpq_updater_pid,
1461 MSG_PRINTER_UPDATE, buffer, len, False);
1464 SAFE_FREE( buffer );
1469 /****************************************************************************
1470 Create/Update an entry in the print tdb that will allow us to send notify
1471 updates only to interested smbd's.
1472 ****************************************************************************/
1474 BOOL print_notify_register_pid(int snum)
1477 struct tdb_print_db *pdb = NULL;
1478 TDB_CONTEXT *tdb = NULL;
1479 const char *printername;
1480 uint32 mypid = (uint32)sys_getpid();
1484 /* if (snum == -1), then the change notify request was
1485 on a print server handle and we need to register on
1490 int num_services = lp_numservices();
1493 for ( idx=0; idx<num_services; idx++ ) {
1494 if (lp_snum_ok(idx) && lp_print_ok(idx) )
1495 print_notify_register_pid(idx);
1500 else /* register for a specific printer */
1502 printername = lp_const_servicename(snum);
1503 pdb = get_print_db_byname(printername);
1509 if (tdb_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1510 DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n",
1513 release_print_db(pdb);
1517 data = get_printer_notify_pid_list( tdb, printername, True );
1519 /* Add ourselves and increase the refcount. */
1521 for (i = 0; i < data.dsize; i += 8) {
1522 if (IVAL(data.dptr,i) == mypid) {
1523 uint32 new_refcount = IVAL(data.dptr, i+4) + 1;
1524 SIVAL(data.dptr, i+4, new_refcount);
1529 if (i == data.dsize) {
1530 /* We weren't in the list. Realloc. */
1531 data.dptr = SMB_REALLOC(data.dptr, data.dsize + 8);
1533 DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n",
1538 SIVAL(data.dptr,data.dsize - 8,mypid);
1539 SIVAL(data.dptr,data.dsize - 4,1); /* Refcount. */
1542 /* Store back the record. */
1543 if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1544 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1545 list for printer %s\n", printername));
1553 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1555 release_print_db(pdb);
1556 SAFE_FREE(data.dptr);
1560 /****************************************************************************
1561 Update an entry in the print tdb that will allow us to send notify
1562 updates only to interested smbd's.
1563 ****************************************************************************/
1565 BOOL print_notify_deregister_pid(int snum)
1568 struct tdb_print_db *pdb = NULL;
1569 TDB_CONTEXT *tdb = NULL;
1570 const char *printername;
1571 uint32 mypid = (uint32)sys_getpid();
1575 /* if ( snum == -1 ), we are deregister a print server handle
1576 which means to deregister on all print queues */
1580 int num_services = lp_numservices();
1583 for ( idx=0; idx<num_services; idx++ ) {
1584 if ( lp_snum_ok(idx) && lp_print_ok(idx) )
1585 print_notify_deregister_pid(idx);
1590 else /* deregister a specific printer */
1592 printername = lp_const_servicename(snum);
1593 pdb = get_print_db_byname(printername);
1599 if (tdb_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1600 DEBUG(0,("print_notify_register_pid: Failed to lock \
1601 printer %s database\n", printername));
1603 release_print_db(pdb);
1607 data = get_printer_notify_pid_list( tdb, printername, True );
1609 /* Reduce refcount. Remove ourselves if zero. */
1611 for (i = 0; i < data.dsize; ) {
1612 if (IVAL(data.dptr,i) == mypid) {
1613 uint32 refcount = IVAL(data.dptr, i+4);
1617 if (refcount == 0) {
1618 if (data.dsize - i > 8)
1619 memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
1623 SIVAL(data.dptr, i+4, refcount);
1629 if (data.dsize == 0)
1630 SAFE_FREE(data.dptr);
1632 /* Store back the record. */
1633 if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1634 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1635 list for printer %s\n", printername));
1643 tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1645 release_print_db(pdb);
1646 SAFE_FREE(data.dptr);
1650 /****************************************************************************
1651 Check if a jobid is valid. It is valid if it exists in the database.
1652 ****************************************************************************/
1654 BOOL print_job_exists(const char* sharename, uint32 jobid)
1656 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1661 ret = tdb_exists(pdb->tdb, print_key(jobid));
1662 release_print_db(pdb);
1666 /****************************************************************************
1667 Give the fd used for a jobid.
1668 ****************************************************************************/
1670 int print_job_fd(const char* sharename, uint32 jobid)
1672 struct printjob *pjob = print_job_find(sharename, jobid);
1675 /* don't allow another process to get this info - it is meaningless */
1676 if (pjob->pid != sys_getpid())
1681 /****************************************************************************
1682 Give the filename used for a jobid.
1683 Only valid for the process doing the spooling and when the job
1684 has not been spooled.
1685 ****************************************************************************/
1687 char *print_job_fname(const char* sharename, uint32 jobid)
1689 struct printjob *pjob = print_job_find(sharename, jobid);
1690 if (!pjob || pjob->spooled || pjob->pid != sys_getpid())
1692 return pjob->filename;
1696 /****************************************************************************
1697 Give the filename used for a jobid.
1698 Only valid for the process doing the spooling and when the job
1699 has not been spooled.
1700 ****************************************************************************/
1702 NT_DEVICEMODE *print_job_devmode(const char* sharename, uint32 jobid)
1704 struct printjob *pjob = print_job_find(sharename, jobid);
1709 return pjob->nt_devmode;
1712 /****************************************************************************
1713 Set the place in the queue for a job.
1714 ****************************************************************************/
1716 BOOL print_job_set_place(int snum, uint32 jobid, int place)
1718 DEBUG(2,("print_job_set_place not implemented yet\n"));
1722 /****************************************************************************
1723 Set the name of a job. Only possible for owner.
1724 ****************************************************************************/
1726 BOOL print_job_set_name(int snum, uint32 jobid, char *name)
1728 const char* sharename = lp_const_servicename(snum);
1729 struct printjob *pjob;
1731 pjob = print_job_find(sharename, jobid);
1732 if (!pjob || pjob->pid != sys_getpid())
1735 fstrcpy(pjob->jobname, name);
1736 return pjob_store(sharename, jobid, pjob);
1739 /***************************************************************************
1740 Remove a jobid from the 'jobs changed' list.
1741 ***************************************************************************/
1743 static BOOL remove_from_jobs_changed(const char* sharename, uint32 jobid)
1745 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1747 size_t job_count, i;
1749 BOOL gotlock = False;
1757 key = string_tdb_data("INFO/jobs_changed");
1759 if (tdb_chainlock_with_timeout(pdb->tdb, key, 5) == -1)
1764 data = tdb_fetch(pdb->tdb, key);
1766 if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0))
1769 job_count = data.dsize / 4;
1770 for (i = 0; i < job_count; i++) {
1773 ch_jobid = IVAL(data.dptr, i*4);
1774 if (ch_jobid == jobid) {
1775 if (i < job_count -1 )
1776 memmove(data.dptr + (i*4), data.dptr + (i*4) + 4, (job_count - i - 1)*4 );
1778 if (tdb_store(pdb->tdb, key, data, TDB_REPLACE) == -1)
1788 tdb_chainunlock(pdb->tdb, key);
1789 SAFE_FREE(data.dptr);
1790 release_print_db(pdb);
1792 DEBUG(10,("remove_from_jobs_changed: removed jobid %u\n", (unsigned int)jobid ));
1794 DEBUG(10,("remove_from_jobs_changed: Failed to remove jobid %u\n", (unsigned int)jobid ));
1798 /****************************************************************************
1799 Delete a print job - don't update queue.
1800 ****************************************************************************/
1802 static BOOL print_job_delete1(int snum, uint32 jobid)
1804 const char* sharename = lp_const_servicename(snum);
1805 struct printjob *pjob = print_job_find(sharename, jobid);
1807 struct printif *current_printif = get_printer_fns( snum );
1809 pjob = print_job_find(sharename, jobid);
1815 * If already deleting just return.
1818 if (pjob->status == LPQ_DELETING)
1821 /* Hrm - we need to be able to cope with deleting a job before it
1822 has reached the spooler. */
1824 if (pjob->sysjob == -1) {
1825 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid));
1828 /* Set the tdb entry to be deleting. */
1830 pjob->status = LPQ_DELETING;
1831 pjob_store(sharename, jobid, pjob);
1833 if (pjob->spooled && pjob->sysjob != -1)
1834 result = (*(current_printif->job_delete))(snum, pjob);
1836 /* Delete the tdb entry if the delete succeeded or the job hasn't
1840 struct tdb_print_db *pdb = get_print_db_byname(sharename);
1845 pjob_delete(sharename, jobid);
1846 /* Ensure we keep a rough count of the number of total jobs... */
1847 tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, -1);
1848 release_print_db(pdb);
1851 return (result == 0);
1854 /****************************************************************************
1855 Return true if the current user owns the print job.
1856 ****************************************************************************/
1858 static BOOL is_owner(struct current_user *user, int snum, uint32 jobid)
1860 struct printjob *pjob = print_job_find(lp_const_servicename(snum), jobid);
1866 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1867 return strequal(pjob->user, vuser->user.smb_name);
1869 return strequal(pjob->user, uidtoname(user->uid));
1873 /****************************************************************************
1875 ****************************************************************************/
1877 BOOL print_job_delete(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1879 const char* sharename = lp_const_servicename( snum );
1880 BOOL owner, deleted;
1885 owner = is_owner(user, snum, jobid);
1887 /* Check access against security descriptor or whether the user
1891 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1892 DEBUG(3, ("delete denied by security descriptor\n"));
1893 *errcode = WERR_ACCESS_DENIED;
1895 /* BEGIN_ADMIN_LOG */
1896 sys_adminlog( LOG_ERR,
1897 "Permission denied-- user not allowed to delete, \
1898 pause, or resume print job. User name: %s. Printer name: %s.",
1899 uidtoname(user->uid), PRINTERNAME(snum) );
1906 * get the spooled filename of the print job
1907 * if this works, then the file has not been spooled
1908 * to the underlying print system. Just delete the
1909 * spool file & return.
1912 if ( (fname = print_job_fname( sharename, jobid )) != NULL )
1914 /* remove the spool file */
1915 DEBUG(10,("print_job_delete: Removing spool file [%s]\n", fname ));
1916 if ( unlink( fname ) == -1 ) {
1917 *errcode = map_werror_from_unix(errno);
1922 if (!print_job_delete1(snum, jobid)) {
1923 *errcode = WERR_ACCESS_DENIED;
1927 /* force update the database and say the delete failed if the
1930 print_queue_update(snum, True);
1932 deleted = !print_job_exists(sharename, jobid);
1934 *errcode = WERR_ACCESS_DENIED;
1939 /****************************************************************************
1941 ****************************************************************************/
1943 BOOL print_job_pause(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1945 const char* sharename = lp_const_servicename(snum);
1946 struct printjob *pjob;
1948 struct printif *current_printif = get_printer_fns( snum );
1950 pjob = print_job_find(sharename, jobid);
1955 if (!pjob->spooled || pjob->sysjob == -1)
1958 if (!is_owner(user, snum, jobid) &&
1959 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
1960 DEBUG(3, ("pause denied by security descriptor\n"));
1962 /* BEGIN_ADMIN_LOG */
1963 sys_adminlog( LOG_ERR,
1964 "Permission denied-- user not allowed to delete, \
1965 pause, or resume print job. User name: %s. Printer name: %s.",
1966 uidtoname(user->uid), PRINTERNAME(snum) );
1969 *errcode = WERR_ACCESS_DENIED;
1973 /* need to pause the spooled entry */
1974 ret = (*(current_printif->job_pause))(snum, pjob);
1977 *errcode = WERR_INVALID_PARAM;
1981 /* force update the database */
1982 print_cache_flush(snum);
1984 /* Send a printer notify message */
1986 notify_job_status(sharename, jobid, JOB_STATUS_PAUSED);
1988 /* how do we tell if this succeeded? */
1993 /****************************************************************************
1995 ****************************************************************************/
1997 BOOL print_job_resume(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
1999 const char *sharename = lp_const_servicename(snum);
2000 struct printjob *pjob;
2002 struct printif *current_printif = get_printer_fns( snum );
2004 pjob = print_job_find(sharename, jobid);
2009 if (!pjob->spooled || pjob->sysjob == -1)
2012 if (!is_owner(user, snum, jobid) &&
2013 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
2014 DEBUG(3, ("resume denied by security descriptor\n"));
2015 *errcode = WERR_ACCESS_DENIED;
2017 /* BEGIN_ADMIN_LOG */
2018 sys_adminlog( LOG_ERR,
2019 "Permission denied-- user not allowed to delete, \
2020 pause, or resume print job. User name: %s. Printer name: %s.",
2021 uidtoname(user->uid), PRINTERNAME(snum) );
2026 ret = (*(current_printif->job_resume))(snum, pjob);
2029 *errcode = WERR_INVALID_PARAM;
2033 /* force update the database */
2034 print_cache_flush(snum);
2036 /* Send a printer notify message */
2038 notify_job_status(sharename, jobid, JOB_STATUS_QUEUED);
2043 /****************************************************************************
2044 Write to a print file.
2045 ****************************************************************************/
2047 ssize_t print_job_write(int snum, uint32 jobid, const char *buf, SMB_OFF_T pos, size_t size)
2049 const char* sharename = lp_const_servicename(snum);
2051 struct printjob *pjob;
2053 pjob = print_job_find(sharename, jobid);
2057 /* don't allow another process to get this info - it is meaningless */
2058 if (pjob->pid != sys_getpid())
2061 return_code = write_data_at_offset(pjob->fd, buf, size, pos);
2063 if (return_code>0) {
2065 pjob_store(sharename, jobid, pjob);
2070 /****************************************************************************
2071 Get the queue status - do not update if db is out of date.
2072 ****************************************************************************/
2074 static int get_queue_status(const char* sharename, print_status_struct *status)
2078 struct tdb_print_db *pdb = get_print_db_byname(sharename);
2082 ZERO_STRUCTP(status);
2089 fstr_sprintf(keystr, "STATUS/%s", sharename);
2090 data = tdb_fetch(pdb->tdb, string_tdb_data(keystr));
2092 if (data.dsize == sizeof(print_status_struct))
2093 /* this memcpy is ok since the status struct was
2094 not packed before storing it in the tdb */
2095 memcpy(status, data.dptr, sizeof(print_status_struct));
2096 SAFE_FREE(data.dptr);
2099 len = tdb_fetch_int32(pdb->tdb, "INFO/total_jobs");
2100 release_print_db(pdb);
2101 return (len == -1 ? 0 : len);
2104 /****************************************************************************
2105 Determine the number of jobs in a queue.
2106 ****************************************************************************/
2108 int print_queue_length(int snum, print_status_struct *pstatus)
2110 const char* sharename = lp_const_servicename( snum );
2111 print_status_struct status;
2114 ZERO_STRUCT( status );
2116 /* make sure the database is up to date */
2117 if (print_cache_expired(lp_const_servicename(snum), True))
2118 print_queue_update(snum, False);
2120 /* also fetch the queue status */
2121 memset(&status, 0, sizeof(status));
2122 len = get_queue_status(sharename, &status);
2130 /***************************************************************************
2131 Allocate a jobid. Hold the lock for as short a time as possible.
2132 ***************************************************************************/
2134 static BOOL allocate_print_jobid(struct tdb_print_db *pdb, int snum, const char *sharename, uint32 *pjobid)
2139 *pjobid = (uint32)-1;
2141 for (i = 0; i < 3; i++) {
2142 /* Lock the database - only wait 20 seconds. */
2143 if (tdb_lock_bystring(pdb->tdb, "INFO/nextjob", 20) == -1) {
2144 DEBUG(0,("allocate_print_jobid: failed to lock printing database %s\n", sharename));
2148 if (!tdb_fetch_uint32(pdb->tdb, "INFO/nextjob", &jobid)) {
2149 if (tdb_error(pdb->tdb) != TDB_ERR_NOEXIST) {
2150 DEBUG(0, ("allocate_print_jobid: failed to fetch INFO/nextjob for print queue %s\n",
2157 jobid = NEXT_JOBID(jobid);
2159 if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
2160 DEBUG(3, ("allocate_print_jobid: failed to store INFO/nextjob.\n"));
2161 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
2165 /* We've finished with the INFO/nextjob lock. */
2166 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
2168 if (!print_job_exists(sharename, jobid))
2173 DEBUG(0, ("allocate_print_jobid: failed to allocate a print job for queue %s\n",
2175 /* Probably full... */
2180 /* Store a dummy placeholder. */
2185 if (tdb_store(pdb->tdb, print_key(jobid), dum, TDB_INSERT) == -1) {
2186 DEBUG(3, ("allocate_print_jobid: jobid (%d) failed to store placeholder.\n",
2196 /***************************************************************************
2197 Append a jobid to the 'jobs changed' list.
2198 ***************************************************************************/
2200 static BOOL add_to_jobs_changed(struct tdb_print_db *pdb, uint32 jobid)
2205 SIVAL(&store_jobid, 0, jobid);
2206 data.dptr = (char *)&store_jobid;
2209 DEBUG(10,("add_to_jobs_changed: Added jobid %u\n", (unsigned int)jobid ));
2211 return (tdb_append(pdb->tdb, string_tdb_data("INFO/jobs_changed"),
2215 /***************************************************************************
2216 Start spooling a job - return the jobid.
2217 ***************************************************************************/
2219 uint32 print_job_start(struct current_user *user, int snum, char *jobname, NT_DEVICEMODE *nt_devmode )
2223 struct printjob pjob;
2225 const char *sharename = lp_const_servicename(snum);
2226 struct tdb_print_db *pdb = get_print_db_byname(sharename);
2234 if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
2235 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
2236 release_print_db(pdb);
2240 if (!print_time_access_check(snum)) {
2241 DEBUG(3, ("print_job_start: job start denied by time check\n"));
2242 release_print_db(pdb);
2246 path = lp_pathname(snum);
2248 /* see if we have sufficient disk space */
2249 if (lp_minprintspace(snum)) {
2250 SMB_BIG_UINT dspace, dsize;
2251 if (sys_fsusage(path, &dspace, &dsize) == 0 &&
2252 dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
2253 DEBUG(3, ("print_job_start: disk space check failed.\n"));
2254 release_print_db(pdb);
2260 /* for autoloaded printers, check that the printcap entry still exists */
2261 if (lp_autoloaded(snum) && !pcap_printername_ok(lp_const_servicename(snum))) {
2262 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum) ));
2263 release_print_db(pdb);
2268 /* Insure the maximum queue size is not violated */
2269 if ((njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
2270 DEBUG(3, ("print_job_start: Queue %s number of jobs (%d) larger than max printjobs per queue (%d).\n",
2271 sharename, njobs, lp_maxprintjobs(snum) ));
2272 release_print_db(pdb);
2277 DEBUG(10,("print_job_start: Queue %s number of jobs (%d), max printjobs = %d\n",
2278 sharename, njobs, lp_maxprintjobs(snum) ));
2280 if (!allocate_print_jobid(pdb, snum, sharename, &jobid))
2283 /* create the database entry */
2287 pjob.pid = sys_getpid();
2290 pjob.starttime = time(NULL);
2291 pjob.status = LPQ_SPOOLING;
2293 pjob.spooled = False;
2295 pjob.nt_devmode = nt_devmode;
2297 fstrcpy(pjob.jobname, jobname);
2299 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
2300 fstrcpy(pjob.user, vuser->user.smb_name);
2302 fstrcpy(pjob.user, uidtoname(user->uid));
2305 fstrcpy(pjob.queuename, lp_const_servicename(snum));
2307 /* we have a job entry - now create the spool file */
2308 slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.8u.XXXXXX",
2309 path, PRINT_SPOOL_PREFIX, (unsigned int)jobid);
2310 pjob.fd = smb_mkstemp(pjob.filename);
2312 if (pjob.fd == -1) {
2313 if (errno == EACCES) {
2314 /* Common setup error, force a report. */
2315 DEBUG(0, ("print_job_start: insufficient permissions \
2316 to open spool file %s.\n", pjob.filename));
2318 /* Normal case, report at level 3 and above. */
2319 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
2320 DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
2325 pjob_store(sharename, jobid, &pjob);
2327 /* Update the 'jobs changed' entry used by print_queue_status. */
2328 add_to_jobs_changed(pdb, jobid);
2330 /* Ensure we keep a rough count of the number of total jobs... */
2331 tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, 1);
2333 release_print_db(pdb);
2339 pjob_delete(sharename, jobid);
2341 release_print_db(pdb);
2343 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
2347 /****************************************************************************
2348 Update the number of pages spooled to jobid
2349 ****************************************************************************/
2351 void print_job_endpage(int snum, uint32 jobid)
2353 const char* sharename = lp_const_servicename(snum);
2354 struct printjob *pjob;
2356 pjob = print_job_find(sharename, jobid);
2359 /* don't allow another process to get this info - it is meaningless */
2360 if (pjob->pid != sys_getpid())
2364 pjob_store(sharename, jobid, pjob);
2367 /****************************************************************************
2368 Print a file - called on closing the file. This spools the job.
2369 If normal close is false then we're tearing down the jobs - treat as an
2371 ****************************************************************************/
2373 BOOL print_job_end(int snum, uint32 jobid, BOOL normal_close)
2375 const char* sharename = lp_const_servicename(snum);
2376 struct printjob *pjob;
2378 SMB_STRUCT_STAT sbuf;
2379 struct printif *current_printif = get_printer_fns( snum );
2381 pjob = print_job_find(sharename, jobid);
2386 if (pjob->spooled || pjob->pid != sys_getpid())
2389 if (normal_close && (sys_fstat(pjob->fd, &sbuf) == 0)) {
2390 pjob->size = sbuf.st_size;
2396 * Not a normal close or we couldn't stat the job file,
2397 * so something has gone wrong. Cleanup.
2401 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
2405 /* Technically, this is not quite right. If the printer has a separator
2406 * page turned on, the NT spooler prints the separator page even if the
2407 * print job is 0 bytes. 010215 JRR */
2408 if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
2409 /* don't bother spooling empty files or something being deleted. */
2410 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
2411 pjob->filename, pjob->size ? "deleted" : "zero length" ));
2412 unlink(pjob->filename);
2413 pjob_delete(sharename, jobid);
2417 pjob->smbjob = jobid;
2419 ret = (*(current_printif->job_submit))(snum, pjob);
2424 /* The print job has been sucessfully handed over to the back-end */
2426 pjob->spooled = True;
2427 pjob->status = LPQ_QUEUED;
2428 pjob_store(sharename, jobid, pjob);
2430 /* make sure the database is up to date */
2431 if (print_cache_expired(lp_const_servicename(snum), True))
2432 print_queue_update(snum, False);
2438 /* The print job was not succesfully started. Cleanup */
2439 /* Still need to add proper error return propagation! 010122:JRR */
2440 unlink(pjob->filename);
2441 pjob_delete(sharename, jobid);
2445 /****************************************************************************
2446 Get a snapshot of jobs in the system without traversing.
2447 ****************************************************************************/
2449 static BOOL get_stored_queue_info(struct tdb_print_db *pdb, int snum, int *pcount, print_queue_struct **ppqueue)
2451 TDB_DATA data, cgdata;
2452 print_queue_struct *queue = NULL;
2454 uint32 extra_count = 0;
2455 int total_count = 0;
2458 int max_reported_jobs = lp_max_reported_jobs(snum);
2460 const char* sharename = lp_servicename(snum);
2462 /* make sure the database is up to date */
2463 if (print_cache_expired(lp_const_servicename(snum), True))
2464 print_queue_update(snum, False);
2470 ZERO_STRUCT(cgdata);
2472 /* Get the stored queue data. */
2473 data = tdb_fetch(pdb->tdb, string_tdb_data("INFO/linear_queue_array"));
2475 if (data.dptr && data.dsize >= sizeof(qcount))
2476 len += tdb_unpack(data.dptr + len, data.dsize - len, "d", &qcount);
2478 /* Get the changed jobs list. */
2479 cgdata = tdb_fetch(pdb->tdb, string_tdb_data("INFO/jobs_changed"));
2480 if (cgdata.dptr != NULL && (cgdata.dsize % 4 == 0))
2481 extra_count = cgdata.dsize/4;
2483 DEBUG(5,("get_stored_queue_info: qcount = %u, extra_count = %u\n", (unsigned int)qcount, (unsigned int)extra_count));
2485 /* Allocate the queue size. */
2486 if (qcount == 0 && extra_count == 0)
2489 if ((queue = SMB_MALLOC_ARRAY(print_queue_struct, qcount + extra_count)) == NULL)
2492 /* Retrieve the linearised queue data. */
2494 for( i = 0; i < qcount; i++) {
2495 uint32 qjob, qsize, qpage_count, qstatus, qpriority, qtime;
2496 len += tdb_unpack(data.dptr + len, data.dsize - len, "ddddddff",
2505 queue[i].job = qjob;
2506 queue[i].size = qsize;
2507 queue[i].page_count = qpage_count;
2508 queue[i].status = qstatus;
2509 queue[i].priority = qpriority;
2510 queue[i].time = qtime;
2513 total_count = qcount;
2515 /* Add in the changed jobids. */
2516 for( i = 0; i < extra_count; i++) {
2518 struct printjob *pjob;
2520 jobid = IVAL(cgdata.dptr, i*4);
2521 DEBUG(5,("get_stored_queue_info: changed job = %u\n", (unsigned int)jobid));
2522 pjob = print_job_find(lp_const_servicename(snum), jobid);
2524 DEBUG(5,("get_stored_queue_info: failed to find changed job = %u\n", (unsigned int)jobid));
2525 remove_from_jobs_changed(sharename, jobid);
2529 queue[total_count].job = jobid;
2530 queue[total_count].size = pjob->size;
2531 queue[total_count].page_count = pjob->page_count;
2532 queue[total_count].status = pjob->status;
2533 queue[total_count].priority = 1;
2534 queue[total_count].time = pjob->starttime;
2535 fstrcpy(queue[total_count].fs_user, pjob->user);
2536 fstrcpy(queue[total_count].fs_file, pjob->jobname);
2540 /* Sort the queue by submission time otherwise they are displayed
2543 qsort(queue, total_count, sizeof(print_queue_struct), QSORT_CAST(printjob_comp));
2545 DEBUG(5,("get_stored_queue_info: total_count = %u\n", (unsigned int)total_count));
2547 if (max_reported_jobs && total_count > max_reported_jobs)
2548 total_count = max_reported_jobs;
2551 *pcount = total_count;
2557 SAFE_FREE(data.dptr);
2558 SAFE_FREE(cgdata.dptr);
2562 /****************************************************************************
2563 Get a printer queue listing.
2564 set queue = NULL and status = NULL if you just want to update the cache
2565 ****************************************************************************/
2567 int print_queue_status(int snum,
2568 print_queue_struct **ppqueue,
2569 print_status_struct *status)
2573 const char *sharename;
2574 struct tdb_print_db *pdb;
2577 /* make sure the database is up to date */
2579 if (print_cache_expired(lp_const_servicename(snum), True))
2580 print_queue_update(snum, False);
2582 /* return if we are done */
2583 if ( !ppqueue || !status )
2587 sharename = lp_const_servicename(snum);
2588 pdb = get_print_db_byname(sharename);
2594 * Fetch the queue status. We must do this first, as there may
2595 * be no jobs in the queue.
2598 ZERO_STRUCTP(status);
2599 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", sharename);
2601 key.dsize = strlen(keystr);
2602 data = tdb_fetch(pdb->tdb, key);
2604 if (data.dsize == sizeof(*status)) {
2605 /* this memcpy is ok since the status struct was
2606 not packed before storing it in the tdb */
2607 memcpy(status, data.dptr, sizeof(*status));
2609 SAFE_FREE(data.dptr);
2613 * Now, fetch the print queue information. We first count the number
2614 * of entries, and then only retrieve the queue if necessary.
2617 if (!get_stored_queue_info(pdb, snum, &count, ppqueue)) {
2618 release_print_db(pdb);
2622 release_print_db(pdb);
2626 /****************************************************************************
2628 ****************************************************************************/
2630 BOOL print_queue_pause(struct current_user *user, int snum, WERROR *errcode)
2633 struct printif *current_printif = get_printer_fns( snum );
2635 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2636 *errcode = WERR_ACCESS_DENIED;
2643 ret = (*(current_printif->queue_pause))(snum);
2648 *errcode = WERR_INVALID_PARAM;
2652 /* force update the database */
2653 print_cache_flush(snum);
2655 /* Send a printer notify message */
2657 notify_printer_status(snum, PRINTER_STATUS_PAUSED);
2662 /****************************************************************************
2664 ****************************************************************************/
2666 BOOL print_queue_resume(struct current_user *user, int snum, WERROR *errcode)
2669 struct printif *current_printif = get_printer_fns( snum );
2671 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
2672 *errcode = WERR_ACCESS_DENIED;
2678 ret = (*(current_printif->queue_resume))(snum);
2683 *errcode = WERR_INVALID_PARAM;
2687 /* make sure the database is up to date */
2688 if (print_cache_expired(lp_const_servicename(snum), True))
2689 print_queue_update(snum, True);
2691 /* Send a printer notify message */
2693 notify_printer_status(snum, PRINTER_STATUS_OK);
2698 /****************************************************************************
2699 Purge a queue - implemented by deleting all jobs that we can delete.
2700 ****************************************************************************/
2702 BOOL print_queue_purge(struct current_user *user, int snum, WERROR *errcode)
2704 print_queue_struct *queue;
2705 print_status_struct status;
2709 /* Force and update so the count is accurate (i.e. not a cached count) */
2710 print_queue_update(snum, True);
2712 can_job_admin = print_access_check(user, snum, JOB_ACCESS_ADMINISTER);
2713 njobs = print_queue_status(snum, &queue, &status);
2715 if ( can_job_admin )
2718 for (i=0;i<njobs;i++) {
2719 BOOL owner = is_owner(user, snum, queue[i].job);
2721 if (owner || can_job_admin) {
2722 print_job_delete1(snum, queue[i].job);
2726 if ( can_job_admin )
2729 /* update the cache */
2730 print_queue_update( snum, True );