shim_ioctl.c 17 KB

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