D-Bus: UNIX-specific internal API

D-Bus 1.16.2

Data Structures | Typedefs | Enumerations | Functions
UNIX-specific internal API
D-Bus secret internal implementation details

Internal system-dependent API available on UNIX only. More...

## Data Structures
struct | DBusUserInfo
| Information about a UNIX user. More...
struct | DBusGroupInfo
| Information about a UNIX group. More...

## Typedefs
typedef struct DBusUserInfo | DBusUserInfo
| Information about a UNIX user.
typedef struct DBusGroupInfo | DBusGroupInfo
| Information about a UNIX group.
typedef void(* | DBusSignalHandler) (int sig)
| A UNIX signal handler.

## Enumerations
enum | DBusEnsureStandardFdsFlags { DBUS_FORCE_STDIN_NULL = (1 << 0) , DBUS_FORCE_STDOUT_NULL = (1 << 1) , DBUS_FORCE_STDERR_NULL = (1 << 2) }

## Functions
DBUS_PRIVATE_EXPORT dbus_bool_t | _dbus_close (int fd, DBusError *error)
| Closes a file descriptor.
DBUS_PRIVATE_EXPORT int | _dbus_dup (int fd, DBusError *error)
| Duplicates a file descriptor.
DBUS_PRIVATE_EXPORT int | _dbus_read (int fd, DBusString *buffer, int count)
| Thin wrapper around the read() system call that appends the data it reads to the DBusString buffer.
int | _dbus_write (int fd, const DBusString *buffer, int start, int len)
| Thin wrapper around the write() system call that writes a part of a DBusString and handles EINTR for you.
int | _dbus_write_two (int fd, const DBusString *buffer1, int start1, int len1, const DBusString *buffer2, int start2, int len2)
| Like _dbus_write() but will use writev() if possible to write both buffers in sequence.
int | _dbus_listen_systemd_sockets (DBusSocket **fd, DBusError *error)
| Acquires one or more sockets passed in from systemd.
dbus_bool_t | _dbus_read_credentials (int client_fd, DBusCredentials *credentials, DBusError *error)
dbus_bool_t | _dbus_send_credentials (int server_fd, DBusError *error)
dbus_bool_t | _dbus_lookup_launchd_socket (DBusString *socket_path, const char *launchd_env_var, DBusError *error)
| quries launchd for a specific env var which holds the socket path.
dbus_bool_t | _dbus_user_info_fill (DBusUserInfo *info, const DBusString *username, DBusError *error)
| Gets user info for the given username.
dbus_bool_t | _dbus_user_info_fill_uid (DBusUserInfo *info, dbus_uid_t uid, DBusError *error)
| Gets user info for the given user ID.
void | _dbus_user_info_free (DBusUserInfo *info)
| Frees the members of info (but not info itself)
dbus_bool_t | _dbus_group_info_fill (DBusGroupInfo *info, const DBusString *groupname, DBusError *error)
| Initializes the given DBusGroupInfo struct with information about the given group name.
dbus_bool_t | _dbus_group_info_fill_gid (DBusGroupInfo *info, dbus_gid_t gid, DBusError *error)
| Initializes the given DBusGroupInfo struct with information about the given group ID.
void | _dbus_group_info_free (DBusGroupInfo *info)
| Frees the members of info (but not info itself).
DBUS_PRIVATE_EXPORT dbus_uid_t | _dbus_geteuid (void)
| Gets our effective UID.
DBUS_PRIVATE_EXPORT void | _dbus_close_all (void)
| Closes all file descriptors except the first three (i.e.
DBUS_PRIVATE_EXPORT void | _dbus_fd_set_all_close_on_exec (void)
| Sets all file descriptors except the first three (i.e.
DBUS_PRIVATE_EXPORT void | _dbus_fd_clear_close_on_exec (int fd)
| Sets the file descriptor to not be close-on-exec.
dbus_bool_t | _dbus_append_address_from_socket (DBusSocket fd, DBusString *address, DBusError *error)
| Read the address from the socket and append it to the string.
DBUS_PRIVATE_EXPORT void | _dbus_fd_set_close_on_exec (int fd)
| Sets the file descriptor to be close on exec.
DBUS_PRIVATE_EXPORT dbus_bool_t | _dbus_ensure_standard_fds (DBusEnsureStandardFdsFlags flags, const char **error_str_p)
| Ensure that the standard file descriptors stdin, stdout and stderr are open, by opening /dev/null if necessary.
void | _dbus_set_signal_handler (int sig, DBusSignalHandler handler)
| Installs a UNIX signal handler.
dbus_bool_t | _dbus_reset_oom_score_adj (const char **error_str_p)
| If the current process has been protected from the Linux OOM killer (the oom_score_adj process parameter is negative), reset it to the default level of protection from the OOM killer (set oom_score_adj to zero).

## Detailed Description

Internal system-dependent API available on UNIX only.

## Typedef Documentation

## ◆ DBusGroupInfo

typedef struct DBusGroupInfo DBusGroupInfo

Information about a UNIX group.

Definition at line 87 of file dbus-sysdeps-unix.h.

## ◆ DBusSignalHandler

typedef void(* DBusSignalHandler) (int sig)

A UNIX signal handler.

Definition at line 158 of file dbus-sysdeps-unix.h.

## ◆ DBusUserInfo

typedef struct DBusUserInfo DBusUserInfo

Information about a UNIX user.

Definition at line 85 of file dbus-sysdeps-unix.h.

## Enumeration Type Documentation

## ◆ DBusEnsureStandardFdsFlags

enum DBusEnsureStandardFdsFlags

Definition at line 146 of file dbus-sysdeps-unix.h.

## Function Documentation

## ◆ _dbus_append_address_from_socket()

dbus_bool_t _dbus_append_address_from_socket | ( | DBusSocket | fd,
|  | DBusString * | address,
|  | DBusError * | error )

Read the address from the socket and append it to the string.

Parameters

  fd | the socket
  address |
  error | return location for error code

Definition at line 5055 of file dbus-sysdeps-unix.c.

References _dbus_address_append_escaped(), _dbus_error_from_errno(), _dbus_string_append(), _dbus_string_append_printf(), _dbus_string_init_const(), dbus_set_error(), FALSE, NULL, and TRUE.

Referenced by _dbus_server_listen_platform_specific().

## ◆ _dbus_close()

DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_close | ( | int | fd,
|  | DBusError * | error )

Closes a file descriptor.

Parameters

  fd | the file descriptor
  error | error object

Returns
  FALSE if error set

Definition at line 3727 of file dbus-sysdeps-unix.c.

References _dbus_error_from_errno(), dbus_set_error(), FALSE, and TRUE.

Referenced by _dbus_close_socket(), _dbus_command_for_pid(), _dbus_create_file_exclusively(), _dbus_file_get_contents(), _dbus_generate_random_bytes(), _dbus_listen_systemd_sockets(), _dbus_listen_tcp_socket(), _dbus_listen_unix_socket(), _dbus_message_iter_get_args_valist(), _dbus_reset_oom_score_adj(), _dbus_socketpair(), _dbus_string_save_to_file(), and dbus_message_iter_append_basic().

## ◆ _dbus_close_all()

DBUS_PRIVATE_EXPORT void _dbus_close_all | ( | void |  | ) |

Closes all file descriptors except the first three (i.e.

stdin, stdout, stderr).

Definition at line 4963 of file dbus-sysdeps-unix.c.

Referenced by _dbus_connect_exec().

## ◆ _dbus_dup()

DBUS_PRIVATE_EXPORT int _dbus_dup | ( | int | fd,
|  | DBusError * | error )

Duplicates a file descriptor.

Makes sure the fd returned is >= 3 (i.e. avoids stdin/stdout/stderr). Sets O_CLOEXEC.

Parameters

  fd | the file descriptor to duplicate
  error | address of error location.

Returns
  duplicated file descriptor

Definition at line 3755 of file dbus-sysdeps-unix.c.

References _dbus_error_from_errno(), _dbus_fd_set_close_on_exec(), and dbus_set_error().

Referenced by _dbus_credentials_add_credential(), _dbus_message_iter_get_args_valist(), _dbus_spawn_async_with_babysitter(), dbus_message_copy(), dbus_message_iter_append_basic(), and dbus_message_iter_get_basic().

## ◆ _dbus_ensure_standard_fds()

DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_ensure_standard_fds | ( | DBusEnsureStandardFdsFlags | flags,
|  | const char ** | error_str_p )

Ensure that the standard file descriptors stdin, stdout and stderr are open, by opening /dev/null if necessary.

This function does not use DBusError, to avoid calling malloc(), so that it can be used in contexts where an async-signal-safe function is required (for example after fork()). Instead, on failure it sets errno and returns something like "Failed to open /dev/null" in *error_str_p. Callers are expected to combine *error_str_p with _dbus_strerror (errno) to get a full error report.

This function can only be called while single-threaded: either during startup of an executable, or after fork().

Definition at line 179 of file dbus-sysdeps-unix.c.

References _dbus_assert, and NULL.

Referenced by _dbus_become_daemon().

## ◆ _dbus_fd_clear_close_on_exec()

DBUS_PRIVATE_EXPORT void _dbus_fd_clear_close_on_exec | ( | int | fd | ) |

Sets the file descriptor to not be close-on-exec.

This can be called after _dbus_fd_set_all_close_on_exec() to make exceptions for pipes used to communicate with child processes.

Parameters

  fd | the file descriptor

Definition at line 3705 of file dbus-sysdeps-unix.c.

## ◆ _dbus_fd_set_all_close_on_exec()

DBUS_PRIVATE_EXPORT void _dbus_fd_set_all_close_on_exec | ( | void |  | ) |

Sets all file descriptors except the first three (i.e.

stdin, stdout, stderr) to be close-on-execute.

Definition at line 4982 of file dbus-sysdeps-unix.c.

References _dbus_fd_set_close_on_exec().

## ◆ _dbus_fd_set_close_on_exec()

DBUS_PRIVATE_EXPORT void _dbus_fd_set_close_on_exec | ( | int | fd | ) |

Sets the file descriptor to be close on exec.

Should be called for all file descriptors in D-Bus code.

Parameters

  fd | the file descriptor

Definition at line 3683 of file dbus-sysdeps-unix.c.

Referenced by _dbus_accept(), _dbus_connect_exec(), _dbus_dup(), _dbus_fd_set_all_close_on_exec(), _dbus_read_socket_with_unix_fds(), _dbus_reset_oom_score_adj(), _dbus_server_new_for_launchd(), _dbus_socketpair(), and _dbus_spawn_async_with_babysitter().

## ◆ _dbus_geteuid()

DBUS_PRIVATE_EXPORT dbus_uid_t _dbus_geteuid | ( | void |  | ) |

Gets our effective UID.

Returns
  process effective UID

Definition at line 3145 of file dbus-sysdeps-unix.c.

Referenced by _dbus_append_user_from_current_process(), _dbus_credentials_add_from_current_process(), _dbus_homedir_from_uid(), and _dbus_unix_user_is_process_owner().

## ◆ _dbus_group_info_fill()

dbus_bool_t _dbus_group_info_fill | ( | DBusGroupInfo * | info,
|  | const DBusString * | groupname,
|  | DBusError * | error )

Initializes the given DBusGroupInfo struct with information about the given group name.

Parameters

  info | the group info struct
  groupname | name of group
  error | the error return

Returns
  FALSE if error is set

Definition at line 884 of file dbus-sysdeps-util-unix.c.

References DBUS_GID_UNSET.

Referenced by _dbus_user_database_lookup_group().

## ◆ _dbus_group_info_fill_gid()

dbus_bool_t _dbus_group_info_fill_gid | ( | DBusGroupInfo * | info,
|  | dbus_gid_t | gid,
|  | DBusError * | error )

Initializes the given DBusGroupInfo struct with information about the given group ID.

Parameters

  info | the group info struct
  gid | group ID
  error | the error return

Returns
  FALSE if error is set

Definition at line 903 of file dbus-sysdeps-util-unix.c.

References NULL.

Referenced by _dbus_user_database_lookup_group().

## ◆ _dbus_group_info_free()

void _dbus_group_info_free | ( | DBusGroupInfo * | info | ) |

Frees the members of info (but not info itself).

Parameters

  info | the group info

Definition at line 121 of file dbus-userdb.c.

References dbus_free(), and DBusGroupInfo::groupname.

Referenced by _dbus_group_info_unref().

## ◆ _dbus_listen_systemd_sockets()

int _dbus_listen_systemd_sockets | ( | DBusSocket ** | fds,
|  | DBusError * | error )

Acquires one or more sockets passed in from systemd.

The sockets are set to be nonblocking.

This will set FD_CLOEXEC for the sockets returned.

Parameters

  fds | the file descriptors
  error | return location for errors

Returns
  the number of file descriptors

Definition at line 1299 of file dbus-sysdeps-unix.c.

References _dbus_close(), _dbus_error_from_errno(), DBUS_ERROR_BAD_ADDRESS, DBUS_ERROR_NO_MEMORY, DBUS_ERROR_NOT_SUPPORTED, dbus_free(), dbus_new, dbus_set_error(), dbus_set_error_const(), NULL, and TRUE.

Referenced by _dbus_server_listen_platform_specific().

## ◆ _dbus_lookup_launchd_socket()

dbus_bool_t _dbus_lookup_launchd_socket | ( | DBusString * | socket_path,
|  | const char * | launchd_env_var,
|  | DBusError * | error )

quries launchd for a specific env var which holds the socket path.

Parameters

  socket_path | append the socket path to this DBusString
  launchd_env_var | the env var to look up
  error | a DBusError to store the error in case of failure

Returns
  the value of the env var

Definition at line 4486 of file dbus-sysdeps-unix.c.

References _dbus_assert, _dbus_check_setuid(), _DBUS_N_ELEMENTS, _dbus_string_shorten(), DBUS_ERROR_NOT_SUPPORTED, dbus_set_error(), dbus_set_error_const(), FALSE, NULL, and TRUE.

Referenced by _dbus_transport_open_platform_specific().

## ◆ _dbus_read()

DBUS_PRIVATE_EXPORT int _dbus_read | ( | int | fd,
|  | DBusString * | buffer,
|  | int | count )

Thin wrapper around the read() system call that appends the data it reads to the DBusString buffer.

It appends up to the given count, and returns the same value and same errno as read(). The only exception is that _dbus_read() handles EINTR for you. Also, _dbus_read() can return ENOMEM, even though regular UNIX read doesn't.

Unlike _dbus_read_socket(), _dbus_read() is not available on Windows.

Parameters

  fd | the file descriptor to read from
  buffer | the buffer to append data to
  count | the amount of data to read

Returns
  the number of bytes read or -1

Definition at line 767 of file dbus-sysdeps-unix.c.

References _dbus_assert, _dbus_string_get_data_len(), _dbus_string_lengthen(), _dbus_string_set_length(), and _dbus_verbose_bytes_of_string().

Referenced by _dbus_command_for_pid(), _dbus_file_get_contents(), _dbus_generate_random_bytes(), and _dbus_read_socket().

## ◆ _dbus_reset_oom_score_adj()

dbus_bool_t _dbus_reset_oom_score_adj | ( | const char ** | error_str_p | ) |

If the current process has been protected from the Linux OOM killer (the oom_score_adj process parameter is negative), reset it to the default level of protection from the OOM killer (set oom_score_adj to zero).

This function does not use DBusError, to avoid calling malloc(), so that it can be used in contexts where an async-signal-safe function is required (for example after fork()). Instead, on failure it sets errno and returns something like "Failed to open /dev/null" in *error_str_p. Callers are expected to combine *error_str_p with _dbus_strerror (errno) to get a full error report.

Definition at line 1602 of file dbus-sysdeps-util-unix.c.

References _dbus_close(), _dbus_fd_set_close_on_exec(), FALSE, NULL, and TRUE.

Referenced by _dbus_spawn_async_with_babysitter().

## ◆ _dbus_set_signal_handler()

void _dbus_set_signal_handler | ( | int | sig,
|  | DBusSignalHandler | handler )

Installs a UNIX signal handler.

Parameters

  sig | the signal to handle
  handler | the handler

Definition at line 545 of file dbus-sysdeps-util-unix.c.

References NULL.

## ◆ _dbus_user_info_fill()

dbus_bool_t _dbus_user_info_fill | ( | DBusUserInfo * | info,
|  | const DBusString * | username,
|  | DBusError * | error )

Gets user info for the given username.

Parameters

  info | user info object to initialize
  username | the username
  error | error return

Returns
  TRUE on success

Definition at line 2966 of file dbus-sysdeps-unix.c.

References DBUS_UID_UNSET.

Referenced by _dbus_user_database_lookup().

## ◆ _dbus_user_info_fill_uid()

dbus_bool_t _dbus_user_info_fill_uid | ( | DBusUserInfo * | info,
|  | dbus_uid_t | uid,
|  | DBusError * | error )

Gets user info for the given user ID.

Parameters

  info | user info object to initialize
  uid | the user ID
  error | error return

Returns
  TRUE on success

Definition at line 2983 of file dbus-sysdeps-unix.c.

References NULL.

Referenced by _dbus_user_database_lookup().

## ◆ _dbus_user_info_free()

void _dbus_user_info_free | ( | DBusUserInfo * | info | ) |

Frees the members of info (but not info itself)

Parameters

  info | the user info struct

Definition at line 108 of file dbus-userdb.c.

References dbus_free(), DBusUserInfo::group_ids, DBusUserInfo::homedir, and DBusUserInfo::username.

Referenced by _dbus_user_info_unref().

## ◆ _dbus_write()

int _dbus_write | ( | int | fd,
|  | const DBusString * | buffer,
|  | int | start,
|  | int | len )

Thin wrapper around the write() system call that writes a part of a DBusString and handles EINTR for you.

Parameters

  fd | the file descriptor to write
  buffer | the buffer to write data from
  start | the first byte in the buffer to write
  len | the number of bytes to try to write

Returns
  the number of bytes written or -1 on error

Definition at line 827 of file dbus-sysdeps-unix.c.

References _dbus_verbose_bytes_of_string().

Referenced by _dbus_string_save_to_file(), _dbus_write_socket(), and _dbus_write_two().

## ◆ _dbus_write_two()

int _dbus_write_two | ( | int | fd,
|  | const DBusString * | buffer1,
|  | int | start1,
|  | int | len1,
|  | const DBusString * | buffer2,
|  | int | start2,
|  | int | len2 )

Like _dbus_write() but will use writev() if possible to write both buffers in sequence.

The return value is the number of bytes written in the first buffer, plus the number written in the second. If the first buffer is written successfully and an error occurs writing the second, the number of bytes in the first is returned (i.e. the error is ignored), on systems that don't have writev. Handles EINTR for you. The second buffer may be NULL.

Parameters

  fd | the file descriptor
  buffer1 | first buffer
  start1 | first byte to write in first buffer
  len1 | number of bytes to write from first buffer
  buffer2 | second buffer, or NULL
  start2 | first byte to write in second buffer
  len2 | number of bytes to write in second buffer

Returns
  total bytes written from both buffers, or -1 on error

Definition at line 873 of file dbus-sysdeps-unix.c.

References _dbus_assert, _dbus_write(), and NULL.

Referenced by _dbus_write_socket_two().