2 Unix SMB/Netbios implementation.
4 printing backend routines
5 Copyright (C) Andrew Tridgell 1992-2000
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* Current printer interface */
25 struct printif
*current_printif
= &generic_printif
;
28 the printing backend revolves around a tdb database that stores the
29 SMB view of the print queue
31 The key for this database is a jobid - a internally generated number that
32 uniquely identifies a print job
34 reading the print queue involves two steps:
35 - possibly running lpq and updating the internal database from that
36 - reading entries from the database
38 jobids are assigned when a job starts spooling.
41 /* the open printing.tdb database */
42 static TDB_CONTEXT
*tdb
;
43 static pid_t local_pid
;
45 static int get_queue_status(int, print_status_struct
*);
47 /****************************************************************************
48 Initialise the printing backend. Called once at startup.
49 Does not survive a fork
50 ****************************************************************************/
52 BOOL
print_backend_init(void)
54 char *sversion
= "INFO/version";
56 if (tdb
&& local_pid
== sys_getpid())
58 tdb
= tdb_open_log(lock_path("printing.tdb"), 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
60 DEBUG(0,("print_backend_init: Failed to open printing backend database %s.\n",
61 lock_path("printing.tdb") ));
64 local_pid
= sys_getpid();
66 /* handle a Samba upgrade */
67 tdb_lock_bystring(tdb
, sversion
, 0);
68 if (tdb_fetch_int32(tdb
, sversion
) != PRINT_DATABASE_VERSION
) {
69 tdb_traverse(tdb
, tdb_traverse_delete_fn
, NULL
);
70 tdb_store_int32(tdb
, sversion
, PRINT_DATABASE_VERSION
);
72 tdb_unlock_bystring(tdb
, sversion
);
74 /* select the appropriate printing interface... */
76 if (strcmp(lp_printcapname(), "cups") == 0)
77 current_printif
= &cups_printif
;
78 #endif /* HAVE_CUPS */
80 /* do NT print initialization... */
81 return nt_printing_init();
84 /****************************************************************************
85 Useful function to generate a tdb key.
86 ****************************************************************************/
88 static TDB_DATA
print_key(int jobid
)
94 ret
.dptr
= (void *)&j
;
95 ret
.dsize
= sizeof(j
);
99 /****************************************************************************
100 Useful function to find a print job in the database.
101 ****************************************************************************/
103 static struct printjob
*print_job_find(int jobid
)
105 static struct printjob pjob
;
108 ret
= tdb_fetch(tdb
, print_key(jobid
));
109 if (!ret
.dptr
|| ret
.dsize
!= sizeof(pjob
))
112 memcpy(&pjob
, ret
.dptr
, sizeof(pjob
));
114 unix_to_dos(pjob
.queuename
);
118 /****************************************************************************
119 Store a job structure back to the database.
120 ****************************************************************************/
122 static BOOL
print_job_store(int jobid
, struct printjob
*pjob
)
127 dos_to_unix(pjob
->queuename
);
128 d
.dptr
= (void *)pjob
;
129 d
.dsize
= sizeof(*pjob
);
130 ret
= (tdb_store(tdb
, print_key(jobid
), d
, TDB_REPLACE
) == 0);
131 unix_to_dos(pjob
->queuename
);
135 /****************************************************************************
136 Parse a file name from the system spooler to generate a jobid.
137 ****************************************************************************/
139 static int print_parse_jobid(char *fname
)
143 if (strncmp(fname
,PRINT_SPOOL_PREFIX
,strlen(PRINT_SPOOL_PREFIX
)) != 0)
145 fname
+= strlen(PRINT_SPOOL_PREFIX
);
154 /****************************************************************************
155 List a unix job in the print database.
156 ****************************************************************************/
158 static void print_unix_job(int snum
, print_queue_struct
*q
)
160 int jobid
= q
->job
+ UNIX_JOB_START
;
161 struct printjob pj
, *old_pj
;
163 /* Preserve the timestamp on an existing unix print job */
165 old_pj
= print_job_find(jobid
);
172 pj
.starttime
= old_pj
? old_pj
->starttime
: q
->time
;
173 pj
.status
= q
->status
;
177 fstrcpy(pj
.filename
, "");
178 fstrcpy(pj
.jobname
, q
->fs_file
);
179 fstrcpy(pj
.user
, q
->fs_user
);
180 fstrcpy(pj
.queuename
, lp_servicename(snum
));
182 print_job_store(jobid
, &pj
);
186 struct traverse_struct
{
187 print_queue_struct
*queue
;
188 int qcount
, snum
, maxcount
, total_jobs
;
191 /****************************************************************************
192 Utility fn to delete any jobs that are no longer active.
193 ****************************************************************************/
195 static int traverse_fn_delete(TDB_CONTEXT
*t
, TDB_DATA key
, TDB_DATA data
, void *state
)
197 struct traverse_struct
*ts
= (struct traverse_struct
*)state
;
198 struct printjob pjob
;
201 if (data
.dsize
!= sizeof(pjob
) || key
.dsize
!= sizeof(int))
203 memcpy(&jobid
, key
.dptr
, sizeof(jobid
));
204 memcpy(&pjob
, data
.dptr
, sizeof(pjob
));
205 unix_to_dos(pjob
.queuename
);
207 if (ts
->snum
!= lp_servicenumber(pjob
.queuename
)) {
208 /* this isn't for the queue we are looking at */
214 /* remove a unix job if it isn't in the system queue any more */
216 for (i
=0;i
<ts
->qcount
;i
++) {
217 if (jobid
== ts
->queue
[i
].job
+ UNIX_JOB_START
)
221 tdb_delete(tdb
, key
);
227 /* maybe it hasn't been spooled yet */
229 /* if a job is not spooled and the process doesn't
230 exist then kill it. This cleans up after smbd
232 if (!process_exists(pjob
.pid
))
233 tdb_delete(tdb
, key
);
239 for (i
=0;i
<ts
->qcount
;i
++) {
240 int qid
= print_parse_jobid(ts
->queue
[i
].fs_file
);
245 /* The job isn't in the system queue - we have to assume it has
246 completed, so delete the database entry. */
248 if (i
== ts
->qcount
) {
249 time_t cur_t
= time(NULL
);
251 /* A race can occur between the time a job is spooled and
252 when it appears in the lpq output. This happens when
253 the job is added to printing.tdb when another smbd
254 running print_queue_update() has completed a lpq and
255 is currently traversing the printing tdb and deleting jobs.
256 A workaround is to not delete the job if it has been
257 submitted less than lp_lpqcachetime() seconds ago. */
259 if ((cur_t
- pjob
.starttime
) > lp_lpqcachetime())
270 /****************************************************************************
271 Check if the print queue has been updated recently enough.
272 ****************************************************************************/
274 static void print_cache_flush(int snum
)
277 slprintf(key
, sizeof(key
)-1, "CACHE/%s", lp_servicename(snum
));
278 dos_to_unix(key
); /* Convert key to unix-codepage */
279 tdb_store_int32(tdb
, key
, -1);
282 /****************************************************************************
283 Check if someone already thinks they are doing the update.
284 ****************************************************************************/
286 static pid_t
get_updating_pid(fstring printer_name
)
292 slprintf(keystr
, sizeof(keystr
)-1, "UPDATING/%s", printer_name
);
294 key
.dsize
= strlen(keystr
);
296 data
= tdb_fetch(tdb
, key
);
297 if (!data
.dptr
|| data
.dsize
!= sizeof(pid_t
))
300 memcpy(&updating_pid
, data
.dptr
, sizeof(pid_t
));
301 SAFE_FREE(data
.dptr
);
303 if (process_exists(updating_pid
))
309 /****************************************************************************
310 Set the fact that we're doing the update, or have finished doing the update
312 ****************************************************************************/
314 static void set_updating_pid(fstring printer_name
, BOOL
delete)
319 pid_t updating_pid
= sys_getpid();
321 slprintf(keystr
, sizeof(keystr
)-1, "UPDATING/%s", printer_name
);
323 key
.dsize
= strlen(keystr
);
326 tdb_delete(tdb
, key
);
330 data
.dptr
= (void *)&updating_pid
;
331 data
.dsize
= sizeof(pid_t
);
333 tdb_store(tdb
, key
, data
, TDB_REPLACE
);
336 /****************************************************************************
337 Send a message saying the queue changed.
338 ****************************************************************************/
340 static void send_queue_message(const char *printer_name
, uint32 high
, uint32 low
)
342 char msg
[sizeof(PRINTER_MESSAGE_INFO
)];
343 PRINTER_MESSAGE_INFO info
;
350 fstrcpy(info
.printer_name
, printer_name
);
351 memcpy( msg
, &info
, sizeof(PRINTER_MESSAGE_INFO
));
353 message_send_all(conn_tdb_ctx(), MSG_PRINTER_NOTIFY
, msg
, sizeof(PRINTER_MESSAGE_INFO
), False
, NULL
);
356 /****************************************************************************
357 update the internal database from the system print queue for a queue
358 ****************************************************************************/
360 static void print_queue_update(int snum
)
363 print_queue_struct
*queue
= NULL
;
364 print_status_struct status
;
365 print_status_struct old_status
;
366 struct printjob
*pjob
;
367 struct traverse_struct tstruct
;
368 fstring keystr
, printer_name
, cachestr
;
371 /* Convert printer name (i.e. share name) to unix-codepage for all of the
372 * following tdb key generation */
373 fstrcpy(printer_name
, lp_servicename(snum
));
374 dos_to_unix(printer_name
);
377 * Check to see if someone else is doing this update.
378 * This is essentially a mutex on the update.
381 if (get_updating_pid(printer_name
) != -1)
384 /* Lock the queue for the database update */
386 slprintf(keystr
, sizeof(keystr
) - 1, "LOCK/%s", printer_name
);
387 /* Only wait 10 seconds for this. */
388 if (tdb_lock_bystring(tdb
, keystr
, 10) == -1) {
389 DEBUG(0,("print_queue_update: Failed to lock printing database\n" ));
394 * Ensure that no one else got in here.
395 * If the updating pid is still -1 then we are
399 if (get_updating_pid(printer_name
) != -1) {
401 * Someone else is doing the update, exit.
403 tdb_unlock_bystring(tdb
, keystr
);
408 * We're going to do the update ourselves.
411 /* Tell others we're doing the update. */
412 set_updating_pid(printer_name
, False
);
415 * Allow others to enter and notice we're doing
419 tdb_unlock_bystring(tdb
, keystr
);
422 * Update the cache time FIRST ! Stops others even
423 * attempting to get the lock and doing this
424 * if the lpq takes a long time.
427 slprintf(cachestr
, sizeof(cachestr
)-1, "CACHE/%s", printer_name
);
428 tdb_store_int32(tdb
, cachestr
, (int)time(NULL
));
430 /* get the current queue using the appropriate interface */
433 qcount
= (*(current_printif
->queue_get
))(snum
, &queue
, &status
);
435 DEBUG(3, ("%d job%s in queue for %s\n", qcount
, (qcount
!= 1) ?
436 "s" : "", printer_name
));
439 any job in the internal database that is marked as spooled
440 and doesn't exist in the system queue is considered finished
441 and removed from the database
443 any job in the system database but not in the internal database
444 is added as a unix job
446 fill in any system job numbers as we go
448 for (i
=0; i
<qcount
; i
++) {
449 int jobid
= print_parse_jobid(queue
[i
].fs_file
);
452 /* assume its a unix print job */
453 print_unix_job(snum
, &queue
[i
]);
457 /* we have an active SMB print job - update its status */
458 pjob
= print_job_find(jobid
);
460 /* err, somethings wrong. Probably smbd was restarted
461 with jobs in the queue. All we can do is treat them
462 like unix jobs. Pity. */
463 print_unix_job(snum
, &queue
[i
]);
467 pjob
->sysjob
= queue
[i
].job
;
468 pjob
->status
= queue
[i
].status
;
470 print_job_store(jobid
, pjob
);
473 /* now delete any queued entries that don't appear in the
475 tstruct
.queue
= queue
;
476 tstruct
.qcount
= qcount
;
478 tstruct
.total_jobs
= 0;
480 tdb_traverse(tdb
, traverse_fn_delete
, (void *)&tstruct
);
482 safe_free(tstruct
.queue
);
484 tdb_store_int32(tdb
, "INFO/total_jobs", tstruct
.total_jobs
);
487 * Get the old print status. We will use this to compare the
488 * number of jobs. If they have changed we need to send a
489 * "changed" message to the smbds.
492 if( qcount
!= get_queue_status(snum
, &old_status
)) {
493 DEBUG(10,("print_queue_update: queue status change %d jobs -> %d jobs for printer %s\n",
494 old_status
.qcount
, qcount
, printer_name
));
495 send_queue_message(printer_name
, 0, PRINTER_CHANGE_JOB
);
498 /* store the new queue status structure */
499 slprintf(keystr
, sizeof(keystr
)-1, "STATUS/%s", printer_name
);
501 key
.dsize
= strlen(keystr
);
503 status
.qcount
= qcount
;
504 data
.dptr
= (void *)&status
;
505 data
.dsize
= sizeof(status
);
506 tdb_store(tdb
, key
, data
, TDB_REPLACE
);
509 * Update the cache time again. We want to do this call
510 * as little as possible...
513 slprintf(keystr
, sizeof(keystr
)-1, "CACHE/%s", printer_name
);
514 tdb_store_int32(tdb
, keystr
, (int32
)time(NULL
));
516 /* Delete our pid from the db. */
517 set_updating_pid(printer_name
, True
);
520 /****************************************************************************
521 Check if a jobid is valid. It is valid if it exists in the database.
522 ****************************************************************************/
524 BOOL
print_job_exists(int jobid
)
526 return tdb_exists(tdb
, print_key(jobid
));
529 /****************************************************************************
530 Work out which service a jobid is for.
531 Note that we have to look up by queue name to ensure that it works for
532 other than the process that started the job.
533 ****************************************************************************/
535 int print_job_snum(int jobid
)
537 struct printjob
*pjob
= print_job_find(jobid
);
541 return find_service(pjob
->queuename
);
544 /****************************************************************************
545 Give the fd used for a jobid.
546 ****************************************************************************/
548 int print_job_fd(int jobid
)
550 struct printjob
*pjob
= print_job_find(jobid
);
553 /* don't allow another process to get this info - it is meaningless */
554 if (pjob
->pid
!= local_pid
)
559 /****************************************************************************
560 Give the filename used for a jobid.
561 Only valid for the process doing the spooling and when the job
562 has not been spooled.
563 ****************************************************************************/
565 char *print_job_fname(int jobid
)
567 struct printjob
*pjob
= print_job_find(jobid
);
568 if (!pjob
|| pjob
->spooled
|| pjob
->pid
!= local_pid
)
570 return pjob
->filename
;
573 /****************************************************************************
574 Set the place in the queue for a job.
575 ****************************************************************************/
577 BOOL
print_job_set_place(int jobid
, int place
)
579 DEBUG(2,("print_job_set_place not implemented yet\n"));
583 /****************************************************************************
584 Set the name of a job. Only possible for owner.
585 ****************************************************************************/
587 BOOL
print_job_set_name(int jobid
, char *name
)
589 struct printjob
*pjob
= print_job_find(jobid
);
590 if (!pjob
|| pjob
->pid
!= local_pid
)
593 fstrcpy(pjob
->jobname
, name
);
594 return print_job_store(jobid
, pjob
);
597 /****************************************************************************
598 Delete a print job - don't update queue.
599 ****************************************************************************/
601 static BOOL
print_job_delete1(int jobid
)
603 struct printjob
*pjob
= print_job_find(jobid
);
604 int snum
, result
= 0;
610 * If already deleting just return.
613 if (pjob
->status
== LPQ_DELETING
)
616 snum
= print_job_snum(jobid
);
618 DEBUG(5,("print_job_delete1: unknown service number for jobid %d\n", jobid
));
622 /* Hrm - we need to be able to cope with deleting a job before it
623 has reached the spooler. */
625 if (pjob
->sysjob
== -1) {
626 DEBUG(5, ("attempt to delete job %d not seen by lpr\n", jobid
));
629 /* Set the tdb entry to be deleting. */
631 pjob
->status
= LPQ_DELETING
;
632 print_job_store(jobid
, pjob
);
634 if (pjob
->spooled
&& pjob
->sysjob
!= -1)
635 result
= (*(current_printif
->job_delete
))(snum
, pjob
);
637 /* Delete the tdb entry if the delete suceeded or the job hasn't
641 tdb_delete(tdb
, print_key(jobid
));
644 return (result
== 0);
647 /****************************************************************************
648 Return true if the current user owns the print job.
649 ****************************************************************************/
651 static BOOL
is_owner(struct current_user
*user
, int jobid
)
653 struct printjob
*pjob
= print_job_find(jobid
);
659 if ((vuser
= get_valid_user_struct(user
->vuid
)) != NULL
) {
660 return strequal(pjob
->user
,
661 unix_to_dos_static(vuser
->user
.smb_name
));
663 return strequal(pjob
->user
,
664 unix_to_dos_static(uidtoname(user
->uid
)));
668 /****************************************************************************
670 ****************************************************************************/
672 BOOL
print_job_delete(struct current_user
*user
, int jobid
, WERROR
*errcode
)
674 int snum
= print_job_snum(jobid
);
679 DEBUG(5,("print_job_delete: unknown service number for jobid %d\n", jobid
));
683 owner
= is_owner(user
, jobid
);
685 /* Check access against security descriptor or whether the user
689 !print_access_check(user
, snum
, JOB_ACCESS_ADMINISTER
)) {
690 DEBUG(3, ("delete denied by security descriptor\n"));
691 *errcode
= WERR_ACCESS_DENIED
;
695 if (!print_job_delete1(jobid
))
698 /* force update the database and say the delete failed if the
701 print_queue_update(snum
);
703 /* Send a printer notify message */
705 printer_name
= PRINTERNAME(snum
);
707 send_queue_message(printer_name
, 0, PRINTER_CHANGE_JOB
);
709 return !print_job_exists(jobid
);
712 /****************************************************************************
714 ****************************************************************************/
716 BOOL
print_job_pause(struct current_user
*user
, int jobid
, WERROR
*errcode
)
718 struct printjob
*pjob
= print_job_find(jobid
);
725 if (!pjob
->spooled
|| pjob
->sysjob
== -1)
728 snum
= print_job_snum(jobid
);
730 DEBUG(5,("print_job_pause: unknown service number for jobid %d\n", jobid
));
734 if (!is_owner(user
, jobid
) &&
735 !print_access_check(user
, snum
, JOB_ACCESS_ADMINISTER
)) {
736 DEBUG(3, ("pause denied by security descriptor\n"));
737 *errcode
= WERR_ACCESS_DENIED
;
741 /* need to pause the spooled entry */
742 ret
= (*(current_printif
->job_pause
))(snum
, pjob
);
745 *errcode
= WERR_INVALID_PARAM
;
749 /* force update the database */
750 print_cache_flush(snum
);
752 /* Send a printer notify message */
754 printer_name
= PRINTERNAME(snum
);
756 send_queue_message(printer_name
, 0, PRINTER_CHANGE_JOB
);
758 /* how do we tell if this succeeded? */
763 /****************************************************************************
765 ****************************************************************************/
767 BOOL
print_job_resume(struct current_user
*user
, int jobid
, WERROR
*errcode
)
769 struct printjob
*pjob
= print_job_find(jobid
);
776 if (!pjob
->spooled
|| pjob
->sysjob
== -1)
779 snum
= print_job_snum(jobid
);
781 DEBUG(5,("print_job_resume: unknown service number for jobid %d\n", jobid
));
785 if (!is_owner(user
, jobid
) &&
786 !print_access_check(user
, snum
, JOB_ACCESS_ADMINISTER
)) {
787 DEBUG(3, ("resume denied by security descriptor\n"));
788 *errcode
= WERR_ACCESS_DENIED
;
792 ret
= (*(current_printif
->job_resume
))(snum
, pjob
);
795 *errcode
= WERR_INVALID_PARAM
;
799 /* force update the database */
800 print_cache_flush(snum
);
802 /* Send a printer notify message */
804 printer_name
= PRINTERNAME(snum
);
806 send_queue_message(printer_name
, 0, PRINTER_CHANGE_JOB
);
811 /****************************************************************************
812 Write to a print file.
813 ****************************************************************************/
815 int print_job_write(int jobid
, const char *buf
, int size
)
818 struct printjob
*pjob
= print_job_find(jobid
);
822 /* don't allow another process to get this info - it is meaningless */
823 if (pjob
->pid
!= local_pid
)
826 return_code
= write(pjob
->fd
, buf
, size
);
829 print_job_store(jobid
, pjob
);
834 /****************************************************************************
835 Check if the print queue has been updated recently enough.
836 ****************************************************************************/
838 static BOOL
print_cache_expired(int snum
)
841 time_t last_qscan_time
, time_now
= time(NULL
);
843 slprintf(key
, sizeof(key
), "CACHE/%s", lp_servicename(snum
));
844 dos_to_unix(key
); /* Convert key to unix-codepage */
845 last_qscan_time
= (time_t)tdb_fetch_int32(tdb
, key
);
848 * Invalidate the queue for 3 reasons.
849 * (1). last queue scan time == -1.
850 * (2). Current time - last queue scan time > allowed cache time.
851 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
852 * This last test picks up machines for which the clock has been moved
853 * forward, an lpq scan done and then the clock moved back. Otherwise
854 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
857 if (last_qscan_time
== ((time_t)-1) || (time_now
- last_qscan_time
) >= lp_lpqcachetime() ||
858 last_qscan_time
> (time_now
+ MAX_CACHE_VALID_TIME
)) {
859 DEBUG(3, ("print cache expired for queue %s \
860 (last_qscan_time = %d, time now = %d, qcachetime = %d)\n", lp_servicename(snum
),
861 (int)last_qscan_time
, (int)time_now
, (int)lp_lpqcachetime() ));
867 /****************************************************************************
868 Get the queue status - do not update if db is out of date.
869 ****************************************************************************/
871 static int get_queue_status(int snum
, print_status_struct
*status
)
876 ZERO_STRUCTP(status
);
877 slprintf(keystr
, sizeof(keystr
)-1, "STATUS/%s", lp_servicename(snum
));
878 dos_to_unix(keystr
); /* Convert key to unix-codepage */
880 key
.dsize
= strlen(keystr
);
881 data
= tdb_fetch(tdb
, key
);
883 if (data
.dsize
== sizeof(print_status_struct
)) {
884 memcpy(status
, data
.dptr
, sizeof(print_status_struct
));
886 SAFE_FREE(data
.dptr
);
888 return status
->qcount
;
891 /****************************************************************************
892 Determine the number of jobs in a queue.
893 ****************************************************************************/
895 int print_queue_length(int snum
, print_status_struct
*pstatus
)
897 print_status_struct status
;
900 /* make sure the database is up to date */
901 if (print_cache_expired(snum
))
902 print_queue_update(snum
);
904 /* also fetch the queue status */
905 memset(&status
, 0, sizeof(status
));
906 len
= get_queue_status(snum
, &status
);
912 /****************************************************************************
913 Determine the number of jobs in all queues.
914 ****************************************************************************/
916 static int get_total_jobs(int snum
)
920 /* make sure the database is up to date */
921 if (print_cache_expired(snum
))
922 print_queue_update(snum
);
924 total_jobs
= tdb_fetch_int32(tdb
, "INFO/total_jobs");
931 /***************************************************************************
932 Start spooling a job - return the jobid.
933 ***************************************************************************/
935 int print_job_start(struct current_user
*user
, int snum
, char *jobname
)
939 struct printjob pjob
;
946 if (!print_access_check(user
, snum
, PRINTER_ACCESS_USE
)) {
947 DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
951 if (!print_time_access_check(snum
)) {
952 DEBUG(3, ("print_job_start: job start denied by time check\n"));
956 path
= lp_pathname(snum
);
958 /* see if we have sufficient disk space */
959 if (lp_minprintspace(snum
)) {
960 SMB_BIG_UINT dspace
, dsize
;
961 if (sys_fsusage(path
, &dspace
, &dsize
) == 0 &&
962 dspace
< 2*(SMB_BIG_UINT
)lp_minprintspace(snum
)) {
963 DEBUG(3, ("print_job_start: disk space check failed.\n"));
969 /* for autoloaded printers, check that the printcap entry still exists */
970 if (lp_autoloaded(snum
) && !pcap_printername_ok(lp_servicename(snum
), NULL
)) {
971 DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_servicename(snum
) ));
976 /* Insure the maximum queue size is not violated */
977 if (lp_maxprintjobs(snum
) && (njobs
= print_queue_length(snum
,NULL
)) > lp_maxprintjobs(snum
)) {
978 DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per queue (%d).\n",
979 njobs
, lp_maxprintjobs(snum
) ));
984 /* Insure the maximum print jobs in the system is not violated */
985 if (lp_totalprintjobs() && get_total_jobs(snum
) > lp_totalprintjobs()) {
986 DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per system (%d).\n",
987 njobs
, lp_totalprintjobs() ));
992 /* create the database entry */
994 pjob
.pid
= local_pid
;
997 pjob
.starttime
= time(NULL
);
998 pjob
.status
= LPQ_SPOOLING
;
1000 pjob
.spooled
= False
;
1003 fstrcpy(pjob
.jobname
, jobname
);
1005 if ((vuser
= get_valid_user_struct(user
->vuid
)) != NULL
) {
1006 fstrcpy(pjob
.user
, unix_to_dos_static(vuser
->user
.smb_name
));
1008 fstrcpy(pjob
.user
, unix_to_dos_static(uidtoname(user
->uid
)));
1011 fstrcpy(pjob
.queuename
, lp_servicename(snum
));
1013 /* Lock the database - only wait 20 seconds. */
1014 if (tdb_lock_bystring(tdb
, "INFO/nextjob", 20) == -1) {
1015 DEBUG(0,("print_job_start: failed to lock printing database.\n"));
1019 next_jobid
= tdb_fetch_int32(tdb
, "INFO/nextjob");
1020 if (next_jobid
== -1)
1023 for (jobid
= NEXT_JOBID(next_jobid
); jobid
!= next_jobid
; jobid
= NEXT_JOBID(jobid
)) {
1024 if (!print_job_exists(jobid
))
1027 if (jobid
== next_jobid
|| !print_job_store(jobid
, &pjob
)) {
1028 DEBUG(3, ("print_job_start: either jobid (%d)==next_jobid(%d) or print_job_store failed.\n",
1029 jobid
, next_jobid
));
1034 tdb_store_int32(tdb
, "INFO/nextjob", jobid
);
1036 /* we have a job entry - now create the spool file */
1037 slprintf(pjob
.filename
, sizeof(pjob
.filename
)-1, "%s/%s%.6d.XXXXXX",
1038 path
, PRINT_SPOOL_PREFIX
, jobid
);
1039 pjob
.fd
= smb_mkstemp(pjob
.filename
);
1041 if (pjob
.fd
== -1) {
1042 if (errno
== EACCES
) {
1043 /* Common setup error, force a report. */
1044 DEBUG(0, ("print_job_start: insufficient permissions \
1045 to open spool file %s.\n", pjob
.filename
));
1047 /* Normal case, report at level 3 and above. */
1048 DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob
.filename
));
1049 DEBUGADD(3, ("errno = %d (%s).\n", errno
, strerror(errno
)));
1054 print_job_store(jobid
, &pjob
);
1056 tdb_unlock_bystring(tdb
, "INFO/nextjob");
1059 * If the printer is marked as postscript output a leading
1060 * file identifier to ensure the file is treated as a raw
1062 * This has a similar effect as CtrlD=0 in WIN.INI file.
1063 * tim@fsg.com 09/06/94
1065 if (lp_postscript(snum
)) {
1066 print_job_write(jobid
, "%!\n",3);
1073 tdb_delete(tdb
, print_key(jobid
));
1076 tdb_unlock_bystring(tdb
, "INFO/nextjob");
1078 DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno
) ));
1082 /****************************************************************************
1083 Update the number of pages spooled to jobid
1084 ****************************************************************************/
1086 void print_job_endpage(int jobid
)
1088 struct printjob
*pjob
= print_job_find(jobid
);
1091 /* don't allow another process to get this info - it is meaningless */
1092 if (pjob
->pid
!= local_pid
)
1096 print_job_store(jobid
, pjob
);
1099 /****************************************************************************
1100 Print a file - called on closing the file. This spools the job.
1101 If normal close is false then we're tearing down the jobs - treat as an
1103 ****************************************************************************/
1105 BOOL
print_job_end(int jobid
, BOOL normal_close
)
1107 struct printjob
*pjob
= print_job_find(jobid
);
1109 SMB_STRUCT_STAT sbuf
;
1114 if (pjob
->spooled
|| pjob
->pid
!= local_pid
)
1117 snum
= print_job_snum(jobid
);
1119 DEBUG(5,("print_job_end: unknown service number for jobid %d\n", jobid
));
1123 if (normal_close
&& (sys_fstat(pjob
->fd
, &sbuf
) == 0)) {
1124 pjob
->size
= sbuf
.st_size
;
1130 * Not a normal close or we couldn't stat the job file,
1131 * so something has gone wrong. Cleanup.
1135 DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid
));
1139 /* Technically, this is not quit right. If the printer has a separator
1140 * page turned on, the NT spooler prints the separator page even if the
1141 * print job is 0 bytes. 010215 JRR */
1142 if (pjob
->size
== 0 || pjob
->status
== LPQ_DELETING
) {
1143 /* don't bother spooling empty files or something being deleted. */
1144 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
1145 pjob
->filename
, pjob
->size
? "deleted" : "zero length" ));
1146 unlink(pjob
->filename
);
1147 tdb_delete(tdb
, print_key(jobid
));
1151 ret
= (*(current_printif
->job_submit
))(snum
, pjob
);
1156 /* The print job has been sucessfully handed over to the back-end */
1158 pjob
->spooled
= True
;
1159 pjob
->status
= LPQ_QUEUED
;
1160 print_job_store(jobid
, pjob
);
1162 /* make sure the database is up to date */
1163 if (print_cache_expired(snum
))
1164 print_queue_update(snum
);
1170 /* The print job was not succesfully started. Cleanup */
1171 /* Still need to add proper error return propagation! 010122:JRR */
1172 unlink(pjob
->filename
);
1173 tdb_delete(tdb
, print_key(jobid
));
1177 /****************************************************************************
1178 Utility fn to enumerate the print queue.
1179 ****************************************************************************/
1181 static int traverse_fn_queue(TDB_CONTEXT
*t
, TDB_DATA key
, TDB_DATA data
, void *state
)
1183 struct traverse_struct
*ts
= (struct traverse_struct
*)state
;
1184 struct printjob pjob
;
1187 if (data
.dsize
!= sizeof(pjob
) || key
.dsize
!= sizeof(int))
1189 memcpy(&jobid
, key
.dptr
, sizeof(jobid
));
1190 memcpy(&pjob
, data
.dptr
, sizeof(pjob
));
1191 unix_to_dos(pjob
.queuename
);
1193 /* maybe it isn't for this queue */
1194 if (ts
->snum
!= lp_servicenumber(pjob
.queuename
))
1197 if (ts
->qcount
>= ts
->maxcount
)
1202 ts
->queue
[i
].job
= jobid
;
1203 ts
->queue
[i
].size
= pjob
.size
;
1204 ts
->queue
[i
].page_count
= pjob
.page_count
;
1205 ts
->queue
[i
].status
= pjob
.status
;
1206 ts
->queue
[i
].priority
= 1;
1207 ts
->queue
[i
].time
= pjob
.starttime
;
1208 fstrcpy(ts
->queue
[i
].fs_user
, pjob
.user
);
1209 fstrcpy(ts
->queue
[i
].fs_file
, pjob
.jobname
);
1216 struct traverse_count_struct
{
1220 /****************************************************************************
1221 Utility fn to count the number of entries in the print queue.
1222 ****************************************************************************/
1224 static int traverse_count_fn_queue(TDB_CONTEXT
*t
, TDB_DATA key
, TDB_DATA data
, void *state
)
1226 struct traverse_count_struct
*ts
= (struct traverse_count_struct
*)state
;
1227 struct printjob pjob
;
1230 if (data
.dsize
!= sizeof(pjob
) || key
.dsize
!= sizeof(int))
1232 memcpy(&jobid
, key
.dptr
, sizeof(jobid
));
1233 memcpy(&pjob
, data
.dptr
, sizeof(pjob
));
1234 unix_to_dos(pjob
.queuename
);
1236 /* maybe it isn't for this queue */
1237 if (ts
->snum
!= lp_servicenumber(pjob
.queuename
))
1245 /****************************************************************************
1246 Sort print jobs by submittal time.
1247 ****************************************************************************/
1249 static int printjob_comp(print_queue_struct
*j1
, print_queue_struct
*j2
)
1260 /* Sort on job start time */
1262 if (j1
->time
== j2
->time
)
1264 return (j1
->time
> j2
->time
) ? 1 : -1;
1267 /****************************************************************************
1268 Get a printer queue listing.
1269 ****************************************************************************/
1271 int print_queue_status(int snum
,
1272 print_queue_struct
**queue
,
1273 print_status_struct
*status
)
1275 struct traverse_struct tstruct
;
1276 struct traverse_count_struct tsc
;
1280 /* make sure the database is up to date */
1281 if (print_cache_expired(snum
))
1282 print_queue_update(snum
);
1287 * Fetch the queue status. We must do this first, as there may
1288 * be no jobs in the queue.
1290 ZERO_STRUCTP(status
);
1291 slprintf(keystr
, sizeof(keystr
)-1, "STATUS/%s", lp_servicename(snum
));
1292 dos_to_unix(keystr
); /* Convert key to unix-codepage */
1294 key
.dsize
= strlen(keystr
);
1295 data
= tdb_fetch(tdb
, key
);
1297 if (data
.dsize
== sizeof(*status
)) {
1298 memcpy(status
, data
.dptr
, sizeof(*status
));
1300 SAFE_FREE(data
.dptr
);
1304 * Now, fetch the print queue information. We first count the number
1305 * of entries, and then only retrieve the queue if necessary.
1310 tdb_traverse(tdb
, traverse_count_fn_queue
, (void *)&tsc
);
1315 /* Allocate the queue size. */
1316 if ((tstruct
.queue
= (print_queue_struct
*)
1317 malloc(sizeof(print_queue_struct
)*tsc
.count
)) == NULL
)
1321 * Fill in the queue.
1322 * We need maxcount as the queue size may have changed between
1323 * the two calls to tdb_traverse.
1326 tstruct
.maxcount
= tsc
.count
;
1327 tstruct
.snum
= snum
;
1329 tdb_traverse(tdb
, traverse_fn_queue
, (void *)&tstruct
);
1331 /* Sort the queue by submission time otherwise they are displayed
1334 qsort(tstruct
.queue
, tstruct
.qcount
, sizeof(print_queue_struct
),
1335 QSORT_CAST(printjob_comp
));
1337 *queue
= tstruct
.queue
;
1338 return tstruct
.qcount
;
1341 /****************************************************************************
1342 Turn a queue name into a snum.
1343 ****************************************************************************/
1345 int print_queue_snum(char *qname
)
1347 int snum
= lp_servicenumber(qname
);
1348 if (snum
== -1 || !lp_print_ok(snum
))
1353 /****************************************************************************
1355 ****************************************************************************/
1357 BOOL
print_queue_pause(struct current_user
*user
, int snum
, WERROR
*errcode
)
1362 if (!print_access_check(user
, snum
, PRINTER_ACCESS_ADMINISTER
)) {
1363 *errcode
= WERR_ACCESS_DENIED
;
1367 ret
= (*(current_printif
->queue_pause
))(snum
);
1370 *errcode
= WERR_INVALID_PARAM
;
1374 /* force update the database */
1375 print_cache_flush(snum
);
1377 /* Send a printer notify message */
1379 printer_name
= PRINTERNAME(snum
);
1381 send_queue_message(printer_name
, 0, PRINTER_CHANGE_JOB
);
1386 /****************************************************************************
1388 ****************************************************************************/
1390 BOOL
print_queue_resume(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_resume
))(snum
);
1403 *errcode
= WERR_INVALID_PARAM
;
1407 /* make sure the database is up to date */
1408 if (print_cache_expired(snum
)) print_queue_update(snum
);
1410 /* Send a printer notify message */
1412 printer_name
= PRINTERNAME(snum
);
1414 send_queue_message(printer_name
, 0, PRINTER_CHANGE_JOB
);
1419 /****************************************************************************
1420 Purge a queue - implemented by deleting all jobs that we can delete.
1421 ****************************************************************************/
1423 BOOL
print_queue_purge(struct current_user
*user
, int snum
, WERROR
*errcode
)
1425 print_queue_struct
*queue
;
1426 print_status_struct status
;
1431 /* Force and update so the count is accurate (i.e. not a cached count) */
1432 print_queue_update(snum
);
1434 can_job_admin
= print_access_check(user
, snum
, JOB_ACCESS_ADMINISTER
);
1435 njobs
= print_queue_status(snum
, &queue
, &status
);
1437 for (i
=0;i
<njobs
;i
++) {
1438 BOOL owner
= is_owner(user
, queue
[i
].job
);
1440 if (owner
|| can_job_admin
) {
1441 print_job_delete1(queue
[i
].job
);
1447 /* Send a printer notify message */
1449 printer_name
= PRINTERNAME(snum
);
1451 send_queue_message(printer_name
, 0, PRINTER_CHANGE_JOB
);