Manager *manager_new(void) { Manager *m; int r; m = new0(Manager, 1); if (!m) return NULL; m->machines = hashmap_new(string_hash_func, string_compare_func); m->machine_units = hashmap_new(string_hash_func, string_compare_func); m->machine_leaders = hashmap_new(trivial_hash_func, trivial_compare_func); if (!m->machines || !m->machine_units || !m->machine_leaders) { manager_free(m); return NULL; } r = sd_event_default(&m->event); if (r < 0) { manager_free(m); return NULL; } return m; }
static int manager_new(Manager **ret) { _cleanup_(manager_unrefp) Manager *m = NULL; int r; assert(ret); m = new0(Manager, 1); if (!m) return -ENOMEM; m->machines = hashmap_new(&string_hash_ops); m->machine_units = hashmap_new(&string_hash_ops); m->machine_leaders = hashmap_new(NULL); if (!m->machines || !m->machine_units || !m->machine_leaders) return -ENOMEM; r = sd_event_default(&m->event); if (r < 0) return r; r = sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL); if (r < 0) return r; r = sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL); if (r < 0) return r; (void) sd_event_set_watchdog(m->event, true); *ret = TAKE_PTR(m); return 0; }
int rtnl_message_parse(sd_rtnl_message *m, size_t **rta_offset_tb, unsigned short *rta_tb_size, int max, struct rtattr *rta, unsigned int rt_len) { unsigned short type; size_t *tb; tb = new0(size_t, max + 1); if(!tb) return -ENOMEM; *rta_tb_size = max + 1; for (; RTA_OK(rta, rt_len); rta = RTA_NEXT(rta, rt_len)) { type = RTA_TYPE(rta); /* if the kernel is newer than the headers we used when building, we ignore out-of-range attributes */ if (type > max) continue; if (tb[type]) log_debug("rtnl: message parse - overwriting repeated attribute"); tb[type] = (uint8_t *) rta - (uint8_t *) m->hdr; } *rta_offset_tb = tb; return 0; }
int sd_dhcp_client_new(sd_dhcp_client **ret) { _cleanup_dhcp_client_free_ sd_dhcp_client *client = NULL; assert_return(ret, -EINVAL); client = new0(sd_dhcp_client, 1); if (!client) return -ENOMEM; client->n_ref = REFCNT_INIT; client->state = DHCP_STATE_INIT; client->index = -1; client->fd = -1; client->attempt = 1; client->req_opts_size = ELEMENTSOF(default_req_opts); client->req_opts = memdup(default_req_opts, client->req_opts_size); if (!client->req_opts) return -ENOMEM; *ret = client; client = NULL; return 0; }
int sysview_seat_new(sysview_seat **out, sysview_context *c, const char *name) { _cleanup_(sysview_seat_freep) sysview_seat *seat = NULL; int r; assert_return(c, -EINVAL); assert_return(name, -EINVAL); seat = new0(sysview_seat, 1); if (!seat) return -ENOMEM; seat->context = c; seat->name = strdup(name); if (!seat->name) return -ENOMEM; seat->session_map = hashmap_new(string_hash_func, string_compare_func); if (!seat->session_map) return -ENOMEM; seat->device_map = hashmap_new(string_hash_func, string_compare_func); if (!seat->device_map) return -ENOMEM; r = hashmap_put(c->seat_map, seat->name, seat); if (r < 0) return r; if (out) *out = seat; seat = NULL; return 0; }
int raw_export_new( RawExport **ret, sd_event *event, RawExportFinished on_finished, void *userdata) { _cleanup_(raw_export_unrefp) RawExport *e = NULL; int r; assert(ret); e = new0(RawExport, 1); if (!e) return -ENOMEM; e->output_fd = e->input_fd = -1; e->on_finished = on_finished; e->userdata = userdata; RATELIMIT_INIT(e->progress_rate_limit, 100 * USEC_PER_MSEC, 1); e->last_percent = (unsigned) -1; if (event) e->event = sd_event_ref(event); else { r = sd_event_default(&e->event); if (r < 0) return r; } *ret = TAKE_PTR(e); return 0; }
int route_new_static(Network *network, unsigned section, Route **ret) { _cleanup_route_free_ Route *route = NULL; if (section) { uint64_t key = section; route = hashmap_get(network->routes_by_section, &key); if (route) { *ret = route; route = NULL; return 0; } } route = new0(Route, 1); if (!route) return -ENOMEM; route->family = AF_UNSPEC; route->network = network; LIST_PREPEND(static_routes, network->static_routes, route); if (section) { route->section = section; hashmap_put(network->routes_by_section, &route->section, route); } *ret = route; route = NULL; return 0; }
_public_ int sd_lldp_new(sd_lldp **ret, int ifindex) { _cleanup_(sd_lldp_unrefp) sd_lldp *lldp = NULL; int r; assert_return(ret, -EINVAL); assert_return(ifindex > 0, -EINVAL); lldp = new0(sd_lldp, 1); if (!lldp) return -ENOMEM; lldp->fd = -1; lldp->ifindex = ifindex; lldp->neighbors_max = LLDP_DEFAULT_NEIGHBORS_MAX; lldp->capability_mask = (uint16_t) -1; lldp->neighbor_by_id = hashmap_new(&lldp_neighbor_id_hash_ops); if (!lldp->neighbor_by_id) return -ENOMEM; r = prioq_ensure_allocated(&lldp->neighbor_by_expiry, lldp_neighbor_prioq_compare_func); if (r < 0) return r; *ret = lldp; lldp = NULL; return 0; }
int lldp_port_new(int ifindex, const char *ifname, const struct ether_addr *addr, void *userdata, lldp_port **ret) { _cleanup_free_ lldp_port *p = NULL; assert_return(ifindex, -EINVAL); assert_return(ifname, -EINVAL); assert_return(addr, -EINVAL); p = new0(lldp_port, 1); if (!p) return -ENOMEM; p->rawfd = -1; p->ifindex = ifindex; p->ifname = strdup(ifname); if (!p->ifname) return -ENOMEM; memcpy(&p->mac, addr, ETH_ALEN); p->userdata = userdata; *ret = p; p = NULL; return 0; }
static int json_variant_deep_copy(JsonVariant *ret, JsonVariant *variant) { int r; assert(ret); assert(variant); ret->type = variant->type; ret->size = variant->size; if (variant->type == JSON_VARIANT_STRING) { ret->string = memdup(variant->string, variant->size+1); if (!ret->string) return -ENOMEM; } else if (variant->type == JSON_VARIANT_ARRAY || variant->type == JSON_VARIANT_OBJECT) { size_t i; ret->objects = new0(JsonVariant, variant->size); if (!ret->objects) return -ENOMEM; for (i = 0; i < variant->size; ++i) { r = json_variant_deep_copy(&ret->objects[i], &variant->objects[i]); if (r < 0) return r; } } else ret->value = variant->value; return 0; }
int sd_ipv4ll_new(sd_ipv4ll **ret) { _cleanup_(sd_ipv4ll_unrefp) sd_ipv4ll *ll = NULL; int r; assert_return(ret, -EINVAL); ll = new0(sd_ipv4ll, 1); if (!ll) return -ENOMEM; ll->n_ref = 1; r = sd_ipv4acd_new(&ll->acd); if (r < 0) return r; r = sd_ipv4acd_set_callback(ll->acd, ipv4ll_on_acd, ll); if (r < 0) return r; *ret = ll; ll = NULL; return 0; }
int link_new(Manager *m, Link **ret, int ifindex) { _cleanup_(link_freep) Link *l = NULL; int r; assert(m); assert(ifindex > 0); r = hashmap_ensure_allocated(&m->links, NULL); if (r < 0) return r; l = new0(Link, 1); if (!l) return -ENOMEM; l->ifindex = ifindex; l->llmnr_support = RESOLVE_SUPPORT_YES; l->mdns_support = RESOLVE_SUPPORT_NO; l->dnssec_mode = _DNSSEC_MODE_INVALID; r = hashmap_put(m->links, INT_TO_PTR(ifindex), l); if (r < 0) return r; l->manager = m; if (ret) *ret = l; l = NULL; return 0; }
static JournalRateLimitGroup* journal_rate_limit_group_new(JournalRateLimit *r, const char *id, usec_t ts) { JournalRateLimitGroup *g; struct siphash state; assert(r); assert(id); g = new0(JournalRateLimitGroup, 1); if (!g) return NULL; g->id = strdup(id); if (!g->id) goto fail; siphash24_init(&state, r->hash_key); string_hash_func(g->id, &state); g->hash = siphash24_finalize(&state); journal_rate_limit_vacuum(r, ts); LIST_PREPEND(bucket, r->buckets[g->hash % BUCKETS_MAX], g); LIST_PREPEND(lru, r->lru, g); if (!g->lru_next) r->lru_tail = g; r->n_groups++; g->parent = r; return g; fail: journal_rate_limit_group_free(g); return NULL; }
Manager *manager_new(void) { Manager *m; int r; m = new0(Manager, 1); if (!m) return NULL; m->machines = hashmap_new(&string_hash_ops); m->machine_units = hashmap_new(&string_hash_ops); m->machine_leaders = hashmap_new(NULL); if (!m->machines || !m->machine_units || !m->machine_leaders) { manager_free(m); return NULL; } r = sd_event_default(&m->event); if (r < 0) { manager_free(m); return NULL; } sd_event_set_watchdog(m->event, true); return m; }
_public_ int sd_bus_track_new( sd_bus *bus, sd_bus_track **track, sd_bus_track_handler_t handler, void *userdata) { sd_bus_track *t; assert_return(bus, -EINVAL); assert_return(bus = bus_resolve(bus), -ENOPKG); assert_return(track, -EINVAL); if (!bus->bus_client) return -EINVAL; t = new0(sd_bus_track, 1); if (!t) return -ENOMEM; t->n_ref = 1; t->handler = handler; t->userdata = userdata; t->bus = sd_bus_ref(bus); LIST_PREPEND(tracks, bus->tracks, t); t->in_list = true; bus_track_add_to_queue(t); *track = t; return 0; }
Button* button_new(Manager *m, const char *name) { Button *b; assert(m); assert(name); b = new0(Button, 1); if (!b) return NULL; b->name = strdup(name); if (!b->name) { free(b); return NULL; } if (hashmap_put(m->buttons, b->name, b) < 0) { free(b->name); free(b); return NULL; } b->manager = m; b->fd = -1; return b; }
int address_new_static(Network *network, unsigned section, Address **ret) { _cleanup_address_free_ Address *address = NULL; if (section) { uint64_t key = section; address = hashmap_get(network->addresses_by_section, &key); if (address) { *ret = address; address = NULL; return 0; } } address = new0(Address, 1); if (!address) return -ENOMEM; address_init(address); address->network = network; LIST_PREPEND(static_addresses, network->static_addresses, address); if (section) { address->section = section; hashmap_put(network->addresses_by_section, &address->section, address); } *ret = address; address = NULL; return 0; }
static int loopback_list_get(MountPoint **head) { _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL; struct udev_list_entry *item = NULL, *first = NULL; _cleanup_udev_unref_ struct udev *udev = NULL; int r; assert(head); udev = udev_new(); if (!udev) return -ENOMEM; e = udev_enumerate_new(udev); if (!e) return -ENOMEM; r = udev_enumerate_add_match_subsystem(e, "block"); if (r < 0) return r; r = udev_enumerate_add_match_sysname(e, "loop*"); if (r < 0) return r; r = udev_enumerate_add_match_sysattr(e, "loop/backing_file", NULL); if (r < 0) return r; r = udev_enumerate_scan_devices(e); if (r < 0) return r; first = udev_enumerate_get_list_entry(e); udev_list_entry_foreach(item, first) { MountPoint *lb; _cleanup_udev_device_unref_ struct udev_device *d; char *loop; const char *dn; d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)); if (!d) return -ENOMEM; dn = udev_device_get_devnode(d); if (!dn) continue; loop = strdup(dn); if (!loop) return -ENOMEM; lb = new0(MountPoint, 1); if (!lb) { free(loop); return -ENOMEM; } lb->path = loop; LIST_PREPEND(mount_point, *head, lb); }
void btGImpactCollisionAlgorithm::shape_vs_shape_collision( btDispatcher* dispatcher, const btCollider* body0, const btCollider* body1, const btCollisionShape* shape0, const btCollisionShape* shape1 ) { const btCollisionShape* tmpShape0 = body0->getCollisionShape(); const btCollisionShape* tmpShape1 = body1->getCollisionShape(); btCollider new0(body0, tmpShape0, body0->getCollisionObject(), body0->getWorldTransform()); btCollider new1(body1, tmpShape1, body1->getCollisionObject(), body1->getWorldTransform()); { btCollisionAlgorithm* algor = newAlgorithm(dispatcher, body0, body1); // post : checkManifold is called m_resultOut->setShapeIdentifiersA(m_part0,m_triface0); m_resultOut->setShapeIdentifiersB(m_part1,m_triface1); btCollisionProcessInfo processInfo(new0, new1, *m_dispatchInfo, m_resultOut, dispatcher); algor->processCollision(processInfo); algor->nihilize(dispatcher); algor->~btCollisionAlgorithm(); dispatcher->freeCollisionAlgorithm(algor); } }
_public_ int sd_bus_track_new( sd_bus *bus, sd_bus_track **track, sd_bus_track_handler_t handler, void *userdata) { sd_bus_track *t; assert_return(bus, -EINVAL); assert_return(track, -EINVAL); if (!bus->bus_client) return -EINVAL; t = new0(sd_bus_track, 1); if (!t) return -ENOMEM; t->n_ref = 1; t->handler = handler; t->userdata = userdata; t->bus = sd_bus_ref(bus); bus_track_add_to_queue(t); *track = t; return 0; }
int sysview_device_new(sysview_device **out, sysview_seat *seat, const char *name) { _cleanup_(sysview_device_freep) sysview_device *device = NULL; int r; assert_return(seat, -EINVAL); assert_return(name, -EINVAL); device = new0(sysview_device, 1); if (!device) return -ENOMEM; device->seat = seat; device->type = (unsigned)-1; device->name = strdup(name); if (!device->name) return -ENOMEM; r = hashmap_put(seat->context->device_map, device->name, device); if (r < 0) return r; r = hashmap_put(seat->device_map, device->name, device); if (r < 0) return r; if (out) *out = device; device = NULL; return 0; }
int dkr_pull_new( DkrPull **ret, sd_event *event, const char *index_url, const char *image_root, DkrPullFinished on_finished, void *userdata) { _cleanup_(dkr_pull_unrefp) DkrPull *i = NULL; char *e; int r; assert(ret); assert(index_url); if (!http_url_is_valid(index_url)) return -EINVAL; i = new0(DkrPull, 1); if (!i) return -ENOMEM; i->on_finished = on_finished; i->userdata = userdata; i->image_root = strdup(image_root ?: "/var/lib/machines"); if (!i->image_root) return -ENOMEM; i->grow_machine_directory = path_startswith(i->image_root, "/var/lib/machines"); i->index_url = strdup(index_url); if (!i->index_url) return -ENOMEM; e = endswith(i->index_url, "/"); if (e) *e = 0; if (event) i->event = sd_event_ref(event); else { r = sd_event_default(&i->event); if (r < 0) return r; } r = curl_glue_new(&i->glue, i->event); if (r < 0) return r; i->glue->on_finished = pull_job_curl_on_finished; i->glue->userdata = i; *ret = i; i = NULL; return 0; }
static int client_acquire(Context *context, uint64_t id, Client **_c) { char *watch = NULL; Client *c; int r; assert(context); assert(_c); c = hashmap_get(context->clients, &id); if (c) { *_c = c; return 0; } if (hashmap_size(context->clients) >= CLIENTS_MAX) return -ENOBUFS; r = hashmap_ensure_allocated(&context->clients, uint64_hash_func, uint64_compare_func); if (r < 0) return r; c = new0(Client, 1); if (!c) return -ENOMEM; c->id = id; r = hashmap_put(context->clients, &c->id, c); if (r < 0) goto fail; c->context = context; if (asprintf(&watch, "type='signal'," "sender='org.freedesktop.DBus'," "path='/org/freedesktop/DBus'," "interface='org.freedesktop.DBus'," "member='NameOwnerChanged'," "arg0=':1.%llu'", (unsigned long long) id) < 0) { r = -ENOMEM; goto fail; } r = sd_bus_add_match(context->bus, watch, on_name_owner_changed, c); if (r < 0) { free(watch); goto fail; } c->watch = watch; *_c = c; return 0; fail: client_free(c); return r; }
int sysview_session_new(sysview_session **out, sysview_seat *seat, const char *name) { _cleanup_(sysview_session_freep) sysview_session *session = NULL; int r; assert_return(seat, -EINVAL); session = new0(sysview_session, 1); if (!session) return -ENOMEM; session->seat = seat; if (name) { /* * If a name is given, we require it to be a logind session * name. The session will be put in managed mode and we use * logind to request controller access. */ session->name = strdup(name); if (!session->name) return -ENOMEM; r = sd_bus_path_encode("/org/freedesktop/login1/session", session->name, &session->path); if (r < 0) return r; session->custom = false;; } else { /* * No session name was given. We assume this is an unmanaged * session controlled by the application. We don't use logind * at all and leave session management to the application. The * name of the session-object is set to a unique random string * that does not clash with the logind namespace. */ r = asprintf(&session->name, "@custom%" PRIu64, ++seat->context->custom_sid); if (r < 0) return -ENOMEM; session->custom = true; } r = hashmap_put(seat->context->session_map, session->name, session); if (r < 0) return r; r = hashmap_put(seat->session_map, session->name, session); if (r < 0) return r; if (out) *out = session; session = NULL; return 0; }
int manager_new(Manager **ret) { _cleanup_manager_free_ Manager *m = NULL; int r; m = new0(Manager, 1); if (!m) return -ENOMEM; r = sd_event_default(&m->event); if (r < 0) return r; sd_event_set_watchdog(m->event, true); r = sd_rtnl_open(&m->rtnl, RTMGRP_LINK | RTMGRP_IPV4_IFADDR); if (r < 0) return r; r = sd_bus_default_system(&m->bus); if (r < 0 && r != -ENOENT) /* TODO: drop when we can rely on kdbus */ return r; r = setup_signals(m); if (r < 0) return r; m->udev = udev_new(); if (!m->udev) return -ENOMEM; /* udev does not initialize devices inside containers, * so we rely on them being already initialized before * entering the container */ if (detect_container(NULL) > 0) { m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "kernel"); if (!m->udev_monitor) return -ENOMEM; } else { m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev"); if (!m->udev_monitor) return -ENOMEM; } m->links = hashmap_new(uint64_hash_func, uint64_compare_func); if (!m->links) return -ENOMEM; m->netdevs = hashmap_new(string_hash_func, string_compare_func); if (!m->netdevs) return -ENOMEM; LIST_HEAD_INIT(m->networks); *ret = m; m = NULL; return 0; }
static int swap_list_get(MountPoint **head) { _cleanup_fclose_ FILE *proc_swaps = NULL; unsigned int i; int r; assert(head); proc_swaps = fopen("/proc/swaps", "re"); if (!proc_swaps) return (errno == ENOENT) ? 0 : -errno; (void) fscanf(proc_swaps, "%*s %*s %*s %*s %*s\n"); for (i = 2;; i++) { MountPoint *swap; char *dev = NULL, *d; int k; k = fscanf(proc_swaps, "%ms " /* device/file */ "%*s " /* type of swap */ "%*s " /* swap size */ "%*s " /* used */ "%*s\n", /* priority */ &dev); if (k != 1) { if (k == EOF) break; log_warning("Failed to parse /proc/swaps:%u.", i); free(dev); continue; } if (endswith(dev, " (deleted)")) { free(dev); continue; } r = cunescape(dev, UNESCAPE_RELAX, &d); free(dev); if (r < 0) return r; swap = new0(MountPoint, 1); if (!swap) { free(d); return -ENOMEM; } swap->path = d; LIST_PREPEND(mount_point, *head, swap); } return 0; }
static int prepend_component(const char **p, bool usec, unsigned nesting, CalendarComponent **c) { int r, start, stop = -1, repeat = 0; CalendarComponent *cc; const char *e = *p; assert(p); assert(c); if (nesting > CALENDARSPEC_COMPONENTS_MAX) return -ENOBUFS; r = parse_component_decimal(&e, usec, &start); if (r < 0) return r; if (e[0] == '.' && e[1] == '.') { e += 2; r = parse_component_decimal(&e, usec, &stop); if (r < 0) return r; repeat = usec ? USEC_PER_SEC : 1; } if (*e == '/') { e++; r = parse_component_decimal(&e, usec, &repeat); if (r < 0) return r; if (repeat == 0) return -ERANGE; } if (!IN_SET(*e, 0, ' ', ',', '-', '~', ':')) return -EINVAL; cc = new0(CalendarComponent, 1); if (!cc) return -ENOMEM; cc->start = start; cc->stop = stop; cc->repeat = repeat; cc->next = *c; *p = e; *c = cc; if (*e ==',') { *p += 1; return prepend_component(p, usec, nesting + 1, c); } return 0; }
static int prepend_component(const char **p, bool usec, CalendarComponent **c) { unsigned long start, stop = -1, repeat = 0; CalendarComponent *cc; int r; const char *e; assert(p); assert(c); e = *p; r = parse_component_decimal(&e, usec, &start); if (r < 0) return r; if (e[0] == '.' && e[1] == '.') { e += 2; r = parse_component_decimal(&e, usec, &stop); if (r < 0) return r; repeat = usec ? USEC_PER_SEC : 1; } if (*e == '/') { e++; r = parse_component_decimal(&e, usec, &repeat); if (r < 0) return r; if (repeat == 0) return -ERANGE; } if (*e != 0 && *e != ' ' && *e != ',' && *e != '-' && *e != '~' && *e != ':') return -EINVAL; cc = new0(CalendarComponent, 1); if (!cc) return -ENOMEM; cc->start = start; cc->stop = stop; cc->repeat = repeat; cc->next = *c; *p = e; *c = cc; if (*e ==',') { *p += 1; return prepend_component(p, usec, c); } return 0; }
int json_variant_new(JsonVariant **ret, JsonVariantType type) { JsonVariant *v; v = new0(JsonVariant, 1); if (!v) return -ENOMEM; v->type = type; *ret = v; return 0; }
Prioq *prioq_new(compare_func_t compare_func) { Prioq *q; q = new0(Prioq, 1); if (!q) return q; q->compare_func = compare_func; return q; }