int manager_rtnl_listen(Manager *m) { int r; r = sd_rtnl_attach_event(m->rtnl, m->event, 0); if (r < 0) return r; r = sd_rtnl_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m); if (r < 0) return r; return 0; }
static void test_event_loop(int ifindex) { _cleanup_event_unref_ sd_event *event = NULL; _cleanup_sd_rtnl_unref_ sd_rtnl *rtnl = NULL; _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *m = NULL; char *ifname; ifname = strdup("lo2"); assert(ifname); assert(sd_rtnl_open(0, &rtnl) >= 0); assert(sd_rtnl_message_link_new(RTM_GETLINK, ifindex, 0, 0, &m) >= 0); assert(sd_rtnl_call_async(rtnl, m, &link_handler, ifname, 0, NULL) >= 0); assert(sd_event_default(&event) >= 0); assert(sd_rtnl_attach_event(rtnl, event, 0) >= 0); assert(sd_event_run(event, 0) >= 0); assert(sd_rtnl_detach_event(rtnl) >= 0); }
static int test_pppoe_server(sd_event *e) { sd_rtnl *rtnl; sd_rtnl_message *m; pid_t pid; int r, client_ifindex, server_ifindex; r = unshare(CLONE_NEWNET); if (r < 0 && errno == EPERM) return EXIT_TEST_SKIP; assert_se(r >= 0); assert_se(sd_rtnl_open(&rtnl, 0) >= 0); assert_se(sd_rtnl_attach_event(rtnl, e, 0) >= 0); assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0) >= 0); assert_se(sd_rtnl_message_append_string(m, IFLA_IFNAME, "pppoe-server") >= 0); assert_se(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0); assert_se(sd_rtnl_message_open_container_union(m, IFLA_INFO_DATA, "veth") >= 0); assert_se(sd_rtnl_message_open_container(m, VETH_INFO_PEER) >= 0); assert_se(sd_rtnl_message_append_string(m, IFLA_IFNAME, "pppoe-client") >= 0); assert_se(sd_rtnl_message_close_container(m) >= 0); assert_se(sd_rtnl_message_close_container(m) >= 0); assert_se(sd_rtnl_message_close_container(m) >= 0); assert_se(sd_rtnl_call(rtnl, m, 0, NULL) >= 0); client_ifindex = (int) if_nametoindex("pppoe-client"); assert_se(client_ifindex > 0); server_ifindex = (int) if_nametoindex("pppoe-server"); assert_se(server_ifindex > 0); m = sd_rtnl_message_unref(m); assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_SETLINK, client_ifindex) >= 0); assert_se(sd_rtnl_message_link_set_flags(m, IFF_UP, IFF_UP) >= 0); assert_se(sd_rtnl_call(rtnl, m, 0, NULL) >= 0); m = sd_rtnl_message_unref(m); assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_SETLINK, server_ifindex) >= 0); assert_se(sd_rtnl_message_link_set_flags(m, IFF_UP, IFF_UP) >= 0); assert_se(sd_rtnl_call(rtnl, m, 0, NULL) >= 0); pid = fork(); assert_se(pid >= 0); if (pid == 0) { /* let the client send some discover messages before the server is started */ sleep(2); /* TODO: manage pppoe-server-options */ execlp("pppoe-server", "pppoe-server", "-F", "-I", "pppoe-server", "-C", "Test-AC", "-S", "Service-Default", "-S", "Service-First-Auxiliary", "-S", "Service-Second-Auxiliary", NULL); assert_not_reached("failed to execute pppoe-server. not installed?"); } client_run("pppoe-client", e); assert_se(kill(pid, SIGTERM) >= 0); assert_se(wait_for_terminate(pid, NULL) >= 0); assert_se(!sd_rtnl_message_unref(m)); assert_se(!sd_rtnl_unref(rtnl)); return EXIT_SUCCESS; }