1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006-2016 OpenWrt.org
7 CFGDIR=/var/etc/radicale
11 DATADIR="/srv/radicale"
14 PGREP="ps | grep '[p]ython.*[r]adicale' 2>/dev/null | awk '{print \$1}' "
16 # we could start with empty configuration file using defaults
17 [ -f /etc/config/radicale ] || touch /etc/config/radicale
20 local _SYSTMP="$SYSCFG.tmp"
21 local _LOGTMP="$LOGCFG.tmp"
22 local _LOPT # list option name
23 local _LVAL # list option value
24 local _STYPE # section type
25 local _SNAME # section name
26 local _console_level="ERROR" # logging console level
27 local _file_level="INFO" # logging file level
28 local _file_path="/var/log/radicale" # logging file path
29 local _file_maxbytes="8196" # logging file maxBytes
30 local _file_backupcount="1" # logging file backupCount
31 local _syslog_level="WARNING" # logging syslog level
33 # write list values to config
35 _write_value "$_LOPT" "$_LVAL" # there might be spaces in _LVAL
45 # section "server" ignore option "daemon" and "pid"
46 [ "$_SNAME" = "server" -a "$__OPT" = "daemon" ] && return 0
47 [ "$_SNAME" = "server" -a "$__OPT" = "pid" ] && return 0
48 # section "logging" ignore option "config" (logging config file)
49 [ "$_SNAME" = "logging" -a "$__OPT" = "config" ] && return 0
50 # section "auth" ignore option "htpasswd_filename" (htpasswd file)
51 [ "$_SNAME" = "auth" -a "$__OPT" = "htpasswd_filename" ] && return 0
52 # section "rights" ignore option "file" (reg-based rights file)
53 [ "$_SNAME" = "rights" -a "$__OPT" = "file" ] && return 0
54 # section "headers" replace "_" with "-" in option (UCI problem)
55 [ "$_SNAME" = "headers" ] && __OPT=$(echo "$__OPT" | sed -e "s#_#-#g")
57 [ "$_SNAME" = "storage" -a "$__OPT" = "filesystem_folder" ] && DATADIR="$__VAL"
58 # special handling for well-known, value needs single quotes
59 [ "$_SNAME" = "well-known" -a "${__VAL#*\%\(}" != "$__VAL" ] && __VAL="'$__VAL'"
60 # handling of log settings
61 if [ "$_STYPE" = "logging" -a "$_SNAME" = "logger" ]; then
62 eval "_$__OPT='$__VAL'" # set to environment for later use
65 [ "$__VAL" = "0" ] && __VAL="False"
66 [ "$__VAL" = "1" ] && __VAL="True"
67 # append data to the corresponding section
68 sed -i "/\[$_SNAME\]/a $__OPT = $__VAL" $_SYSTMP
72 # redefined callback for sections when calling config_load
76 # write out last list option
77 [ -n "$_LOPT" ] && _write_list
82 [ "$1" = "setting" -o "$1" = "logging" ] && {
86 # translate section name
87 [ "$2" = "well_known" ] && _SNAME="well-known"
91 # redefined callback for lists when calling config_load
95 # invalid section type then ignore
96 [ -z "$_STYPE" -o -z "$_SNAME" ] && return 0
97 # write out last list option if new list starts
98 [ -n "$_LOPT" -a "$_LOPT" != "$1" ] && _write_list
100 if [ -z "$_LOPT" ]; then
109 # redefined callback for options when calling config_load
111 # $1 name of variable
115 # invalid section type then ignore
116 [ -z "$_STYPE" -o -z "$_SNAME" ] && return 0
117 # ignore list entrys will be handled by list_cb()
118 [ "${__OPT#*_ITEM}" != "$__OPT" ] && return 0 # ignore lists *_ITEM*
119 [ "${__OPT#*_LENGTH}" != "$__OPT" ] && return 0 # ignore lists *_LENGTH
120 # write out last list option and clear
121 [ -n "$_LOPT" ] && _write_list
123 _write_value "$__OPT" "$__VAL" # there might be spaces in __VAL
127 # temporary config file
128 # radicale need read access
129 mkdir -m0755 -p $CFGDIR
131 cp /etc/radicale/config.template $_SYSTMP
132 config_load radicale # calling above config_cb()/option_cb()/list_cb() and write into $_SYSTMP
133 sed -i "/\[logging\]/a config = /var/etc/radicale/logging" $_SYSTMP # hard-code logging config
134 sed -i "/\[auth\]/a htpasswd_filename = /etc/radicale/users" $_SYSTMP # hard-code htpasswd
135 sed -i "/\[rights\]/a file = /etc/radicale/rights" $_SYSTMP # hard-code regexp-based rights
137 # temporary logging config file
138 cp /etc/radicale/logging.template $_LOGTMP
140 sed -i "/\[handler_console\]/a level = $_console_level" $_LOGTMP
141 sed -i "/\[handler_file\]/a level = $_file_level" $_LOGTMP
142 sed -i "/\[handler_file\]/a args = ('$_file_path/radicale','a',$_file_maxbytes,$_file_backupcount)" $_LOGTMP
143 sed -i "/\[handler_syslog\]/a level = $_syslog_level" $_LOGTMP
146 mv -f $_SYSTMP $SYSCFG
147 mv -f $_LOGTMP $LOGCFG
151 # config file permissions (read access for group)
152 chmod 644 $SYSCFG $LOGCFG
153 chgrp -R radicale $CFGDIR
154 # log directory (full access and owner)
155 [ -d $LOGDIR ] || mkdir -m0755 -p $LOGDIR
156 chown -R radicale:radicale $LOGDIR
157 # data directory does not exist
159 logger -p user.error -t "radicale[----]" "Data directory '$DATADIR' does not exists. Startup failed !!!"
162 chgrp -R radicale $DATADIR
166 # wait a given time (default 10 seconds) before startup
167 # to wait for interfaces to come up / not using hotplug events during boot
169 [ $1 -gt 0 ] && sleep $1
174 _DELAY=$(uci_get "radicale" "system" "boot_delay" "10")
180 rm -f /tmp/radicale.hotplug
186 sleep 2 # give radicale time to completely come up
187 local _PID=$(eval "$PGREP")
188 kill -1 $_PID 2>/dev/null
190 && logger -p user.notice -t "radicale[$_PID]" "Service started successfully"\
191 || logger -p user.warn -t "radicale[----]" "Service failed to start"
194 # if already running do nothing
195 local _PID=$(eval "$PGREP")
196 kill -1 $_PID 2>/dev/null && return 0
201 radicale --daemon --config=$SYSCFG
202 touch /tmp/radicale.hotplug
204 _running & # check if running and syslog
210 # reload is also used by luci
211 local _PID=$(eval "$PGREP")
212 kill -1 $_PID 2>/dev/null
213 if [ $? -eq 0 ]; then
214 # only restart if already running
217 # only start if enabled
224 local _PID=$(eval "$PGREP")
225 [ -z "$_PID" ] && return 0 # not running
226 kill -15 $_PID 2>/dev/null
227 sleep 3 # give time to shutdown
228 local _tmp=$(eval "$PGREP")
229 if [ -z "$_tmp" ]; then
230 logger -p user.notice -t "radicale[$_PID]" "Service shutdown successfully"
232 kill -9 $_tmp # Normally never come here
233 logger -p user.warn -t "radicale[----]" "Service shutdown FORCED"