Пример #1
0
static void test_basic_request(sd_event *e) {

        sd_ipv4ll *ll;
        struct ether_arp arp;
        struct ether_addr mac_addr = {
                .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}};

        if (verbose)
                printf("* %s\n", __FUNCTION__);

        assert_se(sd_ipv4ll_new(&ll) == 0);
        assert_se(sd_ipv4ll_start(ll) == -EINVAL);

        assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0);
        assert_se(sd_ipv4ll_start(ll) == -EINVAL);

        assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0);
        assert_se(sd_ipv4ll_start(ll) == -EINVAL);

        assert_se(sd_ipv4ll_set_callback(ll, basic_request_handler,
                                         basic_request_handler_userdata) == 0);
        assert_se(sd_ipv4ll_start(ll) == -EINVAL);

        assert_se(sd_ipv4ll_set_index(ll, 1) == 0);
        assert_se(sd_ipv4ll_start(ll) == 0);

        sd_event_run(e, (uint64_t) -1);
        assert_se(sd_ipv4ll_start(ll) == -EBUSY);

        assert_se(sd_ipv4ll_is_running(ll));

        /* PROBE */
        sd_event_run(e, (uint64_t) -1);
        assert_se(read(test_fd[1], &arp, sizeof(struct ether_arp)) == sizeof(struct ether_arp));

        if (extended) {
                /* PROBE */
                sd_event_run(e, (uint64_t) -1);
                assert_se(read(test_fd[1], &arp, sizeof(struct ether_arp)) == sizeof(struct ether_arp));

                /* PROBE */
                sd_event_run(e, (uint64_t) -1);
                assert_se(read(test_fd[1], &arp, sizeof(struct ether_arp)) == sizeof(struct ether_arp));

                sd_event_run(e, (uint64_t) -1);
                assert_se(basic_request_handler_bind == 1);
        }

        sd_ipv4ll_stop(ll);
        assert_se(basic_request_handler_stop == 1);

        /* Cleanup */
        assert_se(sd_ipv4ll_unref(ll) == NULL);
        safe_close(test_fd[1]);
}
Пример #2
0
static void test_sd_event_now(void) {
        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
        uint64_t event_now;

        assert_se(sd_event_new(&e) >= 0);
        assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) > 0);
        assert_se(sd_event_now(e, CLOCK_REALTIME, &event_now) > 0);
        assert_se(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now) > 0);
        if (clock_boottime_supported()) {
                assert_se(sd_event_now(e, CLOCK_BOOTTIME, &event_now) > 0);
                assert_se(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now) > 0);
        }
        assert_se(sd_event_now(e, -1, &event_now) == -EOPNOTSUPP);
        assert_se(sd_event_now(e, 900 /* arbitrary big number */, &event_now) == -EOPNOTSUPP);

        assert_se(sd_event_run(e, 0) == 0);

        assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) == 0);
        assert_se(sd_event_now(e, CLOCK_REALTIME, &event_now) == 0);
        assert_se(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now) == 0);
        if (clock_boottime_supported()) {
                assert_se(sd_event_now(e, CLOCK_BOOTTIME, &event_now) == 0);
                assert_se(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now) == 0);
        }
        assert_se(sd_event_now(e, -1, &event_now) == -EOPNOTSUPP);
        assert_se(sd_event_now(e, 900 /* arbitrary big number */, &event_now) == -EOPNOTSUPP);
}
Пример #3
0
static void check(Manager *m, Unit *unit, int status_expected, int code_expected) {
        Service *service = NULL;
        usec_t ts;
        usec_t timeout = 2 * USEC_PER_SEC;

        assert_se(m);
        assert_se(unit);

        service = SERVICE(unit);
        printf("%s\n", unit->id);
        exec_context_dump(&service->exec_context, stdout, "\t");
        ts = now(CLOCK_MONOTONIC);
        while (service->state != SERVICE_DEAD && service->state != SERVICE_FAILED) {
                int r;
                usec_t n;

                r = sd_event_run(m->event, 100 * USEC_PER_MSEC);
                assert_se(r >= 0);

                n = now(CLOCK_MONOTONIC);
                if (ts + timeout < n) {
                        log_error("Test timeout when testing %s", unit->id);
                        exit(EXIT_FAILURE);
                }
        }
        exec_status_dump(&service->main_exec_status, stdout, "\t");
        assert_se(service->main_exec_status.status == status_expected);
        assert_se(service->main_exec_status.code == code_expected);
}
Пример #4
0
static void test_receive_incomplete_packet(sd_event *e) {
    sd_lldp *lldp;
    sd_lldp_packet **packets;
    uint8_t frame[] = {
        /* Ethernet header */
        0x01, 0x80, 0xc2, 0x00, 0x00, 0x03,     /* Destination MAC*/
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06,     /* Source MAC */
        0x88, 0xcc,                             /* Ethertype */
        /* LLDP mandatory TLVs */
        0x02, 0x07, 0x04, 0x00, 0x01, 0x02,     /* Chassis: MAC, 00:01:02:03:04:05 */
        0x03, 0x04, 0x05,
        0x04, 0x04, 0x05, 0x31, 0x2f, 0x33,     /* Port: interface name, "1/3" */
        /* Missing TTL */
        0x00, 0x00                              /* End Of LLDPDU */
    };

    lldp_handler_calls = 0;
    assert_se(start_lldp(&lldp, e, lldp_handler, NULL) == 0);

    assert_se(write(test_fd[1], frame, sizeof(frame)) == sizeof(frame));
    sd_event_run(e, 0);
    assert_se(lldp_handler_calls == 0);
    assert_se(sd_lldp_get_packets(lldp, &packets) == 0);

    assert_se(stop_lldp(lldp) == 0);
}
Пример #5
0
int bus_event_loop_with_idle(sd_event *e, sd_bus *bus, const char *name, usec_t timeout) {
        bool exiting = false;
        int r;

        assert(e);
        assert(bus);
        assert(name);

        for (;;) {
                r = sd_event_get_state(e);
                if (r < 0)
                        return r;

                if (r == SD_EVENT_FINISHED)
                        break;

                r = sd_event_run(e, exiting ? (uint64_t) -1 : timeout);
                if (r < 0)
                        return r;

                if (r == 0 && !exiting) {
                        r = bus_async_unregister_and_quit(e, bus, name);
                        if (r < 0)
                                return r;

                        exiting = true;
                }
        }

        return 0;
}
Пример #6
0
static void test_discover_message(sd_event *e) {
        sd_dhcp_client *client;
        int res, r;

        if (verbose)
                printf("* %s\n", __FUNCTION__);

        r = sd_dhcp_client_new(&client, false);
        assert_se(r >= 0);
        assert_se(client);

        r = sd_dhcp_client_attach_event(client, e, 0);
        assert_se(r >= 0);

        assert_se(sd_dhcp_client_set_ifindex(client, 42) >= 0);
        assert_se(sd_dhcp_client_set_mac(client, mac_addr, ETH_ALEN, ARPHRD_ETHER) >= 0);

        assert_se(sd_dhcp_client_set_request_option(client, 248) >= 0);

        callback_recv = test_discover_message_verify;

        res = sd_dhcp_client_start(client);

        assert_se(IN_SET(res, 0, -EINPROGRESS));

        sd_event_run(e, (uint64_t) -1);

        sd_dhcp_client_stop(client);
        sd_dhcp_client_unref(client);

        test_fd[1] = safe_close(test_fd[1]);

        callback_recv = NULL;
}
Пример #7
0
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
        _cleanup_(sd_lldp_unrefp) sd_lldp *lldp = NULL;

        assert_se(sd_event_new(&e) == 0);
        assert_se(sd_lldp_new(&lldp) >= 0);
        assert_se(sd_lldp_set_ifindex(lldp, 42) >= 0);
        assert_se(sd_lldp_attach_event(lldp, e, 0) >= 0);
        assert_se(sd_lldp_start(lldp) >= 0);

        assert_se(write(test_fd[1], data, size) == (ssize_t) size);
        assert_se(sd_event_run(e, 0) >= 0);

        assert_se(sd_lldp_stop(lldp) >= 0);
        assert_se(sd_lldp_detach_event(lldp) >= 0);
        test_fd[1] = safe_close(test_fd[1]);

        return 0;
}
Пример #8
0
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
        struct ether_addr mac_addr = {
                .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}
        };
        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
        _cleanup_(sd_ndisc_unrefp) sd_ndisc *nd = NULL;

        assert_se(sd_event_new(&e) >= 0);
        assert_se(sd_ndisc_new(&nd) >= 0);
        assert_se(sd_ndisc_attach_event(nd, e, 0) >= 0);
        assert_se(sd_ndisc_set_ifindex(nd, 42) >= 0);
        assert_se(sd_ndisc_set_mac(nd, &mac_addr) >= 0);
        assert_se(sd_ndisc_start(nd) >= 0);
        assert_se(write(test_fd[1], data, size) == (ssize_t) size);
        (void) sd_event_run(e, (uint64_t) -1);
        assert_se(sd_ndisc_stop(nd) >= 0);
        close(test_fd[1]);

        return 0;
}
Пример #9
0
int manager_run(Manager *m) {
    int r;

    assert(m);

    for (;;) {
        r = sd_event_get_state(m->event);
        if (r < 0)
            return r;
        if (r == SD_EVENT_FINISHED)
            return 0;

        manager_gc(m, true);

        r = sd_event_run(m->event, (uint64_t) -1);
        if (r < 0)
            return r;
    }

    return 0;
}
Пример #10
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);
}
Пример #11
0
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
        Server s;
        StdoutStream *stream;
        int v;

        if (size == 0 || size > 65536)
                return 0;

        if (!getenv("SYSTEMD_LOG_LEVEL"))
                log_set_max_level(LOG_CRIT);

        assert_se(socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0, stream_fds) >= 0);
        dummy_server_init(&s, NULL, 0);
        assert_se(stdout_stream_install(&s, stream_fds[0], &stream) >= 0);
        assert_se(write(stream_fds[1], data, size) == (ssize_t) size);
        while (ioctl(stream_fds[0], SIOCINQ, &v) == 0 && v)
                sd_event_run(s.event, (uint64_t) -1);
        if (s.n_stdout_streams)
                stdout_stream_destroy(stream);
        server_done(&s);
        stream_fds[1] = safe_close(stream_fds[1]);

        return 0;
}
Пример #12
0
static void test_event_loop(int ifindex) {
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
        _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
        char *ifname;

        ifname = strdup("lo2");
        assert_se(ifname);

        assert_se(sd_netlink_open(&rtnl) >= 0);
        assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);

        assert_se(sd_netlink_call_async(rtnl, m, link_handler, ifname, 0, NULL) >= 0);

        assert_se(sd_event_default(&event) >= 0);

        assert_se(sd_netlink_attach_event(rtnl, event, 0) >= 0);

        assert_se(sd_event_run(event, 0) >= 0);

        assert_se(sd_netlink_detach_event(rtnl) >= 0);

        assert_se((rtnl = sd_netlink_unref(rtnl)) == NULL);
}
Пример #13
0
        assert_se(sd_event_add_signal(e, &u, SIGRTMIN+2, rtqueue_handler, NULL) >= 0);
        assert_se(sd_event_add_signal(e, &v, SIGRTMIN+3, rtqueue_handler, NULL) >= 0);
        assert_se(sd_event_add_signal(e, &s, SIGUSR2, rtqueue_handler, NULL) >= 0);

        assert_se(sd_event_source_set_priority(v, -10) >= 0);

        assert(sigqueue(getpid(), SIGRTMIN+2, (union sigval) { .sival_int = 1 }) >= 0);
        assert(sigqueue(getpid(), SIGRTMIN+3, (union sigval) { .sival_int = 2 }) >= 0);
        assert(sigqueue(getpid(), SIGUSR2, (union sigval) { .sival_int = 3 }) >= 0);
        assert(sigqueue(getpid(), SIGRTMIN+3, (union sigval) { .sival_int = 4 }) >= 0);
        assert(sigqueue(getpid(), SIGUSR2, (union sigval) { .sival_int = 5 }) >= 0);

        assert_se(n_rtqueue == 0);
        assert_se(last_rtqueue_sigval == 0);

        assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
        assert_se(n_rtqueue == 1);
        assert_se(last_rtqueue_sigval == 2); /* first SIGRTMIN+3 */

        assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
        assert_se(n_rtqueue == 2);
        assert_se(last_rtqueue_sigval == 4); /* second SIGRTMIN+3 */

        assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
        assert_se(n_rtqueue == 3);
        assert_se(last_rtqueue_sigval == 3); /* first SIGUSR2 */

        assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
        assert_se(n_rtqueue == 4);
        assert_se(last_rtqueue_sigval == 1); /* SIGRTMIN+2 */
Пример #14
0
int bus_event_loop_with_idle(
                sd_event *e,
                sd_bus *bus,
                const char *name,
                usec_t timeout,
                check_idle_t check_idle,
                void *userdata) {
        bool exiting = false;
        int r, code;

        assert(e);
        assert(bus);
        assert(name);

        for (;;) {
                bool idle;

                r = sd_event_get_state(e);
                if (r < 0)
                        return r;
                if (r == SD_EVENT_FINISHED)
                        break;

                if (check_idle)
                        idle = check_idle(userdata);
                else
                        idle = true;

                r = sd_event_run(e, exiting || !idle ? (uint64_t) -1 : timeout);
                if (r < 0)
                        return r;

                if (r == 0 && !exiting) {

                        r = sd_bus_try_close(bus);
                        if (r == -EBUSY)
                                continue;

                        /* Fallback for dbus1 connections: we
                         * unregister the name and wait for the
                         * response to come through for it */
                        if (r == -ENOTSUP) {

                                /* Inform the service manager that we
                                 * are going down, so that it will
                                 * queue all further start requests,
                                 * instead of assuming we are already
                                 * running. */
                                sd_notify(false, "STOPPING=1");

                                r = bus_async_unregister_and_exit(e, bus, name);
                                if (r < 0)
                                        return r;

                                exiting = true;
                                continue;
                        }

                        if (r < 0)
                                return r;

                        sd_event_exit(e, 0);
                        break;
                }
        }

        r = sd_event_get_exit_code(e, &code);
        if (r < 0)
                return r;

        return code;
}
Пример #15
0
int main(int argc, char *argv[]) {
        Server server;
        int r;

        if (argc > 1) {
                log_error("This program does not take arguments.");
                return EXIT_FAILURE;
        }

        log_set_target(LOG_TARGET_SAFE);
        log_set_facility(LOG_SYSLOG);
        log_parse_environment();
        log_open();

        umask(0022);

        sigbus_install();

        r = server_init(&server);
        if (r < 0)
                goto finish;

        server_vacuum(&server, false);
        server_flush_to_var(&server, true);
        server_flush_dev_kmsg(&server);

        log_debug("systemd-journald running as pid "PID_FMT, getpid_cached());
        server_driver_message(&server, 0,
                              "MESSAGE_ID=" SD_MESSAGE_JOURNAL_START_STR,
                              LOG_MESSAGE("Journal started"),
                              NULL);

        /* Make sure to send the usage message *after* flushing the
         * journal so entries from the runtime journals are ordered
         * before this message. See #4190 for some details. */
        server_space_usage_message(&server, NULL);

        for (;;) {
                usec_t t = USEC_INFINITY, n;

                r = sd_event_get_state(server.event);
                if (r < 0)
                        goto finish;
                if (r == SD_EVENT_FINISHED)
                        break;

                n = now(CLOCK_REALTIME);

                if (server.max_retention_usec > 0 && server.oldest_file_usec > 0) {

                        /* The retention time is reached, so let's vacuum! */
                        if (server.oldest_file_usec + server.max_retention_usec < n) {
                                log_info("Retention time reached.");
                                server_rotate(&server);
                                server_vacuum(&server, false);
                                continue;
                        }

                        /* Calculate when to rotate the next time */
                        t = server.oldest_file_usec + server.max_retention_usec - n;
                }

#if HAVE_GCRYPT
                if (server.system_journal) {
                        usec_t u;

                        if (journal_file_next_evolve_usec(server.system_journal, &u)) {
                                if (n >= u)
                                        t = 0;
                                else
                                        t = MIN(t, u - n);
                        }
                }
#endif

                r = sd_event_run(server.event, t);
                if (r < 0) {
                        log_error_errno(r, "Failed to run event loop: %m");
                        goto finish;
                }

                server_maybe_append_tags(&server);
                server_maybe_warn_forward_syslog_missed(&server);
        }

        log_debug("systemd-journald stopped as pid "PID_FMT, getpid_cached());
        server_driver_message(&server, 0,
                              "MESSAGE_ID=" SD_MESSAGE_JOURNAL_STOP_STR,
                              LOG_MESSAGE("Journal stopped"),
                              NULL);

finish:
        server_done(&server);

        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
Пример #16
0
static void test_receive_oui_packet(sd_event *e) {
    sd_lldp *lldp;
    sd_lldp_packet **packets;
    uint32_t id32;
    uint16_t id16, len;
    uint8_t flags;
    char *str;
    uint8_t frame[] = {
        /* Ethernet header */
        0x01, 0x80, 0xc2, 0x00, 0x00, 0x03,     /* Destination MAC*/
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06,     /* Source MAC */
        0x88, 0xcc,                             /* Ethertype */
        /* LLDP mandatory TLVs */
        0x02, 0x07, 0x04, 0x00, 0x01, 0x02,     /* Chassis: MAC, 00:01:02:03:04:05 */
        0x03, 0x04, 0x05,
        0x04, 0x04, 0x05, 0x31, 0x2f, 0x33,     /* Port TLV: interface name, "1/3" */
        0x06, 0x02, 0x00, 0x78,                 /* TTL: 120 seconds*/
        /* LLDP optional TLVs */
        0xfe, 0x06, 0x00, 0x80, 0xc2, 0x01,     /* Port VLAN ID: 0x1234 */
        0x12, 0x34,
        0xfe, 0x07, 0x00, 0x80, 0xc2, 0x02,     /* Port and protocol: flag 1, PPVID 0x7788 */
        0x01, 0x77, 0x88,
        0xfe, 0x0d, 0x00, 0x80, 0xc2, 0x03,     /* VLAN Name: ID 0x1234, name "Vlan51" */
        0x12, 0x34, 0x06, 0x56, 0x6c, 0x61,
        0x6e, 0x35, 0x31,
        0xfe, 0x06, 0x00, 0x80, 0xc2, 0x06,     /* Management VID: 0x0102 */
        0x01, 0x02,
        0xfe, 0x09, 0x00, 0x80, 0xc2, 0x07,     /* Link aggregation: status 1, ID 0x00140012 */
        0x01, 0x00, 0x14, 0x00, 0x12,
        0x00, 0x00                              /* End of LLDPDU */
    };

    lldp_handler_calls = 0;
    assert_se(start_lldp(&lldp, e, lldp_handler, NULL) == 0);

    assert_se(write(test_fd[1], frame, sizeof(frame)) == sizeof(frame));
    sd_event_run(e, 0);
    assert_se(lldp_handler_calls == 1);
    assert_se(sd_lldp_get_packets(lldp, &packets) == 1);

    assert_se(sd_lldp_packet_read_port_vlan_id(packets[0], &id16) == 0);
    assert_se(id16 == 0x1234);

    assert_se(sd_lldp_packet_read_port_protocol_vlan_id(packets[0], &flags, &id16) == 0);
    assert_se(flags == 1);
    assert_se(id16 == 0x7788);

    assert_se(sd_lldp_packet_read_vlan_name(packets[0], &id16, &str, &len) == 0);
    assert_se(id16 == 0x1234);
    assert_se(len == 6);
    assert_se(strneq(str, "Vlan51", 6));

    assert_se(sd_lldp_packet_read_management_vid(packets[0], &id16) == 0);
    assert_se(id16 == 0x0102);

    assert_se(sd_lldp_packet_read_link_aggregation(packets[0], &flags, &id32) == 0);
    assert_se(flags == 1);
    assert_se(id32 == 0x00140012);

    sd_lldp_packet_unref(packets[0]);
    free(packets);

    assert_se(stop_lldp(lldp) == 0);
}
Пример #17
0
static void test_receive_basic_packet(sd_event *e) {
    sd_lldp *lldp;
    sd_lldp_packet **packets;
    uint8_t type, *data;
    uint16_t length, ttl;
    int dest_type;
    char *str;
    uint8_t frame[] = {
        /* Ethernet header */
        0x01, 0x80, 0xc2, 0x00, 0x00, 0x03,     /* Destination MAC*/
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06,     /* Source MAC */
        0x88, 0xcc,                             /* Ethertype */
        /* LLDP mandatory TLVs */
        0x02, 0x07, 0x04, 0x00, 0x01, 0x02,     /* Chassis: MAC, 00:01:02:03:04:05 */
        0x03, 0x04, 0x05,
        0x04, 0x04, 0x05, 0x31, 0x2f, 0x33,     /* Port: interface name, "1/3" */
        0x06, 0x02, 0x00, 0x78,                 /* TTL: 120 seconds*/
        /* LLDP optional TLVs */
        0x08, 0x04, 0x50, 0x6f, 0x72, 0x74,     /* Port Description: "Port" */
        0x0a, 0x03, 0x53, 0x59, 0x53,           /* System Name: "SYS" */
        0x0c, 0x04, 0x66, 0x6f, 0x6f, 0x00,     /* System Description: "foo" (NULL-terminated) */
        0x00, 0x00                              /* End Of LLDPDU */
    };

    lldp_handler_calls = 0;
    assert_se(start_lldp(&lldp, e, lldp_handler, NULL) == 0);

    assert_se(write(test_fd[1], frame, sizeof(frame)) == sizeof(frame));
    sd_event_run(e, 0);
    assert_se(lldp_handler_calls == 1);
    assert_se(sd_lldp_get_packets(lldp, &packets) == 1);

    assert_se(sd_lldp_packet_read_chassis_id(packets[0], &type, &data, &length) == 0);
    assert_se(type == LLDP_CHASSIS_SUBTYPE_MAC_ADDRESS);
    assert_se(length == ETH_ALEN);
    assert_se(!memcmp(data, "\x00\x01\x02\x03\x04\x05", ETH_ALEN));

    assert_se(sd_lldp_packet_read_port_id(packets[0], &type, &data, &length) == 0);
    assert_se(type == LLDP_PORT_SUBTYPE_INTERFACE_NAME);
    assert_se(length == 3);
    assert_se(strneq((char *) data, "1/3", 3));

    assert_se(sd_lldp_packet_read_port_description(packets[0], &str, &length) == 0);
    assert_se(length == 4);
    assert_se(strneq(str, "Port", 4));

    assert_se(sd_lldp_packet_read_system_name(packets[0], &str, &length) == 0);
    assert_se(length == 3);
    assert_se(strneq(str, "SYS", 3));

    assert_se(sd_lldp_packet_read_system_description(packets[0], &str, &length) == 0);
    assert_se(length == 4);         /* This is the real length in the TLV packet */
    assert_se(strneq(str, "foo", 3));

    assert_se(sd_lldp_packet_read_ttl(packets[0], &ttl) == 0);
    assert_se(ttl == 120);

    assert_se(sd_lldp_packet_get_destination_type(packets[0], &dest_type) == 0);
    assert_se(dest_type == SD_LLDP_DESTINATION_TYPE_NEAREST_NON_TPMR_BRIDGE);

    sd_lldp_packet_unref(packets[0]);
    free(packets);

    assert_se(stop_lldp(lldp) == 0);
}
Пример #18
0
int main(int argc, char *argv[])
{
    int result = 0;
    struct cc_event_context *context = NULL;
    sd_event *event = NULL;
    struct cc_client_Calculator *instance1 = NULL, *instance2 = NULL;
    double value = 3.14159265;
    int32_t whole = 0;
    int32_t fraction = 0;

    CC_LOG_OPEN("simpleclient");
    printf("Started simpleclient\n");

    result = cc_backend_startup();
    if (result < 0) {
        printf("unable to startup the backend: %s\n", strerror(-result));
        goto fail;
    }
    result = cc_client_Calculator_new(
        "org.genivi.capic.Server:/instance1:org.genivi.capic.Calculator",
        NULL, &instance1);
    if (result < 0) {
        printf("unable to create client instance '/instance1': %s\n", strerror(-result));
        goto fail;
    }
    result = cc_client_Calculator_new(
        "org.genivi.capic.Server:/instance2:org.genivi.capic.Calculator",
        NULL, &instance2);
    if (result < 0) {
        printf("unable to create client instance '/instance2': %s\n", strerror(-result));
        goto fail;
    }

    result = cc_backend_get_event_context(&context);
    if (result < 0) {
        printf("unable to get backend event context: %s\n", strerror(-result));
        goto fail;
    }
    event = (sd_event *) cc_event_get_native(context);
    assert(event);
    sd_event_ref(event);

    printf("invoking method instance1.split() with value=%g\n", value);
    printf(
        "expecting to receive whole=%d, fraction=%d\n", (int32_t)value,
        (int32_t)((value - (double)(int32_t)value) * 1.0e+9));
    result = cc_Calculator_split(instance1, value, &whole, &fraction);
    if (result < 0) {
        printf("failed while calling cc_Calculator_split(): %s\n", strerror(-result));
        goto fail;
    }
    printf("received whole=%d, fraction=%d\n", whole, fraction);

    printf("invoking method instance2.split() with value=%g\n", value);
    printf(
        "expecting to receive whole=%d, fraction=%d\n", (int32_t)value,
        (int32_t)((value - (double)(int32_t)value) * 1.0e+9));
    result = cc_Calculator_split_async(
        instance2, value, &complete_Calculator_split);
    if (result < 0) {
        printf("unable to issue cc_Calculator_split_async(): %s\n", strerror(-result));
        goto fail;
    }
    result = cc_Calculator_split(instance2, value, &whole, &fraction);
    if (result >= 0) {
        printf("invoking method with pending reply succeeded unexpectedly");
        goto fail;
    }
    assert(result == -EBUSY);
    result = sd_event_run(event, (uint64_t) -1);
    if (result < 0) {
        printf(
            "unable to complete cc_Calculator_split_async(): %s\n", strerror(-result));
        goto fail;
    }

fail:
    instance2 = cc_client_Calculator_free(instance2);
    instance1 = cc_client_Calculator_free(instance1);
    cc_backend_shutdown();

    CC_LOG_CLOSE();
    printf("exiting simpleclient\n");

    return result;
}
int main(int argc, char *argv[]) {
        sd_event *e = NULL;
        sd_event_source *w = NULL, *x = NULL, *y = NULL, *z = NULL, *q = NULL, *t = NULL;
        static const char ch = 'x';
        int a[2] = { -1, -1 }, b[2] = { -1, -1}, d[2] = { -1, -1}, k[2] = { -1, -1 };

        assert_se(pipe(a) >= 0);
        assert_se(pipe(b) >= 0);
        assert_se(pipe(d) >= 0);
        assert_se(pipe(k) >= 0);

        assert_se(sd_event_default(&e) >= 0);

        assert_se(sd_event_set_watchdog(e, true) >= 0);

        /* Test whether we cleanly can destroy an io event source from its own handler */
        got_unref = false;
        assert_se(sd_event_add_io(e, &t, k[0], EPOLLIN, unref_handler, NULL) >= 0);
        assert_se(write(k[1], &ch, 1) == 1);
        assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
        assert_se(got_unref);

        got_a = false, got_b = false, got_c = false, got_d = 0;

        /* Add a oneshot handler, trigger it, re-enable it, and trigger
         * it again. */
        assert_se(sd_event_add_io(e, &w, d[0], EPOLLIN, io_handler, INT_TO_PTR('d')) >= 0);
        assert_se(sd_event_source_set_enabled(w, SD_EVENT_ONESHOT) >= 0);
        assert_se(write(d[1], &ch, 1) >= 0);
        assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
        assert_se(got_d == 1);
        assert_se(write(d[1], &ch, 1) >= 0);
        assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
        assert_se(got_d == 2);

        assert_se(sd_event_add_io(e, &x, a[0], EPOLLIN, io_handler, INT_TO_PTR('a')) >= 0);
        assert_se(sd_event_add_io(e, &y, b[0], EPOLLIN, io_handler, INT_TO_PTR('b')) >= 0);
        assert_se(sd_event_add_time(e, &z, CLOCK_MONOTONIC, 0, 0, time_handler, INT_TO_PTR('c')) >= 0);
        assert_se(sd_event_add_exit(e, &q, exit_handler, INT_TO_PTR('g')) >= 0);

        assert_se(sd_event_source_set_priority(x, 99) >= 0);
        assert_se(sd_event_source_set_enabled(y, SD_EVENT_ONESHOT) >= 0);
        assert_se(sd_event_source_set_prepare(x, prepare_handler) >= 0);
        assert_se(sd_event_source_set_priority(z, 50) >= 0);
        assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0);
        assert_se(sd_event_source_set_prepare(z, prepare_handler) >= 0);

        /* Test for floating event sources */
        assert_se(sigprocmask_many(SIG_BLOCK, SIGRTMIN+1, -1) == 0);
        assert_se(sd_event_add_signal(e, NULL, SIGRTMIN+1, NULL, NULL) >= 0);

        assert_se(write(a[1], &ch, 1) >= 0);
        assert_se(write(b[1], &ch, 1) >= 0);

        assert_se(!got_a && !got_b && !got_c);

        assert_se(sd_event_run(e, (uint64_t) -1) >= 1);

        assert_se(!got_a && got_b && !got_c);

        assert_se(sd_event_run(e, (uint64_t) -1) >= 1);

        assert_se(!got_a && got_b && got_c);

        assert_se(sd_event_run(e, (uint64_t) -1) >= 1);

        assert_se(got_a && got_b && got_c);

        sd_event_source_unref(x);
        sd_event_source_unref(y);

        do_quit = true;
        assert_se(sd_event_source_set_time(z, now(CLOCK_MONOTONIC) + 200 * USEC_PER_MSEC) >= 0);
        assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0);

        assert_se(sd_event_loop(e) >= 0);

        sd_event_source_unref(z);
        sd_event_source_unref(q);

        sd_event_source_unref(w);

        sd_event_unref(e);

        safe_close_pair(a);
        safe_close_pair(b);
        safe_close_pair(d);
        safe_close_pair(k);

        return 0;
}
Пример #20
0
int main(int argc, char *argv[])  {
  struct afb_hsrv *hsrv;
  struct afb_config *config;
  struct sd_event *eventloop;

  LOGAUTH("afb-daemon");

  // ------------- Build session handler & init config -------
  config = calloc (1, sizeof (struct afb_config));

  on_exit(closeSession, config);
  parse_arguments(argc, argv, config);

  // ------------------ sanity check ----------------------------------------
  if (config->httpdPort <= 0) {
     ERROR("no port is defined");
     exit (1);
  }

  if (config->ldpaths) {
    if (afb_api_so_add_pathset(config->ldpaths) < 0) {
      ERROR("initialisation of plugins within %s failed", config->ldpaths);
      exit(1);
    }
  }

  start_items(config->items);
  config->items = NULL;

  ctxStoreInit(CTX_NBCLIENTS, config->cntxTimeout, config->token, afb_apis_count());
  if (!afb_hreq_init_cookie(config->httpdPort, config->rootapi, DEFLT_CNTX_TIMEOUT)) {
     ERROR("initialisation of cookies failed");
     exit (1);
  }

  if (afb_sig_handler_init() < 0) {
     ERROR("main fail to initialise signal handlers");
     return 1;
  }

  // let's run this program with a low priority
  nice (20);

  // ------------------ Finaly Process Commands -----------------------------
  // let's not take the risk to run as ROOT
  //if (getuid() == 0)  goto errorNoRoot;

  DEBUG("Init config done");

  // --------- run -----------
  if (config->background) {
      // --------- in background mode -----------
      INFO("entering background mode");
      daemonize(config);
  } else {
      // ---- in foreground mode --------------------
      INFO("entering foreground mode");
  }

   hsrv = start_http_server(config);
   if (hsrv == NULL)
	exit(1);

   if (config->readyfd != 0) {
		static const char readystr[] = "READY=1";
		write(config->readyfd, readystr, sizeof(readystr) - 1);
		close(config->readyfd);
  }

   // infinite loop
  eventloop = afb_common_get_event_loop();
  for(;;)
    sd_event_run(eventloop, 30000000);

  WARNING("hoops returned from infinite loop [report bug]");

  return 0;
}
Пример #21
0
static void test_receive_basic_packet(sd_event *e) {

        static const uint8_t frame[] = {
                /* Ethernet header */
                0x01, 0x80, 0xc2, 0x00, 0x00, 0x03,     /* Destination MAC*/
                0x01, 0x02, 0x03, 0x04, 0x05, 0x06,     /* Source MAC */
                0x88, 0xcc,                             /* Ethertype */
                /* LLDP mandatory TLVs */
                0x02, 0x07, 0x04, 0x00, 0x01, 0x02,     /* Chassis: MAC, 00:01:02:03:04:05 */
                0x03, 0x04, 0x05,
                0x04, 0x04, 0x05, 0x31, 0x2f, 0x33,     /* Port: interface name, "1/3" */
                0x06, 0x02, 0x00, 0x78,                 /* TTL: 120 seconds*/
                /* LLDP optional TLVs */
                0x08, 0x04, 0x50, 0x6f, 0x72, 0x74,     /* Port Description: "Port" */
                0x0a, 0x03, 0x53, 0x59, 0x53,           /* System Name: "SYS" */
                0x0c, 0x04, 0x66, 0x6f, 0x6f, 0x00,     /* System Description: "foo" (NULL-terminated) */
                0x00, 0x00                              /* End Of LLDPDU */
        };

        sd_lldp *lldp;
        sd_lldp_neighbor **neighbors;
        uint8_t type;
        const void *data;
        uint16_t ttl;
        size_t length;
        const char *str;

        lldp_handler_calls = 0;
        assert_se(start_lldp(&lldp, e, lldp_handler, NULL) == 0);

        assert_se(write(test_fd[1], frame, sizeof(frame)) == sizeof(frame));
        sd_event_run(e, 0);
        assert_se(lldp_handler_calls == 1);
        assert_se(sd_lldp_get_neighbors(lldp, &neighbors) == 1);

        assert_se(sd_lldp_neighbor_get_chassis_id(neighbors[0], &type, &data, &length) == 0);
        assert_se(type == SD_LLDP_CHASSIS_SUBTYPE_MAC_ADDRESS);
        assert_se(length == ETH_ALEN);
        assert_se(!memcmp(data, "\x00\x01\x02\x03\x04\x05", ETH_ALEN));

        assert_se(sd_lldp_neighbor_get_port_id(neighbors[0], &type, &data, &length) == 0);
        assert_se(type == SD_LLDP_PORT_SUBTYPE_INTERFACE_NAME);
        assert_se(length == 3);
        assert_se(strneq((char *) data, "1/3", 3));

        assert_se(sd_lldp_neighbor_get_port_description(neighbors[0], &str) == 0);
        assert_se(streq(str, "Port"));

        assert_se(sd_lldp_neighbor_get_system_name(neighbors[0], &str) == 0);
        assert_se(streq(str, "SYS"));

        assert_se(sd_lldp_neighbor_get_system_description(neighbors[0], &str) == 0);
        assert_se(streq(str, "foo"));

        assert_se(sd_lldp_neighbor_get_ttl(neighbors[0], &ttl) == 0);
        assert_se(ttl == 120);

        sd_lldp_neighbor_unref(neighbors[0]);
        free(neighbors);

        assert_se(stop_lldp(lldp) == 0);
}
Пример #22
0
static void test_receive_oui_packet(sd_event *e) {
        sd_lldp *lldp;
        sd_lldp_neighbor **neighbors;
        uint8_t frame[] = {
                /* Ethernet header */
                0x01, 0x80, 0xc2, 0x00, 0x00, 0x03,     /* Destination MAC*/
                0x01, 0x02, 0x03, 0x04, 0x05, 0x06,     /* Source MAC */
                0x88, 0xcc,                             /* Ethertype */
                /* LLDP mandatory TLVs */
                0x02, 0x07, 0x04, 0x00, 0x01, 0x02,     /* Chassis: MAC, 00:01:02:03:04:05 */
                0x03, 0x04, 0x05,
                0x04, 0x04, 0x05, 0x31, 0x2f, 0x33,     /* Port TLV: interface name, "1/3" */
                0x06, 0x02, 0x00, 0x78,                 /* TTL: 120 seconds*/
                /* LLDP optional TLVs */
                0xfe, 0x06, 0x00, 0x80, 0xc2, 0x01,     /* Port VLAN ID: 0x1234 */
                0x12, 0x34,
                0xfe, 0x07, 0x00, 0x80, 0xc2, 0x02,     /* Port and protocol: flag 1, PPVID 0x7788 */
                0x01, 0x77, 0x88,
                0xfe, 0x0d, 0x00, 0x80, 0xc2, 0x03,     /* VLAN Name: ID 0x1234, name "Vlan51" */
                0x12, 0x34, 0x06, 0x56, 0x6c, 0x61,
                0x6e, 0x35, 0x31,
                0xfe, 0x06, 0x00, 0x80, 0xc2, 0x06,     /* Management VID: 0x0102 */
                0x01, 0x02,
                0xfe, 0x09, 0x00, 0x80, 0xc2, 0x07,     /* Link aggregation: status 1, ID 0x00140012 */
                0x01, 0x00, 0x14, 0x00, 0x12,
                0x00, 0x00                              /* End of LLDPDU */
        };

        lldp_handler_calls = 0;
        assert_se(start_lldp(&lldp, e, lldp_handler, NULL) == 0);

        assert_se(write(test_fd[1], frame, sizeof(frame)) == sizeof(frame));
        sd_event_run(e, 0);
        assert_se(lldp_handler_calls == 1);
        assert_se(sd_lldp_get_neighbors(lldp, &neighbors) == 1);

        assert_se(sd_lldp_neighbor_tlv_rewind(neighbors[0]) >= 0);
        assert_se(sd_lldp_neighbor_tlv_is_type(neighbors[0], SD_LLDP_TYPE_CHASSIS_ID) > 0);
        assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0);
        assert_se(sd_lldp_neighbor_tlv_is_type(neighbors[0], SD_LLDP_TYPE_PORT_ID) > 0);
        assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0);
        assert_se(sd_lldp_neighbor_tlv_is_type(neighbors[0], SD_LLDP_TYPE_TTL) > 0);
        assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0);
        assert_se(sd_lldp_neighbor_tlv_is_oui(neighbors[0], SD_LLDP_OUI_802_1, SD_LLDP_OUI_802_1_SUBTYPE_PORT_VLAN_ID) > 0);
        assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0);
        assert_se(sd_lldp_neighbor_tlv_is_oui(neighbors[0], SD_LLDP_OUI_802_1, SD_LLDP_OUI_802_1_SUBTYPE_PORT_PROTOCOL_VLAN_ID) > 0);
        assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0);
        assert_se(sd_lldp_neighbor_tlv_is_oui(neighbors[0], SD_LLDP_OUI_802_1, SD_LLDP_OUI_802_1_SUBTYPE_VLAN_NAME) > 0);
        assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0);
        assert_se(sd_lldp_neighbor_tlv_is_oui(neighbors[0], SD_LLDP_OUI_802_1, SD_LLDP_OUI_802_1_SUBTYPE_MANAGEMENT_VID) > 0);
        assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0);
        assert_se(sd_lldp_neighbor_tlv_is_oui(neighbors[0], SD_LLDP_OUI_802_1, SD_LLDP_OUI_802_1_SUBTYPE_LINK_AGGREGATION) > 0);
        assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) > 0);
        assert_se(sd_lldp_neighbor_tlv_is_type(neighbors[0], SD_LLDP_TYPE_END) > 0);
        assert_se(sd_lldp_neighbor_tlv_next(neighbors[0]) == 0);

        sd_lldp_neighbor_unref(neighbors[0]);
        free(neighbors);

        assert_se(stop_lldp(lldp) == 0);
}