sys_sunos.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /* $Id: sys_sunos.c,v 1.137 2002/02/08 03:57:14 davem Exp $
  2. * sys_sunos.c: SunOS specific syscall compatibility support.
  3. *
  4. * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
  5. * Copyright (C) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx)
  6. *
  7. * Based upon preliminary work which is:
  8. *
  9. * Copyright (C) 1995 Adrian M. Rodriguez (adrian@remus.rutgers.edu)
  10. *
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/types.h>
  15. #include <linux/mman.h>
  16. #include <linux/mm.h>
  17. #include <linux/swap.h>
  18. #include <linux/fs.h>
  19. #include <linux/file.h>
  20. #include <linux/resource.h>
  21. #include <linux/ipc.h>
  22. #include <linux/shm.h>
  23. #include <linux/msg.h>
  24. #include <linux/sem.h>
  25. #include <linux/signal.h>
  26. #include <linux/uio.h>
  27. #include <linux/utsname.h>
  28. #include <linux/major.h>
  29. #include <linux/stat.h>
  30. #include <linux/slab.h>
  31. #include <linux/pagemap.h>
  32. #include <linux/capability.h>
  33. #include <linux/errno.h>
  34. #include <linux/smp.h>
  35. #include <linux/smp_lock.h>
  36. #include <linux/syscalls.h>
  37. #include <net/sock.h>
  38. #include <asm/uaccess.h>
  39. #ifndef KERNEL_DS
  40. #include <linux/segment.h>
  41. #endif
  42. #include <asm/page.h>
  43. #include <asm/pgtable.h>
  44. #include <asm/pconf.h>
  45. #include <asm/idprom.h> /* for gethostid() */
  46. #include <asm/unistd.h>
  47. #include <asm/system.h>
  48. /* For the nfs mount emulation */
  49. #include <linux/socket.h>
  50. #include <linux/in.h>
  51. #include <linux/nfs.h>
  52. #include <linux/nfs2.h>
  53. #include <linux/nfs_mount.h>
  54. /* for sunos_select */
  55. #include <linux/time.h>
  56. #include <linux/personality.h>
  57. /* NR_OPEN is now larger and dynamic in recent kernels. */
  58. #define SUNOS_NR_OPEN 256
  59. /* We use the SunOS mmap() semantics. */
  60. asmlinkage unsigned long sunos_mmap(unsigned long addr, unsigned long len,
  61. unsigned long prot, unsigned long flags,
  62. unsigned long fd, unsigned long off)
  63. {
  64. struct file * file = NULL;
  65. unsigned long retval, ret_type;
  66. if (flags & MAP_NORESERVE) {
  67. static int cnt;
  68. if (cnt++ < 10)
  69. printk("%s: unimplemented SunOS MAP_NORESERVE mmap() flag\n",
  70. current->comm);
  71. flags &= ~MAP_NORESERVE;
  72. }
  73. retval = -EBADF;
  74. if (!(flags & MAP_ANONYMOUS)) {
  75. if (fd >= SUNOS_NR_OPEN)
  76. goto out;
  77. file = fget(fd);
  78. if (!file)
  79. goto out;
  80. }
  81. retval = -EINVAL;
  82. /* If this is ld.so or a shared library doing an mmap
  83. * of /dev/zero, transform it into an anonymous mapping.
  84. * SunOS is so stupid some times... hmph!
  85. */
  86. if (file) {
  87. if (imajor(file->f_dentry->d_inode) == MEM_MAJOR &&
  88. iminor(file->f_dentry->d_inode) == 5) {
  89. flags |= MAP_ANONYMOUS;
  90. fput(file);
  91. file = NULL;
  92. }
  93. }
  94. ret_type = flags & _MAP_NEW;
  95. flags &= ~_MAP_NEW;
  96. if (!(flags & MAP_FIXED))
  97. addr = 0;
  98. else {
  99. if (ARCH_SUN4C_SUN4 &&
  100. (len > 0x20000000 ||
  101. ((flags & MAP_FIXED) &&
  102. addr < 0xe0000000 && addr + len > 0x20000000)))
  103. goto out_putf;
  104. /* See asm-sparc/uaccess.h */
  105. if (len > TASK_SIZE - PAGE_SIZE ||
  106. addr + len > TASK_SIZE - PAGE_SIZE)
  107. goto out_putf;
  108. }
  109. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  110. down_write(&current->mm->mmap_sem);
  111. retval = do_mmap(file, addr, len, prot, flags, off);
  112. up_write(&current->mm->mmap_sem);
  113. if (!ret_type)
  114. retval = ((retval < PAGE_OFFSET) ? 0 : retval);
  115. out_putf:
  116. if (file)
  117. fput(file);
  118. out:
  119. return retval;
  120. }
  121. /* lmbench calls this, just say "yeah, ok" */
  122. asmlinkage int sunos_mctl(unsigned long addr, unsigned long len, int function, char *arg)
  123. {
  124. return 0;
  125. }
  126. /* SunOS is completely broken... it returns 0 on success, otherwise
  127. * ENOMEM. For sys_sbrk() it wants the old brk value as a return
  128. * on success and ENOMEM as before on failure.
  129. */
  130. asmlinkage int sunos_brk(unsigned long brk)
  131. {
  132. int freepages, retval = -ENOMEM;
  133. unsigned long rlim;
  134. unsigned long newbrk, oldbrk;
  135. down_write(&current->mm->mmap_sem);
  136. if (ARCH_SUN4C_SUN4) {
  137. if (brk >= 0x20000000 && brk < 0xe0000000) {
  138. goto out;
  139. }
  140. }
  141. if (brk < current->mm->end_code)
  142. goto out;
  143. newbrk = PAGE_ALIGN(brk);
  144. oldbrk = PAGE_ALIGN(current->mm->brk);
  145. retval = 0;
  146. if (oldbrk == newbrk) {
  147. current->mm->brk = brk;
  148. goto out;
  149. }
  150. /*
  151. * Always allow shrinking brk
  152. */
  153. if (brk <= current->mm->brk) {
  154. current->mm->brk = brk;
  155. do_munmap(current->mm, newbrk, oldbrk-newbrk);
  156. goto out;
  157. }
  158. /*
  159. * Check against rlimit and stack..
  160. */
  161. retval = -ENOMEM;
  162. rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
  163. if (rlim >= RLIM_INFINITY)
  164. rlim = ~0;
  165. if (brk - current->mm->end_code > rlim)
  166. goto out;
  167. /*
  168. * Check against existing mmap mappings.
  169. */
  170. if (find_vma_intersection(current->mm, oldbrk, newbrk+PAGE_SIZE))
  171. goto out;
  172. /*
  173. * stupid algorithm to decide if we have enough memory: while
  174. * simple, it hopefully works in most obvious cases.. Easy to
  175. * fool it, but this should catch most mistakes.
  176. */
  177. freepages = global_page_state(NR_FILE_PAGES);
  178. freepages >>= 1;
  179. freepages += nr_free_pages();
  180. freepages += nr_swap_pages;
  181. freepages -= num_physpages >> 4;
  182. freepages -= (newbrk-oldbrk) >> PAGE_SHIFT;
  183. if (freepages < 0)
  184. goto out;
  185. /*
  186. * Ok, we have probably got enough memory - let it rip.
  187. */
  188. current->mm->brk = brk;
  189. do_brk(oldbrk, newbrk-oldbrk);
  190. retval = 0;
  191. out:
  192. up_write(&current->mm->mmap_sem);
  193. return retval;
  194. }
  195. asmlinkage unsigned long sunos_sbrk(int increment)
  196. {
  197. int error;
  198. unsigned long oldbrk;
  199. /* This should do it hopefully... */
  200. lock_kernel();
  201. oldbrk = current->mm->brk;
  202. error = sunos_brk(((int) current->mm->brk) + increment);
  203. if (!error)
  204. error = oldbrk;
  205. unlock_kernel();
  206. return error;
  207. }
  208. /* XXX Completely undocumented, and completely magic...
  209. * XXX I believe it is to increase the size of the stack by
  210. * XXX argument 'increment' and return the new end of stack
  211. * XXX area. Wheee...
  212. */
  213. asmlinkage unsigned long sunos_sstk(int increment)
  214. {
  215. lock_kernel();
  216. printk("%s: Call to sunos_sstk(increment<%d>) is unsupported\n",
  217. current->comm, increment);
  218. unlock_kernel();
  219. return -1;
  220. }
  221. /* Give hints to the kernel as to what paging strategy to use...
  222. * Completely bogus, don't remind me.
  223. */
  224. #define VA_NORMAL 0 /* Normal vm usage expected */
  225. #define VA_ABNORMAL 1 /* Abnormal/random vm usage probable */
  226. #define VA_SEQUENTIAL 2 /* Accesses will be of a sequential nature */
  227. #define VA_INVALIDATE 3 /* Page table entries should be flushed ??? */
  228. static char *vstrings[] = {
  229. "VA_NORMAL",
  230. "VA_ABNORMAL",
  231. "VA_SEQUENTIAL",
  232. "VA_INVALIDATE",
  233. };
  234. asmlinkage void sunos_vadvise(unsigned long strategy)
  235. {
  236. /* I wanna see who uses this... */
  237. lock_kernel();
  238. printk("%s: Advises us to use %s paging strategy\n",
  239. current->comm,
  240. strategy <= 3 ? vstrings[strategy] : "BOGUS");
  241. unlock_kernel();
  242. }
  243. /* This just wants the soft limit (ie. rlim_cur element) of the RLIMIT_NOFILE
  244. * resource limit and is for backwards compatibility with older sunos
  245. * revs.
  246. */
  247. asmlinkage long sunos_getdtablesize(void)
  248. {
  249. return SUNOS_NR_OPEN;
  250. }
  251. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  252. asmlinkage unsigned long sunos_sigblock(unsigned long blk_mask)
  253. {
  254. unsigned long old;
  255. spin_lock_irq(&current->sighand->siglock);
  256. old = current->blocked.sig[0];
  257. current->blocked.sig[0] |= (blk_mask & _BLOCKABLE);
  258. recalc_sigpending();
  259. spin_unlock_irq(&current->sighand->siglock);
  260. return old;
  261. }
  262. asmlinkage unsigned long sunos_sigsetmask(unsigned long newmask)
  263. {
  264. unsigned long retval;
  265. spin_lock_irq(&current->sighand->siglock);
  266. retval = current->blocked.sig[0];
  267. current->blocked.sig[0] = (newmask & _BLOCKABLE);
  268. recalc_sigpending();
  269. spin_unlock_irq(&current->sighand->siglock);
  270. return retval;
  271. }
  272. /* SunOS getdents is very similar to the newer Linux (iBCS2 compliant) */
  273. /* getdents system call, the format of the structure just has a different */
  274. /* layout (d_off+d_ino instead of d_ino+d_off) */
  275. struct sunos_dirent {
  276. long d_off;
  277. unsigned long d_ino;
  278. unsigned short d_reclen;
  279. unsigned short d_namlen;
  280. char d_name[1];
  281. };
  282. struct sunos_dirent_callback {
  283. struct sunos_dirent __user *curr;
  284. struct sunos_dirent __user *previous;
  285. int count;
  286. int error;
  287. };
  288. #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
  289. #define ROUND_UP(x) (((x)+sizeof(long)-1) & ~(sizeof(long)-1))
  290. static int sunos_filldir(void * __buf, const char * name, int namlen,
  291. loff_t offset, ino_t ino, unsigned int d_type)
  292. {
  293. struct sunos_dirent __user *dirent;
  294. struct sunos_dirent_callback * buf = __buf;
  295. int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 1);
  296. buf->error = -EINVAL; /* only used if we fail.. */
  297. if (reclen > buf->count)
  298. return -EINVAL;
  299. dirent = buf->previous;
  300. if (dirent)
  301. put_user(offset, &dirent->d_off);
  302. dirent = buf->curr;
  303. buf->previous = dirent;
  304. put_user(ino, &dirent->d_ino);
  305. put_user(namlen, &dirent->d_namlen);
  306. put_user(reclen, &dirent->d_reclen);
  307. copy_to_user(dirent->d_name, name, namlen);
  308. put_user(0, dirent->d_name + namlen);
  309. dirent = (void __user *) dirent + reclen;
  310. buf->curr = dirent;
  311. buf->count -= reclen;
  312. return 0;
  313. }
  314. asmlinkage int sunos_getdents(unsigned int fd, void __user *dirent, int cnt)
  315. {
  316. struct file * file;
  317. struct sunos_dirent __user *lastdirent;
  318. struct sunos_dirent_callback buf;
  319. int error = -EBADF;
  320. if (fd >= SUNOS_NR_OPEN)
  321. goto out;
  322. file = fget(fd);
  323. if (!file)
  324. goto out;
  325. error = -EINVAL;
  326. if (cnt < (sizeof(struct sunos_dirent) + 255))
  327. goto out_putf;
  328. buf.curr = (struct sunos_dirent __user *) dirent;
  329. buf.previous = NULL;
  330. buf.count = cnt;
  331. buf.error = 0;
  332. error = vfs_readdir(file, sunos_filldir, &buf);
  333. if (error < 0)
  334. goto out_putf;
  335. lastdirent = buf.previous;
  336. error = buf.error;
  337. if (lastdirent) {
  338. put_user(file->f_pos, &lastdirent->d_off);
  339. error = cnt - buf.count;
  340. }
  341. out_putf:
  342. fput(file);
  343. out:
  344. return error;
  345. }
  346. /* Old sunos getdirentries, severely broken compatibility stuff here. */
  347. struct sunos_direntry {
  348. unsigned long d_ino;
  349. unsigned short d_reclen;
  350. unsigned short d_namlen;
  351. char d_name[1];
  352. };
  353. struct sunos_direntry_callback {
  354. struct sunos_direntry __user *curr;
  355. struct sunos_direntry __user *previous;
  356. int count;
  357. int error;
  358. };
  359. static int sunos_filldirentry(void * __buf, const char * name, int namlen,
  360. loff_t offset, ino_t ino, unsigned int d_type)
  361. {
  362. struct sunos_direntry __user *dirent;
  363. struct sunos_direntry_callback *buf = __buf;
  364. int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 1);
  365. buf->error = -EINVAL; /* only used if we fail.. */
  366. if (reclen > buf->count)
  367. return -EINVAL;
  368. dirent = buf->previous;
  369. dirent = buf->curr;
  370. buf->previous = dirent;
  371. put_user(ino, &dirent->d_ino);
  372. put_user(namlen, &dirent->d_namlen);
  373. put_user(reclen, &dirent->d_reclen);
  374. copy_to_user(dirent->d_name, name, namlen);
  375. put_user(0, dirent->d_name + namlen);
  376. dirent = (void __user *) dirent + reclen;
  377. buf->curr = dirent;
  378. buf->count -= reclen;
  379. return 0;
  380. }
  381. asmlinkage int sunos_getdirentries(unsigned int fd, void __user *dirent,
  382. int cnt, unsigned int __user *basep)
  383. {
  384. struct file * file;
  385. struct sunos_direntry __user *lastdirent;
  386. struct sunos_direntry_callback buf;
  387. int error = -EBADF;
  388. if (fd >= SUNOS_NR_OPEN)
  389. goto out;
  390. file = fget(fd);
  391. if (!file)
  392. goto out;
  393. error = -EINVAL;
  394. if (cnt < (sizeof(struct sunos_direntry) + 255))
  395. goto out_putf;
  396. buf.curr = (struct sunos_direntry __user *) dirent;
  397. buf.previous = NULL;
  398. buf.count = cnt;
  399. buf.error = 0;
  400. error = vfs_readdir(file, sunos_filldirentry, &buf);
  401. if (error < 0)
  402. goto out_putf;
  403. lastdirent = buf.previous;
  404. error = buf.error;
  405. if (lastdirent) {
  406. put_user(file->f_pos, basep);
  407. error = cnt - buf.count;
  408. }
  409. out_putf:
  410. fput(file);
  411. out:
  412. return error;
  413. }
  414. struct sunos_utsname {
  415. char sname[9];
  416. char nname[9];
  417. char nnext[56];
  418. char rel[9];
  419. char ver[9];
  420. char mach[9];
  421. };
  422. asmlinkage int sunos_uname(struct sunos_utsname __user *name)
  423. {
  424. int ret;
  425. down_read(&uts_sem);
  426. ret = copy_to_user(&name->sname[0], &system_utsname.sysname[0], sizeof(name->sname) - 1);
  427. if (!ret) {
  428. ret |= __copy_to_user(&name->nname[0], &system_utsname.nodename[0], sizeof(name->nname) - 1);
  429. ret |= __put_user('\0', &name->nname[8]);
  430. ret |= __copy_to_user(&name->rel[0], &system_utsname.release[0], sizeof(name->rel) - 1);
  431. ret |= __copy_to_user(&name->ver[0], &system_utsname.version[0], sizeof(name->ver) - 1);
  432. ret |= __copy_to_user(&name->mach[0], &system_utsname.machine[0], sizeof(name->mach) - 1);
  433. }
  434. up_read(&uts_sem);
  435. return ret ? -EFAULT : 0;
  436. }
  437. asmlinkage int sunos_nosys(void)
  438. {
  439. struct pt_regs *regs;
  440. siginfo_t info;
  441. static int cnt;
  442. lock_kernel();
  443. regs = current->thread.kregs;
  444. info.si_signo = SIGSYS;
  445. info.si_errno = 0;
  446. info.si_code = __SI_FAULT|0x100;
  447. info.si_addr = (void __user *)regs->pc;
  448. info.si_trapno = regs->u_regs[UREG_G1];
  449. send_sig_info(SIGSYS, &info, current);
  450. if (cnt++ < 4) {
  451. printk("Process makes ni_syscall number %d, register dump:\n",
  452. (int) regs->u_regs[UREG_G1]);
  453. show_regs(regs);
  454. }
  455. unlock_kernel();
  456. return -ENOSYS;
  457. }
  458. /* This is not a real and complete implementation yet, just to keep
  459. * the easy SunOS binaries happy.
  460. */
  461. asmlinkage int sunos_fpathconf(int fd, int name)
  462. {
  463. int ret;
  464. switch(name) {
  465. case _PCONF_LINK:
  466. ret = LINK_MAX;
  467. break;
  468. case _PCONF_CANON:
  469. ret = MAX_CANON;
  470. break;
  471. case _PCONF_INPUT:
  472. ret = MAX_INPUT;
  473. break;
  474. case _PCONF_NAME:
  475. ret = NAME_MAX;
  476. break;
  477. case _PCONF_PATH:
  478. ret = PATH_MAX;
  479. break;
  480. case _PCONF_PIPE:
  481. ret = PIPE_BUF;
  482. break;
  483. case _PCONF_CHRESTRICT: /* XXX Investigate XXX */
  484. ret = 1;
  485. break;
  486. case _PCONF_NOTRUNC: /* XXX Investigate XXX */
  487. case _PCONF_VDISABLE:
  488. ret = 0;
  489. break;
  490. default:
  491. ret = -EINVAL;
  492. break;
  493. }
  494. return ret;
  495. }
  496. asmlinkage int sunos_pathconf(char __user *path, int name)
  497. {
  498. int ret;
  499. ret = sunos_fpathconf(0, name); /* XXX cheese XXX */
  500. return ret;
  501. }
  502. /* SunOS mount system call emulation */
  503. asmlinkage int sunos_select(int width, fd_set __user *inp, fd_set __user *outp,
  504. fd_set __user *exp, struct timeval __user *tvp)
  505. {
  506. int ret;
  507. /* SunOS binaries expect that select won't change the tvp contents */
  508. ret = sys_select (width, inp, outp, exp, tvp);
  509. if (ret == -EINTR && tvp) {
  510. time_t sec, usec;
  511. __get_user(sec, &tvp->tv_sec);
  512. __get_user(usec, &tvp->tv_usec);
  513. if (sec == 0 && usec == 0)
  514. ret = 0;
  515. }
  516. return ret;
  517. }
  518. asmlinkage void sunos_nop(void)
  519. {
  520. return;
  521. }
  522. /* SunOS mount/umount. */
  523. #define SMNT_RDONLY 1
  524. #define SMNT_NOSUID 2
  525. #define SMNT_NEWTYPE 4
  526. #define SMNT_GRPID 8
  527. #define SMNT_REMOUNT 16
  528. #define SMNT_NOSUB 32
  529. #define SMNT_MULTI 64
  530. #define SMNT_SYS5 128
  531. struct sunos_fh_t {
  532. char fh_data [NFS_FHSIZE];
  533. };
  534. struct sunos_nfs_mount_args {
  535. struct sockaddr_in __user *addr; /* file server address */
  536. struct nfs_fh __user *fh; /* File handle to be mounted */
  537. int flags; /* flags */
  538. int wsize; /* write size in bytes */
  539. int rsize; /* read size in bytes */
  540. int timeo; /* initial timeout in .1 secs */
  541. int retrans; /* times to retry send */
  542. char __user *hostname; /* server's hostname */
  543. int acregmin; /* attr cache file min secs */
  544. int acregmax; /* attr cache file max secs */
  545. int acdirmin; /* attr cache dir min secs */
  546. int acdirmax; /* attr cache dir max secs */
  547. char __user *netname; /* server's netname */
  548. };
  549. /* Bind the socket on a local reserved port and connect it to the
  550. * remote server. This on Linux/i386 is done by the mount program,
  551. * not by the kernel.
  552. */
  553. static int
  554. sunos_nfs_get_server_fd (int fd, struct sockaddr_in *addr)
  555. {
  556. struct sockaddr_in local;
  557. struct sockaddr_in server;
  558. int try_port;
  559. struct socket *socket;
  560. struct inode *inode;
  561. struct file *file;
  562. int ret, result = 0;
  563. file = fget(fd);
  564. if (!file)
  565. goto out;
  566. inode = file->f_dentry->d_inode;
  567. socket = SOCKET_I(inode);
  568. local.sin_family = AF_INET;
  569. local.sin_addr.s_addr = INADDR_ANY;
  570. /* IPPORT_RESERVED = 1024, can't find the definition in the kernel */
  571. try_port = 1024;
  572. do {
  573. local.sin_port = htons (--try_port);
  574. ret = socket->ops->bind(socket, (struct sockaddr*)&local,
  575. sizeof(local));
  576. } while (ret && try_port > (1024 / 2));
  577. if (ret)
  578. goto out_putf;
  579. server.sin_family = AF_INET;
  580. server.sin_addr = addr->sin_addr;
  581. server.sin_port = NFS_PORT;
  582. /* Call sys_connect */
  583. ret = socket->ops->connect (socket, (struct sockaddr *) &server,
  584. sizeof (server), file->f_flags);
  585. if (ret >= 0)
  586. result = 1;
  587. out_putf:
  588. fput(file);
  589. out:
  590. return result;
  591. }
  592. static int get_default (int value, int def_value)
  593. {
  594. if (value)
  595. return value;
  596. else
  597. return def_value;
  598. }
  599. static int sunos_nfs_mount(char *dir_name, int linux_flags, void __user *data)
  600. {
  601. int server_fd, err;
  602. char *the_name, *mount_page;
  603. struct nfs_mount_data linux_nfs_mount;
  604. struct sunos_nfs_mount_args sunos_mount;
  605. /* Ok, here comes the fun part: Linux's nfs mount needs a
  606. * socket connection to the server, but SunOS mount does not
  607. * require this, so we use the information on the destination
  608. * address to create a socket and bind it to a reserved
  609. * port on this system
  610. */
  611. if (copy_from_user(&sunos_mount, data, sizeof(sunos_mount)))
  612. return -EFAULT;
  613. server_fd = sys_socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  614. if (server_fd < 0)
  615. return -ENXIO;
  616. if (copy_from_user(&linux_nfs_mount.addr,sunos_mount.addr,
  617. sizeof(*sunos_mount.addr)) ||
  618. copy_from_user(&linux_nfs_mount.root,sunos_mount.fh,
  619. sizeof(*sunos_mount.fh))) {
  620. sys_close (server_fd);
  621. return -EFAULT;
  622. }
  623. if (!sunos_nfs_get_server_fd (server_fd, &linux_nfs_mount.addr)){
  624. sys_close (server_fd);
  625. return -ENXIO;
  626. }
  627. /* Now, bind it to a locally reserved port */
  628. linux_nfs_mount.version = NFS_MOUNT_VERSION;
  629. linux_nfs_mount.flags = sunos_mount.flags;
  630. linux_nfs_mount.fd = server_fd;
  631. linux_nfs_mount.rsize = get_default (sunos_mount.rsize, 8192);
  632. linux_nfs_mount.wsize = get_default (sunos_mount.wsize, 8192);
  633. linux_nfs_mount.timeo = get_default (sunos_mount.timeo, 10);
  634. linux_nfs_mount.retrans = sunos_mount.retrans;
  635. linux_nfs_mount.acregmin = sunos_mount.acregmin;
  636. linux_nfs_mount.acregmax = sunos_mount.acregmax;
  637. linux_nfs_mount.acdirmin = sunos_mount.acdirmin;
  638. linux_nfs_mount.acdirmax = sunos_mount.acdirmax;
  639. the_name = getname(sunos_mount.hostname);
  640. if (IS_ERR(the_name))
  641. return PTR_ERR(the_name);
  642. strlcpy(linux_nfs_mount.hostname, the_name,
  643. sizeof(linux_nfs_mount.hostname));
  644. putname (the_name);
  645. mount_page = (char *) get_zeroed_page(GFP_KERNEL);
  646. if (!mount_page)
  647. return -ENOMEM;
  648. memcpy(mount_page, &linux_nfs_mount, sizeof(linux_nfs_mount));
  649. err = do_mount("", dir_name, "nfs", linux_flags, mount_page);
  650. free_page((unsigned long) mount_page);
  651. return err;
  652. }
  653. asmlinkage int
  654. sunos_mount(char __user *type, char __user *dir, int flags, void __user *data)
  655. {
  656. int linux_flags = 0;
  657. int ret = -EINVAL;
  658. char *dev_fname = NULL;
  659. char *dir_page, *type_page;
  660. if (!capable (CAP_SYS_ADMIN))
  661. return -EPERM;
  662. lock_kernel();
  663. /* We don't handle the integer fs type */
  664. if ((flags & SMNT_NEWTYPE) == 0)
  665. goto out;
  666. /* Do not allow for those flags we don't support */
  667. if (flags & (SMNT_GRPID|SMNT_NOSUB|SMNT_MULTI|SMNT_SYS5))
  668. goto out;
  669. if (flags & SMNT_REMOUNT)
  670. linux_flags |= MS_REMOUNT;
  671. if (flags & SMNT_RDONLY)
  672. linux_flags |= MS_RDONLY;
  673. if (flags & SMNT_NOSUID)
  674. linux_flags |= MS_NOSUID;
  675. dir_page = getname(dir);
  676. ret = PTR_ERR(dir_page);
  677. if (IS_ERR(dir_page))
  678. goto out;
  679. type_page = getname(type);
  680. ret = PTR_ERR(type_page);
  681. if (IS_ERR(type_page))
  682. goto out1;
  683. if (strcmp(type_page, "ext2") == 0) {
  684. dev_fname = getname(data);
  685. } else if (strcmp(type_page, "iso9660") == 0) {
  686. dev_fname = getname(data);
  687. } else if (strcmp(type_page, "minix") == 0) {
  688. dev_fname = getname(data);
  689. } else if (strcmp(type_page, "nfs") == 0) {
  690. ret = sunos_nfs_mount (dir_page, flags, data);
  691. goto out2;
  692. } else if (strcmp(type_page, "ufs") == 0) {
  693. printk("Warning: UFS filesystem mounts unsupported.\n");
  694. ret = -ENODEV;
  695. goto out2;
  696. } else if (strcmp(type_page, "proc")) {
  697. ret = -ENODEV;
  698. goto out2;
  699. }
  700. ret = PTR_ERR(dev_fname);
  701. if (IS_ERR(dev_fname))
  702. goto out2;
  703. ret = do_mount(dev_fname, dir_page, type_page, linux_flags, NULL);
  704. if (dev_fname)
  705. putname(dev_fname);
  706. out2:
  707. putname(type_page);
  708. out1:
  709. putname(dir_page);
  710. out:
  711. unlock_kernel();
  712. return ret;
  713. }
  714. asmlinkage int sunos_setpgrp(pid_t pid, pid_t pgid)
  715. {
  716. int ret;
  717. /* So stupid... */
  718. if ((!pid || pid == current->pid) &&
  719. !pgid) {
  720. sys_setsid();
  721. ret = 0;
  722. } else {
  723. ret = sys_setpgid(pid, pgid);
  724. }
  725. return ret;
  726. }
  727. /* So stupid... */
  728. asmlinkage int sunos_wait4(pid_t pid, unsigned int __user *stat_addr,
  729. int options, struct rusage __user*ru)
  730. {
  731. int ret;
  732. ret = sys_wait4((pid ? pid : -1), stat_addr, options, ru);
  733. return ret;
  734. }
  735. extern int kill_pg(int, int, int);
  736. asmlinkage int sunos_killpg(int pgrp, int sig)
  737. {
  738. int ret;
  739. lock_kernel();
  740. ret = kill_pg(pgrp, sig, 0);
  741. unlock_kernel();
  742. return ret;
  743. }
  744. asmlinkage int sunos_audit(void)
  745. {
  746. lock_kernel();
  747. printk ("sys_audit\n");
  748. unlock_kernel();
  749. return -1;
  750. }
  751. asmlinkage unsigned long sunos_gethostid(void)
  752. {
  753. unsigned long ret;
  754. lock_kernel();
  755. ret = ((unsigned long)idprom->id_machtype << 24) |
  756. (unsigned long)idprom->id_sernum;
  757. unlock_kernel();
  758. return ret;
  759. }
  760. /* sysconf options, for SunOS compatibility */
  761. #define _SC_ARG_MAX 1
  762. #define _SC_CHILD_MAX 2
  763. #define _SC_CLK_TCK 3
  764. #define _SC_NGROUPS_MAX 4
  765. #define _SC_OPEN_MAX 5
  766. #define _SC_JOB_CONTROL 6
  767. #define _SC_SAVED_IDS 7
  768. #define _SC_VERSION 8
  769. asmlinkage long sunos_sysconf (int name)
  770. {
  771. long ret;
  772. switch (name){
  773. case _SC_ARG_MAX:
  774. ret = ARG_MAX;
  775. break;
  776. case _SC_CHILD_MAX:
  777. ret = -1; /* no limit */
  778. break;
  779. case _SC_CLK_TCK:
  780. ret = HZ;
  781. break;
  782. case _SC_NGROUPS_MAX:
  783. ret = NGROUPS_MAX;
  784. break;
  785. case _SC_OPEN_MAX:
  786. ret = OPEN_MAX;
  787. break;
  788. case _SC_JOB_CONTROL:
  789. ret = 1; /* yes, we do support job control */
  790. break;
  791. case _SC_SAVED_IDS:
  792. ret = 1; /* yes, we do support saved uids */
  793. break;
  794. case _SC_VERSION:
  795. /* mhm, POSIX_VERSION is in /usr/include/unistd.h
  796. * should it go on /usr/include/linux?
  797. */
  798. ret = 199009L;
  799. break;
  800. default:
  801. ret = -1;
  802. break;
  803. };
  804. return ret;
  805. }
  806. asmlinkage int sunos_semsys(int op, unsigned long arg1, unsigned long arg2,
  807. unsigned long arg3, void *ptr)
  808. {
  809. union semun arg4;
  810. int ret;
  811. switch (op) {
  812. case 0:
  813. /* Most arguments match on a 1:1 basis but cmd doesn't */
  814. switch(arg3) {
  815. case 4:
  816. arg3=GETPID; break;
  817. case 5:
  818. arg3=GETVAL; break;
  819. case 6:
  820. arg3=GETALL; break;
  821. case 3:
  822. arg3=GETNCNT; break;
  823. case 7:
  824. arg3=GETZCNT; break;
  825. case 8:
  826. arg3=SETVAL; break;
  827. case 9:
  828. arg3=SETALL; break;
  829. }
  830. /* sys_semctl(): */
  831. /* value to modify semaphore to */
  832. arg4.__pad = (void __user *) ptr;
  833. ret = sys_semctl((int)arg1, (int)arg2, (int)arg3, arg4 );
  834. break;
  835. case 1:
  836. /* sys_semget(): */
  837. ret = sys_semget((key_t)arg1, (int)arg2, (int)arg3);
  838. break;
  839. case 2:
  840. /* sys_semop(): */
  841. ret = sys_semop((int)arg1, (struct sembuf __user *)arg2, (unsigned)arg3);
  842. break;
  843. default:
  844. ret = -EINVAL;
  845. break;
  846. };
  847. return ret;
  848. }
  849. asmlinkage int sunos_msgsys(int op, unsigned long arg1, unsigned long arg2,
  850. unsigned long arg3, unsigned long arg4)
  851. {
  852. struct sparc_stackf *sp;
  853. unsigned long arg5;
  854. int rval;
  855. switch(op) {
  856. case 0:
  857. rval = sys_msgget((key_t)arg1, (int)arg2);
  858. break;
  859. case 1:
  860. rval = sys_msgctl((int)arg1, (int)arg2,
  861. (struct msqid_ds __user *)arg3);
  862. break;
  863. case 2:
  864. lock_kernel();
  865. sp = (struct sparc_stackf *)current->thread.kregs->u_regs[UREG_FP];
  866. arg5 = sp->xxargs[0];
  867. unlock_kernel();
  868. rval = sys_msgrcv((int)arg1, (struct msgbuf __user *)arg2,
  869. (size_t)arg3, (long)arg4, (int)arg5);
  870. break;
  871. case 3:
  872. rval = sys_msgsnd((int)arg1, (struct msgbuf __user *)arg2,
  873. (size_t)arg3, (int)arg4);
  874. break;
  875. default:
  876. rval = -EINVAL;
  877. break;
  878. }
  879. return rval;
  880. }
  881. asmlinkage int sunos_shmsys(int op, unsigned long arg1, unsigned long arg2,
  882. unsigned long arg3)
  883. {
  884. unsigned long raddr;
  885. int rval;
  886. switch(op) {
  887. case 0:
  888. /* do_shmat(): attach a shared memory area */
  889. rval = do_shmat((int)arg1,(char __user *)arg2,(int)arg3,&raddr);
  890. if (!rval)
  891. rval = (int) raddr;
  892. break;
  893. case 1:
  894. /* sys_shmctl(): modify shared memory area attr. */
  895. rval = sys_shmctl((int)arg1,(int)arg2,(struct shmid_ds __user *)arg3);
  896. break;
  897. case 2:
  898. /* sys_shmdt(): detach a shared memory area */
  899. rval = sys_shmdt((char __user *)arg1);
  900. break;
  901. case 3:
  902. /* sys_shmget(): get a shared memory area */
  903. rval = sys_shmget((key_t)arg1,(int)arg2,(int)arg3);
  904. break;
  905. default:
  906. rval = -EINVAL;
  907. break;
  908. };
  909. return rval;
  910. }
  911. #define SUNOS_EWOULDBLOCK 35
  912. /* see the sunos man page read(2v) for an explanation
  913. of this garbage. We use O_NDELAY to mark
  914. file descriptors that have been set non-blocking
  915. using 4.2BSD style calls. (tridge) */
  916. static inline int check_nonblock(int ret, int fd)
  917. {
  918. if (ret == -EAGAIN) {
  919. struct file * file = fget(fd);
  920. if (file) {
  921. if (file->f_flags & O_NDELAY)
  922. ret = -SUNOS_EWOULDBLOCK;
  923. fput(file);
  924. }
  925. }
  926. return ret;
  927. }
  928. asmlinkage int sunos_read(unsigned int fd, char __user *buf, int count)
  929. {
  930. int ret;
  931. ret = check_nonblock(sys_read(fd,buf,count),fd);
  932. return ret;
  933. }
  934. asmlinkage int sunos_readv(unsigned long fd, const struct iovec __user *vector,
  935. long count)
  936. {
  937. int ret;
  938. ret = check_nonblock(sys_readv(fd,vector,count),fd);
  939. return ret;
  940. }
  941. asmlinkage int sunos_write(unsigned int fd, char __user *buf, int count)
  942. {
  943. int ret;
  944. ret = check_nonblock(sys_write(fd,buf,count),fd);
  945. return ret;
  946. }
  947. asmlinkage int sunos_writev(unsigned long fd,
  948. const struct iovec __user *vector, long count)
  949. {
  950. int ret;
  951. ret = check_nonblock(sys_writev(fd,vector,count),fd);
  952. return ret;
  953. }
  954. asmlinkage int sunos_recv(int fd, void __user *ubuf, int size, unsigned flags)
  955. {
  956. int ret;
  957. ret = check_nonblock(sys_recv(fd,ubuf,size,flags),fd);
  958. return ret;
  959. }
  960. asmlinkage int sunos_send(int fd, void __user *buff, int len, unsigned flags)
  961. {
  962. int ret;
  963. ret = check_nonblock(sys_send(fd,buff,len,flags),fd);
  964. return ret;
  965. }
  966. asmlinkage int sunos_accept(int fd, struct sockaddr __user *sa,
  967. int __user *addrlen)
  968. {
  969. int ret;
  970. while (1) {
  971. ret = check_nonblock(sys_accept(fd,sa,addrlen),fd);
  972. if (ret != -ENETUNREACH && ret != -EHOSTUNREACH)
  973. break;
  974. }
  975. return ret;
  976. }
  977. #define SUNOS_SV_INTERRUPT 2
  978. asmlinkage int
  979. sunos_sigaction(int sig, const struct old_sigaction __user *act,
  980. struct old_sigaction __user *oact)
  981. {
  982. struct k_sigaction new_ka, old_ka;
  983. int ret;
  984. if (act) {
  985. old_sigset_t mask;
  986. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  987. __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
  988. __get_user(new_ka.sa.sa_flags, &act->sa_flags))
  989. return -EFAULT;
  990. __get_user(mask, &act->sa_mask);
  991. new_ka.sa.sa_restorer = NULL;
  992. new_ka.ka_restorer = NULL;
  993. siginitset(&new_ka.sa.sa_mask, mask);
  994. new_ka.sa.sa_flags ^= SUNOS_SV_INTERRUPT;
  995. }
  996. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  997. if (!ret && oact) {
  998. /* In the clone() case we could copy half consistent
  999. * state to the user, however this could sleep and
  1000. * deadlock us if we held the signal lock on SMP. So for
  1001. * now I take the easy way out and do no locking.
  1002. * But then again we don't support SunOS lwp's anyways ;-)
  1003. */
  1004. old_ka.sa.sa_flags ^= SUNOS_SV_INTERRUPT;
  1005. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  1006. __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
  1007. __put_user(old_ka.sa.sa_flags, &oact->sa_flags))
  1008. return -EFAULT;
  1009. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
  1010. }
  1011. return ret;
  1012. }
  1013. asmlinkage int sunos_setsockopt(int fd, int level, int optname,
  1014. char __user *optval, int optlen)
  1015. {
  1016. int tr_opt = optname;
  1017. int ret;
  1018. if (level == SOL_IP) {
  1019. /* Multicast socketopts (ttl, membership) */
  1020. if (tr_opt >=2 && tr_opt <= 6)
  1021. tr_opt += 30;
  1022. }
  1023. ret = sys_setsockopt(fd, level, tr_opt, optval, optlen);
  1024. return ret;
  1025. }
  1026. asmlinkage int sunos_getsockopt(int fd, int level, int optname,
  1027. char __user *optval, int __user *optlen)
  1028. {
  1029. int tr_opt = optname;
  1030. int ret;
  1031. if (level == SOL_IP) {
  1032. /* Multicast socketopts (ttl, membership) */
  1033. if (tr_opt >=2 && tr_opt <= 6)
  1034. tr_opt += 30;
  1035. }
  1036. ret = sys_getsockopt(fd, level, tr_opt, optval, optlen);
  1037. return ret;
  1038. }