1*12883Ssam /* sys_generic.c 5.37 83/06/02 */ 27423Sroot 37423Sroot #include "../h/param.h" 47423Sroot #include "../h/systm.h" 57423Sroot #include "../h/dir.h" 67423Sroot #include "../h/user.h" 79560Ssam #include "../h/ioctl.h" 87423Sroot #include "../h/file.h" 97423Sroot #include "../h/proc.h" 107714Sroot #include "../h/uio.h" 1112751Ssam #include "../h/kernel.h" 1212751Ssam #include "../h/stat.h" 137423Sroot 147423Sroot /* 157423Sroot * Read system call. 167423Sroot */ 177423Sroot read() 187423Sroot { 197423Sroot register struct a { 207423Sroot int fdes; 217423Sroot char *cbuf; 227423Sroot unsigned count; 237820Sroot } *uap = (struct a *)u.u_ap; 247746Sroot struct uio auio; 257746Sroot struct iovec aiov; 267423Sroot 277820Sroot aiov.iov_base = (caddr_t)uap->cbuf; 287820Sroot aiov.iov_len = uap->count; 297820Sroot auio.uio_iov = &aiov; 307820Sroot auio.uio_iovcnt = 1; 317820Sroot rwuio(&auio, UIO_READ); 327820Sroot } 337820Sroot 347820Sroot readv() 357820Sroot { 367820Sroot register struct a { 377820Sroot int fdes; 387820Sroot struct iovec *iovp; 397820Sroot int iovcnt; 407820Sroot } *uap = (struct a *)u.u_ap; 417820Sroot struct uio auio; 427820Sroot struct iovec aiov[16]; /* XXX */ 437820Sroot 447820Sroot if (uap->iovcnt <= 0 || uap->iovcnt > sizeof(aiov)/sizeof(aiov[0])) { 457423Sroot u.u_error = EINVAL; 467423Sroot return; 477423Sroot } 487820Sroot auio.uio_iov = aiov; 497820Sroot auio.uio_iovcnt = uap->iovcnt; 509999Ssam u.u_error = copyin((caddr_t)uap->iovp, (caddr_t)aiov, 519999Ssam (unsigned)(uap->iovcnt * sizeof (struct iovec))); 529999Ssam if (u.u_error) 537423Sroot return; 547820Sroot rwuio(&auio, UIO_READ); 557423Sroot } 567423Sroot 577423Sroot /* 587423Sroot * Write system call 597423Sroot */ 607423Sroot write() 617423Sroot { 627423Sroot register struct a { 637423Sroot int fdes; 647423Sroot char *cbuf; 657820Sroot int count; 667820Sroot } *uap = (struct a *)u.u_ap; 677820Sroot struct uio auio; 687820Sroot struct iovec aiov; 697423Sroot 707820Sroot auio.uio_iov = &aiov; 717820Sroot auio.uio_iovcnt = 1; 727820Sroot aiov.iov_base = uap->cbuf; 737820Sroot aiov.iov_len = uap->count; 747820Sroot rwuio(&auio, UIO_WRITE); 757820Sroot } 767820Sroot 777820Sroot writev() 787820Sroot { 797820Sroot register struct a { 807820Sroot int fdes; 817820Sroot struct iovec *iovp; 827820Sroot int iovcnt; 837820Sroot } *uap = (struct a *)u.u_ap; 847820Sroot struct uio auio; 857820Sroot struct iovec aiov[16]; /* XXX */ 867820Sroot 877820Sroot if (uap->iovcnt <= 0 || uap->iovcnt > sizeof(aiov)/sizeof(aiov[0])) { 887423Sroot u.u_error = EINVAL; 897423Sroot return; 907423Sroot } 917820Sroot auio.uio_iov = aiov; 927820Sroot auio.uio_iovcnt = uap->iovcnt; 939999Ssam u.u_error = copyin((caddr_t)uap->iovp, (caddr_t)aiov, 949999Ssam (unsigned)(uap->iovcnt * sizeof (struct iovec))); 959999Ssam if (u.u_error) 967820Sroot return; 977820Sroot rwuio(&auio, UIO_WRITE); 987820Sroot } 997820Sroot 1007820Sroot rwuio(uio, rw) 1017820Sroot register struct uio *uio; 1027820Sroot enum uio_rw rw; 1037820Sroot { 1047820Sroot struct a { 1057820Sroot int fdes; 1067820Sroot }; 1077820Sroot register struct file *fp; 1087820Sroot register struct iovec *iov; 1097820Sroot int i, count; 1107820Sroot 1117820Sroot GETF(fp, ((struct a *)u.u_ap)->fdes); 1127820Sroot if ((fp->f_flag&(rw==UIO_READ ? FREAD : FWRITE)) == 0) { 1137423Sroot u.u_error = EBADF; 1147423Sroot return; 1157423Sroot } 1167820Sroot uio->uio_resid = 0; 1177820Sroot uio->uio_segflg = 0; 1187820Sroot iov = uio->uio_iov; 1197820Sroot for (i = 0; i < uio->uio_iovcnt; i++) { 1207820Sroot if (iov->iov_len < 0) { 1217820Sroot u.u_error = EINVAL; 1227820Sroot return; 1237820Sroot } 1247820Sroot uio->uio_resid += iov->iov_len; 1257820Sroot if (uio->uio_resid < 0) { 1267820Sroot u.u_error = EINVAL; 1277820Sroot return; 1287820Sroot } 1297820Sroot } 1307820Sroot count = uio->uio_resid; 13112751Ssam uio->uio_offset = fp->f_offset; 132*12883Ssam if ((u.u_procp->p_flag&SOUSIG) == 0 && setjmp(&u.u_qsave)) { 1337820Sroot if (uio->uio_resid == count) 1347423Sroot u.u_eosys = RESTARTSYS; 13512751Ssam } else 13612751Ssam u.u_error = (*fp->f_ops->fo_rw)(fp, rw, uio); 1377820Sroot u.u_r.r_val1 = count - uio->uio_resid; 13812751Ssam fp->f_offset += u.u_r.r_val1; 1397423Sroot } 1407423Sroot 1417423Sroot /* 1427423Sroot * Ioctl system call 1437423Sroot */ 1447423Sroot ioctl() 1457423Sroot { 1467423Sroot register struct file *fp; 1477624Ssam struct a { 1487423Sroot int fdes; 1497423Sroot int cmd; 1507423Sroot caddr_t cmarg; 1517423Sroot } *uap; 1527820Sroot register int com; 1537820Sroot register u_int size; 1547624Ssam char data[IOCPARM_MASK+1]; 1557423Sroot 1567423Sroot uap = (struct a *)u.u_ap; 1577423Sroot if ((fp = getf(uap->fdes)) == NULL) 1587423Sroot return; 1597423Sroot if ((fp->f_flag & (FREAD|FWRITE)) == 0) { 1607423Sroot u.u_error = EBADF; 1617423Sroot return; 1627423Sroot } 1637624Ssam com = uap->cmd; 1647624Ssam 16512751Ssam #if defined(vax) && !defined(NOCOMPAT) 1667624Ssam /* 1677624Ssam * Map old style ioctl's into new for the 1687624Ssam * sake of backwards compatibility (sigh). 1697624Ssam */ 1707624Ssam if ((com&~0xffff) == 0) { 1717624Ssam com = mapioctl(com); 1727624Ssam if (com == 0) { 1737624Ssam u.u_error = EINVAL; 1747624Ssam return; 1757624Ssam } 1767624Ssam } 1777624Ssam #endif 1787624Ssam if (com == FIOCLEX) { 1799592Ssam u.u_pofile[uap->fdes] |= UF_EXCLOSE; 1807423Sroot return; 1817423Sroot } 1827624Ssam if (com == FIONCLEX) { 1839592Ssam u.u_pofile[uap->fdes] &= ~UF_EXCLOSE; 1847423Sroot return; 1857423Sroot } 1867624Ssam 1877624Ssam /* 1887624Ssam * Interpret high order word to find 1897624Ssam * amount of data to be copied to/from the 1907624Ssam * user's address space. 1917624Ssam */ 1927624Ssam size = (com &~ (IOC_INOUT|IOC_VOID)) >> 16; 1937624Ssam if (size > sizeof (data)) { 1947624Ssam u.u_error = EFAULT; 1957423Sroot return; 1967423Sroot } 19710601Ssam if (com&IOC_IN) { 19810601Ssam if (size) { 19910601Ssam u.u_error = 20010601Ssam copyin(uap->cmarg, (caddr_t)data, (u_int)size); 20110601Ssam if (u.u_error) 20210601Ssam return; 20310601Ssam } else 20410601Ssam *(caddr_t *)data = uap->cmarg; 20510601Ssam } else if ((com&IOC_OUT) && size) 20610601Ssam /* 20710601Ssam * Zero the buffer on the stack so the user 20810601Ssam * always gets back something deterministic. 20910601Ssam */ 2107624Ssam bzero((caddr_t)data, size); 21111284Ssam else if (com&IOC_VOID) 21211284Ssam *(caddr_t *)data = uap->cmarg; 2137423Sroot 21412751Ssam switch (com) { 2157624Ssam 21612751Ssam case FIONBIO: 21712751Ssam u.u_error = fset(fp, FNDELAY, *(int *)data); 21812751Ssam return; 21912751Ssam 22012751Ssam case FIOASYNC: 22112751Ssam u.u_error = fset(fp, FASYNC, *(int *)data); 22212751Ssam return; 22312751Ssam 22412751Ssam case FIOSETOWN: 22512751Ssam u.u_error = fsetown(fp, *(int *)data); 22612751Ssam return; 22712751Ssam 22812751Ssam case FIOGETOWN: 22912751Ssam u.u_error = fgetown(fp, (int *)data); 23012751Ssam return; 2317423Sroot } 23212751Ssam u.u_error = (*fp->f_ops->fo_ioctl)(fp, com, data); 2337624Ssam /* 2347624Ssam * Copy any data to user, size was 2357624Ssam * already set and checked above. 2367624Ssam */ 2379999Ssam if (u.u_error == 0 && (com&IOC_OUT) && size) 2389999Ssam u.u_error = copyout(data, uap->cmarg, (u_int)size); 2397423Sroot } 2407423Sroot 24112751Ssam int unselect(); 24212751Ssam int nselcoll; 2437423Sroot /* 24412751Ssam * Select system call. 2457423Sroot */ 24612751Ssam select() 24712751Ssam { 24812751Ssam register struct uap { 24912751Ssam int nd; 25012751Ssam long *in, *ou, *ex; 25112751Ssam struct timeval *tv; 25212751Ssam } *uap = (struct uap *)u.u_ap; 25312751Ssam int ibits[3], obits[3]; 25412751Ssam struct timeval atv; 25512751Ssam int s, ncoll; 25612751Ssam label_t lqsave; 25712751Ssam 25812751Ssam obits[0] = obits[1] = obits[2] = 0; 25912751Ssam if (uap->nd > NOFILE) 26012751Ssam uap->nd = NOFILE; /* forgiving, if slightly wrong */ 26112751Ssam 26212751Ssam #define getbits(name, x) \ 26312751Ssam if (uap->name) { \ 26412751Ssam u.u_error = copyin((caddr_t)uap->name, (caddr_t)&ibits[x], \ 26512751Ssam sizeof (ibits[x])); \ 26612751Ssam if (u.u_error) \ 26712751Ssam goto done; \ 26812751Ssam } else \ 26912751Ssam ibits[x] = 0; 27012751Ssam getbits(in, 0); 27112751Ssam getbits(ou, 1); 27212751Ssam getbits(ex, 2); 27312751Ssam #undef getbits 27412751Ssam 27512751Ssam if (uap->tv) { 27612751Ssam u.u_error = copyin((caddr_t)uap->tv, (caddr_t)&atv, 27712751Ssam sizeof (atv)); 27812751Ssam if (u.u_error) 27912751Ssam goto done; 28012751Ssam if (itimerfix(&atv)) { 28112751Ssam u.u_error = EINVAL; 28212751Ssam goto done; 28312751Ssam } 28412751Ssam s = spl7(); timevaladd(&atv, &time); splx(s); 28512751Ssam } 28612751Ssam retry: 28712751Ssam ncoll = nselcoll; 28812751Ssam u.u_procp->p_flag |= SSEL; 28912751Ssam u.u_r.r_val1 = selscan(ibits, obits); 29012751Ssam if (u.u_error || u.u_r.r_val1) 29112751Ssam goto done; 29212751Ssam s = spl6(); 29312751Ssam if (uap->tv && timercmp(&time, &atv, >=)) { 29412751Ssam splx(s); 29512751Ssam goto done; 29612751Ssam } 29712751Ssam if ((u.u_procp->p_flag & SSEL) == 0 || nselcoll != ncoll) { 29812751Ssam u.u_procp->p_flag &= ~SSEL; 29912751Ssam splx(s); 30012751Ssam goto retry; 30112751Ssam } 30212751Ssam u.u_procp->p_flag &= ~SSEL; 30312751Ssam if (uap->tv) { 30412751Ssam lqsave = u.u_qsave; 30512751Ssam if (setjmp(&u.u_qsave)) { 30612751Ssam untimeout(unselect, (caddr_t)u.u_procp); 30712751Ssam u.u_error = EINTR; 30812751Ssam splx(s); 30912751Ssam goto done; 31012751Ssam } 31112751Ssam timeout(unselect, (caddr_t)u.u_procp, hzto(&atv)); 31212751Ssam } 31312751Ssam sleep((caddr_t)&selwait, PZERO+1); 31412751Ssam if (uap->tv) { 31512751Ssam u.u_qsave = lqsave; 31612751Ssam untimeout(unselect, (caddr_t)u.u_procp); 31712751Ssam } 31812751Ssam splx(s); 31912751Ssam goto retry; 32012751Ssam done: 32112751Ssam #define putbits(name, x) \ 32212751Ssam if (uap->name) { \ 32312751Ssam int error = copyout((caddr_t)&obits[x], (caddr_t)uap->name, \ 32412751Ssam sizeof (obits[x])); \ 32512751Ssam if (error) \ 32612751Ssam u.u_error = error; \ 32712751Ssam } 32812751Ssam putbits(in, 0); 32912751Ssam putbits(ou, 1); 33012751Ssam putbits(ex, 2); 33112751Ssam #undef putbits 33212751Ssam } 33312751Ssam 33412751Ssam unselect(p) 33512751Ssam register struct proc *p; 33612751Ssam { 33712751Ssam register int s = spl6(); 33812751Ssam 33912751Ssam switch (p->p_stat) { 34012751Ssam 34112751Ssam case SSLEEP: 34212751Ssam setrun(p); 34312751Ssam break; 34412751Ssam 34512751Ssam case SSTOP: 34612751Ssam unsleep(p); 34712751Ssam break; 34812751Ssam } 34912751Ssam splx(s); 35012751Ssam } 35112751Ssam 35212751Ssam selscan(ibits, obits) 35312751Ssam int *ibits, *obits; 35412751Ssam { 35512751Ssam register int which, bits, i; 35612751Ssam int flag; 35712751Ssam struct file *fp; 35812751Ssam int n = 0; 35912751Ssam 36012751Ssam for (which = 0; which < 3; which++) { 36112751Ssam bits = ibits[which]; 36212751Ssam obits[which] = 0; 36312751Ssam switch (which) { 36412751Ssam 36512751Ssam case 0: 36612751Ssam flag = FREAD; break; 36712751Ssam 36812751Ssam case 1: 36912751Ssam flag = FWRITE; break; 37012751Ssam 37112751Ssam case 2: 37212751Ssam flag = 0; break; 37312751Ssam } 37412751Ssam while (i = ffs(bits)) { 37512751Ssam bits &= ~(1<<(i-1)); 37612751Ssam fp = u.u_ofile[i-1]; 37712751Ssam if (fp == NULL) { 37812751Ssam u.u_error = EBADF; 37912751Ssam break; 38012751Ssam } 38112751Ssam if ((*fp->f_ops->fo_select)(fp, flag)) { 38212751Ssam obits[which] |= (1<<(i-1)); 38312751Ssam n++; 38412751Ssam } 38512751Ssam } 38612751Ssam } 38712751Ssam return (n); 38812751Ssam } 38912751Ssam 3907423Sroot /*ARGSUSED*/ 39112751Ssam seltrue(dev, flag) 39212751Ssam dev_t dev; 39312751Ssam int flag; 3947423Sroot { 3957423Sroot 39612751Ssam return (1); 3977423Sroot } 3988103Sroot 39912751Ssam selwakeup(p, coll) 40012751Ssam register struct proc *p; 40112751Ssam int coll; 4028103Sroot { 4038103Sroot 40412751Ssam if (coll) { 40512751Ssam nselcoll++; 40612751Ssam wakeup((caddr_t)&selwait); 40712751Ssam } 40812751Ssam if (p) { 40912751Ssam int s = spl6(); 41012751Ssam if (p->p_wchan == (caddr_t)&selwait) 41112751Ssam setrun(p); 41212751Ssam else if (p->p_flag & SSEL) 41312751Ssam p->p_flag &= ~SSEL; 41412751Ssam splx(s); 41512751Ssam } 4168103Sroot } 4178103Sroot 41812751Ssam fstat() 4198103Sroot { 42012751Ssam register struct file *fp; 42112751Ssam register struct a { 42212751Ssam int fdes; 42312751Ssam struct stat *sb; 42412751Ssam } *uap; 42512751Ssam struct stat ub; 4268103Sroot 42712751Ssam uap = (struct a *)u.u_ap; 42812751Ssam fp = getf(uap->fdes); 42912751Ssam if (fp == 0) 43012751Ssam return; 43112751Ssam u.u_error = (*fp->f_ops->fo_stat)(fp, &ub); 43212751Ssam if (u.u_error == 0) 43312751Ssam u.u_error = copyout(&ub, uap->sb, sizeof (ub)); 4348103Sroot } 435