int tcpconnect(const ipaddr *addr, int64_t deadline) { int err; /* Open a socket. */ int s = socket(ipfamily(addr), SOCK_STREAM, 0); if(dill_slow(s < 0)) return -1; tcptune(s); /* Connect to the remote endpoint. */ int rc = dsconnect(s, ipsockaddr(addr), iplen(addr), deadline); if(dill_slow(rc < 0)) return -1; /* Create the object. */ struct tcpconn *conn = tcpconn_create(); if(dill_slow(!conn)) {err = errno; goto error1;} conn->fd = s; conn->addr = *addr; /* Bind the object to a sock handle. */ int bs = bsock(tcpconn_type, conn, &tcpconn_vfptrs); if(dill_slow(bs < 0)) {err = errno; goto error2;} return bs; error2: tcpconn_destroy(conn); error1: rc = dsclose(s); dill_assert(rc == 0); errno = err; return -1; }
static int usalo_close(SCSI *usalp) { #ifndef USE_DSLIB register int f; register int b; register int t; register int l; if (usalp->local == NULL) return (-1); for (b = 0; b < MAX_SCG; b++) { for (t = 0; t < MAX_TGT; t++) { for (l = 0; l < MAX_LUN; l++) { f = usallocal(usalp)->usalfiles[b][t][l]; if (f >= 0) close(f); usallocal(usalp)->usalfiles[b][t][l] = (short)-1; } } } #else dsclose(dsp); #endif return (0); }
static void tcplistener_close(int s) { struct tcplistener *lst = hdata(s, tcplistener_type); dill_assert(lst); int rc = dsclose(lst->fd); dill_assert(rc == 0); free(lst); }
int tcpaccept(int s, int64_t deadline) { int err; struct tcplistener *lst = hdata(s, tcplistener_type); if(dill_slow(!lst)) return -1; /* Try to get new connection in a non-blocking way. */ ipaddr addr; socklen_t addrlen; int as = dsaccept(lst->fd, (struct sockaddr*)&addr, &addrlen, deadline); if(dill_slow(as < 0)) return -1; tcptune(as); /* Create the object. */ struct tcpconn *conn = tcpconn_create(); if(dill_slow(!conn)) {err = errno; goto error1;} conn->fd = as; conn->addr = addr; /* Bind the object to a handle. */ int hndl = bsock(tcpconn_type, conn, &tcpconn_vfptrs); if(dill_slow(hndl < 0)) {err = errno; goto error2;} return hndl; error2: tcpconn_destroy(conn); error1:; int rc = dsclose(s); dill_assert(rc == 0); errno = err; return -1; }
/* ARGSUSED */ int idclose(dev_t dev, int flags, int fmt, struct proc *p) { struct ida_drv *drv; int part = dkpart(dev); int unit = dkunit(dev); if (unit >= NID || part >= MAXPARTITIONS) /* bounds check */ return(ENXIO); drv = id_drive[unit]; dsclose(dev, fmt, drv->slices); if (!dsisopen(drv->slices)) { drv->flags &= ~ID_DEV_OPEN; } return 0; }
static int tcpconn_finish(int s, int64_t deadline) { int rc; int err = 0; struct tcpconn *conn = bsockdata(s, tcpconn_type); if(dill_slow(!conn)) return -1; /* First, let's try to flush remaining outbound data. */ if(deadline != 0) { rc = tcpconn_flush(s, deadline); if(dill_slow(rc < 0)) err = errno; } /* Close the underlying socket. */ rc = dsclose(conn->fd); dill_assert(rc == 0); /* Deallocate the object. */ tcpconn_destroy(conn); errno = err; return err ? -1 : 0; }
int tcplisten(const ipaddr *addr, int backlog) { int err; if(dill_slow(backlog < 0)) {errno = EINVAL; return -1;} /* Open listening socket. */ int s = socket(ipfamily(addr), SOCK_STREAM, 0); if(dill_slow(s < 0)) return -1; tcptune(s); /* Start listening. */ int rc = bind(s, ipsockaddr(addr), iplen(addr)); if(dill_slow(rc != 0)) return -1; rc = listen(s, backlog); if(dill_slow(rc != 0)) return -1; /* If the user requested an ephemeral port, retrieve the port number assigned by the OS now. */ int port = ipport(addr); if(port == 0) { ipaddr baddr; socklen_t len = sizeof(ipaddr); rc = getsockname(s, (struct sockaddr*)&baddr, &len); if(dill_slow(rc < 0)) {err = errno; goto error1;} port = ipport(&baddr); } /* Create the object. */ struct tcplistener *lst = malloc(sizeof(struct tcplistener)); if(dill_slow(!lst)) {errno = ENOMEM; goto error1;} lst->fd = s; lst->port = port; /* Bind the object to a handle. */ int h = handle(tcplistener_type, lst, &tcplistener_vfptrs); if(dill_slow(h < 0)) {err = errno; goto error2;} return h; error2: free(lst); error1: rc = dsclose(s); dill_assert(rc == 0); errno = err; return -1; }