tty_io.c 89 KB

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