Skip to content

Commit 55ebcbf

Browse files
committed
Don't crash on OSes with no SO_PEERCRED
1 parent 5fe87b6 commit 55ebcbf

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

mautrix/util/manhole.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77
# Based on https://github.com/nhoad/aiomanhole Copyright (c) 2014, Nathan Hoad
88
from typing import Any, Tuple, Optional, Dict, Union, List, Set, Callable, Type
9-
from socket import SOL_SOCKET, SO_PEERCRED
9+
from socket import SOL_SOCKET
1010
from abc import ABC, abstractmethod
1111
from io import BytesIO, StringIO
1212
from types import CodeType
@@ -22,6 +22,11 @@
2222
import sys
2323
import os
2424

25+
try:
26+
from socket import SO_PEERCRED
27+
except ImportError:
28+
SO_PEERCRED = None
29+
2530
log = logging.getLogger("mau.manhole")
2631

2732

@@ -301,6 +306,8 @@ def conn_id(self) -> int:
301306
async def __call__(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter
302307
) -> None:
303308
sock = writer.transport.get_extra_info("socket")
309+
# TODO support non-linux OSes
310+
# I think FreeBSD uses SCM_CREDS
304311
creds = sock.getsockopt(SOL_SOCKET, SO_PEERCRED, struct.calcsize('3i'))
305312
pid, uid, gid = struct.unpack('3i', creds)
306313
user_info = pwd.getpwuid(uid)
@@ -340,6 +347,8 @@ async def start_manhole(path: str, banner: str = "", namespace: Optional[Dict[st
340347
loop: The asyncio event loop to use.
341348
whitelist: List of user IDs to allow connecting.
342349
"""
350+
if not SO_PEERCRED:
351+
raise ValueError("SO_PEERCRED is not supported on this platform")
343352
loop = loop or asyncio.get_event_loop()
344353
factory = InterpreterFactory(namespace=namespace, banner=banner,
345354
interpreter_class=AsyncInterpreter, loop=loop,

0 commit comments

Comments
 (0)