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())
57 tdb = tdb_open_log(lock_path("printing.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
59 DEBUG(0,("print_backend_init: Failed to open printing backend database %s\n",
60 lock_path("printing.tdb") ));
63 local_pid = sys_getpid();
65 /* handle a Samba upgrade */
66 tdb_lock_bystring(tdb, sversion);
67 if (tdb_fetch_int32(tdb, sversion) != PRINT_DATABASE_VERSION) {
68 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
69 tdb_store_int32(tdb, sversion, PRINT_DATABASE_VERSION);
71 tdb_unlock_bystring(tdb, sversion);
73 /* select the appropriate printing interface... */
75 if (strcmp(lp_printcapname(), "cups") == 0)
76 current_printif = &cups_printif;
77 #endif /* HAVE_CUPS */
79 /* do NT print initialization... */
80 return nt_printing_init();
83 /****************************************************************************
84 Useful function to generate a tdb key.
85 ****************************************************************************/
87 static TDB_DATA print_key(int jobid)
93 ret.dptr = (void *)&j;
94 ret.dsize = sizeof(j);
98 /****************************************************************************
99 Useful function to find a print job in the database.
100 ****************************************************************************/
102 static struct printjob *print_job_find(int jobid)
104 static struct printjob pjob;
107 ret = tdb_fetch(tdb, print_key(jobid));
108 if (!ret.dptr || ret.dsize != sizeof(pjob))
111 memcpy(&pjob, ret.dptr, sizeof(pjob));
116 /****************************************************************************
117 Store a job structure back to the database.
118 ****************************************************************************/
120 static BOOL print_job_store(int jobid, struct printjob *pjob)
125 d.dptr = (void *)pjob;
126 d.dsize = sizeof(*pjob);
127 ret = (tdb_store(tdb, print_key(jobid), d, TDB_REPLACE) == 0);
131 /****************************************************************************
132 Parse a file name from the system spooler to generate a jobid.
133 ****************************************************************************/
135 static int print_parse_jobid(char *fname)
139 if (strncmp(fname,PRINT_SPOOL_PREFIX,strlen(PRINT_SPOOL_PREFIX)) != 0)
141 fname += strlen(PRINT_SPOOL_PREFIX);
150 /****************************************************************************
151 List a unix job in the print database.
152 ****************************************************************************/
154 static void print_unix_job(int snum, print_queue_struct *q)
156 int jobid = q->job + UNIX_JOB_START;
157 struct printjob pj, *old_pj;
159 /* Preserve the timestamp on an existing unix print job */
161 old_pj = print_job_find(jobid);
168 pj.starttime = old_pj ? old_pj->starttime : q->time;
169 pj.status = q->status;
173 fstrcpy(pj.filename, "");
174 fstrcpy(pj.jobname, q->fs_file);
175 fstrcpy(pj.user, q->fs_user);
176 fstrcpy(pj.queuename, lp_servicename(snum));
178 print_job_store(jobid, &pj);
182 struct traverse_struct {
183 print_queue_struct *queue;
184 int qcount, snum, maxcount, total_jobs;
187 /****************************************************************************
188 Utility fn to delete any jobs that are no longer active.
189 ****************************************************************************/
191 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
193 struct traverse_struct *ts = (struct traverse_struct *)state;
194 struct printjob pjob;
197 if (data.dsize != sizeof(pjob) || key.dsize != sizeof(int))
199 memcpy(&jobid, key.dptr, sizeof(jobid));
200 memcpy(&pjob, data.dptr, sizeof(pjob));
202 if (ts->snum != lp_servicenumber(pjob.queuename)) {
203 /* this isn't for the queue we are looking at */
209 /* remove a unix job if it isn't in the system queue any more */
211 for (i=0;i<ts->qcount;i++) {
212 if (jobid == ts->queue[i].job + UNIX_JOB_START)
216 tdb_delete(tdb, key);
222 /* maybe it hasn't been spooled yet */
224 /* if a job is not spooled and the process doesn't
225 exist then kill it. This cleans up after smbd
227 if (!process_exists(pjob.pid))
228 tdb_delete(tdb, key);
234 for (i=0;i<ts->qcount;i++) {
235 int qid = print_parse_jobid(ts->queue[i].fs_file);
240 /* The job isn't in the system queue - we have to assume it has
241 completed, so delete the database entry. */
243 if (i == ts->qcount) {
244 time_t cur_t = time(NULL);
246 /* A race can occur between the time a job is spooled and
247 when it appears in the lpq output. This happens when
248 the job is added to printing.tdb when another smbd
249 running print_queue_update() has completed a lpq and
250 is currently traversing the printing tdb and deleting jobs.
251 A workaround is to not delete the job if it has been
252 submitted less than lp_lpqcachetime() seconds ago. */
254 if ((cur_t - pjob.starttime) > lp_lpqcachetime())
265 /****************************************************************************
266 Check if the print queue has been updated recently enough.
267 ****************************************************************************/
269 static void print_cache_flush(int snum)
272 slprintf(key, sizeof(key)-1, "CACHE/%s", lp_servicename(snum));
273 tdb_store_int32(tdb, key, -1);
276 /****************************************************************************
277 Check if someone already thinks they are doing the update.
278 ****************************************************************************/
280 static pid_t get_updating_pid(fstring printer_name)
286 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
288 key.dsize = strlen(keystr);
290 data = tdb_fetch(tdb, key);
291 if (!data.dptr || data.dsize != sizeof(pid_t))
294 memcpy(&updating_pid, data.dptr, sizeof(pid_t));
295 SAFE_FREE(data.dptr);
297 if (process_exists(updating_pid))
303 /****************************************************************************
304 Set the fact that we're doing the update, or have finished doing the update
306 ****************************************************************************/
308 static void set_updating_pid(fstring printer_name, BOOL delete)
313 pid_t updating_pid = sys_getpid();
315 slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
317 key.dsize = strlen(keystr);
320 tdb_delete(tdb, key);
324 data.dptr = (void *)&updating_pid;
325 data.dsize = sizeof(pid_t);
327 tdb_store(tdb, key, data, TDB_REPLACE);
330 /****************************************************************************
331 Send a message saying the queue changed.
332 ****************************************************************************/
334 static void send_queue_message(const char *printer_name, uint32 high, uint32 low)
336 char msg[8 + sizeof(fstring)];
339 fstrcpy(&msg[8], printer_name);
341 message_send_all(conn_tdb_ctx(), MSG_PRINTER_NOTIFY, msg, 8 + strlen(printer_name) + 1, False, NULL);
344 /****************************************************************************
345 Update the internal database from the system print queue for a queue in the background
346 ****************************************************************************/
348 static void print_queue_update_background(int snum)
351 print_queue_struct *queue = NULL;
352 print_status_struct status;
353 print_status_struct old_status;
354 struct printjob *pjob;
355 struct traverse_struct tstruct;
356 fstring keystr, printer_name, cachestr;
359 fstrcpy(printer_name, lp_servicename(snum));
362 * Check to see if someone else is doing this update.
363 * This is essentially a mutex on the update.
366 if (get_updating_pid(printer_name) != -1)
369 /* Lock the queue for the database update */
371 slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", printer_name);
372 tdb_lock_bystring(tdb, keystr);
375 * Ensure that no one else got in here.
376 * If the updating pid is still -1 then we are
380 if (get_updating_pid(printer_name) != -1) {
382 * Someone else is doing the update, exit.
384 tdb_unlock_bystring(tdb, keystr);
389 * We're going to do the update ourselves.
392 /* Tell others we're doing the update. */
393 set_updating_pid(printer_name, False);
396 * Allow others to enter and notice we're doing
400 tdb_unlock_bystring(tdb, keystr);
403 * Update the cache time FIRST ! Stops others even
404 * attempting to get the lock and doing this
405 * if the lpq takes a long time.
408 slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", printer_name);
409 tdb_store_int32(tdb, cachestr, (int)time(NULL));
411 /* get the current queue using the appropriate interface */
414 qcount = (*(current_printif->queue_get))(snum, &queue, &status);
416 DEBUG(3, ("%d job%s in queue for %s\n", qcount, (qcount != 1) ?
417 "s" : "", printer_name));
420 any job in the internal database that is marked as spooled
421 and doesn't exist in the system queue is considered finished
422 and removed from the database
424 any job in the system database but not in the internal database
425 is added as a unix job
427 fill in any system job numbers as we go
429 for (i=0; i<qcount; i++) {
430 int jobid = print_parse_jobid(queue[i].fs_file);
433 /* assume its a unix print job */
434 print_unix_job(snum, &queue[i]);
438 /* we have an active SMB print job - update its status */
439 pjob = print_job_find(jobid);
441 /* err, somethings wrong. Probably smbd was restarted
442 with jobs in the queue. All we can do is treat them
443 like unix jobs. Pity. */
444 print_unix_job(snum, &queue[i]);
448 pjob->sysjob = queue[i].job;
449 pjob->status = queue[i].status;
451 print_job_store(jobid, pjob);
454 /* now delete any queued entries that don't appear in the
456 tstruct.queue = queue;
457 tstruct.qcount = qcount;
459 tstruct.total_jobs = 0;
461 tdb_traverse(tdb, traverse_fn_delete, (void *)&tstruct);
463 safe_free(tstruct.queue);
465 tdb_store_int32(tdb, "INFO/total_jobs", tstruct.total_jobs);
468 * Get the old print status. We will use this to compare the
469 * number of jobs. If they have changed we need to send a
470 * "changed" message to the smbds.
473 if( qcount != get_queue_status(snum, &old_status)) {
474 DEBUG(10,("print_queue_update: queue status change %d jobs -> %d jobs for printer %s\n",
475 old_status.qcount, qcount, printer_name ));
476 send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);
479 /* store the new queue status structure */
480 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printer_name);
482 key.dsize = strlen(keystr);
484 status.qcount = qcount;
485 data.dptr = (void *)&status;
486 data.dsize = sizeof(status);
487 tdb_store(tdb, key, data, TDB_REPLACE);
490 * Update the cache time again. We want to do this call
491 * as little as possible...
494 slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", printer_name);
495 tdb_store_int32(tdb, keystr, (int)time(NULL));
497 /* Delete our pid from the db. */
498 set_updating_pid(printer_name, True);
501 /****************************************************************************
502 This is the receive function of the background lpq updater.
503 ****************************************************************************/
505 static void print_queue_receive(int msg_type, pid_t src, void *buf, size_t len)
509 print_queue_update_background(snum);
512 static pid_t background_lpq_updater_pid;
514 /****************************************************************************
515 Main thread of the background lpq updater.
516 ****************************************************************************/
518 void start_background_queue(void)
520 DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
521 background_lpq_updater_pid = sys_fork();
523 if (background_lpq_updater_pid == -1) {
524 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
528 if(background_lpq_updater_pid == 0) {
530 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
532 claim_connection(NULL,"smbd lpq backend",0,False);
534 if (!locking_init(0))
537 if (!print_backend_init())
540 message_register(MSG_PRINTER_UPDATE, print_queue_receive);
542 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
545 DEBUG(10,("start_background_queue: background LPQ thread got a message\n"));
551 /****************************************************************************
552 Update the internal database from the system print queue for a queue.
553 ****************************************************************************/
555 static void print_queue_update(int snum)
557 if (background_lpq_updater_pid > 0) {
558 message_send_pid(background_lpq_updater_pid, MSG_PRINTER_UPDATE,
559 &snum, sizeof(snum), False);
563 /****************************************************************************
564 Check if a jobid is valid. It is valid if it exists in the database.
565 ****************************************************************************/
567 BOOL print_job_exists(int jobid)
569 return tdb_exists(tdb, print_key(jobid));
572 /****************************************************************************
573 Work out which service a jobid is for.
574 Note that we have to look up by queue name to ensure that it works for
575 other than the process that started the job.
576 ****************************************************************************/
578 int print_job_snum(int jobid)
580 struct printjob *pjob = print_job_find(jobid);
584 return find_service(pjob->queuename);
587 /****************************************************************************
588 Give the fd used for a jobid.
589 ****************************************************************************/
591 int print_job_fd(int jobid)
593 struct printjob *pjob = print_job_find(jobid);
596 /* don't allow another process to get this info - it is meaningless */
597 if (pjob->pid != local_pid)
602 /****************************************************************************
603 Give the filename used for a jobid.
604 Only valid for the process doing the spooling and when the job
605 has not been spooled.
606 ****************************************************************************/
608 char *print_job_fname(int jobid)
610 struct printjob *pjob = print_job_find(jobid);
611 if (!pjob || pjob->spooled || pjob->pid != local_pid)
613 return pjob->filename;
616 /****************************************************************************
617 Set the place in the queue for a job.
618 ****************************************************************************/
620 BOOL print_job_set_place(int jobid, int place)
622 DEBUG(2,("print_job_set_place not implemented yet\n"));
626 /****************************************************************************
627 Set the name of a job. Only possible for owner.
628 ****************************************************************************/
630 BOOL print_job_set_name(int jobid, char *name)
632 struct printjob *pjob = print_job_find(jobid);
633 if (!pjob || pjob->pid != local_pid)
636 fstrcpy(pjob->jobname, name);
637 return print_job_store(jobid, pjob);
640 /****************************************************************************
641 Delete a print job - don't update queue.
642 ****************************************************************************/
644 static BOOL print_job_delete1(int jobid)
646 struct printjob *pjob = print_job_find(jobid);
647 int snum, result = 0;
653 * If already deleting just return.
656 if (pjob->status == LPQ_DELETING)
659 snum = print_job_snum(jobid);
661 DEBUG(5,("print_job_delete1: unknown service number for jobid %d\n", jobid));
665 /* Hrm - we need to be able to cope with deleting a job before it
666 has reached the spooler. */
668 if (pjob->sysjob == -1) {
669 DEBUG(5, ("attempt to delete job %d not seen by lpr\n", jobid));
672 /* Set the tdb entry to be deleting. */
674 pjob->status = LPQ_DELETING;
675 print_job_store(jobid, pjob);
677 if (pjob->spooled && pjob->sysjob != -1)
678 result = (*(current_printif->job_delete))(snum, pjob);
680 /* Delete the tdb entry if the delete suceeded or the job hasn't
684 tdb_delete(tdb, print_key(jobid));
687 return (result == 0);
690 /****************************************************************************
691 Return true if the current user owns the print job.
692 ****************************************************************************/
694 static BOOL is_owner(struct current_user *user, int jobid)
696 struct printjob *pjob = print_job_find(jobid);
702 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
703 return strequal(pjob->user, vuser->user.smb_name);
705 return strequal(pjob->user, uidtoname(user->uid));
709 /****************************************************************************
711 ****************************************************************************/
713 BOOL print_job_delete(struct current_user *user, int jobid, WERROR *errcode)
715 int snum = print_job_snum(jobid);
720 DEBUG(5,("print_job_delete: unknown service number for jobid %d\n", jobid));
724 owner = is_owner(user, jobid);
726 /* Check access against security descriptor or whether the user
730 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
731 DEBUG(3, ("delete denied by security descriptor\n"));
732 *errcode = WERR_ACCESS_DENIED;
736 if (!print_job_delete1(jobid))
739 /* force update the database and say the delete failed if the
742 print_queue_update(snum);
744 /* Send a printer notify message */
746 printer_name = PRINTERNAME(snum);
748 send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);
750 return !print_job_exists(jobid);
753 /****************************************************************************
755 ****************************************************************************/
757 BOOL print_job_pause(struct current_user *user, int jobid, WERROR *errcode)
759 struct printjob *pjob = print_job_find(jobid);
766 if (!pjob->spooled || pjob->sysjob == -1)
769 snum = print_job_snum(jobid);
771 DEBUG(5,("print_job_pause: unknown service number for jobid %d\n", jobid));
775 DEBUG(5,("print_job_resume: unknown service number for jobid %d\n", jobid));
779 if (!is_owner(user, jobid) &&
780 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
781 DEBUG(3, ("pause denied by security descriptor\n"));
782 *errcode = WERR_ACCESS_DENIED;
786 /* need to pause the spooled entry */
787 ret = (*(current_printif->job_pause))(snum, pjob);
790 *errcode = WERR_INVALID_PARAM;
794 /* force update the database */
795 print_cache_flush(snum);
797 /* Send a printer notify message */
799 printer_name = PRINTERNAME(snum);
801 send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);
803 /* how do we tell if this succeeded? */
808 /****************************************************************************
810 ****************************************************************************/
812 BOOL print_job_resume(struct current_user *user, int jobid, WERROR *errcode)
814 struct printjob *pjob = print_job_find(jobid);
821 if (!pjob->spooled || pjob->sysjob == -1)
824 snum = print_job_snum(jobid);
826 if (!is_owner(user, jobid) &&
827 !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
828 DEBUG(3, ("resume denied by security descriptor\n"));
829 *errcode = WERR_ACCESS_DENIED;
833 ret = (*(current_printif->job_resume))(snum, pjob);
836 *errcode = WERR_INVALID_PARAM;
840 /* force update the database */
841 print_cache_flush(snum);
843 /* Send a printer notify message */
845 printer_name = PRINTERNAME(snum);
847 send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);
852 /****************************************************************************
853 Write to a print file.
854 ****************************************************************************/
856 int print_job_write(int jobid, const char *buf, int size)
859 struct printjob *pjob = print_job_find(jobid);
863 /* don't allow another process to get this info - it is meaningless */
864 if (pjob->pid != local_pid)
867 return_code = write(pjob->fd, buf, size);
870 print_job_store(jobid, pjob);
875 /****************************************************************************
876 Check if the print queue has been updated recently enough.
877 ****************************************************************************/
879 static BOOL print_cache_expired(int snum)
882 time_t last_qscan_time, time_now = time(NULL);
884 slprintf(key, sizeof(key), "CACHE/%s", lp_servicename(snum));
885 last_qscan_time = (time_t)tdb_fetch_int32(tdb, key);
888 * Invalidate the queue for 3 reasons.
889 * (1). last queue scan time == -1.
890 * (2). Current time - last queue scan time > allowed cache time.
891 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
892 * This last test picks up machines for which the clock has been moved
893 * forward, an lpq scan done and then the clock moved back. Otherwise
894 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
897 if (last_qscan_time == ((time_t)-1) || (time_now - last_qscan_time) >= lp_lpqcachetime() ||
898 last_qscan_time > (time_now + MAX_CACHE_VALID_TIME)) {
899 DEBUG(3, ("print cache expired for queue %s \
900 (last_qscan_time = %d, time now = %d, qcachetime = %d)\n", lp_servicename(snum),
901 (int)last_qscan_time, (int)time_now, (int)lp_lpqcachetime() ));
907 /****************************************************************************
908 Get the queue status - do not update if db is out of date.
909 ****************************************************************************/
911 static int get_queue_status(int snum, print_status_struct *status)
916 ZERO_STRUCTP(status);
917 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", lp_servicename(snum));
919 key.dsize = strlen(keystr);
920 data = tdb_fetch(tdb, key);
922 if (data.dsize == sizeof(print_status_struct)) {
923 memcpy(status, data.dptr, sizeof(print_status_struct));
925 SAFE_FREE(data.dptr);
927 return status->qcount;
930 /****************************************************************************
931 Determine the number of jobs in a queue.
932 ****************************************************************************/
934 int print_queue_length(int snum, print_status_struct *pstatus)
936 print_status_struct status;
939 /* make sure the database is up to date */
940 if (print_cache_expired(snum))
941 print_queue_update(snum);
943 /* also fetch the queue status */
944 memset(&status, 0, sizeof(status));
945 len = get_queue_status(snum, &status);
951 /****************************************************************************
952 Determine the number of jobs in all queues.
953 ****************************************************************************/
955 static int get_total_jobs(int snum)
959 /* make sure the database is up to date */
960 if (print_cache_expired(snum))
961 print_queue_update(snum);
963 total_jobs = tdb_fetch_int32(tdb, "INFO/total_jobs");
970 /***************************************************************************
971 Start spooling a job - return the jobid.
972 ***************************************************************************/
974 int print_job_start(struct current_user *user, int snum, char *jobname)
978 struct printjob pjob;
985 if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
986 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
990 if (!print_time_access_check(snum)) {
991 DEBUG(3, ("print_job_start: job start denied by time check\n"));
995 path = lp_pathname(snum);
997 /* see if we have sufficient disk space */
998 if (lp_minprintspace(snum)) {
999 SMB_BIG_UINT dspace, dsize;
1000 if (sys_fsusage(path, &dspace, &dsize) == 0 &&
1001 dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
1002 DEBUG(3, ("print_job_start: disk space check failed.\n"));
1008 /* for autoloaded printers, check that the printcap entry still exists */
1009 if (lp_autoloaded(snum) && !pcap_printername_ok(lp_servicename(snum), NULL)) {
1010 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_servicename(snum) ));
1015 /* Insure the maximum queue size is not violated */
1016 if (lp_maxprintjobs(snum) && (njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
1017 DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per queue (%d).\n",
1018 njobs, lp_maxprintjobs(snum) ));
1023 /* Insure the maximum print jobs in the system is not violated */
1024 if (lp_totalprintjobs() && get_total_jobs(snum) > lp_totalprintjobs()) {
1025 DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per system (%d).\n",
1026 njobs, lp_totalprintjobs() ));
1031 /* create the database entry */
1033 pjob.pid = local_pid;
1036 pjob.starttime = time(NULL);
1037 pjob.status = LPQ_SPOOLING;
1039 pjob.spooled = False;
1042 fstrcpy(pjob.jobname, jobname);
1044 if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
1045 fstrcpy(pjob.user, vuser->user.smb_name);
1047 fstrcpy(pjob.user, uidtoname(user->uid));
1050 fstrcpy(pjob.queuename, lp_servicename(snum));
1052 /* lock the database */
1053 tdb_lock_bystring(tdb, "INFO/nextjob");
1055 next_jobid = tdb_fetch_int32(tdb, "INFO/nextjob");
1056 if (next_jobid == -1)
1059 for (jobid = NEXT_JOBID(next_jobid); jobid != next_jobid; jobid = NEXT_JOBID(jobid)) {
1060 if (!print_job_exists(jobid))
1063 if (jobid == next_jobid || !print_job_store(jobid, &pjob)) {
1064 DEBUG(3, ("print_job_start: either jobid (%d)==next_jobid(%d) or print_job_store failed.\n",
1065 jobid, next_jobid ));
1070 tdb_store_int32(tdb, "INFO/nextjob", jobid);
1072 /* we have a job entry - now create the spool file */
1073 slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.6d.XXXXXX",
1074 path, PRINT_SPOOL_PREFIX, jobid);
1075 pjob.fd = smb_mkstemp(pjob.filename);
1077 if (pjob.fd == -1) {
1078 if (errno == EACCES) {
1079 /* Common setup error, force a report. */
1080 DEBUG(0, ("print_job_start: insufficient permissions \
1081 to open spool file %s.\n", pjob.filename));
1083 /* Normal case, report at level 3 and above. */
1084 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
1085 DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
1090 print_job_store(jobid, &pjob);
1092 tdb_unlock_bystring(tdb, "INFO/nextjob");
1095 * If the printer is marked as postscript output a leading
1096 * file identifier to ensure the file is treated as a raw
1098 * This has a similar effect as CtrlD=0 in WIN.INI file.
1099 * tim@fsg.com 09/06/94
1101 if (lp_postscript(snum)) {
1102 print_job_write(jobid, "%!\n",3);
1109 tdb_delete(tdb, print_key(jobid));
1112 tdb_unlock_bystring(tdb, "INFO/nextjob");
1114 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
1118 /****************************************************************************
1119 Update the number of pages spooled to jobid
1120 ****************************************************************************/
1122 void print_job_endpage(int jobid)
1124 struct printjob *pjob = print_job_find(jobid);
1127 /* don't allow another process to get this info - it is meaningless */
1128 if (pjob->pid != local_pid)
1132 print_job_store(jobid, pjob);
1135 /****************************************************************************
1136 Print a file - called on closing the file. This spools the job.
1137 If normal close is false then we're tearing down the jobs - treat as an
1139 ****************************************************************************/
1141 BOOL print_job_end(int jobid, BOOL normal_close)
1143 struct printjob *pjob = print_job_find(jobid);
1145 SMB_STRUCT_STAT sbuf;
1150 if (pjob->spooled || pjob->pid != local_pid)
1153 snum = print_job_snum(jobid);
1155 DEBUG(5,("print_job_end: unknown service number for jobid %d\n", jobid));
1159 if (normal_close && (sys_fstat(pjob->fd, &sbuf) == 0)) {
1160 pjob->size = sbuf.st_size;
1166 * Not a normal close or we couldn't stat the job file,
1167 * so something has gone wrong. Cleanup.
1171 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
1175 /* Technically, this is not quit right. If the printer has a separator
1176 * page turned on, the NT spooler prints the separator page even if the
1177 * print job is 0 bytes. 010215 JRR */
1178 if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
1179 /* don't bother spooling empty files or something being deleted. */
1180 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
1181 pjob->filename, pjob->size ? "deleted" : "zero length" ));
1182 unlink(pjob->filename);
1183 tdb_delete(tdb, print_key(jobid));
1187 ret = (*(current_printif->job_submit))(snum, pjob);
1192 /* The print job has been sucessfully handed over to the back-end */
1194 pjob->spooled = True;
1195 pjob->status = LPQ_QUEUED;
1196 print_job_store(jobid, pjob);
1198 /* make sure the database is up to date */
1199 if (print_cache_expired(snum))
1200 print_queue_update(snum);
1206 /* The print job was not succesfully started. Cleanup */
1207 /* Still need to add proper error return propagation! 010122:JRR */
1208 unlink(pjob->filename);
1209 tdb_delete(tdb, print_key(jobid));
1213 /****************************************************************************
1214 Utility fn to enumerate the print queue.
1215 ****************************************************************************/
1217 static int traverse_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
1219 struct traverse_struct *ts = (struct traverse_struct *)state;
1220 struct printjob pjob;
1223 if (data.dsize != sizeof(pjob) || key.dsize != sizeof(int))
1225 memcpy(&jobid, key.dptr, sizeof(jobid));
1226 memcpy(&pjob, data.dptr, sizeof(pjob));
1228 /* maybe it isn't for this queue */
1229 if (ts->snum != lp_servicenumber(pjob.queuename))
1232 if (ts->qcount >= ts->maxcount)
1237 ts->queue[i].job = jobid;
1238 ts->queue[i].size = pjob.size;
1239 ts->queue[i].page_count = pjob.page_count;
1240 ts->queue[i].status = pjob.status;
1241 ts->queue[i].priority = 1;
1242 ts->queue[i].time = pjob.starttime;
1243 fstrcpy(ts->queue[i].fs_user, pjob.user);
1244 fstrcpy(ts->queue[i].fs_file, pjob.jobname);
1251 struct traverse_count_struct {
1255 /****************************************************************************
1256 Utility fn to count the number of entries in the print queue.
1257 ****************************************************************************/
1259 static int traverse_count_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
1261 struct traverse_count_struct *ts = (struct traverse_count_struct *)state;
1262 struct printjob pjob;
1265 if (data.dsize != sizeof(pjob) || key.dsize != sizeof(int))
1267 memcpy(&jobid, key.dptr, sizeof(jobid));
1268 memcpy(&pjob, data.dptr, sizeof(pjob));
1270 /* maybe it isn't for this queue */
1271 if (ts->snum != lp_servicenumber(pjob.queuename))
1279 /****************************************************************************
1280 Sort print jobs by submittal time.
1281 ****************************************************************************/
1283 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
1294 /* Sort on job start time */
1296 if (j1->time == j2->time)
1298 return (j1->time > j2->time) ? 1 : -1;
1301 /****************************************************************************
1302 Get a printer queue listing.
1303 ****************************************************************************/
1305 int print_queue_status(int snum,
1306 print_queue_struct **queue,
1307 print_status_struct *status)
1309 struct traverse_struct tstruct;
1310 struct traverse_count_struct tsc;
1314 /* make sure the database is up to date */
1315 if (print_cache_expired(snum))
1316 print_queue_update(snum);
1321 * Fetch the queue status. We must do this first, as there may
1322 * be no jobs in the queue.
1324 ZERO_STRUCTP(status);
1325 slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", lp_servicename(snum));
1327 key.dsize = strlen(keystr);
1328 data = tdb_fetch(tdb, key);
1330 if (data.dsize == sizeof(*status)) {
1331 memcpy(status, data.dptr, sizeof(*status));
1333 SAFE_FREE(data.dptr);
1337 * Now, fetch the print queue information. We first count the number
1338 * of entries, and then only retrieve the queue if necessary.
1343 tdb_traverse(tdb, traverse_count_fn_queue, (void *)&tsc);
1348 /* Allocate the queue size. */
1349 if ((tstruct.queue = (print_queue_struct *)
1350 malloc(sizeof(print_queue_struct)*tsc.count)) == NULL)
1354 * Fill in the queue.
1355 * We need maxcount as the queue size may have changed between
1356 * the two calls to tdb_traverse.
1359 tstruct.maxcount = tsc.count;
1360 tstruct.snum = snum;
1362 tdb_traverse(tdb, traverse_fn_queue, (void *)&tstruct);
1364 /* Sort the queue by submission time otherwise they are displayed
1367 qsort(tstruct.queue, tstruct.qcount, sizeof(print_queue_struct),
1368 QSORT_CAST(printjob_comp));
1370 *queue = tstruct.queue;
1371 return tstruct.qcount;
1374 /****************************************************************************
1375 Turn a queue name into a snum.
1376 ****************************************************************************/
1378 int print_queue_snum(char *qname)
1380 int snum = lp_servicenumber(qname);
1381 if (snum == -1 || !lp_print_ok(snum))
1386 /****************************************************************************
1388 ****************************************************************************/
1390 BOOL print_queue_pause(struct current_user *user, int snum, WERROR *errcode)
1395 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
1396 *errcode = WERR_ACCESS_DENIED;
1400 ret = (*(current_printif->queue_pause))(snum);
1403 *errcode = WERR_INVALID_PARAM;
1407 /* force update the database */
1408 print_cache_flush(snum);
1410 /* Send a printer notify message */
1412 printer_name = PRINTERNAME(snum);
1414 send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);
1419 /****************************************************************************
1421 ****************************************************************************/
1423 BOOL print_queue_resume(struct current_user *user, int snum, WERROR *errcode)
1428 if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
1429 *errcode = WERR_ACCESS_DENIED;
1433 ret = (*(current_printif->queue_resume))(snum);
1436 *errcode = WERR_INVALID_PARAM;
1440 /* make sure the database is up to date */
1441 if (print_cache_expired(snum)) print_queue_update(snum);
1443 /* Send a printer notify message */
1445 printer_name = PRINTERNAME(snum);
1447 send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);
1452 /****************************************************************************
1453 Purge a queue - implemented by deleting all jobs that we can delete.
1454 ****************************************************************************/
1456 BOOL print_queue_purge(struct current_user *user, int snum, WERROR *errcode)
1458 print_queue_struct *queue;
1459 print_status_struct status;
1464 /* Force and update so the count is accurate (i.e. not a cached count) */
1465 print_queue_update(snum);
1467 can_job_admin = print_access_check(user, snum, JOB_ACCESS_ADMINISTER);
1468 njobs = print_queue_status(snum, &queue, &status);
1470 for (i=0;i<njobs;i++) {
1471 BOOL owner = is_owner(user, queue[i].job);
1473 if (owner || can_job_admin) {
1474 print_job_delete1(queue[i].job);
1480 /* Send a printer notify message */
1482 printer_name = PRINTERNAME(snum);
1484 send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);