示例#1
0
_public_ int sd_network_get_operational_state(char **state) {
        _cleanup_free_ char *s = NULL;
        int r;

        assert_return(state, -EINVAL);

        r = parse_env_file("/run/systemd/netif/state", NEWLINE, "OPER_STATE", &s, NULL);
        if (r == -ENOENT)
                return -ENODATA;
        if (r < 0)
                return r;
        if (isempty(s))
                return -ENODATA;

        *state = s;
        s = NULL;

        return 0;
}
示例#2
0
int sd_ipv4acd_new(sd_ipv4acd **ret) {
        _cleanup_ipv4acd_unref_ sd_ipv4acd *ll = NULL;

        assert_return(ret, -EINVAL);

        ll = new0(sd_ipv4acd, 1);
        if (!ll)
                return -ENOMEM;

        ll->n_ref = REFCNT_INIT;
        ll->state = IPV4ACD_STATE_INIT;
        ll->index = -1;
        ll->fd = -1;

        *ret = ll;
        ll = NULL;

        return 0;
}
示例#3
0
int sd_ipv4ll_set_mac(sd_ipv4ll *ll, const struct ether_addr *addr) {
        int r;

        assert_return(ll, -EINVAL);

        if (!ll->random_data) {
                uint64_t seed;

                /* If no random data is set, generate some from the MAC */
                seed = siphash24(&addr->ether_addr_octet, ETH_ALEN, HASH_KEY.bytes);

                assert_cc(sizeof(unsigned) <= 8);

                r = sd_ipv4ll_set_address_seed(ll, (unsigned) htole64(seed));
                if (r < 0)
                        return r;
        }

        return sd_ipv4acd_set_mac(ll->acd, addr);
}
示例#4
0
_public_ int sd_bus_track_remove_name(sd_bus_track *track, const char *name) {
        _cleanup_bus_slot_unref_ sd_bus_slot *slot = NULL;
        _cleanup_free_ char *n = NULL;

        assert_return(name, -EINVAL);

        if (!track)
                return 0;

        slot = hashmap_remove2(track->names, (char*) name, (void**) &n);
        if (!slot)
                return 0;

        if (hashmap_isempty(track->names))
                bus_track_add_to_queue(track);

        track->modified = true;

        return 1;
}
int sd_pppoe_new (sd_pppoe **ret) {
        sd_pppoe *ppp;

        assert_return(ret, -EINVAL);

        ppp = new0(sd_pppoe, 1);
        if (!ppp)
                return -ENOMEM;

        ppp->n_ref = REFCNT_INIT;
        ppp->state = _PPPOE_STATE_INVALID;
        ppp->ifindex = -1;
        ppp->fd = -1;
        ppp->pppoe_fd = -1;
        ppp->padr_resend_count = PPPOE_MAX_PADR_RESEND;

        *ret = ppp;

        return 0;
}
static int output_winch(Output *o) {
    struct winsize wsz = { };
    int r;

    assert_return(o, -EINVAL);

    r = ioctl(o->fd, TIOCGWINSZ, &wsz);
    if (r < 0)
        return log_error_errno(errno, "error: cannot read window-size: %m");

    if (wsz.ws_col != o->width || wsz.ws_row != o->height) {
        o->width = wsz.ws_col;
        o->height = wsz.ws_row;
        o->in_width = MAX(o->width, 2U) - 2;
        o->in_height = MAX(o->height, 6U) - 6;
        o->resized = true;
    }

    return 0;
}
示例#7
0
_public_ int sd_session_get_uid(const char *session, uid_t *uid) {
        int r;
        _cleanup_free_ char *p = NULL, *s = NULL;

        assert_return(uid, -EINVAL);

        r = file_of_session(session, &p);
        if (r < 0)
                return r;

        r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
        if (r == -ENOENT)
                return -ENXIO;
        if (r < 0)
                return r;
        if (isempty(s))
                return -EIO;

        return parse_uid(s, uid);
}
示例#8
0
                        /* Is this a GUID? Then be nice, and skip over
                         * the dashes */

                        if (i == 8)
                                is_guid = true;
                        else if (i == 13 || i == 18 || i == 23) {
                                if (!is_guid)
                                        return -EINVAL;
                        } else
                                return -EINVAL;

                        i++;
                        continue;
                }

                a = unhexchar(s[i++]);
                if (a < 0)
                        return -EINVAL;

                b = unhexchar(s[i++]);
                if (b < 0)
                        return -EINVAL;

                t.bytes[n++] = (a << 4) | b;
        }

        if (i != (is_guid ? 36 : 32))
                return -EINVAL;

        if (s[i] != 0)
                return -EINVAL;

        if (ret)
                *ret = t;
        return 0;
}

_public_ int sd_id128_get_machine(sd_id128_t *ret) {
        static thread_local sd_id128_t saved_machine_id = {};
        int r;

        assert_return(ret, -EINVAL);

        if (sd_id128_is_null(saved_machine_id)) {
                r = id128_read("/etc/machine-id", ID128_PLAIN, &saved_machine_id);
                if (r < 0)
                        return r;

                if (sd_id128_is_null(saved_machine_id))
                        return -EINVAL;
        }

        *ret = saved_machine_id;
        return 0;
}

_public_ int sd_id128_get_boot(sd_id128_t *ret) {
        static thread_local sd_id128_t saved_boot_id = {};
        int r;

        assert_return(ret, -EINVAL);

        if (sd_id128_is_null(saved_boot_id)) {
                r = id128_read("/proc/sys/kernel/random/boot_id", ID128_UUID, &saved_boot_id);
                if (r < 0)
                        return r;
        }

        *ret = saved_boot_id;
        return 0;
}

#if 0 /* NM_IGNORED */
_public_ int sd_id128_get_invocation(sd_id128_t *ret) {
        static thread_local sd_id128_t saved_invocation_id = {};
        int r;

        assert_return(ret, -EINVAL);

        if (sd_id128_is_null(saved_invocation_id)) {
                const char *e;

                e = secure_getenv("INVOCATION_ID");
                if (!e)
                        return -ENXIO;

                r = sd_id128_from_string(e, &saved_invocation_id);
                if (r < 0)
                        return r;
        }

        *ret = saved_invocation_id;
        return 0;
}
示例#9
0
int sd_netlink_message_read_string(sd_netlink_message *m, unsigned short type, const char **data) {
        int r;
        void *attr_data;

        assert_return(m, -EINVAL);

        r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_STRING);
        if (r < 0)
                return r;

        r = netlink_message_read_internal(m, type, &attr_data, NULL);
        if (r < 0)
                return r;
        else if (strnlen(attr_data, r) >= (size_t) r)
                return -EIO;

        if (data)
                *data = (const char *) attr_data;

        return 0;
}
示例#10
0
int sd_netlink_message_read_in6_addr(sd_netlink_message *m, unsigned short type, struct in6_addr *data) {
        int r;
        void *attr_data;

        assert_return(m, -EINVAL);

        r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR);
        if (r < 0)
                return r;

        r = netlink_message_read_internal(m, type, &attr_data, NULL);
        if (r < 0)
                return r;
        else if ((size_t)r < sizeof(struct in6_addr))
                return -EIO;

        if (data)
                memcpy(data, attr_data, sizeof(struct in6_addr));

        return 0;
}
示例#11
0
int sd_netlink_slot_set_floating(sd_netlink_slot *slot, int b) {
        assert_return(slot, -EINVAL);

        if (slot->floating == !!b)
                return 0;

        if (!slot->netlink) /* Already disconnected */
                return -ESTALE;

        slot->floating = b;

        if (b) {
                sd_netlink_slot_ref(slot);
                sd_netlink_unref(slot->netlink);
        } else {
                sd_netlink_ref(slot->netlink);
                sd_netlink_slot_unref(slot);
        }

        return 1;
}
示例#12
0
int sd_netlink_message_read_u8(sd_netlink_message *m, unsigned short type, uint8_t *data) {
        int r;
        void *attr_data;

        assert_return(m, -EINVAL);

        r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U8);
        if (r < 0)
                return r;

        r = netlink_message_read_internal(m, type, &attr_data, NULL);
        if (r < 0)
                return r;
        else if ((size_t) r < sizeof(uint8_t))
                return -EIO;

        if (data)
                *data = *(uint8_t *) attr_data;

        return 0;
}
示例#13
0
int sd_netlink_message_read_cache_info(sd_netlink_message *m, unsigned short type, struct ifa_cacheinfo *info) {
        int r;
        void *attr_data;

        assert_return(m, -EINVAL);

        r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_CACHE_INFO);
        if (r < 0)
                return r;

        r = netlink_message_read_internal(m, type, &attr_data, NULL);
        if (r < 0)
                return r;
        else if ((size_t)r < sizeof(struct ifa_cacheinfo))
                return -EIO;

        if (info)
                memcpy(info, attr_data, sizeof(struct ifa_cacheinfo));

        return 0;
}
示例#14
0
static int session_get_string(const char *session, const char *field, char **value) {
        _cleanup_free_ char *p = NULL, *s = NULL;
        int r;

        assert_return(value, -EINVAL);

        r = file_of_session(session, &p);
        if (r < 0)
                return r;

        r = parse_env_file(p, NEWLINE, field, &s, NULL);
        if (r < 0)
                return r;

        if (isempty(s))
                return -ENOENT;

        *value = s;
        s = NULL;
        return 0;
}
示例#15
0
_public_ int sd_session_get_state(const char *session, char **state) {
        _cleanup_free_ char *p = NULL, *s = NULL;
        int r;

        assert_return(state, -EINVAL);

        r = file_of_session(session, &p);
        if (r < 0)
                return r;

        r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
        if (r < 0)
                return r;
        else if (!s)
                return -EIO;

        *state = s;
        s = NULL;

        return 0;
}
示例#16
0
static int driver_list_queued_owners(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
        struct kdbus_cmd_name_list cmd = {};
        struct kdbus_name_list *name_list;
        struct kdbus_cmd_name *name;
        _cleanup_strv_free_ char **owners = NULL;
        char *arg0;
        int r;

        r = sd_bus_message_read(m, "s", &arg0);
        if (r < 0)
                return r;

        assert_return(service_name_is_valid(arg0), -EINVAL);

        cmd.flags = KDBUS_NAME_LIST_QUEUED;

        r = ioctl(bus->input_fd, KDBUS_CMD_NAME_LIST, &cmd);
        if (r < 0)
                return -errno;

        name_list = (struct kdbus_name_list *) ((uint8_t *) bus->kdbus_buffer + cmd.offset);

        KDBUS_ITEM_FOREACH(name, name_list, names) {
                char *n;

                if (name->size <= sizeof(*name))
                        continue;

                if (!streq(name->name, arg0))
                        continue;

                if (asprintf(&n, ":1.%llu", (unsigned long long) name->owner_id) < 0)
                        return -ENOMEM;

                r = strv_push(&owners, n);
                if (r < 0) {
                        free(n);
                        return -ENOMEM;
                }
        }
示例#17
0
_public_ int sd_session_get_state(const char *session, char **state) {
        _cleanup_free_ char *p = NULL, *s = NULL;
        int r;

        assert_return(state, -EINVAL);

        r = file_of_session(session, &p);
        if (r < 0)
                return r;

        r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
        if (r == -ENOENT)
                return -ENXIO;
        if (r < 0)
                return r;
        if (isempty(s))
                return -EIO;

        *state = TAKE_PTR(s);

        return 0;
}
示例#18
0
_public_ int sd_uid_get_display(uid_t uid, char **session) {
        _cleanup_free_ char *p = NULL, *s = NULL;
        int r;

        assert_return(session, -EINVAL);

        r = file_of_uid(uid, &p);
        if (r < 0)
                return r;

        r = parse_env_file(p, NEWLINE, "DISPLAY", &s, NULL);
        if (r < 0)
                return r;

        if (isempty(s))
                return -ENOENT;

        *session = s;
        s = NULL;

        return 0;
}
示例#19
0
int message_new_empty(sd_netlink *rtnl, sd_netlink_message **ret) {
        sd_netlink_message *m;

        assert_return(ret, -EINVAL);

        /* Note that 'rtnl' is currently unused, if we start using it internally
           we must take care to avoid problems due to mutual references between
           buses and their queued messages. See sd-bus.
         */

        m = new0(sd_netlink_message, 1);
        if (!m)
                return -ENOMEM;

        m->n_ref = REFCNT_INIT;

        m->sealed = false;

        *ret = m;

        return 0;
}
示例#20
0
static int test_hash_add_unique(const struct test *t)
{
	const char *k[] = { "k1", "k2", "k3", "k4", "k5" };
	const char *v[] = { "v1", "v2", "v3", "v4", "v5" };
	unsigned int i, j, N;

	N = ARRAY_SIZE(k);
	for (i = 0; i < N; i++) {
		/* With N - 1 buckets, there'll be a bucket with more than one key. */
		struct hash *h = hash_new(N - 1, NULL);

		/* Add the keys in different orders. */
		for (j = 0; j < N; j++) {
			unsigned int idx = (j + i) % N;
			hash_add_unique(h, k[idx], v[idx]);
		}

		assert_return(hash_get_count(h) == N, EXIT_FAILURE);
		hash_free(h);
	}
	return 0;
}
示例#21
0
static int check_good_user(sd_bus_message *m, uid_t good_user) {
        _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
        uid_t sender_uid;
        int r;

        assert(m);

        if (good_user == UID_INVALID)
                return 0;

        r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_EUID, &creds);
        if (r < 0)
                return r;

        /* Don't trust augmented credentials for authorization */
        assert_return((sd_bus_creds_get_augmented_mask(creds) & SD_BUS_CREDS_EUID) == 0, -EPERM);

        r = sd_bus_creds_get_euid(creds, &sender_uid);
        if (r < 0)
                return r;

        return sender_uid == good_user;
}
示例#22
0
文件: sd-hwdb.c 项目: Keruspe/systemd
_public_ int sd_hwdb_new(sd_hwdb **ret) {
        _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
        const char *hwdb_bin_path;
        const char sig[] = HWDB_SIG;

        assert_return(ret, -EINVAL);

        hwdb = new0(sd_hwdb, 1);
        if (!hwdb)
                return -ENOMEM;

        hwdb->n_ref = REFCNT_INIT;

        /* find hwdb.bin in hwdb_bin_paths */
        NULSTR_FOREACH(hwdb_bin_path, hwdb_bin_paths) {
                hwdb->f = fopen(hwdb_bin_path, "re");
                if (hwdb->f)
                        break;
                else if (errno == ENOENT)
                        continue;
                else
                        return log_debug_errno(errno, "Failed to open %s: %m", hwdb_bin_path);
        }
示例#23
0
int lldp_port_start(lldp_port *p) {
        int r;

        assert_return(p, -EINVAL);

        r = lldp_network_bind_raw_socket(p->ifindex);
        if (r < 0)
                return r;

        p->rawfd = r;

        r = sd_event_add_io(p->event, &p->lldp_port_rx,
                            p->rawfd, EPOLLIN, lldp_receive_packet, p);
        if (r < 0) {
                log_debug_errno(r, "Failed to allocate event source: %m");
                goto fail;
        }

        r = sd_event_source_set_priority(p->lldp_port_rx, p->event_priority);
        if (r < 0) {
                log_debug_errno(r, "Failed to set event priority: %m");
                goto fail;
        }

        r = sd_event_source_set_description(p->lldp_port_rx, "lldp-port-rx");
        if (r < 0) {
                log_debug_errno(r, "Failed to set event name: %m");
                goto fail;
        }

        return 0;

fail:
        lldp_port_stop(p);

        return r;
}
示例#24
0
_public_ int sd_bus_set_property(
                sd_bus *bus,
                const char *destination,
                const char *path,
                const char *interface,
                const char *member,
                sd_bus_error *error,
                const char *type, ...) {

        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
        va_list ap;
        int r;

        assert_return(bus, -EINVAL);
        assert_return(isempty(interface) || interface_name_is_valid(interface), -EINVAL);
        assert_return(member_name_is_valid(member), -EINVAL);
        assert_return(signature_is_single(type, false), -EINVAL);
        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
        assert_return(!bus_pid_changed(bus), -ECHILD);

        r = sd_bus_message_new_method_call(bus, destination, path, "org.freedesktop.DBus.Properties", "Set", &m);
        if (r < 0)
                return r;

        r = sd_bus_message_append(m, "ss", strempty(interface), member);
        if (r < 0)
                return r;

        r = sd_bus_message_open_container(m, 'v', type);
        if (r < 0)
                return r;

        va_start(ap, type);
        r = bus_message_append_ap(m, type, ap);
        va_end(ap);
        if (r < 0)
                return r;

        r = sd_bus_message_close_container(m);
        if (r < 0)
                return r;

        return sd_bus_call(bus, m, 0, error, NULL);
}
示例#25
0
int main(int argc, char **argv) {
  int sender_port = 6666, recv_port = 7777;

  struct sockaddr_in recv_addr, sender_addr;
  setupAddress(&recv_addr, recv_port);
  setupAddress(&sender_addr, sender_port);

  int senderFd;
  assert_return(senderFd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
  assert_return(bind(senderFd, (const struct sockaddr*)&sender_addr, sizeof sender_addr));

  int recvFd;
  assert_return(recvFd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP));
  assert_return(bind(recvFd, (const struct sockaddr*)&recv_addr, sizeof recv_addr));

  //sendto
  char buf[BUF_LENGTH];
  int datagram_size;
  assert_return(datagram_size = sprintf(buf, "This is packet %d\n", 666));
  my_assert(sendto(senderFd, buf, datagram_size, 0,
      (const struct sockaddr*)&recv_addr, sizeof(struct sockaddr_in)) == datagram_size);

  //recvfrom
  socklen_t addr_len = 10000000;
  struct sockaddr_in actual_sender_addr;
  int res = recvfrom(recvFd, buf, BUF_LENGTH, 0, (struct sockaddr*)&actual_sender_addr, &addr_len);
  my_assert(res == datagram_size);

  assert(addr_len == sizeof sender_addr);
  assert(actual_sender_addr.sin_port == sender_addr.sin_port);
  assert(actual_sender_addr.sin_family == sender_addr.sin_family);

  assert_return(close(recvFd));
  assert_return(close(senderFd));

  assert(get_number_of_allocated_net_endpoints() == 0);

  return 0;
}
示例#26
0
static int get_creds_by_name(sd_bus *bus, const char *name, uint64_t mask, sd_bus_creds **_creds, sd_bus_error *error) {
        _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
        int r;

        assert(bus);
        assert(name);
        assert(_creds);

        assert_return(service_name_is_valid(name), -EINVAL);

        r = sd_bus_get_owner(bus, name, mask, &c);
        if (r == -ENOENT || r == -ENXIO)
                return sd_bus_error_setf(error, SD_BUS_ERROR_NAME_HAS_NO_OWNER, "Name %s is currently not owned by anyone.", name);
        if (r < 0)
                return r;

        if ((c->mask & mask) != mask)
                return -ENOTSUP;

        *_creds = c;
        c = NULL;

        return 0;
}
示例#27
0
_public_ int sd_bus_slot_set_floating(sd_bus_slot *slot, int b) {
        assert_return(slot, -EINVAL);

        if (slot->floating == !!b)
                return 0;

        if (!slot->bus) /* already disconnected slots can't be reconnected */
                return -ESTALE;

        slot->floating = b;

        /* When a slot is "floating" then the bus references the slot. Otherwise the slot references the bus. Hence,
         * when we move from one to the other, let's increase one reference and decrease the other. */

        if (b) {
                sd_bus_slot_ref(slot);
                sd_bus_unref(slot->bus);
        } else {
                sd_bus_ref(slot->bus);
                sd_bus_slot_unref(slot);
        }

        return 1;
}
示例#28
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;
}
示例#29
0
int sd_ipv4acd_start(sd_ipv4acd *ll) {
        int r;

        assert_return(ll, -EINVAL);
        assert_return(ll->event, -EINVAL);
        assert_return(ll->index > 0, -EINVAL);
        assert_return(ll->address != 0, -EINVAL);
        assert_return(!ether_addr_is_nul(&ll->mac_addr), -EINVAL);
        assert_return(ll->state == IPV4ACD_STATE_INIT, -EBUSY);

        ll->defend_window = 0;

        r = arp_network_bind_raw_socket(ll->index, ll->address, &ll->mac_addr);
        if (r < 0)
                goto out;

        ll->fd = safe_close(ll->fd);
        ll->fd = r;

        r = sd_event_add_io(ll->event, &ll->receive_message, ll->fd,
                            EPOLLIN, ipv4acd_on_packet, ll);
        if (r < 0)
                goto out;

        r = sd_event_source_set_priority(ll->receive_message, ll->event_priority);
        if (r < 0)
                goto out;

        r = sd_event_source_set_description(ll->receive_message, "ipv4acd-receive-message");
        if (r < 0)
                goto out;

        r = ipv4acd_set_next_wakeup(ll, 0, 0);
        if (r < 0)
                goto out;
out:
        if (r < 0) {
                ipv4acd_stop(ll);
                return r;
        }

        return 0;
}
示例#30
0
_public_ int sd_bus_match_signal(
                sd_bus *bus,
                sd_bus_slot **ret,
                const char *sender,
                const char *path,
                const char *interface,
                const char *member,
                sd_bus_message_handler_t callback,
                void *userdata) {

        const char *expression;

        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(!bus_pid_changed(bus), -ECHILD);
        assert_return(!sender || service_name_is_valid(sender), -EINVAL);
        assert_return(!path || object_path_is_valid(path), -EINVAL);
        assert_return(!interface || interface_name_is_valid(interface), -EINVAL);
        assert_return(!member || member_name_is_valid(member), -EINVAL);

        expression = make_expression(sender, path, interface, member);

        return sd_bus_add_match(bus, ret, expression, callback, userdata);
}