Module rsyscall.unistd.cwd

Expand source code Browse git
import typing as t

#### Classes ####
from rsyscall.handle.pointer import WrittenPointer
from rsyscall.handle.fd import BaseFileDescriptor, FileDescriptorTask
import os

class CWDTask(FileDescriptorTask):
    async def chdir(self, path: WrittenPointer[t.Union[str, os.PathLike]]) -> None:
        with path.borrow(self) as path_n:
            await _chdir(self.sysif, path_n)

    async def fchdir(self, fd: BaseFileDescriptor) -> None:
        with fd.borrow(self) as fd_n:
            await _fchdir(self.sysif, fd_n)

    async def chroot(self, path: WrittenPointer[t.Union[str, os.PathLike]]) -> None:
        with path.borrow(self) as path_n:
            await _chroot(self.sysif, path_n)

#### Raw syscalls ####
import rsyscall.near.types as near
from rsyscall.near.sysif import SyscallInterface
from rsyscall.sys.syscall import SYS

async def _chdir(sysif: SyscallInterface, path: near.Address) -> None:
    await sysif.syscall(SYS.chdir, path)

async def _fchdir(sysif: SyscallInterface, fd: near.FileDescriptor) -> None:
    await sysif.syscall(SYS.fchdir, fd)

async def _chroot(sysif: SyscallInterface, path: near.Address) -> None:
    await sysif.syscall(SYS.chroot, path)

Classes

class CWDTask (sysif: SyscallInterface, near_process: Process, fd_table: FDTable, address_space: AddressSpace, pidns: PidNamespace)

A wrapper around SyscallInterface which tracks the namespaces of the underlying process

Note that this is a base class for the more fully featured Task.

We store namespace objects to represent the namespaces that we believe that underlying processes is in. Since we have complete control over the process, we can make sure this belief is accurate, by updating our stored namespaces when the process changes namespace. That isn't done here; it's done in handle.Task.

Currently, we store only one PidNamespace. But each process actually has two pid namespaces:

  • the process's own pid namespace, which determines the pids returned from getpid, clone, and other syscalls.
  • the pid namespace that new children will be in.

The two pid namespaces only differ if we call unshare(CLONE.NEWPID). Currently we don't do that because unshare(CLONE.NEWPID) makes monitoring children more complex, since they can be deleted without leaving a zombie at any time if the pid namespace shuts down. But if we did call unshare(CLONE.NEWPID), we'd need to handle this right.

In the analogy to near and far pointers, this is like a segment register, if a segment register was write-only. Then we'd need to maintain the knowledge of what the segment register was set to, outside the segment register itself. That's what we do here.

There actually were systems where segment registers were, if not quite write-only, at least expensive to set and expensive to read. For example, x86_64 - the FS and GS segment registers can only be set via syscall. If you wanted to use segmentation on such systems, you'd probably have a structure much like this one.

Expand source code Browse git
class CWDTask(FileDescriptorTask):
    async def chdir(self, path: WrittenPointer[t.Union[str, os.PathLike]]) -> None:
        with path.borrow(self) as path_n:
            await _chdir(self.sysif, path_n)

    async def fchdir(self, fd: BaseFileDescriptor) -> None:
        with fd.borrow(self) as fd_n:
            await _fchdir(self.sysif, fd_n)

    async def chroot(self, path: WrittenPointer[t.Union[str, os.PathLike]]) -> None:
        with path.borrow(self) as path_n:
            await _chroot(self.sysif, path_n)

Ancestors

Subclasses

Class variables

var sysifSyscallInterface
var near_processProcess
var fd_tableFDTable
var address_spaceAddressSpace
var pidnsPidNamespace

Methods

async def chdir(self, path: WrittenPointer[typing.Union[str, os.PathLike]]) ‑> NoneType
Expand source code Browse git
async def chdir(self, path: WrittenPointer[t.Union[str, os.PathLike]]) -> None:
    with path.borrow(self) as path_n:
        await _chdir(self.sysif, path_n)
async def fchdir(self, fd: BaseFileDescriptor) ‑> NoneType
Expand source code Browse git
async def fchdir(self, fd: BaseFileDescriptor) -> None:
    with fd.borrow(self) as fd_n:
        await _fchdir(self.sysif, fd_n)
async def chroot(self, path: WrittenPointer[typing.Union[str, os.PathLike]]) ‑> NoneType
Expand source code Browse git
async def chroot(self, path: WrittenPointer[t.Union[str, os.PathLike]]) -> None:
    with path.borrow(self) as path_n:
        await _chroot(self.sysif, path_n)

Inherited members