GDBserver

Classes | Macros | Enumerations | Functions | Variables
linux-namespaces.c File Reference
`#include "common-defs.h"`
`#include "nat/linux-namespaces.h"`
`#include "filestuff.h"`
`#include <fcntl.h>`
`#include <sys/syscall.h>`
`#include <sys/types.h>`
`#include <sys/stat.h>`
`#include <sys/socket.h>`
`#include "gdb_wait.h"`
`#include <signal.h>`
`#include <sched.h>`

Go to the source code of this file.

## Classes
struct | linux_ns
struct | linux_mnsh

## Macros
#define | MSG_CMSG_CLOEXEC 0
#define | mnsh_return_int(sock, result, error) mnsh_send_message (sock, MNSH_RET_INT, -1, result, error, NULL, 0)
#define | mnsh_return_fd(sock, fd, error)
#define | mnsh_return_intstr(sock, result, buf, bufsiz, error)
#define | mnsh_send_setns(helper, fd, nstype)
#define | mnsh_send_open(helper, filename, flags, mode)
#define | mnsh_send_unlink(helper, filename)
#define | mnsh_send_readlink(helper, filename)

## Enumerations
enum | mnsh_msg_type { MNSH_MSG_ERROR, MNSH_REQ_SETNS, MNSH_REQ_OPEN, MNSH_REQ_UNLINK, MNSH_REQ_READLINK, MNSH_RET_INT, MNSH_RET_FD, MNSH_RET_INTSTR }
enum | mnsh_fs_code { MNSH_FS_ERROR = -1, MNSH_FS_DIRECT, MNSH_FS_HELPER }

## Functions
static pid_t | do_fork (void)
static int | do_setns (int fd, int nstype)
static const char * | linux_ns_filename (struct linux_ns *ns, int pid)
static struct linux_ns * | linux_ns_get_namespace (enum linux_ns_type type)
int | linux_ns_same (pid_t pid, enum linux_ns_type type)
static void | mnsh_debug_print_message (enum mnsh_msg_type type, int fd, int int1, int int2, const void *buf, int bufsiz)
static void | mnsh_maybe_mourn_peer (void)
static ssize_t | mnsh_send_message (int sock, enum mnsh_msg_type type, int fd, int int1, int int2, const void *buf, int bufsiz)
static ssize_t | mnsh_recv_message (int sock, enum mnsh_msg_type *type, int *fd, int *int1, int *int2, void *buf, int bufsiz)
static ssize_t | mnsh_handle_setns (int sock, int fd, int nstype)
static ssize_t | mnsh_handle_open (int sock, const char *filename, int flags, mode_t mode)
static ssize_t | mnsh_handle_unlink (int sock, const char *filename)
static ssize_t | mnsh_handle_readlink (int sock, const char *filename)
static void | mnsh_main (int sock) ATTRIBUTE_NORETURN
static struct linux_mnsh * | linux_mntns_get_helper (void)
static int | mnsh_recv_int (struct linux_mnsh *helper, int *result, int *error)
static int | mnsh_recv_fd (struct linux_mnsh *helper, int *fd, int *error)
static ssize_t | mnsh_recv_intstr (struct linux_mnsh *helper, int *result, int *error, void *buf, int bufsiz)
static enum mnsh_fs_code | linux_mntns_access_fs (pid_t pid)
int | linux_mntns_open_cloexec (pid_t pid, const char *filename, int flags, mode_t mode)
int | linux_mntns_unlink (pid_t pid, const char *filename)
ssize_t | linux_mntns_readlink (pid_t pid, const char *filename, char *buf, size_t bufsiz)

## Variables
int | debug_linux_namespaces
static int | mnsh_creator_pid = 0

## Macro Definition Documentation

## ◆ mnsh_return_fd

#define mnsh_return_fd | ( |  | sock,
|  |  | fd,
|  |  | error
| ) |  |

Value:
mnsh_send_message (sock, MNSH_RET_FD, \
(fd) < 0 ? -1 : (fd), \
(fd) < 0 ? (fd) : 0, \
error, NULL, 0)
MNSH_RET_FD
Definition: linux-namespaces.c:260
mnsh_send_message
static ssize_t mnsh_send_message(int sock, enum mnsh_msg_type type, int fd, int int1, int int2, const void *buf, int bufsiz)
Definition: linux-namespaces.c:336
error
void error(const char *fmt,...)
Definition: errors.c:38

Definition at line 496 of file linux-namespaces.c.

Referenced by mnsh_handle_open().

## ◆ mnsh_return_int

#define mnsh_return_int | ( |  | sock,
|  |  | result,
|  |  | error
| ) |  | mnsh_send_message (sock, MNSH_RET_INT, -1, result, error, NULL, 0)

Definition at line 493 of file linux-namespaces.c.

Referenced by mnsh_handle_setns(), and mnsh_handle_unlink().

## ◆ mnsh_return_intstr

#define mnsh_return_intstr | ( |  | sock,
|  |  | result,
|  |  | buf,
|  |  | bufsiz,
|  |  | error
| ) |  |

Value:
mnsh_send_message (sock, MNSH_RET_INTSTR, -1, result, error, \
buf, bufsiz)
MNSH_RET_INTSTR
Definition: linux-namespaces.c:264
mnsh_send_message
static ssize_t mnsh_send_message(int sock, enum mnsh_msg_type type, int fd, int int1, int int2, const void *buf, int bufsiz)
Definition: linux-namespaces.c:336
error
void error(const char *fmt,...)
Definition: errors.c:38

Definition at line 502 of file linux-namespaces.c.

Referenced by mnsh_handle_readlink().

## ◆ mnsh_send_open

#define mnsh_send_open | ( |  | helper,
|  |  | filename,
|  |  | flags,
|  |  | mode
| ) |  |

Value:
mnsh_send_message (helper->sock, MNSH_REQ_OPEN, -1, flags, mode, \
filename, strlen (filename) + 1)
mnsh_send_message
static ssize_t mnsh_send_message(int sock, enum mnsh_msg_type type, int fd, int int1, int int2, const void *buf, int bufsiz)
Definition: linux-namespaces.c:336
MNSH_REQ_OPEN
Definition: linux-namespaces.c:239

Definition at line 771 of file linux-namespaces.c.

Referenced by linux_mntns_open_cloexec().

## ◆ mnsh_send_readlink

#define mnsh_send_readlink | ( |  | helper,
|  |  | filename
| ) |  |

Value:
mnsh_send_message (helper->sock, MNSH_REQ_READLINK, -1, 0, 0, \
filename, strlen (filename) + 1)
mnsh_send_message
static ssize_t mnsh_send_message(int sock, enum mnsh_msg_type type, int fd, int int1, int int2, const void *buf, int bufsiz)
Definition: linux-namespaces.c:336
MNSH_REQ_READLINK
Definition: linux-namespaces.c:251

Definition at line 779 of file linux-namespaces.c.

Referenced by linux_mntns_readlink().

## ◆ mnsh_send_setns

#define mnsh_send_setns | ( |  | helper,
|  |  | fd,
|  |  | nstype
| ) |  |

Value:
mnsh_send_message (helper->sock, MNSH_REQ_SETNS, fd, nstype, 0, \
NULL, 0)
MNSH_REQ_SETNS
Definition: linux-namespaces.c:233
mnsh_send_message
static ssize_t mnsh_send_message(int sock, enum mnsh_msg_type type, int fd, int int1, int int2, const void *buf, int bufsiz)
Definition: linux-namespaces.c:336

Definition at line 767 of file linux-namespaces.c.

Referenced by linux_mntns_access_fs().

## ◆ mnsh_send_unlink

#define mnsh_send_unlink | ( |  | helper,
|  |  | filename
| ) |  |

Value:
mnsh_send_message (helper->sock, MNSH_REQ_UNLINK, -1, 0, 0, \
filename, strlen (filename) + 1)
MNSH_REQ_UNLINK
Definition: linux-namespaces.c:245
mnsh_send_message
static ssize_t mnsh_send_message(int sock, enum mnsh_msg_type type, int fd, int int1, int int2, const void *buf, int bufsiz)
Definition: linux-namespaces.c:336

Definition at line 775 of file linux-namespaces.c.

Referenced by linux_mntns_unlink().

## ◆ MSG_CMSG_CLOEXEC

#define MSG_CMSG_CLOEXEC 0

Definition at line 66 of file linux-namespaces.c.

Referenced by mnsh_recv_message().

## Enumeration Type Documentation

## ◆ mnsh_fs_code

enum mnsh_fs_code

Enumerator
MNSH_FS_ERROR |
MNSH_FS_DIRECT |
MNSH_FS_HELPER |

Definition at line 870 of file linux-namespaces.c.

## ◆ mnsh_msg_type

enum mnsh_msg_type

Enumerator
MNSH_MSG_ERROR |
MNSH_REQ_SETNS |
MNSH_REQ_OPEN |
MNSH_REQ_UNLINK |
MNSH_REQ_READLINK |
MNSH_RET_INT |
MNSH_RET_FD |
MNSH_RET_INTSTR |

Definition at line 221 of file linux-namespaces.c.

## Function Documentation

## ◆ do_fork()

static pid_t do_fork | ( | void |  | ) |
| inline static

Definition at line 38 of file linux-namespaces.c.

References errno.

Referenced by linux_mntns_get_helper().

## ◆ do_setns()

static int do_setns | ( | int | fd,
|  | int | nstype
| ) |  |
| inline static

Definition at line 51 of file linux-namespaces.c.

References errno.

Referenced by mnsh_handle_setns().

## ◆ linux_mntns_access_fs()

static enum mnsh_fs_code linux_mntns_access_fs | ( | pid_t | pid | ) |
| static

Definition at line 888 of file linux-namespaces.c.

References do_cleanups(), errno, error(), gdb_open_cloexec(), linux_ns::id, linux_mntns_get_helper(), linux_ns_filename(), linux_ns_get_namespace(), LINUX_NS_MNT, make_cleanup(), make_cleanup_close(), MNSH_FS_DIRECT, MNSH_FS_ERROR, MNSH_FS_HELPER, mnsh_recv_int(), mnsh_send_setns, linux_mnsh::nsid, null_cleanup(), and linux_mnsh::pid.

Referenced by linux_mntns_open_cloexec(), linux_mntns_readlink(), and linux_mntns_unlink().

## ◆ linux_mntns_get_helper()

static struct linux_mnsh* linux_mntns_get_helper | ( | void |  | ) |
| static

Definition at line 642 of file linux-namespaces.c.

References debug_linux_namespaces, debug_printf(), do_fork(), errno, gdb_socketpair_cloexec(), linux_ns::id, linux_ns_get_namespace(), LINUX_NS_MNT, mnsh_creator_pid, mnsh_main(), linux_mnsh::nsid, linux_mnsh::pid, and linux_mnsh::sock.

Referenced by linux_mntns_access_fs(), linux_mntns_open_cloexec(), linux_mntns_readlink(), linux_mntns_unlink(), and mnsh_maybe_mourn_peer().

## ◆ linux_mntns_open_cloexec()

int linux_mntns_open_cloexec | ( | pid_t | pid,
|  | const char * | filename,
|  | int | flags,
|  | mode_t | mode
| ) |  |

Definition at line 970 of file linux-namespaces.c.

References errno, error(), gdb_assert, gdb_open_cloexec(), linux_mntns_access_fs(), linux_mntns_get_helper(), MNSH_FS_DIRECT, MNSH_FS_ERROR, MNSH_FS_HELPER, mnsh_recv_fd(), mnsh_send_open, and linux_mnsh::pid.

## ◆ linux_mntns_readlink()

ssize_t linux_mntns_readlink | ( | pid_t | pid,
|  | const char * | filename,
|  | char * | buf,
|  | size_t | bufsiz
| ) |  |

Definition at line 1037 of file linux-namespaces.c.

References errno, error(), gdb_assert, linux_mntns_access_fs(), linux_mntns_get_helper(), MNSH_FS_DIRECT, MNSH_FS_ERROR, MNSH_FS_HELPER, mnsh_recv_intstr(), mnsh_send_readlink, and linux_mnsh::pid.

## ◆ linux_mntns_unlink()

int linux_mntns_unlink | ( | pid_t | pid,
|  | const char * | filename
| ) |  |

Definition at line 1004 of file linux-namespaces.c.

References errno, error(), gdb_assert, linux_mntns_access_fs(), linux_mntns_get_helper(), MNSH_FS_DIRECT, MNSH_FS_ERROR, MNSH_FS_HELPER, mnsh_recv_int(), mnsh_send_unlink, and linux_mnsh::pid.

## ◆ linux_ns_filename()

static const char* linux_ns_filename | ( | struct linux_ns * | ns,
|  | int | pid
| ) |  |
| static

Definition at line 94 of file linux-namespaces.c.

References linux_ns::filename, gdb_assert, and xsnprintf().

Referenced by linux_mntns_access_fs(), linux_ns_get_namespace(), and linux_ns_same().

## ◆ linux_ns_get_namespace()

static struct linux_ns* linux_ns_get_namespace | ( | enum linux_ns_type | type | ) |
| static

Definition at line 109 of file linux-namespaces.c.

References gdb_assert, linux_ns::id, linux_ns::initialized, linux_ns_filename(), NUM_LINUX_NS_TYPES, and linux_ns::supported.

Referenced by linux_mntns_access_fs(), linux_mntns_get_helper(), and linux_ns_same().

## ◆ linux_ns_same()

int linux_ns_same | ( | pid_t | pid,
|  | enum linux_ns_type | type
| ) |  |

Definition at line 145 of file linux-namespaces.c.

References linux_ns::filename, linux_ns::id, linux_ns_filename(), linux_ns_get_namespace(), and perror_with_name().

## ◆ mnsh_debug_print_message()

static void mnsh_debug_print_message | ( | enum mnsh_msg_type | type,
|  | int | fd,
|  | int | int1,
|  | int | int2,
|  | const void * | buf,
|  | int | bufsiz
| ) |  |
| static

Definition at line 272 of file linux-namespaces.c.

References debug_printf(), MNSH_MSG_ERROR, MNSH_REQ_OPEN, MNSH_REQ_READLINK, MNSH_REQ_SETNS, MNSH_REQ_UNLINK, MNSH_RET_FD, MNSH_RET_INT, and MNSH_RET_INTSTR.

Referenced by mnsh_recv_message(), and mnsh_send_message().

## ◆ mnsh_handle_open()

static ssize_t mnsh_handle_open | ( | int | sock,
|  | const char * | filename,
|  | int | flags,
|  | mode_t | mode
| ) |  |
| static

Definition at line 519 of file linux-namespaces.c.

References errno, gdb_open_cloexec(), and mnsh_return_fd.

Referenced by mnsh_main().

## ◆ mnsh_handle_readlink()

static ssize_t mnsh_handle_readlink | ( | int | sock,
|  | const char * | filename
| ) |  |
| static

Definition at line 544 of file linux-namespaces.c.

References errno, and mnsh_return_intstr.

Referenced by mnsh_main().

## ◆ mnsh_handle_setns()

static ssize_t mnsh_handle_setns | ( | int | sock,
|  | int | fd,
|  | int | nstype
| ) |  |
| static

Definition at line 509 of file linux-namespaces.c.

References do_setns(), errno, and mnsh_return_int.

Referenced by mnsh_main().

## ◆ mnsh_handle_unlink()

static ssize_t mnsh_handle_unlink | ( | int | sock,
|  | const char * | filename
| ) |  |
| static

Definition at line 534 of file linux-namespaces.c.

References errno, and mnsh_return_int.

Referenced by mnsh_main().

## ◆ mnsh_main()

static void mnsh_main | ( | int | sock | ) |
| static

Definition at line 559 of file linux-namespaces.c.

References mnsh_handle_open(), mnsh_handle_readlink(), mnsh_handle_setns(), mnsh_handle_unlink(), MNSH_MSG_ERROR, mnsh_recv_message(), MNSH_REQ_OPEN, MNSH_REQ_READLINK, MNSH_REQ_SETNS, MNSH_REQ_UNLINK, and mnsh_send_message().

Referenced by linux_mntns_get_helper().

## ◆ mnsh_maybe_mourn_peer()

static void mnsh_maybe_mourn_peer | ( | void |  | ) |
| static

Definition at line 705 of file linux-namespaces.c.

References _, errno, internal_warning(), linux_mntns_get_helper(), mnsh_creator_pid, linux_mnsh::pid, warning(), WEXITSTATUS, WIFEXITED, WIFSIGNALED, WNOHANG, and WTERMSIG.

Referenced by mnsh_recv_message(), and mnsh_send_message().

## ◆ mnsh_recv_fd()

static int mnsh_recv_fd | ( | struct linux_mnsh * | helper,
|  | int * | fd,
|  | int * | error
| ) |  |
| static

Definition at line 815 of file linux-namespaces.c.

References error(), gdb_assert, mnsh_recv_message(), MNSH_RET_FD, and linux_mnsh::sock.

Referenced by linux_mntns_open_cloexec().

## ◆ mnsh_recv_int()

static int mnsh_recv_int | ( | struct linux_mnsh * | helper,
|  | int * | result,
|  | int * | error
| ) |  |
| static

Definition at line 789 of file linux-namespaces.c.

References error(), gdb_assert, mnsh_recv_message(), MNSH_RET_INT, and linux_mnsh::sock.

Referenced by linux_mntns_access_fs(), and linux_mntns_unlink().

## ◆ mnsh_recv_intstr()

static ssize_t mnsh_recv_intstr | ( | struct linux_mnsh * | helper,
|  | int * | result,
|  | int * | error,
|  | void * | buf,
|  | int | bufsiz
| ) |  |
| static

Definition at line 847 of file linux-namespaces.c.

References error(), gdb_assert, mnsh_recv_message(), MNSH_RET_INTSTR, and linux_mnsh::sock.

Referenced by linux_mntns_readlink().

## ◆ mnsh_recv_message()

static ssize_t mnsh_recv_message | ( | int | sock,
|  | enum mnsh_msg_type * | type,
|  | int * | fd,
|  | int * | int1,
|  | int * | int2,
|  | void * | buf,
|  | int | bufsiz
| ) |  |
| static

Definition at line 411 of file linux-namespaces.c.

References debug_linux_namespaces, debug_printf(), errno, memset(), mnsh_debug_print_message(), mnsh_maybe_mourn_peer(), MSG_CMSG_CLOEXEC, and pulongest().

Referenced by mnsh_main(), mnsh_recv_fd(), mnsh_recv_int(), and mnsh_recv_intstr().

## ◆ mnsh_send_message()

static ssize_t mnsh_send_message | ( | int | sock,
|  | enum mnsh_msg_type | type,
|  | int | fd,
|  | int | int1,
|  | int | int2,
|  | const void * | buf,
|  | int | bufsiz
| ) |  |
| static

Definition at line 336 of file linux-namespaces.c.

References alloca(), debug_linux_namespaces, debug_printf(), memset(), mnsh_debug_print_message(), mnsh_maybe_mourn_peer(), and pulongest().

Referenced by mnsh_main().

## Variable Documentation

## ◆ debug_linux_namespaces

int debug_linux_namespaces

Definition at line 33 of file linux-namespaces.c.

Referenced by linux_mntns_get_helper(), mnsh_recv_message(), and mnsh_send_message().

## ◆ mnsh_creator_pid

int mnsh_creator_pid = 0
| static

Definition at line 634 of file linux-namespaces.c.

Referenced by linux_mntns_get_helper(), and mnsh_maybe_mourn_peer().