shim_ioctl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser 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. /* 0x00005422 TIOCNOTTY void */
  120. case TIOCNOTTY:
  121. /* 0x00005423 TIOCSETD const int * */
  122. case TIOCSETD:
  123. /* 0x00005424 TIOCGETD int * */
  124. case TIOCGETD:
  125. /* 0x00005425 TCSBRKP int */
  126. case TCSBRKP:
  127. /* 0x00005453 TIOCSERCONFIG void */
  128. case TIOCSERCONFIG:
  129. /* 0x00005454 TIOCSERGWILD int * */
  130. case TIOCSERGWILD:
  131. /* 0x00005455 TIOCSERSWILD const int * */
  132. case TIOCSERSWILD:
  133. /* 0x00005456 TIOCGLCKTRMIOS struct termios * */
  134. case TIOCGLCKTRMIOS:
  135. /* 0x00005457 TIOCSLCKTRMIOS const struct termios * */
  136. case TIOCSLCKTRMIOS:
  137. /* 0x00005458 TIOCSERGSTRUCT struct async_struct * */
  138. case TIOCSERGSTRUCT:
  139. /* 0x00005459 TIOCSERGETLSR int * */
  140. case TIOCSERGETLSR:
  141. /* 0x0000545A TIOCSERGETMULTI struct serial_multiport_struct * */
  142. case TIOCSERGETMULTI:
  143. /* 0x0000545B TIOCSERSETMULTI const struct serial_multiport_struct * */
  144. case TIOCSERSETMULTI:
  145. default:
  146. goto passthrough;
  147. }
  148. passthrough:
  149. return -EAGAIN;
  150. }
  151. static int ioctl_fd (struct shim_handle * hdl, unsigned int cmd,
  152. unsigned long arg)
  153. {
  154. switch(cmd) {
  155. /* <include/linux/fd.h> */
  156. /* 0x00000000 FDCLRPRM void */
  157. case FDCLRPRM:
  158. /* 0x00000001 FDSETPRM const struct floppy_struct * */
  159. case FDSETPRM:
  160. /* 0x00000002 FDDEFPRM const struct floppy_struct * */
  161. case FDDEFPRM:
  162. /* 0x00000003 FDGETPRM struct floppy_struct * */
  163. case FDGETPRM:
  164. /* 0x00000004 FDMSGON void */
  165. case FDMSGON:
  166. /* 0x00000005 FDMSGOFF void */
  167. case FDMSGOFF:
  168. /* 0x00000006 FDFMTBEG void */
  169. case FDFMTBEG:
  170. /* 0x00000007 FDFMTTRK const struct format_descr * */
  171. case FDFMTTRK:
  172. /* 0x00000008 FDFMTEND void */
  173. case FDFMTEND:
  174. /* 0x0000000A FDSETEMSGTRESH int */
  175. case FDSETEMSGTRESH:
  176. /* 0x0000000B FDFLUSH void */
  177. case FDFLUSH:
  178. /* 0x0000000C FDSETMAXERRS const struct floppy_max_errors * */
  179. case FDSETMAXERRS:
  180. /* 0x0000000E FDGETMAXERRS struct floppy_max_errors * */
  181. case FDGETMAXERRS:
  182. /* 0x00000010 FDGETDRVTYP struct { char [16]; } * */
  183. case FDGETDRVTYP:
  184. /* 0x00000014 FDSETDRVPRM const struct floppy_drive_params * */
  185. case FDSETDRVPRM:
  186. /* 0x00000015 FDGETDRVPRM struct floppy_drive_params * */
  187. case FDGETDRVPRM:
  188. /* 0x00000016 FDGETDRVSTAT struct floppy_drive_struct * */
  189. case FDGETDRVSTAT:
  190. /* 0x00000017 FDPOLLDRVSTAT struct floppy_drive_struct * */
  191. case FDPOLLDRVSTAT:
  192. /* 0x00000018 FDRESET int */
  193. case FDRESET:
  194. /* 0x00000019 FDGETFDCSTAT struct floppy_fdc_state * */
  195. case FDGETFDCSTAT:
  196. /* 0x0000001B FDWERRORCLR void */
  197. case FDWERRORCLR:
  198. /* 0x0000001C FDWERRORGET struct floppy_write_errors * */
  199. case FDWERRORGET:
  200. /* 0x0000001E FDRAWCMD struct floppy_raw_cmd *floppy_raw_cmd */
  201. case FDRAWCMD:
  202. /* 0x00000028 FDTWADDLE void */
  203. case FDTWADDLE:
  204. default:
  205. goto passthrough;
  206. }
  207. passthrough:
  208. return -EAGAIN;
  209. }
  210. static int ioctl_netdevice (struct shim_handle * hdl, unsigned int cmd,
  211. unsigned long arg)
  212. {
  213. if (hdl->type != TYPE_SOCK)
  214. return -ENOTSOCK;
  215. struct shim_sock_handle * sock = &hdl->info.sock;
  216. if (sock->sock_state == SOCK_CREATED) {
  217. if (sock->sock_type == SOCK_STREAM)
  218. return -ENOTCONN;
  219. }
  220. switch(cmd) {
  221. /* Socket configuration controls. */
  222. case SIOCGIFNAME: /* 0x8910 get iface name */
  223. case SIOCSIFLINK: /* 0x8911 set iface channel */
  224. case SIOCGIFCONF: /* 0x8912 get iface list */
  225. case SIOCGIFFLAGS: /* 0x8913 get flags */
  226. case SIOCSIFFLAGS: /* 0x8914 set flags */
  227. case SIOCGIFADDR: /* 0x8915 get PA address */
  228. case SIOCSIFADDR: /* 0x8916 set PA address */
  229. case SIOCGIFDSTADDR: /* 0x8917 get remote PA address */
  230. case SIOCSIFDSTADDR: /* 0x8918 set remote PA address */
  231. case SIOCGIFBRDADDR: /* 0x8919 get broadcast PA address */
  232. case SIOCSIFBRDADDR: /* 0x891a set broadcast PA address */
  233. case SIOCGIFNETMASK: /* 0x891b get network PA mask */
  234. case SIOCSIFNETMASK: /* 0x891c set network PA mask */
  235. case SIOCGIFMETRIC: /* 0x891d get metric */
  236. case SIOCSIFMETRIC: /* 0x891e set metric */
  237. case SIOCGIFMEM: /* 0x891f get memory address (BSD) */
  238. case SIOCSIFMEM: /* 0x8920 set memory address (BSD) */
  239. case SIOCGIFMTU: /* 0x8921 get MTU size */
  240. case SIOCSIFMTU: /* 0x8922 set MTU size */
  241. case SIOCSIFNAME: /* 0x8923 set interface name */
  242. case SIOCSIFHWADDR: /* 0x8924 set hardware address */
  243. case SIOCGIFENCAP: /* 0x8925 get/set encapsulations */
  244. case SIOCSIFENCAP: /* 0x8926 */
  245. case SIOCGIFHWADDR: /* 0x8927 Get hardware address */
  246. case SIOCGIFSLAVE: /* 0x8929 Driver slaving support */
  247. case SIOCSIFSLAVE: /* 0x8930 */
  248. case SIOCADDMULTI: /* 0x8931 Multicast address lists */
  249. case SIOCDELMULTI: /* 0x8932 */
  250. case SIOCGIFINDEX: /* 0x8933 name -> if_index mapping */
  251. /* SIOGIFINDEX = SIOCGIFINDEX misprint compatibility :-) */
  252. case SIOCSIFPFLAGS: /* 0x8934 set/get extended flags set */
  253. case SIOCGIFPFLAGS: /* 0x8935 */
  254. case SIOCDIFADDR: /* 0x8936 delete PA address */
  255. case SIOCSIFHWBROADCAST: /* 0x8937 set hardware broadcast addr */
  256. case SIOCGIFCOUNT: /* 0x8938 get number of devices */
  257. case SIOCGIFBR: /* 0x8940 Bridging support */
  258. case SIOCSIFBR: /* 0x8941 Set bridging options */
  259. case SIOCGIFTXQLEN: /* 0x8942 Get the tx queue length */
  260. case SIOCSIFTXQLEN: /* 0x8943 Set the tx queue length */
  261. default:
  262. goto passthrough;
  263. }
  264. passthrough:
  265. return -EAGAIN;
  266. }
  267. void signal_io (IDTYPE target, void * arg)
  268. {
  269. debug("detecting input, signaling thread %u\n", target);
  270. struct shim_thread * thread = lookup_thread(target);
  271. if (!thread)
  272. return;
  273. append_signal(thread, SIGIO, NULL, true);
  274. }
  275. int shim_do_ioctl (int fd, int cmd, unsigned long arg)
  276. {
  277. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  278. if (!hdl)
  279. return -EBADF;
  280. int ret = -EAGAIN;
  281. switch(cmd) {
  282. /* <include/asm/termios.h> */
  283. case TCGETS:
  284. case TCSETS:
  285. case TCSETSW:
  286. case TCSETSF:
  287. case TCGETA:
  288. case TCSETA:
  289. case TCSETAW:
  290. case TCSETAF:
  291. case TCSBRK:
  292. case TCXONC:
  293. case TCFLSH:
  294. case TIOCEXCL:
  295. case TIOCNXCL:
  296. case TIOCSCTTY:
  297. case TIOCGPGRP:
  298. case TIOCSPGRP:
  299. case TIOCOUTQ:
  300. case TIOCSTI:
  301. case TIOCGWINSZ:
  302. case TIOCMGET:
  303. case TIOCMBIS:
  304. case TIOCMBIC:
  305. case TIOCMSET:
  306. case TIOCGSOFTCAR:
  307. case TIOCSSOFTCAR:
  308. /* case TIOCINQ = FIONREAD */
  309. case TIOCLINUX:
  310. case TIOCCONS:
  311. case TIOCGSERIAL:
  312. case TIOCSSERIAL:
  313. case TIOCPKT:
  314. case TIOCNOTTY:
  315. case TIOCSETD:
  316. case TIOCGETD:
  317. case TCSBRKP:
  318. ret = ioctl_termios(hdl, cmd, arg);
  319. break;
  320. case FIONBIO:
  321. if (hdl->fs && hdl->fs->fs_ops &&
  322. hdl->fs->fs_ops->setflags)
  323. hdl->fs->fs_ops->setflags(hdl, hdl->flags | O_NONBLOCK);
  324. hdl->flags |= O_NONBLOCK;
  325. ret = 0;
  326. break;
  327. case FIONCLEX:
  328. hdl->flags &= ~FD_CLOEXEC;
  329. ret = 0;
  330. break;
  331. case FIOCLEX:
  332. hdl->flags |= FD_CLOEXEC;
  333. ret = 0;
  334. break;
  335. case FIOASYNC:
  336. ret = install_async_event(hdl->pal_handle, 0, &signal_io, NULL);
  337. break;
  338. case TIOCSERCONFIG:
  339. case TIOCSERGWILD:
  340. case TIOCSERSWILD:
  341. case TIOCGLCKTRMIOS:
  342. case TIOCSLCKTRMIOS:
  343. case TIOCSERGSTRUCT:
  344. case TIOCSERGETLSR:
  345. case TIOCSERGETMULTI:
  346. case TIOCSERSETMULTI:
  347. ret = ioctl_termios(hdl, cmd, arg);
  348. break;
  349. case FDCLRPRM:
  350. case FDSETPRM:
  351. case FDDEFPRM:
  352. case FDGETPRM:
  353. case FDMSGON:
  354. case FDMSGOFF:
  355. case FDFMTBEG:
  356. case FDFMTTRK:
  357. case FDFMTEND:
  358. case FDSETEMSGTRESH:
  359. case FDFLUSH:
  360. case FDSETMAXERRS:
  361. case FDGETMAXERRS:
  362. case FDGETDRVTYP:
  363. case FDSETDRVPRM:
  364. case FDGETDRVPRM:
  365. case FDGETDRVSTAT:
  366. case FDPOLLDRVSTAT:
  367. case FDRESET:
  368. case FDGETFDCSTAT:
  369. case FDWERRORCLR:
  370. case FDWERRORGET:
  371. case FDRAWCMD:
  372. case FDTWADDLE:
  373. ret = ioctl_fd(hdl, cmd, arg);
  374. break;
  375. case FIONREAD: {
  376. struct shim_mount * fs = hdl->fs;
  377. int size = 0;
  378. int offset = 0;
  379. if (!fs || !fs->fs_ops) {
  380. ret = -EACCES;
  381. break;
  382. }
  383. if (fs->fs_ops->hstat) {
  384. struct stat stat;
  385. ret = fs->fs_ops->hstat(hdl, &stat);
  386. if (ret < 0)
  387. break;
  388. size = stat.st_size;
  389. goto done_fioread;
  390. }
  391. if (hdl->pal_handle) {
  392. PAL_STREAM_ATTR attr;
  393. if (!DkStreamAttributesQuerybyHandle(hdl->pal_handle, &attr)) {
  394. ret = -PAL_ERRNO;
  395. break;
  396. }
  397. size = attr.pending_size;
  398. goto done_fioread;
  399. }
  400. done_fioread:
  401. if (fs->fs_ops->seek) {
  402. ret = fs->fs_ops->seek(hdl, 0, SEEK_CUR);
  403. if (ret < 0)
  404. break;
  405. offset = ret;
  406. }
  407. *(int *) arg = size - offset;
  408. ret = 0;
  409. break;
  410. }
  411. /* Socket configuration controls. */
  412. case SIOCGIFNAME: /* 0x8910 get iface name */
  413. case SIOCSIFLINK: /* 0x8911 set iface channel */
  414. case SIOCGIFCONF: /* 0x8912 get iface list */
  415. case SIOCGIFFLAGS: /* 0x8913 get flags */
  416. case SIOCSIFFLAGS: /* 0x8914 set flags */
  417. case SIOCGIFADDR: /* 0x8915 get PA address */
  418. case SIOCSIFADDR: /* 0x8916 set PA address */
  419. case SIOCGIFDSTADDR: /* 0x8917 get remote PA address */
  420. case SIOCSIFDSTADDR: /* 0x8918 set remote PA address */
  421. case SIOCGIFBRDADDR: /* 0x8919 get broadcast PA address */
  422. case SIOCSIFBRDADDR: /* 0x891a set broadcast PA address */
  423. case SIOCGIFNETMASK: /* 0x891b get network PA mask */
  424. case SIOCSIFNETMASK: /* 0x891c set network PA mask */
  425. case SIOCGIFMETRIC: /* 0x891d get metric */
  426. case SIOCSIFMETRIC: /* 0x891e set metric */
  427. case SIOCGIFMEM: /* 0x891f get memory address (BSD) */
  428. case SIOCSIFMEM: /* 0x8920 set memory address (BSD) */
  429. case SIOCGIFMTU: /* 0x8921 get MTU size */
  430. case SIOCSIFMTU: /* 0x8922 set MTU size */
  431. case SIOCSIFNAME: /* 0x8923 set interface name */
  432. case SIOCSIFHWADDR: /* 0x8924 set hardware address */
  433. case SIOCGIFENCAP: /* 0x8925 get/set encapsulations */
  434. case SIOCSIFENCAP: /* 0x8926 */
  435. case SIOCGIFHWADDR: /* 0x8927 Get hardware address */
  436. case SIOCGIFSLAVE: /* 0x8929 Driver slaving support */
  437. case SIOCSIFSLAVE: /* 0x8930 */
  438. case SIOCADDMULTI: /* 0x8931 Multicast address lists */
  439. case SIOCDELMULTI: /* 0x8932 */
  440. case SIOCGIFINDEX: /* 0x8933 name -> if_index mapping */
  441. /* SIOGIFINDEX = SIOCGIFINDEX misprint compatibility :-) */
  442. case SIOCSIFPFLAGS: /* 0x8934 set/get extended flags set */
  443. case SIOCGIFPFLAGS: /* 0x8935 */
  444. case SIOCDIFADDR: /* 0x8936 delete PA address */
  445. case SIOCSIFHWBROADCAST: /* 0x8937 set hardware broadcast addr */
  446. case SIOCGIFCOUNT: /* 0x8938 get number of devices */
  447. case SIOCGIFBR: /* 0x8940 Bridging support */
  448. case SIOCSIFBR: /* 0x8941 Set bridging options */
  449. case SIOCGIFTXQLEN: /* 0x8942 Get the tx queue length */
  450. case SIOCSIFTXQLEN: /* 0x8943 Set the tx queue length */
  451. ret = ioctl_netdevice(hdl, cmd, arg);
  452. break;
  453. default:
  454. ret = -ENOSYS;
  455. break;
  456. }
  457. put_handle(hdl);
  458. return ret;
  459. }