From 0cf464564f23709fe2533dea237b9980c3d681dd Mon Sep 17 00:00:00 2001 From: Taeyeon Mori Date: Mon, 19 Mar 2018 16:01:16 +0100 Subject: [PATCH 1/5] Add --pidfile and --logfile for daemon mode --- create_ap | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/create_ap b/create_ap index 8fa6671..49c1adf 100755 --- a/create_ap +++ b/create_ap @@ -62,6 +62,8 @@ usage() { echo " --mac Set MAC address" echo " --dhcp-dns Set DNS returned by DHCP" echo " --daemon Run create_ap in the background" + echo " --pidfile Save daemon PID to file" + echo " --logfile Save daemon messages to file" echo " --stop Send stop command to an already running create_ap. For an " echo " you can put the PID of create_ap or the WiFi interface. You can" echo " get them with --list-running" @@ -628,6 +630,8 @@ COUNTRY= FREQ_BAND=2.4 NEW_MACADDR= DAEMONIZE=0 +DAEMON_PIDFILE= +DAEMON_LOGFILE=/dev/null NO_HAVEGED=0 USE_PSK=0 @@ -636,7 +640,7 @@ REDIRECT_TO_LOCALHOST=0 CONFIG_OPTS=(CHANNEL GATEWAY WPA_VERSION ETC_HOSTS DHCP_DNS NO_DNS NO_DNSMASQ HIDDEN MAC_FILTER MAC_FILTER_ACCEPT ISOLATE_CLIENTS SHARE_METHOD IEEE80211N IEEE80211AC HT_CAPAB VHT_CAPAB DRIVER NO_VIRT COUNTRY FREQ_BAND - NEW_MACADDR DAEMONIZE NO_HAVEGED WIFI_IFACE INTERNET_IFACE + NEW_MACADDR DAEMONIZE DAEMON_PIDFILE DAEMON_LOGFILE NO_HAVEGED WIFI_IFACE INTERNET_IFACE SSID PASSPHRASE USE_PSK) FIX_UNMANAGED=0 @@ -790,6 +794,10 @@ _cleanup() { mutex_unlock cleanup_lock + + if [[ $RUNNING_AS_DAEMON -eq 1 && -n "$DAEMON_PIDFILE" && -f "$DAEMON_PIDFILE" ]]; then + rm $DAEMON_PIDFILE + fi } cleanup() { @@ -1028,7 +1036,7 @@ for ((i=0; i<$#; i++)); do fi done -GETOPT_ARGS=$(getopt -o hc:w:g:de:nm: -l "help","hidden","hostapd-debug:","redirect-to-localhost","mac-filter","mac-filter-accept:","isolate-clients","ieee80211n","ieee80211ac","ht_capab:","vht_capab:","driver:","no-virt","fix-unmanaged","country:","freq-band:","mac:","dhcp-dns:","daemon","stop:","list","list-running","list-clients:","version","psk","no-haveged","no-dns","no-dnsmasq","mkconfig:","config:" -n "$PROGNAME" -- "$@") +GETOPT_ARGS=$(getopt -o hc:w:g:de:nm: -l "help","hidden","hostapd-debug:","redirect-to-localhost","mac-filter","mac-filter-accept:","isolate-clients","ieee80211n","ieee80211ac","ht_capab:","vht_capab:","driver:","no-virt","fix-unmanaged","country:","freq-band:","mac:","dhcp-dns:","daemon","pidfile:","logfile:","stop:","list","list-running","list-clients:","version","psk","no-haveged","no-dns","no-dnsmasq","mkconfig:","config:" -n "$PROGNAME" -- "$@") [[ $? -ne 0 ]] && exit 1 eval set -- "$GETOPT_ARGS" @@ -1148,6 +1156,16 @@ while :; do shift DAEMONIZE=1 ;; + --pidfile) + shift + DAEMON_PIDFILE="$1" + shift + ;; + --logfile) + shift + DAEMON_LOGFILE="$1" + shift + ;; --stop) shift STOP_ID="$1" @@ -1287,8 +1305,10 @@ fi if [[ $DAEMONIZE -eq 1 && $RUNNING_AS_DAEMON -eq 0 ]]; then echo "Running as Daemon..." # run a detached create_ap - RUNNING_AS_DAEMON=1 setsid "$0" "${ARGS[@]}" & + RUNNING_AS_DAEMON=1 setsid "$0" "${ARGS[@]}" >>$DAEMON_LOGFILE 2>&1 & exit 0 +elif [[ $RUNNING_AS_DAEMON -eq 1 && -n "$DAEMON_PIDFILE" ]]; then + echo $$ >$DAEMON_PIDFILE fi if [[ $FREQ_BAND != 2.4 && $FREQ_BAND != 5 ]]; then From a210bf99f7b0b07b3bc45816eb204cc53f21ac6b Mon Sep 17 00:00:00 2001 From: Taeyeon Mori Date: Mon, 19 Mar 2018 17:21:38 +0100 Subject: [PATCH 2/5] Polyfill cp -n for busybox --- create_ap | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/create_ap b/create_ap index 49c1adf..dccc2ad 100755 --- a/create_ap +++ b/create_ap @@ -103,6 +103,17 @@ usage() { echo " "$PROGNAME" --stop wlan0" } +# Busybox polyfills +if cp --help 2>&1 | grep -q -- --no-clobber; then + cp_n() { + cp -n "$@" + } +else + cp_n() { + yes n | cp -i "$@" + } +fi + # on success it echos a non-zero unused FD # on error it echos 0 get_avail_fd() { @@ -1514,12 +1525,12 @@ mkdir -p $COMMON_CONFDIR if [[ "$SHARE_METHOD" == "nat" ]]; then echo $INTERNET_IFACE > $CONFDIR/nat_internet_iface - cp -n /proc/sys/net/ipv4/conf/$INTERNET_IFACE/forwarding \ + cp_n /proc/sys/net/ipv4/conf/$INTERNET_IFACE/forwarding \ $COMMON_CONFDIR/${INTERNET_IFACE}_forwarding fi -cp -n /proc/sys/net/ipv4/ip_forward $COMMON_CONFDIR +cp_n /proc/sys/net/ipv4/ip_forward $COMMON_CONFDIR if [[ -e /proc/sys/net/bridge/bridge-nf-call-iptables ]]; then - cp -n /proc/sys/net/bridge/bridge-nf-call-iptables $COMMON_CONFDIR + cp_n /proc/sys/net/bridge/bridge-nf-call-iptables $COMMON_CONFDIR fi mutex_unlock From ba6f924f4fb61f7199975e60686f2795db8e7471 Mon Sep 17 00:00:00 2001 From: Taeyeon Mori Date: Mon, 19 Mar 2018 17:27:27 +0100 Subject: [PATCH 3/5] Add OpenRC initscript --- Makefile | 2 ++ create_ap.openrc | 11 +++++++++++ 2 files changed, 13 insertions(+) create mode 100644 create_ap.openrc diff --git a/Makefile b/Makefile index 57911b5..5ad53c6 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,7 @@ install: install -Dm755 create_ap $(DESTDIR)$(BINDIR)/create_ap install -Dm644 create_ap.conf $(DESTDIR)/etc/create_ap.conf [ ! -d /lib/systemd/system ] || install -Dm644 create_ap.service $(DESTDIR)$(PREFIX)/lib/systemd/system/create_ap.service + [ ! -e /sbin/openrc-run ] || install -Dm755 create_ap.openrc $(DESTDIR)/etc/init.d/create_ap install -Dm644 bash_completion $(DESTDIR)$(PREFIX)/share/bash-completion/completions/create_ap install -Dm644 README.md $(DESTDIR)$(PREFIX)/share/doc/create_ap/README.md @@ -17,5 +18,6 @@ uninstall: rm -f $(DESTDIR)$(BINDIR)/create_ap rm -f $(DESTDIR)/etc/create_ap.conf [ ! -f /lib/systemd/system/create_ap.service ] || rm -f $(DESTDIR)$(PREFIX)/lib/systemd/system/create_ap.service + [ ! -e /sbin/openrc-run ] || rm -f $(DESTDIR)/etc/init.d/create_ap rm -f $(DESTDIR)$(PREFIX)/share/bash-completion/completions/create_ap rm -f $(DESTDIR)$(PREFIX)/share/doc/create_ap/README.md diff --git a/create_ap.openrc b/create_ap.openrc new file mode 100644 index 0000000..9bb4876 --- /dev/null +++ b/create_ap.openrc @@ -0,0 +1,11 @@ +#!/sbin/openrc-run + +name=$RC_SVCNAME + +cfgfile=/etc/$RC_SVCNAME.conf +pidfile=/run/$RC_SVCNAME.pid + +command=/usr/bin/create_ap +command_args="--config $cfgfile" +command_args_background="--daemon --pidfile $pidfile" +stopsig=USR1 From 32ce3fcf02252a97bf677e2ebbe2c198882a6193 Mon Sep 17 00:00:00 2001 From: Taeyeon Mori Date: Mon, 19 Mar 2018 17:52:03 +0100 Subject: [PATCH 4/5] echo's trailing newline seems to sometimes cause EINVAL in sysfs/forward_delay --- create_ap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/create_ap b/create_ap index dccc2ad..5ecea86 100755 --- a/create_ap +++ b/create_ap @@ -1788,7 +1788,7 @@ if [[ "$SHARE_METHOD" != "none" ]]; then ip link add name $BRIDGE_IFACE type bridge || die ip link set dev $BRIDGE_IFACE up || die # set 0ms forward delay - echo 0 > /sys/class/net/$BRIDGE_IFACE/bridge/forward_delay + echo -n 0 > /sys/class/net/$BRIDGE_IFACE/bridge/forward_delay # attach internet interface to bridge interface ip link set dev $INTERNET_IFACE promisc on || die From 907db9a5383136f945560b1c0c41cf50dde07f13 Mon Sep 17 00:00:00 2001 From: Taeyeon Mori Date: Mon, 19 Mar 2018 18:29:27 +0100 Subject: [PATCH 5/5] Avoid printing to stdout in daemon mode --- create_ap | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/create_ap b/create_ap index 5ecea86..0cda41d 100755 --- a/create_ap +++ b/create_ap @@ -1314,7 +1314,11 @@ if [[ $FIX_UNMANAGED -eq 1 ]]; then fi if [[ $DAEMONIZE -eq 1 && $RUNNING_AS_DAEMON -eq 0 ]]; then - echo "Running as Daemon..." + # Assume we're running underneath a service manager if PIDFILE is set + # and don't clobber it's output with a useless message + if [ -z "$DAEMON_PIDFILE" ]; then + echo "Running as Daemon..." + fi # run a detached create_ap RUNNING_AS_DAEMON=1 setsid "$0" "${ARGS[@]}" >>$DAEMON_LOGFILE 2>&1 & exit 0