From 88cbccd3e8de8f7c2df03b12f5224d2b82d706dd Mon Sep 17 00:00:00 2001 From: Chen Yufei Date: Thu, 24 Jan 2013 20:03:08 +0800 Subject: [PATCH 1/2] Enhance Linux startup script. --- sample-config/shadowsocks | 138 +++++++++++++++++++++++--------------- 1 file changed, 83 insertions(+), 55 deletions(-) diff --git a/sample-config/shadowsocks b/sample-config/shadowsocks index 442d01b..421fcea 100755 --- a/sample-config/shadowsocks +++ b/sample-config/shadowsocks @@ -21,64 +21,92 @@ CONFIG_FILE=/etc/shadowsocks/config.json LOG_FILE=/var/log/shadowsocks USER=nobody GROUP=nobody -PID_DIR=/var/run/ +PID_DIR=/var/run PID_FILE=$PID_DIR/shadowsocks.pid RET_VAL=0 +check_running() { + if [[ -r $PID_FILE ]]; then + read PID <$PID_FILE + if [[ -d "/proc/$PID" ]]; then + return 0 + else + rm -f $PID_FILE + return 1 + fi + else + return 2 + fi +} + +do_status() { + check_running + case $? in + 0) + echo "shadowsocks running with PID $PID" + ;; + 1) + echo "shadowsocks not running, remove PID file $PID_FILE" + ;; + 2) + echo "Could not find PID file $PID_FILE, shadowsocks does not appear to be running" + ;; + esac + return 0 +} + +do_start() { + if [[ ! -d $PID_DIR ]]; then + echo "creating PID dir" + mkdir $PID_DIR || echo "failed creating PID directory $PID_DIR"; exit 1 + chown $USER:$GROUP $PID_DIR || echo "failed creating PID directory $PID_DIR"; exit 1 + chmod 0770 $PID_DIR + fi + if check_running; then + echo "shadowsocks already running with PID $PID" + return 0 + fi + if [[ ! -r $CONFIG_FILE ]]; then + echo "config file $CONFIG_FILE not found" + return 1 + fi + echo "starting shadowsocks" + # sudo will set the group to the primary group of $USER + sudo -u $USER $BIN -c $CONFIG_FILE >>$LOG_FILE & + PID=$! + echo $PID > $PID_FILE + sleep 0.3 + if ! check_running; then + echo "start failed, log file $LOG_FILE may provide some help" + return 1 + fi + echo "shadowsocks running with PID $PID" + return 0 +} + +do_stop() { + if check_running; then + echo "stopping shadowsocks with PID $PID" + kill $PID + rm -f $PID_FILE + else + echo "Could not find PID file $PID_FILE" + fi +} + +do_restart() { + do_stop + do_start +} + case "$1" in - start) - if [[ ! -d $PID_DIR ]] - then - mkdir $PID_DIR - chown $USER:$GROUP $PID_DIR - chmod 0770 $PID_DIR - fi - if [[ -r $PID_FILE ]] - then - echo "shadowsocks already running with PID `cat $PID_FILE`" - RET_VAL=1 - else - # sudo will set the group to the primary group of $USER - sudo -u $USER $BIN -c $CONFIG_FILE >$LOG_FILE & - echo $! > $PID_FILE - RET_VAL=$? - fi - ;; - stop) - if [[ -r $PID_FILE ]] - then - kill `cat $PID_FILE` - rm $PID_FILE - RET_VAL=$? - else - echo "Could not find PID file $PID_FILE" - RET_VAL=1 - fi - ;; - restart) - if [[ -r $PID_FILE ]] - then - kill `cat $PID_FILE` - rm $PID_FILE - RET_VAL=$? - else - echo "Could not find PID file $PID_FILE" - fi - start - RET_VAL=$? - ;; - status) - if [[ -r $PID_FILE ]] - then - echo "shadowsocks running with PID `cat $PID_FILE`" - RET_VAL=$? - else - echo "Could not find PID file $PID_FILE, shadowsocks does not appear to be running" - fi - ;; - *) - echo "Usage: shadowsocks {start|stop|restart|status}" - RET_VAL=1 - ;; + start|stop|restart|status) + do_$1 + ;; + *) + echo "Usage: shadowsocks {start|stop|restart|status}" + RET_VAL=1 + ;; esac + exit $RET_VAL From eb2046f55ba0cee4fb43eca95a543533af4bbfc2 Mon Sep 17 00:00:00 2001 From: Chen Yufei Date: Thu, 24 Jan 2013 20:21:11 +0800 Subject: [PATCH 2/2] Print startup error message to stderr. --- cmd/shadowsocks-local/local.go | 19 +++++++++++-------- cmd/shadowsocks-server/server.go | 14 +++++++------- sample-config/shadowsocks | 2 +- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/cmd/shadowsocks-local/local.go b/cmd/shadowsocks-local/local.go index 5e4ee3c..1dce8c2 100644 --- a/cmd/shadowsocks-local/local.go +++ b/cmd/shadowsocks-local/local.go @@ -4,6 +4,7 @@ import ( "encoding/binary" "errors" "flag" + "fmt" ss "github.com/shadowsocks/shadowsocks-go/shadowsocks" "io" "log" @@ -322,28 +323,30 @@ func main() { config, err := ss.ParseConfig(configFile) if err != nil { config = &cmdConfig - if os.IsNotExist(err) { - log.Println("config file not found, using all options from command line") - } else { - log.Fatal("error reading config file: %v\n", err) + if !os.IsNotExist(err) { + fmt.Fprintf(os.Stderr, "error reading %s: %v\n", configFile, err) + os.Exit(1) } } else { ss.UpdateConfig(config, &cmdConfig) } if err = ss.SetDefaultCipher(config.Method); err != nil { - log.Fatal(err) + fmt.Fprintln(os.Stderr, err) + os.Exit(1) } if len(config.ServerPassword) == 0 { if !enoughOptions(config) { - log.Fatal("must specify server address, password and both server/local port") + fmt.Fprintln(os.Stderr, "must specify server address, password and both server/local port") + os.Exit(1) } } else { if config.Password != "" || config.ServerPort != 0 || config.GetServerArray() != nil { - log.Println("given server_password, ignore server, server_port and password option:", config) + fmt.Fprintln(os.Stderr, "given server_password, ignore server, server_port and password option:", config) } if config.LocalPort == 0 { - log.Fatal("must specify local port") + fmt.Fprintln(os.Stderr, "must specify local port") + os.Exit(1) } } diff --git a/cmd/shadowsocks-server/server.go b/cmd/shadowsocks-server/server.go index 936d4d4..fb432ca 100644 --- a/cmd/shadowsocks-server/server.go +++ b/cmd/shadowsocks-server/server.go @@ -4,6 +4,7 @@ import ( "encoding/binary" "errors" "flag" + "fmt" ss "github.com/shadowsocks/shadowsocks-go/shadowsocks" "io" "log" @@ -279,14 +280,14 @@ func enoughOptions(config *ss.Config) bool { func unifyPortPassword(config *ss.Config) (err error) { if len(config.PortPassword) == 0 { // this handles both nil PortPassword and empty one if !enoughOptions(config) { - log.Println("must specify both port and password") + fmt.Fprintln(os.Stderr, "must specify both port and password") return errors.New("not enough options") } port := strconv.Itoa(config.ServerPort) config.PortPassword = map[string]string{port: config.Password} } else { if config.Password != "" || config.ServerPort != 0 { - log.Println("given port_password, ignore server_port and password option") + fmt.Fprintln(os.Stderr, "given port_password, ignore server_port and password option") } } return @@ -323,10 +324,8 @@ func main() { var err error config, err = ss.ParseConfig(configFile) if err != nil { - if os.IsNotExist(err) { - log.Println("config file not found, using all options from command line") - } else { - log.Printf("error reading %s: %v\n", configFile, err) + if !os.IsNotExist(err) { + fmt.Fprintf(os.Stderr, "error reading %s: %v\n", configFile, err) os.Exit(1) } config = &cmdConfig @@ -337,7 +336,8 @@ func main() { os.Exit(1) } if err = ss.SetDefaultCipher(config.Method); err != nil { - log.Fatal(err) + fmt.Fprintln(os.Stderr, err) + os.Exit(1) } if core > 0 { runtime.GOMAXPROCS(core) diff --git a/sample-config/shadowsocks b/sample-config/shadowsocks index 421fcea..c107017 100755 --- a/sample-config/shadowsocks +++ b/sample-config/shadowsocks @@ -77,7 +77,7 @@ do_start() { echo $PID > $PID_FILE sleep 0.3 if ! check_running; then - echo "start failed, log file $LOG_FILE may provide some help" + echo "start failed" return 1 fi echo "shadowsocks running with PID $PID"