github.com/yggdrasil-network/yggdrasil-go@v0.5.6/contrib/freebsd/yggdrasil (about)

     1  #!/bin/sh
     2  #
     3  # Put the yggdrasil and yggdrasilctl binaries into /usr/local/bin
     4  # Then copy this script into /etc/rc.d/yggdrasil
     5  # Finally, run:
     6  #   1. chmod +x /etc/rc.d/yggdrasil /usr/local/bin/{yggdrasil,yggdrasilctl}
     7  #   2. echo "yggdrasil_enable=yes" >> /etc/rc.d
     8  #   3. service yggdrasil start
     9  #
    10  # PROVIDE: yggdrasil
    11  # REQUIRE: networking
    12  # KEYWORD:
    13  
    14  . /etc/rc.subr
    15  
    16  name="yggdrasil"
    17  rcvar="yggdrasil_enable"
    18  
    19  start_cmd="${name}_start"
    20  stop_cmd="${name}_stop"
    21  
    22  pidfile="/var/run/yggdrasil/${name}.pid"
    23  command="/usr/sbin/daemon"
    24  command_args="-P ${pidfile} -r -f ${yggdrasil_command}"
    25  
    26  yggdrasil_start()
    27  {
    28  	test ! -x /usr/local/bin/yggdrasil && (
    29  		logger -s -t yggdrasil "Warning: /usr/local/bin/yggdrasil is missing or not executable"
    30  		logger -s -t yggdrasil "Copy the yggdrasil binary into /usr/local/bin and then chmod +x /usr/local/bin/yggdrasil"
    31  		return 1
    32  	)
    33  
    34  	test ! -f /etc/yggdrasil.conf && (
    35  		logger -s -t yggdrasil "Generating new configuration file into /etc/yggdrasil.conf"
    36  		/usr/local/bin/yggdrasil -genconf > /etc/yggdrasil.conf
    37  	)
    38  
    39  	tap_path="$(cat /etc/yggdrasil.conf | egrep -o '/dev/tap[0-9]{1,2}$')"
    40  	tap_name="$(echo -n ${tap_path} | tr -d '/dev/')"
    41  
    42  	/sbin/ifconfig ${tap_name} >/dev/null 2>&1 || (
    43  		logger -s -t yggdrasil "Creating ${tap_name} adapter"
    44  		/sbin/ifconfig ${tap_name} create || logger -s -t yggdrasil "Failed to create ${tap_name} adapter"
    45  	)
    46  
    47  	test ! -d /var/run/yggdrasil && mkdir -p /var/run/yggdrasil
    48  
    49  	logger -s -t yggdrasil "Starting yggdrasil"
    50  	${command} ${command_args} /usr/local/bin/yggdrasil -useconffile /etc/yggdrasil.conf \
    51  		1>/var/log/yggdrasil.stdout.log \
    52  		2>/var/log/yggdrasil.stderr.log &
    53  }
    54  
    55  yggdrasil_stop()
    56  {
    57  	logger -s -t yggdrasil "Stopping yggdrasil"
    58  	test -f /var/run/yggdrasil/${name}.pid && kill -TERM $(cat /var/run/yggdrasil/${name}.pid)
    59  
    60  	tap_path="$(cat /etc/yggdrasil.conf | grep /dev/tap | egrep -o '/dev/.*$')"
    61          tap_name="$(echo -n ${tap_path} | tr -d '/dev/')"
    62  
    63  	/sbin/ifconfig ${tap_name} >/dev/null 2>&1 && (
    64  		logger -s -t yggdrasil "Destroying ${tap_name} adapter"
    65  		/sbin/ifconfig ${tap_name} destroy || logger -s -t yggdrasil "Failed to destroy ${tap_name} adapter"
    66  	)
    67  }
    68  
    69  load_rc_config $name
    70  : ${yggdrasil_enable:=no}
    71  
    72  run_rc_command "$1"