int sd_pppoe_stop(sd_pppoe *ppp) { assert_return(ppp, -EINVAL); if (ppp->state == PPPOE_STATE_RUNNING) pppoe_send_terminate(ppp); ppp->io = sd_event_source_unref(ppp->io); ppp->timeout = sd_event_source_unref(ppp->timeout); ppp->fd = asynchronous_close(ppp->fd); ppp->pppoe_fd = asynchronous_close(ppp->pppoe_fd); return 0; }
int lldp_port_stop(lldp_port *p) { assert_return(p, -EINVAL); p->rawfd = asynchronous_close(p->rawfd); p->lldp_port_rx = sd_event_source_unref(p->lldp_port_rx); return 0; }
static int icmp6_nd_init(sd_icmp6_nd *nd) { assert(nd); nd->recv = sd_event_source_unref(nd->recv); nd->fd = asynchronous_close(nd->fd); nd->timeout = sd_event_source_unref(nd->timeout); return 0; }
static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata) { sd_dhcp_client *client = userdata; int r; client->receive_message = sd_event_source_unref(client->receive_message); client->fd = asynchronous_close(client->fd); client->state = DHCP_STATE_REBINDING; client->attempt = 1; r = dhcp_network_bind_raw_socket(client->index, &client->link, client->xid); if (r < 0) { client_stop(client, r); return 0; } client->fd = r; return client_initialize_events(client, client_receive_message_raw); }
int main(int argc, char *argv[]) { int fd; char name[] = "/tmp/test-asynchronous_close.XXXXXX"; fd = mkostemp_safe(name); assert_se(fd >= 0); asynchronous_close(fd); assert_se(asynchronous_job(async_func, NULL) >= 0); assert_se(asynchronous_sync(NULL) >= 0); sleep(1); assert_se(fcntl(fd, F_GETFD) == -1); assert_se(test_async); unlink(name); return 0; }
static int client_initialize(sd_dhcp_client *client) { assert_return(client, -EINVAL); client->receive_message = sd_event_source_unref(client->receive_message); client->fd = asynchronous_close(client->fd); client->timeout_resend = sd_event_source_unref(client->timeout_resend); client->timeout_t1 = sd_event_source_unref(client->timeout_t1); client->timeout_t2 = sd_event_source_unref(client->timeout_t2); client->timeout_expire = sd_event_source_unref(client->timeout_expire); client->attempt = 1; client->state = DHCP_STATE_INIT; client->xid = 0; if (client->lease) client->lease = sd_dhcp_lease_unref(client->lease); return 0; }
static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message, int len) { int r = 0, notify_event = 0; assert(client); assert(client->event); assert(message); if (be32toh(message->magic) != DHCP_MAGIC_COOKIE) { log_dhcp_client(client, "not a DHCP message: ignoring"); return 0; } if (message->op != BOOTREPLY) { log_dhcp_client(client, "not a BOOTREPLY message: ignoring"); return 0; } if (be32toh(message->xid) != client->xid) { log_dhcp_client(client, "received xid (%u) does not match " "expected (%u): ignoring", be32toh(message->xid), client->xid); return 0; } if (message->htype != ARPHRD_ETHER || message->hlen != ETHER_ADDR_LEN) { log_dhcp_client(client, "not an ethernet packet"); return 0; } if (memcmp(&message->chaddr[0], &client->client_id.mac_addr, ETH_ALEN)) { log_dhcp_client(client, "received chaddr does not match " "expected: ignoring"); return 0; } switch (client->state) { case DHCP_STATE_SELECTING: r = client_handle_offer(client, message, len); if (r >= 0) { client->timeout_resend = sd_event_source_unref(client->timeout_resend); client->state = DHCP_STATE_REQUESTING; client->attempt = 1; r = sd_event_add_time(client->event, &client->timeout_resend, CLOCK_MONOTONIC, 0, 0, client_timeout_resend, client); if (r < 0) goto error; r = sd_event_source_set_priority(client->timeout_resend, client->event_priority); if (r < 0) goto error; } else if (r == -ENOMSG) /* invalid message, let's ignore it */ return 0; break; case DHCP_STATE_REBOOTING: case DHCP_STATE_REQUESTING: case DHCP_STATE_RENEWING: case DHCP_STATE_REBINDING: r = client_handle_ack(client, message, len); if (r == DHCP_EVENT_NO_LEASE) { client->timeout_resend = sd_event_source_unref(client->timeout_resend); if (client->state == DHCP_STATE_REBOOTING) { r = client_initialize(client); if (r < 0) goto error; r = client_start(client); if (r < 0) goto error; log_dhcp_client(client, "REBOOTED"); } goto error; } else if (r >= 0) { client->timeout_resend = sd_event_source_unref(client->timeout_resend); if (IN_SET(client->state, DHCP_STATE_REQUESTING, DHCP_STATE_REBOOTING)) notify_event = DHCP_EVENT_IP_ACQUIRE; else if (r != DHCP_EVENT_IP_ACQUIRE) notify_event = r; client->state = DHCP_STATE_BOUND; client->attempt = 1; client->last_addr = client->lease->address; r = client_set_lease_timeouts(client); if (r < 0) goto error; if (notify_event) { client = client_notify(client, notify_event); if (!client || client->state == DHCP_STATE_STOPPED) return 0; } client->receive_message = sd_event_source_unref(client->receive_message); client->fd = asynchronous_close(client->fd); } else if (r == -ENOMSG) /* invalid message, let's ignore it */ return 0; break; case DHCP_STATE_INIT: case DHCP_STATE_INIT_REBOOT: case DHCP_STATE_BOUND: break; case DHCP_STATE_STOPPED: r = -EINVAL; goto error; } error: if (r < 0 || r == DHCP_EVENT_NO_LEASE) client_stop(client, r); return r; }