/usr/bin/supybot-botchk is in supybot 0.83.4.1.ds-3.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | #!/usr/bin/python
###
# Copyright (c) 2005, Jeremiah Fincher
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
VERBOSE = False
def readPid(filename):
fd = file(filename)
try:
return int(fd.read().strip())
finally:
fd.close()
def isAlive(pid):
try:
os.kill(pid, 0)
return True
except OSError:
return False
def debug(s):
if VERBOSE:
if not s.endswith(os.linesep):
s += os.linesep
sys.stdout.write(s)
if __name__ == '__main__':
# XXX I wanted this for conf.version, but this will create directories. We
# really need to refactor conf so it either doesn't create directories, or
# so that static information (like the version) can be imported from
# somewhere else.
# import supybot.conf as conf
import os
import sys
import popen2
import optparse
parser = optparse.OptionParser(usage='Usage: %prog [options]')
parser.add_option('', '--verbose', action='store_true',
help='Makes output verbose.')
parser.add_option('', '--botdir',
help='Determines what directory the bot resides in and '
'should be started from.')
parser.add_option('', '--pidfile',
help='Determines what file to look in for the pid of '
'the running bot. This should be relative to the '
'given bot directory.')
parser.add_option('', '--supybot', default='supybot',
help='Determines where the supybot executable is '
'located. If not given, assumes that supybot is '
'in $PATH.')
parser.add_option('', '--conffile',
help='Determines what configuration file should be '
'given to the supybot executable when (re)starting the '
'bot.')
(options, args) = parser.parse_args()
VERBOSE = options.verbose
if args:
parser.error('Extra arguments given.')
if not options.botdir:
parser.error('No botdir given.')
if not options.pidfile:
parser.error('No pidfile given.')
if not options.conffile:
parser.error('No conffile given.')
os.chdir(options.botdir)
os.system('touch %s' % options.pidfile)
pid = None
try:
pid = readPid(options.pidfile)
debug('Found pidFile with proper pid contents of %s' % pid)
except ValueError, e:
foundBot = False
if pid is not None:
foundBot = isAlive(pid)
if foundBot:
debug('Pid %s is alive and belongs to us.' % pid)
else:
debug('Pid %s is not the bot.' % pid)
if not foundBot:
# First, we check if the pidfile is writable. If not, supybot will just exit,
# so we go ahead and refuse to start it.
try:
file(options.pidfile, 'r+')
except EnvironmentError, e:
debug('pidfile (%s) is not writable: %s' % (options.pidfile, e))
sys.exit(-1)
debug('Bot not found, starting.')
home = os.environ['HOME']
inst = popen2.Popen4('sh')
for filename in ('.login', '.bash_profile', '.profile', '.bashrc'):
filename = os.path.join(home, filename)
if os.path.exists(filename):
debug('Found %s, sourcing.' % filename)
inst.tochild.write('source %s' % filename + os.linesep)
cmdline = "%s --daemon %s" % (options.supybot, options.conffile)
debug('Sending cmdline to sh process.')
inst.tochild.write(cmdline + os.linesep)
inst.tochild.close()
debug('Received from sh process: %r' % inst.fromchild.read())
ret = inst.wait()
debug('Bot started, command line %r returned %s.' % (cmdline, ret))
sys.exit(ret)
else:
sys.exit(0)
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|