summaryrefslogtreecommitdiffstats
path: root/core/modules/printergui/data/opt/openslx/vmchooser/run-virt.d/setup_printer_lpd.inc
blob: 639b7bca8cba236add9d486ccda57ec430a1f125 (plain) (blame)
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
#####################################
# Include: Setup printer daemon LPD #
#####################################
## Functions ##
setup_virtual_printer() {
	# TODO: QUEUE should prolly not be hardcoded ....
	local QUEUE="STANDARD" # This has to match the queue you configured in your VM
	local SPOOLDIR
	
	### Disabled: 100megs is not enough, some jobs are HUGE, try to use temp which should be on disk
	## Try using user's tmpfs home first, as it gets wiped on logout
	#if [ -n "${HOME}" ] && [ -w "${HOME}" ]; then
	#       SPOOLDIR="${HOME}/.spool"
	#       mkdir -p "${SPOOLDIR}/${QUEUE}"
	#fi
	# If failed, try to fall back to /tmp
	
	if [ -z "${SPOOLDIR}" ] || [ ! -w "${SPOOLDIR}/${QUEUE}" ]; then
		SPOOLDIR="${TMPDIR}/printergui-${RANDOM}"
		rm -rf -- "${SPOOLDIR}"
		if ! mkdir -p "${SPOOLDIR}/${QUEUE}"; then
			writelog "Could not mkdir -p '${SPOOLDIR}/${QUEUE}'"
			notify_user "Drucksystem" "Das Drucksystem konnte nicht initialisiert werden. Druckfunktion nicht verfügbar."
		fi
		chmod 0700 "${SPOOLDIR}/${QUEUE}"
	fi
	
	# Start the lpdaemon listening on the given port
	# TODO: externalize with something like runvirt.d (other parts might benefit from that too)
	tcpsvd -E 192.168.101.1 5515 \
		lpd "$SPOOLDIR" \
		ash -c "/opt/openslx/scripts/run-virt_print '${USER}' \"${SPOOLDIR}/${QUEUE}/\$DATAFILE\"" \
		&> "${TMPDIR}/tcpsvd.log" \
		&
	
	# PID to kill the process
	declare -rg PID_LPD="$!"
	add_cleanup "stop_virtual_printer"
	{
		sleep 2
		# Check if tcpsvd is running. Do this a little delayed so we do not check
		# immediately after trying to spawn it, as this could result in
		# success even if it's not really working.
	
		if ! kill -0 "$PID_LPD"; then
			writelog "Could not start tcpsvd/lpd for virtual machine session:"
			writelog "$( cat "${TMPDIR}/tcpsvd.log" )"
			notify_user "Drucksystem" "Das Drucksystem konnte nicht initialisiert werden. Druckfunktion nicht verfügbar."
		fi
	} &
}

# cleanup helper to stop lpd
stop_virtual_printer() {
	isset PID_LPD && kill "${PID_LPD}"
}

## MAIN ##
# Sanity checks
unset FAIL
if ! check_dep tcpsvd lpd; then
	writelog "One of 'tcpsvd' or 'lpd' could not be found in PATH: $PATH"
	FAIL=y
fi
if [ ! -f "/opt/openslx/scripts/run-virt_print" ]; then
	writelog "File not found: /opt/openslx/scripts/run-virt_print"
	FAIL=y
fi
if isset FAIL; then
	unset FAIL
	notify_user "Drucksystem" "Das Drucksystem konnte nicht initialisiert werden. Druckfunktion nicht verfügbar."
else
	reg_feature_handler "printer" "setup_virtual_printer"
fi