summaryrefslogtreecommitdiff
path: root/source3/printing/printing.c
blob: 7e1f59f59003d4a7aa9e1b4b8de6e8eb6415543e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
/* 
   Unix SMB/CIFS implementation.
   printing backend routines
   Copyright (C) Andrew Tridgell 1992-2000
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "printing.h"

/* Current printer interface */
struct printif *current_printif = &generic_printif;

/* 
   the printing backend revolves around a tdb database that stores the
   SMB view of the print queue 
   
   The key for this database is a jobid - a internally generated number that
   uniquely identifies a print job

   reading the print queue involves two steps:
     - possibly running lpq and updating the internal database from that
     - reading entries from the database

   jobids are assigned when a job starts spooling. 
*/

/* the open printing.tdb database */
static TDB_CONTEXT *tdb;
static pid_t local_pid;

static int get_queue_status(int, print_status_struct *);

/****************************************************************************
 Initialise the printing backend. Called once at startup. 
 Does not survive a fork
****************************************************************************/

BOOL print_backend_init(void)
{
	char *sversion = "INFO/version";

	if (tdb && local_pid == sys_getpid()) return True;
	tdb = tdb_open_log(lock_path("printing.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
	if (!tdb) {
		DEBUG(0,("print_backend_init: Failed to open printing backend database. Error = [%s]\n",
				 tdb_errorstr(tdb)));
		return False;
	}
	local_pid = sys_getpid();

	/* handle a Samba upgrade */
	tdb_lock_bystring(tdb, sversion);
	if (tdb_fetch_int32(tdb, sversion) != PRINT_DATABASE_VERSION) {
		tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
		tdb_store_int32(tdb, sversion, PRINT_DATABASE_VERSION);
	}
	tdb_unlock_bystring(tdb, sversion);

	/* select the appropriate printing interface... */
#ifdef HAVE_CUPS
	if (strcmp(lp_printcapname(), "cups") == 0)
		current_printif = &cups_printif;
#endif /* HAVE_CUPS */

	/* do NT print initialization... */
	return nt_printing_init();
}

/****************************************************************************
useful function to generate a tdb key
****************************************************************************/
static TDB_DATA print_key(int jobid)
{
	static int j;
	TDB_DATA ret;

	j = jobid;
	ret.dptr = (void *)&j;
	ret.dsize = sizeof(j);
	return ret;
}

/****************************************************************************
useful function to find a print job in the database
****************************************************************************/
static struct printjob *print_job_find(int jobid)
{
	static struct printjob pjob;
	TDB_DATA ret;

	ret = tdb_fetch(tdb, print_key(jobid));
	if (!ret.dptr || ret.dsize != sizeof(pjob)) return NULL;

	memcpy(&pjob, ret.dptr, sizeof(pjob));
	free(ret.dptr);
	return &pjob;
}

/****************************************************************************
store a job structure back to the database
****************************************************************************/
static BOOL print_job_store(int jobid, struct printjob *pjob)
{
	TDB_DATA d;
	d.dptr = (void *)pjob;
	d.dsize = sizeof(*pjob);

	return (tdb_store(tdb, print_key(jobid), d, TDB_REPLACE) == 0);
}

/****************************************************************************
parse a file name from the system spooler to generate a jobid
****************************************************************************/
static int print_parse_jobid(char *fname)
{
	int jobid;

	if (strncmp(fname,PRINT_SPOOL_PREFIX,strlen(PRINT_SPOOL_PREFIX)) != 0) return -1;
	fname += strlen(PRINT_SPOOL_PREFIX);

	jobid = atoi(fname);
	if (jobid <= 0) return -1;

	return jobid;
}


/****************************************************************************
list a unix job in the print database
****************************************************************************/
static void print_unix_job(int snum, print_queue_struct *q)
{
	int jobid = q->job + UNIX_JOB_START;
	struct printjob pj, *old_pj;

	/* Preserve the timestamp on an existing unix print job */

	old_pj = print_job_find(jobid);

	ZERO_STRUCT(pj);

	pj.pid = (pid_t)-1;
	pj.sysjob = q->job;
	pj.fd = -1;
	pj.starttime = old_pj ? old_pj->starttime : q->time;
	pj.status = q->status;
	pj.size = q->size;
	pj.spooled = True;
	pj.smbjob = False;
	fstrcpy(pj.filename, "");
	fstrcpy(pj.jobname, q->fs_file);
	fstrcpy(pj.user, q->fs_user);
	pj.snum = snum;

	print_job_store(jobid, &pj);
}


struct traverse_struct {
	print_queue_struct *queue;
	int qcount, snum, maxcount, total_jobs;
};

/* utility fn to delete any jobs that are no longer active */
static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
{
	struct traverse_struct *ts = (struct traverse_struct *)state;
	struct printjob pjob;
	int i, jobid;

	if (data.dsize != sizeof(pjob) || key.dsize != sizeof(int)) return 0;
	memcpy(&jobid, key.dptr, sizeof(jobid));
	memcpy(&pjob,  data.dptr, sizeof(pjob));

	if (ts->snum != pjob.snum) {
		/* this isn't for the queue we are looking at */
		ts->total_jobs++;
		return 0;
	}

	if (!pjob.smbjob) {
		/* remove a unix job if it isn't in the system queue any more */

		for (i=0;i<ts->qcount;i++) {
			if (jobid == ts->queue[i].job + UNIX_JOB_START) break;
		}
		if (i == ts->qcount)
			tdb_delete(tdb, key);
		else
			ts->total_jobs++;
		return 0;
	}

	/* maybe it hasn't been spooled yet */
	if (!pjob.spooled) {
		/* if a job is not spooled and the process doesn't
                   exist then kill it. This cleans up after smbd
                   deaths */
		if (!process_exists(pjob.pid))
			tdb_delete(tdb, key);
		else
			ts->total_jobs++;
		return 0;
	}

	for (i=0;i<ts->qcount;i++) {
		int qid = print_parse_jobid(ts->queue[i].fs_file);
		if (jobid == qid) break;
	}
	
	/* The job isn't in the system queue - we have to assume it has
	   completed, so delete the database entry. */

	if (i == ts->qcount) {
		time_t cur_t = time(NULL);

		/* A race can occur between the time a job is spooled and
		   when it appears in the lpq output.  This happens when
		   the job is added to printing.tdb when another smbd
		   running print_queue_update() has completed a lpq and
		   is currently traversing the printing tdb and deleting jobs.
		   A workaround is to not delete the job if it has been 
		   submitted less than lp_lpqcachetime() seconds ago. */

		if ((cur_t - pjob.starttime) > lp_lpqcachetime())
			tdb_delete(t, key);
		else
			ts->total_jobs++;
	}
	else
		ts->total_jobs++;

	return 0;
}

/****************************************************************************
check if the print queue has been updated recently enough
****************************************************************************/
static void print_cache_flush(int snum)
{
	fstring key;
	slprintf(key, sizeof(key)-1, "CACHE/%s", lp_servicename(snum));
	tdb_store_int32(tdb, key, -1);
}

/****************************************************************************
 Check if someone already thinks they are doing the update.
****************************************************************************/

static pid_t get_updating_pid(fstring printer_name)
{
	fstring keystr;
	TDB_DATA data, key;
	pid_t updating_pid;

	slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
    	key.dptr = keystr;
	key.dsize = strlen(keystr);

	data = tdb_fetch(tdb, key);
	if (!data.dptr || data.dsize != sizeof(pid_t))
		return (pid_t)-1;

	memcpy(&updating_pid, data.dptr, sizeof(pid_t));
	free(data.dptr);

	if (process_exists(updating_pid))
		return updating_pid;

	return (pid_t)-1;
}

/****************************************************************************
 Set the fact that we're doing the update, or have finished doing the update
 in th tdb.
****************************************************************************/

static void set_updating_pid(fstring printer_name, BOOL delete)
{
	fstring keystr;
	TDB_DATA key;
	TDB_DATA data;
	pid_t updating_pid = sys_getpid();

	slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", printer_name);
    	key.dptr = keystr;
	key.dsize = strlen(keystr);

	if (delete) {
		tdb_delete(tdb, key);
		return;
	}
	
	data.dptr = (void *)&updating_pid;
	data.dsize = sizeof(pid_t);

	tdb_store(tdb, key, data, TDB_REPLACE);	
}

/****************************************************************************
 Send a message saying the queue changed.
****************************************************************************/

static void send_queue_message(const char *printer_name, uint32 high, uint32 low)
{
	char msg[8 + sizeof(fstring)];
	SIVAL(msg,0,low);
	SIVAL(msg,4,high);
	fstrcpy(&msg[8], printer_name);

	message_send_all(conn_tdb_ctx(), MSG_PRINTER_NOTIFY, msg, 8 + strlen(printer_name) + 1, False, NULL);
}

/****************************************************************************
update the internal database from the system print queue for a queue in the background
****************************************************************************/

static void print_queue_update_background(int snum)
{
	int i, qcount;
	print_queue_struct *queue = NULL;
	print_status_struct status;
	print_status_struct old_status;
	struct printjob *pjob;
	struct traverse_struct tstruct;
	fstring keystr, printer_name, cachestr;
	TDB_DATA data, key;

	fstrcpy(printer_name, lp_servicename(snum));
	
	/*
	 * Check to see if someone else is doing this update.
	 * This is essentially a mutex on the update.
	 */

	if (get_updating_pid(printer_name) != -1)
		return;

	/* Lock the queue for the database update */

	slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", printer_name);
	tdb_lock_bystring(tdb, keystr);

	/*
	 * Ensure that no one else got in here.
	 * If the updating pid is still -1 then we are
	 * the winner.
	 */

	if (get_updating_pid(printer_name) != -1) {
		/*
		 * Someone else is doing the update, exit.
		 */
		tdb_unlock_bystring(tdb, keystr);
		return;
	}

	/*
	 * We're going to do the update ourselves.
	 */

	/* Tell others we're doing the update. */
	set_updating_pid(printer_name, False);

	/*
	 * Allow others to enter and notice we're doing
	 * the update.
	 */

	tdb_unlock_bystring(tdb, keystr);

	/*
	 * Update the cache time FIRST ! Stops others even
	 * attempting to get the lock and doing this
	 * if the lpq takes a long time.
	 */

	slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", printer_name);
	tdb_store_int32(tdb, cachestr, (int)time(NULL));

        /* get the current queue using the appropriate interface */
	ZERO_STRUCT(status);

	qcount = (*(current_printif->queue_get))(snum, &queue, &status);

	DEBUG(3, ("%d job%s in queue for %s\n", qcount, (qcount != 1) ?
		"s" : "", printer_name));

	/*
	  any job in the internal database that is marked as spooled
	  and doesn't exist in the system queue is considered finished
	  and removed from the database

	  any job in the system database but not in the internal database 
	  is added as a unix job

	  fill in any system job numbers as we go
	*/
	for (i=0; i<qcount; i++) {
		int jobid = print_parse_jobid(queue[i].fs_file);

		if (jobid == -1) {
			/* assume its a unix print job */
			print_unix_job(snum, &queue[i]);
			continue;
		}

		/* we have an active SMB print job - update its status */
		pjob = print_job_find(jobid);
		if (!pjob) {
			/* err, somethings wrong. Probably smbd was restarted
			   with jobs in the queue. All we can do is treat them
			   like unix jobs. Pity. */
			print_unix_job(snum, &queue[i]);
			continue;
		}

		pjob->sysjob = queue[i].job;
		pjob->status = queue[i].status;

		print_job_store(jobid, pjob);
	}

	/* now delete any queued entries that don't appear in the
           system queue */
	tstruct.queue = queue;
	tstruct.qcount = qcount;
	tstruct.snum = snum;
	tstruct.total_jobs = 0;

	tdb_traverse(tdb, traverse_fn_delete, (void *)&tstruct);

	safe_free(tstruct.queue);

	tdb_store_int32(tdb, "INFO/total_jobs", tstruct.total_jobs);

	/*
	 * Get the old print status. We will use this to compare the
	 * number of jobs. If they have changed we need to send a
	 * "changed" message to the smbds.
	 */

	if( qcount != get_queue_status(snum, &old_status)) {
		DEBUG(10,("print_queue_update: queue status change %d jobs -> %d jobs for printer %s\n",
				old_status.qcount, qcount, printer_name ));
		send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);
	}

	/* store the new queue status structure */
	slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", printer_name);
	key.dptr = keystr;
	key.dsize = strlen(keystr);

	status.qcount = qcount;
	data.dptr = (void *)&status;
	data.dsize = sizeof(status);
	tdb_store(tdb, key, data, TDB_REPLACE);	

	/*
	 * Update the cache time again. We want to do this call
	 * as little as possible...
	 */

	slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", printer_name);
	tdb_store_int32(tdb, keystr, (int)time(NULL));

	/* Delete our pid from the db. */
	set_updating_pid(printer_name, True);
}

/****************************************************************************
this is the receive function of the background lpq updater
****************************************************************************/
static void print_queue_receive(int msg_type, pid_t src, void *buf, size_t len)
{
	int snum;
	snum=*((int *)buf);
	print_queue_update_background(snum);
}

static pid_t background_lpq_updater_pid;

/****************************************************************************
main thread of the background lpq updater
****************************************************************************/
void start_background_queue(void)
{
	DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
	background_lpq_updater_pid = sys_fork();

	if (background_lpq_updater_pid == -1) {
		DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
		exit(1);
	}

	if(background_lpq_updater_pid == 0) {
		/* Child. */
		DEBUG(5,("start_background_queue: background LPQ thread started\n"));

		claim_connection(NULL,"smbd lpq backend",0,False);

		if (!locking_init(0))
			exit(1);

		if (!print_backend_init())
			exit(1);

		message_register(MSG_PRINTER_UPDATE, print_queue_receive);
		
		DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
		while (1) {
			pause();
			DEBUG(10,("start_background_queue: background LPQ thread got a message\n"));
			message_dispatch();
		}
	}
}

/****************************************************************************
update the internal database from the system print queue for a queue
****************************************************************************/
static void print_queue_update(int snum)
{
	message_send_pid(background_lpq_updater_pid, MSG_PRINTER_UPDATE, &snum, sizeof(snum), False);
}

/****************************************************************************
check if a jobid is valid. It is valid if it exists in the database
****************************************************************************/
BOOL print_job_exists(int jobid)
{
	return tdb_exists(tdb, print_key(jobid));
}


/****************************************************************************
work out which service a jobid is for
note that we have to look up by queue name to ensure that it works for 
other than the process that started the job
****************************************************************************/
int print_job_snum(int jobid)
{
	struct printjob *pjob = print_job_find(jobid);
	if (!pjob) return -1;

	return pjob->snum;
}

/****************************************************************************
give the fd used for a jobid
****************************************************************************/
int print_job_fd(int jobid)
{
	struct printjob *pjob = print_job_find(jobid);
	if (!pjob) return -1;
	/* don't allow another process to get this info - it is meaningless */
	if (pjob->pid != local_pid) return -1;
	return pjob->fd;
}

/****************************************************************************
give the filename used for a jobid
only valid for the process doing the spooling and when the job
has not been spooled
****************************************************************************/
char *print_job_fname(int jobid)
{
	struct printjob *pjob = print_job_find(jobid);
	if (!pjob || pjob->spooled || pjob->pid != local_pid) return NULL;
	return pjob->filename;
}


/****************************************************************************
set the place in the queue for a job
****************************************************************************/
BOOL print_job_set_place(int jobid, int place)
{
	DEBUG(2,("print_job_set_place not implemented yet\n"));
	return False;
}

/****************************************************************************
set the name of a job. Only possible for owner
****************************************************************************/
BOOL print_job_set_name(int jobid, char *name)
{
	struct printjob *pjob = print_job_find(jobid);
	if (!pjob || pjob->pid != local_pid) return False;

	fstrcpy(pjob->jobname, name);
	return print_job_store(jobid, pjob);
}


/****************************************************************************
delete a print job - don't update queue
****************************************************************************/
static BOOL print_job_delete1(int jobid)
{
	struct printjob *pjob = print_job_find(jobid);
	int snum, result = 0;

	if (!pjob) return False;

	/*
	 * If already deleting just return.
	 */

	if (pjob->status == LPQ_DELETING)
		return True;

	snum = print_job_snum(jobid);

	/* Hrm - we need to be able to cope with deleting a job before it
	   has reached the spooler. */

	if (pjob->sysjob == -1) {
		DEBUG(5, ("attempt to delete job %d not seen by lpr\n",
			  jobid));
	}

	/* Set the tdb entry to be deleting. */

	pjob->status = LPQ_DELETING;
	print_job_store(jobid, pjob);

	if (pjob->spooled && pjob->sysjob != -1)
		result = (*(current_printif->job_delete))(snum, pjob);

	/* Delete the tdb entry if the delete suceeded or the job hasn't
	   been spooled. */

	if (result == 0) {
		tdb_delete(tdb, print_key(jobid));
	}

	return (result == 0);
}

/****************************************************************************
return true if the current user owns the print job
****************************************************************************/
static BOOL is_owner(struct current_user *user, int jobid)
{
	struct printjob *pjob = print_job_find(jobid);
	user_struct *vuser;

	if (!pjob || !user) return False;

	if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
		return strequal(pjob->user, vuser->user.smb_name);
	} else {
		return strequal(pjob->user, uidtoname(user->uid));
	}
}

/****************************************************************************
delete a print job
****************************************************************************/
BOOL print_job_delete(struct current_user *user, int jobid, WERROR *errcode)
{
	int snum = print_job_snum(jobid);
	char *printer_name;
	BOOL owner;
	
	owner = is_owner(user, jobid);
	
	/* Check access against security descriptor or whether the user
	   owns their job. */

	if (!owner && 
	    !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
		DEBUG(3, ("delete denied by security descriptor\n"));
		*errcode = WERR_ACCESS_DENIED;
		return False;
	}

	if (!print_job_delete1(jobid)) return False;

	/* force update the database and say the delete failed if the
           job still exists */

	print_queue_update(snum);

	/* Send a printer notify message */

	printer_name = PRINTERNAME(snum);

	send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);

	return !print_job_exists(jobid);
}


/****************************************************************************
pause a job
****************************************************************************/
BOOL print_job_pause(struct current_user *user, int jobid, WERROR *errcode)
{
	struct printjob *pjob = print_job_find(jobid);
	int snum, ret = -1;
	char *printer_name;
	
	if (!pjob || !user) return False;

	if (!pjob->spooled || pjob->sysjob == -1) return False;

	snum = print_job_snum(jobid);

	if (!is_owner(user, jobid) &&
	    !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
		DEBUG(3, ("pause denied by security descriptor\n"));
		*errcode = WERR_ACCESS_DENIED;
		return False;
	}

	/* need to pause the spooled entry */
	ret = (*(current_printif->job_pause))(snum, pjob);

	if (ret != 0) {
		*errcode = WERR_INVALID_PARAM;
		return False;
	}

	/* force update the database */
	print_cache_flush(snum);

	/* Send a printer notify message */

	printer_name = PRINTERNAME(snum);

	send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);

	/* how do we tell if this succeeded? */

	return True;
}

/****************************************************************************
resume a job
****************************************************************************/
BOOL print_job_resume(struct current_user *user, int jobid, WERROR *errcode)
{
	struct printjob *pjob = print_job_find(jobid);
	char *printer_name;
	int snum, ret;
	
	if (!pjob || !user) return False;

	if (!pjob->spooled || pjob->sysjob == -1) return False;

	snum = print_job_snum(jobid);

	if (!is_owner(user, jobid) &&
	    !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
		DEBUG(3, ("resume denied by security descriptor\n"));
		*errcode = WERR_ACCESS_DENIED;
		return False;
	}

	ret = (*(current_printif->job_resume))(snum, pjob);

	if (ret != 0) {
		*errcode = WERR_INVALID_PARAM;
		return False;
	}

	/* force update the database */
	print_cache_flush(snum);

	/* Send a printer notify message */

	printer_name = PRINTERNAME(snum);

	send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);

	return True;
}

/****************************************************************************
write to a print file
****************************************************************************/
int print_job_write(int jobid, const char *buf, int size)
{
	int return_code;
	struct printjob *pjob = print_job_find(jobid);

	if (!pjob)
		return -1;
	/* don't allow another process to get this info - it is meaningless */
	if (pjob->pid != local_pid)
		return -1;

	return_code = write(pjob->fd, buf, size);
	if (return_code>0) {
		pjob->size += size;
		print_job_store(jobid, pjob);
	}
	return return_code;
}

/****************************************************************************
 Check if the print queue has been updated recently enough.
****************************************************************************/

static BOOL print_cache_expired(int snum)
{
	fstring key;
	time_t last_qscan_time, time_now = time(NULL);

	slprintf(key, sizeof(key), "CACHE/%s", lp_servicename(snum));
	last_qscan_time = (time_t)tdb_fetch_int32(tdb, key);

	/*
	 * Invalidate the queue for 3 reasons.
	 * (1). last queue scan time == -1.
	 * (2). Current time - last queue scan time > allowed cache time.
	 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
	 * This last test picks up machines for which the clock has been moved
	 * forward, an lpq scan done and then the clock moved back. Otherwise
	 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
	 */

	if (last_qscan_time == ((time_t)-1) || (time_now - last_qscan_time) >= lp_lpqcachetime() ||
			last_qscan_time > (time_now + MAX_CACHE_VALID_TIME)) {
		DEBUG(3, ("print cache expired for queue %s \
(last_qscan_time = %d, time now = %d, qcachetime = %d)\n", lp_servicename(snum),
			(int)last_qscan_time, (int)time_now, (int)lp_lpqcachetime() ));
		return True;
	}
	return False;
}

/****************************************************************************
 Get the queue status - do not update if db is out of date.
****************************************************************************/
static int get_queue_status(int snum, print_status_struct *status)
{
	fstring keystr;
	TDB_DATA data, key;

	ZERO_STRUCTP(status);
	slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", lp_servicename(snum));
	key.dptr = keystr;
	key.dsize = strlen(keystr);
	data = tdb_fetch(tdb, key);
	if (data.dptr) {
		if (data.dsize == sizeof(print_status_struct)) {
			memcpy(status, data.dptr, sizeof(print_status_struct));
		}
		free(data.dptr);
	}
	return status->qcount;
}

/****************************************************************************
 Determine the number of jobs in a queue.
****************************************************************************/

int print_queue_length(int snum, print_status_struct *pstatus)
{
	print_status_struct status;
	int len;
 
	/* make sure the database is up to date */
	if (print_cache_expired(snum))
		print_queue_update(snum);
 
	/* also fetch the queue status */
	memset(&status, 0, sizeof(status));
	len = get_queue_status(snum, &status);
	if (pstatus)
		*pstatus = status;
	return len;
}

/****************************************************************************
 Determine the number of jobs in all queues.
****************************************************************************/
static int get_total_jobs(int snum)
{
	int total_jobs;

	/* make sure the database is up to date */
	if (print_cache_expired(snum)) print_queue_update(snum);

	total_jobs = tdb_fetch_int32(tdb, "INFO/total_jobs");
	if (total_jobs >0)
		return total_jobs;
	else
		return 0;
}

/***************************************************************************
start spooling a job - return the jobid
***************************************************************************/
int print_job_start(struct current_user *user, int snum, char *jobname)
{
	int jobid;
	char *path;
	struct printjob pjob;
	int next_jobid;
	user_struct *vuser;
	int njobs = 0;

	errno = 0;

	if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
		DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
		return -1;
	}

	if (!print_time_access_check(snum)) {
		DEBUG(3, ("print_job_start: job start denied by time check\n"));
		return -1;
	}

	path = lp_pathname(snum);

	/* see if we have sufficient disk space */
	if (lp_minprintspace(snum)) {
		SMB_BIG_UINT dspace, dsize;
		if (sys_fsusage(path, &dspace, &dsize) == 0 &&
		    dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
			DEBUG(3, ("print_job_start: disk space check failed.\n"));
			errno = ENOSPC;
			return -1;
		}
	}

	/* for autoloaded printers, check that the printcap entry still exists */
	if (lp_autoloaded(snum) && !pcap_printername_ok(lp_servicename(snum), NULL)) {
		DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_servicename(snum) ));
		errno = ENOENT;
		return -1;
	}

	/* Insure the maximum queue size is not violated */
	if (lp_maxprintjobs(snum) && (njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
		DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per queue (%d).\n",
			njobs, lp_maxprintjobs(snum) ));
		errno = ENOSPC;
		return -1;
	}

	/* Insure the maximum print jobs in the system is not violated */
	if (lp_totalprintjobs() && get_total_jobs(snum) > lp_totalprintjobs()) {
		DEBUG(3, ("print_job_start: number of jobs (%d) larger than max printjobs per system (%d).\n",
			njobs, lp_totalprintjobs() ));
		errno = ENOSPC;
		return -1;
	}

	/* create the database entry */
	ZERO_STRUCT(pjob);
	pjob.pid = local_pid;
	pjob.sysjob = -1;
	pjob.fd = -1;
	pjob.starttime = time(NULL);
	pjob.status = LPQ_SPOOLING;
	pjob.size = 0;
	pjob.spooled = False;
	pjob.smbjob = True;

	fstrcpy(pjob.jobname, jobname);

	if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
		fstrcpy(pjob.user, vuser->user.smb_name);
	} else {
		fstrcpy(pjob.user, uidtoname(user->uid));
	}

	pjob.snum = snum;

	/* lock the database */
	tdb_lock_bystring(tdb, "INFO/nextjob");

	next_jobid = tdb_fetch_int32(tdb, "INFO/nextjob");
	if (next_jobid == -1)
		next_jobid = 1;

	for (jobid = NEXT_JOBID(next_jobid); jobid != next_jobid; jobid = NEXT_JOBID(jobid)) {
		if (!print_job_exists(jobid))
			break;
	}
	if (jobid == next_jobid || !print_job_store(jobid, &pjob)) {
		DEBUG(3, ("print_job_start: either jobid (%d)==next_jobid(%d) or print_job_store failed.\n",
				jobid, next_jobid ));
		jobid = -1;
		goto fail;
	}

	tdb_store_int32(tdb, "INFO/nextjob", jobid);

	/* we have a job entry - now create the spool file */
	slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.6d.XXXXXX", 
		 path, PRINT_SPOOL_PREFIX, jobid);
	pjob.fd = smb_mkstemp(pjob.filename);

	if (pjob.fd == -1) {
		if (errno == EACCES) {
			/* Common setup error, force a report. */
			DEBUG(0, ("print_job_start: insufficient permissions \
to open spool file %s.\n", pjob.filename));
		} else {
			/* Normal case, report at level 3 and above. */
			DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
			DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
		}
		goto fail;
	}

	print_job_store(jobid, &pjob);

	tdb_unlock_bystring(tdb, "INFO/nextjob");

	/*
	 * If the printer is marked as postscript output a leading
	 * file identifier to ensure the file is treated as a raw
	 * postscript file.
	 * This has a similar effect as CtrlD=0 in WIN.INI file.
	 * tim@fsg.com 09/06/94
	 */
	if (lp_postscript(snum)) {
		print_job_write(jobid, "%!\n",3);
	}

	return jobid;

 fail:
	if (jobid != -1) {
		tdb_delete(tdb, print_key(jobid));
	}

	tdb_unlock_bystring(tdb, "INFO/nextjob");

	DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
	return -1;
}

/****************************************************************************
 Update the number of pages spooled to jobid
****************************************************************************/

void print_job_endpage(int jobid)
{
	struct printjob *pjob = print_job_find(jobid);
	if (!pjob)
		return;
	/* don't allow another process to get this info - it is meaningless */
	if (pjob->pid != local_pid)
		return;

	pjob->page_count++;
	print_job_store(jobid, pjob);
}

/****************************************************************************
 Print a file - called on closing the file. This spools the job.
 If normal close is false then we're tearing down the jobs - treat as an
 error.
****************************************************************************/

BOOL print_job_end(int jobid, BOOL normal_close)
{
	struct printjob *pjob = print_job_find(jobid);
	int snum, ret;
	SMB_STRUCT_STAT sbuf;

	if (!pjob)
		return False;

	if (pjob->spooled || pjob->pid != local_pid)
		return False;

	snum = print_job_snum(jobid);

	if (normal_close && (sys_fstat(pjob->fd, &sbuf) == 0)) {
		pjob->size = sbuf.st_size;
		close(pjob->fd);
		pjob->fd = -1;
	} else {

		/* 
		 * Not a normal close or we couldn't stat the job file,
		 * so something has gone wrong. Cleanup.
		 */
		close(pjob->fd);
		pjob->fd = -1;
		DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
		goto fail;
	}

	/* Technically, this is not quit right. If the printer has a separator
	 * page turned on, the NT spooler prints the separator page even if the
	 * print job is 0 bytes. 010215 JRR */
	if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
		/* don't bother spooling empty files or something being deleted. */
		DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
			pjob->filename, pjob->size ? "deleted" : "zero length" ));
		unlink(pjob->filename);
		tdb_delete(tdb, print_key(jobid));
		return True;
	}

	ret = (*(current_printif->job_submit))(snum, pjob);

	if (ret)
		goto fail;

	/* The print job has been sucessfully handed over to the back-end */
	
	pjob->spooled = True;
	pjob->status = LPQ_QUEUED;
	print_job_store(jobid, pjob);
	
	/* make sure the database is up to date */
	if (print_cache_expired(snum))
		print_queue_update(snum);
	
	return True;

fail:

	/* The print job was not succesfully started. Cleanup */
	/* Still need to add proper error return propagation! 010122:JRR */
	unlink(pjob->filename);
	tdb_delete(tdb, print_key(jobid));
	return False;
}

/* utility fn to enumerate the print queue */
static int traverse_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
{
	struct traverse_struct *ts = (struct traverse_struct *)state;
	struct printjob pjob;
	int i, jobid;

	if (data.dsize != sizeof(pjob) || key.dsize != sizeof(int)) return 0;
	memcpy(&jobid, key.dptr, sizeof(jobid));
	memcpy(&pjob,  data.dptr, sizeof(pjob));

	/* maybe it isn't for this queue */
	if (ts->snum != pjob.snum) return 0;

	if (ts->qcount >= ts->maxcount) return 0;

	i = ts->qcount;

	ts->queue[i].job = jobid;
	ts->queue[i].size = pjob.size;
	ts->queue[i].page_count = pjob.page_count;
	ts->queue[i].status = pjob.status;
	ts->queue[i].priority = 1;
	ts->queue[i].time = pjob.starttime;
	fstrcpy(ts->queue[i].fs_user, pjob.user);
	fstrcpy(ts->queue[i].fs_file, pjob.jobname);

	ts->qcount++;

	return 0;
}

struct traverse_count_struct {
	int snum, count;
};

/* utility fn to count the number of entries in the print queue */
static int traverse_count_fn_queue(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
{
	struct traverse_count_struct *ts = (struct traverse_count_struct *)state;
	struct printjob pjob;
	int jobid;

	if (data.dsize != sizeof(pjob) || key.dsize != sizeof(int)) return 0;
	memcpy(&jobid, key.dptr, sizeof(jobid));
	memcpy(&pjob,  data.dptr, sizeof(pjob));

	/* maybe it isn't for this queue */
	if (ts->snum != pjob.snum) return 0;

	ts->count++;

	return 0;
}

/* Sort print jobs by submittal time */

static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
{
	/* Silly cases */

	if (!j1 && !j2) return 0;
	if (!j1) return -1;
	if (!j2) return 1;

	/* Sort on job start time */

	if (j1->time == j2->time) return 0;
	return (j1->time > j2->time) ? 1 : -1;
}

/****************************************************************************
get a printer queue listing
****************************************************************************/
int print_queue_status(int snum, 
		       print_queue_struct **queue,
		       print_status_struct *status)
{
	struct traverse_struct tstruct;
	struct traverse_count_struct tsc;
	fstring keystr;
	TDB_DATA data, key;

	/* make sure the database is up to date */
	if (print_cache_expired(snum)) print_queue_update(snum);

	*queue = NULL;
	
	/*
	 * Fetch the queue status.  We must do this first, as there may
	 * be no jobs in the queue.
	 */
	ZERO_STRUCTP(status);
	slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", lp_servicename(snum));
	key.dptr = keystr;
	key.dsize = strlen(keystr);
	data = tdb_fetch(tdb, key);
	if (data.dptr) {
		if (data.dsize == sizeof(*status)) {
			memcpy(status, data.dptr, sizeof(*status));
		}
		free(data.dptr);
	}

	/*
	 * Now, fetch the print queue information.  We first count the number
	 * of entries, and then only retrieve the queue if necessary.
	 */
	tsc.count = 0;
	tsc.snum = snum;
	
	tdb_traverse(tdb, traverse_count_fn_queue, (void *)&tsc);

	if (tsc.count == 0)
		return 0;

	/* Allocate the queue size. */
	if ((tstruct.queue = (print_queue_struct *)
	     malloc(sizeof(print_queue_struct)*tsc.count))
				== NULL)
		return 0;

	/*
	 * Fill in the queue.
	 * We need maxcount as the queue size may have changed between
	 * the two calls to tdb_traverse.
	 */
	tstruct.qcount = 0;
	tstruct.maxcount = tsc.count;
	tstruct.snum = snum;

	tdb_traverse(tdb, traverse_fn_queue, (void *)&tstruct);

	/* Sort the queue by submission time otherwise they are displayed
	   in hash order. */

	qsort(tstruct.queue, tstruct.qcount, sizeof(print_queue_struct),
	      QSORT_CAST(printjob_comp));

	*queue = tstruct.queue;
	return tstruct.qcount;
}


/****************************************************************************
turn a queue name into a snum
****************************************************************************/
int print_queue_snum(char *qname)
{
	int snum = lp_servicenumber(qname);
	if (snum == -1 || !lp_print_ok(snum)) return -1;
	return snum;
}


/****************************************************************************
 pause a queue
****************************************************************************/
BOOL print_queue_pause(struct current_user *user, int snum, WERROR *errcode)
{
	char *printer_name;
	int ret;
	
	if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
		*errcode = WERR_ACCESS_DENIED;
		return False;
	}

	ret = (*(current_printif->queue_pause))(snum);

	if (ret != 0) {
		*errcode = WERR_INVALID_PARAM;
		return False;
	}

	/* force update the database */
	print_cache_flush(snum);

	/* Send a printer notify message */

	printer_name = PRINTERNAME(snum);

	send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);

	return True;
}

/****************************************************************************
 resume a queue
****************************************************************************/
BOOL print_queue_resume(struct current_user *user, int snum, WERROR *errcode)
{
	char *printer_name;
	int ret;

	if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
		*errcode = WERR_ACCESS_DENIED;
		return False;
	}

	ret = (*(current_printif->queue_resume))(snum);

	if (ret != 0) {
		*errcode = WERR_INVALID_PARAM;
		return False;
	}

	/* make sure the database is up to date */
	if (print_cache_expired(snum)) print_queue_update(snum);

	/* Send a printer notify message */

	printer_name = PRINTERNAME(snum);

	send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);

	return True;
}

/****************************************************************************
 purge a queue - implemented by deleting all jobs that we can delete
****************************************************************************/
BOOL print_queue_purge(struct current_user *user, int snum, WERROR *errcode)
{
	print_queue_struct *queue;
	print_status_struct status;
	char *printer_name;
	int njobs, i;
	BOOL can_job_admin;

	/* Force and update so the count is accurate (i.e. not a cached count) */
	print_queue_update(snum);
	
	can_job_admin = print_access_check(user, snum, JOB_ACCESS_ADMINISTER);
	njobs = print_queue_status(snum, &queue, &status);

	for (i=0;i<njobs;i++) {
		BOOL owner = is_owner(user, queue[i].job);

		if (owner || can_job_admin) {
			print_job_delete1(queue[i].job);
		}
	}

	safe_free(queue);

	/* Send a printer notify message */

	printer_name = PRINTERNAME(snum);

	send_queue_message(printer_name, 0, PRINTER_CHANGE_JOB);

	return True;
}