aboutsummaryrefslogtreecommitdiff
blob: d4a514b8f15d6b85b24bab7a36daa7f744c03565 (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
# Compatibility layer for netifrc to work with multiple init
# systems.
# shellcheck shell=sh disable=SC1008

# First check whether e* commands are present in the environment
if [ x$RC_GOT_FUNCTIONS = xyes -o -n "$(command -v ebegin 2>/dev/null)" ]; then
	:

# Then check for the presence of functions.sh
elif [ -f /lib/gentoo/functions.sh ]; then
	. /lib/gentoo/functions.sh

else
	echo "/lib/gentoo/functions.sh not found. Exiting"
	exit 1
fi

# runscript functions
# TODO: if another non-openrc system provides these in future, we have to
# change this structure.
if [ "$INIT" != "openrc" ]; then

	# OpenRC functions used in depend
	after() {
		:
	}
	before() {
		:
	}
	program() {
		:
	}
	need() {
		:
	}

	shell_var() {
		local output= sanitized_arg=
		for arg; do
			sanitized_arg=$arg
			while :; do
				case $sanitized_arg in
				*[!a-zA-Z0-9_]*)
					sanitized_arg=${sanitized_arg%%[!a-zA-Z0-9_]*}_${sanitized_arg#*[!a-zA-Z0-9_]}
					continue;;
				esac
				break
			done
			if [ x"$output" = x"" ] ; then
				output=$sanitized_arg
			else
				output="$output $sanitized_arg"
			fi
		done
		echo "$output"
	}

	net_fs_list="afs ceph cifs coda davfs fuse fuse.sshfs gfs glusterfs lustre ncpfs nfs nfs4 ocfs2 shfs smbfs"
	is_net_fs()
	{
		[ -z "$1" ] && return 1

		local fs=$(mount | grep " on $1 " | cut -f 5 -d ' ')
		for x in $fs; do
			for y in $net_fs_list; do
				[ "$x" = "$y" ] && return 0
			done
		done
		return 1
	}

	service_set_value() {
		local OPTION="$1" VALUE="$2"
		if [ -z "$OPTION" ]; then
			eerror "service_set_value requires parameter KEY"
			return
		fi
		local file="$OPTIONSDIR/${OPTION}"
		echo "$VALUE" > $file
	}
	service_get_value() {
		local OPTION="$1"
		if [ -z "$OPTION" ]; then
			eerror "service_get_value requires parameter KEY"
			return
		fi
		local file="$OPTIONSDIR/${OPTION}"
		cat $file 2>/dev/null
	}
	STATEFILE="${STATEDIR}/state"
	# Internal Function
	# Stores the state of netifrc in ${SVCDIR}/${SVCNAME}/state file
	_mark_service() {
		local state=$1
		echo $state > $STATEFILE
	}
	#Internal Function
	# Checks whether the state of netifrc is same as $1
	_service_state() {
		state=$(cat $STATEFILE 2>/dev/null)
		if [ state = $1 ]; then
			return 1
		fi
		return 0
	}

	mark_service_started() {
		_mark_service started
	}
	mark_service_inactive() {
		_mark_service inactive
	}
	mark_service_stopped() {
		_mark_service stopped
	}
	service_started() {
		_service_state started
		return $?
	}
	service_inactive() {
		_service_state inactive
		return $?
	}
fi

# Extracts the interface for the current invocation
get_interface() {
	case $INIT in
		openrc)
			printf ${RC_SVCNAME#*.};;
		systemd)
			printf ${RC_IFACE};;
		*)
			eerror "Init system not supported. Aborting"
			exit 1;;
	esac
}

# runs a command in a network namespace
_netns()
{
	local netns
	eval netns="\$netns_${IFVAR}"

	if [ -n "${netns}" ] && [ -e /run/netns/"${netns}" ]; then
		# we always want to use the system "ip" command, not the busybox internal
		local ip
		if ! ip=$(command -v ip 2>/dev/null) || [ ! -x "${ip}" ]; then
			eerror "Cannot use network namespaces without iproute2"
			exit 1
		fi
		# shellcheck disable=SC2016
		case "${1}" in
			ip)
				shift
				"${ip}" -n "${netns}" "${@}"
			;;
			glob) "${ip}" netns exec "${netns}" /bin/sh -c 'printf "%s\\n" ${@}' "${@}";;
			echo|printf) "${ip}" netns exec "${netns}" /bin/sh -c "${*}";;
			*) "${ip}" netns exec "${netns}" "${@}";;
		esac
	else
		# shellcheck disable=SC2048
		case "${1}" in
			ip)
				shift
				"${ip}" "${@}"
			;;
			glob)
				shift
				eval printf '%s\\n' "${@}"
			;;
			echo|printf) eval "${@}";;
			*) "${@}";;
		esac
	fi
}


# vim: ts=4 sw=4 noexpandtab