static gboolean transport_mapper_privileged_bind(gint sock, GSockAddr *bind_addr) { cap_t saved_caps; GIOStatus status; saved_caps = g_process_cap_save(); g_process_cap_modify(CAP_NET_BIND_SERVICE, TRUE); g_process_cap_modify(CAP_DAC_OVERRIDE, TRUE); status = g_bind(sock, bind_addr); g_process_cap_restore(saved_caps); return status == G_IO_STATUS_NORMAL; }
void control_server_start(ControlServer *s) { ControlServerUnix *self = (ControlServerUnix *)s; GSockAddr *saddr; saddr = g_sockaddr_unix_new(self->super.control_socket_name); self->control_socket = socket(PF_UNIX, SOCK_STREAM, 0); if (self->control_socket == -1) { msg_error("Error opening control socket, external controls will not be available", evt_tag_str("socket", self->super.control_socket_name), NULL); return; } if (g_bind(self->control_socket, saddr) != G_IO_STATUS_NORMAL) { msg_error("Error opening control socket, bind() failed", evt_tag_str("socket", self->super.control_socket_name), evt_tag_errno("error", errno), NULL); goto error; } if (listen(self->control_socket, 255) < 0) { msg_error("Error opening control socket, listen() failed", evt_tag_str("socket", self->super.control_socket_name), evt_tag_errno("error", errno), NULL); goto error; } self->control_listen.fd = self->control_socket; self->control_listen.cookie = self; iv_fd_register(&self->control_listen); iv_fd_set_handler_in(&self->control_listen, control_socket_accept); g_sockaddr_unref(saddr); return; error: if (self->control_socket != -1) { close(self->control_socket); self->control_socket = -1; } g_sockaddr_unref(saddr); return; }
void control_init(const gchar *control_name) { GSockAddr *saddr; saddr = g_sockaddr_unix_new(control_name); control_socket = socket(PF_UNIX, SOCK_STREAM, 0); if (control_socket == -1) { msg_error("Error opening control socket, external controls will not be available", evt_tag_str("socket", control_name), NULL); return; } if (g_bind(control_socket, saddr) != G_IO_STATUS_NORMAL) { msg_error("Error opening control socket, bind() failed", evt_tag_str("socket", control_name), evt_tag_errno("error", errno), NULL); goto error; } if (listen(control_socket, 255) < 0) { msg_error("Error opening control socket, listen() failed", evt_tag_str("socket", control_name), evt_tag_errno("error", errno), NULL); goto error; } IV_FD_INIT(&control_listen); control_listen.fd = control_socket; control_listen.handler_in = control_socket_accept; iv_fd_register(&control_listen); g_sockaddr_unref(saddr); return; error: if (control_socket != -1) { close(control_socket); control_socket = -1; } g_sockaddr_unref(saddr); }
void control_init(const gchar *control_name, GMainContext *main_context) { GSockAddr *saddr; GSource *source; saddr = g_sockaddr_unix_new(control_name); control_socket = socket(PF_UNIX, SOCK_STREAM, 0); if (control_socket == -1) { msg_error("Error opening control socket, external controls will not be available", evt_tag_str("socket", control_name), NULL); return; } if (g_bind(control_socket, saddr) != G_IO_STATUS_NORMAL) { msg_error("Error opening control socket, bind() failed", evt_tag_str("socket", control_name), evt_tag_errno("error", errno), NULL); goto error; } if (listen(control_socket, 255) < 0) { msg_error("Error opening control socket, listen() failed", evt_tag_str("socket", control_name), evt_tag_errno("error", errno), NULL); goto error; } source = g_listen_source_new(control_socket); g_source_set_callback(source, control_socket_accept, NULL, NULL); g_source_attach(source, main_context); g_source_unref(source); g_sockaddr_unref(saddr); return; error: if (control_socket != -1) { close(control_socket); control_socket = -1; } g_sockaddr_unref(saddr); }
static gboolean afsocket_open_socket(GSockAddr *bind_addr, int stream_or_dgram, int *fd) { gint sock; if (stream_or_dgram) sock = socket(bind_addr->sa.sa_family, SOCK_STREAM, 0); else sock = socket(bind_addr->sa.sa_family, SOCK_DGRAM, 0); if (sock != -1) { cap_t saved_caps; g_fd_set_nonblock(sock, TRUE); g_fd_set_cloexec(sock, TRUE); saved_caps = g_process_cap_save(); g_process_cap_modify(CAP_NET_BIND_SERVICE, TRUE); g_process_cap_modify(CAP_DAC_OVERRIDE, TRUE); if (g_bind(sock, bind_addr) != G_IO_STATUS_NORMAL) { gchar buf[256]; g_process_cap_restore(saved_caps); msg_error("Error binding socket", evt_tag_str("addr", g_sockaddr_format(bind_addr, buf, sizeof(buf), GSA_FULL)), evt_tag_errno(EVT_TAG_OSERROR, errno), NULL); close(sock); return FALSE; } g_process_cap_restore(saved_caps); *fd = sock; return TRUE; } else { msg_error("Error creating socket", evt_tag_errno(EVT_TAG_OSERROR, errno), NULL); return FALSE; } }