1/* 2 * Copyright (c) 2015, Yawning Angel <yawning at schwanenlied dot me> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * * Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 11 * * Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28package main 29 30import ( 31 "io" 32 "io/ioutil" 33 "os" 34 "os/signal" 35 "runtime" 36 "syscall" 37 "time" 38 39 "gitlab.com/yawning/obfs4.git/common/log" 40) 41 42var termMonitorOSInit func(*termMonitor) error 43 44type termMonitor struct { 45 sigChan chan os.Signal 46 handlerChan chan int 47 numHandlers int 48} 49 50func (m *termMonitor) onHandlerStart() { 51 m.handlerChan <- 1 52} 53 54func (m *termMonitor) onHandlerFinish() { 55 m.handlerChan <- -1 56} 57 58func (m *termMonitor) wait(termOnNoHandlers bool) os.Signal { 59 // Block until a signal has been received, or (optionally) the 60 // number of pending handlers has hit 0. In the case of the 61 // latter, treat it as if a SIGTERM has been received. 62 for { 63 select { 64 case n := <-m.handlerChan: 65 m.numHandlers += n 66 case sig := <-m.sigChan: 67 return sig 68 } 69 if termOnNoHandlers && m.numHandlers == 0 { 70 return syscall.SIGTERM 71 } 72 } 73} 74 75func (m *termMonitor) termOnStdinClose() { 76 _, err := io.Copy(ioutil.Discard, os.Stdin) 77 78 // io.Copy() will return a nil on EOF, since reaching EOF is 79 // expected behavior. No matter what, if this unblocks, assume 80 // that stdin is closed, and treat that as having received a 81 // SIGTERM. 82 log.Noticef("Stdin is closed or unreadable: %v", err) 83 m.sigChan <- syscall.SIGTERM 84} 85 86func (m *termMonitor) termOnPPIDChange(ppid int) { 87 // Under most if not all U*IX systems, the parent PID will change 88 // to that of init once the parent dies. There are several notable 89 // exceptions (Slowlaris/Android), but the parent PID changes 90 // under those platforms as well. 91 // 92 // Naturally we lose if the parent has died by the time when the 93 // Getppid() call was issued in our parent, but, this is better 94 // than nothing. 95 const ppidPollInterval = 1 * time.Second 96 for ppid == os.Getppid() { 97 time.Sleep(ppidPollInterval) 98 } 99 100 // Treat the parent PID changing as the same as having received 101 // a SIGTERM. 102 log.Noticef("Parent pid changed: %d (was %d)", os.Getppid(), ppid) 103 m.sigChan <- syscall.SIGTERM 104} 105 106func newTermMonitor() (m *termMonitor) { 107 ppid := os.Getppid() 108 m = new(termMonitor) 109 m.sigChan = make(chan os.Signal) 110 m.handlerChan = make(chan int) 111 signal.Notify(m.sigChan, syscall.SIGINT, syscall.SIGTERM) 112 113 // If tor supports feature #15435, we can use Stdin being closed as an 114 // indication that tor has died, or wants the PT to shutdown for any 115 // reason. 116 if ptShouldExitOnStdinClose() { 117 go m.termOnStdinClose() 118 } else { 119 // Instead of feature #15435, use various kludges and hacks: 120 // * Linux - Platform specific code that should always work. 121 // * Other U*IX - Somewhat generic code, that works unless the 122 // parent dies before the monitor is initialized. 123 if termMonitorOSInit != nil { 124 // Errors here are non-fatal, since it might still be 125 // possible to fall back to a generic implementation. 126 if err := termMonitorOSInit(m); err == nil { 127 return 128 } 129 } 130 if runtime.GOOS != "windows" { 131 go m.termOnPPIDChange(ppid) 132 } 133 } 134 return 135} 136