2 Unix SMB/CIFS implementation.
3 printing backend routines
4 Copyright (C) Andrew Tridgell 1992-2000
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /* Current printer interface */
24 struct printif *current_printif = &generic_printif;
27 the printing backend revolves around a tdb database that stores the
28 SMB view of the print queue
30 The key for this database is a jobid - a internally generated number that
31 uniquely identifies a print job
33 reading the print queue involves two steps:
34 - possibly running lpq and updating the internal database from that
35 - reading entries from the database
37 jobids are assigned when a job starts spooling.
40 /* the open printing.tdb database */
41 static TDB_CONTEXT *tdb;
42 static pid_t local_pid;
44 static int get_queue_status(int, print_status_struct *);
46 /****************************************************************************
47 Initialise the printing backend. Called once at startup.
48 Does not survive a fork
49 ****************************************************************************/
51 BOOL print_backend_init(void)
53 char *sversion = "INFO/version";
55 if (tdb && local_pid == sys_getpid()) return True;
56 tdb = tdb_open_log(lock_path("printing.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
58 DEBUG(0,("print_backend_init: Failed to open printing backend database %s\n",
59 lock_path("printing.tdb") ));
62 local_pid = sys_getpid();
64 /* handle a Samba upgrade */
65 tdb_lock_bystring(tdb, sversion);
66 if (tdb_fetch_int32(tdb, sversion) != PRINT_DATABASE_VERSION) {
67 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
68 tdb_store_int32(tdb, sversion, PRINT_DATABASE_VERSION);
70 tdb_unlock_bystring(tdb, sversion);
72 /* select the appropriate printing interface... */
74 if (strcmp(lp_printcapname(), "cups") == 0)
75 current_printif = &cups_printif;
76 #endif /* HAVE_CUPS */
78 /* do NT print initialization... */
79 return nt_printing_init();
82 /****************************************************************************
83 useful function to generate a tdb key
84 ****************************************************************************/
85 static TDB_DATA print_key(int jobid)
91 ret.dptr = (void *)&j;
92 ret.dsize = sizeof(j);
96 /****************************************************************************
97 useful function to find a print job in the database
98 ****************************************************************************/
99 static struct printjob *print_job_find(int jobid)
101 static struct printjob pjob;
104 ret = tdb_fetch(tdb, print_key(jobid));
105 if (!ret.dptr || ret.dsize != sizeof(pjob)) return NULL;
107 memcpy(&pjob, ret.dptr, sizeof(pjob));
112 /****************************************************************************
113 store a job structure back to the database
114 ****************************************************************************/
115 static BOOL print_job_store(int jobid, struct printjob *pjob)
120 d.dptr = (void *)pjob;
121 d.dsize = sizeof(*pjob);
122 ret = (tdb_store(tdb, print_key(jobid), d, TDB_REPLACE) == 0);
126 /****************************************************************************
127 parse a file name from the system spooler to generate a jobid
128 ****************************************************************************/
129 static int print_parse_jobid(char *fname)
133 if (strncmp(fname,PRINT_SPOOL_PREFIX,strlen(PRINT_SPOOL_PREFIX)) != 0) return -1;
134 fname += strlen(PRINT_SPOOL_PREFIX);
137 if (jobid <= 0) return -1;
143 /****************************************************************************
144 list a unix job in the print database
145 ****************************************************************************/
146 static void print_unix_job(int snum, print_queue_struct *q)
148 int jobid = q->job + UNIX_JOB_START;
149 struct printjob pj, *old_pj;
151 /* Preserve the timestamp on an existing unix print job */
153 old_pj = print_job_find(jobid);
160 pj.starttime = old_pj ? old_pj->starttime : q->time;
161 pj.status = q->status;
165 fstrcpy(pj.filename, "");
166 fstrcpy(pj.jobname, q->fs_file);
167 fstrcpy(pj.user, q->fs_user);
168 fstrcpy(pj.queuename, lp_servicename(snum));
170 print_job_store(jobid, &pj);
174 struct traverse_struct {
175 print_queue_struct *queue;
176 int qcount, snum, maxcount, total_jobs;
179 /* utility fn to delete any jobs that are no longer active */
180 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
182 struct traverse_struct *ts = (struct traverse_struct *)state;
183 struct printjob pjob;
186 if (data.dsize != sizeof(pjob) || key.dsize != sizeof(int)) return 0;
187 memcpy(&jobid, key.dptr, sizeof(jobid));
188 memcpy(&pjob, data.dptr, sizeof(pjob));
190 if (ts->snum != lp_servicenumber(pjob.queuename)) {
191 /* this isn't for the queue we are looking at */
197 /* remove a unix job if it isn't in the system queue any more */
199 for (i=0;i<ts->qcount;i++) {
200 if (jobid == ts->queue[i].job + UNIX_JOB_START) break;
203 tdb_delete(tdb, key);
209 /* maybe it hasn't been spooled yet */
211 /* if a job is not spooled and the process doesn't
212 exist then kill it. This cleans up after smbd
214 if (!process_exists(pjob.pid))
215 tdb_delete(tdb, key);
221 for (i=0;i<ts->qcount;i++) {
222 int qid = print_parse_jobid(ts->queue[i].fs_file);
223 if (jobid == qid) break;
226 /* The job isn't in the system queue - we have to assume it has
227 completed, so delete the database entry. */
229 if (i == ts->qcount) {
230 time_t cur_t = time(NULL);
232 /* A race can occur between the time a job is spooled and
233 when it appears in the lpq output. This happens when
234 the job is added to printing.tdb when another smbd
235 running print_queue_update() has completed a lpq and
236 is currently traversing the printing tdb and deleting jobs.
237 A workaround is to not delete the job if it has been
238 submitted less than lp_lpqcachetime() seconds ago. */
240 if ((cur_t - pjob.starttime) > lp_lpqcachetime())
251 /****************************************************************************
252 check if the print queue has been updated recently enough
253 ****************************************************************************/
254 static void print_cache_flush(int snum)
257 slprintf(key, sizeof(key)-1, "CACHE/%s", lp_servicename(snum));
258 tdb_store_int32(tdb, key, -1);
261 /****************************************************************************
262 Check if someone already thinks they are doing the update.
263 ****************************************************************************/
265 static pid_t get_updating_pid(fstring printer_name)
271 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
273 key.dsize = strlen(keystr);
275 data = tdb_fetch(tdb, key);
276 if (!data.dptr || data.dsize != sizeof(pid_t))
279 memcpy(&updating_pid, data.dptr, sizeof(pid_t));
282 if (process_exists(updating_pid))
288 /****************************************************************************
289 Set the fact that we're doing the update, or have finished doing the update
291 ****************************************************************************/
293 static void set_updating_pid(fstring printer_name, BOOL delete)
298 pid_t updating_pid = sys_getpid();
300 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
302 key.dsize = strlen(keystr);
305 tdb_delete(tdb, key);
309 data.dptr = (void *)&updating_pid;
310 data.dsize = sizeof(pid_t);
312 tdb_store(tdb, key, data, TDB_REPLACE);
315 /****************************************************************************
316 Send a message saying the queue changed.
317 ****************************************************************************/
319 static void send_queue_message(const char *printer_name, uint32 high, uint32 low)
321 char msg[8 + sizeof(fstring)];
324 fstrcpy(&msg[8], printer_name);
326 message_send_all(conn_tdb_ctx(), MSG_PRINTER_NOTIFY, msg, 8 + strlen(printer_name) + 1, False, NULL);
329 /****************************************************************************
330 update the internal database from the system print queue for a queue in the background
331 ****************************************************************************/
333 static void print_queue_update_background(int snum)
336 print_queue_struct *queue = NULL;
337 print_status_struct status;
338 print_status_struct old_status;
339 struct printjob *pjob;
340 struct traverse_struct tstruct;
341 fstring keystr, printer_name, cachestr;
344 fstrcpy(printer_name, lp_servicename(snum));
347 * Check to see if someone else is doing this update.
348 * This is essentially a mutex on the update.
351 if (get_updating_pid(printer_name) != -1)
354 /* Lock the queue for the database update */
356 slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", printer_name);
357 tdb_lock_bystring(tdb, keystr);
360 * Ensure that no one else got in here.
361 * If the updating pid is still -1 then we are
365 if (get_updating_pid(printer_name) != -1) {
367 * Someone else is doing the update, exit.
369 tdb_unlock_bystring(tdb, keystr);
374 * We're going to do the update ourselves.
377 /* Tell others we're doing the update. */
378 set_updating_pid(printer_name, False);
381 * Allow others to enter and notice we're doing
385 tdb_unlock_bystring(tdb, keystr);
388 * Update the cache time FIRST ! Stops others even
389 * attempting to get the lock and doing this
390 * if the lpq takes a long time.
393 slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", printer_name);
394 tdb_store_int32(tdb, cachestr, (int)time(NULL));
396 /* get the current queue using the appropriate interface */
399 qcount = (*(current_printif->queue_get))(snum, &queue, &status);
401 DEBUG(3, ("%d job%s in queue for %s\n", qcount, (qcount != 1) ?
402 "s" : "", printer_name));
405 any job in the internal database that is marked as spooled
406 and doesn't exist in the system queue is considered finished
407 and removed from the database
409 any job in the system database but not in the internal database
410 is added as a unix job
412 fill in any system job numbers as we go
414 for (i=0; i<qcount; i++) {
415 int jobid = print_parse_jobid(queue[i].fs_file);
418 /* assume its a unix print job */
419 print_unix_job(snum, &queue[i]);
423 /* we have an active SMB print job - update its status */
424 pjob = print_job_find(jobid);
426 /* err, somethings wrong. Probably smbd was restarted
427 with jobs in the queue. All we can do is treat them
428 like unix jobs. Pity. */
429 print_unix_job(snum, &queue[i]);
433 pjob->sysjob = queue[i].job;
434 pjob->status = queue[i].status;
436 print_job_store(jobid, pjob);
439 /* now delete any queued entries that don't appear in the
441 tstruct.queue = queue;
442 tstruct.qcount = qcount;
444 tstruct.total_jobs = 0;
446 tdb_traverse(tdb, traverse_fn_delete, (void *)&tstruct);
448 safe_free(tstruct.queue);
450 tdb_store_int32(tdb, "INFO/total_jobs", tstruct.total_jobs);
453 * Get the old print status. We will use this to compare the
454 * number of jobs. If they have changed we need to send a
455 * "changed" message to the smbds.
458 if( qcount != get_queue_status(snum, &old_status)) {
459 DEBUG(10,("print_queue_update: queue status change %d jobs -> %d jobs for printer %s\n",
460 old_status.qcount, qcount, printer_name ));
461 send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);
464 /* store the new queue status structure */
465 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printer_name);
467 key.dsize = strlen(keystr);
469 status.qcount = qcount;
470 data.dptr = (void *)&status;
471 data.dsize = sizeof(status);
472 tdb_store(tdb, key, data, TDB_REPLACE);
475 * Update the cache time again. We want to do this call
476 * as little as possible...
479 slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", printer_name);
480 tdb_store_int32(tdb, keystr, (int)time(NULL));
482 /* Delete our pid from the db. */
483 set_updating_pid(printer_name, True);
486 /****************************************************************************
487 this is the receive function of the background lpq updater
488 ****************************************************************************/
489 static void print_queue_receive(int msg_type, pid_t src, void *buf, size_t len)
493 print_queue_update_background(snum);
496 static pid_t background_lpq_updater_pid;
498 /****************************************************************************
499 main thread of the background lpq updater
500 ****************************************************************************/
501 void start_background_queue(void)
503 DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
504 background_lpq_updater_pid = sys_fork();
506 if (background_lpq_updater_pid == -1) {
507 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
511 if(background_lpq_updater_pid == 0) {
513 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
515 claim_connection(NULL,"smbd lpq backend",0,False);
517 if (!locking_init(0))
520 if (!print_backend_init())
523 message_register(MSG_PRINTER_UPDATE, print_queue_receive);
525 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
528 DEBUG(10,("start_background_queue: background LPQ thread got a message\n"));
534 /****************************************************************************
535 update the internal database from the system print queue for a queue
536 ****************************************************************************/
537 static void print_queue_update(int snum)
539 if (background_lpq_updater_pid > 0) {
540 message_send_pid(background_lpq_updater_pid, MSG_PRINTER_UPDATE,
541 &snum, sizeof(snum), False);
545 /****************************************************************************
546 check if a jobid is valid. It is valid if it exists in the database
547 ****************************************************************************/
548 BOOL print_job_exists(int jobid)
550 return tdb_exists(tdb, print_key(jobid));
554 /****************************************************************************
555 work out which service a jobid is for
556 note that we have to look up by queue name to ensure that it works for
557 other than the process that started the job
558 ****************************************************************************/
559 int print_job_snum(int jobid)
561 struct printjob *pjob = print_job_find(jobid);
562 if (!pjob) return -1;
564 return find_service(pjob->queuename);
567 /****************************************************************************
568 give the fd used for a jobid
569 ****************************************************************************/
570 int print_job_fd(int jobid)
572 struct printjob *pjob = print_job_find(jobid);
573 if (!pjob) return -1;
574 /* don't allow another process to get this info - it is meaningless */
575 if (pjob->pid != local_pid) return -1;
579 /****************************************************************************
580 give the filename used for a jobid
581 only valid for the process doing the spooling and when the job
583 ****************************************************************************/
584 char *print_job_fname(int jobid)
586 struct printjob *pjob = print_job_find(jobid);
587 if (!pjob || pjob->spooled || pjob->pid != local_pid) return NULL;
588 return pjob->filename;
592 /****************************************************************************
593 set the place in the queue for a job
594 ****************************************************************************/
595 BOOL print_job_set_place(int jobid, int place)
597 DEBUG(2,("print_job_set_place not implemented yet\n"));
601 /****************************************************************************
602 set the name of a job. Only possible for owner
603 ****************************************************************************/
604 BOOL print_job_set_name(int jobid, char *name)
606 struct printjob *pjob = print_job_find(jobid);
607 if (!pjob || pjob->pid != local_pid) return False;
609 fstrcpy(pjob->jobname, name);
610 return print_job_store(jobid, pjob);
614 /****************************************************************************
615 delete a print job - don't update queue
616 ****************************************************************************/
617 static BOOL print_job_delete1(int jobid)
619 struct printjob *pjob = print_job_find(jobid);
620 int snum, result = 0;
622 if (!pjob) return False;
625 * If already deleting just return.
628 if (pjob->status == LPQ_DELETING)
631 snum = print_job_snum(jobid);
633 DEBUG(5,("print_job_delete1: unknown service number for jobid %d\n", jobid));
637 /* Hrm - we need to be able to cope with deleting a job before it
638 has reached the spooler. */
640 if (pjob->sysjob == -1) {
641 DEBUG(5, ("attempt to delete job %d not seen by lpr\n",
645 /* Set the tdb entry to be deleting. */
647 pjob->status = LPQ_DELETING;
648 print_job_store(jobid, pjob);
650 if (pjob->spooled && pjob->sysjob != -1)
651 result = (*(current_printif->job_delete))(snum, pjob);
653 /* Delete the tdb entry if the delete suceeded or the job hasn't
657 tdb_delete(tdb, print_key(jobid));
660 return (result == 0);
663 /****************************************************************************
664 return true if the current user owns the print job
665 ****************************************************************************/
666 static BOOL is_owner(struct current_user *user, int jobid)
668 struct printjob *pjob = print_job_find(jobid);
671 if (!pjob || !user) return False;
673 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
674 return strequal(pjob->user, vuser->user.smb_name);
676 return strequal(pjob->user, uidtoname(user->uid));
680 /****************************************************************************
682 ****************************************************************************/
683 BOOL print_job_delete(struct current_user *user, int jobid, WERROR *errcode)
685 int snum = print_job_snum(jobid);
690 DEBUG(5,("print_job_delete: unknown service number for jobid %d\n", jobid));
694 owner = is_owner(user, jobid);
696 /* Check access against security descriptor or whether the user
700 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
701 DEBUG(3, ("delete denied by security descriptor\n"));
702 *errcode = WERR_ACCESS_DENIED;
706 if (!print_job_delete1(jobid)) return False;
708 /* force update the database and say the delete failed if the
711 print_queue_update(snum);
713 /* Send a printer notify message */
715 printer_name = PRINTERNAME(snum);
717 send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);
719 return !print_job_exists(jobid);
723 /****************************************************************************
725 ****************************************************************************/
726 BOOL print_job_pause(struct current_user *user, int jobid, WERROR *errcode)
728 struct printjob *pjob = print_job_find(jobid);
732 if (!pjob || !user) return False;
734 if (!pjob->spooled || pjob->sysjob == -1) return False;
736 snum = print_job_snum(jobid);
738 DEBUG(5,("print_job_pause: unknown service number for jobid %d\n", jobid));
742 DEBUG(5,("print_job_resume: unknown service number for jobid %d\n", jobid));
746 if (!is_owner(user, jobid) &&
747 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
748 DEBUG(3, ("pause denied by security descriptor\n"));
749 *errcode = WERR_ACCESS_DENIED;
753 /* need to pause the spooled entry */
754 ret = (*(current_printif->job_pause))(snum, pjob);
757 *errcode = WERR_INVALID_PARAM;
761 /* force update the database */
762 print_cache_flush(snum);
764 /* Send a printer notify message */
766 printer_name = PRINTERNAME(snum);
768 send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);
770 /* how do we tell if this succeeded? */
775 /****************************************************************************
777 ****************************************************************************/
778 BOOL print_job_resume(struct current_user *user, int jobid, WERROR *errcode)
780 struct printjob *pjob = print_job_find(jobid);
784 if (!pjob || !user) return False;
786 if (!pjob->spooled || pjob->sysjob == -1) return False;
788 snum = print_job_snum(jobid);
790 if (!is_owner(user, jobid) &&
791 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
792 DEBUG(3, ("resume denied by security descriptor\n"));
793 *errcode = WERR_ACCESS_DENIED;
797 ret = (*(current_printif->job_resume))(snum, pjob);
800 *errcode = WERR_INVALID_PARAM;
804 /* force update the database */
805 print_cache_flush(snum);
807 /* Send a printer notify message */
809 printer_name = PRINTERNAME(snum);
811 send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);
816 /****************************************************************************
817 write to a print file
818 ****************************************************************************/
819 int print_job_write(int jobid, const char *buf, int size)
822 struct printjob *pjob = print_job_find(jobid);
826 /* don't allow another process to get this info - it is meaningless */
827 if (pjob->pid != local_pid)
830 return_code = write(pjob->fd, buf, size);
833 print_job_store(jobid, pjob);
838 /****************************************************************************
839 Check if the print queue has been updated recently enough.
840 ****************************************************************************/
842 static BOOL print_cache_expired(int snum)
845 time_t last_qscan_time, time_now = time(NULL);
847 slprintf(key, sizeof(key), "CACHE/%s", lp_servicename(snum));
848 last_qscan_time = (time_t)tdb_fetch_int32(tdb, key);
851 * Invalidate the queue for 3 reasons.
852 * (1). last queue scan time == -1.
853 * (2). Current time - last queue scan time > allowed cache time.
854 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
855 * This last test picks up machines for which the clock has been moved
856 * forward, an lpq scan done and then the clock moved back. Otherwise
857 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
860 if (last_qscan_time == ((time_t)-1) || (time_now - last_qscan_time) >= lp_lpqcachetime() ||
861 last_qscan_time > (time_now + MAX_CACHE_VALID_TIME)) {
862 DEBUG(3, ("print cache expired for queue %s \
863 (last_qscan_time = %d, time now = %d, qcachetime = %d)\n", lp_servicename(snum),
864 (int)last_qscan_time, (int)time_now, (int)lp_lpqcachetime() ));
870 /****************************************************************************
871 Get the queue status - do not update if db is out of date.
872 ****************************************************************************/
873 static int get_queue_status(int snum, print_status_struct *status)
878 ZERO_STRUCTP(status);
879 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", lp_servicename(snum));
881 key.dsize = strlen(keystr);
882 data = tdb_fetch(tdb, key);
884 if (data.dsize == sizeof(print_status_struct)) {
885 memcpy(status, data.dptr, sizeof(print_status_struct));
889 return status->qcount;
892 /****************************************************************************
893 Determine the number of jobs in a queue.
894 ****************************************************************************/
896 int print_queue_length(int snum, print_status_struct *pstatus)
898 print_status_struct status;
901 /* make sure the database is up to date */
902 if (print_cache_expired(snum))
903 print_queue_update(snum);
905 /* also fetch the queue status */
906 memset(&status, 0, sizeof(status));
907 len = get_queue_status(snum, &status);
913 /****************************************************************************
914 Determine the number of jobs in all queues.
915 ****************************************************************************/
916 static int get_total_jobs(int snum)
920 /* make sure the database is up to date */
921 if (print_cache_expired(snum)) print_queue_update(snum);
923 total_jobs = tdb_fetch_int32(tdb, "INFO/total_jobs");
930 /***************************************************************************
931 start spooling a job - return the jobid
932 ***************************************************************************/
933 int print_job_start(struct current_user *user, int snum, char *jobname)
937 struct printjob pjob;
944 if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
945 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
949 if (!print_time_access_check(snum)) {
950 DEBUG(3, ("print_job_start: job start denied by time check\n"));
954 path = lp_pathname(snum);
956 /* see if we have sufficient disk space */
957 if (lp_minprintspace(snum)) {
958 SMB_BIG_UINT dspace, dsize;
959 if (sys_fsusage(path, &dspace, &dsize) == 0 &&
960 dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
961 DEBUG(3, ("print_job_start: disk space check failed.\n"));
967 /* for autoloaded printers, check that the printcap entry still exists */
968 if (lp_autoloaded(snum) && !pcap_printername_ok(lp_servicename(snum), NULL)) {
969 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_servicename(snum) ));
974 /* Insure the maximum queue size is not violated */
975 if (lp_maxprintjobs(snum) && (njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
976 DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per queue (%d).\n",
977 njobs, lp_maxprintjobs(snum) ));
982 /* Insure the maximum print jobs in the system is not violated */
983 if (lp_totalprintjobs() && get_total_jobs(snum) > lp_totalprintjobs()) {
984 DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per system (%d).\n",
985 njobs, lp_totalprintjobs() ));
990 /* create the database entry */
992 pjob.pid = local_pid;
995 pjob.starttime = time(NULL);
996 pjob.status = LPQ_SPOOLING;
998 pjob.spooled = False;
1001 fstrcpy(pjob.jobname, jobname);
1003 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1004 fstrcpy(pjob.user, vuser->user.smb_name);
1006 fstrcpy(pjob.user, uidtoname(user->uid));
1009 fstrcpy(pjob.queuename, lp_servicename(snum));
1011 /* lock the database */
1012 tdb_lock_bystring(tdb, "INFO/nextjob");
1014 next_jobid = tdb_fetch_int32(tdb, "INFO/nextjob");
1015 if (next_jobid == -1)
1018 for (jobid = NEXT_JOBID(next_jobid); jobid != next_jobid; jobid = NEXT_JOBID(jobid)) {
1019 if (!print_job_exists(jobid))
1022 if (jobid == next_jobid || !print_job_store(jobid, &pjob)) {
1023 DEBUG(3, ("print_job_start: either jobid (%d)==next_jobid(%d) or print_job_store failed.\n",
1024 jobid, next_jobid ));
1029 tdb_store_int32(tdb, "INFO/nextjob", jobid);
1031 /* we have a job entry - now create the spool file */
1032 slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.6d.XXXXXX",
1033 path, PRINT_SPOOL_PREFIX, jobid);
1034 pjob.fd = smb_mkstemp(pjob.filename);
1036 if (pjob.fd == -1) {
1037 if (errno == EACCES) {
1038 /* Common setup error, force a report. */
1039 DEBUG(0, ("print_job_start: insufficient permissions \
1040 to open spool file %s.\n", pjob.filename));
1042 /* Normal case, report at level 3 and above. */
1043 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
1044 DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
1049 print_job_store(jobid, &pjob);
1051 tdb_unlock_bystring(tdb, "INFO/nextjob");
1054 * If the printer is marked as postscript output a leading
1055 * file identifier to ensure the file is treated as a raw
1057 * This has a similar effect as CtrlD=0 in WIN.INI file.
1058 * tim@fsg.com 09/06/94
1060 if (lp_postscript(snum)) {
1061 print_job_write(jobid, "%!\n",3);
1068 tdb_delete(tdb, print_key(jobid));
1071 tdb_unlock_bystring(tdb, "INFO/nextjob");
1073 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
1077 /****************************************************************************
1078 Update the number of pages spooled to jobid
1079 ****************************************************************************/
1081 void print_job_endpage(int jobid)
1083 struct printjob *pjob = print_job_find(jobid);
1086 /* don't allow another process to get this info - it is meaningless */
1087 if (pjob->pid != local_pid)
1091 print_job_store(jobid, pjob);
1094 /****************************************************************************
1095 Print a file - called on closing the file. This spools the job.
1096 If normal close is false then we're tearing down the jobs - treat as an
1098 ****************************************************************************/
1100 BOOL print_job_end(int jobid, BOOL normal_close)
1102 struct printjob *pjob = print_job_find(jobid);
1104 SMB_STRUCT_STAT sbuf;
1109 if (pjob->spooled || pjob->pid != local_pid)
1112 snum = print_job_snum(jobid);
1114 DEBUG(5,("print_job_end: unknown service number for jobid %d\n", jobid));
1118 if (normal_close && (sys_fstat(pjob->fd, &sbuf) == 0)) {
1119 pjob->size = sbuf.st_size;
1125 * Not a normal close or we couldn't stat the job file,
1126 * so something has gone wrong. Cleanup.
1130 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
1134 /* Technically, this is not quit right. If the printer has a separator
1135 * page turned on, the NT spooler prints the separator page even if the
1136 * print job is 0 bytes. 010215 JRR */
1137 if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
1138 /* don't bother spooling empty files or something being deleted. */
1139 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
1140 pjob->filename, pjob->size ? "deleted" : "zero length" ));
1141 unlink(pjob->filename);
1142 tdb_delete(tdb, print_key(jobid));
1146 ret = (*(current_printif->job_submit))(snum, pjob);
1151 /* The print job has been sucessfully handed over to the back-end */
1153 pjob->spooled = True;
1154 pjob->status = LPQ_QUEUED;
1155 print_job_store(jobid, pjob);
1157 /* make sure the database is up to date */
1158 if (print_cache_expired(snum))
1159 print_queue_update(snum);
1165 /* The print job was not succesfully started. Cleanup */
1166 /* Still need to add proper error return propagation! 010122:JRR */
1167 unlink(pjob->filename);
1168 tdb_delete(tdb, print_key(jobid));
1172 /* utility fn to enumerate the print queue */
1173 static int traverse_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
1175 struct traverse_struct *ts = (struct traverse_struct *)state;
1176 struct printjob pjob;
1179 if (data.dsize != sizeof(pjob) || key.dsize != sizeof(int)) return 0;
1180 memcpy(&jobid, key.dptr, sizeof(jobid));
1181 memcpy(&pjob, data.dptr, sizeof(pjob));
1183 /* maybe it isn't for this queue */
1184 if (ts->snum != lp_servicenumber(pjob.queuename))
1187 if (ts->qcount >= ts->maxcount) return 0;
1191 ts->queue[i].job = jobid;
1192 ts->queue[i].size = pjob.size;
1193 ts->queue[i].page_count = pjob.page_count;
1194 ts->queue[i].status = pjob.status;
1195 ts->queue[i].priority = 1;
1196 ts->queue[i].time = pjob.starttime;
1197 fstrcpy(ts->queue[i].fs_user, pjob.user);
1198 fstrcpy(ts->queue[i].fs_file, pjob.jobname);
1205 struct traverse_count_struct {
1209 /* utility fn to count the number of entries in the print queue */
1210 static int traverse_count_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
1212 struct traverse_count_struct *ts = (struct traverse_count_struct *)state;
1213 struct printjob pjob;
1216 if (data.dsize != sizeof(pjob) || key.dsize != sizeof(int)) return 0;
1217 memcpy(&jobid, key.dptr, sizeof(jobid));
1218 memcpy(&pjob, data.dptr, sizeof(pjob));
1220 /* maybe it isn't for this queue */
1221 if (ts->snum != lp_servicenumber(pjob.queuename))
1229 /* Sort print jobs by submittal time */
1231 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
1235 if (!j1 && !j2) return 0;
1239 /* Sort on job start time */
1241 if (j1->time == j2->time) return 0;
1242 return (j1->time > j2->time) ? 1 : -1;
1245 /****************************************************************************
1246 get a printer queue listing
1247 ****************************************************************************/
1248 int print_queue_status(int snum,
1249 print_queue_struct **queue,
1250 print_status_struct *status)
1252 struct traverse_struct tstruct;
1253 struct traverse_count_struct tsc;
1257 /* make sure the database is up to date */
1258 if (print_cache_expired(snum)) print_queue_update(snum);
1263 * Fetch the queue status. We must do this first, as there may
1264 * be no jobs in the queue.
1266 ZERO_STRUCTP(status);
1267 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", lp_servicename(snum));
1269 key.dsize = strlen(keystr);
1270 data = tdb_fetch(tdb, key);
1272 if (data.dsize == sizeof(*status)) {
1273 memcpy(status, data.dptr, sizeof(*status));
1279 * Now, fetch the print queue information. We first count the number
1280 * of entries, and then only retrieve the queue if necessary.
1285 tdb_traverse(tdb, traverse_count_fn_queue, (void *)&tsc);
1290 /* Allocate the queue size. */
1291 if ((tstruct.queue = (print_queue_struct *)
1292 malloc(sizeof(print_queue_struct)*tsc.count))
1297 * Fill in the queue.
1298 * We need maxcount as the queue size may have changed between
1299 * the two calls to tdb_traverse.
1302 tstruct.maxcount = tsc.count;
1303 tstruct.snum = snum;
1305 tdb_traverse(tdb, traverse_fn_queue, (void *)&tstruct);
1307 /* Sort the queue by submission time otherwise they are displayed
1310 qsort(tstruct.queue, tstruct.qcount, sizeof(print_queue_struct),
1311 QSORT_CAST(printjob_comp));
1313 *queue = tstruct.queue;
1314 return tstruct.qcount;
1318 /****************************************************************************
1319 turn a queue name into a snum
1320 ****************************************************************************/
1321 int print_queue_snum(char *qname)
1323 int snum = lp_servicenumber(qname);
1324 if (snum == -1 || !lp_print_ok(snum)) return -1;
1329 /****************************************************************************
1331 ****************************************************************************/
1332 BOOL print_queue_pause(struct current_user *user, int snum, WERROR *errcode)
1337 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
1338 *errcode = WERR_ACCESS_DENIED;
1342 ret = (*(current_printif->queue_pause))(snum);
1345 *errcode = WERR_INVALID_PARAM;
1349 /* force update the database */
1350 print_cache_flush(snum);
1352 /* Send a printer notify message */
1354 printer_name = PRINTERNAME(snum);
1356 send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);
1361 /****************************************************************************
1363 ****************************************************************************/
1364 BOOL print_queue_resume(struct current_user *user, int snum, WERROR *errcode)
1369 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
1370 *errcode = WERR_ACCESS_DENIED;
1374 ret = (*(current_printif->queue_resume))(snum);
1377 *errcode = WERR_INVALID_PARAM;
1381 /* make sure the database is up to date */
1382 if (print_cache_expired(snum)) print_queue_update(snum);
1384 /* Send a printer notify message */
1386 printer_name = PRINTERNAME(snum);
1388 send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);
1393 /****************************************************************************
1394 purge a queue - implemented by deleting all jobs that we can delete
1395 ****************************************************************************/
1396 BOOL print_queue_purge(struct current_user *user, int snum, WERROR *errcode)
1398 print_queue_struct *queue;
1399 print_status_struct status;
1404 /* Force and update so the count is accurate (i.e. not a cached count) */
1405 print_queue_update(snum);
1407 can_job_admin = print_access_check(user, snum, JOB_ACCESS_ADMINISTER);
1408 njobs = print_queue_status(snum, &queue, &status);
1410 for (i=0;i<njobs;i++) {
1411 BOOL owner = is_owner(user, queue[i].job);
1413 if (owner || can_job_admin) {
1414 print_job_delete1(queue[i].job);
1420 /* Send a printer notify message */
1422 printer_name = PRINTERNAME(snum);
1424 send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);