shim_ioctl.c 17 KB

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