/usr/lib/gdesklets/main/RemoteSocket.py is in gdesklets 0.36.1-5.
This file is owned by root:root, with mode 0o644.
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | from main import DISPLAY, SOCKET_PATH
from main.remotecommands import *
from utils import xdr
import atexit
import os
import sys
import socket
import gobject
class RemoteSocket:
"""
This class implements a socket server for accepting incoming commands
from clients.
Socket server programming not necessarily requires threads. Actually, by
not using threads, we gain some performance and stability.
"""
def __init__(self):
# handlers for incoming messages
self.__message_handlers = {}
self.__stopevent = False
# marks the socket as blocked, i.e. must not accept new requests
self.__block_timeout = {}
def start(self):
"""
Starts the server. Call this after the handlers have been set.
"""
import utils
utils.makedirs(SOCKET_PATH)
serversock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sockfile = os.path.join(SOCKET_PATH, DISPLAY)
try:
serversock.bind(sockfile)
except socket.error:
try:
os.remove(sockfile)
except OSError:
log("Couldn't remove dead socket file \"%s\"." % sockfile)
sys.exit(1)
try:
serversock.bind(sockfile)
except socket.error:
log("Couldn't bind to socket. Aborting.")
sys.exit(1)
serversock.listen(3)
serversock.setblocking(False)
atexit.register(self.__shutdown, serversock, sockfile)
gobject.timeout_add(100, self.__server_handler, serversock, sockfile)
def add_message_handler(self, message, handler):
"""
Adds a new message handler.
"""
self.__message_handlers[message] = handler
def __shutdown(self, serversock, sockfile):
"""
Shuts down the server and closes/removes the used socket.
"""
self.__stopevent = True
serversock.close()
if (sockfile):
os.remove(sockfile)
def __server_handler(self, serversock, sockfile):
"""
The socket server reactor.
"""
if (self.__stopevent):
return False
try:
cnx, addr = serversock.accept()
except socket.error:
pass
else:
# new connection
cnx.setblocking(False)
self.__block_timeout[cnx] = 0
gobject.timeout_add(250, self.__client_handler, cnx)
return True
def __client_handler(self, clientsock):
"""
The client server reactor. A new client gets its own new reactor.
"""
def callback(*retval):
self.__block_timeout[clientsock] = 0
try:
xdr.send(clientsock, *retval)
# hmm, we aren't able to send somthing here
except socket.error, exc:
log("Socket is dead!\n%s\n" % exc)
return False
# the data seems to be of size zero here
except xdr.XDRError, exc:
log("Error: %s\n" % `exc`)
# something really unexpected has happened
except Exception:
try:
xdr.send_error(clientsock)
except Exception:
import traceback; traceback.print_exc()
if (self.__block_timeout[clientsock] > 0):
self.__block_timeout[clientsock] -= 1
if (self.__block_timeout[clientsock] == 0):
callback()
return True
try:
data = xdr.recv(clientsock)
# no data on connected socket
except socket.error:
return True
# socket disconnected
except xdr.XDRError:
clientsock.close()
return False
message = data[0]
args = data[1:]
try:
handler = self.__message_handlers[message]
# message_handler doesn't exist
except KeyError:
log("Key %s doesn't exist in message_handlers!" % (message,))
clientsock.close()
return False
# timeout after 20 * 250 ms
self.__block_timeout[clientsock] = 20
handler(callback, *args)
return True
try:
retval = handler(*args)
if (retval == None): retval = ()
# something bad happened while trying to execute the handler
except Exception:
from utils.ErrorFormatter import ErrorFormatter
details = ErrorFormatter().format(sys.exc_info())
log("Execution of handler (%s, %s) failed!\n%s" % (`handler`,
`args`,
details))
# here we get our broken pipes
retval = ()
try:
xdr.send(clientsock, *retval)
# hmm, we aren't able to send somthing here
except socket.error, exc:
log("Socket is dead!\n%s\n" % exc)
return False
# the data seems to be of size zero here
except xdr.XDRError, exc:
log("Error: %s\n" % `exc`)
# something really unexpected has happened
except Exception:
try:
xdr.send_error(clientsock)
except Exception:
import traceback; traceback.print_exc()
return True
|