procd: convert various packages to procd style init.d scripts
[openwrt/svn-archive/archive.git] / package / network / services / uhttpd / files / uhttpd.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2010 Jo-Philipp Wich
3
4 START=50
5
6 USE_PROCD=1
7
8 UHTTPD_BIN="/usr/sbin/uhttpd"
9 PX5G_BIN="/usr/sbin/px5g"
10
11 append_arg() {
12 local cfg="$1"
13 local var="$2"
14 local opt="$3"
15 local def="$4"
16 local val
17
18 config_get val "$cfg" "$var"
19 [ -n "$val" -o -n "$def" ] && procd_append_param command "$opt" "${val:-$def}"
20 }
21
22 append_bool() {
23 local cfg="$1"
24 local var="$2"
25 local opt="$3"
26 local def="$4"
27 local val
28
29 config_get_bool val "$cfg" "$var" "$def"
30 [ "$val" = 1 ] && procd_append_param command "$opt"
31 }
32
33 generate_keys() {
34 local cfg="$1"
35 local key="$2"
36 local crt="$3"
37 local days bits country state location commonname
38
39 config_get days "$cfg" days
40 config_get bits "$cfg" bits
41 config_get country "$cfg" country
42 config_get state "$cfg" state
43 config_get location "$cfg" location
44 config_get commonname "$cfg" commonname
45
46 [ -x "$PX5G_BIN" ] && {
47 $PX5G_BIN selfsigned -der \
48 -days ${days:-730} -newkey rsa:${bits:-1024} -keyout "$UHTTPD_KEY" -out "$UHTTPD_CERT" \
49 -subj /C="${country:-DE}"/ST="${state:-Saxony}"/L="${location:-Leipzig}"/CN="${commonname:-OpenWrt}"
50 }
51 }
52
53 start_instance()
54 {
55 UHTTPD_CERT=""
56 UHTTPD_KEY=""
57
58 local cfg="$1"
59 local realm="$(uci_get system.@system[0].hostname)"
60 local listen http https interpreter indexes path
61
62 procd_open_instance
63 procd_set_param command "$UHTTPD_BIN" -f
64
65 append_arg "$cfg" home "-h"
66 append_arg "$cfg" realm "-r" "${realm:-OpenWrt}"
67 append_arg "$cfg" config "-c"
68 append_arg "$cfg" cgi_prefix "-x"
69 append_arg "$cfg" lua_prefix "-l"
70 append_arg "$cfg" lua_handler "-L"
71 append_arg "$cfg" ubus_prefix "-u"
72 append_arg "$cfg" ubus_socket "-U"
73 append_arg "$cfg" script_timeout "-t"
74 append_arg "$cfg" network_timeout "-T"
75 append_arg "$cfg" http_keepalive "-k"
76 append_arg "$cfg" tcp_keepalive "-A"
77 append_arg "$cfg" error_page "-E"
78 append_arg "$cfg" max_requests "-n" 3
79 append_arg "$cfg" max_connections "-N"
80
81 append_bool "$cfg" no_ubusauth "-a" 0
82 append_bool "$cfg" no_symlinks "-S" 0
83 append_bool "$cfg" no_dirlists "-D" 0
84 append_bool "$cfg" rfc1918_filter "-R" 0
85
86 config_get http "$cfg" listen_http
87 for listen in $http; do
88 procd_append_param command -p "$listen"
89 done
90
91 config_get interpreter "$cfg" interpreter
92 for path in $interpreter; do
93 procd_append_param command -i "$path"
94 done
95
96 config_get indexes "$cfg" index_page
97 for path in $indexes; do
98 procd_append_param command -I "$path"
99 done
100
101 config_get https "$cfg" listen_https
102 config_get UHTTPD_KEY "$cfg" key /etc/uhttpd.key
103 config_get UHTTPD_CERT "$cfg" cert /etc/uhttpd.crt
104
105 [ -n "$https" ] && {
106 [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] || {
107 config_foreach generate_keys cert
108 }
109
110 [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] && {
111 append_arg "$cfg" cert "-C"
112 append_arg "$cfg" key "-K"
113
114 for listen in $https; do
115 append UHTTPD_ARGS "-s $listen"
116 done
117 }
118 }
119
120 procd_close_instance
121
122 # Check if daemon is running, if not then
123 # re-execute in foreground to display error.
124 # sleep 1 && service_check $UHTTPD_BIN || \
125 # $UHTTPD_BIN -f $UHTTPD_ARGS
126 }
127
128 service_triggers()
129 {
130 procd_add_reload_trigger "uhttpd"
131 }
132
133 start_service() {
134 config_load uhttpd
135 config_foreach start_instance uhttpd
136 }