aboutsummaryrefslogtreecommitdiffstats
path: root/meta-systemd/recipes-core/systemd/systemd-systemctl-native/systemctl
blob: a5bd77085b41089fb41aeba1113904b97ae5b587 (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
#!/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
			;;
		--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

for service in $services; do
	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

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

	# create the required symbolic links
	wanted_by=$(grep WantedBy $ROOT/$service_file \
		        | sed 's,WantedBy=,,g' \
		        | tr ',' '\n' \
		        | grep '\.target$')

	for r in $wanted_by; do
		echo "WantedBy=$r found in $service"
		if [ "$action" = "enable" ]; then
			mkdir -p $ROOT/etc/systemd/system/$r.wants
			ln -s $service_file $ROOT/etc/systemd/system/$r.wants
			echo "Enabled $service for $wanted_by."
		else
			rm -f $ROOT/etc/systemd/system/$r.wants/$service
			rmdir --ignore-fail-on-non-empty -p $ROOT/etc/systemd/system/$r.wants
			echo "Disabled $service for $wanted_by."
		fi
	done

	# call us for the other required scripts
	also=$(grep Also $ROOT/$service_file \
		   | sed 's,Also=,,g' \
		   | 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