本文整理汇总了C++中sd_event_add_io函数的典型用法代码示例。如果您正苦于以下问题:C++ sd_event_add_io函数的具体用法?C++ sd_event_add_io怎么用?C++ sd_event_add_io使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sd_event_add_io函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: button_open
int button_open(Button *b) {
char *p, name[256];
int r;
assert(b);
b->fd = safe_close(b->fd);
p = strjoina("/dev/input/", b->name);
b->fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
if (b->fd < 0)
return log_warning_errno(errno, "Failed to open %s: %m", b->name);
if (ioctl(b->fd, EVIOCGNAME(sizeof(name)), name) < 0) {
r = log_error_errno(errno, "Failed to get input name: %m");
goto fail;
}
r = sd_event_add_io(b->manager->event, &b->io_event_source, b->fd, EPOLLIN, button_dispatch, b);
if (r < 0) {
log_error_errno(r, "Failed to add button event: %m");
goto fail;
}
log_info("Watching system buttons on /dev/input/%s (%s)", b->name, name);
return 0;
fail:
b->fd = safe_close(b->fd);
return r;
}
示例2: automount_coldplug
static int automount_coldplug(Unit *u) {
Automount *a = AUTOMOUNT(u);
int r;
assert(a);
assert(a->state == AUTOMOUNT_DEAD);
if (a->deserialized_state != a->state) {
r = open_dev_autofs(u->manager);
if (r < 0)
return r;
if (a->deserialized_state == AUTOMOUNT_WAITING ||
a->deserialized_state == AUTOMOUNT_RUNNING) {
assert(a->pipe_fd >= 0);
r = sd_event_add_io(u->manager->event, a->pipe_fd, EPOLLIN, automount_dispatch_io, u, &a->pipe_event_source);
if (r < 0)
return r;
}
automount_set_state(a, a->deserialized_state);
}
return 0;
}
示例3: sd_lldp_start
_public_ int sd_lldp_start(sd_lldp *lldp) {
int r;
assert_return(lldp, -EINVAL);
if (lldp->fd >= 0)
return 0;
assert(!lldp->io_event_source);
lldp->fd = lldp_network_bind_raw_socket(lldp->ifindex);
if (lldp->fd < 0)
return lldp->fd;
if (lldp->event) {
r = sd_event_add_io(lldp->event, &lldp->io_event_source, lldp->fd, EPOLLIN, lldp_receive_datagram, lldp);
if (r < 0)
goto fail;
r = sd_event_source_set_priority(lldp->io_event_source, lldp->event_priority);
if (r < 0)
goto fail;
(void) sd_event_source_set_description(lldp->io_event_source, "lldp-io");
}
return 1;
fail:
lldp->io_event_source = sd_event_source_unref(lldp->io_event_source);
lldp->fd = safe_close(lldp->fd);
return r;
}
示例4: dns_transaction_emit
static int dns_transaction_emit(DnsTransaction *t) {
int r;
assert(t);
if (t->scope->protocol == DNS_PROTOCOL_DNS && !t->server) {
DnsServer *server = NULL;
_cleanup_close_ int fd = -1;
fd = dns_scope_udp_dns_socket(t->scope, &server);
if (fd < 0)
return fd;
r = sd_event_add_io(t->scope->manager->event, &t->dns_udp_event_source, fd, EPOLLIN, on_dns_packet, t);
if (r < 0)
return r;
t->dns_udp_fd = fd;
fd = -1;
t->server = dns_server_ref(server);
}
r = dns_scope_emit(t->scope, t->dns_udp_fd, t->sent);
if (r < 0)
return r;
return 0;
}
示例5: busname_watch_fd
static int busname_watch_fd(BusName *n) {
int r;
assert(n);
if (n->starter_fd < 0)
return 0;
if (n->starter_event_source) {
r = sd_event_source_set_enabled(n->starter_event_source, SD_EVENT_ON);
if (r < 0)
goto fail;
} else {
r = sd_event_add_io(UNIT(n)->manager->event, &n->starter_event_source, n->starter_fd, EPOLLIN, busname_dispatch_io, n);
if (r < 0)
goto fail;
(void) sd_event_source_set_description(n->starter_event_source, "busname-starter");
}
return 0;
fail:
log_unit_warning_errno(UNIT(n), r, "Failed to watch starter fd: %m");
busname_unwatch_fd(n);
return r;
}
示例6: DbusApiInit
void DbusApiInit(int sock)
{
fd = sock;
sd_event_source *busSource = NULL;
sd_bus_slot *slot = NULL;
int ret = sd_event_default(&event);
char tmp = '0';
read(fd, &tmp, sizeof(char));
ret = sd_bus_open_system(&bus);
ret = sd_bus_add_object_vtable(bus, &slot, "/org/watchdogd1",
"org.watchdogd1", watchdogPmon, NULL);
ret = sd_bus_request_name(bus, "org.watchdogd1", 0);
if (ret < 0) {
ReloadDbusDaemon();
ret = sd_bus_request_name(bus, "org.watchdogd1", 0);
}
sd_event_add_io(event, &busSource, sd_bus_get_fd(bus), EPOLLIN, BusHandler, NULL);
sd_event_loop(event);
}
示例7: automount_coldplug
static int automount_coldplug(Unit *u) {
Automount *a = AUTOMOUNT(u);
int r;
assert(a);
assert(a->state == AUTOMOUNT_DEAD);
if (a->deserialized_state != a->state) {
r = open_dev_autofs(u->manager);
if (r < 0)
return r;
if (a->deserialized_state == AUTOMOUNT_WAITING ||
a->deserialized_state == AUTOMOUNT_RUNNING) {
assert(a->pipe_fd >= 0);
r = sd_event_add_io(u->manager->event, &a->pipe_event_source, a->pipe_fd, EPOLLIN, automount_dispatch_io, u);
if (r < 0)
return r;
(void) sd_event_source_set_description(a->pipe_event_source, "automount-io");
if (a->deserialized_state == AUTOMOUNT_RUNNING) {
r = automount_start_expire(a);
if (r < 0)
log_unit_warning_errno(UNIT(a), r, "Failed to start expiration timer, ignoring: %m");
}
}
automount_set_state(a, a->deserialized_state);
}
return 0;
}
示例8: raw_export_start
int raw_export_start(RawExport *e, const char *path, int fd, ImportCompressType compress) {
_cleanup_close_ int sfd = -1, tfd = -1;
int r;
assert(e);
assert(path);
assert(fd >= 0);
assert(compress < _IMPORT_COMPRESS_TYPE_MAX);
assert(compress != IMPORT_COMPRESS_UNKNOWN);
if (e->output_fd >= 0)
return -EBUSY;
r = fd_nonblock(fd, true);
if (r < 0)
return r;
r = free_and_strdup(&e->path, path);
if (r < 0)
return r;
sfd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (sfd < 0)
return -errno;
if (fstat(sfd, &e->st) < 0)
return -errno;
r = stat_verify_regular(&e->st);
if (r < 0)
return r;
/* Try to take a reflink snapshot of the file, if we can t make the export atomic */
tfd = reflink_snapshot(sfd, path);
if (tfd >= 0)
e->input_fd = TAKE_FD(tfd);
else
e->input_fd = TAKE_FD(sfd);
r = import_compress_init(&e->compress, compress);
if (r < 0)
return r;
r = sd_event_add_io(e->event, &e->output_event_source, fd, EPOLLOUT, raw_export_on_output, e);
if (r == -EPERM) {
r = sd_event_add_defer(e->event, &e->output_event_source, raw_export_on_defer, e);
if (r < 0)
return r;
r = sd_event_source_set_enabled(e->output_event_source, SD_EVENT_ON);
}
if (r < 0)
return r;
e->output_fd = fd;
return r;
}
示例9: stdout_stream_install
static int stdout_stream_install(Server *s, int fd, StdoutStream **ret) {
_cleanup_(stdout_stream_freep) StdoutStream *stream = NULL;
sd_id128_t id;
int r;
assert(s);
assert(fd >= 0);
r = sd_id128_randomize(&id);
if (r < 0)
return log_error_errno(r, "Failed to generate stream ID: %m");
stream = new0(StdoutStream, 1);
if (!stream)
return log_oom();
stream->fd = -1;
stream->priority = LOG_INFO;
xsprintf(stream->id_field, "_STREAM_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(id));
r = getpeercred(fd, &stream->ucred);
if (r < 0)
return log_error_errno(r, "Failed to determine peer credentials: %m");
if (mac_selinux_use()) {
r = getpeersec(fd, &stream->label);
if (r < 0 && r != -EOPNOTSUPP)
(void) log_warning_errno(r, "Failed to determine peer security context: %m");
}
(void) shutdown(fd, SHUT_WR);
r = sd_event_add_io(s->event, &stream->event_source, fd, EPOLLIN, stdout_stream_process, stream);
if (r < 0)
return log_error_errno(r, "Failed to add stream to event loop: %m");
r = sd_event_source_set_priority(stream->event_source, SD_EVENT_PRIORITY_NORMAL+5);
if (r < 0)
return log_error_errno(r, "Failed to adjust stdout event source priority: %m");
stream->fd = fd;
stream->server = s;
LIST_PREPEND(stdout_stream, s->stdout_streams, stream);
s->n_stdout_streams++;
if (ret)
*ret = stream;
stream = NULL;
return 0;
}
示例10: sd_pppoe_start
int sd_pppoe_start(sd_pppoe *ppp) {
union sockaddr_union link = {
.ll = {
.sll_family = AF_PACKET,
.sll_protocol = htons(ETH_P_PPP_DISC),
},
};
_cleanup_close_ int s = -1;
_cleanup_event_source_unref_ sd_event_source *io = NULL;
int r;
assert_return(ppp, -EINVAL);
assert_return(ppp->fd == -1, -EBUSY);
assert_return(!ppp->io, -EBUSY);
assert_return(ppp->ifindex > 0, -EUNATCH);
assert_return(ppp->ifname, -EUNATCH);
assert_return(ppp->event, -EUNATCH);
assert_return(ppp->cb, -EUNATCH);
s = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
if (s < 0)
return -errno;
link.ll.sll_ifindex = ppp->ifindex;
r = bind(s, &link.sa, sizeof(link.ll));
if (r < 0)
return r;
r = sd_event_add_io(ppp->event, &io,
s, EPOLLIN, pppoe_receive_message,
ppp);
if (r < 0)
return r;
r = sd_event_source_set_priority(io, ppp->event_priority);
if (r < 0)
return r;
ppp->fd = s;
s = -1;
ppp->io = io;
io = NULL;
r = pppoe_send_initiation(ppp);
if (r < 0)
return r;
ppp->state = PPPOE_STATE_INITIALIZING;
return 0;
}
示例11: server_open_native_socket
int server_open_native_socket(Server*s) {
static const union sockaddr_union sa = {
.un.sun_family = AF_UNIX,
.un.sun_path = "/run/systemd/journal/socket",
};
static const int one = 1;
int r;
assert(s);
if (s->native_fd < 0) {
s->native_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
if (s->native_fd < 0)
return log_error_errno(errno, "socket() failed: %m");
(void) unlink(sa.un.sun_path);
r = bind(s->native_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un));
if (r < 0)
return log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path);
(void) chmod(sa.un.sun_path, 0666);
} else
fd_nonblock(s->native_fd, 1);
r = setsockopt(s->native_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
if (r < 0)
return log_error_errno(errno, "SO_PASSCRED failed: %m");
#ifdef HAVE_SELINUX
if (mac_selinux_use()) {
r = setsockopt(s->native_fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one));
if (r < 0)
log_warning_errno(errno, "SO_PASSSEC failed: %m");
}
#endif
r = setsockopt(s->native_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
if (r < 0)
return log_error_errno(errno, "SO_TIMESTAMP failed: %m");
r = sd_event_add_io(s->event, &s->native_event_source, s->native_fd, EPOLLIN, server_process_datagram, s);
if (r < 0)
return log_error_errno(r, "Failed to add native server fd to event loop: %m");
r = sd_event_source_set_priority(s->native_event_source, SD_EVENT_PRIORITY_NORMAL+5);
if (r < 0)
return log_error_errno(r, "Failed to adjust native event source priority: %m");
return 0;
}
示例12: server_open_dev_kmsg
int server_open_dev_kmsg(Server *s) {
mode_t mode;
int r;
assert(s);
if (s->read_kmsg)
mode = O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY;
else
mode = O_WRONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY;
s->dev_kmsg_fd = open("/dev/kmsg", mode);
if (s->dev_kmsg_fd < 0) {
log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
"Failed to open /dev/kmsg, ignoring: %m");
return 0;
}
if (!s->read_kmsg)
return 0;
r = sd_event_add_io(s->event, &s->dev_kmsg_event_source, s->dev_kmsg_fd, EPOLLIN, dispatch_dev_kmsg, s);
if (r < 0) {
/* This will fail with EPERM on older kernels where
* /dev/kmsg is not readable. */
if (r == -EPERM) {
r = 0;
goto fail;
}
log_error_errno(r, "Failed to add /dev/kmsg fd to event loop: %m");
goto fail;
}
r = sd_event_source_set_priority(s->dev_kmsg_event_source, SD_EVENT_PRIORITY_IMPORTANT+10);
if (r < 0) {
log_error_errno(r, "Failed to adjust priority of kmsg event source: %m");
goto fail;
}
s->dev_kmsg_readable = true;
return 0;
fail:
s->dev_kmsg_event_source = sd_event_source_unref(s->dev_kmsg_event_source);
s->dev_kmsg_fd = safe_close(s->dev_kmsg_fd);
return r;
}
示例13: connection_enable_event_sources
static int connection_enable_event_sources(Connection *c) {
uint32_t a = 0, b = 0;
int r;
assert(c);
if (c->server_to_client_buffer_full > 0)
b |= EPOLLOUT;
if (c->server_to_client_buffer_full < c->server_to_client_buffer_size)
a |= EPOLLIN;
if (c->client_to_server_buffer_full > 0)
a |= EPOLLOUT;
if (c->client_to_server_buffer_full < c->client_to_server_buffer_size)
b |= EPOLLIN;
if (c->server_event_source)
r = sd_event_source_set_io_events(c->server_event_source, a);
else if (c->server_fd >= 0)
r = sd_event_add_io(c->context->event, &c->server_event_source, c->server_fd, a, traffic_cb, c);
else
r = 0;
if (r < 0)
return log_error_errno(r, "Failed to set up server event source: %m");
if (c->client_event_source)
r = sd_event_source_set_io_events(c->client_event_source, b);
else if (c->client_fd >= 0)
r = sd_event_add_io(c->context->event, &c->client_event_source, c->client_fd, b, traffic_cb, c);
else
r = 0;
if (r < 0)
return log_error_errno(r, "Failed to set up client event source: %m");
return 0;
}
示例14: sd_icmp6_router_solicitation_start
int sd_icmp6_router_solicitation_start(sd_icmp6_nd *nd) {
int r;
assert(nd);
assert(nd->event);
if (nd->state != ICMP6_NEIGHBOR_DISCOVERY_IDLE)
return -EINVAL;
if (nd->index < 1)
return -EINVAL;
r = dhcp_network_icmp6_bind_router_solicitation(nd->index);
if (r < 0)
return r;
nd->fd = r;
r = sd_event_add_io(nd->event, &nd->recv, nd->fd, EPOLLIN,
icmp6_router_advertisment_recv, nd);
if (r < 0)
goto error;
r = sd_event_source_set_priority(nd->recv, nd->event_priority);
if (r < 0)
goto error;
r = sd_event_source_set_description(nd->recv, "icmp6-receive-message");
if (r < 0)
goto error;
r = sd_event_add_time(nd->event, &nd->timeout, clock_boottime_or_monotonic(),
0, 0, icmp6_router_solicitation_timeout, nd);
if (r < 0)
goto error;
r = sd_event_source_set_priority(nd->timeout, nd->event_priority);
if (r < 0)
goto error;
r = sd_event_source_set_description(nd->timeout, "icmp6-timeout");
error:
if (r < 0)
icmp6_nd_init(nd);
else
log_icmp6_nd(client, "Start Router Solicitation");
return r;
}
示例15: server_open_dev_kmsg
int server_open_dev_kmsg(Server *s) {
int r;
assert(s);
s->dev_kmsg_fd = open("/dev/kmsg", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (s->dev_kmsg_fd < 0) {
log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
"Failed to open /dev/kmsg, ignoring: %m");
return 0;
}
r = sd_event_add_io(s->event, &s->dev_kmsg_event_source, s->dev_kmsg_fd, EPOLLIN, dispatch_dev_kmsg, s);
if (r < 0) {
/* This will fail with EPERM on older kernels where
* /dev/kmsg is not readable. */
if (r == -EPERM) {
r = 0;
goto fail;
}
log_error("Failed to add /dev/kmsg fd to event loop: %s", strerror(-r));
goto fail;
}
r = sd_event_source_set_priority(s->dev_kmsg_event_source, SD_EVENT_PRIORITY_IMPORTANT+10);
if (r < 0) {
log_error("Failed to adjust priority of kmsg event source: %s", strerror(-r));
goto fail;
}
s->dev_kmsg_readable = true;
return 0;
fail:
if (s->dev_kmsg_event_source)
s->dev_kmsg_event_source = sd_event_source_unref(s->dev_kmsg_event_source);
if (s->dev_kmsg_fd >= 0) {
close_nointr_nofail(s->dev_kmsg_fd);
s->dev_kmsg_fd = -1;
}
return r;
}