shim_ioctl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_ioctl.c
  17. *
  18. * Implementation of system call "ioctl".
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_table.h>
  22. #include <shim_thread.h>
  23. #include <shim_handle.h>
  24. #include <shim_fs.h>
  25. #include <pal.h>
  26. #include <pal_error.h>
  27. #include <asm/unistd.h>
  28. #include <errno.h>
  29. #include <asm/ioctl.h>
  30. #include <asm/ioctls.h>
  31. #include <asm/termios.h>
  32. #include <asm/termbits.h>
  33. #include <linux/fd.h>
  34. #include <linux/sockios.h>
  35. #define TERM_DEFAULT_IFLAG (ICRNL|IUTF8)
  36. #define TERM_DEFAULT_OFLAG (OPOST|ONLCR)
  37. #define TERM_DEFAULT_CFLAG (B38400|CS8|CREAD)
  38. #define TERM_DEFAULT_LFLAG (ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN)
  39. static int ioctl_termios (struct shim_handle * hdl, unsigned int cmd,
  40. unsigned long arg)
  41. {
  42. if (hdl->type != TYPE_FILE ||
  43. hdl->info.file.type != FILE_TTY)
  44. return -ENOTTY;
  45. if (!arg)
  46. return -EINVAL;
  47. switch(cmd) {
  48. /* <include/asm/termios.h> */
  49. case TIOCGPGRP:
  50. *(int *) arg = get_cur_thread()->pgid;
  51. return 0;
  52. case TIOCSPGRP:
  53. return -EINVAL;
  54. case TCGETS: {
  55. #if 0
  56. struct termios * termios = (struct termios *) arg;
  57. termios->c_iflag = TERM_DEFAULT_IFLAG;
  58. termios->c_oflag = TERM_DEFAULT_OFLAG;
  59. termios->c_cflag = TERM_DEFAULT_CFLAG;
  60. termios->c_lflag = TERM_DEFAULT_LFLAG;
  61. return 0;
  62. #endif
  63. return -EINVAL;
  64. }
  65. case TCSETS:
  66. case TCSETSW:
  67. case TCSETSF:
  68. return -EINVAL;
  69. /* 0x00005405 TCGETA struct termio * */
  70. case TCGETA:
  71. /* 0x00005406 TCSETA const struct termio * */
  72. case TCSETA:
  73. /* 0x00005407 TCSETAW const struct termio * */
  74. case TCSETAW:
  75. /* 0x00005408 TCSETAF const struct termio * */
  76. case TCSETAF:
  77. /* 0x00005409 TCSBRK int */
  78. case TCSBRK:
  79. /* 0x0000540A TCXONC int */
  80. case TCXONC:
  81. /* 0x0000540B TCFLSH int */
  82. case TCFLSH:
  83. /* 0x0000540C TIOCEXCL void */
  84. case TIOCEXCL:
  85. /* 0x0000540D TIOCNXCL void */
  86. case TIOCNXCL:
  87. /* 0x0000540E TIOCSCTTY int */
  88. case TIOCSCTTY:
  89. /* 0x0000540F TIOCGPGRP pid_t * */
  90. case TIOCOUTQ:
  91. /* 0x00005412 TIOCSTI const char * */
  92. case TIOCSTI:
  93. /* 0x00005413 TIOCGWINSZ struct winsize * */
  94. case TIOCGWINSZ:
  95. /* 0x00005415 TIOCMGET int * */
  96. case TIOCMGET:
  97. /* 0x00005416 TIOCMBIS const int * */
  98. case TIOCMBIS:
  99. /* 0x00005417 TIOCMBIC const int * */
  100. case TIOCMBIC:
  101. /* 0x00005418 TIOCMSET const int * */
  102. case TIOCMSET:
  103. /* 0x00005419 TIOCGSOFTCAR int * */
  104. case TIOCGSOFTCAR:
  105. /* 0x0000541A TIOCSSOFTCAR const int * */
  106. case TIOCSSOFTCAR:
  107. /* 0x0000541B FIONREAD int / TIOCINQ int * */
  108. case TIOCINQ:
  109. /* 0x0000541C TIOCLINUX const char * */
  110. case TIOCLINUX:
  111. /* 0x0000541D TIOCCONS void */
  112. case TIOCCONS:
  113. /* 0x0000541E TIOCGSERIAL struct serial_struct * */
  114. case TIOCGSERIAL:
  115. /* 0x0000541F TIOCSSERIAL const struct serial_struct * */
  116. case TIOCSSERIAL:
  117. /* 0x00005420 TIOCPKT const int * */
  118. case TIOCPKT:
  119. /* 0x00005421 FIONBIO const int * */
  120. case FIONBIO:
  121. /* 0x00005422 TIOCNOTTY void */
  122. case TIOCNOTTY:
  123. /* 0x00005423 TIOCSETD const int * */
  124. case TIOCSETD:
  125. /* 0x00005424 TIOCGETD int * */
  126. case TIOCGETD:
  127. /* 0x00005425 TCSBRKP int */
  128. case TCSBRKP:
  129. /* 0x00005452 FIOASYNC const int * */
  130. case FIOASYNC:
  131. /* 0x00005453 TIOCSERCONFIG void */
  132. case TIOCSERCONFIG:
  133. /* 0x00005454 TIOCSERGWILD int * */
  134. case TIOCSERGWILD:
  135. /* 0x00005455 TIOCSERSWILD const int * */
  136. case TIOCSERSWILD:
  137. /* 0x00005456 TIOCGLCKTRMIOS struct termios * */
  138. case TIOCGLCKTRMIOS:
  139. /* 0x00005457 TIOCSLCKTRMIOS const struct termios * */
  140. case TIOCSLCKTRMIOS:
  141. /* 0x00005458 TIOCSERGSTRUCT struct async_struct * */
  142. case TIOCSERGSTRUCT:
  143. /* 0x00005459 TIOCSERGETLSR int * */
  144. case TIOCSERGETLSR:
  145. /* 0x0000545A TIOCSERGETMULTI struct serial_multiport_struct * */
  146. case TIOCSERGETMULTI:
  147. /* 0x0000545B TIOCSERSETMULTI const struct serial_multiport_struct * */
  148. case TIOCSERSETMULTI:
  149. default:
  150. goto passthrough;
  151. }
  152. passthrough:
  153. return -EAGAIN;
  154. }
  155. static int ioctl_fd (struct shim_handle * hdl, unsigned int cmd,
  156. unsigned long arg)
  157. {
  158. switch(cmd) {
  159. /* <include/linux/fd.h> */
  160. /* 0x00000000 FDCLRPRM void */
  161. case FDCLRPRM:
  162. /* 0x00000001 FDSETPRM const struct floppy_struct * */
  163. case FDSETPRM:
  164. /* 0x00000002 FDDEFPRM const struct floppy_struct * */
  165. case FDDEFPRM:
  166. /* 0x00000003 FDGETPRM struct floppy_struct * */
  167. case FDGETPRM:
  168. /* 0x00000004 FDMSGON void */
  169. case FDMSGON:
  170. /* 0x00000005 FDMSGOFF void */
  171. case FDMSGOFF:
  172. /* 0x00000006 FDFMTBEG void */
  173. case FDFMTBEG:
  174. /* 0x00000007 FDFMTTRK const struct format_descr * */
  175. case FDFMTTRK:
  176. /* 0x00000008 FDFMTEND void */
  177. case FDFMTEND:
  178. /* 0x0000000A FDSETEMSGTRESH int */
  179. case FDSETEMSGTRESH:
  180. /* 0x0000000B FDFLUSH void */
  181. case FDFLUSH:
  182. /* 0x0000000C FDSETMAXERRS const struct floppy_max_errors * */
  183. case FDSETMAXERRS:
  184. /* 0x0000000E FDGETMAXERRS struct floppy_max_errors * */
  185. case FDGETMAXERRS:
  186. /* 0x00000010 FDGETDRVTYP struct { char [16]; } * */
  187. case FDGETDRVTYP:
  188. /* 0x00000014 FDSETDRVPRM const struct floppy_drive_params * */
  189. case FDSETDRVPRM:
  190. /* 0x00000015 FDGETDRVPRM struct floppy_drive_params * */
  191. case FDGETDRVPRM:
  192. /* 0x00000016 FDGETDRVSTAT struct floppy_drive_struct * */
  193. case FDGETDRVSTAT:
  194. /* 0x00000017 FDPOLLDRVSTAT struct floppy_drive_struct * */
  195. case FDPOLLDRVSTAT:
  196. /* 0x00000018 FDRESET int */
  197. case FDRESET:
  198. /* 0x00000019 FDGETFDCSTAT struct floppy_fdc_state * */
  199. case FDGETFDCSTAT:
  200. /* 0x0000001B FDWERRORCLR void */
  201. case FDWERRORCLR:
  202. /* 0x0000001C FDWERRORGET struct floppy_write_errors * */
  203. case FDWERRORGET:
  204. /* 0x0000001E FDRAWCMD struct floppy_raw_cmd *floppy_raw_cmd */
  205. case FDRAWCMD:
  206. /* 0x00000028 FDTWADDLE void */
  207. case FDTWADDLE:
  208. default:
  209. goto passthrough;
  210. }
  211. passthrough:
  212. return -EAGAIN;
  213. }
  214. static int ioctl_netdevice (struct shim_handle * hdl, unsigned int cmd,
  215. unsigned long arg)
  216. {
  217. if (hdl->type != TYPE_SOCK)
  218. return -ENOTSOCK;
  219. struct shim_sock_handle * sock = &hdl->info.sock;
  220. if (sock->sock_state == SOCK_CREATED) {
  221. if (sock->sock_type == SOCK_STREAM)
  222. return -ENOTCONN;
  223. }
  224. switch(cmd) {
  225. /* Socket configuration controls. */
  226. case SIOCGIFNAME: /* 0x8910 get iface name */
  227. case SIOCSIFLINK: /* 0x8911 set iface channel */
  228. case SIOCGIFCONF: /* 0x8912 get iface list */
  229. case SIOCGIFFLAGS: /* 0x8913 get flags */
  230. case SIOCSIFFLAGS: /* 0x8914 set flags */
  231. case SIOCGIFADDR: /* 0x8915 get PA address */
  232. case SIOCSIFADDR: /* 0x8916 set PA address */
  233. case SIOCGIFDSTADDR: /* 0x8917 get remote PA address */
  234. case SIOCSIFDSTADDR: /* 0x8918 set remote PA address */
  235. case SIOCGIFBRDADDR: /* 0x8919 get broadcast PA address */
  236. case SIOCSIFBRDADDR: /* 0x891a set broadcast PA address */
  237. case SIOCGIFNETMASK: /* 0x891b get network PA mask */
  238. case SIOCSIFNETMASK: /* 0x891c set network PA mask */
  239. case SIOCGIFMETRIC: /* 0x891d get metric */
  240. case SIOCSIFMETRIC: /* 0x891e set metric */
  241. case SIOCGIFMEM: /* 0x891f get memory address (BSD) */
  242. case SIOCSIFMEM: /* 0x8920 set memory address (BSD) */
  243. case SIOCGIFMTU: /* 0x8921 get MTU size */
  244. case SIOCSIFMTU: /* 0x8922 set MTU size */
  245. case SIOCSIFNAME: /* 0x8923 set interface name */
  246. case SIOCSIFHWADDR: /* 0x8924 set hardware address */
  247. case SIOCGIFENCAP: /* 0x8925 get/set encapsulations */
  248. case SIOCSIFENCAP: /* 0x8926 */
  249. case SIOCGIFHWADDR: /* 0x8927 Get hardware address */
  250. case SIOCGIFSLAVE: /* 0x8929 Driver slaving support */
  251. case SIOCSIFSLAVE: /* 0x8930 */
  252. case SIOCADDMULTI: /* 0x8931 Multicast address lists */
  253. case SIOCDELMULTI: /* 0x8932 */
  254. case SIOCGIFINDEX: /* 0x8933 name -> if_index mapping */
  255. /* SIOGIFINDEX = SIOCGIFINDEX misprint compatibility :-) */
  256. case SIOCSIFPFLAGS: /* 0x8934 set/get extended flags set */
  257. case SIOCGIFPFLAGS: /* 0x8935 */
  258. case SIOCDIFADDR: /* 0x8936 delete PA address */
  259. case SIOCSIFHWBROADCAST: /* 0x8937 set hardware broadcast addr */
  260. case SIOCGIFCOUNT: /* 0x8938 get number of devices */
  261. case SIOCGIFBR: /* 0x8940 Bridging support */
  262. case SIOCSIFBR: /* 0x8941 Set bridging options */
  263. case SIOCGIFTXQLEN: /* 0x8942 Get the tx queue length */
  264. case SIOCSIFTXQLEN: /* 0x8943 Set the tx queue length */
  265. default:
  266. goto passthrough;
  267. }
  268. passthrough:
  269. return -EAGAIN;
  270. }
  271. int shim_do_ioctl (int fd, int cmd, unsigned long arg)
  272. {
  273. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  274. if (!hdl)
  275. return -EBADF;
  276. int ret = -EAGAIN;
  277. switch(cmd) {
  278. /* <include/asm/termios.h> */
  279. case TCGETS:
  280. case TCSETS:
  281. case TCSETSW:
  282. case TCSETSF:
  283. case TCGETA:
  284. case TCSETA:
  285. case TCSETAW:
  286. case TCSETAF:
  287. case TCSBRK:
  288. case TCXONC:
  289. case TCFLSH:
  290. case TIOCEXCL:
  291. case TIOCNXCL:
  292. case TIOCSCTTY:
  293. case TIOCGPGRP:
  294. case TIOCSPGRP:
  295. case TIOCOUTQ:
  296. case TIOCSTI:
  297. case TIOCGWINSZ:
  298. case TIOCMGET:
  299. case TIOCMBIS:
  300. case TIOCMBIC:
  301. case TIOCMSET:
  302. case TIOCGSOFTCAR:
  303. case TIOCSSOFTCAR:
  304. /* case TIOCINQ = FIONREAD */
  305. case TIOCLINUX:
  306. case TIOCCONS:
  307. case TIOCGSERIAL:
  308. case TIOCSSERIAL:
  309. case TIOCPKT:
  310. case FIONBIO:
  311. case TIOCNOTTY:
  312. case TIOCSETD:
  313. case TIOCGETD:
  314. case TCSBRKP:
  315. ret = ioctl_termios(hdl, cmd, arg);
  316. break;
  317. case FIONCLEX:
  318. hdl->flags &= ~FD_CLOEXEC;
  319. ret = 0;
  320. break;
  321. case FIOCLEX:
  322. hdl->flags |= FD_CLOEXEC;
  323. ret = 0;
  324. break;
  325. case FIOASYNC:
  326. case TIOCSERCONFIG:
  327. case TIOCSERGWILD:
  328. case TIOCSERSWILD:
  329. case TIOCGLCKTRMIOS:
  330. case TIOCSLCKTRMIOS:
  331. case TIOCSERGSTRUCT:
  332. case TIOCSERGETLSR:
  333. case TIOCSERGETMULTI:
  334. case TIOCSERSETMULTI:
  335. ret = ioctl_termios(hdl, cmd, arg);
  336. break;
  337. case FDCLRPRM:
  338. case FDSETPRM:
  339. case FDDEFPRM:
  340. case FDGETPRM:
  341. case FDMSGON:
  342. case FDMSGOFF:
  343. case FDFMTBEG:
  344. case FDFMTTRK:
  345. case FDFMTEND:
  346. case FDSETEMSGTRESH:
  347. case FDFLUSH:
  348. case FDSETMAXERRS:
  349. case FDGETMAXERRS:
  350. case FDGETDRVTYP:
  351. case FDSETDRVPRM:
  352. case FDGETDRVPRM:
  353. case FDGETDRVSTAT:
  354. case FDPOLLDRVSTAT:
  355. case FDRESET:
  356. case FDGETFDCSTAT:
  357. case FDWERRORCLR:
  358. case FDWERRORGET:
  359. case FDRAWCMD:
  360. case FDTWADDLE:
  361. ret = ioctl_fd(hdl, cmd, arg);
  362. break;
  363. case FIONREAD: {
  364. struct shim_mount * fs = hdl->fs;
  365. int size = 0;
  366. int offset = 0;
  367. if (!fs || !fs->fs_ops) {
  368. ret = -EACCES;
  369. break;
  370. }
  371. if (fs->fs_ops->hstat) {
  372. struct stat stat;
  373. ret = fs->fs_ops->hstat(hdl, &stat);
  374. if (ret < 0)
  375. break;
  376. size = stat.st_size;
  377. goto done_fioread;
  378. }
  379. if (hdl->pal_handle) {
  380. PAL_STREAM_ATTR attr;
  381. if (!DkStreamAttributesQuerybyHandle(hdl->pal_handle, &attr)) {
  382. ret = -PAL_ERRNO;
  383. break;
  384. }
  385. size = attr.pending_size;
  386. goto done_fioread;
  387. }
  388. done_fioread:
  389. if (fs->fs_ops->seek) {
  390. ret = fs->fs_ops->seek(hdl, 0, SEEK_CUR);
  391. if (ret < 0)
  392. break;
  393. offset = ret;
  394. }
  395. *(int *) arg = size - offset;
  396. ret = 0;
  397. break;
  398. }
  399. /* Socket configuration controls. */
  400. case SIOCGIFNAME: /* 0x8910 get iface name */
  401. case SIOCSIFLINK: /* 0x8911 set iface channel */
  402. case SIOCGIFCONF: /* 0x8912 get iface list */
  403. case SIOCGIFFLAGS: /* 0x8913 get flags */
  404. case SIOCSIFFLAGS: /* 0x8914 set flags */
  405. case SIOCGIFADDR: /* 0x8915 get PA address */
  406. case SIOCSIFADDR: /* 0x8916 set PA address */
  407. case SIOCGIFDSTADDR: /* 0x8917 get remote PA address */
  408. case SIOCSIFDSTADDR: /* 0x8918 set remote PA address */
  409. case SIOCGIFBRDADDR: /* 0x8919 get broadcast PA address */
  410. case SIOCSIFBRDADDR: /* 0x891a set broadcast PA address */
  411. case SIOCGIFNETMASK: /* 0x891b get network PA mask */
  412. case SIOCSIFNETMASK: /* 0x891c set network PA mask */
  413. case SIOCGIFMETRIC: /* 0x891d get metric */
  414. case SIOCSIFMETRIC: /* 0x891e set metric */
  415. case SIOCGIFMEM: /* 0x891f get memory address (BSD) */
  416. case SIOCSIFMEM: /* 0x8920 set memory address (BSD) */
  417. case SIOCGIFMTU: /* 0x8921 get MTU size */
  418. case SIOCSIFMTU: /* 0x8922 set MTU size */
  419. case SIOCSIFNAME: /* 0x8923 set interface name */
  420. case SIOCSIFHWADDR: /* 0x8924 set hardware address */
  421. case SIOCGIFENCAP: /* 0x8925 get/set encapsulations */
  422. case SIOCSIFENCAP: /* 0x8926 */
  423. case SIOCGIFHWADDR: /* 0x8927 Get hardware address */
  424. case SIOCGIFSLAVE: /* 0x8929 Driver slaving support */
  425. case SIOCSIFSLAVE: /* 0x8930 */
  426. case SIOCADDMULTI: /* 0x8931 Multicast address lists */
  427. case SIOCDELMULTI: /* 0x8932 */
  428. case SIOCGIFINDEX: /* 0x8933 name -> if_index mapping */
  429. /* SIOGIFINDEX = SIOCGIFINDEX misprint compatibility :-) */
  430. case SIOCSIFPFLAGS: /* 0x8934 set/get extended flags set */
  431. case SIOCGIFPFLAGS: /* 0x8935 */
  432. case SIOCDIFADDR: /* 0x8936 delete PA address */
  433. case SIOCSIFHWBROADCAST: /* 0x8937 set hardware broadcast addr */
  434. case SIOCGIFCOUNT: /* 0x8938 get number of devices */
  435. case SIOCGIFBR: /* 0x8940 Bridging support */
  436. case SIOCSIFBR: /* 0x8941 Set bridging options */
  437. case SIOCGIFTXQLEN: /* 0x8942 Get the tx queue length */
  438. case SIOCSIFTXQLEN: /* 0x8943 Set the tx queue length */
  439. ret = ioctl_netdevice(hdl, cmd, arg);
  440. break;
  441. default:
  442. ret = -ENOSYS;
  443. break;
  444. }
  445. put_handle(hdl);
  446. return ret;
  447. }