summaryrefslogtreecommitdiff
path: root/bird1-openwrt/bird1-ipv6-openwrt/src/init.d/bird6
blob: 32e16a911297a78a64b314eddafd32c954d92805 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/sh /etc/rc.common

# Copyright (C) 2014-2017 - Eloi Carbo
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# Extra Service Function to get the Status of the Service
# This complements /etc/rc.common functions
# Commands ending with *_quiet are meant to be ran in Luci. These
# scripts' return minimal output.
EXTRA_COMMANDS="status start_quiet stop_quiet restart_quiet status_quiet"
EXTRA_HELP="        status  Returns service status"

BIRD="bird6"
BIRD_CONFIG="/etc/${BIRD}.conf"
BIRD_LOG="/var/log/${BIRD}.log"
BIRD_ERR="/tmp/${BIRD}.err"

START=99
STOP=10

SERVICE_DAEMONIZE=1
SERVICE_USE_PID=1
SERVICE_PID_FILE="/var/run/${BIRD}.pid"

BIRD_BIN="/usr/sbin/${BIRD}"
# Special non-terminal-rich output for Luci calls
LUCI="false"

. /etc/${BIRD}/init.d/${BIRD}-lib.sh

start() {
    config_load ${BIRD}
    local use_UCI_config
    get use_UCI_config 'bird'

    #Start the service
    if [ "${LUCI}" == "false" ]; then
        echo "Starting ${BIRD} Service [ ... ]"
    fi
    if [ -f ${BIRD_ERR} ]; then
        echo "" > ${BIRD_ERR}
    else
        touch ${BIRD_ERR}
    fi

    if [ -z "${use_UCI_config}" -o "${use_UCI_config}" = "0" ]; then
        # Disable Custom bird-openwrt settings.
        # Use default behaviour and files
        ${BIRD_BIN} -d -c ${BIRD_CONFIG} -P ${SERVICE_PID_FILE} -D ${BIRD_LOG} &> ${BIRD_ERR} &
    else
        #Set Bird6 configuration location:
        local UCI_config_file
        local log_file
        get UCI_config_file 'bird'
        get log_file 'global'
        BIRD_CONFIG="${UCI_config_file:-$BIRD_CONFIG}"
        BIRD_LOG="${log_file:-$BIRD_LOG}"
        #Backup previous configuration
        [ -f ${BIRD_CONFIG} ] && cp ${BIRD_CONFIG} ${BIRD_CONFIG}.bak
        #Setup the basic configuration
        prepare_global 'global'

        # Gather and set all Functions
        gather_functions
        # Gather and set all Filters
        gather_filters

        # Setup Main Protocols
        config_foreach prepare_kernel 'kernel'
        config_foreach prepare_static 'static'
        config_foreach prepare_device 'device'
        config_foreach prepare_direct 'direct'
        config_foreach prepare_pipe 'pipe'

        #Setup protocol's configuration: BGP
        config_foreach prepare_bgp_template 'bgp_template'
        config_foreach prepare_bgp 'bgp'

        #Setup protocol's configuration: OSPF
        #config_foreach prepare_ospf_instance 'ospf'

        #Start the service
        ${BIRD_BIN} -d -c ${BIRD_CONFIG} -P ${SERVICE_PID_FILE} -D ${BIRD_LOG} &>${BIRD_ERR} &
    fi
    while [ ! -s ${SERVICE_PID_FILE} ]; do
        sleep 1
        if [ -s ${BIRD_ERR} ]; then
            if [ "${LUCI}" == "false" ]; then
                echo -e "${BIRD} Daemon Start Status: \033[0;31m[ FAILED ]\e[m"
                cat ${BIRD_ERR}
                cat ${BIRD_ERR} >> ${BIRD_LOG}
            else
                echo "${BIRD} - Failed: $(cat ${BIRD_ERR})"
                cat ${BIRD_ERR} >> ${BIRD_LOG}
            fi
            break
        fi
    done

    if [ -s ${SERVICE_PID_FILE} ]; then
        if [ -s ${BIRD_ERR} ]; then
            if [ "${LUCI}" == "false" ]; then
                echo -e "${BIRD} Daemon already started. Status \033[0;32m[ RUNNING ]\e[m"
            else
                echo "${BIRD} already started"
            fi
        else
            if [ "${LUCI}" == "false" ]; then
                echo -e "${BIRD} Daemon Start Status: \033[0;32m[ STARTED ]\e[m"
            else
                echo "${BIRD} - Started"
            fi
        fi
        # PID File found (service started correctly)
        return 0
    fi

    # PID File not found (error while starting service)
    return 1
}

stop() {
    if [ -s ${SERVICE_PID_FILE} ]; then
        config_load ${BIRD}
        local log_file
        get log_file 'global'
        BIRD_LOG="${log_file:-$BIRD_LOG}"
        start-stop-daemon -p ${SERVICE_PID_FILE} -K 2>&1 >> ${BIRD_LOG}
        if [ $? -eq 0 ]; then
            if [ "${LUCI}" == "false" ]; then
                echo -e "${BIRD} Daemon Stop Status: \033[0;32m[ OK ]\e[m"
            else
                echo "${BIRD} - Stopped"
            fi
        else
            if [ "${LUCI}" == "false" ]; then
                echo -e "${BIRD} Daemon Stop Status: \033[0;31m[ FAILED ]\e[m"
                echo "Check ${BIRD_LOG} file for more information."
            else
                echo "${BIRD} Failed to Stop. See Log file: ${BIRD_LOG}"
            fi
        fi
    else
        if [ "${LUCI}" == "false" ]; then
            echo -e "${BIRD} Daemon Service already stopped. \033[0;31m[ FAILED ]\e[m"
        else
            echo "${BIRD} already stopped"
        fi
    fi
    return 0
}

restart() {
    stop
    sleep 1
    if [ "${LUCI}" == "true" ]; then
        echo " ... "
    fi
    start
}

reload() {
    service_reload ${BIRD_BIN}
}

status() {
    if [ -s ${SERVICE_PID_FILE} ]; then
        if [ "${LUCI}" == "false" ]; then
            echo -e "${BIRD} start status: \033[0;32m[ RUNNING ]\e[m"
        else
            echo "${BIRD}: Running"
        fi
        return 0
    else
        if [ -s ${BIRD_ERR} ]; then
            if [ "${LUCI}" == "false" ]; then
                echo -e "${BIRD} service status: \033[0;31m[ STOPPED ]\e[m"
                cat ${BIRD_ERR}
            else
                echo "${BIRD}: Failed - $(cat ${BIRD_ERR})"
            fi
            return 2
        else
            if [ "${LUCI}" == "false" ]; then
                echo -e "${BIRD} service status: \033[0;31m[ STOPPED ]\e[m"
            else
                echo "${BIRD}: Stopped"
            fi
            return 1
        fi
    fi
}

# Luci-specific calls (stripped output).
# The following scripts are not meant to be ran using Ash Terminal
# Used in: LUCI/model/cbi/bird6/status.lua
start_quiet() {
    LUCI="true"
    start
}
stop_quiet() {
    LUCI="true"
    stop
}
restart_quiet() {
    LUCI="true"
    restart
}
status_quiet() {
    LUCI="true"
    status
}