tty_io.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. */
  5. /*
  6. * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
  7. * or rs-channels. It also implements echoing, cooked mode etc.
  8. *
  9. * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
  10. *
  11. * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
  12. * tty_struct and tty_queue structures. Previously there was an array
  13. * of 256 tty_struct's which was statically allocated, and the
  14. * tty_queue structures were allocated at boot time. Both are now
  15. * dynamically allocated only when the tty is open.
  16. *
  17. * Also restructured routines so that there is more of a separation
  18. * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
  19. * the low-level tty routines (serial.c, pty.c, console.c). This
  20. * makes for cleaner and more compact code. -TYT, 9/17/92
  21. *
  22. * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
  23. * which can be dynamically activated and de-activated by the line
  24. * discipline handling modules (like SLIP).
  25. *
  26. * NOTE: pay no attention to the line discipline code (yet); its
  27. * interface is still subject to change in this version...
  28. * -- TYT, 1/31/92
  29. *
  30. * Added functionality to the OPOST tty handling. No delays, but all
  31. * other bits should be there.
  32. * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
  33. *
  34. * Rewrote canonical mode and added more termios flags.
  35. * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
  36. *
  37. * Reorganized FASYNC support so mouse code can share it.
  38. * -- ctm@ardi.com, 9Sep95
  39. *
  40. * New TIOCLINUX variants added.
  41. * -- mj@k332.feld.cvut.cz, 19-Nov-95
  42. *
  43. * Restrict vt switching via ioctl()
  44. * -- grif@cs.ucr.edu, 5-Dec-95
  45. *
  46. * Move console and virtual terminal code to more appropriate files,
  47. * implement CONFIG_VT and generalize console device interface.
  48. * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
  49. *
  50. * Rewrote tty_init_dev and tty_release_dev to eliminate races.
  51. * -- Bill Hawes <whawes@star.net>, June 97
  52. *
  53. * Added devfs support.
  54. * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
  55. *
  56. * Added support for a Unix98-style ptmx device.
  57. * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
  58. *
  59. * Reduced memory usage for older ARM systems
  60. * -- Russell King <rmk@arm.linux.org.uk>
  61. *
  62. * Move do_SAK() into process context. Less stack use in devfs functions.
  63. * alloc_tty_struct() always uses kmalloc()
  64. * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
  65. */
  66. #include <linux/types.h>
  67. #include <linux/major.h>
  68. #include <linux/errno.h>
  69. #include <linux/signal.h>
  70. #include <linux/fcntl.h>
  71. #include <linux/sched/signal.h>
  72. #include <linux/sched/task.h>
  73. #include <linux/interrupt.h>
  74. #include <linux/tty.h>
  75. #include <linux/tty_driver.h>
  76. #include <linux/tty_flip.h>
  77. #include <linux/devpts_fs.h>
  78. #include <linux/file.h>
  79. #include <linux/fdtable.h>
  80. #include <linux/console.h>
  81. #include <linux/timer.h>
  82. #include <linux/ctype.h>
  83. #include <linux/kd.h>
  84. #include <linux/mm.h>
  85. #include <linux/string.h>
  86. #include <linux/slab.h>
  87. #include <linux/poll.h>
  88. #include <linux/ppp-ioctl.h>
  89. #include <linux/proc_fs.h>
  90. #include <linux/init.h>
  91. #include <linux/module.h>
  92. #include <linux/device.h>
  93. #include <linux/wait.h>
  94. #include <linux/bitops.h>
  95. #include <linux/delay.h>
  96. #include <linux/seq_file.h>
  97. #include <linux/serial.h>
  98. #include <linux/ratelimit.h>
  99. #include <linux/compat.h>
  100. #include <linux/uaccess.h>
  101. #include <linux/termios_internal.h>
  102. #include <linux/fs.h>
  103. #include <linux/kbd_kern.h>
  104. #include <linux/vt_kern.h>
  105. #include <linux/selection.h>
  106. #include <linux/kmod.h>
  107. #include <linux/nsproxy.h>
  108. #include "tty.h"
  109. #undef TTY_DEBUG_HANGUP
  110. #ifdef TTY_DEBUG_HANGUP
  111. # define tty_debug_hangup(tty, f, args...) tty_debug(tty, f, ##args)
  112. #else
  113. # define tty_debug_hangup(tty, f, args...) do { } while (0)
  114. #endif
  115. #define TTY_PARANOIA_CHECK 1
  116. #define CHECK_TTY_COUNT 1
  117. struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
  118. .c_iflag = ICRNL | IXON,
  119. .c_oflag = OPOST | ONLCR,
  120. .c_cflag = B38400 | CS8 | CREAD | HUPCL,
  121. .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
  122. ECHOCTL | ECHOKE | IEXTEN,
  123. .c_cc = INIT_C_CC,
  124. .c_ispeed = 38400,
  125. .c_ospeed = 38400,
  126. /* .c_line = N_TTY, */
  127. };
  128. EXPORT_SYMBOL(tty_std_termios);
  129. /* This list gets poked at by procfs and various bits of boot up code. This
  130. * could do with some rationalisation such as pulling the tty proc function
  131. * into this file.
  132. */
  133. LIST_HEAD(tty_drivers); /* linked list of tty drivers */
  134. /* Mutex to protect creating and releasing a tty */
  135. DEFINE_MUTEX(tty_mutex);
  136. static ssize_t tty_read(struct kiocb *, struct iov_iter *);
  137. static ssize_t tty_write(struct kiocb *, struct iov_iter *);
  138. static __poll_t tty_poll(struct file *, poll_table *);
  139. static int tty_open(struct inode *, struct file *);
  140. #ifdef CONFIG_COMPAT
  141. static long tty_compat_ioctl(struct file *file, unsigned int cmd,
  142. unsigned long arg);
  143. #else
  144. #define tty_compat_ioctl NULL
  145. #endif
  146. static int __tty_fasync(int fd, struct file *filp, int on);
  147. static int tty_fasync(int fd, struct file *filp, int on);
  148. static void release_tty(struct tty_struct *tty, int idx);
  149. /**
  150. * free_tty_struct - free a disused tty
  151. * @tty: tty struct to free
  152. *
  153. * Free the write buffers, tty queue and tty memory itself.
  154. *
  155. * Locking: none. Must be called after tty is definitely unused
  156. */
  157. static void free_tty_struct(struct tty_struct *tty)
  158. {
  159. tty_ldisc_deinit(tty);
  160. put_device(tty->dev);
  161. kvfree(tty->write_buf);
  162. kfree(tty);
  163. }
  164. static inline struct tty_struct *file_tty(struct file *file)
  165. {
  166. return ((struct tty_file_private *)file->private_data)->tty;
  167. }
  168. int tty_alloc_file(struct file *file)
  169. {
  170. struct tty_file_private *priv;
  171. priv = kmalloc(sizeof(*priv), GFP_KERNEL);
  172. if (!priv)
  173. return -ENOMEM;
  174. file->private_data = priv;
  175. return 0;
  176. }
  177. /* Associate a new file with the tty structure */
  178. void tty_add_file(struct tty_struct *tty, struct file *file)
  179. {
  180. struct tty_file_private *priv = file->private_data;
  181. priv->tty = tty;
  182. priv->file = file;
  183. spin_lock(&tty->files_lock);
  184. list_add(&priv->list, &tty->tty_files);
  185. spin_unlock(&tty->files_lock);
  186. }
  187. /**
  188. * tty_free_file - free file->private_data
  189. * @file: to free private_data of
  190. *
  191. * This shall be used only for fail path handling when tty_add_file was not
  192. * called yet.
  193. */
  194. void tty_free_file(struct file *file)
  195. {
  196. struct tty_file_private *priv = file->private_data;
  197. file->private_data = NULL;
  198. kfree(priv);
  199. }
  200. /* Delete file from its tty */
  201. static void tty_del_file(struct file *file)
  202. {
  203. struct tty_file_private *priv = file->private_data;
  204. struct tty_struct *tty = priv->tty;
  205. spin_lock(&tty->files_lock);
  206. list_del(&priv->list);
  207. spin_unlock(&tty->files_lock);
  208. tty_free_file(file);
  209. }
  210. /**
  211. * tty_name - return tty naming
  212. * @tty: tty structure
  213. *
  214. * Convert a tty structure into a name. The name reflects the kernel naming
  215. * policy and if udev is in use may not reflect user space
  216. *
  217. * Locking: none
  218. */
  219. const char *tty_name(const struct tty_struct *tty)
  220. {
  221. if (!tty) /* Hmm. NULL pointer. That's fun. */
  222. return "NULL tty";
  223. return tty->name;
  224. }
  225. EXPORT_SYMBOL(tty_name);
  226. const char *tty_driver_name(const struct tty_struct *tty)
  227. {
  228. if (!tty || !tty->driver)
  229. return "";
  230. return tty->driver->name;
  231. }
  232. static int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
  233. const char *routine)
  234. {
  235. #ifdef TTY_PARANOIA_CHECK
  236. if (!tty) {
  237. pr_warn("(%d:%d): %s: NULL tty\n",
  238. imajor(inode), iminor(inode), routine);
  239. return 1;
  240. }
  241. #endif
  242. return 0;
  243. }
  244. /* Caller must hold tty_lock */
  245. static void check_tty_count(struct tty_struct *tty, const char *routine)
  246. {
  247. #ifdef CHECK_TTY_COUNT
  248. struct list_head *p;
  249. int count = 0, kopen_count = 0;
  250. spin_lock(&tty->files_lock);
  251. list_for_each(p, &tty->tty_files) {
  252. count++;
  253. }
  254. spin_unlock(&tty->files_lock);
  255. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  256. tty->driver->subtype == PTY_TYPE_SLAVE &&
  257. tty->link && tty->link->count)
  258. count++;
  259. if (tty_port_kopened(tty->port))
  260. kopen_count++;
  261. if (tty->count != (count + kopen_count)) {
  262. tty_warn(tty, "%s: tty->count(%d) != (#fd's(%d) + #kopen's(%d))\n",
  263. routine, tty->count, count, kopen_count);
  264. }
  265. #endif
  266. }
  267. /**
  268. * get_tty_driver - find device of a tty
  269. * @device: device identifier
  270. * @index: returns the index of the tty
  271. *
  272. * This routine returns a tty driver structure, given a device number and also
  273. * passes back the index number.
  274. *
  275. * Locking: caller must hold tty_mutex
  276. */
  277. static struct tty_driver *get_tty_driver(dev_t device, int *index)
  278. {
  279. struct tty_driver *p;
  280. list_for_each_entry(p, &tty_drivers, tty_drivers) {
  281. dev_t base = MKDEV(p->major, p->minor_start);
  282. if (device < base || device >= base + p->num)
  283. continue;
  284. *index = device - base;
  285. return tty_driver_kref_get(p);
  286. }
  287. return NULL;
  288. }
  289. /**
  290. * tty_dev_name_to_number - return dev_t for device name
  291. * @name: user space name of device under /dev
  292. * @number: pointer to dev_t that this function will populate
  293. *
  294. * This function converts device names like ttyS0 or ttyUSB1 into dev_t like
  295. * (4, 64) or (188, 1). If no corresponding driver is registered then the
  296. * function returns -%ENODEV.
  297. *
  298. * Locking: this acquires tty_mutex to protect the tty_drivers list from
  299. * being modified while we are traversing it, and makes sure to
  300. * release it before exiting.
  301. */
  302. int tty_dev_name_to_number(const char *name, dev_t *number)
  303. {
  304. struct tty_driver *p;
  305. int ret;
  306. int index, prefix_length = 0;
  307. const char *str;
  308. for (str = name; *str && !isdigit(*str); str++)
  309. ;
  310. if (!*str)
  311. return -EINVAL;
  312. ret = kstrtoint(str, 10, &index);
  313. if (ret)
  314. return ret;
  315. prefix_length = str - name;
  316. guard(mutex)(&tty_mutex);
  317. list_for_each_entry(p, &tty_drivers, tty_drivers)
  318. if (prefix_length == strlen(p->name) && strncmp(name,
  319. p->name, prefix_length) == 0) {
  320. if (index < p->num) {
  321. *number = MKDEV(p->major, p->minor_start + index);
  322. return 0;
  323. }
  324. }
  325. return -ENODEV;
  326. }
  327. EXPORT_SYMBOL_GPL(tty_dev_name_to_number);
  328. #ifdef CONFIG_CONSOLE_POLL
  329. /**
  330. * tty_find_polling_driver - find device of a polled tty
  331. * @name: name string to match
  332. * @line: pointer to resulting tty line nr
  333. *
  334. * This routine returns a tty driver structure, given a name and the condition
  335. * that the tty driver is capable of polled operation.
  336. */
  337. struct tty_driver *tty_find_polling_driver(char *name, int *line)
  338. {
  339. struct tty_driver *p, *res = NULL;
  340. int tty_line = 0;
  341. int len;
  342. char *str, *stp;
  343. for (str = name; *str; str++)
  344. if ((*str >= '0' && *str <= '9') || *str == ',')
  345. break;
  346. if (!*str)
  347. return NULL;
  348. len = str - name;
  349. tty_line = simple_strtoul(str, &str, 10);
  350. mutex_lock(&tty_mutex);
  351. /* Search through the tty devices to look for a match */
  352. list_for_each_entry(p, &tty_drivers, tty_drivers) {
  353. if (!len || strncmp(name, p->name, len) != 0)
  354. continue;
  355. stp = str;
  356. if (*stp == ',')
  357. stp++;
  358. if (*stp == '\0')
  359. stp = NULL;
  360. if (tty_line >= 0 && tty_line < p->num && p->ops &&
  361. p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
  362. res = tty_driver_kref_get(p);
  363. *line = tty_line;
  364. break;
  365. }
  366. }
  367. mutex_unlock(&tty_mutex);
  368. return res;
  369. }
  370. EXPORT_SYMBOL_GPL(tty_find_polling_driver);
  371. #endif
  372. static ssize_t hung_up_tty_read(struct kiocb *iocb, struct iov_iter *to)
  373. {
  374. return 0;
  375. }
  376. static ssize_t hung_up_tty_write(struct kiocb *iocb, struct iov_iter *from)
  377. {
  378. return -EIO;
  379. }
  380. /* No kernel lock held - none needed ;) */
  381. static __poll_t hung_up_tty_poll(struct file *filp, poll_table *wait)
  382. {
  383. return EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP | EPOLLRDNORM | EPOLLWRNORM;
  384. }
  385. static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
  386. unsigned long arg)
  387. {
  388. return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
  389. }
  390. static long hung_up_tty_compat_ioctl(struct file *file,
  391. unsigned int cmd, unsigned long arg)
  392. {
  393. return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
  394. }
  395. static int hung_up_tty_fasync(int fd, struct file *file, int on)
  396. {
  397. return -ENOTTY;
  398. }
  399. static void tty_show_fdinfo(struct seq_file *m, struct file *file)
  400. {
  401. struct tty_struct *tty = file_tty(file);
  402. if (tty && tty->ops && tty->ops->show_fdinfo)
  403. tty->ops->show_fdinfo(tty, m);
  404. }
  405. static const struct file_operations tty_fops = {
  406. .read_iter = tty_read,
  407. .write_iter = tty_write,
  408. .splice_read = copy_splice_read,
  409. .splice_write = iter_file_splice_write,
  410. .poll = tty_poll,
  411. .unlocked_ioctl = tty_ioctl,
  412. .compat_ioctl = tty_compat_ioctl,
  413. .open = tty_open,
  414. .release = tty_release,
  415. .fasync = tty_fasync,
  416. .show_fdinfo = tty_show_fdinfo,
  417. };
  418. static const struct file_operations console_fops = {
  419. .read_iter = tty_read,
  420. .write_iter = redirected_tty_write,
  421. .splice_read = copy_splice_read,
  422. .splice_write = iter_file_splice_write,
  423. .poll = tty_poll,
  424. .unlocked_ioctl = tty_ioctl,
  425. .compat_ioctl = tty_compat_ioctl,
  426. .open = tty_open,
  427. .release = tty_release,
  428. .fasync = tty_fasync,
  429. };
  430. static const struct file_operations hung_up_tty_fops = {
  431. .read_iter = hung_up_tty_read,
  432. .write_iter = hung_up_tty_write,
  433. .poll = hung_up_tty_poll,
  434. .unlocked_ioctl = hung_up_tty_ioctl,
  435. .compat_ioctl = hung_up_tty_compat_ioctl,
  436. .release = tty_release,
  437. .fasync = hung_up_tty_fasync,
  438. };
  439. static DEFINE_SPINLOCK(redirect_lock);
  440. static struct file *redirect;
  441. /**
  442. * tty_wakeup - request more data
  443. * @tty: terminal
  444. *
  445. * Internal and external helper for wakeups of tty. This function informs the
  446. * line discipline if present that the driver is ready to receive more output
  447. * data.
  448. */
  449. void tty_wakeup(struct tty_struct *tty)
  450. {
  451. struct tty_ldisc *ld;
  452. if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
  453. ld = tty_ldisc_ref(tty);
  454. if (ld) {
  455. if (ld->ops->write_wakeup)
  456. ld->ops->write_wakeup(tty);
  457. tty_ldisc_deref(ld);
  458. }
  459. }
  460. wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
  461. }
  462. EXPORT_SYMBOL_GPL(tty_wakeup);
  463. /**
  464. * tty_release_redirect - Release a redirect on a pty if present
  465. * @tty: tty device
  466. *
  467. * This is available to the pty code so if the master closes, if the slave is a
  468. * redirect it can release the redirect.
  469. */
  470. static struct file *tty_release_redirect(struct tty_struct *tty)
  471. {
  472. struct file *f = NULL;
  473. spin_lock(&redirect_lock);
  474. if (redirect && file_tty(redirect) == tty) {
  475. f = redirect;
  476. redirect = NULL;
  477. }
  478. spin_unlock(&redirect_lock);
  479. return f;
  480. }
  481. /**
  482. * __tty_hangup - actual handler for hangup events
  483. * @tty: tty device
  484. * @exit_session: if non-zero, signal all foreground group processes
  485. *
  486. * This can be called by a "kworker" kernel thread. That is process synchronous
  487. * but doesn't hold any locks, so we need to make sure we have the appropriate
  488. * locks for what we're doing.
  489. *
  490. * The hangup event clears any pending redirections onto the hung up device. It
  491. * ensures future writes will error and it does the needed line discipline
  492. * hangup and signal delivery. The tty object itself remains intact.
  493. *
  494. * Locking:
  495. * * BTM
  496. *
  497. * * redirect lock for undoing redirection
  498. * * file list lock for manipulating list of ttys
  499. * * tty_ldiscs_lock from called functions
  500. * * termios_rwsem resetting termios data
  501. * * tasklist_lock to walk task list for hangup event
  502. *
  503. * * ->siglock to protect ->signal/->sighand
  504. *
  505. */
  506. static void __tty_hangup(struct tty_struct *tty, int exit_session)
  507. {
  508. struct file *cons_filp = NULL;
  509. struct file *filp, *f;
  510. struct tty_file_private *priv;
  511. int closecount = 0, n;
  512. int refs;
  513. if (!tty)
  514. return;
  515. f = tty_release_redirect(tty);
  516. tty_lock(tty);
  517. if (test_bit(TTY_HUPPED, &tty->flags)) {
  518. tty_unlock(tty);
  519. return;
  520. }
  521. /*
  522. * Some console devices aren't actually hung up for technical and
  523. * historical reasons, which can lead to indefinite interruptible
  524. * sleep in n_tty_read(). The following explicitly tells
  525. * n_tty_read() to abort readers.
  526. */
  527. set_bit(TTY_HUPPING, &tty->flags);
  528. /* inuse_filps is protected by the single tty lock,
  529. * this really needs to change if we want to flush the
  530. * workqueue with the lock held.
  531. */
  532. check_tty_count(tty, "tty_hangup");
  533. spin_lock(&tty->files_lock);
  534. /* This breaks for file handles being sent over AF_UNIX sockets ? */
  535. list_for_each_entry(priv, &tty->tty_files, list) {
  536. filp = priv->file;
  537. if (filp->f_op->write_iter == redirected_tty_write)
  538. cons_filp = filp;
  539. if (filp->f_op->write_iter != tty_write)
  540. continue;
  541. closecount++;
  542. __tty_fasync(-1, filp, 0); /* can't block */
  543. filp->f_op = &hung_up_tty_fops;
  544. }
  545. spin_unlock(&tty->files_lock);
  546. refs = tty_signal_session_leader(tty, exit_session);
  547. /* Account for the p->signal references we killed */
  548. while (refs--)
  549. tty_kref_put(tty);
  550. tty_ldisc_hangup(tty, cons_filp != NULL);
  551. spin_lock_irq(&tty->ctrl.lock);
  552. clear_bit(TTY_THROTTLED, &tty->flags);
  553. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  554. put_pid(tty->ctrl.session);
  555. put_pid(tty->ctrl.pgrp);
  556. tty->ctrl.session = NULL;
  557. tty->ctrl.pgrp = NULL;
  558. tty->ctrl.pktstatus = 0;
  559. spin_unlock_irq(&tty->ctrl.lock);
  560. /*
  561. * If one of the devices matches a console pointer, we
  562. * cannot just call hangup() because that will cause
  563. * tty->count and state->count to go out of sync.
  564. * So we just call close() the right number of times.
  565. */
  566. if (cons_filp) {
  567. if (tty->ops->close)
  568. for (n = 0; n < closecount; n++)
  569. tty->ops->close(tty, cons_filp);
  570. } else if (tty->ops->hangup)
  571. tty->ops->hangup(tty);
  572. /*
  573. * We don't want to have driver/ldisc interactions beyond the ones
  574. * we did here. The driver layer expects no calls after ->hangup()
  575. * from the ldisc side, which is now guaranteed.
  576. */
  577. set_bit(TTY_HUPPED, &tty->flags);
  578. clear_bit(TTY_HUPPING, &tty->flags);
  579. tty_unlock(tty);
  580. if (f)
  581. fput(f);
  582. }
  583. static void do_tty_hangup(struct work_struct *work)
  584. {
  585. struct tty_struct *tty =
  586. container_of(work, struct tty_struct, hangup_work);
  587. __tty_hangup(tty, 0);
  588. }
  589. /**
  590. * tty_hangup - trigger a hangup event
  591. * @tty: tty to hangup
  592. *
  593. * A carrier loss (virtual or otherwise) has occurred on @tty. Schedule a
  594. * hangup sequence to run after this event.
  595. */
  596. void tty_hangup(struct tty_struct *tty)
  597. {
  598. tty_debug_hangup(tty, "hangup\n");
  599. schedule_work(&tty->hangup_work);
  600. }
  601. EXPORT_SYMBOL(tty_hangup);
  602. /**
  603. * tty_vhangup - process vhangup
  604. * @tty: tty to hangup
  605. *
  606. * The user has asked via system call for the terminal to be hung up. We do
  607. * this synchronously so that when the syscall returns the process is complete.
  608. * That guarantee is necessary for security reasons.
  609. */
  610. void tty_vhangup(struct tty_struct *tty)
  611. {
  612. tty_debug_hangup(tty, "vhangup\n");
  613. __tty_hangup(tty, 0);
  614. }
  615. EXPORT_SYMBOL(tty_vhangup);
  616. /**
  617. * tty_vhangup_self - process vhangup for own ctty
  618. *
  619. * Perform a vhangup on the current controlling tty
  620. */
  621. void tty_vhangup_self(void)
  622. {
  623. struct tty_struct *tty;
  624. tty = get_current_tty();
  625. if (tty) {
  626. tty_vhangup(tty);
  627. tty_kref_put(tty);
  628. }
  629. }
  630. /**
  631. * tty_vhangup_session - hangup session leader exit
  632. * @tty: tty to hangup
  633. *
  634. * The session leader is exiting and hanging up its controlling terminal.
  635. * Every process in the foreground process group is signalled %SIGHUP.
  636. *
  637. * We do this synchronously so that when the syscall returns the process is
  638. * complete. That guarantee is necessary for security reasons.
  639. */
  640. void tty_vhangup_session(struct tty_struct *tty)
  641. {
  642. tty_debug_hangup(tty, "session hangup\n");
  643. __tty_hangup(tty, 1);
  644. }
  645. /**
  646. * tty_hung_up_p - was tty hung up
  647. * @filp: file pointer of tty
  648. *
  649. * Return: true if the tty has been subject to a vhangup or a carrier loss
  650. */
  651. int tty_hung_up_p(struct file *filp)
  652. {
  653. return (filp && filp->f_op == &hung_up_tty_fops);
  654. }
  655. EXPORT_SYMBOL(tty_hung_up_p);
  656. void __stop_tty(struct tty_struct *tty)
  657. {
  658. if (tty->flow.stopped)
  659. return;
  660. tty->flow.stopped = true;
  661. if (tty->ops->stop)
  662. tty->ops->stop(tty);
  663. }
  664. /**
  665. * stop_tty - propagate flow control
  666. * @tty: tty to stop
  667. *
  668. * Perform flow control to the driver. May be called on an already stopped
  669. * device and will not re-call the &tty_driver->stop() method.
  670. *
  671. * This functionality is used by both the line disciplines for halting incoming
  672. * flow and by the driver. It may therefore be called from any context, may be
  673. * under the tty %atomic_write_lock but not always.
  674. *
  675. * Locking:
  676. * flow.lock
  677. */
  678. void stop_tty(struct tty_struct *tty)
  679. {
  680. unsigned long flags;
  681. spin_lock_irqsave(&tty->flow.lock, flags);
  682. __stop_tty(tty);
  683. spin_unlock_irqrestore(&tty->flow.lock, flags);
  684. }
  685. EXPORT_SYMBOL(stop_tty);
  686. void __start_tty(struct tty_struct *tty)
  687. {
  688. if (!tty->flow.stopped || tty->flow.tco_stopped)
  689. return;
  690. tty->flow.stopped = false;
  691. if (tty->ops->start)
  692. tty->ops->start(tty);
  693. tty_wakeup(tty);
  694. }
  695. /**
  696. * start_tty - propagate flow control
  697. * @tty: tty to start
  698. *
  699. * Start a tty that has been stopped if at all possible. If @tty was previously
  700. * stopped and is now being started, the &tty_driver->start() method is invoked
  701. * and the line discipline woken.
  702. *
  703. * Locking:
  704. * flow.lock
  705. */
  706. void start_tty(struct tty_struct *tty)
  707. {
  708. unsigned long flags;
  709. spin_lock_irqsave(&tty->flow.lock, flags);
  710. __start_tty(tty);
  711. spin_unlock_irqrestore(&tty->flow.lock, flags);
  712. }
  713. EXPORT_SYMBOL(start_tty);
  714. static void tty_update_time(struct tty_struct *tty, bool mtime)
  715. {
  716. time64_t sec = ktime_get_real_seconds();
  717. struct tty_file_private *priv;
  718. spin_lock(&tty->files_lock);
  719. list_for_each_entry(priv, &tty->tty_files, list) {
  720. struct inode *inode = file_inode(priv->file);
  721. struct timespec64 time = mtime ? inode_get_mtime(inode) : inode_get_atime(inode);
  722. /*
  723. * We only care if the two values differ in anything other than the
  724. * lower three bits (i.e every 8 seconds). If so, then we can update
  725. * the time of the tty device, otherwise it could be construded as a
  726. * security leak to let userspace know the exact timing of the tty.
  727. */
  728. if ((sec ^ time.tv_sec) & ~7) {
  729. if (mtime)
  730. inode_set_mtime(inode, sec, 0);
  731. else
  732. inode_set_atime(inode, sec, 0);
  733. }
  734. }
  735. spin_unlock(&tty->files_lock);
  736. }
  737. /*
  738. * Iterate on the ldisc ->read() function until we've gotten all
  739. * the data the ldisc has for us.
  740. *
  741. * The "cookie" is something that the ldisc read function can fill
  742. * in to let us know that there is more data to be had.
  743. *
  744. * We promise to continue to call the ldisc until it stops returning
  745. * data or clears the cookie. The cookie may be something that the
  746. * ldisc maintains state for and needs to free.
  747. */
  748. static ssize_t iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty,
  749. struct file *file, struct iov_iter *to)
  750. {
  751. void *cookie = NULL;
  752. unsigned long offset = 0;
  753. ssize_t retval = 0;
  754. size_t copied, count = iov_iter_count(to);
  755. u8 kernel_buf[64];
  756. do {
  757. ssize_t size = min(count, sizeof(kernel_buf));
  758. size = ld->ops->read(tty, file, kernel_buf, size, &cookie, offset);
  759. if (!size)
  760. break;
  761. if (size < 0) {
  762. /* Did we have an earlier error (ie -EFAULT)? */
  763. if (retval)
  764. break;
  765. retval = size;
  766. /*
  767. * -EOVERFLOW means we didn't have enough space
  768. * for a whole packet, and we shouldn't return
  769. * a partial result.
  770. */
  771. if (retval == -EOVERFLOW)
  772. offset = 0;
  773. break;
  774. }
  775. copied = copy_to_iter(kernel_buf, size, to);
  776. offset += copied;
  777. count -= copied;
  778. /*
  779. * If the user copy failed, we still need to do another ->read()
  780. * call if we had a cookie to let the ldisc clear up.
  781. *
  782. * But make sure size is zeroed.
  783. */
  784. if (unlikely(copied != size)) {
  785. count = 0;
  786. retval = -EFAULT;
  787. }
  788. } while (cookie);
  789. /* We always clear tty buffer in case they contained passwords */
  790. memzero_explicit(kernel_buf, sizeof(kernel_buf));
  791. return offset ? offset : retval;
  792. }
  793. /**
  794. * tty_read - read method for tty device files
  795. * @iocb: kernel I/O control block
  796. * @to: destination for the data read
  797. *
  798. * Perform the read system call function on this terminal device. Checks
  799. * for hung up devices before calling the line discipline method.
  800. *
  801. * Locking:
  802. * Locks the line discipline internally while needed. Multiple read calls
  803. * may be outstanding in parallel.
  804. */
  805. static ssize_t tty_read(struct kiocb *iocb, struct iov_iter *to)
  806. {
  807. struct file *file = iocb->ki_filp;
  808. struct inode *inode = file_inode(file);
  809. struct tty_struct *tty = file_tty(file);
  810. struct tty_ldisc *ld;
  811. ssize_t ret;
  812. if (tty_paranoia_check(tty, inode, "tty_read"))
  813. return -EIO;
  814. if (!tty || tty_io_error(tty))
  815. return -EIO;
  816. /* We want to wait for the line discipline to sort out in this
  817. * situation.
  818. */
  819. ld = tty_ldisc_ref_wait(tty);
  820. if (!ld)
  821. return hung_up_tty_read(iocb, to);
  822. ret = -EIO;
  823. if (ld->ops->read)
  824. ret = iterate_tty_read(ld, tty, file, to);
  825. tty_ldisc_deref(ld);
  826. if (ret > 0)
  827. tty_update_time(tty, false);
  828. return ret;
  829. }
  830. void tty_write_unlock(struct tty_struct *tty)
  831. {
  832. mutex_unlock(&tty->atomic_write_lock);
  833. wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
  834. }
  835. int tty_write_lock(struct tty_struct *tty, bool ndelay)
  836. {
  837. if (!mutex_trylock(&tty->atomic_write_lock)) {
  838. if (ndelay)
  839. return -EAGAIN;
  840. if (mutex_lock_interruptible(&tty->atomic_write_lock))
  841. return -ERESTARTSYS;
  842. }
  843. return 0;
  844. }
  845. /*
  846. * Split writes up in sane blocksizes to avoid
  847. * denial-of-service type attacks
  848. */
  849. static ssize_t iterate_tty_write(struct tty_ldisc *ld, struct tty_struct *tty,
  850. struct file *file, struct iov_iter *from)
  851. {
  852. size_t chunk, count = iov_iter_count(from);
  853. ssize_t ret, written = 0;
  854. ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
  855. if (ret < 0)
  856. return ret;
  857. /*
  858. * We chunk up writes into a temporary buffer. This
  859. * simplifies low-level drivers immensely, since they
  860. * don't have locking issues and user mode accesses.
  861. *
  862. * But if TTY_NO_WRITE_SPLIT is set, we should use a
  863. * big chunk-size..
  864. *
  865. * The default chunk-size is 2kB, because the NTTY
  866. * layer has problems with bigger chunks. It will
  867. * claim to be able to handle more characters than
  868. * it actually does.
  869. */
  870. chunk = 2048;
  871. if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
  872. chunk = 65536;
  873. if (count < chunk)
  874. chunk = count;
  875. /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
  876. if (tty->write_cnt < chunk) {
  877. u8 *buf_chunk;
  878. if (chunk < 1024)
  879. chunk = 1024;
  880. buf_chunk = kvmalloc(chunk, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  881. if (!buf_chunk) {
  882. ret = -ENOMEM;
  883. goto out;
  884. }
  885. kvfree(tty->write_buf);
  886. tty->write_cnt = chunk;
  887. tty->write_buf = buf_chunk;
  888. }
  889. /* Do the write .. */
  890. for (;;) {
  891. size_t size = min(chunk, count);
  892. ret = -EFAULT;
  893. if (copy_from_iter(tty->write_buf, size, from) != size)
  894. break;
  895. ret = ld->ops->write(tty, file, tty->write_buf, size);
  896. if (ret <= 0)
  897. break;
  898. written += ret;
  899. if (ret > size)
  900. break;
  901. /* FIXME! Have Al check this! */
  902. if (ret != size)
  903. iov_iter_revert(from, size-ret);
  904. count -= ret;
  905. if (!count)
  906. break;
  907. ret = -ERESTARTSYS;
  908. if (signal_pending(current))
  909. break;
  910. cond_resched();
  911. }
  912. if (written) {
  913. tty_update_time(tty, true);
  914. ret = written;
  915. }
  916. out:
  917. tty_write_unlock(tty);
  918. return ret;
  919. }
  920. #ifdef CONFIG_PRINT_QUOTA_WARNING
  921. /**
  922. * tty_write_message - write a message to a certain tty, not just the console.
  923. * @tty: the destination tty_struct
  924. * @msg: the message to write
  925. *
  926. * This is used for messages that need to be redirected to a specific tty. We
  927. * don't put it into the syslog queue right now maybe in the future if really
  928. * needed.
  929. *
  930. * We must still hold the BTM and test the CLOSING flag for the moment.
  931. *
  932. * This function is DEPRECATED, do not use in new code.
  933. */
  934. void tty_write_message(struct tty_struct *tty, char *msg)
  935. {
  936. if (tty) {
  937. mutex_lock(&tty->atomic_write_lock);
  938. tty_lock(tty);
  939. if (tty->ops->write && tty->count > 0)
  940. tty->ops->write(tty, msg, strlen(msg));
  941. tty_unlock(tty);
  942. tty_write_unlock(tty);
  943. }
  944. }
  945. #endif
  946. static ssize_t file_tty_write(struct file *file, struct kiocb *iocb, struct iov_iter *from)
  947. {
  948. struct tty_struct *tty = file_tty(file);
  949. struct tty_ldisc *ld;
  950. ssize_t ret;
  951. if (tty_paranoia_check(tty, file_inode(file), "tty_write"))
  952. return -EIO;
  953. if (!tty || !tty->ops->write || tty_io_error(tty))
  954. return -EIO;
  955. /* Short term debug to catch buggy drivers */
  956. if (tty->ops->write_room == NULL)
  957. tty_err(tty, "missing write_room method\n");
  958. ld = tty_ldisc_ref_wait(tty);
  959. if (!ld)
  960. return hung_up_tty_write(iocb, from);
  961. if (!ld->ops->write)
  962. ret = -EIO;
  963. else
  964. ret = iterate_tty_write(ld, tty, file, from);
  965. tty_ldisc_deref(ld);
  966. return ret;
  967. }
  968. /**
  969. * tty_write - write method for tty device file
  970. * @iocb: kernel I/O control block
  971. * @from: iov_iter with data to write
  972. *
  973. * Write data to a tty device via the line discipline.
  974. *
  975. * Locking:
  976. * Locks the line discipline as required
  977. * Writes to the tty driver are serialized by the atomic_write_lock
  978. * and are then processed in chunks to the device. The line
  979. * discipline write method will not be invoked in parallel for
  980. * each device.
  981. */
  982. static ssize_t tty_write(struct kiocb *iocb, struct iov_iter *from)
  983. {
  984. return file_tty_write(iocb->ki_filp, iocb, from);
  985. }
  986. ssize_t redirected_tty_write(struct kiocb *iocb, struct iov_iter *iter)
  987. {
  988. struct file *p = NULL;
  989. spin_lock(&redirect_lock);
  990. if (redirect)
  991. p = get_file(redirect);
  992. spin_unlock(&redirect_lock);
  993. /*
  994. * We know the redirected tty is just another tty, we can
  995. * call file_tty_write() directly with that file pointer.
  996. */
  997. if (p) {
  998. ssize_t res;
  999. res = file_tty_write(p, iocb, iter);
  1000. fput(p);
  1001. return res;
  1002. }
  1003. return tty_write(iocb, iter);
  1004. }
  1005. /**
  1006. * tty_send_xchar - send priority character
  1007. * @tty: the tty to send to
  1008. * @ch: xchar to send
  1009. *
  1010. * Send a high priority character to the tty even if stopped.
  1011. *
  1012. * Locking: none for xchar method, write ordering for write method.
  1013. */
  1014. int tty_send_xchar(struct tty_struct *tty, u8 ch)
  1015. {
  1016. bool was_stopped = tty->flow.stopped;
  1017. if (tty->ops->send_xchar) {
  1018. down_read(&tty->termios_rwsem);
  1019. tty->ops->send_xchar(tty, ch);
  1020. up_read(&tty->termios_rwsem);
  1021. return 0;
  1022. }
  1023. if (tty_write_lock(tty, false) < 0)
  1024. return -ERESTARTSYS;
  1025. down_read(&tty->termios_rwsem);
  1026. if (was_stopped)
  1027. start_tty(tty);
  1028. tty->ops->write(tty, &ch, 1);
  1029. if (was_stopped)
  1030. stop_tty(tty);
  1031. up_read(&tty->termios_rwsem);
  1032. tty_write_unlock(tty);
  1033. return 0;
  1034. }
  1035. /**
  1036. * pty_line_name - generate name for a pty
  1037. * @driver: the tty driver in use
  1038. * @index: the minor number
  1039. * @p: output buffer of at least 6 bytes
  1040. *
  1041. * Generate a name from a @driver reference and write it to the output buffer
  1042. * @p.
  1043. *
  1044. * Locking: None
  1045. */
  1046. static void pty_line_name(struct tty_driver *driver, int index, char *p)
  1047. {
  1048. static const char ptychar[] = "pqrstuvwxyzabcde";
  1049. int i = index + driver->name_base;
  1050. /* ->name is initialized to "ttyp", but "tty" is expected */
  1051. sprintf(p, "%s%c%x",
  1052. driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
  1053. ptychar[i >> 4 & 0xf], i & 0xf);
  1054. }
  1055. /**
  1056. * tty_line_name - generate name for a tty
  1057. * @driver: the tty driver in use
  1058. * @index: the minor number
  1059. * @p: output buffer of at least 7 bytes
  1060. *
  1061. * Generate a name from a @driver reference and write it to the output buffer
  1062. * @p.
  1063. *
  1064. * Locking: None
  1065. */
  1066. static ssize_t tty_line_name(struct tty_driver *driver, int index, char *p)
  1067. {
  1068. if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE)
  1069. return sprintf(p, "%s", driver->name);
  1070. else
  1071. return sprintf(p, "%s%d", driver->name,
  1072. index + driver->name_base);
  1073. }
  1074. /**
  1075. * tty_driver_lookup_tty() - find an existing tty, if any
  1076. * @driver: the driver for the tty
  1077. * @file: file object
  1078. * @idx: the minor number
  1079. *
  1080. * Return: the tty, if found. If not found, return %NULL or ERR_PTR() if the
  1081. * driver lookup() method returns an error.
  1082. *
  1083. * Locking: tty_mutex must be held. If the tty is found, bump the tty kref.
  1084. */
  1085. static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
  1086. struct file *file, int idx)
  1087. {
  1088. struct tty_struct *tty;
  1089. if (driver->ops->lookup) {
  1090. if (!file)
  1091. tty = ERR_PTR(-EIO);
  1092. else
  1093. tty = driver->ops->lookup(driver, file, idx);
  1094. } else {
  1095. if (idx >= driver->num)
  1096. return ERR_PTR(-EINVAL);
  1097. tty = driver->ttys[idx];
  1098. }
  1099. if (!IS_ERR(tty))
  1100. tty_kref_get(tty);
  1101. return tty;
  1102. }
  1103. /**
  1104. * tty_init_termios - helper for termios setup
  1105. * @tty: the tty to set up
  1106. *
  1107. * Initialise the termios structure for this tty. This runs under the
  1108. * %tty_mutex currently so we can be relaxed about ordering.
  1109. */
  1110. void tty_init_termios(struct tty_struct *tty)
  1111. {
  1112. struct ktermios *tp;
  1113. int idx = tty->index;
  1114. if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
  1115. tty->termios = tty->driver->init_termios;
  1116. else {
  1117. /* Check for lazy saved data */
  1118. tp = tty->driver->termios[idx];
  1119. if (tp != NULL) {
  1120. tty->termios = *tp;
  1121. tty->termios.c_line = tty->driver->init_termios.c_line;
  1122. } else
  1123. tty->termios = tty->driver->init_termios;
  1124. }
  1125. /* Compatibility until drivers always set this */
  1126. tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
  1127. tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
  1128. }
  1129. EXPORT_SYMBOL_GPL(tty_init_termios);
  1130. /**
  1131. * tty_standard_install - usual tty->ops->install
  1132. * @driver: the driver for the tty
  1133. * @tty: the tty
  1134. *
  1135. * If the @driver overrides @tty->ops->install, it still can call this function
  1136. * to perform the standard install operations.
  1137. */
  1138. int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty)
  1139. {
  1140. tty_init_termios(tty);
  1141. tty_driver_kref_get(driver);
  1142. tty->count++;
  1143. driver->ttys[tty->index] = tty;
  1144. return 0;
  1145. }
  1146. EXPORT_SYMBOL_GPL(tty_standard_install);
  1147. /**
  1148. * tty_driver_install_tty() - install a tty entry in the driver
  1149. * @driver: the driver for the tty
  1150. * @tty: the tty
  1151. *
  1152. * Install a tty object into the driver tables. The @tty->index field will be
  1153. * set by the time this is called. This method is responsible for ensuring any
  1154. * need additional structures are allocated and configured.
  1155. *
  1156. * Locking: tty_mutex for now
  1157. */
  1158. static int tty_driver_install_tty(struct tty_driver *driver,
  1159. struct tty_struct *tty)
  1160. {
  1161. return driver->ops->install ? driver->ops->install(driver, tty) :
  1162. tty_standard_install(driver, tty);
  1163. }
  1164. /**
  1165. * tty_driver_remove_tty() - remove a tty from the driver tables
  1166. * @driver: the driver for the tty
  1167. * @tty: tty to remove
  1168. *
  1169. * Remove a tty object from the driver tables. The tty->index field will be set
  1170. * by the time this is called.
  1171. *
  1172. * Locking: tty_mutex for now
  1173. */
  1174. static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)
  1175. {
  1176. if (driver->ops->remove)
  1177. driver->ops->remove(driver, tty);
  1178. else
  1179. driver->ttys[tty->index] = NULL;
  1180. }
  1181. /**
  1182. * tty_reopen() - fast re-open of an open tty
  1183. * @tty: the tty to open
  1184. *
  1185. * Re-opens on master ptys are not allowed and return -%EIO.
  1186. *
  1187. * Locking: Caller must hold tty_lock
  1188. * Return: 0 on success, -errno on error.
  1189. */
  1190. static int tty_reopen(struct tty_struct *tty)
  1191. {
  1192. struct tty_driver *driver = tty->driver;
  1193. struct tty_ldisc *ld;
  1194. int retval = 0;
  1195. if (driver->type == TTY_DRIVER_TYPE_PTY &&
  1196. driver->subtype == PTY_TYPE_MASTER)
  1197. return -EIO;
  1198. if (!tty->count)
  1199. return -EAGAIN;
  1200. if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
  1201. return -EBUSY;
  1202. ld = tty_ldisc_ref_wait(tty);
  1203. if (ld) {
  1204. tty_ldisc_deref(ld);
  1205. } else {
  1206. retval = tty_ldisc_lock(tty, 5 * HZ);
  1207. if (retval)
  1208. return retval;
  1209. if (!tty->ldisc)
  1210. retval = tty_ldisc_reinit(tty, tty->termios.c_line);
  1211. tty_ldisc_unlock(tty);
  1212. }
  1213. if (retval == 0)
  1214. tty->count++;
  1215. return retval;
  1216. }
  1217. /**
  1218. * tty_init_dev - initialise a tty device
  1219. * @driver: tty driver we are opening a device on
  1220. * @idx: device index
  1221. *
  1222. * Prepare a tty device. This may not be a "new" clean device but could also be
  1223. * an active device. The pty drivers require special handling because of this.
  1224. *
  1225. * Locking:
  1226. * The function is called under the tty_mutex, which protects us from the
  1227. * tty struct or driver itself going away.
  1228. *
  1229. * On exit the tty device has the line discipline attached and a reference
  1230. * count of 1. If a pair was created for pty/tty use and the other was a pty
  1231. * master then it too has a reference count of 1.
  1232. *
  1233. * WSH 06/09/97: Rewritten to remove races and properly clean up after a failed
  1234. * open. The new code protects the open with a mutex, so it's really quite
  1235. * straightforward. The mutex locking can probably be relaxed for the (most
  1236. * common) case of reopening a tty.
  1237. *
  1238. * Return: new tty structure
  1239. */
  1240. struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx)
  1241. {
  1242. struct tty_struct *tty;
  1243. int retval;
  1244. /*
  1245. * First time open is complex, especially for PTY devices.
  1246. * This code guarantees that either everything succeeds and the
  1247. * TTY is ready for operation, or else the table slots are vacated
  1248. * and the allocated memory released. (Except that the termios
  1249. * may be retained.)
  1250. */
  1251. if (!try_module_get(driver->owner))
  1252. return ERR_PTR(-ENODEV);
  1253. tty = alloc_tty_struct(driver, idx);
  1254. if (!tty) {
  1255. retval = -ENOMEM;
  1256. goto err_module_put;
  1257. }
  1258. tty_lock(tty);
  1259. retval = tty_driver_install_tty(driver, tty);
  1260. if (retval < 0)
  1261. goto err_free_tty;
  1262. if (!tty->port)
  1263. tty->port = driver->ports[idx];
  1264. if (WARN_RATELIMIT(!tty->port,
  1265. "%s: %s driver does not set tty->port. This would crash the kernel. Fix the driver!\n",
  1266. __func__, tty->driver->name)) {
  1267. retval = -EINVAL;
  1268. goto err_release_lock;
  1269. }
  1270. retval = tty_ldisc_lock(tty, 5 * HZ);
  1271. if (retval)
  1272. goto err_release_lock;
  1273. tty->port->itty = tty;
  1274. /*
  1275. * Structures all installed ... call the ldisc open routines.
  1276. * If we fail here just call release_tty to clean up. No need
  1277. * to decrement the use counts, as release_tty doesn't care.
  1278. */
  1279. retval = tty_ldisc_setup(tty, tty->link);
  1280. if (retval)
  1281. goto err_release_tty;
  1282. tty_ldisc_unlock(tty);
  1283. /* Return the tty locked so that it cannot vanish under the caller */
  1284. return tty;
  1285. err_free_tty:
  1286. tty_unlock(tty);
  1287. free_tty_struct(tty);
  1288. err_module_put:
  1289. module_put(driver->owner);
  1290. return ERR_PTR(retval);
  1291. /* call the tty release_tty routine to clean out this slot */
  1292. err_release_tty:
  1293. tty_ldisc_unlock(tty);
  1294. tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n",
  1295. retval, idx);
  1296. err_release_lock:
  1297. tty_unlock(tty);
  1298. release_tty(tty, idx);
  1299. return ERR_PTR(retval);
  1300. }
  1301. /**
  1302. * tty_save_termios() - save tty termios data in driver table
  1303. * @tty: tty whose termios data to save
  1304. *
  1305. * Locking: Caller guarantees serialisation with tty_init_termios().
  1306. */
  1307. void tty_save_termios(struct tty_struct *tty)
  1308. {
  1309. struct ktermios *tp;
  1310. int idx = tty->index;
  1311. /* If the port is going to reset then it has no termios to save */
  1312. if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
  1313. return;
  1314. /* Stash the termios data */
  1315. tp = tty->driver->termios[idx];
  1316. if (tp == NULL) {
  1317. tp = kmalloc(sizeof(*tp), GFP_KERNEL);
  1318. if (tp == NULL)
  1319. return;
  1320. tty->driver->termios[idx] = tp;
  1321. }
  1322. *tp = tty->termios;
  1323. }
  1324. EXPORT_SYMBOL_GPL(tty_save_termios);
  1325. /**
  1326. * tty_flush_works - flush all works of a tty/pty pair
  1327. * @tty: tty device to flush works for (or either end of a pty pair)
  1328. *
  1329. * Sync flush all works belonging to @tty (and the 'other' tty).
  1330. */
  1331. static void tty_flush_works(struct tty_struct *tty)
  1332. {
  1333. flush_work(&tty->SAK_work);
  1334. flush_work(&tty->hangup_work);
  1335. if (tty->link) {
  1336. flush_work(&tty->link->SAK_work);
  1337. flush_work(&tty->link->hangup_work);
  1338. }
  1339. }
  1340. /**
  1341. * release_one_tty - release tty structure memory
  1342. * @work: work of tty we are obliterating
  1343. *
  1344. * Releases memory associated with a tty structure, and clears out the
  1345. * driver table slots. This function is called when a device is no longer
  1346. * in use. It also gets called when setup of a device fails.
  1347. *
  1348. * Locking:
  1349. * takes the file list lock internally when working on the list of ttys
  1350. * that the driver keeps.
  1351. *
  1352. * This method gets called from a work queue so that the driver private
  1353. * cleanup ops can sleep (needed for USB at least)
  1354. */
  1355. static void release_one_tty(struct work_struct *work)
  1356. {
  1357. struct tty_struct *tty =
  1358. container_of(work, struct tty_struct, hangup_work);
  1359. struct tty_driver *driver = tty->driver;
  1360. struct module *owner = driver->owner;
  1361. if (tty->ops->cleanup)
  1362. tty->ops->cleanup(tty);
  1363. tty_driver_kref_put(driver);
  1364. module_put(owner);
  1365. spin_lock(&tty->files_lock);
  1366. list_del_init(&tty->tty_files);
  1367. spin_unlock(&tty->files_lock);
  1368. put_pid(tty->ctrl.pgrp);
  1369. put_pid(tty->ctrl.session);
  1370. free_tty_struct(tty);
  1371. }
  1372. static void queue_release_one_tty(struct kref *kref)
  1373. {
  1374. struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
  1375. /* The hangup queue is now free so we can reuse it rather than
  1376. * waste a chunk of memory for each port.
  1377. */
  1378. INIT_WORK(&tty->hangup_work, release_one_tty);
  1379. schedule_work(&tty->hangup_work);
  1380. }
  1381. /**
  1382. * tty_kref_put - release a tty kref
  1383. * @tty: tty device
  1384. *
  1385. * Release a reference to the @tty device and if need be let the kref layer
  1386. * destruct the object for us.
  1387. */
  1388. void tty_kref_put(struct tty_struct *tty)
  1389. {
  1390. if (tty)
  1391. kref_put(&tty->kref, queue_release_one_tty);
  1392. }
  1393. EXPORT_SYMBOL(tty_kref_put);
  1394. /**
  1395. * release_tty - release tty structure memory
  1396. * @tty: tty device release
  1397. * @idx: index of the tty device release
  1398. *
  1399. * Release both @tty and a possible linked partner (think pty pair),
  1400. * and decrement the refcount of the backing module.
  1401. *
  1402. * Locking:
  1403. * tty_mutex
  1404. * takes the file list lock internally when working on the list of ttys
  1405. * that the driver keeps.
  1406. */
  1407. static void release_tty(struct tty_struct *tty, int idx)
  1408. {
  1409. /* This should always be true but check for the moment */
  1410. WARN_ON(tty->index != idx);
  1411. WARN_ON(!mutex_is_locked(&tty_mutex));
  1412. if (tty->ops->shutdown)
  1413. tty->ops->shutdown(tty);
  1414. tty_save_termios(tty);
  1415. tty_driver_remove_tty(tty->driver, tty);
  1416. if (tty->port)
  1417. tty->port->itty = NULL;
  1418. if (tty->link)
  1419. tty->link->port->itty = NULL;
  1420. if (tty->port)
  1421. tty_buffer_cancel_work(tty->port);
  1422. if (tty->link)
  1423. tty_buffer_cancel_work(tty->link->port);
  1424. tty_kref_put(tty->link);
  1425. tty_kref_put(tty);
  1426. }
  1427. /**
  1428. * tty_release_checks - check a tty before real release
  1429. * @tty: tty to check
  1430. * @idx: index of the tty
  1431. *
  1432. * Performs some paranoid checking before true release of the @tty. This is a
  1433. * no-op unless %TTY_PARANOIA_CHECK is defined.
  1434. */
  1435. static int tty_release_checks(struct tty_struct *tty, int idx)
  1436. {
  1437. #ifdef TTY_PARANOIA_CHECK
  1438. if (idx < 0 || idx >= tty->driver->num) {
  1439. tty_debug(tty, "bad idx %d\n", idx);
  1440. return -1;
  1441. }
  1442. /* not much to check for devpts */
  1443. if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)
  1444. return 0;
  1445. if (tty != tty->driver->ttys[idx]) {
  1446. tty_debug(tty, "bad driver table[%d] = %p\n",
  1447. idx, tty->driver->ttys[idx]);
  1448. return -1;
  1449. }
  1450. if (tty->driver->other) {
  1451. struct tty_struct *o_tty = tty->link;
  1452. if (o_tty != tty->driver->other->ttys[idx]) {
  1453. tty_debug(tty, "bad other table[%d] = %p\n",
  1454. idx, tty->driver->other->ttys[idx]);
  1455. return -1;
  1456. }
  1457. if (o_tty->link != tty) {
  1458. tty_debug(tty, "bad link = %p\n", o_tty->link);
  1459. return -1;
  1460. }
  1461. }
  1462. #endif
  1463. return 0;
  1464. }
  1465. /**
  1466. * tty_kclose - closes tty opened by tty_kopen
  1467. * @tty: tty device
  1468. *
  1469. * Performs the final steps to release and free a tty device. It is the same as
  1470. * tty_release_struct() except that it also resets %TTY_PORT_KOPENED flag on
  1471. * @tty->port.
  1472. */
  1473. void tty_kclose(struct tty_struct *tty)
  1474. {
  1475. /*
  1476. * Ask the line discipline code to release its structures
  1477. */
  1478. tty_ldisc_release(tty);
  1479. /* Wait for pending work before tty destruction commences */
  1480. tty_flush_works(tty);
  1481. tty_debug_hangup(tty, "freeing structure\n");
  1482. /*
  1483. * The release_tty function takes care of the details of clearing
  1484. * the slots and preserving the termios structure.
  1485. */
  1486. mutex_lock(&tty_mutex);
  1487. tty_port_set_kopened(tty->port, 0);
  1488. release_tty(tty, tty->index);
  1489. mutex_unlock(&tty_mutex);
  1490. }
  1491. EXPORT_SYMBOL_GPL(tty_kclose);
  1492. /**
  1493. * tty_release_struct - release a tty struct
  1494. * @tty: tty device
  1495. * @idx: index of the tty
  1496. *
  1497. * Performs the final steps to release and free a tty device. It is roughly the
  1498. * reverse of tty_init_dev().
  1499. */
  1500. void tty_release_struct(struct tty_struct *tty, int idx)
  1501. {
  1502. /*
  1503. * Ask the line discipline code to release its structures
  1504. */
  1505. tty_ldisc_release(tty);
  1506. /* Wait for pending work before tty destruction commmences */
  1507. tty_flush_works(tty);
  1508. tty_debug_hangup(tty, "freeing structure\n");
  1509. /*
  1510. * The release_tty function takes care of the details of clearing
  1511. * the slots and preserving the termios structure.
  1512. */
  1513. mutex_lock(&tty_mutex);
  1514. release_tty(tty, idx);
  1515. mutex_unlock(&tty_mutex);
  1516. }
  1517. EXPORT_SYMBOL_GPL(tty_release_struct);
  1518. /**
  1519. * tty_release - vfs callback for close
  1520. * @inode: inode of tty
  1521. * @filp: file pointer for handle to tty
  1522. *
  1523. * Called the last time each file handle is closed that references this tty.
  1524. * There may however be several such references.
  1525. *
  1526. * Locking:
  1527. * Takes BKL. See tty_release_dev().
  1528. *
  1529. * Even releasing the tty structures is a tricky business. We have to be very
  1530. * careful that the structures are all released at the same time, as interrupts
  1531. * might otherwise get the wrong pointers.
  1532. *
  1533. * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
  1534. * lead to double frees or releasing memory still in use.
  1535. */
  1536. int tty_release(struct inode *inode, struct file *filp)
  1537. {
  1538. struct tty_struct *tty = file_tty(filp);
  1539. struct tty_struct *o_tty = NULL;
  1540. int do_sleep, final;
  1541. int idx;
  1542. long timeout = 0;
  1543. int once = 1;
  1544. if (tty_paranoia_check(tty, inode, __func__))
  1545. return 0;
  1546. tty_lock(tty);
  1547. check_tty_count(tty, __func__);
  1548. __tty_fasync(-1, filp, 0);
  1549. idx = tty->index;
  1550. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  1551. tty->driver->subtype == PTY_TYPE_MASTER)
  1552. o_tty = tty->link;
  1553. if (tty_release_checks(tty, idx)) {
  1554. tty_unlock(tty);
  1555. return 0;
  1556. }
  1557. tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count);
  1558. if (tty->ops->close)
  1559. tty->ops->close(tty, filp);
  1560. /* If tty is pty master, lock the slave pty (stable lock order) */
  1561. tty_lock_slave(o_tty);
  1562. /*
  1563. * Sanity check: if tty->count is going to zero, there shouldn't be
  1564. * any waiters on tty->read_wait or tty->write_wait. We test the
  1565. * wait queues and kick everyone out _before_ actually starting to
  1566. * close. This ensures that we won't block while releasing the tty
  1567. * structure.
  1568. *
  1569. * The test for the o_tty closing is necessary, since the master and
  1570. * slave sides may close in any order. If the slave side closes out
  1571. * first, its count will be one, since the master side holds an open.
  1572. * Thus this test wouldn't be triggered at the time the slave closed,
  1573. * so we do it now.
  1574. */
  1575. while (1) {
  1576. do_sleep = 0;
  1577. if (tty->count <= 1) {
  1578. if (waitqueue_active(&tty->read_wait)) {
  1579. wake_up_poll(&tty->read_wait, EPOLLIN);
  1580. do_sleep++;
  1581. }
  1582. if (waitqueue_active(&tty->write_wait)) {
  1583. wake_up_poll(&tty->write_wait, EPOLLOUT);
  1584. do_sleep++;
  1585. }
  1586. }
  1587. if (o_tty && o_tty->count <= 1) {
  1588. if (waitqueue_active(&o_tty->read_wait)) {
  1589. wake_up_poll(&o_tty->read_wait, EPOLLIN);
  1590. do_sleep++;
  1591. }
  1592. if (waitqueue_active(&o_tty->write_wait)) {
  1593. wake_up_poll(&o_tty->write_wait, EPOLLOUT);
  1594. do_sleep++;
  1595. }
  1596. }
  1597. if (!do_sleep)
  1598. break;
  1599. if (once) {
  1600. once = 0;
  1601. tty_warn(tty, "read/write wait queue active!\n");
  1602. }
  1603. schedule_timeout_killable(timeout);
  1604. if (timeout < 120 * HZ)
  1605. timeout = 2 * timeout + 1;
  1606. else
  1607. timeout = MAX_SCHEDULE_TIMEOUT;
  1608. }
  1609. if (o_tty) {
  1610. if (--o_tty->count < 0) {
  1611. tty_warn(tty, "bad slave count (%d)\n", o_tty->count);
  1612. o_tty->count = 0;
  1613. }
  1614. }
  1615. if (--tty->count < 0) {
  1616. tty_warn(tty, "bad tty->count (%d)\n", tty->count);
  1617. tty->count = 0;
  1618. }
  1619. /*
  1620. * We've decremented tty->count, so we need to remove this file
  1621. * descriptor off the tty->tty_files list; this serves two
  1622. * purposes:
  1623. * - check_tty_count sees the correct number of file descriptors
  1624. * associated with this tty.
  1625. * - do_tty_hangup no longer sees this file descriptor as
  1626. * something that needs to be handled for hangups.
  1627. */
  1628. tty_del_file(filp);
  1629. /*
  1630. * Perform some housekeeping before deciding whether to return.
  1631. *
  1632. * If _either_ side is closing, make sure there aren't any
  1633. * processes that still think tty or o_tty is their controlling
  1634. * tty.
  1635. */
  1636. if (!tty->count) {
  1637. read_lock(&tasklist_lock);
  1638. session_clear_tty(tty->ctrl.session);
  1639. if (o_tty)
  1640. session_clear_tty(o_tty->ctrl.session);
  1641. read_unlock(&tasklist_lock);
  1642. }
  1643. /* check whether both sides are closing ... */
  1644. final = !tty->count && !(o_tty && o_tty->count);
  1645. tty_unlock_slave(o_tty);
  1646. tty_unlock(tty);
  1647. /* At this point, the tty->count == 0 should ensure a dead tty
  1648. * cannot be re-opened by a racing opener.
  1649. */
  1650. if (!final)
  1651. return 0;
  1652. tty_debug_hangup(tty, "final close\n");
  1653. tty_release_struct(tty, idx);
  1654. return 0;
  1655. }
  1656. /**
  1657. * tty_open_current_tty - get locked tty of current task
  1658. * @device: device number
  1659. * @filp: file pointer to tty
  1660. * @return: locked tty of the current task iff @device is /dev/tty
  1661. *
  1662. * Performs a re-open of the current task's controlling tty.
  1663. *
  1664. * We cannot return driver and index like for the other nodes because devpts
  1665. * will not work then. It expects inodes to be from devpts FS.
  1666. */
  1667. static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
  1668. {
  1669. struct tty_struct *tty;
  1670. int retval;
  1671. if (device != MKDEV(TTYAUX_MAJOR, 0))
  1672. return NULL;
  1673. tty = get_current_tty();
  1674. if (!tty)
  1675. return ERR_PTR(-ENXIO);
  1676. filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
  1677. /* noctty = 1; */
  1678. tty_lock(tty);
  1679. tty_kref_put(tty); /* safe to drop the kref now */
  1680. retval = tty_reopen(tty);
  1681. if (retval < 0) {
  1682. tty_unlock(tty);
  1683. tty = ERR_PTR(retval);
  1684. }
  1685. return tty;
  1686. }
  1687. /**
  1688. * tty_lookup_driver - lookup a tty driver for a given device file
  1689. * @device: device number
  1690. * @filp: file pointer to tty
  1691. * @index: index for the device in the @return driver
  1692. *
  1693. * If returned value is not erroneous, the caller is responsible to decrement
  1694. * the refcount by tty_driver_kref_put().
  1695. *
  1696. * Locking: %tty_mutex protects get_tty_driver()
  1697. *
  1698. * Return: driver for this inode (with increased refcount)
  1699. */
  1700. static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp,
  1701. int *index)
  1702. {
  1703. struct tty_driver *driver = NULL;
  1704. switch (device) {
  1705. #ifdef CONFIG_VT
  1706. case MKDEV(TTY_MAJOR, 0): {
  1707. extern struct tty_driver *console_driver;
  1708. driver = tty_driver_kref_get(console_driver);
  1709. *index = fg_console;
  1710. break;
  1711. }
  1712. #endif
  1713. case MKDEV(TTYAUX_MAJOR, 1): {
  1714. struct tty_driver *console_driver = console_device(index);
  1715. if (console_driver) {
  1716. driver = tty_driver_kref_get(console_driver);
  1717. if (driver && filp) {
  1718. /* Don't let /dev/console block */
  1719. filp->f_flags |= O_NONBLOCK;
  1720. break;
  1721. }
  1722. }
  1723. if (driver)
  1724. tty_driver_kref_put(driver);
  1725. return ERR_PTR(-ENODEV);
  1726. }
  1727. default:
  1728. driver = get_tty_driver(device, index);
  1729. if (!driver)
  1730. return ERR_PTR(-ENODEV);
  1731. break;
  1732. }
  1733. return driver;
  1734. }
  1735. static struct tty_struct *tty_kopen(dev_t device, int shared)
  1736. {
  1737. struct tty_struct *tty;
  1738. struct tty_driver *driver;
  1739. int index = -1;
  1740. mutex_lock(&tty_mutex);
  1741. driver = tty_lookup_driver(device, NULL, &index);
  1742. if (IS_ERR(driver)) {
  1743. mutex_unlock(&tty_mutex);
  1744. return ERR_CAST(driver);
  1745. }
  1746. /* check whether we're reopening an existing tty */
  1747. tty = tty_driver_lookup_tty(driver, NULL, index);
  1748. if (IS_ERR(tty) || shared)
  1749. goto out;
  1750. if (tty) {
  1751. /* drop kref from tty_driver_lookup_tty() */
  1752. tty_kref_put(tty);
  1753. tty = ERR_PTR(-EBUSY);
  1754. } else { /* tty_init_dev returns tty with the tty_lock held */
  1755. tty = tty_init_dev(driver, index);
  1756. if (IS_ERR(tty))
  1757. goto out;
  1758. tty_port_set_kopened(tty->port, 1);
  1759. }
  1760. out:
  1761. mutex_unlock(&tty_mutex);
  1762. tty_driver_kref_put(driver);
  1763. return tty;
  1764. }
  1765. /**
  1766. * tty_kopen_exclusive - open a tty device for kernel
  1767. * @device: dev_t of device to open
  1768. *
  1769. * Opens tty exclusively for kernel. Performs the driver lookup, makes sure
  1770. * it's not already opened and performs the first-time tty initialization.
  1771. *
  1772. * Claims the global %tty_mutex to serialize:
  1773. * * concurrent first-time tty initialization
  1774. * * concurrent tty driver removal w/ lookup
  1775. * * concurrent tty removal from driver table
  1776. *
  1777. * Return: the locked initialized &tty_struct
  1778. */
  1779. struct tty_struct *tty_kopen_exclusive(dev_t device)
  1780. {
  1781. return tty_kopen(device, 0);
  1782. }
  1783. EXPORT_SYMBOL_GPL(tty_kopen_exclusive);
  1784. /**
  1785. * tty_kopen_shared - open a tty device for shared in-kernel use
  1786. * @device: dev_t of device to open
  1787. *
  1788. * Opens an already existing tty for in-kernel use. Compared to
  1789. * tty_kopen_exclusive() above it doesn't ensure to be the only user.
  1790. *
  1791. * Locking: identical to tty_kopen() above.
  1792. */
  1793. struct tty_struct *tty_kopen_shared(dev_t device)
  1794. {
  1795. return tty_kopen(device, 1);
  1796. }
  1797. EXPORT_SYMBOL_GPL(tty_kopen_shared);
  1798. /**
  1799. * tty_open_by_driver - open a tty device
  1800. * @device: dev_t of device to open
  1801. * @filp: file pointer to tty
  1802. *
  1803. * Performs the driver lookup, checks for a reopen, or otherwise performs the
  1804. * first-time tty initialization.
  1805. *
  1806. *
  1807. * Claims the global tty_mutex to serialize:
  1808. * * concurrent first-time tty initialization
  1809. * * concurrent tty driver removal w/ lookup
  1810. * * concurrent tty removal from driver table
  1811. *
  1812. * Return: the locked initialized or re-opened &tty_struct
  1813. */
  1814. static struct tty_struct *tty_open_by_driver(dev_t device,
  1815. struct file *filp)
  1816. {
  1817. struct tty_struct *tty;
  1818. struct tty_driver *driver = NULL;
  1819. int index = -1;
  1820. int retval;
  1821. mutex_lock(&tty_mutex);
  1822. driver = tty_lookup_driver(device, filp, &index);
  1823. if (IS_ERR(driver)) {
  1824. mutex_unlock(&tty_mutex);
  1825. return ERR_CAST(driver);
  1826. }
  1827. /* check whether we're reopening an existing tty */
  1828. tty = tty_driver_lookup_tty(driver, filp, index);
  1829. if (IS_ERR(tty)) {
  1830. mutex_unlock(&tty_mutex);
  1831. goto out;
  1832. }
  1833. if (tty) {
  1834. if (tty_port_kopened(tty->port)) {
  1835. tty_kref_put(tty);
  1836. mutex_unlock(&tty_mutex);
  1837. tty = ERR_PTR(-EBUSY);
  1838. goto out;
  1839. }
  1840. mutex_unlock(&tty_mutex);
  1841. retval = tty_lock_interruptible(tty);
  1842. tty_kref_put(tty); /* drop kref from tty_driver_lookup_tty() */
  1843. if (retval) {
  1844. if (retval == -EINTR)
  1845. retval = -ERESTARTSYS;
  1846. tty = ERR_PTR(retval);
  1847. goto out;
  1848. }
  1849. retval = tty_reopen(tty);
  1850. if (retval < 0) {
  1851. tty_unlock(tty);
  1852. tty = ERR_PTR(retval);
  1853. }
  1854. } else { /* Returns with the tty_lock held for now */
  1855. tty = tty_init_dev(driver, index);
  1856. mutex_unlock(&tty_mutex);
  1857. }
  1858. out:
  1859. tty_driver_kref_put(driver);
  1860. return tty;
  1861. }
  1862. /**
  1863. * tty_open - open a tty device
  1864. * @inode: inode of device file
  1865. * @filp: file pointer to tty
  1866. *
  1867. * tty_open() and tty_release() keep up the tty count that contains the number
  1868. * of opens done on a tty. We cannot use the inode-count, as different inodes
  1869. * might point to the same tty.
  1870. *
  1871. * Open-counting is needed for pty masters, as well as for keeping track of
  1872. * serial lines: DTR is dropped when the last close happens.
  1873. * (This is not done solely through tty->count, now. - Ted 1/27/92)
  1874. *
  1875. * The termios state of a pty is reset on the first open so that settings don't
  1876. * persist across reuse.
  1877. *
  1878. * Locking:
  1879. * * %tty_mutex protects tty, tty_lookup_driver() and tty_init_dev().
  1880. * * @tty->count should protect the rest.
  1881. * * ->siglock protects ->signal/->sighand
  1882. *
  1883. * Note: the tty_unlock/lock cases without a ref are only safe due to %tty_mutex
  1884. */
  1885. static int tty_open(struct inode *inode, struct file *filp)
  1886. {
  1887. struct tty_struct *tty;
  1888. int noctty, retval;
  1889. dev_t device = inode->i_rdev;
  1890. unsigned saved_flags = filp->f_flags;
  1891. nonseekable_open(inode, filp);
  1892. retry_open:
  1893. retval = tty_alloc_file(filp);
  1894. if (retval)
  1895. return -ENOMEM;
  1896. tty = tty_open_current_tty(device, filp);
  1897. if (!tty)
  1898. tty = tty_open_by_driver(device, filp);
  1899. if (IS_ERR(tty)) {
  1900. tty_free_file(filp);
  1901. retval = PTR_ERR(tty);
  1902. if (retval != -EAGAIN || signal_pending(current))
  1903. return retval;
  1904. schedule();
  1905. goto retry_open;
  1906. }
  1907. tty_add_file(tty, filp);
  1908. check_tty_count(tty, __func__);
  1909. tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
  1910. if (tty->ops->open)
  1911. retval = tty->ops->open(tty, filp);
  1912. else
  1913. retval = -ENODEV;
  1914. filp->f_flags = saved_flags;
  1915. if (retval) {
  1916. tty_debug_hangup(tty, "open error %d, releasing\n", retval);
  1917. tty_unlock(tty); /* need to call tty_release without BTM */
  1918. tty_release(inode, filp);
  1919. if (retval != -ERESTARTSYS)
  1920. return retval;
  1921. if (signal_pending(current))
  1922. return retval;
  1923. schedule();
  1924. /*
  1925. * Need to reset f_op in case a hangup happened.
  1926. */
  1927. if (tty_hung_up_p(filp))
  1928. filp->f_op = &tty_fops;
  1929. goto retry_open;
  1930. }
  1931. clear_bit(TTY_HUPPED, &tty->flags);
  1932. noctty = (filp->f_flags & O_NOCTTY) ||
  1933. (IS_ENABLED(CONFIG_VT) && device == MKDEV(TTY_MAJOR, 0)) ||
  1934. device == MKDEV(TTYAUX_MAJOR, 1) ||
  1935. (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  1936. tty->driver->subtype == PTY_TYPE_MASTER);
  1937. if (!noctty)
  1938. tty_open_proc_set_tty(filp, tty);
  1939. tty_unlock(tty);
  1940. return 0;
  1941. }
  1942. /**
  1943. * tty_poll - check tty status
  1944. * @filp: file being polled
  1945. * @wait: poll wait structures to update
  1946. *
  1947. * Call the line discipline polling method to obtain the poll status of the
  1948. * device.
  1949. *
  1950. * Locking: locks called line discipline but ldisc poll method may be
  1951. * re-entered freely by other callers.
  1952. */
  1953. static __poll_t tty_poll(struct file *filp, poll_table *wait)
  1954. {
  1955. struct tty_struct *tty = file_tty(filp);
  1956. struct tty_ldisc *ld;
  1957. __poll_t ret = 0;
  1958. if (tty_paranoia_check(tty, file_inode(filp), "tty_poll"))
  1959. return 0;
  1960. ld = tty_ldisc_ref_wait(tty);
  1961. if (!ld)
  1962. return hung_up_tty_poll(filp, wait);
  1963. if (ld->ops->poll)
  1964. ret = ld->ops->poll(tty, filp, wait);
  1965. tty_ldisc_deref(ld);
  1966. return ret;
  1967. }
  1968. static int __tty_fasync(int fd, struct file *filp, int on)
  1969. {
  1970. struct tty_struct *tty = file_tty(filp);
  1971. unsigned long flags;
  1972. int retval = 0;
  1973. if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync"))
  1974. goto out;
  1975. if (on) {
  1976. retval = file_f_owner_allocate(filp);
  1977. if (retval)
  1978. goto out;
  1979. }
  1980. retval = fasync_helper(fd, filp, on, &tty->fasync);
  1981. if (retval <= 0)
  1982. goto out;
  1983. if (on) {
  1984. enum pid_type type;
  1985. struct pid *pid;
  1986. spin_lock_irqsave(&tty->ctrl.lock, flags);
  1987. if (tty->ctrl.pgrp) {
  1988. pid = tty->ctrl.pgrp;
  1989. type = PIDTYPE_PGID;
  1990. } else {
  1991. pid = task_pid(current);
  1992. type = PIDTYPE_TGID;
  1993. }
  1994. get_pid(pid);
  1995. spin_unlock_irqrestore(&tty->ctrl.lock, flags);
  1996. __f_setown(filp, pid, type, 0);
  1997. put_pid(pid);
  1998. retval = 0;
  1999. }
  2000. out:
  2001. return retval;
  2002. }
  2003. static int tty_fasync(int fd, struct file *filp, int on)
  2004. {
  2005. struct tty_struct *tty = file_tty(filp);
  2006. int retval = -ENOTTY;
  2007. tty_lock(tty);
  2008. if (!tty_hung_up_p(filp))
  2009. retval = __tty_fasync(fd, filp, on);
  2010. tty_unlock(tty);
  2011. return retval;
  2012. }
  2013. static bool tty_legacy_tiocsti __read_mostly = IS_ENABLED(CONFIG_LEGACY_TIOCSTI);
  2014. /**
  2015. * tiocsti - fake input character
  2016. * @tty: tty to fake input into
  2017. * @p: pointer to character
  2018. *
  2019. * Fake input to a tty device. Does the necessary locking and input management.
  2020. *
  2021. * FIXME: does not honour flow control ??
  2022. *
  2023. * Locking:
  2024. * * Called functions take tty_ldiscs_lock
  2025. * * current->signal->tty check is safe without locks
  2026. */
  2027. static int tiocsti(struct tty_struct *tty, u8 __user *p)
  2028. {
  2029. struct tty_ldisc *ld;
  2030. u8 ch;
  2031. if (!tty_legacy_tiocsti && !capable(CAP_SYS_ADMIN))
  2032. return -EIO;
  2033. if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
  2034. return -EPERM;
  2035. if (get_user(ch, p))
  2036. return -EFAULT;
  2037. tty_audit_tiocsti(tty, ch);
  2038. ld = tty_ldisc_ref_wait(tty);
  2039. if (!ld)
  2040. return -EIO;
  2041. tty_buffer_lock_exclusive(tty->port);
  2042. if (ld->ops->receive_buf)
  2043. ld->ops->receive_buf(tty, &ch, NULL, 1);
  2044. tty_buffer_unlock_exclusive(tty->port);
  2045. tty_ldisc_deref(ld);
  2046. return 0;
  2047. }
  2048. /**
  2049. * tiocgwinsz - implement window query ioctl
  2050. * @tty: tty
  2051. * @arg: user buffer for result
  2052. *
  2053. * Copies the kernel idea of the window size into the user buffer.
  2054. *
  2055. * Locking: @tty->winsize_mutex is taken to ensure the winsize data is
  2056. * consistent.
  2057. */
  2058. static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
  2059. {
  2060. int err;
  2061. mutex_lock(&tty->winsize_mutex);
  2062. err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
  2063. mutex_unlock(&tty->winsize_mutex);
  2064. return err ? -EFAULT : 0;
  2065. }
  2066. /**
  2067. * tty_do_resize - resize event
  2068. * @tty: tty being resized
  2069. * @ws: new dimensions
  2070. *
  2071. * Update the termios variables and send the necessary signals to peform a
  2072. * terminal resize correctly.
  2073. */
  2074. int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
  2075. {
  2076. struct pid *pgrp;
  2077. /* Lock the tty */
  2078. mutex_lock(&tty->winsize_mutex);
  2079. if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
  2080. goto done;
  2081. /* Signal the foreground process group */
  2082. pgrp = tty_get_pgrp(tty);
  2083. if (pgrp)
  2084. kill_pgrp(pgrp, SIGWINCH, 1);
  2085. put_pid(pgrp);
  2086. tty->winsize = *ws;
  2087. done:
  2088. mutex_unlock(&tty->winsize_mutex);
  2089. return 0;
  2090. }
  2091. EXPORT_SYMBOL(tty_do_resize);
  2092. /**
  2093. * tiocswinsz - implement window size set ioctl
  2094. * @tty: tty side of tty
  2095. * @arg: user buffer for result
  2096. *
  2097. * Copies the user idea of the window size to the kernel. Traditionally this is
  2098. * just advisory information but for the Linux console it actually has driver
  2099. * level meaning and triggers a VC resize.
  2100. *
  2101. * Locking:
  2102. * Driver dependent. The default do_resize method takes the tty termios
  2103. * mutex and ctrl.lock. The console takes its own lock then calls into the
  2104. * default method.
  2105. */
  2106. static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
  2107. {
  2108. struct winsize tmp_ws;
  2109. if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
  2110. return -EFAULT;
  2111. if (tty->ops->resize)
  2112. return tty->ops->resize(tty, &tmp_ws);
  2113. else
  2114. return tty_do_resize(tty, &tmp_ws);
  2115. }
  2116. /**
  2117. * tioccons - allow admin to move logical console
  2118. * @file: the file to become console
  2119. *
  2120. * Allow the administrator to move the redirected console device.
  2121. *
  2122. * Locking: uses redirect_lock to guard the redirect information
  2123. */
  2124. static int tioccons(struct file *file)
  2125. {
  2126. if (!capable(CAP_SYS_ADMIN))
  2127. return -EPERM;
  2128. if (file->f_op->write_iter == redirected_tty_write) {
  2129. struct file *f;
  2130. spin_lock(&redirect_lock);
  2131. f = redirect;
  2132. redirect = NULL;
  2133. spin_unlock(&redirect_lock);
  2134. if (f)
  2135. fput(f);
  2136. return 0;
  2137. }
  2138. if (file->f_op->write_iter != tty_write)
  2139. return -ENOTTY;
  2140. if (!(file->f_mode & FMODE_WRITE))
  2141. return -EBADF;
  2142. if (!(file->f_mode & FMODE_CAN_WRITE))
  2143. return -EINVAL;
  2144. spin_lock(&redirect_lock);
  2145. if (redirect) {
  2146. spin_unlock(&redirect_lock);
  2147. return -EBUSY;
  2148. }
  2149. redirect = get_file(file);
  2150. spin_unlock(&redirect_lock);
  2151. return 0;
  2152. }
  2153. /**
  2154. * tiocsetd - set line discipline
  2155. * @tty: tty device
  2156. * @p: pointer to user data
  2157. *
  2158. * Set the line discipline according to user request.
  2159. *
  2160. * Locking: see tty_set_ldisc(), this function is just a helper
  2161. */
  2162. static int tiocsetd(struct tty_struct *tty, int __user *p)
  2163. {
  2164. int disc;
  2165. int ret;
  2166. if (get_user(disc, p))
  2167. return -EFAULT;
  2168. ret = tty_set_ldisc(tty, disc);
  2169. return ret;
  2170. }
  2171. /**
  2172. * tiocgetd - get line discipline
  2173. * @tty: tty device
  2174. * @p: pointer to user data
  2175. *
  2176. * Retrieves the line discipline id directly from the ldisc.
  2177. *
  2178. * Locking: waits for ldisc reference (in case the line discipline is changing
  2179. * or the @tty is being hungup)
  2180. */
  2181. static int tiocgetd(struct tty_struct *tty, int __user *p)
  2182. {
  2183. struct tty_ldisc *ld;
  2184. int ret;
  2185. ld = tty_ldisc_ref_wait(tty);
  2186. if (!ld)
  2187. return -EIO;
  2188. ret = put_user(ld->ops->num, p);
  2189. tty_ldisc_deref(ld);
  2190. return ret;
  2191. }
  2192. /**
  2193. * send_break - performed time break
  2194. * @tty: device to break on
  2195. * @duration: timeout in mS
  2196. *
  2197. * Perform a timed break on hardware that lacks its own driver level timed
  2198. * break functionality.
  2199. *
  2200. * Locking:
  2201. * @tty->atomic_write_lock serializes
  2202. */
  2203. static int send_break(struct tty_struct *tty, unsigned int duration)
  2204. {
  2205. int retval;
  2206. if (tty->ops->break_ctl == NULL)
  2207. return 0;
  2208. if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
  2209. return tty->ops->break_ctl(tty, duration);
  2210. /* Do the work ourselves */
  2211. if (tty_write_lock(tty, false) < 0)
  2212. return -EINTR;
  2213. retval = tty->ops->break_ctl(tty, -1);
  2214. if (!retval) {
  2215. msleep_interruptible(duration);
  2216. retval = tty->ops->break_ctl(tty, 0);
  2217. } else if (retval == -EOPNOTSUPP) {
  2218. /* some drivers can tell only dynamically */
  2219. retval = 0;
  2220. }
  2221. tty_write_unlock(tty);
  2222. if (signal_pending(current))
  2223. retval = -EINTR;
  2224. return retval;
  2225. }
  2226. /**
  2227. * tty_get_tiocm - get tiocm status register
  2228. * @tty: tty device
  2229. *
  2230. * Obtain the modem status bits from the tty driver if the feature
  2231. * is supported.
  2232. */
  2233. int tty_get_tiocm(struct tty_struct *tty)
  2234. {
  2235. int retval = -ENOTTY;
  2236. if (tty->ops->tiocmget)
  2237. retval = tty->ops->tiocmget(tty);
  2238. return retval;
  2239. }
  2240. EXPORT_SYMBOL_GPL(tty_get_tiocm);
  2241. /**
  2242. * tty_tiocmget - get modem status
  2243. * @tty: tty device
  2244. * @p: pointer to result
  2245. *
  2246. * Obtain the modem status bits from the tty driver if the feature is
  2247. * supported. Return -%ENOTTY if it is not available.
  2248. *
  2249. * Locking: none (up to the driver)
  2250. */
  2251. static int tty_tiocmget(struct tty_struct *tty, int __user *p)
  2252. {
  2253. int retval;
  2254. retval = tty_get_tiocm(tty);
  2255. if (retval >= 0)
  2256. retval = put_user(retval, p);
  2257. return retval;
  2258. }
  2259. /**
  2260. * tty_tiocmset - set modem status
  2261. * @tty: tty device
  2262. * @cmd: command - clear bits, set bits or set all
  2263. * @p: pointer to desired bits
  2264. *
  2265. * Set the modem status bits from the tty driver if the feature
  2266. * is supported. Return -%ENOTTY if it is not available.
  2267. *
  2268. * Locking: none (up to the driver)
  2269. */
  2270. static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd,
  2271. unsigned __user *p)
  2272. {
  2273. int retval;
  2274. unsigned int set, clear, val;
  2275. if (tty->ops->tiocmset == NULL)
  2276. return -ENOTTY;
  2277. retval = get_user(val, p);
  2278. if (retval)
  2279. return retval;
  2280. set = clear = 0;
  2281. switch (cmd) {
  2282. case TIOCMBIS:
  2283. set = val;
  2284. break;
  2285. case TIOCMBIC:
  2286. clear = val;
  2287. break;
  2288. case TIOCMSET:
  2289. set = val;
  2290. clear = ~val;
  2291. break;
  2292. }
  2293. set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
  2294. clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
  2295. return tty->ops->tiocmset(tty, set, clear);
  2296. }
  2297. /**
  2298. * tty_get_icount - get tty statistics
  2299. * @tty: tty device
  2300. * @icount: output parameter
  2301. *
  2302. * Gets a copy of the @tty's icount statistics.
  2303. *
  2304. * Locking: none (up to the driver)
  2305. */
  2306. int tty_get_icount(struct tty_struct *tty,
  2307. struct serial_icounter_struct *icount)
  2308. {
  2309. memset(icount, 0, sizeof(*icount));
  2310. if (tty->ops->get_icount)
  2311. return tty->ops->get_icount(tty, icount);
  2312. else
  2313. return -ENOTTY;
  2314. }
  2315. EXPORT_SYMBOL_GPL(tty_get_icount);
  2316. static int tty_tiocgicount(struct tty_struct *tty, void __user *arg)
  2317. {
  2318. struct serial_icounter_struct icount;
  2319. int retval;
  2320. retval = tty_get_icount(tty, &icount);
  2321. if (retval != 0)
  2322. return retval;
  2323. if (copy_to_user(arg, &icount, sizeof(icount)))
  2324. return -EFAULT;
  2325. return 0;
  2326. }
  2327. static int tty_set_serial(struct tty_struct *tty, struct serial_struct *ss)
  2328. {
  2329. char comm[TASK_COMM_LEN];
  2330. int flags;
  2331. flags = ss->flags & ASYNC_DEPRECATED;
  2332. if (flags)
  2333. pr_warn_ratelimited("%s: '%s' is using deprecated serial flags (with no effect): %.8x\n",
  2334. __func__, get_task_comm(comm, current), flags);
  2335. if (!tty->ops->set_serial)
  2336. return -ENOTTY;
  2337. return tty->ops->set_serial(tty, ss);
  2338. }
  2339. static int tty_tiocsserial(struct tty_struct *tty, struct serial_struct __user *ss)
  2340. {
  2341. struct serial_struct v;
  2342. if (copy_from_user(&v, ss, sizeof(*ss)))
  2343. return -EFAULT;
  2344. return tty_set_serial(tty, &v);
  2345. }
  2346. static int tty_tiocgserial(struct tty_struct *tty, struct serial_struct __user *ss)
  2347. {
  2348. struct serial_struct v;
  2349. int err;
  2350. memset(&v, 0, sizeof(v));
  2351. if (!tty->ops->get_serial)
  2352. return -ENOTTY;
  2353. err = tty->ops->get_serial(tty, &v);
  2354. if (!err && copy_to_user(ss, &v, sizeof(v)))
  2355. err = -EFAULT;
  2356. return err;
  2357. }
  2358. /*
  2359. * if pty, return the slave side (real_tty)
  2360. * otherwise, return self
  2361. */
  2362. static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
  2363. {
  2364. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  2365. tty->driver->subtype == PTY_TYPE_MASTER)
  2366. tty = tty->link;
  2367. return tty;
  2368. }
  2369. /*
  2370. * Split this up, as gcc can choke on it otherwise..
  2371. */
  2372. long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2373. {
  2374. struct tty_struct *tty = file_tty(file);
  2375. struct tty_struct *real_tty;
  2376. void __user *p = (void __user *)arg;
  2377. int retval;
  2378. struct tty_ldisc *ld;
  2379. if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
  2380. return -EINVAL;
  2381. real_tty = tty_pair_get_tty(tty);
  2382. /*
  2383. * Factor out some common prep work
  2384. */
  2385. switch (cmd) {
  2386. case TIOCSETD:
  2387. case TIOCSBRK:
  2388. case TIOCCBRK:
  2389. case TCSBRK:
  2390. case TCSBRKP:
  2391. retval = tty_check_change(tty);
  2392. if (retval)
  2393. return retval;
  2394. if (cmd != TIOCCBRK) {
  2395. tty_wait_until_sent(tty, 0);
  2396. if (signal_pending(current))
  2397. return -EINTR;
  2398. }
  2399. break;
  2400. }
  2401. /*
  2402. * Now do the stuff.
  2403. */
  2404. switch (cmd) {
  2405. case TIOCSTI:
  2406. return tiocsti(tty, p);
  2407. case TIOCGWINSZ:
  2408. return tiocgwinsz(real_tty, p);
  2409. case TIOCSWINSZ:
  2410. return tiocswinsz(real_tty, p);
  2411. case TIOCCONS:
  2412. return real_tty != tty ? -EINVAL : tioccons(file);
  2413. case TIOCEXCL:
  2414. set_bit(TTY_EXCLUSIVE, &tty->flags);
  2415. return 0;
  2416. case TIOCNXCL:
  2417. clear_bit(TTY_EXCLUSIVE, &tty->flags);
  2418. return 0;
  2419. case TIOCGEXCL:
  2420. {
  2421. int excl = test_bit(TTY_EXCLUSIVE, &tty->flags);
  2422. return put_user(excl, (int __user *)p);
  2423. }
  2424. case TIOCGETD:
  2425. return tiocgetd(tty, p);
  2426. case TIOCSETD:
  2427. return tiocsetd(tty, p);
  2428. case TIOCVHANGUP:
  2429. if (!capable(CAP_SYS_ADMIN))
  2430. return -EPERM;
  2431. tty_vhangup(tty);
  2432. return 0;
  2433. case TIOCGDEV:
  2434. {
  2435. unsigned int ret = new_encode_dev(tty_devnum(real_tty));
  2436. return put_user(ret, (unsigned int __user *)p);
  2437. }
  2438. /*
  2439. * Break handling
  2440. */
  2441. case TIOCSBRK: /* Turn break on, unconditionally */
  2442. if (tty->ops->break_ctl)
  2443. return tty->ops->break_ctl(tty, -1);
  2444. return 0;
  2445. case TIOCCBRK: /* Turn break off, unconditionally */
  2446. if (tty->ops->break_ctl)
  2447. return tty->ops->break_ctl(tty, 0);
  2448. return 0;
  2449. case TCSBRK: /* SVID version: non-zero arg --> no break */
  2450. /* non-zero arg means wait for all output data
  2451. * to be sent (performed above) but don't send break.
  2452. * This is used by the tcdrain() termios function.
  2453. */
  2454. if (!arg)
  2455. return send_break(tty, 250);
  2456. return 0;
  2457. case TCSBRKP: /* support for POSIX tcsendbreak() */
  2458. return send_break(tty, arg ? arg*100 : 250);
  2459. case TIOCMGET:
  2460. return tty_tiocmget(tty, p);
  2461. case TIOCMSET:
  2462. case TIOCMBIC:
  2463. case TIOCMBIS:
  2464. return tty_tiocmset(tty, cmd, p);
  2465. case TIOCGICOUNT:
  2466. return tty_tiocgicount(tty, p);
  2467. case TCFLSH:
  2468. switch (arg) {
  2469. case TCIFLUSH:
  2470. case TCIOFLUSH:
  2471. /* flush tty buffer and allow ldisc to process ioctl */
  2472. tty_buffer_flush(tty, NULL);
  2473. break;
  2474. }
  2475. break;
  2476. case TIOCSSERIAL:
  2477. return tty_tiocsserial(tty, p);
  2478. case TIOCGSERIAL:
  2479. return tty_tiocgserial(tty, p);
  2480. case TIOCGPTPEER:
  2481. /* Special because the struct file is needed */
  2482. return ptm_open_peer(file, tty, (int)arg);
  2483. default:
  2484. retval = tty_jobctrl_ioctl(tty, real_tty, file, cmd, arg);
  2485. if (retval != -ENOIOCTLCMD)
  2486. return retval;
  2487. }
  2488. if (tty->ops->ioctl) {
  2489. retval = tty->ops->ioctl(tty, cmd, arg);
  2490. if (retval != -ENOIOCTLCMD)
  2491. return retval;
  2492. }
  2493. ld = tty_ldisc_ref_wait(tty);
  2494. if (!ld)
  2495. return hung_up_tty_ioctl(file, cmd, arg);
  2496. retval = -EINVAL;
  2497. if (ld->ops->ioctl) {
  2498. retval = ld->ops->ioctl(tty, cmd, arg);
  2499. if (retval == -ENOIOCTLCMD)
  2500. retval = -ENOTTY;
  2501. }
  2502. tty_ldisc_deref(ld);
  2503. return retval;
  2504. }
  2505. #ifdef CONFIG_COMPAT
  2506. struct serial_struct32 {
  2507. compat_int_t type;
  2508. compat_int_t line;
  2509. compat_uint_t port;
  2510. compat_int_t irq;
  2511. compat_int_t flags;
  2512. compat_int_t xmit_fifo_size;
  2513. compat_int_t custom_divisor;
  2514. compat_int_t baud_base;
  2515. unsigned short close_delay;
  2516. char io_type;
  2517. char reserved_char;
  2518. compat_int_t hub6;
  2519. unsigned short closing_wait; /* time to wait before closing */
  2520. unsigned short closing_wait2; /* no longer used... */
  2521. compat_uint_t iomem_base;
  2522. unsigned short iomem_reg_shift;
  2523. unsigned int port_high;
  2524. /* compat_ulong_t iomap_base FIXME */
  2525. compat_int_t reserved;
  2526. };
  2527. static int compat_tty_tiocsserial(struct tty_struct *tty,
  2528. struct serial_struct32 __user *ss)
  2529. {
  2530. struct serial_struct32 v32;
  2531. struct serial_struct v;
  2532. if (copy_from_user(&v32, ss, sizeof(*ss)))
  2533. return -EFAULT;
  2534. memcpy(&v, &v32, offsetof(struct serial_struct32, iomem_base));
  2535. v.iomem_base = compat_ptr(v32.iomem_base);
  2536. v.iomem_reg_shift = v32.iomem_reg_shift;
  2537. v.port_high = v32.port_high;
  2538. v.iomap_base = 0;
  2539. return tty_set_serial(tty, &v);
  2540. }
  2541. static int compat_tty_tiocgserial(struct tty_struct *tty,
  2542. struct serial_struct32 __user *ss)
  2543. {
  2544. struct serial_struct32 v32;
  2545. struct serial_struct v;
  2546. int err;
  2547. memset(&v, 0, sizeof(v));
  2548. memset(&v32, 0, sizeof(v32));
  2549. if (!tty->ops->get_serial)
  2550. return -ENOTTY;
  2551. err = tty->ops->get_serial(tty, &v);
  2552. if (!err) {
  2553. memcpy(&v32, &v, offsetof(struct serial_struct32, iomem_base));
  2554. v32.iomem_base = (unsigned long)v.iomem_base >> 32 ?
  2555. 0xfffffff : ptr_to_compat(v.iomem_base);
  2556. v32.iomem_reg_shift = v.iomem_reg_shift;
  2557. v32.port_high = v.port_high;
  2558. if (copy_to_user(ss, &v32, sizeof(v32)))
  2559. err = -EFAULT;
  2560. }
  2561. return err;
  2562. }
  2563. static long tty_compat_ioctl(struct file *file, unsigned int cmd,
  2564. unsigned long arg)
  2565. {
  2566. struct tty_struct *tty = file_tty(file);
  2567. struct tty_ldisc *ld;
  2568. int retval = -ENOIOCTLCMD;
  2569. switch (cmd) {
  2570. case TIOCOUTQ:
  2571. case TIOCSTI:
  2572. case TIOCGWINSZ:
  2573. case TIOCSWINSZ:
  2574. case TIOCGEXCL:
  2575. case TIOCGETD:
  2576. case TIOCSETD:
  2577. case TIOCGDEV:
  2578. case TIOCMGET:
  2579. case TIOCMSET:
  2580. case TIOCMBIC:
  2581. case TIOCMBIS:
  2582. case TIOCGICOUNT:
  2583. case TIOCGPGRP:
  2584. case TIOCSPGRP:
  2585. case TIOCGSID:
  2586. case TIOCSERGETLSR:
  2587. case TIOCGRS485:
  2588. case TIOCSRS485:
  2589. #ifdef TIOCGETP
  2590. case TIOCGETP:
  2591. case TIOCSETP:
  2592. case TIOCSETN:
  2593. #endif
  2594. #ifdef TIOCGETC
  2595. case TIOCGETC:
  2596. case TIOCSETC:
  2597. #endif
  2598. #ifdef TIOCGLTC
  2599. case TIOCGLTC:
  2600. case TIOCSLTC:
  2601. #endif
  2602. case TCSETSF:
  2603. case TCSETSW:
  2604. case TCSETS:
  2605. case TCGETS:
  2606. #ifdef TCGETS2
  2607. case TCGETS2:
  2608. case TCSETSF2:
  2609. case TCSETSW2:
  2610. case TCSETS2:
  2611. #endif
  2612. case TCGETA:
  2613. case TCSETAF:
  2614. case TCSETAW:
  2615. case TCSETA:
  2616. case TIOCGLCKTRMIOS:
  2617. case TIOCSLCKTRMIOS:
  2618. #ifdef TCGETX
  2619. case TCGETX:
  2620. case TCSETX:
  2621. case TCSETXW:
  2622. case TCSETXF:
  2623. #endif
  2624. case TIOCGSOFTCAR:
  2625. case TIOCSSOFTCAR:
  2626. case PPPIOCGCHAN:
  2627. case PPPIOCGUNIT:
  2628. return tty_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  2629. case TIOCCONS:
  2630. case TIOCEXCL:
  2631. case TIOCNXCL:
  2632. case TIOCVHANGUP:
  2633. case TIOCSBRK:
  2634. case TIOCCBRK:
  2635. case TCSBRK:
  2636. case TCSBRKP:
  2637. case TCFLSH:
  2638. case TIOCGPTPEER:
  2639. case TIOCNOTTY:
  2640. case TIOCSCTTY:
  2641. case TCXONC:
  2642. case TIOCMIWAIT:
  2643. case TIOCSERCONFIG:
  2644. return tty_ioctl(file, cmd, arg);
  2645. }
  2646. if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
  2647. return -EINVAL;
  2648. switch (cmd) {
  2649. case TIOCSSERIAL:
  2650. return compat_tty_tiocsserial(tty, compat_ptr(arg));
  2651. case TIOCGSERIAL:
  2652. return compat_tty_tiocgserial(tty, compat_ptr(arg));
  2653. }
  2654. if (tty->ops->compat_ioctl) {
  2655. retval = tty->ops->compat_ioctl(tty, cmd, arg);
  2656. if (retval != -ENOIOCTLCMD)
  2657. return retval;
  2658. }
  2659. ld = tty_ldisc_ref_wait(tty);
  2660. if (!ld)
  2661. return hung_up_tty_compat_ioctl(file, cmd, arg);
  2662. if (ld->ops->compat_ioctl)
  2663. retval = ld->ops->compat_ioctl(tty, cmd, arg);
  2664. if (retval == -ENOIOCTLCMD && ld->ops->ioctl)
  2665. retval = ld->ops->ioctl(tty, (unsigned long)compat_ptr(cmd),
  2666. arg);
  2667. tty_ldisc_deref(ld);
  2668. return retval;
  2669. }
  2670. #endif
  2671. static int this_tty(const void *t, struct file *file, unsigned fd)
  2672. {
  2673. if (likely(file->f_op->read_iter != tty_read))
  2674. return 0;
  2675. return file_tty(file) != t ? 0 : fd + 1;
  2676. }
  2677. /*
  2678. * This implements the "Secure Attention Key" --- the idea is to
  2679. * prevent trojan horses by killing all processes associated with this
  2680. * tty when the user hits the "Secure Attention Key". Required for
  2681. * super-paranoid applications --- see the Orange Book for more details.
  2682. *
  2683. * This code could be nicer; ideally it should send a HUP, wait a few
  2684. * seconds, then send a INT, and then a KILL signal. But you then
  2685. * have to coordinate with the init process, since all processes associated
  2686. * with the current tty must be dead before the new getty is allowed
  2687. * to spawn.
  2688. *
  2689. * Now, if it would be correct ;-/ The current code has a nasty hole -
  2690. * it doesn't catch files in flight. We may send the descriptor to ourselves
  2691. * via AF_UNIX socket, close it and later fetch from socket. FIXME.
  2692. *
  2693. * Nasty bug: do_SAK is being called in interrupt context. This can
  2694. * deadlock. We punt it up to process context. AKPM - 16Mar2001
  2695. */
  2696. void __do_SAK(struct tty_struct *tty)
  2697. {
  2698. struct task_struct *g, *p;
  2699. struct pid *session;
  2700. int i;
  2701. unsigned long flags;
  2702. spin_lock_irqsave(&tty->ctrl.lock, flags);
  2703. session = get_pid(tty->ctrl.session);
  2704. spin_unlock_irqrestore(&tty->ctrl.lock, flags);
  2705. tty_ldisc_flush(tty);
  2706. tty_driver_flush_buffer(tty);
  2707. read_lock(&tasklist_lock);
  2708. /* Kill the entire session */
  2709. do_each_pid_task(session, PIDTYPE_SID, p) {
  2710. tty_notice(tty, "SAK: killed process %d (%s): by session\n",
  2711. task_pid_nr(p), p->comm);
  2712. group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_SID);
  2713. } while_each_pid_task(session, PIDTYPE_SID, p);
  2714. /* Now kill any processes that happen to have the tty open */
  2715. for_each_process_thread(g, p) {
  2716. if (p->signal->tty == tty) {
  2717. tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n",
  2718. task_pid_nr(p), p->comm);
  2719. group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p,
  2720. PIDTYPE_SID);
  2721. continue;
  2722. }
  2723. task_lock(p);
  2724. i = iterate_fd(p->files, 0, this_tty, tty);
  2725. if (i != 0) {
  2726. tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n",
  2727. task_pid_nr(p), p->comm, i - 1);
  2728. group_send_sig_info(SIGKILL, SEND_SIG_PRIV, p,
  2729. PIDTYPE_SID);
  2730. }
  2731. task_unlock(p);
  2732. }
  2733. read_unlock(&tasklist_lock);
  2734. put_pid(session);
  2735. }
  2736. static void do_SAK_work(struct work_struct *work)
  2737. {
  2738. struct tty_struct *tty =
  2739. container_of(work, struct tty_struct, SAK_work);
  2740. __do_SAK(tty);
  2741. }
  2742. /*
  2743. * The tq handling here is a little racy - tty->SAK_work may already be queued.
  2744. * Fortunately we don't need to worry, because if ->SAK_work is already queued,
  2745. * the values which we write to it will be identical to the values which it
  2746. * already has. --akpm
  2747. */
  2748. void do_SAK(struct tty_struct *tty)
  2749. {
  2750. if (!tty)
  2751. return;
  2752. schedule_work(&tty->SAK_work);
  2753. }
  2754. EXPORT_SYMBOL(do_SAK);
  2755. /* Must put_device() after it's unused! */
  2756. static struct device *tty_get_device(struct tty_struct *tty)
  2757. {
  2758. dev_t devt = tty_devnum(tty);
  2759. return class_find_device_by_devt(&tty_class, devt);
  2760. }
  2761. /**
  2762. * alloc_tty_struct - allocate a new tty
  2763. * @driver: driver which will handle the returned tty
  2764. * @idx: minor of the tty
  2765. *
  2766. * This subroutine allocates and initializes a tty structure.
  2767. *
  2768. * Locking: none - @tty in question is not exposed at this point
  2769. */
  2770. struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
  2771. {
  2772. struct tty_struct *tty;
  2773. tty = kzalloc(sizeof(*tty), GFP_KERNEL_ACCOUNT);
  2774. if (!tty)
  2775. return NULL;
  2776. kref_init(&tty->kref);
  2777. if (tty_ldisc_init(tty)) {
  2778. kfree(tty);
  2779. return NULL;
  2780. }
  2781. tty->ctrl.session = NULL;
  2782. tty->ctrl.pgrp = NULL;
  2783. mutex_init(&tty->legacy_mutex);
  2784. mutex_init(&tty->throttle_mutex);
  2785. init_rwsem(&tty->termios_rwsem);
  2786. mutex_init(&tty->winsize_mutex);
  2787. init_ldsem(&tty->ldisc_sem);
  2788. init_waitqueue_head(&tty->write_wait);
  2789. init_waitqueue_head(&tty->read_wait);
  2790. INIT_WORK(&tty->hangup_work, do_tty_hangup);
  2791. mutex_init(&tty->atomic_write_lock);
  2792. spin_lock_init(&tty->ctrl.lock);
  2793. spin_lock_init(&tty->flow.lock);
  2794. spin_lock_init(&tty->files_lock);
  2795. INIT_LIST_HEAD(&tty->tty_files);
  2796. INIT_WORK(&tty->SAK_work, do_SAK_work);
  2797. tty->driver = driver;
  2798. tty->ops = driver->ops;
  2799. tty->index = idx;
  2800. tty_line_name(driver, idx, tty->name);
  2801. tty->dev = tty_get_device(tty);
  2802. return tty;
  2803. }
  2804. /**
  2805. * tty_put_char - write one character to a tty
  2806. * @tty: tty
  2807. * @ch: character to write
  2808. *
  2809. * Write one byte to the @tty using the provided @tty->ops->put_char() method
  2810. * if present.
  2811. *
  2812. * Note: the specific put_char operation in the driver layer may go
  2813. * away soon. Don't call it directly, use this method
  2814. *
  2815. * Return: the number of characters successfully output.
  2816. */
  2817. int tty_put_char(struct tty_struct *tty, u8 ch)
  2818. {
  2819. if (tty->ops->put_char)
  2820. return tty->ops->put_char(tty, ch);
  2821. return tty->ops->write(tty, &ch, 1);
  2822. }
  2823. EXPORT_SYMBOL_GPL(tty_put_char);
  2824. static int tty_cdev_add(struct tty_driver *driver, dev_t dev,
  2825. unsigned int index, unsigned int count)
  2826. {
  2827. int err;
  2828. /* init here, since reused cdevs cause crashes */
  2829. driver->cdevs[index] = cdev_alloc();
  2830. if (!driver->cdevs[index])
  2831. return -ENOMEM;
  2832. driver->cdevs[index]->ops = &tty_fops;
  2833. driver->cdevs[index]->owner = driver->owner;
  2834. err = cdev_add(driver->cdevs[index], dev, count);
  2835. if (err)
  2836. kobject_put(&driver->cdevs[index]->kobj);
  2837. return err;
  2838. }
  2839. /**
  2840. * tty_register_device - register a tty device
  2841. * @driver: the tty driver that describes the tty device
  2842. * @index: the index in the tty driver for this tty device
  2843. * @device: a struct device that is associated with this tty device.
  2844. * This field is optional, if there is no known struct device
  2845. * for this tty device it can be set to NULL safely.
  2846. *
  2847. * This call is required to be made to register an individual tty device
  2848. * if the tty driver's flags have the %TTY_DRIVER_DYNAMIC_DEV bit set. If
  2849. * that bit is not set, this function should not be called by a tty
  2850. * driver.
  2851. *
  2852. * Locking: ??
  2853. *
  2854. * Return: A pointer to the struct device for this tty device (or
  2855. * ERR_PTR(-EFOO) on error).
  2856. */
  2857. struct device *tty_register_device(struct tty_driver *driver, unsigned index,
  2858. struct device *device)
  2859. {
  2860. return tty_register_device_attr(driver, index, device, NULL, NULL);
  2861. }
  2862. EXPORT_SYMBOL(tty_register_device);
  2863. static void tty_device_create_release(struct device *dev)
  2864. {
  2865. dev_dbg(dev, "releasing...\n");
  2866. kfree(dev);
  2867. }
  2868. /**
  2869. * tty_register_device_attr - register a tty device
  2870. * @driver: the tty driver that describes the tty device
  2871. * @index: the index in the tty driver for this tty device
  2872. * @device: a struct device that is associated with this tty device.
  2873. * This field is optional, if there is no known struct device
  2874. * for this tty device it can be set to %NULL safely.
  2875. * @drvdata: Driver data to be set to device.
  2876. * @attr_grp: Attribute group to be set on device.
  2877. *
  2878. * This call is required to be made to register an individual tty device if the
  2879. * tty driver's flags have the %TTY_DRIVER_DYNAMIC_DEV bit set. If that bit is
  2880. * not set, this function should not be called by a tty driver.
  2881. *
  2882. * Locking: ??
  2883. *
  2884. * Return: A pointer to the struct device for this tty device (or
  2885. * ERR_PTR(-EFOO) on error).
  2886. */
  2887. struct device *tty_register_device_attr(struct tty_driver *driver,
  2888. unsigned index, struct device *device,
  2889. void *drvdata,
  2890. const struct attribute_group **attr_grp)
  2891. {
  2892. char name[64];
  2893. dev_t devt = MKDEV(driver->major, driver->minor_start) + index;
  2894. struct ktermios *tp;
  2895. struct device *dev;
  2896. int retval;
  2897. if (index >= driver->num) {
  2898. pr_err("%s: Attempt to register invalid tty line number (%d)\n",
  2899. driver->name, index);
  2900. return ERR_PTR(-EINVAL);
  2901. }
  2902. if (driver->type == TTY_DRIVER_TYPE_PTY)
  2903. pty_line_name(driver, index, name);
  2904. else
  2905. tty_line_name(driver, index, name);
  2906. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  2907. if (!dev)
  2908. return ERR_PTR(-ENOMEM);
  2909. dev->devt = devt;
  2910. dev->class = &tty_class;
  2911. dev->parent = device;
  2912. dev->release = tty_device_create_release;
  2913. dev_set_name(dev, "%s", name);
  2914. dev->groups = attr_grp;
  2915. dev_set_drvdata(dev, drvdata);
  2916. dev_set_uevent_suppress(dev, 1);
  2917. retval = device_register(dev);
  2918. if (retval)
  2919. goto err_put;
  2920. if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
  2921. /*
  2922. * Free any saved termios data so that the termios state is
  2923. * reset when reusing a minor number.
  2924. */
  2925. tp = driver->termios[index];
  2926. if (tp) {
  2927. driver->termios[index] = NULL;
  2928. kfree(tp);
  2929. }
  2930. retval = tty_cdev_add(driver, devt, index, 1);
  2931. if (retval)
  2932. goto err_del;
  2933. }
  2934. dev_set_uevent_suppress(dev, 0);
  2935. kobject_uevent(&dev->kobj, KOBJ_ADD);
  2936. return dev;
  2937. err_del:
  2938. device_del(dev);
  2939. err_put:
  2940. put_device(dev);
  2941. return ERR_PTR(retval);
  2942. }
  2943. EXPORT_SYMBOL_GPL(tty_register_device_attr);
  2944. /**
  2945. * tty_unregister_device - unregister a tty device
  2946. * @driver: the tty driver that describes the tty device
  2947. * @index: the index in the tty driver for this tty device
  2948. *
  2949. * If a tty device is registered with a call to tty_register_device() then
  2950. * this function must be called when the tty device is gone.
  2951. *
  2952. * Locking: ??
  2953. */
  2954. void tty_unregister_device(struct tty_driver *driver, unsigned index)
  2955. {
  2956. device_destroy(&tty_class, MKDEV(driver->major, driver->minor_start) + index);
  2957. if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
  2958. cdev_del(driver->cdevs[index]);
  2959. driver->cdevs[index] = NULL;
  2960. }
  2961. }
  2962. EXPORT_SYMBOL(tty_unregister_device);
  2963. /**
  2964. * __tty_alloc_driver - allocate tty driver
  2965. * @lines: count of lines this driver can handle at most
  2966. * @owner: module which is responsible for this driver
  2967. * @flags: some of %TTY_DRIVER_ flags, will be set in driver->flags
  2968. *
  2969. * This should not be called directly, some of the provided macros should be
  2970. * used instead. Use IS_ERR() and friends on @retval.
  2971. */
  2972. struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner,
  2973. unsigned long flags)
  2974. {
  2975. struct tty_driver *driver;
  2976. unsigned int cdevs = 1;
  2977. int err;
  2978. if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1))
  2979. return ERR_PTR(-EINVAL);
  2980. driver = kzalloc(sizeof(*driver), GFP_KERNEL);
  2981. if (!driver)
  2982. return ERR_PTR(-ENOMEM);
  2983. kref_init(&driver->kref);
  2984. driver->num = lines;
  2985. driver->owner = owner;
  2986. driver->flags = flags;
  2987. if (!(flags & TTY_DRIVER_DEVPTS_MEM)) {
  2988. driver->ttys = kcalloc(lines, sizeof(*driver->ttys),
  2989. GFP_KERNEL);
  2990. driver->termios = kcalloc(lines, sizeof(*driver->termios),
  2991. GFP_KERNEL);
  2992. if (!driver->ttys || !driver->termios) {
  2993. err = -ENOMEM;
  2994. goto err_free_all;
  2995. }
  2996. }
  2997. if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
  2998. driver->ports = kcalloc(lines, sizeof(*driver->ports),
  2999. GFP_KERNEL);
  3000. if (!driver->ports) {
  3001. err = -ENOMEM;
  3002. goto err_free_all;
  3003. }
  3004. cdevs = lines;
  3005. }
  3006. driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL);
  3007. if (!driver->cdevs) {
  3008. err = -ENOMEM;
  3009. goto err_free_all;
  3010. }
  3011. return driver;
  3012. err_free_all:
  3013. kfree(driver->ports);
  3014. kfree(driver->ttys);
  3015. kfree(driver->termios);
  3016. kfree(driver->cdevs);
  3017. kfree(driver);
  3018. return ERR_PTR(err);
  3019. }
  3020. EXPORT_SYMBOL(__tty_alloc_driver);
  3021. static void destruct_tty_driver(struct kref *kref)
  3022. {
  3023. struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
  3024. int i;
  3025. struct ktermios *tp;
  3026. if (driver->flags & TTY_DRIVER_INSTALLED) {
  3027. for (i = 0; i < driver->num; i++) {
  3028. tp = driver->termios[i];
  3029. if (tp) {
  3030. driver->termios[i] = NULL;
  3031. kfree(tp);
  3032. }
  3033. if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
  3034. tty_unregister_device(driver, i);
  3035. }
  3036. proc_tty_unregister_driver(driver);
  3037. if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)
  3038. cdev_del(driver->cdevs[0]);
  3039. }
  3040. kfree(driver->cdevs);
  3041. kfree(driver->ports);
  3042. kfree(driver->termios);
  3043. kfree(driver->ttys);
  3044. kfree(driver);
  3045. }
  3046. /**
  3047. * tty_driver_kref_put - drop a reference to a tty driver
  3048. * @driver: driver of which to drop the reference
  3049. *
  3050. * The final put will destroy and free up the driver.
  3051. */
  3052. void tty_driver_kref_put(struct tty_driver *driver)
  3053. {
  3054. kref_put(&driver->kref, destruct_tty_driver);
  3055. }
  3056. EXPORT_SYMBOL(tty_driver_kref_put);
  3057. /**
  3058. * tty_register_driver - register a tty driver
  3059. * @driver: driver to register
  3060. *
  3061. * Called by a tty driver to register itself.
  3062. */
  3063. int tty_register_driver(struct tty_driver *driver)
  3064. {
  3065. int error;
  3066. int i;
  3067. dev_t dev;
  3068. struct device *d;
  3069. if (!driver->major) {
  3070. error = alloc_chrdev_region(&dev, driver->minor_start,
  3071. driver->num, driver->name);
  3072. if (!error) {
  3073. driver->major = MAJOR(dev);
  3074. driver->minor_start = MINOR(dev);
  3075. }
  3076. } else {
  3077. dev = MKDEV(driver->major, driver->minor_start);
  3078. error = register_chrdev_region(dev, driver->num, driver->name);
  3079. }
  3080. if (error < 0)
  3081. goto err;
  3082. if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) {
  3083. error = tty_cdev_add(driver, dev, 0, driver->num);
  3084. if (error)
  3085. goto err_unreg_char;
  3086. }
  3087. mutex_lock(&tty_mutex);
  3088. list_add(&driver->tty_drivers, &tty_drivers);
  3089. mutex_unlock(&tty_mutex);
  3090. if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
  3091. for (i = 0; i < driver->num; i++) {
  3092. d = tty_register_device(driver, i, NULL);
  3093. if (IS_ERR(d)) {
  3094. error = PTR_ERR(d);
  3095. goto err_unreg_devs;
  3096. }
  3097. }
  3098. }
  3099. proc_tty_register_driver(driver);
  3100. driver->flags |= TTY_DRIVER_INSTALLED;
  3101. return 0;
  3102. err_unreg_devs:
  3103. for (i--; i >= 0; i--)
  3104. tty_unregister_device(driver, i);
  3105. mutex_lock(&tty_mutex);
  3106. list_del(&driver->tty_drivers);
  3107. mutex_unlock(&tty_mutex);
  3108. err_unreg_char:
  3109. unregister_chrdev_region(dev, driver->num);
  3110. err:
  3111. return error;
  3112. }
  3113. EXPORT_SYMBOL(tty_register_driver);
  3114. /**
  3115. * tty_unregister_driver - unregister a tty driver
  3116. * @driver: driver to unregister
  3117. *
  3118. * Called by a tty driver to unregister itself.
  3119. */
  3120. void tty_unregister_driver(struct tty_driver *driver)
  3121. {
  3122. unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
  3123. driver->num);
  3124. mutex_lock(&tty_mutex);
  3125. list_del(&driver->tty_drivers);
  3126. mutex_unlock(&tty_mutex);
  3127. }
  3128. EXPORT_SYMBOL(tty_unregister_driver);
  3129. dev_t tty_devnum(struct tty_struct *tty)
  3130. {
  3131. return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
  3132. }
  3133. EXPORT_SYMBOL(tty_devnum);
  3134. void tty_default_fops(struct file_operations *fops)
  3135. {
  3136. *fops = tty_fops;
  3137. }
  3138. static char *tty_devnode(const struct device *dev, umode_t *mode)
  3139. {
  3140. if (!mode)
  3141. return NULL;
  3142. if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
  3143. dev->devt == MKDEV(TTYAUX_MAJOR, 2))
  3144. *mode = 0666;
  3145. return NULL;
  3146. }
  3147. const struct class tty_class = {
  3148. .name = "tty",
  3149. .devnode = tty_devnode,
  3150. };
  3151. static int __init tty_class_init(void)
  3152. {
  3153. return class_register(&tty_class);
  3154. }
  3155. postcore_initcall(tty_class_init);
  3156. /* 3/2004 jmc: why do these devices exist? */
  3157. static struct cdev tty_cdev, console_cdev;
  3158. static ssize_t show_cons_active(struct device *dev,
  3159. struct device_attribute *attr, char *buf)
  3160. {
  3161. struct console *cs[16];
  3162. int i = 0;
  3163. struct console *c;
  3164. ssize_t count = 0;
  3165. /*
  3166. * Hold the console_list_lock to guarantee that no consoles are
  3167. * unregistered until all console processing is complete.
  3168. * This also allows safe traversal of the console list and
  3169. * race-free reading of @flags.
  3170. */
  3171. console_list_lock();
  3172. for_each_console(c) {
  3173. if (!c->device)
  3174. continue;
  3175. if (!(c->flags & CON_NBCON) && !c->write)
  3176. continue;
  3177. if ((c->flags & CON_ENABLED) == 0)
  3178. continue;
  3179. cs[i++] = c;
  3180. if (i >= ARRAY_SIZE(cs))
  3181. break;
  3182. }
  3183. /*
  3184. * Take console_lock to serialize device() callback with
  3185. * other console operations. For example, fg_console is
  3186. * modified under console_lock when switching vt.
  3187. */
  3188. console_lock();
  3189. while (i--) {
  3190. int index = cs[i]->index;
  3191. struct tty_driver *drv = cs[i]->device(cs[i], &index);
  3192. /* don't resolve tty0 as some programs depend on it */
  3193. if (drv && (cs[i]->index > 0 || drv->major != TTY_MAJOR))
  3194. count += tty_line_name(drv, index, buf + count);
  3195. else
  3196. count += sprintf(buf + count, "%s%d",
  3197. cs[i]->name, cs[i]->index);
  3198. count += sprintf(buf + count, "%c", i ? ' ':'\n');
  3199. }
  3200. console_unlock();
  3201. console_list_unlock();
  3202. return count;
  3203. }
  3204. static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL);
  3205. static struct attribute *cons_dev_attrs[] = {
  3206. &dev_attr_active.attr,
  3207. NULL
  3208. };
  3209. ATTRIBUTE_GROUPS(cons_dev);
  3210. static struct device *consdev;
  3211. void console_sysfs_notify(void)
  3212. {
  3213. if (consdev)
  3214. sysfs_notify(&consdev->kobj, NULL, "active");
  3215. }
  3216. static struct ctl_table tty_table[] = {
  3217. {
  3218. .procname = "legacy_tiocsti",
  3219. .data = &tty_legacy_tiocsti,
  3220. .maxlen = sizeof(tty_legacy_tiocsti),
  3221. .mode = 0644,
  3222. .proc_handler = proc_dobool,
  3223. },
  3224. {
  3225. .procname = "ldisc_autoload",
  3226. .data = &tty_ldisc_autoload,
  3227. .maxlen = sizeof(tty_ldisc_autoload),
  3228. .mode = 0644,
  3229. .proc_handler = proc_dointvec_minmax,
  3230. .extra1 = SYSCTL_ZERO,
  3231. .extra2 = SYSCTL_ONE,
  3232. },
  3233. };
  3234. /*
  3235. * Ok, now we can initialize the rest of the tty devices and can count
  3236. * on memory allocations, interrupts etc..
  3237. */
  3238. int __init tty_init(void)
  3239. {
  3240. register_sysctl_init("dev/tty", tty_table);
  3241. cdev_init(&tty_cdev, &tty_fops);
  3242. if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
  3243. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
  3244. panic("Couldn't register /dev/tty driver\n");
  3245. device_create(&tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
  3246. cdev_init(&console_cdev, &console_fops);
  3247. if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
  3248. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
  3249. panic("Couldn't register /dev/console driver\n");
  3250. consdev = device_create_with_groups(&tty_class, NULL,
  3251. MKDEV(TTYAUX_MAJOR, 1), NULL,
  3252. cons_dev_groups, "console");
  3253. if (IS_ERR(consdev))
  3254. consdev = NULL;
  3255. #ifdef CONFIG_VT
  3256. vty_init(&console_fops);
  3257. #endif
  3258. return 0;
  3259. }