linux_misc.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  1. /* $OpenBSD: linux_misc.c,v 1.94 2015/05/07 08:53:32 mpi Exp $ */
  2. /* $NetBSD: linux_misc.c,v 1.27 1996/05/20 01:59:21 fvdl Exp $ */
  3. /*-
  4. * Copyright (c) 1995, 1998, 1999 The NetBSD Foundation, Inc.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to The NetBSD Foundation
  8. * by Frank van der Linden and Eric Haszlakiewicz; by Jason R. Thorpe
  9. * of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  24. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /*
  33. * Linux compatibility module. Try to deal with various Linux system calls.
  34. */
  35. #include <sys/param.h>
  36. #include <sys/systm.h>
  37. #include <sys/namei.h>
  38. #include <sys/proc.h>
  39. #include <sys/dirent.h>
  40. #include <sys/file.h>
  41. #include <sys/stat.h>
  42. #include <sys/filedesc.h>
  43. #include <sys/ioctl.h>
  44. #include <sys/kernel.h>
  45. #include <sys/malloc.h>
  46. #include <sys/mbuf.h>
  47. #include <sys/mman.h>
  48. #include <sys/mount.h>
  49. #include <sys/ptrace.h>
  50. #include <sys/resource.h>
  51. #include <sys/swap.h>
  52. #include <sys/resourcevar.h>
  53. #include <sys/signal.h>
  54. #include <sys/signalvar.h>
  55. #include <sys/socket.h>
  56. #include <sys/sysctl.h>
  57. #include <sys/time.h>
  58. #include <sys/vnode.h>
  59. #include <sys/uio.h>
  60. #include <sys/wait.h>
  61. #include <sys/utsname.h>
  62. #include <sys/unistd.h>
  63. #include <sys/syscallargs.h>
  64. #include <uvm/uvm_extern.h>
  65. #include <compat/linux/linux_types.h>
  66. #include <compat/linux/linux_time.h>
  67. #include <compat/linux/linux_fcntl.h>
  68. #include <compat/linux/linux_misc.h>
  69. #include <compat/linux/linux_mmap.h>
  70. #include <compat/linux/linux_sched.h>
  71. #include <compat/linux/linux_signal.h>
  72. #include <compat/linux/linux_syscallargs.h>
  73. #include <compat/linux/linux_util.h>
  74. #include <compat/linux/linux_dirent.h>
  75. #include <compat/linux/linux_emuldata.h>
  76. #include <compat/common/compat_dir.h>
  77. /* linux_misc.c */
  78. void bsd_to_linux_statfs(const struct statfs *, struct linux_statfs *);
  79. void bsd_to_linux_statfs64(const struct statfs *, struct linux_statfs64 *);
  80. int linux_select1(struct proc *, register_t *, int, fd_set *,
  81. fd_set *, fd_set *, struct linux_timeval *);
  82. int getdents_common(struct proc *, void *, register_t *, int);
  83. void linux_to_bsd_mmap_args(struct sys_mmap_args *,
  84. const struct linux_sys_mmap2_args *);
  85. void bsd_to_linux_rusage(struct linux_rusage *, const struct rusage *);
  86. void bsd_to_linux_wstat(int *);
  87. /*
  88. * The information on a terminated (or stopped) process needs
  89. * to be converted in order for Linux binaries to get a valid signal
  90. * number out of it.
  91. */
  92. void
  93. bsd_to_linux_wstat(status)
  94. int *status;
  95. {
  96. if (WIFSIGNALED(*status))
  97. *status = (*status & ~0177) |
  98. bsd_to_linux_sig[WTERMSIG(*status)];
  99. else if (WIFSTOPPED(*status))
  100. *status = (*status & ~0xff00) |
  101. (bsd_to_linux_sig[WSTOPSIG(*status)] << 8);
  102. }
  103. /*
  104. * Convert an rusage to Linux format: small time_t in the timevals
  105. */
  106. void
  107. bsd_to_linux_rusage(struct linux_rusage *lrup, const struct rusage *rup)
  108. {
  109. bsd_to_linux_timeval(&lrup->ru_utime, &rup->ru_utime);
  110. bsd_to_linux_timeval(&lrup->ru_utime, &rup->ru_utime);
  111. memcpy(&lrup->ru_maxrss, &rup->ru_maxrss,
  112. offsetof(struct rusage, ru_nivcsw) -
  113. offsetof(struct rusage, ru_maxrss) +
  114. sizeof(lrup->ru_nivcsw));
  115. }
  116. /*
  117. * waitpid(2). Just forward on to linux_sys_wait4 with a NULL rusage.
  118. */
  119. int
  120. linux_sys_waitpid(p, v, retval)
  121. struct proc *p;
  122. void *v;
  123. register_t *retval;
  124. {
  125. struct linux_sys_waitpid_args /* {
  126. syscallarg(int) pid;
  127. syscallarg(int *) status;
  128. syscallarg(int) options;
  129. } */ *uap = v;
  130. struct sys_wait4_args linux_w4a;
  131. SCARG(&linux_w4a, pid) = SCARG(uap, pid);
  132. SCARG(&linux_w4a, status) = SCARG(uap, status);
  133. SCARG(&linux_w4a, options) = SCARG(uap, options);
  134. SCARG(&linux_w4a, rusage) = NULL;
  135. return (linux_sys_wait4(p, &linux_w4a, retval));
  136. }
  137. /*
  138. * wait4(2): handle conversion of the options on entry and status and rusage
  139. * on return.
  140. */
  141. int
  142. linux_sys_wait4(p, v, retval)
  143. struct proc *p;
  144. void *v;
  145. register_t *retval;
  146. {
  147. struct linux_sys_wait4_args /* {
  148. syscallarg(int) pid;
  149. syscallarg(int *) status;
  150. syscallarg(int) options;
  151. syscallarg(struct linux_rusage *) rusage;
  152. } */ *uap = v;
  153. struct rusage ru;
  154. int error, status, linux_options, options;
  155. linux_options = SCARG(uap, options);
  156. options = 0;
  157. if (linux_options & ~(LINUX_WAIT4_WNOHANG|LINUX_WAIT4_WUNTRACED))
  158. return (EINVAL);
  159. if (linux_options & LINUX_WAIT4_WNOHANG)
  160. options |= WNOHANG;
  161. if (linux_options & LINUX_WAIT4_WUNTRACED)
  162. options |= WUNTRACED;
  163. if ((error = dowait4(p, SCARG(uap, pid),
  164. SCARG(uap, status) ? &status : NULL, options,
  165. SCARG(uap, rusage) ? &ru : NULL, retval)))
  166. return error;
  167. atomic_clearbits_int(&p->p_siglist, sigmask(SIGCHLD));
  168. if (SCARG(uap, rusage) != NULL) {
  169. struct linux_rusage lru;
  170. bsd_to_linux_rusage(&lru, &ru);
  171. if ((error = copyout(&lru, SCARG(uap, rusage), sizeof lru)))
  172. return error;
  173. }
  174. if (SCARG(uap, status) != NULL) {
  175. bsd_to_linux_wstat(&status);
  176. return copyout(&status, SCARG(uap, status), sizeof status);
  177. }
  178. return 0;
  179. }
  180. /*
  181. * getrusage(2): convert rusage on return
  182. */
  183. int
  184. linux_sys_getrusage(p, v, retval)
  185. struct proc *p;
  186. void *v;
  187. register_t *retval;
  188. {
  189. struct linux_sys_getrusage_args /* {
  190. syscallarg(int) who;
  191. syscallarg(struct linux_rusage *) rusage;
  192. } */ *uap = v;
  193. struct rusage ru;
  194. int error;
  195. error = dogetrusage(p, SCARG(uap, who), &ru);
  196. if (error == 0) {
  197. struct linux_rusage lru;
  198. bsd_to_linux_rusage(&lru, &ru);
  199. error = copyout(&lru, SCARG(uap, rusage), sizeof lru);
  200. }
  201. return error;
  202. }
  203. int
  204. linux_sys_setresgid16(p, v, retval)
  205. struct proc *p;
  206. void *v;
  207. register_t *retval;
  208. {
  209. struct linux_sys_setresgid16_args /* {
  210. syscallarg(u_int16_t) rgid;
  211. syscallarg(u_int16_t) egid;
  212. syscallarg(u_int16_t) sgid;
  213. } */ *uap = v;
  214. struct sys_setresgid_args nuap;
  215. u_int16_t rgid, egid, sgid;
  216. rgid = SCARG(uap, rgid);
  217. SCARG(&nuap, rgid) = (rgid == (u_int16_t)-1) ? (gid_t)-1 : rgid;
  218. egid = SCARG(uap, egid);
  219. SCARG(&nuap, egid) = (egid == (u_int16_t)-1) ? (gid_t)-1 : egid;
  220. sgid = SCARG(uap, sgid);
  221. SCARG(&nuap, sgid) = (sgid == (u_int16_t)-1) ? (gid_t)-1 : sgid;
  222. return sys_setresgid(p, &nuap, retval);
  223. }
  224. int
  225. linux_sys_getresgid16(p, v, retval)
  226. struct proc *p;
  227. void *v;
  228. register_t *retval;
  229. {
  230. struct linux_sys_getresgid16_args /* {
  231. syscallarg(u_int16_t *) rgid;
  232. syscallarg(u_int16_t *) egid;
  233. syscallarg(u_int16_t *) sgid;
  234. } */ *uap = v;
  235. struct sys_getresgid_args nuap;
  236. SCARG(&nuap, rgid) = (gid_t *)SCARG(uap, rgid);
  237. SCARG(&nuap, egid) = (gid_t *)SCARG(uap, egid);
  238. SCARG(&nuap, sgid) = (gid_t *)SCARG(uap, sgid);
  239. return sys_getresgid(p, &nuap, retval);
  240. }
  241. int
  242. linux_sys_setresuid16(p, v, retval)
  243. struct proc *p;
  244. void *v;
  245. register_t *retval;
  246. {
  247. struct linux_sys_setresuid16_args /* {
  248. syscallarg(u_int16_t) ruid;
  249. syscallarg(u_int16_t) euid;
  250. syscallarg(u_int16_t) suid;
  251. } */ *uap = v;
  252. struct sys_setresuid_args nuap;
  253. u_int16_t ruid, euid, suid;
  254. ruid = SCARG(uap, ruid);
  255. SCARG(&nuap, ruid) = (ruid == (u_int16_t)-1) ? (uid_t)-1 : ruid;
  256. euid = SCARG(uap, euid);
  257. SCARG(&nuap, euid) = (euid == (u_int16_t)-1) ? (uid_t)-1 : euid;
  258. suid = SCARG(uap, suid);
  259. SCARG(&nuap, suid) = (suid == (u_int16_t)-1) ? (uid_t)-1 : suid;
  260. return sys_setresuid(p, &nuap, retval);
  261. }
  262. int
  263. linux_sys_getresuid16(p, v, retval)
  264. struct proc *p;
  265. void *v;
  266. register_t *retval;
  267. {
  268. struct linux_sys_getresuid16_args /* {
  269. syscallarg(u_int16_t *) ruid;
  270. syscallarg(u_int16_t *) euid;
  271. syscallarg(u_int16_t *) suid;
  272. } */ *uap = v;
  273. struct sys_getresuid_args nuap;
  274. SCARG(&nuap, ruid) = (uid_t *)SCARG(uap, ruid);
  275. SCARG(&nuap, euid) = (uid_t *)SCARG(uap, euid);
  276. SCARG(&nuap, suid) = (uid_t *)SCARG(uap, suid);
  277. return sys_getresuid(p, &nuap, retval);
  278. }
  279. /*
  280. * This is the old brk(2) call. I don't think anything in the Linux
  281. * world uses this anymore
  282. */
  283. int
  284. linux_sys_break(p, v, retval)
  285. struct proc *p;
  286. void *v;
  287. register_t *retval;
  288. {
  289. #if 0
  290. struct linux_sys_brk_args /* {
  291. syscallarg(char *) nsize;
  292. } */ *uap = v;
  293. #endif
  294. return ENOSYS;
  295. }
  296. /*
  297. * Linux brk(2). The check if the new address is >= the old one is
  298. * done in the kernel in Linux. OpenBSD does it in the library.
  299. */
  300. int
  301. linux_sys_brk(p, v, retval)
  302. struct proc *p;
  303. void *v;
  304. register_t *retval;
  305. {
  306. struct linux_sys_brk_args /* {
  307. syscallarg(char *) nsize;
  308. } */ *uap = v;
  309. char *nbrk = SCARG(uap, nsize);
  310. struct sys_obreak_args oba;
  311. struct vmspace *vm = p->p_vmspace;
  312. struct linux_emuldata *ed = (struct linux_emuldata*)p->p_emuldata;
  313. SCARG(&oba, nsize) = nbrk;
  314. if ((caddr_t) nbrk > vm->vm_daddr && sys_obreak(p, &oba, retval) == 0)
  315. ed->p_break = (char*)nbrk;
  316. else
  317. nbrk = ed->p_break;
  318. retval[0] = (register_t)nbrk;
  319. return 0;
  320. }
  321. /*
  322. * I wonder why Linux has gettimeofday() _and_ time().. Still, we
  323. * need to deal with it.
  324. */
  325. int
  326. linux_sys_time(p, v, retval)
  327. struct proc *p;
  328. void *v;
  329. register_t *retval;
  330. {
  331. struct linux_sys_time_args /* {
  332. linux_time_t *t;
  333. } */ *uap = v;
  334. struct timeval atv;
  335. linux_time_t tt;
  336. int error;
  337. microtime(&atv);
  338. if (atv.tv_sec > LINUX_TIME_MAX)
  339. return (EOVERFLOW);
  340. tt = (linux_time_t)atv.tv_sec;
  341. if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
  342. return error;
  343. retval[0] = tt;
  344. return 0;
  345. }
  346. /*
  347. * Convert BSD statfs structure to Linux statfs structure.
  348. * The Linux structure has less fields, and it also wants
  349. * the length of a name in a dir entry in a field, which
  350. * we fake (probably the wrong way).
  351. */
  352. void
  353. bsd_to_linux_statfs(const struct statfs *bsp, struct linux_statfs *lsp)
  354. {
  355. /*
  356. * Convert BSD filesystem names to Linux filesystem type numbers
  357. * where possible. Linux statfs uses a value of -1 to indicate
  358. * an unsupported field.
  359. */
  360. if (!strcmp(bsp->f_fstypename, MOUNT_FFS) ||
  361. !strcmp(bsp->f_fstypename, MOUNT_MFS))
  362. lsp->l_ftype = LINUX_FSTYPE_FFS;
  363. else if (!strcmp(bsp->f_fstypename, MOUNT_NFS))
  364. lsp->l_ftype = LINUX_FSTYPE_NFS;
  365. else if (!strcmp(bsp->f_fstypename, MOUNT_MSDOS))
  366. lsp->l_ftype = LINUX_FSTYPE_MSDOS;
  367. else if (!strcmp(bsp->f_fstypename, MOUNT_EXT2FS))
  368. lsp->l_ftype = LINUX_FSTYPE_EXT2FS;
  369. else if (!strcmp(bsp->f_fstypename, MOUNT_CD9660))
  370. lsp->l_ftype = LINUX_FSTYPE_CD9660;
  371. else if (!strcmp(bsp->f_fstypename, MOUNT_NCPFS))
  372. lsp->l_ftype = LINUX_FSTYPE_NCPFS;
  373. else if (!strcmp(bsp->f_fstypename, MOUNT_NTFS))
  374. lsp->l_ftype = LINUX_FSTYPE_NTFS;
  375. else if (!strcmp(bsp->f_fstypename, MOUNT_UDF))
  376. lsp->l_ftype = LINUX_FSTYPE_UDF;
  377. else if (!strcmp(bsp->f_fstypename, MOUNT_AFS))
  378. lsp->l_ftype = LINUX_FSTYPE_AFS;
  379. else
  380. lsp->l_ftype = -1;
  381. lsp->l_fbsize = bsp->f_bsize;
  382. lsp->l_fblocks = bsp->f_blocks;
  383. lsp->l_fbfree = bsp->f_bfree;
  384. lsp->l_fbavail = bsp->f_bavail;
  385. lsp->l_ffiles = bsp->f_files;
  386. lsp->l_fffree = bsp->f_ffree;
  387. lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
  388. lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
  389. lsp->l_fnamelen = MAXNAMLEN; /* XXX */
  390. }
  391. /*
  392. * Implement the fs stat functions. Straightforward.
  393. */
  394. int
  395. linux_sys_statfs(p, v, retval)
  396. struct proc *p;
  397. void *v;
  398. register_t *retval;
  399. {
  400. struct linux_sys_statfs_args /* {
  401. syscallarg(char *) path;
  402. syscallarg(struct linux_statfs *) sp;
  403. } */ *uap = v;
  404. struct statfs btmp, *bsp;
  405. struct linux_statfs ltmp;
  406. struct sys_statfs_args bsa;
  407. caddr_t sg;
  408. int error;
  409. sg = stackgap_init(p);
  410. bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
  411. LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
  412. SCARG(&bsa, path) = SCARG(uap, path);
  413. SCARG(&bsa, buf) = bsp;
  414. if ((error = sys_statfs(p, &bsa, retval)))
  415. return error;
  416. if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
  417. return error;
  418. bsd_to_linux_statfs(&btmp, &ltmp);
  419. return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
  420. }
  421. void
  422. bsd_to_linux_statfs64(const struct statfs *bsp, struct linux_statfs64 *lsp)
  423. {
  424. /*
  425. * Convert BSD filesystem names to Linux filesystem type numbers
  426. * where possible. Linux statfs uses a value of -1 to indicate
  427. * an unsupported field.
  428. */
  429. if (!strcmp(bsp->f_fstypename, MOUNT_FFS) ||
  430. !strcmp(bsp->f_fstypename, MOUNT_MFS))
  431. lsp->l_ftype = LINUX_FSTYPE_FFS;
  432. else if (!strcmp(bsp->f_fstypename, MOUNT_NFS))
  433. lsp->l_ftype = LINUX_FSTYPE_NFS;
  434. else if (!strcmp(bsp->f_fstypename, MOUNT_MSDOS))
  435. lsp->l_ftype = LINUX_FSTYPE_MSDOS;
  436. else if (!strcmp(bsp->f_fstypename, MOUNT_EXT2FS))
  437. lsp->l_ftype = LINUX_FSTYPE_EXT2FS;
  438. else if (!strcmp(bsp->f_fstypename, MOUNT_CD9660))
  439. lsp->l_ftype = LINUX_FSTYPE_CD9660;
  440. else if (!strcmp(bsp->f_fstypename, MOUNT_NCPFS))
  441. lsp->l_ftype = LINUX_FSTYPE_NCPFS;
  442. else if (!strcmp(bsp->f_fstypename, MOUNT_NTFS))
  443. lsp->l_ftype = LINUX_FSTYPE_NTFS;
  444. else if (!strcmp(bsp->f_fstypename, MOUNT_UDF))
  445. lsp->l_ftype = LINUX_FSTYPE_UDF;
  446. else if (!strcmp(bsp->f_fstypename, MOUNT_AFS))
  447. lsp->l_ftype = LINUX_FSTYPE_AFS;
  448. else
  449. lsp->l_ftype = -1;
  450. lsp->l_fbsize = bsp->f_bsize;
  451. lsp->l_fblocks = bsp->f_blocks;
  452. lsp->l_fbfree = bsp->f_bfree;
  453. lsp->l_fbavail = bsp->f_bavail;
  454. lsp->l_ffiles = bsp->f_files;
  455. lsp->l_fffree = bsp->f_ffree;
  456. lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
  457. lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
  458. lsp->l_fnamelen = MAXNAMLEN; /* XXX */
  459. }
  460. int
  461. linux_sys_statfs64(struct proc *p, void *v, register_t *retval)
  462. {
  463. struct linux_sys_statfs64_args /* {
  464. syscallarg(char *) path;
  465. syscallarg(struct linux_statfs64 *) sp;
  466. } */ *uap = v;
  467. struct statfs btmp, *bsp;
  468. struct linux_statfs64 ltmp;
  469. struct sys_statfs_args bsa;
  470. caddr_t sg;
  471. int error;
  472. sg = stackgap_init(p);
  473. bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
  474. LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
  475. SCARG(&bsa, path) = SCARG(uap, path);
  476. SCARG(&bsa, buf) = bsp;
  477. if ((error = sys_statfs(p, &bsa, retval)))
  478. return error;
  479. if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
  480. return error;
  481. bsd_to_linux_statfs64(&btmp, &ltmp);
  482. return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
  483. }
  484. int
  485. linux_sys_fstatfs(p, v, retval)
  486. struct proc *p;
  487. void *v;
  488. register_t *retval;
  489. {
  490. struct linux_sys_fstatfs_args /* {
  491. syscallarg(int) fd;
  492. syscallarg(struct linux_statfs *) sp;
  493. } */ *uap = v;
  494. struct statfs btmp, *bsp;
  495. struct linux_statfs ltmp;
  496. struct sys_fstatfs_args bsa;
  497. caddr_t sg;
  498. int error;
  499. sg = stackgap_init(p);
  500. bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
  501. SCARG(&bsa, fd) = SCARG(uap, fd);
  502. SCARG(&bsa, buf) = bsp;
  503. if ((error = sys_fstatfs(p, &bsa, retval)))
  504. return error;
  505. if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
  506. return error;
  507. bsd_to_linux_statfs(&btmp, &ltmp);
  508. return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
  509. }
  510. int
  511. linux_sys_fstatfs64(struct proc *p, void *v, register_t *retval)
  512. {
  513. struct linux_sys_fstatfs64_args /* {
  514. syscallarg(int) fd;
  515. syscallarg(struct linux_statfs64 *) sp;
  516. } */ *uap = v;
  517. struct statfs btmp, *bsp;
  518. struct linux_statfs64 ltmp;
  519. struct sys_fstatfs_args bsa;
  520. caddr_t sg;
  521. int error;
  522. sg = stackgap_init(p);
  523. bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
  524. SCARG(&bsa, fd) = SCARG(uap, fd);
  525. SCARG(&bsa, buf) = bsp;
  526. if ((error = sys_fstatfs(p, &bsa, retval)))
  527. return error;
  528. if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
  529. return error;
  530. bsd_to_linux_statfs64(&btmp, &ltmp);
  531. return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
  532. }
  533. /*
  534. * uname(). Just copy the info from the various strings stored in the
  535. * kernel, and put it in the Linux utsname structure. That structure
  536. * is almost the same as the OpenBSD one, only it has fields 65 characters
  537. * long, and an extra domainname field.
  538. */
  539. int
  540. linux_sys_uname(p, v, retval)
  541. struct proc *p;
  542. void *v;
  543. register_t *retval;
  544. {
  545. struct linux_sys_uname_args /* {
  546. syscallarg(struct linux_utsname *) up;
  547. } */ *uap = v;
  548. extern char hostname[], machine[], domainname[];
  549. struct linux_utsname luts;
  550. int len;
  551. char *cp;
  552. strlcpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
  553. strlcpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
  554. strlcpy(luts.l_release, osrelease, sizeof(luts.l_release));
  555. strlcpy(luts.l_version, version, sizeof(luts.l_version));
  556. strlcpy(luts.l_machine, machine, sizeof(luts.l_machine));
  557. strlcpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
  558. /* This part taken from the uname() in libc */
  559. len = sizeof(luts.l_version);
  560. for (cp = luts.l_version; len--; ++cp)
  561. if (*cp == '\n' || *cp == '\t')
  562. *cp = (len > 1) ? ' ' : '\0';
  563. return copyout(&luts, SCARG(uap, up), sizeof(luts));
  564. }
  565. int
  566. linux_sys_olduname(p, v, retval)
  567. struct proc *p;
  568. void *v;
  569. register_t *retval;
  570. {
  571. struct linux_sys_uname_args /* {
  572. syscallarg(struct linux_oldutsname *) up;
  573. } */ *uap = v;
  574. extern char hostname[], machine[];
  575. struct linux_oldutsname luts;
  576. int len;
  577. char *cp;
  578. strlcpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
  579. strlcpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
  580. strlcpy(luts.l_release, osrelease, sizeof(luts.l_release));
  581. strlcpy(luts.l_version, version, sizeof(luts.l_version));
  582. strlcpy(luts.l_machine, machine, sizeof(luts.l_machine));
  583. /* This part taken from the uname() in libc */
  584. len = sizeof(luts.l_version);
  585. for (cp = luts.l_version; len--; ++cp)
  586. if (*cp == '\n' || *cp == '\t')
  587. *cp = (len > 1) ? ' ' : '\0';
  588. return copyout(&luts, SCARG(uap, up), sizeof(luts));
  589. }
  590. int
  591. linux_sys_oldolduname(p, v, retval)
  592. struct proc *p;
  593. void *v;
  594. register_t *retval;
  595. {
  596. struct linux_sys_uname_args /* {
  597. syscallarg(struct linux_oldoldutsname *) up;
  598. } */ *uap = v;
  599. extern char hostname[], machine[];
  600. struct linux_oldoldutsname luts;
  601. int len;
  602. char *cp;
  603. strlcpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
  604. strlcpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
  605. strlcpy(luts.l_release, osrelease, sizeof(luts.l_release));
  606. strlcpy(luts.l_version, version, sizeof(luts.l_version));
  607. strlcpy(luts.l_machine, machine, sizeof(luts.l_machine));
  608. /* This part taken from the uname() in libc */
  609. len = sizeof(luts.l_version);
  610. for (cp = luts.l_version; len--; ++cp)
  611. if (*cp == '\n' || *cp == '\t')
  612. *cp = (len > 1) ? ' ' : '\0';
  613. return copyout(&luts, SCARG(uap, up), sizeof(luts));
  614. }
  615. int
  616. linux_sys_sethostname(struct proc *p, void *v, register_t *retval)
  617. {
  618. struct linux_sys_sethostname_args *uap = v;
  619. int name;
  620. int error;
  621. if ((error = suser(p, 0)) != 0)
  622. return (error);
  623. name = KERN_HOSTNAME;
  624. return (kern_sysctl(&name, 1, 0, 0, SCARG(uap, hostname),
  625. SCARG(uap, len), p));
  626. }
  627. /*
  628. * Linux wants to pass everything to a syscall in registers. However,
  629. * mmap() has 6 of them. Oops: out of register error. They just pass
  630. * everything in a structure.
  631. */
  632. int
  633. linux_sys_mmap(p, v, retval)
  634. struct proc *p;
  635. void *v;
  636. register_t *retval;
  637. {
  638. struct linux_sys_mmap_args /* {
  639. syscallarg(struct linux_mmap *) lmp;
  640. } */ *uap = v;
  641. struct linux_mmap lmap;
  642. struct linux_sys_mmap2_args nlmap;
  643. struct sys_mmap_args cma;
  644. int error;
  645. if ((error = copyin(SCARG(uap, lmp), &lmap, sizeof lmap)))
  646. return error;
  647. if (lmap.lm_pos & PAGE_MASK)
  648. return EINVAL;
  649. /* repackage into something sane */
  650. SCARG(&nlmap,addr) = (unsigned long)lmap.lm_addr;
  651. SCARG(&nlmap,len) = lmap.lm_len;
  652. SCARG(&nlmap,prot) = lmap.lm_prot;
  653. SCARG(&nlmap,flags) = lmap.lm_flags;
  654. SCARG(&nlmap,fd) = lmap.lm_fd;
  655. SCARG(&nlmap,offset) = (unsigned)lmap.lm_pos;
  656. linux_to_bsd_mmap_args(&cma, &nlmap);
  657. SCARG(&cma, pos) = (off_t)SCARG(&nlmap, offset);
  658. return sys_mmap(p, &cma, retval);
  659. }
  660. /*
  661. * Guts of most architectures' mmap64() implementations. This shares
  662. * its list of arguments with linux_sys_mmap().
  663. *
  664. * The difference in linux_sys_mmap2() is that "offset" is actually
  665. * (offset / pagesize), not an absolute byte count. This translation
  666. * to pagesize offsets is done inside glibc between the mmap64() call
  667. * point, and the actual syscall.
  668. */
  669. int
  670. linux_sys_mmap2(p, v, retval)
  671. struct proc *p;
  672. void *v;
  673. register_t *retval;
  674. {
  675. struct linux_sys_mmap2_args /* {
  676. syscallarg(unsigned long) addr;
  677. syscallarg(size_t) len;
  678. syscallarg(int) prot;
  679. syscallarg(int) flags;
  680. syscallarg(int) fd;
  681. syscallarg(linux_off_t) offset;
  682. } */ *uap = v;
  683. struct sys_mmap_args cma;
  684. linux_to_bsd_mmap_args(&cma, uap);
  685. SCARG(&cma, pos) = ((off_t)SCARG(uap, offset)) << PAGE_SHIFT;
  686. return sys_mmap(p, &cma, retval);
  687. }
  688. void
  689. linux_to_bsd_mmap_args(cma, uap)
  690. struct sys_mmap_args *cma;
  691. const struct linux_sys_mmap2_args *uap;
  692. {
  693. int flags = 0, fl = SCARG(uap, flags);
  694. flags |= cvtto_bsd_mask(fl, LINUX_MAP_SHARED, MAP_SHARED);
  695. flags |= cvtto_bsd_mask(fl, LINUX_MAP_PRIVATE, MAP_PRIVATE);
  696. flags |= cvtto_bsd_mask(fl, LINUX_MAP_FIXED, MAP_FIXED);
  697. flags |= cvtto_bsd_mask(fl, LINUX_MAP_ANON, MAP_ANON);
  698. /* XXX XAX ERH: Any other flags here? There are more defined... */
  699. SCARG(cma, addr) = (void *)SCARG(uap, addr);
  700. SCARG(cma, len) = SCARG(uap, len);
  701. SCARG(cma, prot) = SCARG(uap, prot);
  702. if (SCARG(cma, prot) & PROT_WRITE) /* XXX */
  703. SCARG(cma, prot) |= PROT_READ;
  704. SCARG(cma, flags) = flags;
  705. SCARG(cma, fd) = flags & MAP_ANON ? -1 : SCARG(uap, fd);
  706. SCARG(cma, pad) = 0;
  707. }
  708. int
  709. linux_sys_mremap(p, v, retval)
  710. struct proc *p;
  711. void *v;
  712. register_t *retval;
  713. {
  714. struct linux_sys_mremap_args /* {
  715. syscallarg(void *) old_address;
  716. syscallarg(size_t) old_size;
  717. syscallarg(size_t) new_size;
  718. syscallarg(u_long) flags;
  719. } */ *uap = v;
  720. struct sys_munmap_args mua;
  721. size_t old_size, new_size;
  722. int error;
  723. old_size = round_page(SCARG(uap, old_size));
  724. new_size = round_page(SCARG(uap, new_size));
  725. /*
  726. * Growing mapped region.
  727. */
  728. if (new_size > old_size) {
  729. /*
  730. * XXX Implement me. What we probably want to do is
  731. * XXX dig out the guts of the old mapping, mmap that
  732. * XXX object again with the new size, then munmap
  733. * XXX the old mapping.
  734. */
  735. *retval = 0;
  736. return (ENOMEM);
  737. }
  738. /*
  739. * Shrinking mapped region.
  740. */
  741. if (new_size < old_size) {
  742. SCARG(&mua, addr) = (caddr_t)SCARG(uap, old_address) + new_size;
  743. SCARG(&mua, len) = old_size - new_size;
  744. error = sys_munmap(p, &mua, retval);
  745. *retval = error ? 0 : (register_t)SCARG(uap, old_address);
  746. return (error);
  747. }
  748. /*
  749. * No change.
  750. */
  751. *retval = (register_t)SCARG(uap, old_address);
  752. return (0);
  753. }
  754. /*
  755. * This code is partly stolen from src/lib/libc/gen/times.c
  756. * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
  757. */
  758. #define CLK_TCK 100
  759. #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
  760. int
  761. linux_sys_times(p, v, retval)
  762. struct proc *p;
  763. void *v;
  764. register_t *retval;
  765. {
  766. struct linux_sys_times_args /* {
  767. syscallarg(struct linux_tms *) tms;
  768. } */ *uap = v;
  769. struct timeval t, ut, st;
  770. struct linux_tms ltms;
  771. time_t ticks;
  772. int error;
  773. calcru(&p->p_p->ps_tu, &ut, &st, NULL);
  774. ticks = CONVTCK(ut);
  775. if (ticks > LINUX_TIME_MAX)
  776. return EOVERFLOW;
  777. ltms.ltms_utime = (linux_clock_t)ticks;
  778. ticks = CONVTCK(st);
  779. if (ticks > LINUX_TIME_MAX)
  780. return EOVERFLOW;
  781. ltms.ltms_stime = (linux_clock_t)ticks;
  782. ticks = CONVTCK(p->p_p->ps_cru.ru_utime);
  783. if (ticks > LINUX_TIME_MAX)
  784. return EOVERFLOW;
  785. ltms.ltms_cutime = (linux_clock_t)ticks;
  786. ticks = CONVTCK(p->p_p->ps_cru.ru_stime);
  787. if (ticks > LINUX_TIME_MAX)
  788. return EOVERFLOW;
  789. ltms.ltms_cstime = (linux_clock_t)ticks;
  790. if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
  791. return error;
  792. microuptime(&t);
  793. retval[0] = ((linux_clock_t)(CONVTCK(t)));
  794. return 0;
  795. }
  796. /*
  797. * Alarm. This is a libc call which uses setitimer(2) in OpenBSD.
  798. * Fiddle with the timers to make it work.
  799. */
  800. int
  801. linux_sys_alarm(p, v, retval)
  802. struct proc *p;
  803. void *v;
  804. register_t *retval;
  805. {
  806. struct linux_sys_alarm_args /* {
  807. syscallarg(unsigned int) secs;
  808. } */ *uap = v;
  809. struct process *pr;
  810. struct itimerval *itp, it;
  811. struct timeval tv;
  812. int s;
  813. int timo;
  814. linux_time_t seconds_due = 0;
  815. pr = p->p_p;
  816. itp = &pr->ps_timer[ITIMER_REAL];
  817. s = splclock();
  818. /*
  819. * Clear any pending timer alarms.
  820. */
  821. getmicrouptime(&tv);
  822. timeout_del(&pr->ps_realit_to);
  823. timerclear(&itp->it_interval);
  824. if (timerisset(&itp->it_value) &&
  825. timercmp(&itp->it_value, &tv, >))
  826. timersub(&itp->it_value, &tv, &itp->it_value);
  827. /*
  828. * Return how many seconds were left (rounded up)
  829. */
  830. if (itp->it_value.tv_sec > LINUX_TIME_MAX) {
  831. splx(s);
  832. return EOVERFLOW;
  833. }
  834. seconds_due = (linux_time_t)itp->it_value.tv_sec;
  835. if (itp->it_value.tv_usec) {
  836. if (seconds_due == LINUX_TIME_MAX) {
  837. splx(s);
  838. return EOVERFLOW;
  839. }
  840. seconds_due++;
  841. }
  842. retval[0] = seconds_due;
  843. /*
  844. * alarm(0) just resets the timer.
  845. */
  846. if (SCARG(uap, secs) == 0) {
  847. timerclear(&itp->it_value);
  848. splx(s);
  849. return 0;
  850. }
  851. /*
  852. * Check the new alarm time for sanity, and set it.
  853. */
  854. timerclear(&it.it_interval);
  855. it.it_value.tv_sec = SCARG(uap, secs);
  856. it.it_value.tv_usec = 0;
  857. if (itimerfix(&it.it_value)) {
  858. splx(s);
  859. return (EINVAL);
  860. }
  861. if (timerisset(&it.it_value)) {
  862. timo = tvtohz(&it.it_value);
  863. timeradd(&it.it_value, &tv, &it.it_value);
  864. timeout_add(&pr->ps_realit_to, timo);
  865. }
  866. pr->ps_timer[ITIMER_REAL] = it;
  867. splx(s);
  868. return 0;
  869. }
  870. /*
  871. * utime(). Do conversion to things that utimes() understands,
  872. * and pass it on.
  873. */
  874. int
  875. linux_sys_utime(p, v, retval)
  876. struct proc *p;
  877. void *v;
  878. register_t *retval;
  879. {
  880. struct linux_sys_utime_args /* {
  881. syscallarg(char *) path;
  882. syscallarg(struct linux_utimbuf *)times;
  883. } */ *uap = v;
  884. caddr_t sg;
  885. int error;
  886. struct sys_utimes_args ua;
  887. struct timeval tv[2], *tvp;
  888. struct linux_utimbuf lut;
  889. sg = stackgap_init(p);
  890. tvp = (struct timeval *) stackgap_alloc(&sg, sizeof(tv));
  891. LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
  892. SCARG(&ua, path) = SCARG(uap, path);
  893. if (SCARG(uap, times) != NULL) {
  894. if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
  895. return error;
  896. tv[0].tv_usec = tv[1].tv_usec = 0;
  897. tv[0].tv_sec = lut.l_actime;
  898. tv[1].tv_sec = lut.l_modtime;
  899. if ((error = copyout(tv, tvp, sizeof tv)))
  900. return error;
  901. SCARG(&ua, tptr) = tvp;
  902. }
  903. else
  904. SCARG(&ua, tptr) = NULL;
  905. return sys_utimes(p, &ua, retval);
  906. }
  907. /*
  908. * The old Linux readdir was only able to read one entry at a time,
  909. * even though it had a 'count' argument. In fact, the emulation
  910. * of the old call was better than the original, because it did handle
  911. * the count arg properly. Don't bother with it anymore now, and use
  912. * it to distinguish between old and new. The difference is that the
  913. * newer one actually does multiple entries, and the reclen field
  914. * really is the reclen, not the namelength.
  915. */
  916. int
  917. linux_sys_readdir(p, v, retval)
  918. struct proc *p;
  919. void *v;
  920. register_t *retval;
  921. {
  922. struct linux_sys_readdir_args /* {
  923. syscallarg(int) fd;
  924. syscallarg(struct linux_dirent *) dent;
  925. syscallarg(unsigned int) count;
  926. } */ *uap = v;
  927. SCARG(uap, count) = 1;
  928. return linux_sys_getdents(p, uap, retval);
  929. }
  930. /*
  931. * Linux 'readdir' call. This code is mostly taken from the
  932. * SunOS getdents call (see compat/sunos/sunos_misc.c), though
  933. * an attempt has been made to keep it a little cleaner (failing
  934. * miserably, because of the cruft needed if count 1 is passed).
  935. *
  936. * The d_off field should contain the offset of the next valid entry,
  937. * but in Linux it has the offset of the entry itself. We emulate
  938. * that bug here.
  939. *
  940. * Read in BSD-style entries, convert them, and copy them out.
  941. *
  942. * Note that this doesn't handle union-mounted filesystems.
  943. */
  944. int linux_readdir_callback(void *, struct dirent *);
  945. struct linux_readdir_callback_args {
  946. caddr_t outp;
  947. int resid;
  948. int oldcall;
  949. int is64bit;
  950. };
  951. int
  952. linux_readdir_callback(arg, bdp)
  953. void *arg;
  954. struct dirent *bdp;
  955. {
  956. struct linux_dirent64 idb64;
  957. struct linux_dirent idb;
  958. struct linux_readdir_callback_args *cb = arg;
  959. int linux_reclen;
  960. int error;
  961. if (cb->oldcall == 2)
  962. return (ENOMEM);
  963. linux_reclen = (cb->is64bit) ?
  964. LINUX_RECLEN(&idb64, bdp->d_namlen) :
  965. LINUX_RECLEN(&idb, bdp->d_namlen);
  966. if (cb->resid < linux_reclen)
  967. return (ENOMEM);
  968. if (cb->is64bit) {
  969. idb64.d_ino = (linux_ino64_t)bdp->d_fileno;
  970. idb64.d_off = (linux_off64_t)bdp->d_off;
  971. idb64.d_reclen = (u_short)linux_reclen;
  972. idb64.d_type = bdp->d_type;
  973. strlcpy(idb64.d_name, bdp->d_name, sizeof(idb64.d_name));
  974. error = copyout((caddr_t)&idb64, cb->outp, linux_reclen);
  975. } else {
  976. if (bdp->d_fileno > LINUX_INO_MAX)
  977. return EOVERFLOW;
  978. idb.d_ino = (linux_ino_t)bdp->d_fileno;
  979. if (cb->oldcall) {
  980. /*
  981. * The old readdir() call misuses the offset
  982. * and reclen fields.
  983. */
  984. idb.d_off = (linux_off_t)linux_reclen;
  985. idb.d_reclen = (u_short)bdp->d_namlen;
  986. } else {
  987. idb.d_off = (linux_off_t)bdp->d_off;
  988. idb.d_reclen = (u_short)linux_reclen;
  989. }
  990. strlcpy(idb.d_name, bdp->d_name, sizeof(idb.d_name));
  991. error = copyout((caddr_t)&idb, cb->outp, linux_reclen);
  992. }
  993. if (error)
  994. return (error);
  995. /* advance output past Linux-shaped entry */
  996. cb->outp += linux_reclen;
  997. cb->resid -= linux_reclen;
  998. if (cb->oldcall == 1)
  999. ++cb->oldcall;
  1000. return (0);
  1001. }
  1002. int
  1003. linux_sys_getdents64(p, v, retval)
  1004. struct proc *p;
  1005. void *v;
  1006. register_t *retval;
  1007. {
  1008. return getdents_common(p, v, retval, 1);
  1009. }
  1010. int
  1011. linux_sys_getdents(p, v, retval)
  1012. struct proc *p;
  1013. void *v;
  1014. register_t *retval;
  1015. {
  1016. return getdents_common(p, v, retval, 0);
  1017. }
  1018. int
  1019. getdents_common(p, v, retval, is64bit)
  1020. struct proc *p;
  1021. void *v;
  1022. register_t *retval;
  1023. int is64bit;
  1024. {
  1025. struct linux_sys_getdents_args /* {
  1026. syscallarg(int) fd;
  1027. syscallarg(void *) dirent;
  1028. syscallarg(unsigned) count;
  1029. } */ *uap = v;
  1030. struct linux_readdir_callback_args args;
  1031. struct file *fp;
  1032. int error;
  1033. int nbytes = SCARG(uap, count);
  1034. if ((error = getvnode(p, SCARG(uap, fd), &fp)) != 0)
  1035. return (error);
  1036. if (nbytes == 1) { /* emulating old, broken behaviour */
  1037. /* readdir(2) case. Always struct dirent. */
  1038. if (is64bit) {
  1039. FRELE(fp, p);
  1040. return (EINVAL);
  1041. }
  1042. nbytes = sizeof(struct linux_dirent);
  1043. args.oldcall = 1;
  1044. } else {
  1045. args.oldcall = 0;
  1046. }
  1047. args.resid = nbytes;
  1048. args.outp = (caddr_t)SCARG(uap, dirent);
  1049. args.is64bit = is64bit;
  1050. if ((error = readdir_with_callback(fp, &fp->f_offset, nbytes,
  1051. linux_readdir_callback, &args)) != 0)
  1052. goto exit;
  1053. *retval = nbytes - args.resid;
  1054. exit:
  1055. FRELE(fp, p);
  1056. return (error);
  1057. }
  1058. /*
  1059. * Not sure why the arguments to this older version of select() were put
  1060. * into a structure, because there are 5, and that can be handled all
  1061. * in registers on the i386 like Linux wants to.
  1062. */
  1063. int
  1064. linux_sys_oldselect(p, v, retval)
  1065. struct proc *p;
  1066. void *v;
  1067. register_t *retval;
  1068. {
  1069. struct linux_sys_oldselect_args /* {
  1070. syscallarg(struct linux_select *) lsp;
  1071. } */ *uap = v;
  1072. struct linux_select ls;
  1073. int error;
  1074. if ((error = copyin(SCARG(uap, lsp), &ls, sizeof(ls))))
  1075. return error;
  1076. return linux_select1(p, retval, ls.nfds, ls.readfds, ls.writefds,
  1077. ls.exceptfds, ls.timeout);
  1078. }
  1079. /*
  1080. * Even when just using registers to pass arguments to syscalls you can
  1081. * have 5 of them on the i386. So this newer version of select() does
  1082. * this.
  1083. */
  1084. int
  1085. linux_sys_select(p, v, retval)
  1086. struct proc *p;
  1087. void *v;
  1088. register_t *retval;
  1089. {
  1090. struct linux_sys_select_args /* {
  1091. syscallarg(int) nfds;
  1092. syscallarg(fd_set *) readfds;
  1093. syscallarg(fd_set *) writefds;
  1094. syscallarg(fd_set *) exceptfds;
  1095. syscallarg(struct linux_timeval *) timeout;
  1096. } */ *uap = v;
  1097. return linux_select1(p, retval, SCARG(uap, nfds), SCARG(uap, readfds),
  1098. SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
  1099. }
  1100. /*
  1101. * Common code for the old and new versions of select(). A couple of
  1102. * things are important:
  1103. * 1) return the amount of time left in the 'timeout' parameter
  1104. * 2) select never returns ERESTART on Linux, always return EINTR
  1105. */
  1106. int
  1107. linux_select1(struct proc *p, register_t *retval, int nfds, fd_set *readfds,
  1108. fd_set *writefds, fd_set *exceptfds, struct linux_timeval *timeout)
  1109. {
  1110. struct sys_select_args bsa;
  1111. struct linux_timeval lutv;
  1112. struct timeval tv0, tv1, utv, *tvp;
  1113. caddr_t sg;
  1114. int error;
  1115. SCARG(&bsa, nd) = nfds;
  1116. SCARG(&bsa, in) = readfds;
  1117. SCARG(&bsa, ou) = writefds;
  1118. SCARG(&bsa, ex) = exceptfds;
  1119. /*
  1120. * Store current time for computation of the amount of
  1121. * time left.
  1122. */
  1123. if (timeout) {
  1124. if ((error = copyin(timeout, &lutv, sizeof(lutv))))
  1125. return error;
  1126. linux_to_bsd_timeval(&utv, &lutv);
  1127. if (itimerfix(&utv)) {
  1128. /*
  1129. * The timeval was invalid. Convert it to something
  1130. * valid that will act as it does under Linux.
  1131. */
  1132. utv.tv_sec += utv.tv_usec / 1000000;
  1133. utv.tv_usec %= 1000000;
  1134. if (utv.tv_usec < 0) {
  1135. utv.tv_sec -= 1;
  1136. utv.tv_usec += 1000000;
  1137. }
  1138. if (utv.tv_sec < 0)
  1139. timerclear(&utv);
  1140. }
  1141. sg = stackgap_init(p);
  1142. tvp = stackgap_alloc(&sg, sizeof(utv));
  1143. if ((error = copyout(&utv, tvp, sizeof(utv))))
  1144. return error;
  1145. SCARG(&bsa, tv) = tvp;
  1146. microtime(&tv0);
  1147. } else
  1148. SCARG(&bsa, tv) = NULL;
  1149. error = sys_select(p, &bsa, retval);
  1150. if (error) {
  1151. /*
  1152. * See fs/select.c in the Linux kernel. Without this,
  1153. * Maelstrom doesn't work.
  1154. */
  1155. if (error == ERESTART)
  1156. error = EINTR;
  1157. else if (error != EINTR)
  1158. return error;
  1159. }
  1160. if (timeout) {
  1161. if (*retval) {
  1162. /*
  1163. * Compute how much time was left of the timeout,
  1164. * by subtracting the current time and the time
  1165. * before we started the call, and subtracting
  1166. * that result from the user-supplied value.
  1167. */
  1168. microtime(&tv1);
  1169. timersub(&tv1, &tv0, &tv1);
  1170. timersub(&utv, &tv1, &utv);
  1171. if (utv.tv_sec < 0)
  1172. timerclear(&utv);
  1173. } else
  1174. timerclear(&utv);
  1175. bsd_to_linux_timeval(&lutv, &utv); /* can't fail */
  1176. if ((error = copyout(&lutv, timeout, sizeof(lutv))))
  1177. return error;
  1178. }
  1179. return (error);
  1180. }
  1181. /*
  1182. * Get the process group of a certain process. Look it up
  1183. * and return the value.
  1184. */
  1185. int
  1186. linux_sys_getpgid(p, v, retval)
  1187. struct proc *p;
  1188. void *v;
  1189. register_t *retval;
  1190. {
  1191. struct linux_sys_getpgid_args /* {
  1192. syscallarg(int) pid;
  1193. } */ *uap = v;
  1194. struct process *targpr;
  1195. if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_p->ps_pid) {
  1196. if ((targpr = prfind(SCARG(uap, pid))) == 0)
  1197. return ESRCH;
  1198. }
  1199. else
  1200. targpr = p->p_p;
  1201. retval[0] = targpr->ps_pgid;
  1202. return 0;
  1203. }
  1204. /*
  1205. * Set the 'personality' (emulation mode) for the current process. Only
  1206. * accept the Linux personality here (0). This call is needed because
  1207. * the Linux ELF crt0 issues it in an ugly kludge to make sure that
  1208. * ELF binaries run in Linux mode, not SVR4 mode.
  1209. */
  1210. int
  1211. linux_sys_personality(p, v, retval)
  1212. struct proc *p;
  1213. void *v;
  1214. register_t *retval;
  1215. {
  1216. struct linux_sys_personality_args /* {
  1217. syscallarg(int) per;
  1218. } */ *uap = v;
  1219. if (SCARG(uap, per) != 0)
  1220. return EINVAL;
  1221. retval[0] = 0;
  1222. return 0;
  1223. }
  1224. /*
  1225. * The calls are here because of type conversions.
  1226. */
  1227. int
  1228. linux_sys_setreuid16(p, v, retval)
  1229. struct proc *p;
  1230. void *v;
  1231. register_t *retval;
  1232. {
  1233. struct linux_sys_setreuid16_args /* {
  1234. syscallarg(int) ruid;
  1235. syscallarg(int) euid;
  1236. } */ *uap = v;
  1237. struct sys_setreuid_args bsa;
  1238. SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
  1239. (uid_t)-1 : SCARG(uap, ruid);
  1240. SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
  1241. (uid_t)-1 : SCARG(uap, euid);
  1242. return sys_setreuid(p, &bsa, retval);
  1243. }
  1244. int
  1245. linux_sys_setregid16(p, v, retval)
  1246. struct proc *p;
  1247. void *v;
  1248. register_t *retval;
  1249. {
  1250. struct linux_sys_setregid16_args /* {
  1251. syscallarg(int) rgid;
  1252. syscallarg(int) egid;
  1253. } */ *uap = v;
  1254. struct sys_setregid_args bsa;
  1255. SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
  1256. (uid_t)-1 : SCARG(uap, rgid);
  1257. SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
  1258. (uid_t)-1 : SCARG(uap, egid);
  1259. return sys_setregid(p, &bsa, retval);
  1260. }
  1261. int
  1262. linux_sys___sysctl(p, v, retval)
  1263. struct proc *p;
  1264. void *v;
  1265. register_t *retval;
  1266. {
  1267. struct linux_sys___sysctl_args /* {
  1268. syscallarg(struct linux___sysctl *) lsp;
  1269. } */ *uap = v;
  1270. struct linux___sysctl ls;
  1271. struct sys___sysctl_args bsa;
  1272. int error;
  1273. if ((error = copyin(SCARG(uap, lsp), &ls, sizeof ls)))
  1274. return error;
  1275. SCARG(&bsa, name) = ls.name;
  1276. SCARG(&bsa, namelen) = ls.namelen;
  1277. SCARG(&bsa, old) = ls.old;
  1278. SCARG(&bsa, oldlenp) = ls.oldlenp;
  1279. SCARG(&bsa, new) = ls.new;
  1280. SCARG(&bsa, newlen) = ls.newlen;
  1281. return sys___sysctl(p, &bsa, retval);
  1282. }
  1283. /*
  1284. * We have nonexistent fsuid equal to uid.
  1285. * If modification is requested, refuse.
  1286. */
  1287. int
  1288. linux_sys_setfsuid(p, v, retval)
  1289. struct proc *p;
  1290. void *v;
  1291. register_t *retval;
  1292. {
  1293. struct linux_sys_setfsuid_args /* {
  1294. syscallarg(uid_t) uid;
  1295. } */ *uap = v;
  1296. uid_t uid;
  1297. uid = SCARG(uap, uid);
  1298. if (p->p_ucred->cr_ruid != uid)
  1299. return sys_nosys(p, v, retval);
  1300. else
  1301. return (0);
  1302. }
  1303. int
  1304. linux_sys_getfsuid(p, v, retval)
  1305. struct proc *p;
  1306. void *v;
  1307. register_t *retval;
  1308. {
  1309. return sys_getuid(p, v, retval);
  1310. }
  1311. int
  1312. linux_sys_nice(p, v, retval)
  1313. struct proc *p;
  1314. void *v;
  1315. register_t *retval;
  1316. {
  1317. struct linux_sys_nice_args /* {
  1318. syscallarg(int) incr;
  1319. } */ *uap = v;
  1320. struct sys_setpriority_args bsa;
  1321. SCARG(&bsa, which) = PRIO_PROCESS;
  1322. SCARG(&bsa, who) = 0;
  1323. SCARG(&bsa, prio) = SCARG(uap, incr);
  1324. return sys_setpriority(p, &bsa, retval);
  1325. }
  1326. int
  1327. linux_sys_getpid(p, v, retval)
  1328. struct proc *p;
  1329. void *v;
  1330. register_t *retval;
  1331. {
  1332. *retval = p->p_p->ps_pid;
  1333. return (0);
  1334. }
  1335. linux_pid_t
  1336. linux_sys_gettid(struct proc *p, void *v, register_t *retval)
  1337. {
  1338. *retval = p->p_pid + THREAD_PID_OFFSET;
  1339. return (0);
  1340. }
  1341. int
  1342. linux_sys_getuid(p, v, retval)
  1343. struct proc *p;
  1344. void *v;
  1345. register_t *retval;
  1346. {
  1347. *retval = p->p_ucred->cr_ruid;
  1348. return (0);
  1349. }
  1350. int
  1351. linux_sys_getgid(p, v, retval)
  1352. struct proc *p;
  1353. void *v;
  1354. register_t *retval;
  1355. {
  1356. *retval = p->p_ucred->cr_rgid;
  1357. return (0);
  1358. }
  1359. /*
  1360. * sysinfo()
  1361. */
  1362. /* ARGSUSED */
  1363. int
  1364. linux_sys_sysinfo(p, v, retval)
  1365. struct proc *p;
  1366. void *v;
  1367. register_t *retval;
  1368. {
  1369. struct linux_sys_sysinfo_args /* {
  1370. syscallarg(struct linux_sysinfo *) sysinfo;
  1371. } */ *uap = v;
  1372. struct linux_sysinfo si;
  1373. struct loadavg *la;
  1374. extern long bufpages;
  1375. struct timeval tv;
  1376. getmicrouptime(&tv);
  1377. if (tv.tv_sec > LINUX_TIME_MAX)
  1378. return EOVERFLOW;
  1379. si.uptime = (linux_time_t)tv.tv_sec;
  1380. la = &averunnable;
  1381. si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
  1382. si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
  1383. si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
  1384. si.totalram = ptoa(physmem);
  1385. si.freeram = uvmexp.free * uvmexp.pagesize;
  1386. si.sharedram = 0;/* XXX */
  1387. si.bufferram = bufpages * PAGE_SIZE;
  1388. si.totalswap = uvmexp.swpages * PAGE_SIZE;
  1389. si.freeswap = (uvmexp.swpages - uvmexp.swpginuse) * PAGE_SIZE;
  1390. si.procs = nthreads;
  1391. /* The following are only present in newer Linux kernels. */
  1392. si.totalbig = 0;
  1393. si.freebig = 0;
  1394. si.mem_unit = 1;
  1395. return (copyout(&si, SCARG(uap, sysinfo), sizeof(si)));
  1396. }
  1397. int
  1398. linux_sys_mprotect(struct proc *p, void *v, register_t *retval)
  1399. {
  1400. struct sys_mprotect_args *uap = v;
  1401. if (SCARG(uap, prot) & (PROT_WRITE | PROT_EXEC))
  1402. SCARG(uap, prot) |= PROT_READ;
  1403. return (sys_mprotect(p, uap, retval));
  1404. }
  1405. int
  1406. linux_sys_setdomainname(struct proc *p, void *v, register_t *retval)
  1407. {
  1408. struct linux_sys_setdomainname_args *uap = v;
  1409. int error, mib[1];
  1410. if ((error = suser(p, 0)))
  1411. return (error);
  1412. mib[0] = KERN_DOMAINNAME;
  1413. return (kern_sysctl(mib, 1, NULL, NULL, SCARG(uap, name),
  1414. SCARG(uap, len), p));
  1415. }
  1416. int
  1417. linux_sys_swapon(struct proc *p, void *v, register_t *retval)
  1418. {
  1419. struct sys_swapctl_args ua;
  1420. struct linux_sys_swapon_args /* {
  1421. syscallarg(const char *) name;
  1422. } */ *uap = v;
  1423. SCARG(&ua, cmd) = SWAP_ON;
  1424. SCARG(&ua, arg) = (void *)SCARG(uap, name);
  1425. SCARG(&ua, misc) = 0; /* priority */
  1426. return (sys_swapctl(p, &ua, retval));
  1427. }
  1428. int
  1429. linux_sys_prctl(struct proc *p, void *v, register_t *retval)
  1430. {
  1431. int error = 0, max_size, pdeath_signal;
  1432. char comm[LINUX_MAX_COMM_LEN];
  1433. struct linux_emuldata *ed = (struct linux_emuldata*)p->p_emuldata;
  1434. struct linux_sys_prctl_args /* {
  1435. int option;
  1436. unsigned long arg2;
  1437. unsigned long arg3;
  1438. unsigned long arg4;
  1439. unsigned long arg5;
  1440. } */ *uap = v;
  1441. switch (SCARG(uap, option)) {
  1442. case LINUX_PR_SET_PDEATHSIG:
  1443. if (SCARG(uap, arg2) >= LINUX__NSIG)
  1444. return (EINVAL);
  1445. ed->pdeath_signal = SCARG(uap, arg2);
  1446. break;
  1447. case LINUX_PR_GET_PDEATHSIG:
  1448. pdeath_signal = ed->pdeath_signal;
  1449. error = copyout(&pdeath_signal, (void *)SCARG(uap, arg2),
  1450. sizeof(pdeath_signal));
  1451. break;
  1452. case LINUX_PR_GET_KEEPCAPS:
  1453. /*
  1454. * Indicate that we always clear the effective and
  1455. * permitted capability sets when the user id becomes
  1456. * non-zero (actually the capability sets are simply
  1457. * always zero in the current implementation).
  1458. */
  1459. *retval = 0;
  1460. break;
  1461. case LINUX_PR_SET_KEEPCAPS:
  1462. /*
  1463. * Ignore requests to keep the effective and permitted
  1464. * capability sets when the user id becomes non-zero.
  1465. */
  1466. break;
  1467. case LINUX_PR_SET_NAME:
  1468. /*
  1469. * To be on the safe side we need to make sure not to
  1470. * overflow the size a linux program expects. We already
  1471. * do this here in the copyin, so that we don't need to
  1472. * check on copyout.
  1473. */
  1474. max_size = MIN(sizeof(comm), sizeof(p->p_comm));
  1475. error = copyinstr((void *)SCARG(uap, arg2), comm,
  1476. max_size, NULL);
  1477. /* Linux silently truncates the name if it is too long. */
  1478. if (error == ENAMETOOLONG) {
  1479. /*
  1480. * XXX: copyinstr() isn't documented to populate the
  1481. * array completely, so do a copyin() to be on the
  1482. * safe side. This should be changed in case copyinstr()
  1483. * is changed to guarantee this.
  1484. */
  1485. error = copyin((void *)SCARG(uap, arg2), comm,
  1486. max_size - 1);
  1487. comm[max_size - 1] = '\0';
  1488. }
  1489. if (error)
  1490. return (error);
  1491. strlcpy(p->p_comm, comm, sizeof(p->p_comm));
  1492. break;
  1493. case LINUX_PR_GET_NAME:
  1494. strlcpy(comm, p->p_comm, sizeof(comm));
  1495. error = copyout(comm, (void *)SCARG(uap, arg2),
  1496. strlen(comm) + 1);
  1497. break;
  1498. default:
  1499. printf("linux_sys_prctl: unsupported option %d\n",
  1500. SCARG(uap, option));
  1501. error = EINVAL;
  1502. break;
  1503. }
  1504. return (error);
  1505. }