shim_ioctl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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 <asm/ioctl.h>
  19. #include <asm/ioctls.h>
  20. #include <asm/termbits.h>
  21. #include <asm/termios.h>
  22. #include <asm/unistd.h>
  23. #include <errno.h>
  24. #include <linux/fd.h>
  25. #include <linux/sockios.h>
  26. #include <pal.h>
  27. #include <pal_error.h>
  28. #include <shim_fs.h>
  29. #include <shim_handle.h>
  30. #include <shim_internal.h>
  31. #include <shim_table.h>
  32. #include <shim_thread.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, unsigned long arg) {
  38. if (hdl->type != TYPE_FILE || hdl->info.file.type != FILE_TTY)
  39. return -ENOTTY;
  40. if (!arg)
  41. return -EINVAL;
  42. switch (cmd) {
  43. /* <include/asm/termios.h> */
  44. case TIOCGPGRP:
  45. *(int*)arg = get_cur_thread()->pgid;
  46. return 0;
  47. case TIOCSPGRP:
  48. return -EINVAL;
  49. case TCGETS: {
  50. #if 0
  51. struct termios * termios = (struct termios *) arg;
  52. termios->c_iflag = TERM_DEFAULT_IFLAG;
  53. termios->c_oflag = TERM_DEFAULT_OFLAG;
  54. termios->c_cflag = TERM_DEFAULT_CFLAG;
  55. termios->c_lflag = TERM_DEFAULT_LFLAG;
  56. return 0;
  57. #endif
  58. return -EINVAL;
  59. }
  60. case TCSETS:
  61. case TCSETSW:
  62. case TCSETSF:
  63. return -EINVAL;
  64. /* 0x00005405 TCGETA struct termio * */
  65. case TCGETA:
  66. /* 0x00005406 TCSETA const struct termio * */
  67. case TCSETA:
  68. /* 0x00005407 TCSETAW const struct termio * */
  69. case TCSETAW:
  70. /* 0x00005408 TCSETAF const struct termio * */
  71. case TCSETAF:
  72. /* 0x00005409 TCSBRK int */
  73. case TCSBRK:
  74. /* 0x0000540A TCXONC int */
  75. case TCXONC:
  76. /* 0x0000540B TCFLSH int */
  77. case TCFLSH:
  78. /* 0x0000540C TIOCEXCL void */
  79. case TIOCEXCL:
  80. /* 0x0000540D TIOCNXCL void */
  81. case TIOCNXCL:
  82. /* 0x0000540E TIOCSCTTY int */
  83. case TIOCSCTTY:
  84. /* 0x0000540F TIOCGPGRP pid_t * */
  85. case TIOCOUTQ:
  86. /* 0x00005412 TIOCSTI const char * */
  87. case TIOCSTI:
  88. /* 0x00005413 TIOCGWINSZ struct winsize * */
  89. case TIOCGWINSZ:
  90. /* 0x00005415 TIOCMGET int * */
  91. case TIOCMGET:
  92. /* 0x00005416 TIOCMBIS const int * */
  93. case TIOCMBIS:
  94. /* 0x00005417 TIOCMBIC const int * */
  95. case TIOCMBIC:
  96. /* 0x00005418 TIOCMSET const int * */
  97. case TIOCMSET:
  98. /* 0x00005419 TIOCGSOFTCAR int * */
  99. case TIOCGSOFTCAR:
  100. /* 0x0000541A TIOCSSOFTCAR const int * */
  101. case TIOCSSOFTCAR:
  102. /* 0x0000541B FIONREAD int / TIOCINQ int * */
  103. case TIOCINQ:
  104. /* 0x0000541C TIOCLINUX const char * */
  105. case TIOCLINUX:
  106. /* 0x0000541D TIOCCONS void */
  107. case TIOCCONS:
  108. /* 0x0000541E TIOCGSERIAL struct serial_struct * */
  109. case TIOCGSERIAL:
  110. /* 0x0000541F TIOCSSERIAL const struct serial_struct * */
  111. case TIOCSSERIAL:
  112. /* 0x00005420 TIOCPKT const int * */
  113. case TIOCPKT:
  114. /* 0x00005422 TIOCNOTTY void */
  115. case TIOCNOTTY:
  116. /* 0x00005423 TIOCSETD const int * */
  117. case TIOCSETD:
  118. /* 0x00005424 TIOCGETD int * */
  119. case TIOCGETD:
  120. /* 0x00005425 TCSBRKP int */
  121. case TCSBRKP:
  122. /* 0x00005453 TIOCSERCONFIG void */
  123. case TIOCSERCONFIG:
  124. /* 0x00005454 TIOCSERGWILD int * */
  125. case TIOCSERGWILD:
  126. /* 0x00005455 TIOCSERSWILD const int * */
  127. case TIOCSERSWILD:
  128. /* 0x00005456 TIOCGLCKTRMIOS struct termios * */
  129. case TIOCGLCKTRMIOS:
  130. /* 0x00005457 TIOCSLCKTRMIOS const struct termios * */
  131. case TIOCSLCKTRMIOS:
  132. /* 0x00005458 TIOCSERGSTRUCT struct async_struct * */
  133. case TIOCSERGSTRUCT:
  134. /* 0x00005459 TIOCSERGETLSR int * */
  135. case TIOCSERGETLSR:
  136. /* 0x0000545A TIOCSERGETMULTI struct serial_multiport_struct * */
  137. case TIOCSERGETMULTI:
  138. /* 0x0000545B TIOCSERSETMULTI const struct serial_multiport_struct * */
  139. case TIOCSERSETMULTI:
  140. default:
  141. goto passthrough;
  142. }
  143. passthrough:
  144. return -EAGAIN;
  145. }
  146. static int ioctl_fd(struct shim_handle* hdl, unsigned int cmd, unsigned long arg) {
  147. // This is just a placeholder function; arguments are not actually used
  148. // right now
  149. __UNUSED(hdl);
  150. __UNUSED(arg);
  151. switch (cmd) {
  152. /* <include/linux/fd.h> */
  153. /* 0x00000000 FDCLRPRM void */
  154. case FDCLRPRM:
  155. /* 0x00000001 FDSETPRM const struct floppy_struct * */
  156. case FDSETPRM:
  157. /* 0x00000002 FDDEFPRM const struct floppy_struct * */
  158. case FDDEFPRM:
  159. /* 0x00000003 FDGETPRM struct floppy_struct * */
  160. case FDGETPRM:
  161. /* 0x00000004 FDMSGON void */
  162. case FDMSGON:
  163. /* 0x00000005 FDMSGOFF void */
  164. case FDMSGOFF:
  165. /* 0x00000006 FDFMTBEG void */
  166. case FDFMTBEG:
  167. /* 0x00000007 FDFMTTRK const struct format_descr * */
  168. case FDFMTTRK:
  169. /* 0x00000008 FDFMTEND void */
  170. case FDFMTEND:
  171. /* 0x0000000A FDSETEMSGTRESH int */
  172. case FDSETEMSGTRESH:
  173. /* 0x0000000B FDFLUSH void */
  174. case FDFLUSH:
  175. /* 0x0000000C FDSETMAXERRS const struct floppy_max_errors * */
  176. case FDSETMAXERRS:
  177. /* 0x0000000E FDGETMAXERRS struct floppy_max_errors * */
  178. case FDGETMAXERRS:
  179. /* 0x00000010 FDGETDRVTYP struct { char [16]; } * */
  180. case FDGETDRVTYP:
  181. /* 0x00000014 FDSETDRVPRM const struct floppy_drive_params * */
  182. case FDSETDRVPRM:
  183. /* 0x00000015 FDGETDRVPRM struct floppy_drive_params * */
  184. case FDGETDRVPRM:
  185. /* 0x00000016 FDGETDRVSTAT struct floppy_drive_struct * */
  186. case FDGETDRVSTAT:
  187. /* 0x00000017 FDPOLLDRVSTAT struct floppy_drive_struct * */
  188. case FDPOLLDRVSTAT:
  189. /* 0x00000018 FDRESET int */
  190. case FDRESET:
  191. /* 0x00000019 FDGETFDCSTAT struct floppy_fdc_state * */
  192. case FDGETFDCSTAT:
  193. /* 0x0000001B FDWERRORCLR void */
  194. case FDWERRORCLR:
  195. /* 0x0000001C FDWERRORGET struct floppy_write_errors * */
  196. case FDWERRORGET:
  197. /* 0x0000001E FDRAWCMD struct floppy_raw_cmd *floppy_raw_cmd */
  198. case FDRAWCMD:
  199. /* 0x00000028 FDTWADDLE void */
  200. case FDTWADDLE:
  201. default:
  202. goto passthrough;
  203. }
  204. passthrough:
  205. return -EAGAIN;
  206. }
  207. static int ioctl_netdevice(struct shim_handle* hdl, unsigned int cmd, unsigned long arg) {
  208. // This is just a placeholder function; arguments are not actually used
  209. // right now
  210. __UNUSED(arg);
  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. // Kept for compatibility with signal_itimer
  267. __UNUSED(arg);
  268. debug("detecting input, signaling thread %u\n", target);
  269. struct shim_thread* thread = lookup_thread(target);
  270. if (!thread)
  271. return;
  272. lock(&thread->lock);
  273. append_signal(thread, SIGIO, NULL, true);
  274. unlock(&thread->lock);
  275. }
  276. int shim_do_ioctl(int fd, int cmd, unsigned long arg) {
  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 && hdl->fs->fs_ops->setflags)
  322. hdl->fs->fs_ops->setflags(hdl, hdl->flags | O_NONBLOCK);
  323. hdl->flags |= O_NONBLOCK;
  324. ret = 0;
  325. break;
  326. case FIONCLEX:
  327. hdl->flags &= ~FD_CLOEXEC;
  328. ret = 0;
  329. break;
  330. case FIOCLEX:
  331. hdl->flags |= FD_CLOEXEC;
  332. ret = 0;
  333. break;
  334. case FIOASYNC:
  335. ret = install_async_event(hdl->pal_handle, 0, &signal_io, NULL);
  336. break;
  337. case TIOCSERCONFIG:
  338. case TIOCSERGWILD:
  339. case TIOCSERSWILD:
  340. case TIOCGLCKTRMIOS:
  341. case TIOCSLCKTRMIOS:
  342. case TIOCSERGSTRUCT:
  343. case TIOCSERGETLSR:
  344. case TIOCSERGETMULTI:
  345. case TIOCSERSETMULTI:
  346. ret = ioctl_termios(hdl, cmd, arg);
  347. break;
  348. case FDCLRPRM:
  349. case FDSETPRM:
  350. case FDDEFPRM:
  351. case FDGETPRM:
  352. case FDMSGON:
  353. case FDMSGOFF:
  354. case FDFMTBEG:
  355. case FDFMTTRK:
  356. case FDFMTEND:
  357. case FDSETEMSGTRESH:
  358. case FDFLUSH:
  359. case FDSETMAXERRS:
  360. case FDGETMAXERRS:
  361. case FDGETDRVTYP:
  362. case FDSETDRVPRM:
  363. case FDGETDRVPRM:
  364. case FDGETDRVSTAT:
  365. case FDPOLLDRVSTAT:
  366. case FDRESET:
  367. case FDGETFDCSTAT:
  368. case FDWERRORCLR:
  369. case FDWERRORGET:
  370. case FDRAWCMD:
  371. case FDTWADDLE:
  372. ret = ioctl_fd(hdl, cmd, arg);
  373. break;
  374. case FIONREAD: {
  375. struct shim_mount* fs = hdl->fs;
  376. int size = 0;
  377. int offset = 0;
  378. if (!fs || !fs->fs_ops) {
  379. ret = -EACCES;
  380. break;
  381. }
  382. if (fs->fs_ops->hstat) {
  383. struct stat stat;
  384. ret = fs->fs_ops->hstat(hdl, &stat);
  385. if (ret < 0)
  386. break;
  387. size = stat.st_size;
  388. } else if (hdl->pal_handle) {
  389. PAL_STREAM_ATTR attr;
  390. if (!DkStreamAttributesQueryByHandle(hdl->pal_handle, &attr)) {
  391. ret = -PAL_ERRNO;
  392. break;
  393. }
  394. size = attr.pending_size;
  395. }
  396. if (fs->fs_ops->seek) {
  397. ret = fs->fs_ops->seek(hdl, 0, SEEK_CUR);
  398. if (ret < 0)
  399. break;
  400. offset = ret;
  401. }
  402. *(int*)arg = size - offset;
  403. ret = 0;
  404. break;
  405. }
  406. /* Socket configuration controls. */
  407. case SIOCGIFNAME: /* 0x8910 get iface name */
  408. case SIOCSIFLINK: /* 0x8911 set iface channel */
  409. case SIOCGIFCONF: /* 0x8912 get iface list */
  410. case SIOCGIFFLAGS: /* 0x8913 get flags */
  411. case SIOCSIFFLAGS: /* 0x8914 set flags */
  412. case SIOCGIFADDR: /* 0x8915 get PA address */
  413. case SIOCSIFADDR: /* 0x8916 set PA address */
  414. case SIOCGIFDSTADDR: /* 0x8917 get remote PA address */
  415. case SIOCSIFDSTADDR: /* 0x8918 set remote PA address */
  416. case SIOCGIFBRDADDR: /* 0x8919 get broadcast PA address */
  417. case SIOCSIFBRDADDR: /* 0x891a set broadcast PA address */
  418. case SIOCGIFNETMASK: /* 0x891b get network PA mask */
  419. case SIOCSIFNETMASK: /* 0x891c set network PA mask */
  420. case SIOCGIFMETRIC: /* 0x891d get metric */
  421. case SIOCSIFMETRIC: /* 0x891e set metric */
  422. case SIOCGIFMEM: /* 0x891f get memory address (BSD) */
  423. case SIOCSIFMEM: /* 0x8920 set memory address (BSD) */
  424. case SIOCGIFMTU: /* 0x8921 get MTU size */
  425. case SIOCSIFMTU: /* 0x8922 set MTU size */
  426. case SIOCSIFNAME: /* 0x8923 set interface name */
  427. case SIOCSIFHWADDR: /* 0x8924 set hardware address */
  428. case SIOCGIFENCAP: /* 0x8925 get/set encapsulations */
  429. case SIOCSIFENCAP: /* 0x8926 */
  430. case SIOCGIFHWADDR: /* 0x8927 Get hardware address */
  431. case SIOCGIFSLAVE: /* 0x8929 Driver slaving support */
  432. case SIOCSIFSLAVE: /* 0x8930 */
  433. case SIOCADDMULTI: /* 0x8931 Multicast address lists */
  434. case SIOCDELMULTI: /* 0x8932 */
  435. case SIOCGIFINDEX: /* 0x8933 name -> if_index mapping */
  436. /* SIOGIFINDEX = SIOCGIFINDEX misprint compatibility :-) */
  437. case SIOCSIFPFLAGS: /* 0x8934 set/get extended flags set */
  438. case SIOCGIFPFLAGS: /* 0x8935 */
  439. case SIOCDIFADDR: /* 0x8936 delete PA address */
  440. case SIOCSIFHWBROADCAST: /* 0x8937 set hardware broadcast addr */
  441. case SIOCGIFCOUNT: /* 0x8938 get number of devices */
  442. case SIOCGIFBR: /* 0x8940 Bridging support */
  443. case SIOCSIFBR: /* 0x8941 Set bridging options */
  444. case SIOCGIFTXQLEN: /* 0x8942 Get the tx queue length */
  445. case SIOCSIFTXQLEN: /* 0x8943 Set the tx queue length */
  446. ret = ioctl_netdevice(hdl, cmd, arg);
  447. break;
  448. default:
  449. ret = -ENOSYS;
  450. break;
  451. }
  452. put_handle(hdl);
  453. return ret;
  454. }