aboutsummaryrefslogtreecommitdiff
path: root/www/spawn-fcgi/files/spawn-fcgi.in
blob: 6d38a4ec1f52667be55cd925f1965f8473dfd7d3 (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
#!/bin/sh

# PROVIDE: spawn_fcgi
# REQUIRE: %%REQUIRE%%
# BEFORE:  LOGIN
# KEYWORD: shutdown
#
# Optional settings:
#
# spawn_fcgi_app_args
# spawn_fcgi_chroot_dir
# spawn_fcgi_bindsocket
# spawn_fcgi_web_server_addrs
# spawn_fcgi_allowed_env
# spawn_fcgi_command_interpreter

. /etc/rc.subr

name="spawn_fcgi"
rcvar=spawn_fcgi_enable

load_rc_config $name

: ${spawn_fcgi_enable="NO"}
: ${spawn_fcgi_app="%%LOCALBASE%%/bin/php-cgi"}
: ${spawn_fcgi_pidfile="/var/run/spawn-fcgi.pid"}
: ${spawn_fcgi_username="www"}
: ${spawn_fcgi_groupname="www"}
: ${spawn_fcgi_bindaddr="127.0.0.1"}
: ${spawn_fcgi_bindport="9000"}
: ${spawn_fcgi_bindsocket_mode="0777"}
: ${spawn_fcgi_children="5"}
: ${spawn_fcgi_max_requests="1000"}
: ${spawn_fcgi_path_env="/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin"}

if [ -z "${spawn_fcgi_bindsocket}" ]; then
    _spawn_fcgi_bind="-a ${spawn_fcgi_bindaddr} -p ${spawn_fcgi_bindport}"
else
    if [ -n "${spawn_fcgi_bindsocket_mode}" ]; then
        _spawn_fcgi_bind="-s ${spawn_fcgi_bindsocket} -M ${spawn_fcgi_bindsocket_mode}"
    else
        _spawn_fcgi_bind="-s ${spawn_fcgi_bindsocket}"
    fi
fi

if [ -n "${spawn_fcgi_chroot_dir}" ]; then
    _spawn_fcgi_chroot="-c ${spawn_fcgi_chroot_dir}"
else
    _spawn_fcgi_chroot=""
fi

if [ -n "${spawn_fcgi_command_interpreter}" ]; then
  command_interpreter=${spawn_fcgi_command_interpreter}
fi

command="/usr/local/bin/spawn-fcgi"
command_args="-u ${spawn_fcgi_username} -g ${spawn_fcgi_groupname} ${_spawn_fcgi_bind} ${_spawn_fcgi_chroot} -P ${spawn_fcgi_pidfile} -- ${spawn_fcgi_app} ${spawn_fcgi_app_args}"
pidfile=${spawn_fcgi_pidfile}
procname=${spawn_fcgi_app}

_allowed_env="PATH USER PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS FCGI_WEB_SERVER_ADDRS"
_allowed_env="${_allowed_env} ${spawn_fcgi_allowed_env}"

start_precmd="${name}_start_precmd"
stop_postcmd="${name}_stop_postcmd"

spawn_fcgi_start_precmd()
{
    export PATH=${spawn_fcgi_path_env}
    export USER=${spawn_fcgi_username}
    export PHP_FCGI_CHILDREN=${spawn_fcgi_children}
    export PHP_FCGI_MAX_REQUESTS=${spawn_fcgi_max_requests}
    export FCGI_WEB_SERVER_ADDRS=${spawn_fcgi_web_server_addrs}
    E=
    for i in ${_allowed_env}; do
        eval _val="\$$i"
        if [ "${_val}_x" != "_x" ]; then
            eval _add="$i=$_val"
            E="${E} ${_add}"
        fi
    done
    command="env - ${E} ${command}"
}

spawn_fcgi_stop_postcmd()
{
    rm -f ${pidfile}

    if [ -n "${spawn_fcgi_bindsocket}" -a -S "${spawn_fcgi_chroot_dir}${spawn_fcgi_bindsocket}" ]; then
        rm -f ${spawn_fcgi_chroot_dir}${spawn_fcgi_bindsocket}
    fi
}

run_rc_command "$1"