Lines Matching refs:nl
23 _cleanup_(sd_netlink_unrefp) sd_netlink *nl = NULL; in netlink_new()
27 nl = new(sd_netlink, 1); in netlink_new()
28 if (!nl) in netlink_new()
31 *nl = (sd_netlink) { in netlink_new()
34 .sockaddr.nl.nl_family = AF_NETLINK, in netlink_new()
64 if (!greedy_realloc((void**) &nl->rbuffer, sizeof(struct nlmsghdr), sizeof(uint8_t))) in netlink_new()
67 *ret = TAKE_PTR(nl); in netlink_new()
72 _cleanup_(sd_netlink_unrefp) sd_netlink *nl = NULL; in sd_netlink_new_from_fd()
78 r = netlink_new(&nl); in sd_netlink_new_from_fd()
82 addrlen = sizeof(nl->sockaddr); in sd_netlink_new_from_fd()
84 if (getsockname(fd, &nl->sockaddr.sa, &addrlen) < 0) in sd_netlink_new_from_fd()
87 if (nl->sockaddr.nl.nl_family != AF_NETLINK) in sd_netlink_new_from_fd()
90 nl->fd = fd; in sd_netlink_new_from_fd()
92 *ret = TAKE_PTR(nl); in sd_netlink_new_from_fd()
97 _cleanup_(sd_netlink_unrefp) sd_netlink *nl = NULL; in sd_netlink_open_fd()
103 r = netlink_new(&nl); in sd_netlink_open_fd()
111 nl->fd = fd; in sd_netlink_open_fd()
112 nl->protocol = protocol; in sd_netlink_open_fd()
122 r = socket_bind(nl); in sd_netlink_open_fd()
124 … nl->fd = -1; /* on failure, the caller remains owner of the fd, hence don't close it here */ in sd_netlink_open_fd()
125 nl->protocol = -1; in sd_netlink_open_fd()
129 *ret = TAKE_PTR(nl); in sd_netlink_open_fd()
154 bool netlink_pid_changed(sd_netlink *nl) { in netlink_pid_changed() argument
155 assert(nl); in netlink_pid_changed()
160 return nl->original_pid != getpid_cached(); in netlink_pid_changed()
163 int sd_netlink_inc_rcvbuf(sd_netlink *nl, size_t size) { in sd_netlink_inc_rcvbuf() argument
164 assert_return(nl, -EINVAL); in sd_netlink_inc_rcvbuf()
165 assert_return(!netlink_pid_changed(nl), -ECHILD); in sd_netlink_inc_rcvbuf()
167 return fd_inc_rcvbuf(nl->fd, size); in sd_netlink_inc_rcvbuf()
170 static sd_netlink *netlink_free(sd_netlink *nl) { in netlink_free() argument
174 assert(nl); in netlink_free()
176 for (i = 0; i < nl->rqueue_size; i++) in netlink_free()
177 sd_netlink_message_unref(nl->rqueue[i]); in netlink_free()
178 free(nl->rqueue); in netlink_free()
180 for (i = 0; i < nl->rqueue_partial_size; i++) in netlink_free()
181 sd_netlink_message_unref(nl->rqueue_partial[i]); in netlink_free()
182 free(nl->rqueue_partial); in netlink_free()
184 free(nl->rbuffer); in netlink_free()
186 while ((s = nl->slots)) { in netlink_free()
190 hashmap_free(nl->reply_callbacks); in netlink_free()
191 prioq_free(nl->reply_callbacks_prioq); in netlink_free()
193 sd_event_source_unref(nl->io_event_source); in netlink_free()
194 sd_event_source_unref(nl->time_event_source); in netlink_free()
195 sd_event_unref(nl->event); in netlink_free()
197 hashmap_free(nl->broadcast_group_refs); in netlink_free()
199 genl_clear_family(nl); in netlink_free()
201 safe_close(nl->fd); in netlink_free()
202 return mfree(nl); in netlink_free()
207 static void netlink_seal_message(sd_netlink *nl, sd_netlink_message *m) { in netlink_seal_message() argument
210 assert(nl); in netlink_seal_message()
211 assert(!netlink_pid_changed(nl)); in netlink_seal_message()
217 picked = nl->serial; in netlink_seal_message()
221 nl->serial = nl->serial == UINT32_MAX ? 1 : nl->serial + 1; in netlink_seal_message()
223 } while (hashmap_contains(nl->reply_callbacks, UINT32_TO_PTR(picked))); in netlink_seal_message()
230 sd_netlink *nl, in sd_netlink_send() argument
236 assert_return(nl, -EINVAL); in sd_netlink_send()
237 assert_return(!netlink_pid_changed(nl), -ECHILD); in sd_netlink_send()
241 netlink_seal_message(nl, message); in sd_netlink_send()
243 r = socket_write_message(nl, message); in sd_netlink_send()
254 sd_netlink *nl, in sd_netlink_sendv() argument
262 assert_return(nl, -EINVAL); in sd_netlink_sendv()
263 assert_return(!netlink_pid_changed(nl), -ECHILD); in sd_netlink_sendv()
276 netlink_seal_message(nl, messages[i]); in sd_netlink_sendv()
281 r = socket_writev_message(nl, messages, msgcount); in sd_netlink_sendv()
291 int netlink_rqueue_make_room(sd_netlink *nl) { in netlink_rqueue_make_room() argument
292 assert(nl); in netlink_rqueue_make_room()
294 if (nl->rqueue_size >= NETLINK_RQUEUE_MAX) in netlink_rqueue_make_room()
299 if (!GREEDY_REALLOC(nl->rqueue, nl->rqueue_size + 1)) in netlink_rqueue_make_room()
305 int netlink_rqueue_partial_make_room(sd_netlink *nl) { in netlink_rqueue_partial_make_room() argument
306 assert(nl); in netlink_rqueue_partial_make_room()
308 if (nl->rqueue_partial_size >= NETLINK_RQUEUE_MAX) in netlink_rqueue_partial_make_room()
313 if (!GREEDY_REALLOC(nl->rqueue_partial, nl->rqueue_partial_size + 1)) in netlink_rqueue_partial_make_room()
319 static int dispatch_rqueue(sd_netlink *nl, sd_netlink_message **message) { in dispatch_rqueue() argument
322 assert(nl); in dispatch_rqueue()
325 if (nl->rqueue_size <= 0) { in dispatch_rqueue()
327 r = socket_read_message(nl); in dispatch_rqueue()
337 *message = nl->rqueue[0]; in dispatch_rqueue()
338 nl->rqueue_size--; in dispatch_rqueue()
339 memmove(nl->rqueue, nl->rqueue + 1, sizeof(sd_netlink_message*) * nl->rqueue_size); in dispatch_rqueue()
344 static int process_timeout(sd_netlink *nl) { in process_timeout() argument
351 assert(nl); in process_timeout()
353 c = prioq_peek(nl->reply_callbacks_prioq); in process_timeout()
361 r = message_new_synthetic_error(nl, -ETIMEDOUT, c->serial, &m); in process_timeout()
365 assert_se(prioq_pop(nl->reply_callbacks_prioq) == c); in process_timeout()
367 hashmap_remove(nl->reply_callbacks, UINT32_TO_PTR(c->serial)); in process_timeout()
371 r = c->callback(nl, m, slot->userdata); in process_timeout()
384 static int process_reply(sd_netlink *nl, sd_netlink_message *m) { in process_reply() argument
391 assert(nl); in process_reply()
395 c = hashmap_remove(nl->reply_callbacks, UINT32_TO_PTR(serial)); in process_reply()
400 prioq_remove(nl->reply_callbacks_prioq, c, &c->prioq_idx); in process_reply()
413 r = c->callback(nl, m, slot->userdata); in process_reply()
426 static int process_match(sd_netlink *nl, sd_netlink_message *m) { in process_match() argument
431 assert(nl); in process_match()
439 r = sd_genl_message_get_command(nl, m, &cmd); in process_match()
445 LIST_FOREACH(match_callbacks, c, nl->match_callbacks) { in process_match()
465 r = c->callback(nl, m, slot->userdata); in process_match()
478 static int process_running(sd_netlink *nl, sd_netlink_message **ret) { in process_running() argument
482 assert(nl); in process_running()
484 r = process_timeout(nl); in process_running()
488 r = dispatch_rqueue(nl, &m); in process_running()
495 r = process_match(nl, m); in process_running()
497 r = process_reply(nl, m); in process_running()
516 int sd_netlink_process(sd_netlink *nl, sd_netlink_message **ret) { in sd_netlink_process() argument
517 NETLINK_DONT_DESTROY(nl); in sd_netlink_process()
520 assert_return(nl, -EINVAL); in sd_netlink_process()
521 assert_return(!netlink_pid_changed(nl), -ECHILD); in sd_netlink_process()
522 assert_return(!nl->processing, -EBUSY); in sd_netlink_process()
524 nl->processing = true; in sd_netlink_process()
525 r = process_running(nl, ret); in sd_netlink_process()
526 nl->processing = false; in sd_netlink_process()
541 static int netlink_poll(sd_netlink *nl, bool need_more, usec_t timeout_usec) { in netlink_poll() argument
545 assert(nl); in netlink_poll()
547 e = sd_netlink_get_events(nl); in netlink_poll()
561 r = sd_netlink_get_timeout(nl, &until); in netlink_poll()
568 r = fd_wait_for_event(nl->fd, e, MIN(m, timeout_usec)); in netlink_poll()
575 int sd_netlink_wait(sd_netlink *nl, uint64_t timeout_usec) { in sd_netlink_wait() argument
576 assert_return(nl, -EINVAL); in sd_netlink_wait()
577 assert_return(!netlink_pid_changed(nl), -ECHILD); in sd_netlink_wait()
579 if (nl->rqueue_size > 0) in sd_netlink_wait()
582 return netlink_poll(nl, false, timeout_usec); in sd_netlink_wait()
598 sd_netlink *nl, in sd_netlink_call_async() argument
610 assert_return(nl, -EINVAL); in sd_netlink_call_async()
613 assert_return(!netlink_pid_changed(nl), -ECHILD); in sd_netlink_call_async()
615 if (hashmap_size(nl->reply_callbacks) >= REPLY_CALLBACKS_MAX) in sd_netlink_call_async()
618 r = hashmap_ensure_allocated(&nl->reply_callbacks, &trivial_hash_ops); in sd_netlink_call_async()
623 r = prioq_ensure_allocated(&nl->reply_callbacks_prioq, timeout_compare); in sd_netlink_call_async()
628 …r = netlink_slot_allocate(nl, !ret_slot, NETLINK_REPLY_CALLBACK, sizeof(struct reply_callback), us… in sd_netlink_call_async()
635 k = sd_netlink_send(nl, m, &slot->reply_callback.serial); in sd_netlink_call_async()
639 …r = hashmap_put(nl->reply_callbacks, UINT32_TO_PTR(slot->reply_callback.serial), &slot->reply_call… in sd_netlink_call_async()
644 … r = prioq_put(nl->reply_callbacks_prioq, &slot->reply_callback, &slot->reply_callback.prioq_idx); in sd_netlink_call_async()
646 … (void) hashmap_remove(nl->reply_callbacks, UINT32_TO_PTR(slot->reply_callback.serial)); in sd_netlink_call_async()
663 sd_netlink *nl, in sd_netlink_read() argument
671 assert_return(nl, -EINVAL); in sd_netlink_read()
672 assert_return(!netlink_pid_changed(nl), -ECHILD); in sd_netlink_read()
679 for (unsigned i = 0; i < nl->rqueue_size; i++) { in sd_netlink_read()
684 received_serial = message_get_serial(nl->rqueue[i]); in sd_netlink_read()
688 incoming = nl->rqueue[i]; in sd_netlink_read()
691 memmove(nl->rqueue + i, nl->rqueue + i + 1, in sd_netlink_read()
692 sizeof(sd_netlink_message*) * (nl->rqueue_size - i - 1)); in sd_netlink_read()
693 nl->rqueue_size--; in sd_netlink_read()
713 r = socket_read_message(nl); in sd_netlink_read()
731 r = netlink_poll(nl, true, left); in sd_netlink_read()
740 sd_netlink *nl, in sd_netlink_call() argument
748 assert_return(nl, -EINVAL); in sd_netlink_call()
749 assert_return(!netlink_pid_changed(nl), -ECHILD); in sd_netlink_call()
752 r = sd_netlink_send(nl, message, &serial); in sd_netlink_call()
756 return sd_netlink_read(nl, serial, usec, ret); in sd_netlink_call()
759 int sd_netlink_get_events(sd_netlink *nl) { in sd_netlink_get_events() argument
760 assert_return(nl, -EINVAL); in sd_netlink_get_events()
761 assert_return(!netlink_pid_changed(nl), -ECHILD); in sd_netlink_get_events()
763 return nl->rqueue_size == 0 ? POLLIN : 0; in sd_netlink_get_events()
766 int sd_netlink_get_timeout(sd_netlink *nl, uint64_t *timeout_usec) { in sd_netlink_get_timeout() argument
769 assert_return(nl, -EINVAL); in sd_netlink_get_timeout()
771 assert_return(!netlink_pid_changed(nl), -ECHILD); in sd_netlink_get_timeout()
773 if (nl->rqueue_size > 0) { in sd_netlink_get_timeout()
778 c = prioq_peek(nl->reply_callbacks_prioq); in sd_netlink_get_timeout()
790 sd_netlink *nl = userdata; in io_callback() local
793 assert(nl); in io_callback()
795 r = sd_netlink_process(nl, NULL); in io_callback()
803 sd_netlink *nl = userdata; in time_callback() local
806 assert(nl); in time_callback()
808 r = sd_netlink_process(nl, NULL); in time_callback()
816 sd_netlink *nl = userdata; in prepare_callback() local
821 assert(nl); in prepare_callback()
823 e = sd_netlink_get_events(nl); in prepare_callback()
827 r = sd_event_source_set_io_events(nl->io_event_source, e); in prepare_callback()
831 r = sd_netlink_get_timeout(nl, &until); in prepare_callback()
837 j = sd_event_source_set_time(nl->time_event_source, until); in prepare_callback()
842 r = sd_event_source_set_enabled(nl->time_event_source, r > 0); in prepare_callback()
849 int sd_netlink_attach_event(sd_netlink *nl, sd_event *event, int64_t priority) { in sd_netlink_attach_event() argument
852 assert_return(nl, -EINVAL); in sd_netlink_attach_event()
853 assert_return(!nl->event, -EBUSY); in sd_netlink_attach_event()
855 assert(!nl->io_event_source); in sd_netlink_attach_event()
856 assert(!nl->time_event_source); in sd_netlink_attach_event()
859 nl->event = sd_event_ref(event); in sd_netlink_attach_event()
861 r = sd_event_default(&nl->event); in sd_netlink_attach_event()
866 r = sd_event_add_io(nl->event, &nl->io_event_source, nl->fd, 0, io_callback, nl); in sd_netlink_attach_event()
870 r = sd_event_source_set_priority(nl->io_event_source, priority); in sd_netlink_attach_event()
874 r = sd_event_source_set_description(nl->io_event_source, "netlink-receive-message"); in sd_netlink_attach_event()
878 r = sd_event_source_set_prepare(nl->io_event_source, prepare_callback); in sd_netlink_attach_event()
882 …r = sd_event_add_time(nl->event, &nl->time_event_source, CLOCK_MONOTONIC, 0, 0, time_callback, nl); in sd_netlink_attach_event()
886 r = sd_event_source_set_priority(nl->time_event_source, priority); in sd_netlink_attach_event()
890 r = sd_event_source_set_description(nl->time_event_source, "netlink-timer"); in sd_netlink_attach_event()
897 sd_netlink_detach_event(nl); in sd_netlink_attach_event()
901 int sd_netlink_detach_event(sd_netlink *nl) { in sd_netlink_detach_event() argument
902 assert_return(nl, -EINVAL); in sd_netlink_detach_event()
903 assert_return(nl->event, -ENXIO); in sd_netlink_detach_event()
905 nl->io_event_source = sd_event_source_unref(nl->io_event_source); in sd_netlink_detach_event()
907 nl->time_event_source = sd_event_source_unref(nl->time_event_source); in sd_netlink_detach_event()
909 nl->event = sd_event_unref(nl->event); in sd_netlink_detach_event()
915 sd_netlink *nl, in netlink_add_match_internal() argument
933 r = socket_broadcast_group_ref(nl, groups[i]); in netlink_add_match_internal()
938 … r = netlink_slot_allocate(nl, !ret_slot, NETLINK_MATCH_CALLBACK, sizeof(struct match_callback), in netlink_add_match_internal()
952 LIST_PREPEND(match_callbacks, nl->match_callbacks, &slot->match_callback); in netlink_add_match_internal()
1034 int sd_netlink_attach_filter(sd_netlink *nl, size_t len, struct sock_filter *filter) { in sd_netlink_attach_filter() argument
1035 assert_return(nl, -EINVAL); in sd_netlink_attach_filter()
1038 if (setsockopt(nl->fd, SOL_SOCKET, in sd_netlink_attach_filter()