struct hashtable * create_hashtable(unsigned int minsize, unsigned int (*hashf) (void*), int (*eqf) (void*,void*)) { struct hashtable *h; unsigned int pindex, size = primes[0]; /* Check requested hashtable isn't too large */ if (minsize > (1u << 30)) return NULL; /* Enforce size as prime */ for (pindex=0; pindex < prime_table_length; pindex++) { if (primes[pindex] > minsize) { size = primes[pindex]; break; } } h = (struct hashtable *)sp_malloc(sizeof(struct hashtable)); if (NULL == h) return NULL; /*oom*/ h->table = (struct entry **)sp_malloc(sizeof(struct entry*) * size); if (NULL == h->table) { sp_free(h); return NULL; } /*oom*/ memset(h->table, 0, size * sizeof(struct entry *)); h->tablelength = size; h->primeindex = pindex; //since we don't expand the hastable, not needed h->entrycount = 0; // TRANSACTIONAL CONFLICTS ON THIS VARIABLE, h->hashfn = hashf; h->eqfn = eqf; #ifdef DEBUG h->loadlimit = (unsigned int) ceil(size * max_load_factor); #else h->loadlimit = (unsigned int)(size * 65/100); #endif return h; }
int xp_group_add(Xpnodeset *nds, char *adminkey, char *gname, gid_t gid) { Xcmd *addcmd; Spstr *cmd; if (!nds) return -1; cmd = sp_malloc(sizeof(*cmd)); cmd->len = 512; cmd->str = sp_malloc(cmd->len); snprintf(cmd->str, cmd->len, "group-add %s %d\n", gname, gid); addcmd = xcmd_init(cmd, adminkey); if (!addcmd) goto error; if (xp_nodeset_iterate(nds, xp_ctl_cmd, addcmd) > 0) goto error; xcmd_destroy(addcmd); return 0; error: if (addcmd) xcmd_destroy(addcmd); xp_nodeerror_print("xp_group_add"); return -1; }
int xp_group_del(Xpnodeset *nds, char *adminkey, char *gname) { Xcmd *delcmd; Spstr *cmd; if (!nds) return -1; cmd = sp_malloc(sizeof(*cmd)); cmd->len = 512; cmd->str = sp_malloc(cmd->len); snprintf(cmd->str, cmd->len, "group-del %s\n", gname); delcmd = xcmd_init(cmd, adminkey); if (!delcmd) goto error; if (xp_nodeset_iterate(nds, xp_ctl_cmd, delcmd) > 0) goto error; xcmd_destroy(delcmd); return 0; error: if (delcmd) xcmd_destroy(delcmd); xp_nodeerror_print("xp_group_del"); return -1; }
/* The coherently flag tells the program to sum the image pixels coherently or not */ Binned_Data * bin_image_by_r(Image * a, int nshells) { Binned_Data * res = sp_malloc(sizeof(Binned_Data)); res->f = sp_malloc(sizeof(double)*nshells); res->bin_pop = sp_malloc(sizeof(int)*nshells); real max_dist = sqrt(sp_image_x(a)*sp_image_x(a)+sp_image_y(a)*sp_image_y(a)+ sp_image_z(a)*sp_image_z(a)); max_dist /= 2; for(int i = 0; i<nshells; i++) { res->f[i] = 0; res->bin_pop[i] = 0; } for(int i = 0; i<sp_image_size(a); i++) { real dist = sp_image_dist(a,i,SP_TO_CENTER); int bin = sp_min(dist*nshells/max_dist,nshells-1); res->f[bin] += sp_cabs(a->image->data[i]); res->bin_pop[bin]++; } for(int i = 0; i<nshells; i++) { if(res->bin_pop[i] <= 0) { fprintf(stderr,"Zero bin - %d!\n",i); } else { res->f[i] /= res->bin_pop[i]; } } return res; }
int xp_user_del_group(Xpnodeset *nds, char *adminkey, char *uname, char *gname) { Xcmd *grpcmd; Spstr *cmd; if (!nds) return -1; cmd = sp_malloc(sizeof(*cmd)); cmd->len = 1024; cmd->str = sp_malloc(cmd->len); snprintf(cmd->str, cmd->len, "user-del-group %s %s\n", uname, gname); grpcmd = xcmd_init(cmd, adminkey); if (!grpcmd) goto error; if (xp_nodeset_iterate(nds, xp_ctl_cmd, grpcmd) > 0) goto error; xcmd_destroy(grpcmd); return 0; error: if (grpcmd) xcmd_destroy(grpcmd); xp_nodeerror_print("xp_user_del_group"); return -1; }
int xp_user_add(Xpnodeset *nds, char *adminkey, char *uname, uid_t uid, char *gname, char *ukey) { Xcmd *addcmd; Spstr *cmd; char *qkey; if (!nds) return -1; cmd = sp_malloc(sizeof(*cmd)); cmd->len = 4096; cmd->str = sp_malloc(cmd->len); qkey = quotestrdup(ukey); snprintf(cmd->str, cmd->len, "user-add %s %d %s %s\n", uname, uid, gname, qkey); free(qkey); addcmd = xcmd_init(cmd, adminkey); if (!addcmd) goto error; if (xp_nodeset_iterate(nds, xp_ctl_cmd, addcmd) > 0) goto error; xcmd_destroy(addcmd); return 0; error: if (addcmd) xcmd_destroy(addcmd); xp_nodeerror_print("xp_user_add"); return -1; }
Spconn* sp_ethconn_create(Spsrv *srv, int fd) { Spconn *conn = sp_conn_create(srv); if (!conn) return NULL; Spethconn *ethconn = sp_malloc(sizeof(*ethconn)); if (!ethconn) goto error; ethconn->fd = fd; ethconn->spfd = spfd_add(fd, sp_ethconn_notify, conn); if (!ethconn->spfd) goto error; conn->caux = ethconn; conn->shutdown = sp_ethconn_shutdown; conn->dataout = sp_ethconn_dataout; sp_srv_add_conn(srv, conn); return conn; error: free(ethconn); sp_conn_destroy(conn); return NULL; }
Spcfd * spcfd_add_fd(int fd, void (notify)(Spcfd *, void *), void *aux) { Spcfd *ret; ret = sp_malloc(sizeof(*ret)); if (!ret) return NULL; ret->spfd = spfd_add(fd, spcfd_notify, ret); ret->fid = NULL; ret->flags = 0; ret->iounit = 0; ret->notify = notify; ret->aux = aux; ret->offset = 0; ret->rbuf = NULL; ret->rpos = 0; ret->wbuf = NULL; ret->wpos = 0; ret->rtc = NULL; ret->wtc = NULL; ret->next = spcfds; spcfds = ret; return ret; }
Spcfd * spcfd_add(Spcfid *fid, void (notify)(Spcfd *, void *), void *aux, u64 offset) { int iounit; Spcfd *ret; iounit = fid->fsys->msize; if (fid->iounit && iounit > fid->iounit) iounit = fid->iounit; ret = sp_malloc(sizeof(*ret) + 2*iounit); if (!ret) return NULL; ret->spfd = NULL; ret->fid = fid; ret->flags = 0; ret->iounit = iounit; ret->notify = notify; ret->aux = aux; ret->offset = offset; ret->rbuf = ((u8 *) ret) + sizeof(*ret); ret->rpos = 0; ret->wbuf = ret->rbuf + iounit; ret->wpos = 0; ret->rtc = NULL; ret->wtc = NULL; ret->next = spcfds; spcfds = ret; if (fid->mode==Oread || fid->mode==Ordwr) spcfd_send_read_request(ret); return ret; }
int hashtable_insert(struct hashtable *h, void *k, void *v) { /* This method allows duplicate keys - but they shouldn't be used */ unsigned int index; struct entry *e; // if (++(h->entrycount) > h->loadlimit) { /* Ignore the return value. If expand fails, we should * still try cramming just this value into the existing table * -- we may not have memory for a larger table, but one more * element may be ok. Next time we insert, we'll try expanding again.*/ // disable expansion to control memory // hashtable_expand(h); } e = (struct entry *)sp_malloc(sizeof(struct entry)); if (NULL == e) { //--(h->entrycount); return 0; } /*oom*/ e->h = hash(h,k); index = indexFor(h->tablelength,e->h); log("HASH index is %d for hash %08x addr %08x tid %d time %d\n", index, e->h, (unsigned)&(h->table[index]), nf_tid(), nf_time()); e->k = k; e->v = v; e->next = h->table[index]; h->table[index] = e; return -1; }
int xp_group_flush(Xpnodeset *nds, char *adminkey) { Xcmd *flcmd; Spstr *cmd; if (!nds) return -1; cmd = sp_malloc(sizeof(*cmd)); cmd->str = strdup("group-flush\n"); cmd->len = strlen(cmd->str); flcmd = xcmd_init(cmd, adminkey); if (!flcmd) goto error; if (xp_nodeset_iterate(nds, xp_ctl_cmd, flcmd) > 0) goto error; xcmd_destroy(flcmd); return 0; error: if (flcmd) xcmd_destroy(flcmd); xp_nodeerror_print("xp_group_flush"); return -1; }
static Socksrv* sp_socksrv_create_common(int domain, int type, int proto) { Socksrv *ss; int flag = 1; ss = sp_malloc(sizeof(*ss)); if (!ss) return NULL; ss->domain = domain; ss->type = type; ss->proto = proto; ss->shutdown = 0; ss->sock = socket(domain, type, proto); if (ss->sock < 0) { sp_suerror("cannot create socket", errno); free(ss); return NULL; } setsockopt(ss->sock, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof(int)); return ss; }
int xp_getpwent(Xpnode *nd, char *adminkey, char ***pwent) { char *buf = NULL, **toks; int n, bufsize = 8192; Xkey *akey = NULL; Spuser *auser = NULL; Spcfsys *fs = NULL; Spcfid *fid = NULL; if (adminkey) { akey = xauth_privkey_create(adminkey); if (!akey) goto error; } if (xp_defaultuser(&auser, &akey) < 0) goto error; fs = xp_node_mount(nd, auser, akey); if (!fs) { fs = xp_node_mount(nd, NULL, akey); if (!fs) goto error; } fid = spc_open(fs, "pwent", Oread); if (!fid) goto error; buf = sp_malloc(sizeof(*buf) * bufsize); if (!buf) goto error; n = spc_read(fid, (u8 *) buf, bufsize-1, 0); if (n < 0) goto error; buf[bufsize] = '\0'; spc_close(fid); spc_umount(fs); n = tokenize(buf, &toks); if (n < 0) goto error; free(buf); xauth_destroy(akey); *pwent = toks; return n; error: if (fid) spc_close(fid); if (fs) spc_umount(fs); if (buf) free(buf); if (akey) xauth_destroy(akey); return -1; }
int xp_file_create_from_buf(Xpfile **xpf, Spcfsys *fs, char *name, char *buf, int buflen) { Xpfile *f; f = sp_malloc(sizeof(*f)); if (!f) return -1; f->fs = fs; f->name = strdup(name); f->perm = 0; f->create = 0; f->path = NULL; f->buf = buf; f->buflen = buflen; f->bufsize = 0; f->next =NULL; f->fd = -1; f->fid = NULL; f->spcfd = NULL; f->pos = 0; if (*xpf) while (*xpf != NULL) xpf = &(*xpf)->next; *xpf = f; return 0; }
Spfd * spfd_add(int fd, void (*notify)(Spfd *, void *), void *aux) { Spfd *spfd; //fprintf(stderr, "spfd_add fd %d\n", fd); spfd = sp_malloc(sizeof(*spfd)); if (!spfd) return NULL; fcntl(fd, F_SETFL, O_NONBLOCK); spfd->fd = fd; spfd->flags = POLLIN; spfd->aux = aux; spfd->notify = notify; spfd->pfd = NULL; spfd->next = NULL; spfd->next = ptbl.pend_spfds; ptbl.pend_spfds = spfd; ptbl.flags |= TblModified; return spfd; }
static inline spipage* sp_ipagealloc(spi *i) { spipage *p = sp_malloc(i->a, sp_ipagesize(i)); if (spunlikely(p == NULL)) return NULL; p->count = 0; return p; }
char *sp_memdup(sp *s, void *src, size_t size) { char *v = sp_malloc(&s->a, size); if (spunlikely(v == NULL)) return NULL; memcpy(v, src, size); return v; }
spv *sp_vdup(sp *s, spv *v) { spv *vn = sp_malloc(&s->a, sizeof(spv) + v->size); if (spunlikely(vn == NULL)) return NULL; memcpy(vn, v, sizeof(spv) + v->size); return vn; }
sppage *sp_pagenew(sp *s, spepoch *e) { sppage *page = sp_malloc(&s->a, sizeof(sppage)); if (spunlikely(page == NULL)) return NULL; memset(page, 0, sizeof(sppage)); page->epoch = e; sp_listinit(&page->link); return page; }
Xfilepipe * pip_create(int direction) { int pip[2]; Xfilepipe *p; p = sp_malloc(sizeof(*p)); if (!p) return NULL; p->err = 0; p->direction = direction; p->bufsize = 1024; p->buf = sp_malloc(p->bufsize); if (!p->buf) { free(p); return NULL; } p->buflen = 0; if (pipe(pip) < 0) { sp_uerror(errno); free(p->buf); free(p); return NULL; } if (direction == Read) { p->lfd = pip[0]; p->rfd = pip[1]; } else { p->lfd = pip[1]; p->rfd = pip[0]; } fcntl(p->lfd, F_SETFD, FD_CLOEXEC); p->reqs = NULL; p->lspfd = spfd_add(p->lfd, pip_notify, p); // fprintf(stderr, "pip_create %p lfd %d rfd %d\n", p, p->lfd, p->rfd); return p; }
int sp_catinit(spcat *c, spa *a, int top, spcmpf cmp, void *cmparg) { c->a = a; c->cmp = cmp; c->cmparg = cmparg; c->count = 0; c->top = top; c->i = sp_malloc(a, sizeof(sppage*) * top); if (spunlikely(c->i == NULL)) return -1; return 0; }
static inline spv* sp_vnewof(sp *s, void *k, uint16_t size, int reserve) { spv *v = sp_malloc(&s->a, sizeof(spv) + size + reserve); if (spunlikely(v == NULL)) return NULL; v->epoch = 0; v->size = size; v->flags = 0; memcpy(v->key, k, size); return v; }
int spc_rpcnb(Spcfsys *fs, Spfcall *tc, void (*cb)(void *, Spfcall *), void *cba) { Spcreq *req; if (!fs->spfd) { sp_werror("disconnected", ECONNRESET); goto error; } if (fs->ename) { sp_werror(fs->ename, fs->ecode); goto error; } req = sp_malloc(sizeof(*req)); if (!req) goto error; if (tc->type != Tversion) { tc->tag = spc_get_id(fs->tagpool); if (tc->tag == NOTAG) { free(req); sp_werror("tag pool full", EIO); goto error; } sp_set_tag(tc, tc->tag); } req->tag = tc->tag; req->tc = tc; req->rc = NULL; req->cb = cb; req->cba = cba; req->fs = fs; req->flushed = 0; req->next = NULL; if (fs->pend_last) fs->pend_last->next = req; else fs->pend_first = req; fs->pend_last = req; if (!fs->pend_first->next && spfd_can_write(fs->spfd)) spc_fd_write(fs); return 0; error: (*cb)(cba, NULL); return -1; }
static Spfcall * spc_fcall_alloc(u32 msize) { Spfcall *fc; fc = sp_malloc(sizeof(*fc) + msize); if (!fc) return NULL; fc->pkt = (u8*) fc + sizeof(*fc); fc->size = msize; return fc; }
int xp_defaultuser(Spuser **puser, Xkey **pkey) { Spuser *adminuser = NULL; struct passwd *xcpu_admin; Xkey *adminkey = NULL; if (puser) *puser = NULL; if (*pkey) adminkey = *pkey; else adminkey = xauth_privkey_create("/etc/xcpu/admin_key"); if (adminkey) { adminuser = sp_malloc(sizeof(*adminuser)); if (!adminuser) goto error; adminuser->uname = strdup("xcpu-admin"); xcpu_admin = getpwnam("xcpu-admin"); if (xcpu_admin) adminuser->uid = xcpu_admin->pw_uid; else adminuser->uid = 65530; } else { adminuser = sp_unix_users->uid2user(sp_unix_users, geteuid()); if (!adminuser) goto error; adminkey = xauth_user_privkey(); if (!adminkey) goto error; } if (puser) *puser = adminuser; if (pkey) *pkey = adminkey; return 0; error: if (adminuser) free(adminuser); if (adminkey) xauth_destroy(adminkey); return -1; }
Spcfsys * spc_create_fsys(int fd, int msize) { Spcfsys *fs; fs = sp_malloc(sizeof(*fs)); if (!fs) return NULL; fs->fd = fd; fs->spfd = NULL; fs->dotu = 0; fs->msize = msize; fs->root = NULL; fs->afid = NULL; fs->tagpool = NULL; fs->fidpool = NULL; fs->ifcall = NULL; fs->pend_pos = 0; fs->pend_first = NULL; fs->pend_last = NULL; fs->sent_reqs = NULL; fs->ename = NULL; fs->ecode = 0; fs->in_notify = 0; fs->destroyed = 0; fs->laddr = NULL; fs->raddr = NULL; fs->spfd = spfd_add(fd, spc_notify, fs); if (!fs->spfd) goto error; fs->tagpool = spc_create_pool(NOTAG); if (!fs->tagpool) goto error; fs->fidpool = spc_create_pool(NOFID); if (!fs->fidpool) goto error; return fs; error: spc_disconnect_fsys(fs); return NULL; }
int pip_addreq(Xfilepipe* p, Spreq *req) { Spfcall *rc; Xpipereq *r, *preq, *ppreq; // fprintf(stderr, "pip_addreq pip %p fid %d req %p\n", p, req->tcall->fid, req); // if (p->err) // return 0; r = sp_malloc(sizeof(*preq)); if (!r) return 0; r->pip = p; r->req = req; r->fid = req->fid; r->next = NULL; for(ppreq = NULL, preq = p->reqs; preq != NULL; ppreq = preq, preq = preq->next) if (preq->fid->fid > r->fid->fid) break; if (!ppreq) p->reqs = r; else ppreq->next = r; r->next = preq; if (p->lspfd) pip_notify(p->lspfd, p); else if (p->direction == Read) { if (!p->buflen) { rc = sp_create_rread(0, NULL); sp_respond(req, rc); } else pip_read_buf(p); } else { sp_uerror(EPIPE); return 0; } return 1; }
int xauth_startauth(Spfid *afid, char *aname, Spqid *aqid) { Xauth *auth; auth = sp_malloc(sizeof(*auth)); if (!auth) return 0; sp_user_incref(afid->user); auth->user = afid->user; xauth_rand((u8 *) auth->authid, sizeof(auth->authid)); // snprintf(auth->authid, sizeof(auth->authid), "%d", nextid); auth->resplen = 0; memset(auth->response, 0, sizeof(auth->response)); afid->aux = auth; aqid->type = Qtauth; aqid->version = 0; aqid->path = 0; return 1; }
int pkey_gen(Spuser *user, char *buf, int buflen) { int i; char b[16]; Xkey *key; Passkey *p; key = user->aux; p = sp_malloc(sizeof(*p)); if (!p) return -1; sp_user_incref(user); p->user = user; xauth_rand((u8 *) b, sizeof(b)); for(i = 0; i < sizeof(b); i++) sprintf((char *) &p->passkey[i*2], "%02x", b[i]); p->next = passkeys; passkeys = p; return xauth_pubkey_encrypt(p->passkey, sizeof(b)*2, (u8 *) buf, buflen, key); }
int xp_file_create_from_file(Xpfile **xpf, Spcfsys *fs, char *name, char *path) { int n; char *tn, *fn; struct stat st; DIR *d; struct dirent *de; Xpfile *f; if (stat(path, &st) < 0) { sp_uerror(errno); return -1; } f = sp_malloc(sizeof(*f)); if (!f) return -1; f->perm = st.st_mode & 0777; if (S_ISDIR(st.st_mode)) f->perm |= Dmdir; if (!name) { name = strrchr(path, '/'); if (name) name++; else name = path; } f->fs = fs; f->name = strdup(name); f->path = strdup(path); f->create = 1; f->buf = NULL; f->buflen = 0; f->bufsize = 0; f->next = NULL; f->fd = -1; f->fid = NULL; f->spcfd = NULL; f->pos = 0; if (S_ISDIR(st.st_mode)) { d = opendir(path); if (!d) { sp_uerror(errno); goto error; } while ((de = readdir(d)) != NULL) { if (de->d_name[0] == '.' && (de->d_name[1] == '.' || de->d_name[1] == '\0')) continue; fn = malloc(strlen(path) + strlen(de->d_name) + 2); tn = malloc(strlen(f->name) + strlen(de->d_name) + 2); if (!fn || !tn) { free(fn); free(tn); sp_werror(Enomem, ENOMEM); goto error; } sprintf(fn, "%s/%s", path, de->d_name); sprintf(tn, "%s/%s", f->name, de->d_name); n = xp_file_create_from_file(&f->next, fs, tn, fn); free(fn); free(tn); if (n < 0) goto error; } closedir(d); d = NULL; } if (*xpf) while (*xpf != NULL) xpf = &(*xpf)->next; *xpf = f; return 0; error: if (d) closedir(d); xp_file_destroy_all(f); return -1; }