summaryrefslogtreecommitdiff
blob: a757bf36e907b88f44eab8c0610ee8adaf1accbd (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
#!/sbin/runscript

opts="reload slow fast info"

MLDONKEY_BINARY=${MLDONKEY_BINARY:-/usr/bin/mlnet}
MLDONKEY_TIMEOUT=${TIMEOUT:-10}

depend() {
	need localmount net
	${USE_LOGGER} && use logger
}

start() {
	if [ -z "${MLDONKEY_DIR}" ]; then
		ewarn "mldonkey's start script has been changed. You should remove"
		ewarn "BASEDIR and SUBDIR from /etc/conf.d/mldonkey and set MLDONKEY_DIR"
		ewarn "to the correct value (you probably want"
		ewarn "MLDONKEY_DIR=${BASEDIR}/${SUBDIR})"
		MLDONKEY_DIR="${BASEDIR}/${SUBDIR}"
		einfo "Using ${MLDONKEY_DIR} as working directory"
	fi

	if [ ! -d "${MLDONKEY_DIR}" ]; then
		ebegin "Directory ${MLDONKEY_DIR} not existing, trying to create..."
		mkdir -p "${MLDONKEY_DIR}" && chown ${USER}:users "${MLDONKEY_DIR}"
		eend $? || return 1
	fi

	ebegin "Starting ${SVCNAME}"
	cd "${MLDONKEY_DIR}"
	export MLDONKEY_DIR
	start-stop-daemon --start --chuid "${USER}" --nice "${NICE}" \
		--exec "${MLDONKEY_BINARY}" --pidfile /var/run/"${SVCNAME}".pid \
		--background --make-pidfile
	eend $?
}

setup_uri() {
	BASE="http://"
	if [ -n "${USERNAME}" -a -n "${PASSWORD}" ]; then
		BASE="${BASE}${USERNAME}:${PASSWORD}@"
	fi
	BASE="${BASE}${SERVER}:${PORT}"
}

stop() {
	ebegin "Stopping ${SVCNAME} -- please wait"

	setup_uri
	wget --spider --timeout="${MLDONKEY_TIMEOUT}" "${BASE}"/submit?q=close_fds -q
	wget --spider --timeout="${MLDONKEY_TIMEOUT}" "${BASE}"/submit?q=save -q
	wget --spider --timeout="${MLDONKEY_TIMEOUT}" "${BASE}"/submit?q=kill -q

	# give it a chance to die:
	local timeout=${MLDONKEY_TIMEOUT}
	while [ $timeout -gt 0 ]; do
		if ! start-stop-daemon --test --quiet --stop \
			--exec "${MLDONKEY_BINARY}" \
			--pidfile /var/run/"${SVCNAME}".pid ; then
			eend 0
			return 0
		fi
		sleep 1
		timeout=$((${timeout} - 1))
	done

	eend 1 "Failed to cleanly stop ${SVCNAME}"
	ebegin "Forcing ${SVCNAME} to stop"
	start-stop-daemon --stop --exec "${MLDONKEY_BINARY}" \
		--pidfile /var/run/"${SVCNAME}".pid
	eend $?
}

# This doesn't work for baselayout-2
restart() {
	svc_stop
	sleep 5
	svc_start
}

reload() {
	if ! service_started "${SVCNAME}"; then
		eerror "${SVCNAME} is not started"
		return 1
	fi

	ebegin "Reloading ${SVCNAME}"
	start-stop-daemon --stop --signal HUP --oknodo \
		--exec "${MLDONKEY_BINARY}" --pidfile /var/run/"${SVCNAME}".pid
	eend $?
}

slow() {
	if ! service_started "${SVCNAME}"; then
		eerror "${SVCNAME} is not started"
		return 1
	fi

	ebegin "Reducing bandwidth to ${LOW_DOWN}k/${LOW_UP}k"
	setup_uri
	wget --spider --timeout="${MLDONKEY_TIMEOUT}" \
		"${BASE}/submit?q=set+max_hard_download_rate+${LOW_DOWN}" -q
	wget --spider --timeout=${MLDONKEY_TIMEOUT} \
		"${BASE}/submit?q=set+max_hard_upload_rate+${LOW_UP}" -q
	eend $?
}

fast() {
	if ! service_started "${SVCNAME}"; then
		eerror "${SVCNAME} is not started"
		return 1
	fi

	ebegin "Increasing bandwidth to ${HIGH_DOWN}k/${HIGH_UP}k"

	setup_uri
	wget --spider --timeout="${MLDONKEY_TIMEOUT}" \
		"${BASE}/submit?q=set+max_hard_upload_rate+${HIGH_UP}" -q
	wget --spider --timeout="${MLDONKEY_TIMEOUT}" \
		"${BASE}/submit?q=set+max_hard_download_rate+${HIGH_DOWN}" -q
	eend $?
}


info() {
	if ! service_started "${SVCNAME}"; then
		eerror "${SVCNAME} is not started"
		return 1
	fi

	setup_uri
	local result=$(wget --timeout="${MLDONKEY_TIMEOUT}" \
		-O - "${BASE}"/submit?q=vo 2>/dev/null | \
		grep -C1 max_hard_upload | \
		grep value=\" | cut -d\" -f2)
	if [ "${result}" = "${LOW_UP}" ]; then
		einfo "${SVCNAME} runs slow"
	else
		einfo "${SVCNAME} runs fast"
	fi
}