aboutsummaryrefslogtreecommitdiffstats
path: root/add-service
blob: 23b009445341e9aaa241c35223ffe1029fbc9870 (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
#!/bin/sh
#---------------------------------------------------------------
# Project         : Mandriva Linux
# Module          : rpm-helper
# File            : add-service
# Version         : $Id$
# Authors         : Frederic Lepied, Andrey Borzenkov
# Created On      : Mon Jul  8 08:14:34 2002
# Purpose         : helper script for rpm scriptlets to add a
#                   service.
#---------------------------------------------------------------

if [ x$1 = x--no-sysv ]; then
    do_sysv=no
    shift
else
    do_sysv=yes
fi

if [ $# -lt 3 ]; then
    echo "usage: $0 [--no-sysv] <pkg name> <number installed> [<service name>] [<unit name> ...]" 1>&2
    exit 1
fi

pkg=$1				# name of the package
num=$2				# number of packages installed
if [ $do_sysv = yes ]; then
    srv=$3			# name of the SysV script
    shift 3
else
    srv=
    shift 2
fi
units="$*"			# systemd units
units_to_enable=		# units enabled by msec

# If only a sysvinit service is given, then deal with a systemd unit of the same
# name. Specific specs can enable specific unit names as needed but this should
# catch the most common usage.
if [ -z "$units" ]; then
  units="$srv.service"
fi

add_chkconfig_service() {
    if [ -n "$units_to_enable" ]; then
        /bin/systemctl --quiet enable $units_to_enable
    fi
    if [ -n "$srv" ]; then
        /sbin/chkconfig --add $srv
    else
        exit 0
    fi

    if [ -r /etc/sysconfig/system ]; then
        . /etc/sysconfig/system
    fi

    # TODO what to do with system units here?
    if [ -z "$ADD_SERVICES_TO_CURRENT_PROFILE_ONLY" ]; then
        # add the service to all the profiles at once
        if [ -d /etc/netprofile/profiles/default/services ]; then
            for dir in /etc/netprofile/profiles/*/services; do
                touch $dir/$srv
            done
        fi
    fi
}

add_service() {
    # Add the service
    if [ -r /etc/sysconfig/msec ]; then
        . /etc/sysconfig/msec
    fi
    
    # High security: add only authorized services
    LIST=/etc/security/msec/server

    # during the install the symlink isn't done so find the right file
    # by ourselves
    if [ -n "$DURING_INSTALL" -a ! -f $LIST ]; then
        LIST=/etc/security/msec/server.$SECURE_LEVEL
    fi

    # This is half-hearted support for msec: we check "srv" for a unit
    # with name "srv.service". This should account for most(?) common
    # case when SysV script srv is replaced by single unit srv.service
    # If SysV name was supplied, we assume units are equivalent and do
    # not need to be checked.
    # TODO should msec support full unit name?
    if [ -f $LIST ]; then
        if [ -n "$srv" ]; then
            if fgrep -qx "${srv}" $LIST; then
                units_to_enable="$units"
            else
                srv=
            fi
        else
            for i in $units; do
                [ $i != ${i%.service} ] && ! fgrep -qx ${i%.service} $LIST && continue
                units_to_enable="$units_to_enable $i"
            done
        fi
    else
        units_to_enable="$units"
    fi

    add_chkconfig_service
}

if [ $num = 1 ]; then
    # First install mode
    add_service
else
    # Upgrade mode. systemd units are restarted in postun
    [ -n "$srv" ] || exit 0

    # if the service is activated, add it again to be able to handle
    # changes in start/stop levels. This does not change enabled/disabled
    # state, so we do not do it for systemd where dependencies are handled
    # automatically.

    # Restart only SysV services here only if no additional systemd
    # units are defined or systemd is not active. Otherwise user is expected
    # to add postun script that handles systemd units.

    set -- /etc/rc3.d/S??$srv
    if [ $# -gt 1 ]; then
        echo 1>&2 "add-service: Error: $srv appears multiple times: $*"
    fi

    if [ -f "$1" ]; then
        /sbin/chkconfig --add $srv
    fi

    if ! /bin/mountpoint -q /sys/fs/cgroup/systemd; then
        # restart the service if already running
        if [ -f /var/lock/subsys/$srv ]; then
            /sbin/service $srv restart > /dev/null || :
        fi
    fi
fi

# add-service ends here