aboutsummaryrefslogtreecommitdiffstats
path: root/meta/recipes-core/systemd/systemd-systemctl/systemctl
blob: efad14ce17cf8ea1e76b659ba51922831d5d31a1 (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
#!/bin/sh
echo "Started $0 $*"

ROOT=

# parse command line params
action=
while [ $# != 0 ]; do
	opt="$1"

	case "$opt" in
		enable)
			shift

			action="$opt"
			services="$1"
			cmd_args="1"
			shift
			;;
		disable)
			shift

			action="$opt"
			services="$1"
			cmd_args="1"
			shift
			;;
		mask)
			shift

			action="$opt"
			services="$1"
			cmd_args="1"
			shift
			;;
		preset)
			shift

			action="$opt"
			services="$1"
			cmd_args="1"
			shift
			;;
		--root=*)
			ROOT=${opt##--root=}
			cmd_args="0"
			shift
			;;
		*)
			if [ "$cmd_args" = "1" ]; then
				services="$services $opt" 
				shift
			else
				echo "'$opt' is an unkown option; exiting with error"
				exit 1
			fi
			;;
	esac
done
if [ "$action" = "preset" -a "$service_file" = "" ]; then
	services=$(for f in `find $ROOT/etc/systemd/system $ROOT/lib/systemd/system $ROOT/usr/lib/systemd/system -type f 2>1`; do basename $f; done)
	services="$services $opt"
	presetall=1
fi

for service in $services; do
	if [ "$presetall" = "1" ]; then
		action="preset"
	fi
	if [ "$action" = "mask" ]; then
		if [ ! -d $ROOT/etc/systemd/system/ ]; then
			mkdir -p $ROOT/etc/systemd/system/
		fi
		cmd="ln -s /dev/null $ROOT/etc/systemd/system/$service"
		echo "$cmd"
		$cmd
		exit 0
	fi

	service_base_file=`echo $service | sed 's/\(@\).*\(\.[^.]\+\)/\1\2/'`
	if [ -z `echo $service | sed '/@/p;d'` ]; then
		echo "Try to find location of $service..."
		service_template=false
	else
		echo "Try to find location of template $service_base_file of instance $service..."
		service_template=true
		if [ -z `echo $service | sed 's/^.\+@\(.*\)\.[^.]\+/\1/'` ]; then
			instance_specified=false
		else
			instance_specified=true
		fi
	fi

	# find service file
	for p in $ROOT/etc/systemd/system \
		 $ROOT/lib/systemd/system \
		 $ROOT/usr/lib/systemd/system; do
		if [ -e $p/$service_base_file ]; then
			service_file=$p/$service_base_file
			service_file=${service_file##$ROOT}
		fi
	done
	if [ -z "$service_file" ]; then
		echo "'$service_base_file' couldn't be found; exiting with error"
		exit 1
	fi
	echo "Found $service in $service_file"

	# If any new unit types are added to systemd they should be added
	# to this regular expression.
	unit_types_re='\.\(service\|socket\|device\|mount\|automount\|swap\|target\|path\|timer\|snapshot\)\s*$'
	if [ "$action" = "preset" ]; then
		action=`egrep -sh  $service $ROOT/etc/systemd/user-preset/*.preset | cut -f1 -d' '`
		if [ -z "$action" ]; then
			globalpreset=`egrep -sh  '\*'  $ROOT/etc/systemd/user-preset/*.preset | cut -f1 -d' '`
			if [ -n "$globalpreset" ]; then
				action="$globalpreset"
			else
				action="enable"
			fi
		fi
	fi
	# create the required symbolic links
	wanted_by=$(sed '/^WantedBy[[:space:]]*=/s,[^=]*=,,p;d' "$ROOT/$service_file" \
		        | tr ',' '\n' \
		        | grep "$unit_types_re")

	required_by=$(sed '/^RequiredBy[[:space:]]*=/s,[^=]*=,,p;d' "$ROOT/$service_file" \
		        | tr ',' '\n' \
		        | grep "$unit_types_re")

	for dependency in WantedBy RequiredBy; do
		if [ "$dependency" = "WantedBy" ]; then
			suffix="wants"
			dependency_list="$wanted_by"
		elif [ "$dependency" = "RequiredBy" ]; then
			suffix="requires"
			dependency_list="$required_by"
		fi
		for r in $dependency_list; do
			echo "$dependency=$r found in $service"
			if [ "$action" = "enable" ]; then
				enable_service=$service
				if [ "$service_template" = true -a "$instance_specified" = false ]; then
					default_instance=$(sed '/^DefaultInstance[[:space:]]*=/s,[^=]*=,,p;d' "$ROOT/$service_file")
					if [ -z $default_instance ]; then
						echo "Template unit without instance or DefaultInstance directive, nothing to enable"
						continue
					else
						echo "Found DefaultInstance $default_instance, enabling it"
						enable_service=$(echo $service | sed "s/@/@$(echo $default_instance | sed 's/\\/\\\\/g')/")
					fi
				fi
				mkdir -p $ROOT/etc/systemd/system/$r.$suffix
				ln -s $service_file $ROOT/etc/systemd/system/$r.$suffix/$enable_service
				echo "Enabled $enable_service for $r."
			else
				if [ "$service_template" = true -a "$instance_specified" = false ]; then
					disable_service="$ROOT/etc/systemd/system/$r.$suffix/`echo $service | sed 's/@/@*/'`"
				else
					disable_service="$ROOT/etc/systemd/system/$r.$suffix/$service"
				fi
				rm -f $disable_service
				[ -d $ROOT/etc/systemd/system/$r.$suffix ] && rmdir --ignore-fail-on-non-empty -p $ROOT/etc/systemd/system/$r.$suffix
				echo "Disabled ${disable_service##$ROOT/etc/systemd/system/$r.$suffix/} for $r."
			fi
		done
	done

	# create the required symbolic 'Alias' links
	alias=$(sed '/^Alias[[:space:]]*=/s,[^=]*=,,p;d' "$ROOT/$service_file" \
		        | tr ',' '\n' \
		        | grep "$unit_types_re")

	for r in $alias; do
		if [ "$action" = "enable" ]; then
			mkdir -p $ROOT/etc/systemd/system
			ln -s $service_file $ROOT/etc/systemd/system/$r
			echo "Enabled $service for $alias."
		else
			rm -f $ROOT/etc/systemd/system/$r
			echo "Disabled $service for $alias."
		fi
	done

	# call us for the other required scripts
	also=$(sed '/^Also[[:space:]]*=/s,[^=]*=,,p;d' "$ROOT/$service_file" \
		   | tr ',' '\n')
	for a in $also; do
		echo "Also=$a found in $service"
		if [ "$action" = "enable" ]; then
			$0 --root=$ROOT enable $a
		fi
	done
done