summaryrefslogtreecommitdiff
path: root/ejabberdctl.template
blob: 035918bee70dd5a045f03a46a8be938fcd0f142f (plain) (blame)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/bin/sh

# define default configuration
POLL=true
SMP=auto
ERL_MAX_PORTS=32000
ERL_PROCESSES=250000
ERL_MAX_ETS_TABLES=1400
FIREWALL_WINDOW=""
INET_DIST_INTERFACE="127.0.0.1"
ERLANG_NODE=ejabberd@localhost

# define default environment variables
SCRIPT_DIR=`cd ${0%/*} && pwd`
ERL={{erl}}
INSTALLUSER={{installuser}}

# Compatibility in ZSH
#setopt shwordsplit 2>/dev/null

# check the proper system user is used if defined
if [ "$INSTALLUSER" != "" ] ; then
    EXEC_CMD="false"
    for GID in `id -G`; do
        if [ $GID -eq 0 ] ; then
            EXEC_CMD="su $INSTALLUSER -p -c"
        fi
    done
    if [ `id -g` -eq `id -g $INSTALLUSER` ] ; then
        EXEC_CMD="sh -c"
    fi
    if [ "$EXEC_CMD" = "false" ] ; then
        echo "This command can only be run by root or the user $INSTALLUSER" >&2
        exit 4
    fi
else
    EXEC_CMD="sh -c"
fi

# parse command line parameters
ARGS=""
while [ $# -ne 0 ] ; do
    PARAM=$1
    shift
    case $PARAM in
        --) break ;;
        --node) ERLANG_NODE_ARG=$1 ; shift ;;
        --config-dir) ETCDIR=$1 ; shift ;;
        --config) EJABBERD_CONFIG_PATH=$1 ; shift ;;
        --ctl-config) EJABBERDCTL_CONFIG_PATH=$1 ; shift ;;
        --logs) LOGS_DIR=$1 ; shift ;;
        --spool) SPOOLDIR=$1 ; shift ;;
        *) ARGS="$ARGS $PARAM" ;;
    esac
done

# Define ejabberd variable if they have not been defined from the command line
if [ "$ETCDIR" = "" ] ; then
    ETCDIR={{sysconfdir}}/ejabberd
fi
if [ "$EJABBERDCTL_CONFIG_PATH" = "" ] ; then
    EJABBERDCTL_CONFIG_PATH=$ETCDIR/ejabberdctl.cfg
fi
if [ -f "$EJABBERDCTL_CONFIG_PATH" ] ; then
    . "$EJABBERDCTL_CONFIG_PATH"
fi
if [ "$EJABBERD_CONFIG_PATH" = "" ] ; then
    EJABBERD_CONFIG_PATH=$ETCDIR/ejabberd.cfg
fi
if [ "$LOGS_DIR" = "" ] ; then
    LOGS_DIR={{localstatedir}}/log/ejabberd
fi
if [ "$SPOOLDIR" = "" ] ; then
    SPOOLDIR={{localstatedir}}/lib/ejabberd
fi
if [ "$EJABBERD_DOC_PATH" = "" ] ; then
    EJABBERD_DOC_PATH={{docdir}}
fi
if [ "$ERLANG_NODE_ARG" != "" ] ; then
    ERLANG_NODE=$ERLANG_NODE_ARG
    NODE=${ERLANG_NODE%@*}
fi
if [ "{{release}}" != "true" ] ; then
    if [ "$EJABBERDDIR" = "" ] ; then
        EJABBERDDIR={{libdir}}/ejabberd
    fi
    if [ "$EJABBERD_EBIN_PATH" = "" ] ; then
        EJABBERD_EBIN_PATH=$EJABBERDDIR/ebin
    fi
    if [ "$EJABBERD_PRIV_PATH" = "" ] ; then
        EJABBERD_PRIV_PATH=$EJABBERDDIR/priv
    fi
    if [ "$EJABBERD_BIN_PATH" = "" ] ; then
        EJABBERD_BIN_PATH=$EJABBERD_PRIV_PATH/bin
    fi
    if [ "$EJABBERD_SO_PATH" = "" ] ; then
        EJABBERD_SO_PATH=$EJABBERD_PRIV_PATH/lib
    fi
    if [ "$EJABBERD_MSGS_PATH" = "" ] ; then
        EJABBERD_MSGS_PATH=$EJABBERD_PRIV_PATH/msgs
    fi
fi
EJABBERD_LOG_PATH=$LOGS_DIR/ejabberd.log
SASL_LOG_PATH=$LOGS_DIR/erlang.log
DATETIME=`date "+%Y%m%d-%H%M%S"`
ERL_CRASH_DUMP=$LOGS_DIR/erl_crash_$DATETIME.dump
ERL_INETRC=$ETCDIR/inetrc
HOME=$SPOOLDIR

# define erl parameters
ERLANG_OPTS="+K $POLL -smp $SMP +P $ERL_PROCESSES $ERL_OPTIONS"
KERNEL_OPTS=""
if [ "$FIREWALL_WINDOW" != "" ] ; then
    KERNEL_OPTS="${KERNEL_OPTS} -kernel inet_dist_listen_min ${FIREWALL_WINDOW%-*} inet_dist_listen_max ${FIREWALL_WINDOW#*-}"
fi
if [ "$INET_DIST_INTERFACE" != "" ] ; then
    KERNEL_OPTS="${KERNEL_OPTS} -kernel inet_dist_use_interface \"{${INET_DIST_INTERFACE//./,}}\""
fi
if [ "$ERLANG_NODE" = "${ERLANG_NODE%.*}" ] ; then
    NAME="-sname"
else
    NAME="-name"
fi

# create the ejabberd home dir with the proper user if doesn't exist
# then change to that directory readable by INSTALLUSER to
# prevent "File operation error: eacces." messages
[ -d $HOME ] || $EXEC_CMD "mkdir -p $HOME"
cd $HOME

# export global variables
export EJABBERD_CONFIG_PATH
export EJABBERD_MSGS_PATH
export EJABBERD_LOG_PATH
export EJABBERD_SO_PATH
export EJABBERD_BIN_PATH
export EJABBERD_DOC_PATH
export EJABBERD_PID_PATH
export ERL_CRASH_DUMP
export ERL_EPMD_ADDRESS
export ERL_INETRC
export ERL_MAX_PORTS
export ERL_MAX_ETS_TABLES
export HOME
export EXEC_CMD

# start server
start()
{
    check_start
    $EXEC_CMD "$ERL \
      $NAME $ERLANG_NODE \
      -noinput -detached \
      -pa $EJABBERD_EBIN_PATH \
      -mnesia dir \"\\\"$SPOOLDIR\\\"\" \
      $KERNEL_OPTS \
      -s ejabberd \
      -sasl sasl_error_logger \\{file,\\\"$SASL_LOG_PATH\\\"\\} \
      $ERLANG_OPTS $ARGS \"$@\""
}

# attach to server
debug()
{
    echo "--------------------------------------------------------------------"
    echo ""
    echo "IMPORTANT: we will attempt to attach an INTERACTIVE shell"
    echo "to an already running ejabberd node."
    echo "If an ERROR is printed, it means the connection was not successful."
    echo "You can interact with the ejabberd node if you know how to use it."
    echo "Please be extremely cautious with your actions,"
    echo "and exit immediately if you are not completely sure."
    echo ""
    echo "To detach this shell from ejabberd, press:"
    echo "  control+c, control+c"
    echo ""
    echo "--------------------------------------------------------------------"
    echo "To bypass permanently this warning, add to ejabberdctl.cfg the line:"
    echo "  EJABBERD_BYPASS_WARNINGS=true"
    echo "Press any key to continue"
    if [ "$EJABBERD_BYPASS_WARNINGS" != "true" ] ; then
        read foo
    fi
    echo ""
    TTY=`tty | sed -e  's/.*\///g'`
    $EXEC_CMD "$ERL \
      $NAME debug-${TTY}-${ERLANG_NODE} \
      -remsh $ERLANG_NODE \
      -hidden \
      $KERNEL_OPTS \
      $ERLANG_OPTS $ARGS \"$@\""
}

# start interactive server
live()
{
    check_start
    echo "--------------------------------------------------------------------"
    echo ""
    echo "IMPORTANT: ejabberd is going to start in LIVE (interactive) mode."
    echo "All log messages will be shown in the command shell."
    echo "You can interact with the ejabberd node if you know how to use it."
    echo "Please be extremely cautious with your actions,"
    echo "and exit immediately if you are not completely sure."
    echo ""
    echo "To exit this LIVE mode and stop ejabberd, press:"
    echo "  q().  and press the Enter key"
    echo ""
    echo "--------------------------------------------------------------------"
    echo "To bypass permanently this warning, add to ejabberdctl.cfg the line:"
    echo "  EJABBERD_BYPASS_WARNINGS=true"
    echo "Press any key to continue"
    if [ "$EJABBERD_BYPASS_WARNINGS" != "true" ] ; then
        read foo
    fi
    echo ""
    $EXEC_CMD "$ERL \
      $NAME $ERLANG_NODE \
      -pa $EJABBERD_EBIN_PATH \
      -mnesia dir \"\\\"$SPOOLDIR\\\"\" \
      $KERNEL_OPTS \
      -s ejabberd \
      $ERLANG_OPTS $ARGS \"$@\""
}

etop()
{
    $EXEC_CMD "$ERL \
      $NAME debug-${TTY}-${ERLANG_NODE} \
      -hidden -s etop -s erlang halt -output text -node $ERLANG_NODE"
}

help()
{
    echo ""
    echo "Commands to start an ejabberd node:"
    echo "  start  Start an ejabberd node in server mode"
    echo "  debug  Attach an interactive Erlang shell to a running ejabberd node"
    echo "  live   Start an ejabberd node in live (interactive) mode"
    echo ""
    echo "Optional parameters when starting an ejabberd node:"
    echo "  --config-dir dir   Config ejabberd:    $ETCDIR"
    echo "  --config file      Config ejabberd:    $EJABBERD_CONFIG_PATH"
    echo "  --ctl-config file  Config ejabberdctl: $EJABBERDCTL_CONFIG_PATH"
    echo "  --logs dir         Directory for logs: $LOGS_DIR"
    echo "  --spool dir        Database spool dir: $SPOOLDIR"
    echo "  --node nodename    ejabberd node name: $ERLANG_NODE"
    echo ""
}

# common control function
ctl()
{
    COMMAND=$@

    # Control number of connections identifiers
    # using flock if available. Expects a linux-style
    # flock that can lock a file descriptor.
    MAXCONNID=100
    CONNLOCKDIR={{localstatedir}}/lock/ejabberdctl
    FLOCK='/usr/bin/flock'
    if [ ! -x "$FLOCK" ] || [ ! -d "$CONNLOCKDIR" ] ; then
        JOT='/usr/bin/jot'
        if [ ! -x "$JOT" ] ; then
            # no flock or jot, simply invoke ctlexec()
            CTL_CONN="ctl-${ERLANG_NODE}"
            ctlexec $CTL_CONN $COMMAND
            result=$?
        else
            # no flock, but at least there is jot
            RAND=`jot -r 1 0 $MAXCONNID`
            CTL_CONN="ctl-${RAND}-${ERLANG_NODE}"
            ctlexec $CTL_CONN $COMMAND
            result=$?
        fi
    else
        # we have flock so we get a lock
        # on one of a limited number of
        # conn names -- this allows
        # concurrent invocations using a bound
        # number of atoms
        for N in `seq 1 $MAXCONNID`; do
            CTL_CONN="ejabberdctl-$N"
            CTL_LOCKFILE="$CONNLOCKDIR/$CTL_CONN"
            (
                exec 8>"$CTL_LOCKFILE"
                if flock --nb 8; then
                    ctlexec $CTL_CONN $COMMAND
                    ssresult=$?
                    # segregate from possible flock exit(1)
                    ssresult=`expr $ssresult \* 10`
                    exit $ssresult
                else
                    exit 1
                fi
            )
            result=$?
            if [ $result -eq 1 ] ; then
                # means we errored out in flock
                # rather than in the exec - stay in the loop
                # trying other conn names...
                badlock=1
            else
                badlock=""
                break;
            fi
        done
        result=`expr $result / 10`
    fi

    if [ "$badlock" ] ;then
        echo "Ran out of connections to try. Your ejabberd processes" >&2
        echo "may be stuck or this is a very busy server. For very"   >&2
        echo "busy servers, consider raising MAXCONNID in ejabberdctl">&2
        exit 1;
    fi

    case $result in
        0) :;;
        1) :;;
        2) help;;
        3) help;;
    esac
    return $result
}

ctlexec()
{
    CONN_NAME=$1; shift
    COMMAND=$@
    $EXEC_CMD "$ERL \
      $NAME ${CONN_NAME} \
      -noinput \
      -hidden \
      -pa $EJABBERD_EBIN_PATH \
      $KERNEL_OPTS \
      -s ejabberd_ctl -extra $ERLANG_NODE $COMMAND"
}

# stop epmd if there is no other running node
stop_epmd()
{
    epmd -names 2>/dev/null | grep -q name || epmd -kill >/dev/null
}

# make sure node not already running and node name unregistered
check_start()
{
    epmd -names 2>/dev/null | grep -q " ${ERLANG_NODE%@*} " && {
        ps ux | grep -v grep | grep -q " $ERLANG_NODE " && {
            echo "ERROR: The ejabberd node '$ERLANG_NODE' is already running."
            exit 4
        } || {
            ps ux | grep -v grep | grep -q beam && {
                echo "ERROR: The ejabberd node '$ERLANG_NODE' is registered,"
                echo "       but no related beam process has been found."
                echo "Shutdown all other erlang nodes, and call 'epmd -kill'."
                exit 5
            } || {
                epmd -kill >/dev/null
            }
        }
    }
}

# allow sync calls
wait_for_status()
{
    # args: status try delay
    # return: 0 OK, 1 KO
    timeout=$2
    status=4
    while [ $status -ne $1 ] ; do
        sleep $3
        timeout=`expr $timeout - 1`
        [ $timeout -eq 0 ] && {
            status=$1
        } || {
            ctl status > /dev/null
            status=$?
        }
    done
    [ $timeout -eq 0 ] && return 1 || return 0
}

# main handler
case $ARGS in
    ' start') start;;
    ' debug') debug;;
    ' live') live;;
    ' etop') etop;;
    ' started') wait_for_status 0 30 2;; # wait 30x2s before timeout
    ' stopped') wait_for_status 3 15 2 && stop_epmd;; # wait 15x2s before timeout
    *) ctl $ARGS;;
esac