tty_io.c 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231
  1. /*
  2. * linux/drivers/char/tty_io.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
  8. * or rs-channels. It also implements echoing, cooked mode etc.
  9. *
  10. * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
  11. *
  12. * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
  13. * tty_struct and tty_queue structures. Previously there was an array
  14. * of 256 tty_struct's which was statically allocated, and the
  15. * tty_queue structures were allocated at boot time. Both are now
  16. * dynamically allocated only when the tty is open.
  17. *
  18. * Also restructured routines so that there is more of a separation
  19. * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
  20. * the low-level tty routines (serial.c, pty.c, console.c). This
  21. * makes for cleaner and more compact code. -TYT, 9/17/92
  22. *
  23. * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
  24. * which can be dynamically activated and de-activated by the line
  25. * discipline handling modules (like SLIP).
  26. *
  27. * NOTE: pay no attention to the line discipline code (yet); its
  28. * interface is still subject to change in this version...
  29. * -- TYT, 1/31/92
  30. *
  31. * Added functionality to the OPOST tty handling. No delays, but all
  32. * other bits should be there.
  33. * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
  34. *
  35. * Rewrote canonical mode and added more termios flags.
  36. * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
  37. *
  38. * Reorganized FASYNC support so mouse code can share it.
  39. * -- ctm@ardi.com, 9Sep95
  40. *
  41. * New TIOCLINUX variants added.
  42. * -- mj@k332.feld.cvut.cz, 19-Nov-95
  43. *
  44. * Restrict vt switching via ioctl()
  45. * -- grif@cs.ucr.edu, 5-Dec-95
  46. *
  47. * Move console and virtual terminal code to more appropriate files,
  48. * implement CONFIG_VT and generalize console device interface.
  49. * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
  50. *
  51. * Rewrote init_dev and release_dev to eliminate races.
  52. * -- Bill Hawes <whawes@star.net>, June 97
  53. *
  54. * Added devfs support.
  55. * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
  56. *
  57. * Added support for a Unix98-style ptmx device.
  58. * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
  59. *
  60. * Reduced memory usage for older ARM systems
  61. * -- Russell King <rmk@arm.linux.org.uk>
  62. *
  63. * Move do_SAK() into process context. Less stack use in devfs functions.
  64. * alloc_tty_struct() always uses kmalloc()
  65. * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
  66. */
  67. #include <linux/types.h>
  68. #include <linux/major.h>
  69. #include <linux/errno.h>
  70. #include <linux/signal.h>
  71. #include <linux/fcntl.h>
  72. #include <linux/sched.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/proc_fs.h>
  89. #include <linux/init.h>
  90. #include <linux/module.h>
  91. #include <linux/smp_lock.h>
  92. #include <linux/device.h>
  93. #include <linux/wait.h>
  94. #include <linux/bitops.h>
  95. #include <linux/delay.h>
  96. #include <asm/uaccess.h>
  97. #include <asm/system.h>
  98. #include <linux/kbd_kern.h>
  99. #include <linux/vt_kern.h>
  100. #include <linux/selection.h>
  101. #include <linux/kmod.h>
  102. #include <linux/nsproxy.h>
  103. #undef TTY_DEBUG_HANGUP
  104. #define TTY_PARANOIA_CHECK 1
  105. #define CHECK_TTY_COUNT 1
  106. struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
  107. .c_iflag = ICRNL | IXON,
  108. .c_oflag = OPOST | ONLCR,
  109. .c_cflag = B38400 | CS8 | CREAD | HUPCL,
  110. .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
  111. ECHOCTL | ECHOKE | IEXTEN,
  112. .c_cc = INIT_C_CC,
  113. .c_ispeed = 38400,
  114. .c_ospeed = 38400
  115. };
  116. EXPORT_SYMBOL(tty_std_termios);
  117. /* This list gets poked at by procfs and various bits of boot up code. This
  118. could do with some rationalisation such as pulling the tty proc function
  119. into this file */
  120. LIST_HEAD(tty_drivers); /* linked list of tty drivers */
  121. /* Mutex to protect creating and releasing a tty. This is shared with
  122. vt.c for deeply disgusting hack reasons */
  123. DEFINE_MUTEX(tty_mutex);
  124. EXPORT_SYMBOL(tty_mutex);
  125. #ifdef CONFIG_UNIX98_PTYS
  126. extern struct tty_driver *ptm_driver; /* Unix98 pty masters; for /dev/ptmx */
  127. static int ptmx_open(struct inode *, struct file *);
  128. #endif
  129. static void initialize_tty_struct(struct tty_struct *tty);
  130. static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
  131. static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
  132. ssize_t redirected_tty_write(struct file *, const char __user *,
  133. size_t, loff_t *);
  134. static unsigned int tty_poll(struct file *, poll_table *);
  135. static int tty_open(struct inode *, struct file *);
  136. static int tty_release(struct inode *, struct file *);
  137. long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  138. #ifdef CONFIG_COMPAT
  139. static long tty_compat_ioctl(struct file *file, unsigned int cmd,
  140. unsigned long arg);
  141. #else
  142. #define tty_compat_ioctl NULL
  143. #endif
  144. static int tty_fasync(int fd, struct file *filp, int on);
  145. static void release_tty(struct tty_struct *tty, int idx);
  146. static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
  147. static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
  148. /**
  149. * alloc_tty_struct - allocate a tty object
  150. *
  151. * Return a new empty tty structure. The data fields have not
  152. * been initialized in any way but has been zeroed
  153. *
  154. * Locking: none
  155. */
  156. static struct tty_struct *alloc_tty_struct(void)
  157. {
  158. return kzalloc(sizeof(struct tty_struct), GFP_KERNEL);
  159. }
  160. static void tty_buffer_free_all(struct tty_struct *);
  161. /**
  162. * free_tty_struct - free a disused tty
  163. * @tty: tty struct to free
  164. *
  165. * Free the write buffers, tty queue and tty memory itself.
  166. *
  167. * Locking: none. Must be called after tty is definitely unused
  168. */
  169. static inline void free_tty_struct(struct tty_struct *tty)
  170. {
  171. kfree(tty->write_buf);
  172. tty_buffer_free_all(tty);
  173. kfree(tty);
  174. }
  175. #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
  176. /**
  177. * tty_name - return tty naming
  178. * @tty: tty structure
  179. * @buf: buffer for output
  180. *
  181. * Convert a tty structure into a name. The name reflects the kernel
  182. * naming policy and if udev is in use may not reflect user space
  183. *
  184. * Locking: none
  185. */
  186. char *tty_name(struct tty_struct *tty, char *buf)
  187. {
  188. if (!tty) /* Hmm. NULL pointer. That's fun. */
  189. strcpy(buf, "NULL tty");
  190. else
  191. strcpy(buf, tty->name);
  192. return buf;
  193. }
  194. EXPORT_SYMBOL(tty_name);
  195. int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
  196. const char *routine)
  197. {
  198. #ifdef TTY_PARANOIA_CHECK
  199. if (!tty) {
  200. printk(KERN_WARNING
  201. "null TTY for (%d:%d) in %s\n",
  202. imajor(inode), iminor(inode), routine);
  203. return 1;
  204. }
  205. if (tty->magic != TTY_MAGIC) {
  206. printk(KERN_WARNING
  207. "bad magic number for tty struct (%d:%d) in %s\n",
  208. imajor(inode), iminor(inode), routine);
  209. return 1;
  210. }
  211. #endif
  212. return 0;
  213. }
  214. static int check_tty_count(struct tty_struct *tty, const char *routine)
  215. {
  216. #ifdef CHECK_TTY_COUNT
  217. struct list_head *p;
  218. int count = 0;
  219. file_list_lock();
  220. list_for_each(p, &tty->tty_files) {
  221. count++;
  222. }
  223. file_list_unlock();
  224. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  225. tty->driver->subtype == PTY_TYPE_SLAVE &&
  226. tty->link && tty->link->count)
  227. count++;
  228. if (tty->count != count) {
  229. printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
  230. "!= #fd's(%d) in %s\n",
  231. tty->name, tty->count, count, routine);
  232. return count;
  233. }
  234. #endif
  235. return 0;
  236. }
  237. /*
  238. * Tty buffer allocation management
  239. */
  240. /**
  241. * tty_buffer_free_all - free buffers used by a tty
  242. * @tty: tty to free from
  243. *
  244. * Remove all the buffers pending on a tty whether queued with data
  245. * or in the free ring. Must be called when the tty is no longer in use
  246. *
  247. * Locking: none
  248. */
  249. static void tty_buffer_free_all(struct tty_struct *tty)
  250. {
  251. struct tty_buffer *thead;
  252. while ((thead = tty->buf.head) != NULL) {
  253. tty->buf.head = thead->next;
  254. kfree(thead);
  255. }
  256. while ((thead = tty->buf.free) != NULL) {
  257. tty->buf.free = thead->next;
  258. kfree(thead);
  259. }
  260. tty->buf.tail = NULL;
  261. tty->buf.memory_used = 0;
  262. }
  263. /**
  264. * tty_buffer_init - prepare a tty buffer structure
  265. * @tty: tty to initialise
  266. *
  267. * Set up the initial state of the buffer management for a tty device.
  268. * Must be called before the other tty buffer functions are used.
  269. *
  270. * Locking: none
  271. */
  272. static void tty_buffer_init(struct tty_struct *tty)
  273. {
  274. spin_lock_init(&tty->buf.lock);
  275. tty->buf.head = NULL;
  276. tty->buf.tail = NULL;
  277. tty->buf.free = NULL;
  278. tty->buf.memory_used = 0;
  279. }
  280. /**
  281. * tty_buffer_alloc - allocate a tty buffer
  282. * @tty: tty device
  283. * @size: desired size (characters)
  284. *
  285. * Allocate a new tty buffer to hold the desired number of characters.
  286. * Return NULL if out of memory or the allocation would exceed the
  287. * per device queue
  288. *
  289. * Locking: Caller must hold tty->buf.lock
  290. */
  291. static struct tty_buffer *tty_buffer_alloc(struct tty_struct *tty, size_t size)
  292. {
  293. struct tty_buffer *p;
  294. if (tty->buf.memory_used + size > 65536)
  295. return NULL;
  296. p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
  297. if (p == NULL)
  298. return NULL;
  299. p->used = 0;
  300. p->size = size;
  301. p->next = NULL;
  302. p->commit = 0;
  303. p->read = 0;
  304. p->char_buf_ptr = (char *)(p->data);
  305. p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
  306. tty->buf.memory_used += size;
  307. return p;
  308. }
  309. /**
  310. * tty_buffer_free - free a tty buffer
  311. * @tty: tty owning the buffer
  312. * @b: the buffer to free
  313. *
  314. * Free a tty buffer, or add it to the free list according to our
  315. * internal strategy
  316. *
  317. * Locking: Caller must hold tty->buf.lock
  318. */
  319. static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)
  320. {
  321. /* Dumb strategy for now - should keep some stats */
  322. tty->buf.memory_used -= b->size;
  323. WARN_ON(tty->buf.memory_used < 0);
  324. if (b->size >= 512)
  325. kfree(b);
  326. else {
  327. b->next = tty->buf.free;
  328. tty->buf.free = b;
  329. }
  330. }
  331. /**
  332. * __tty_buffer_flush - flush full tty buffers
  333. * @tty: tty to flush
  334. *
  335. * flush all the buffers containing receive data. Caller must
  336. * hold the buffer lock and must have ensured no parallel flush to
  337. * ldisc is running.
  338. *
  339. * Locking: Caller must hold tty->buf.lock
  340. */
  341. static void __tty_buffer_flush(struct tty_struct *tty)
  342. {
  343. struct tty_buffer *thead;
  344. while ((thead = tty->buf.head) != NULL) {
  345. tty->buf.head = thead->next;
  346. tty_buffer_free(tty, thead);
  347. }
  348. tty->buf.tail = NULL;
  349. }
  350. /**
  351. * tty_buffer_flush - flush full tty buffers
  352. * @tty: tty to flush
  353. *
  354. * flush all the buffers containing receive data. If the buffer is
  355. * being processed by flush_to_ldisc then we defer the processing
  356. * to that function
  357. *
  358. * Locking: none
  359. */
  360. static void tty_buffer_flush(struct tty_struct *tty)
  361. {
  362. unsigned long flags;
  363. spin_lock_irqsave(&tty->buf.lock, flags);
  364. /* If the data is being pushed to the tty layer then we can't
  365. process it here. Instead set a flag and the flush_to_ldisc
  366. path will process the flush request before it exits */
  367. if (test_bit(TTY_FLUSHING, &tty->flags)) {
  368. set_bit(TTY_FLUSHPENDING, &tty->flags);
  369. spin_unlock_irqrestore(&tty->buf.lock, flags);
  370. wait_event(tty->read_wait,
  371. test_bit(TTY_FLUSHPENDING, &tty->flags) == 0);
  372. return;
  373. } else
  374. __tty_buffer_flush(tty);
  375. spin_unlock_irqrestore(&tty->buf.lock, flags);
  376. }
  377. /**
  378. * tty_buffer_find - find a free tty buffer
  379. * @tty: tty owning the buffer
  380. * @size: characters wanted
  381. *
  382. * Locate an existing suitable tty buffer or if we are lacking one then
  383. * allocate a new one. We round our buffers off in 256 character chunks
  384. * to get better allocation behaviour.
  385. *
  386. * Locking: Caller must hold tty->buf.lock
  387. */
  388. static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
  389. {
  390. struct tty_buffer **tbh = &tty->buf.free;
  391. while ((*tbh) != NULL) {
  392. struct tty_buffer *t = *tbh;
  393. if (t->size >= size) {
  394. *tbh = t->next;
  395. t->next = NULL;
  396. t->used = 0;
  397. t->commit = 0;
  398. t->read = 0;
  399. tty->buf.memory_used += t->size;
  400. return t;
  401. }
  402. tbh = &((*tbh)->next);
  403. }
  404. /* Round the buffer size out */
  405. size = (size + 0xFF) & ~0xFF;
  406. return tty_buffer_alloc(tty, size);
  407. /* Should possibly check if this fails for the largest buffer we
  408. have queued and recycle that ? */
  409. }
  410. /**
  411. * tty_buffer_request_room - grow tty buffer if needed
  412. * @tty: tty structure
  413. * @size: size desired
  414. *
  415. * Make at least size bytes of linear space available for the tty
  416. * buffer. If we fail return the size we managed to find.
  417. *
  418. * Locking: Takes tty->buf.lock
  419. */
  420. int tty_buffer_request_room(struct tty_struct *tty, size_t size)
  421. {
  422. struct tty_buffer *b, *n;
  423. int left;
  424. unsigned long flags;
  425. spin_lock_irqsave(&tty->buf.lock, flags);
  426. /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
  427. remove this conditional if its worth it. This would be invisible
  428. to the callers */
  429. if ((b = tty->buf.tail) != NULL)
  430. left = b->size - b->used;
  431. else
  432. left = 0;
  433. if (left < size) {
  434. /* This is the slow path - looking for new buffers to use */
  435. if ((n = tty_buffer_find(tty, size)) != NULL) {
  436. if (b != NULL) {
  437. b->next = n;
  438. b->commit = b->used;
  439. } else
  440. tty->buf.head = n;
  441. tty->buf.tail = n;
  442. } else
  443. size = left;
  444. }
  445. spin_unlock_irqrestore(&tty->buf.lock, flags);
  446. return size;
  447. }
  448. EXPORT_SYMBOL_GPL(tty_buffer_request_room);
  449. /**
  450. * tty_insert_flip_string - Add characters to the tty buffer
  451. * @tty: tty structure
  452. * @chars: characters
  453. * @size: size
  454. *
  455. * Queue a series of bytes to the tty buffering. All the characters
  456. * passed are marked as without error. Returns the number added.
  457. *
  458. * Locking: Called functions may take tty->buf.lock
  459. */
  460. int tty_insert_flip_string(struct tty_struct *tty, const unsigned char *chars,
  461. size_t size)
  462. {
  463. int copied = 0;
  464. do {
  465. int space = tty_buffer_request_room(tty, size - copied);
  466. struct tty_buffer *tb = tty->buf.tail;
  467. /* If there is no space then tb may be NULL */
  468. if (unlikely(space == 0))
  469. break;
  470. memcpy(tb->char_buf_ptr + tb->used, chars, space);
  471. memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
  472. tb->used += space;
  473. copied += space;
  474. chars += space;
  475. /* There is a small chance that we need to split the data over
  476. several buffers. If this is the case we must loop */
  477. } while (unlikely(size > copied));
  478. return copied;
  479. }
  480. EXPORT_SYMBOL(tty_insert_flip_string);
  481. /**
  482. * tty_insert_flip_string_flags - Add characters to the tty buffer
  483. * @tty: tty structure
  484. * @chars: characters
  485. * @flags: flag bytes
  486. * @size: size
  487. *
  488. * Queue a series of bytes to the tty buffering. For each character
  489. * the flags array indicates the status of the character. Returns the
  490. * number added.
  491. *
  492. * Locking: Called functions may take tty->buf.lock
  493. */
  494. int tty_insert_flip_string_flags(struct tty_struct *tty,
  495. const unsigned char *chars, const char *flags, size_t size)
  496. {
  497. int copied = 0;
  498. do {
  499. int space = tty_buffer_request_room(tty, size - copied);
  500. struct tty_buffer *tb = tty->buf.tail;
  501. /* If there is no space then tb may be NULL */
  502. if (unlikely(space == 0))
  503. break;
  504. memcpy(tb->char_buf_ptr + tb->used, chars, space);
  505. memcpy(tb->flag_buf_ptr + tb->used, flags, space);
  506. tb->used += space;
  507. copied += space;
  508. chars += space;
  509. flags += space;
  510. /* There is a small chance that we need to split the data over
  511. several buffers. If this is the case we must loop */
  512. } while (unlikely(size > copied));
  513. return copied;
  514. }
  515. EXPORT_SYMBOL(tty_insert_flip_string_flags);
  516. /**
  517. * tty_schedule_flip - push characters to ldisc
  518. * @tty: tty to push from
  519. *
  520. * Takes any pending buffers and transfers their ownership to the
  521. * ldisc side of the queue. It then schedules those characters for
  522. * processing by the line discipline.
  523. *
  524. * Locking: Takes tty->buf.lock
  525. */
  526. void tty_schedule_flip(struct tty_struct *tty)
  527. {
  528. unsigned long flags;
  529. spin_lock_irqsave(&tty->buf.lock, flags);
  530. if (tty->buf.tail != NULL)
  531. tty->buf.tail->commit = tty->buf.tail->used;
  532. spin_unlock_irqrestore(&tty->buf.lock, flags);
  533. schedule_delayed_work(&tty->buf.work, 1);
  534. }
  535. EXPORT_SYMBOL(tty_schedule_flip);
  536. /**
  537. * tty_prepare_flip_string - make room for characters
  538. * @tty: tty
  539. * @chars: return pointer for character write area
  540. * @size: desired size
  541. *
  542. * Prepare a block of space in the buffer for data. Returns the length
  543. * available and buffer pointer to the space which is now allocated and
  544. * accounted for as ready for normal characters. This is used for drivers
  545. * that need their own block copy routines into the buffer. There is no
  546. * guarantee the buffer is a DMA target!
  547. *
  548. * Locking: May call functions taking tty->buf.lock
  549. */
  550. int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars,
  551. size_t size)
  552. {
  553. int space = tty_buffer_request_room(tty, size);
  554. if (likely(space)) {
  555. struct tty_buffer *tb = tty->buf.tail;
  556. *chars = tb->char_buf_ptr + tb->used;
  557. memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
  558. tb->used += space;
  559. }
  560. return space;
  561. }
  562. EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
  563. /**
  564. * tty_prepare_flip_string_flags - make room for characters
  565. * @tty: tty
  566. * @chars: return pointer for character write area
  567. * @flags: return pointer for status flag write area
  568. * @size: desired size
  569. *
  570. * Prepare a block of space in the buffer for data. Returns the length
  571. * available and buffer pointer to the space which is now allocated and
  572. * accounted for as ready for characters. This is used for drivers
  573. * that need their own block copy routines into the buffer. There is no
  574. * guarantee the buffer is a DMA target!
  575. *
  576. * Locking: May call functions taking tty->buf.lock
  577. */
  578. int tty_prepare_flip_string_flags(struct tty_struct *tty,
  579. unsigned char **chars, char **flags, size_t size)
  580. {
  581. int space = tty_buffer_request_room(tty, size);
  582. if (likely(space)) {
  583. struct tty_buffer *tb = tty->buf.tail;
  584. *chars = tb->char_buf_ptr + tb->used;
  585. *flags = tb->flag_buf_ptr + tb->used;
  586. tb->used += space;
  587. }
  588. return space;
  589. }
  590. EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
  591. /**
  592. * tty_set_termios_ldisc - set ldisc field
  593. * @tty: tty structure
  594. * @num: line discipline number
  595. *
  596. * This is probably overkill for real world processors but
  597. * they are not on hot paths so a little discipline won't do
  598. * any harm.
  599. *
  600. * Locking: takes termios_mutex
  601. */
  602. static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
  603. {
  604. mutex_lock(&tty->termios_mutex);
  605. tty->termios->c_line = num;
  606. mutex_unlock(&tty->termios_mutex);
  607. }
  608. /*
  609. * This guards the refcounted line discipline lists. The lock
  610. * must be taken with irqs off because there are hangup path
  611. * callers who will do ldisc lookups and cannot sleep.
  612. */
  613. static DEFINE_SPINLOCK(tty_ldisc_lock);
  614. static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
  615. /* Line disc dispatch table */
  616. static struct tty_ldisc tty_ldiscs[NR_LDISCS];
  617. /**
  618. * tty_register_ldisc - install a line discipline
  619. * @disc: ldisc number
  620. * @new_ldisc: pointer to the ldisc object
  621. *
  622. * Installs a new line discipline into the kernel. The discipline
  623. * is set up as unreferenced and then made available to the kernel
  624. * from this point onwards.
  625. *
  626. * Locking:
  627. * takes tty_ldisc_lock to guard against ldisc races
  628. */
  629. int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
  630. {
  631. unsigned long flags;
  632. int ret = 0;
  633. if (disc < N_TTY || disc >= NR_LDISCS)
  634. return -EINVAL;
  635. spin_lock_irqsave(&tty_ldisc_lock, flags);
  636. tty_ldiscs[disc] = *new_ldisc;
  637. tty_ldiscs[disc].num = disc;
  638. tty_ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
  639. tty_ldiscs[disc].refcount = 0;
  640. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  641. return ret;
  642. }
  643. EXPORT_SYMBOL(tty_register_ldisc);
  644. /**
  645. * tty_unregister_ldisc - unload a line discipline
  646. * @disc: ldisc number
  647. * @new_ldisc: pointer to the ldisc object
  648. *
  649. * Remove a line discipline from the kernel providing it is not
  650. * currently in use.
  651. *
  652. * Locking:
  653. * takes tty_ldisc_lock to guard against ldisc races
  654. */
  655. int tty_unregister_ldisc(int disc)
  656. {
  657. unsigned long flags;
  658. int ret = 0;
  659. if (disc < N_TTY || disc >= NR_LDISCS)
  660. return -EINVAL;
  661. spin_lock_irqsave(&tty_ldisc_lock, flags);
  662. if (tty_ldiscs[disc].refcount)
  663. ret = -EBUSY;
  664. else
  665. tty_ldiscs[disc].flags &= ~LDISC_FLAG_DEFINED;
  666. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  667. return ret;
  668. }
  669. EXPORT_SYMBOL(tty_unregister_ldisc);
  670. /**
  671. * tty_ldisc_get - take a reference to an ldisc
  672. * @disc: ldisc number
  673. *
  674. * Takes a reference to a line discipline. Deals with refcounts and
  675. * module locking counts. Returns NULL if the discipline is not available.
  676. * Returns a pointer to the discipline and bumps the ref count if it is
  677. * available
  678. *
  679. * Locking:
  680. * takes tty_ldisc_lock to guard against ldisc races
  681. */
  682. struct tty_ldisc *tty_ldisc_get(int disc)
  683. {
  684. unsigned long flags;
  685. struct tty_ldisc *ld;
  686. if (disc < N_TTY || disc >= NR_LDISCS)
  687. return NULL;
  688. spin_lock_irqsave(&tty_ldisc_lock, flags);
  689. ld = &tty_ldiscs[disc];
  690. /* Check the entry is defined */
  691. if (ld->flags & LDISC_FLAG_DEFINED) {
  692. /* If the module is being unloaded we can't use it */
  693. if (!try_module_get(ld->owner))
  694. ld = NULL;
  695. else /* lock it */
  696. ld->refcount++;
  697. } else
  698. ld = NULL;
  699. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  700. return ld;
  701. }
  702. EXPORT_SYMBOL_GPL(tty_ldisc_get);
  703. /**
  704. * tty_ldisc_put - drop ldisc reference
  705. * @disc: ldisc number
  706. *
  707. * Drop a reference to a line discipline. Manage refcounts and
  708. * module usage counts
  709. *
  710. * Locking:
  711. * takes tty_ldisc_lock to guard against ldisc races
  712. */
  713. void tty_ldisc_put(int disc)
  714. {
  715. struct tty_ldisc *ld;
  716. unsigned long flags;
  717. BUG_ON(disc < N_TTY || disc >= NR_LDISCS);
  718. spin_lock_irqsave(&tty_ldisc_lock, flags);
  719. ld = &tty_ldiscs[disc];
  720. BUG_ON(ld->refcount == 0);
  721. ld->refcount--;
  722. module_put(ld->owner);
  723. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  724. }
  725. EXPORT_SYMBOL_GPL(tty_ldisc_put);
  726. /**
  727. * tty_ldisc_assign - set ldisc on a tty
  728. * @tty: tty to assign
  729. * @ld: line discipline
  730. *
  731. * Install an instance of a line discipline into a tty structure. The
  732. * ldisc must have a reference count above zero to ensure it remains/
  733. * The tty instance refcount starts at zero.
  734. *
  735. * Locking:
  736. * Caller must hold references
  737. */
  738. static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
  739. {
  740. tty->ldisc = *ld;
  741. tty->ldisc.refcount = 0;
  742. }
  743. /**
  744. * tty_ldisc_try - internal helper
  745. * @tty: the tty
  746. *
  747. * Make a single attempt to grab and bump the refcount on
  748. * the tty ldisc. Return 0 on failure or 1 on success. This is
  749. * used to implement both the waiting and non waiting versions
  750. * of tty_ldisc_ref
  751. *
  752. * Locking: takes tty_ldisc_lock
  753. */
  754. static int tty_ldisc_try(struct tty_struct *tty)
  755. {
  756. unsigned long flags;
  757. struct tty_ldisc *ld;
  758. int ret = 0;
  759. spin_lock_irqsave(&tty_ldisc_lock, flags);
  760. ld = &tty->ldisc;
  761. if (test_bit(TTY_LDISC, &tty->flags)) {
  762. ld->refcount++;
  763. ret = 1;
  764. }
  765. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  766. return ret;
  767. }
  768. /**
  769. * tty_ldisc_ref_wait - wait for the tty ldisc
  770. * @tty: tty device
  771. *
  772. * Dereference the line discipline for the terminal and take a
  773. * reference to it. If the line discipline is in flux then
  774. * wait patiently until it changes.
  775. *
  776. * Note: Must not be called from an IRQ/timer context. The caller
  777. * must also be careful not to hold other locks that will deadlock
  778. * against a discipline change, such as an existing ldisc reference
  779. * (which we check for)
  780. *
  781. * Locking: call functions take tty_ldisc_lock
  782. */
  783. struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
  784. {
  785. /* wait_event is a macro */
  786. wait_event(tty_ldisc_wait, tty_ldisc_try(tty));
  787. if (tty->ldisc.refcount == 0)
  788. printk(KERN_ERR "tty_ldisc_ref_wait\n");
  789. return &tty->ldisc;
  790. }
  791. EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
  792. /**
  793. * tty_ldisc_ref - get the tty ldisc
  794. * @tty: tty device
  795. *
  796. * Dereference the line discipline for the terminal and take a
  797. * reference to it. If the line discipline is in flux then
  798. * return NULL. Can be called from IRQ and timer functions.
  799. *
  800. * Locking: called functions take tty_ldisc_lock
  801. */
  802. struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
  803. {
  804. if (tty_ldisc_try(tty))
  805. return &tty->ldisc;
  806. return NULL;
  807. }
  808. EXPORT_SYMBOL_GPL(tty_ldisc_ref);
  809. /**
  810. * tty_ldisc_deref - free a tty ldisc reference
  811. * @ld: reference to free up
  812. *
  813. * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
  814. * be called in IRQ context.
  815. *
  816. * Locking: takes tty_ldisc_lock
  817. */
  818. void tty_ldisc_deref(struct tty_ldisc *ld)
  819. {
  820. unsigned long flags;
  821. BUG_ON(ld == NULL);
  822. spin_lock_irqsave(&tty_ldisc_lock, flags);
  823. if (ld->refcount == 0)
  824. printk(KERN_ERR "tty_ldisc_deref: no references.\n");
  825. else
  826. ld->refcount--;
  827. if (ld->refcount == 0)
  828. wake_up(&tty_ldisc_wait);
  829. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  830. }
  831. EXPORT_SYMBOL_GPL(tty_ldisc_deref);
  832. /**
  833. * tty_ldisc_enable - allow ldisc use
  834. * @tty: terminal to activate ldisc on
  835. *
  836. * Set the TTY_LDISC flag when the line discipline can be called
  837. * again. Do necessary wakeups for existing sleepers.
  838. *
  839. * Note: nobody should set this bit except via this function. Clearing
  840. * directly is allowed.
  841. */
  842. static void tty_ldisc_enable(struct tty_struct *tty)
  843. {
  844. set_bit(TTY_LDISC, &tty->flags);
  845. wake_up(&tty_ldisc_wait);
  846. }
  847. /**
  848. * tty_set_ldisc - set line discipline
  849. * @tty: the terminal to set
  850. * @ldisc: the line discipline
  851. *
  852. * Set the discipline of a tty line. Must be called from a process
  853. * context.
  854. *
  855. * Locking: takes tty_ldisc_lock.
  856. * called functions take termios_mutex
  857. */
  858. static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
  859. {
  860. int retval = 0;
  861. struct tty_ldisc o_ldisc;
  862. char buf[64];
  863. int work;
  864. unsigned long flags;
  865. struct tty_ldisc *ld;
  866. struct tty_struct *o_tty;
  867. if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS))
  868. return -EINVAL;
  869. restart:
  870. ld = tty_ldisc_get(ldisc);
  871. /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
  872. /* Cyrus Durgin <cider@speakeasy.org> */
  873. if (ld == NULL) {
  874. request_module("tty-ldisc-%d", ldisc);
  875. ld = tty_ldisc_get(ldisc);
  876. }
  877. if (ld == NULL)
  878. return -EINVAL;
  879. /*
  880. * Problem: What do we do if this blocks ?
  881. */
  882. tty_wait_until_sent(tty, 0);
  883. if (tty->ldisc.num == ldisc) {
  884. tty_ldisc_put(ldisc);
  885. return 0;
  886. }
  887. /*
  888. * No more input please, we are switching. The new ldisc
  889. * will update this value in the ldisc open function
  890. */
  891. tty->receive_room = 0;
  892. o_ldisc = tty->ldisc;
  893. o_tty = tty->link;
  894. /*
  895. * Make sure we don't change while someone holds a
  896. * reference to the line discipline. The TTY_LDISC bit
  897. * prevents anyone taking a reference once it is clear.
  898. * We need the lock to avoid racing reference takers.
  899. */
  900. spin_lock_irqsave(&tty_ldisc_lock, flags);
  901. if (tty->ldisc.refcount || (o_tty && o_tty->ldisc.refcount)) {
  902. if (tty->ldisc.refcount) {
  903. /* Free the new ldisc we grabbed. Must drop the lock
  904. first. */
  905. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  906. tty_ldisc_put(ldisc);
  907. /*
  908. * There are several reasons we may be busy, including
  909. * random momentary I/O traffic. We must therefore
  910. * retry. We could distinguish between blocking ops
  911. * and retries if we made tty_ldisc_wait() smarter.
  912. * That is up for discussion.
  913. */
  914. if (wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0)
  915. return -ERESTARTSYS;
  916. goto restart;
  917. }
  918. if (o_tty && o_tty->ldisc.refcount) {
  919. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  920. tty_ldisc_put(ldisc);
  921. if (wait_event_interruptible(tty_ldisc_wait, o_tty->ldisc.refcount == 0) < 0)
  922. return -ERESTARTSYS;
  923. goto restart;
  924. }
  925. }
  926. /*
  927. * If the TTY_LDISC bit is set, then we are racing against
  928. * another ldisc change
  929. */
  930. if (!test_bit(TTY_LDISC, &tty->flags)) {
  931. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  932. tty_ldisc_put(ldisc);
  933. ld = tty_ldisc_ref_wait(tty);
  934. tty_ldisc_deref(ld);
  935. goto restart;
  936. }
  937. clear_bit(TTY_LDISC, &tty->flags);
  938. if (o_tty)
  939. clear_bit(TTY_LDISC, &o_tty->flags);
  940. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  941. /*
  942. * From this point on we know nobody has an ldisc
  943. * usage reference, nor can they obtain one until
  944. * we say so later on.
  945. */
  946. work = cancel_delayed_work(&tty->buf.work);
  947. /*
  948. * Wait for ->hangup_work and ->buf.work handlers to terminate
  949. */
  950. flush_scheduled_work();
  951. /* Shutdown the current discipline. */
  952. if (tty->ldisc.close)
  953. (tty->ldisc.close)(tty);
  954. /* Now set up the new line discipline. */
  955. tty_ldisc_assign(tty, ld);
  956. tty_set_termios_ldisc(tty, ldisc);
  957. if (tty->ldisc.open)
  958. retval = (tty->ldisc.open)(tty);
  959. if (retval < 0) {
  960. tty_ldisc_put(ldisc);
  961. /* There is an outstanding reference here so this is safe */
  962. tty_ldisc_assign(tty, tty_ldisc_get(o_ldisc.num));
  963. tty_set_termios_ldisc(tty, tty->ldisc.num);
  964. if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
  965. tty_ldisc_put(o_ldisc.num);
  966. /* This driver is always present */
  967. tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
  968. tty_set_termios_ldisc(tty, N_TTY);
  969. if (tty->ldisc.open) {
  970. int r = tty->ldisc.open(tty);
  971. if (r < 0)
  972. panic("Couldn't open N_TTY ldisc for "
  973. "%s --- error %d.",
  974. tty_name(tty, buf), r);
  975. }
  976. }
  977. }
  978. /* At this point we hold a reference to the new ldisc and a
  979. a reference to the old ldisc. If we ended up flipping back
  980. to the existing ldisc we have two references to it */
  981. if (tty->ldisc.num != o_ldisc.num && tty->ops->set_ldisc)
  982. tty->ops->set_ldisc(tty);
  983. tty_ldisc_put(o_ldisc.num);
  984. /*
  985. * Allow ldisc referencing to occur as soon as the driver
  986. * ldisc callback completes.
  987. */
  988. tty_ldisc_enable(tty);
  989. if (o_tty)
  990. tty_ldisc_enable(o_tty);
  991. /* Restart it in case no characters kick it off. Safe if
  992. already running */
  993. if (work)
  994. schedule_delayed_work(&tty->buf.work, 1);
  995. return retval;
  996. }
  997. /**
  998. * get_tty_driver - find device of a tty
  999. * @dev_t: device identifier
  1000. * @index: returns the index of the tty
  1001. *
  1002. * This routine returns a tty driver structure, given a device number
  1003. * and also passes back the index number.
  1004. *
  1005. * Locking: caller must hold tty_mutex
  1006. */
  1007. static struct tty_driver *get_tty_driver(dev_t device, int *index)
  1008. {
  1009. struct tty_driver *p;
  1010. list_for_each_entry(p, &tty_drivers, tty_drivers) {
  1011. dev_t base = MKDEV(p->major, p->minor_start);
  1012. if (device < base || device >= base + p->num)
  1013. continue;
  1014. *index = device - base;
  1015. return p;
  1016. }
  1017. return NULL;
  1018. }
  1019. #ifdef CONFIG_CONSOLE_POLL
  1020. /**
  1021. * tty_find_polling_driver - find device of a polled tty
  1022. * @name: name string to match
  1023. * @line: pointer to resulting tty line nr
  1024. *
  1025. * This routine returns a tty driver structure, given a name
  1026. * and the condition that the tty driver is capable of polled
  1027. * operation.
  1028. */
  1029. struct tty_driver *tty_find_polling_driver(char *name, int *line)
  1030. {
  1031. struct tty_driver *p, *res = NULL;
  1032. int tty_line = 0;
  1033. char *str;
  1034. mutex_lock(&tty_mutex);
  1035. /* Search through the tty devices to look for a match */
  1036. list_for_each_entry(p, &tty_drivers, tty_drivers) {
  1037. str = name + strlen(p->name);
  1038. tty_line = simple_strtoul(str, &str, 10);
  1039. if (*str == ',')
  1040. str++;
  1041. if (*str == '\0')
  1042. str = NULL;
  1043. if (tty_line >= 0 && tty_line <= p->num && p->ops &&
  1044. p->ops->poll_init && !p->ops->poll_init(p, tty_line, str)) {
  1045. res = p;
  1046. *line = tty_line;
  1047. break;
  1048. }
  1049. }
  1050. mutex_unlock(&tty_mutex);
  1051. return res;
  1052. }
  1053. EXPORT_SYMBOL_GPL(tty_find_polling_driver);
  1054. #endif
  1055. /**
  1056. * tty_check_change - check for POSIX terminal changes
  1057. * @tty: tty to check
  1058. *
  1059. * If we try to write to, or set the state of, a terminal and we're
  1060. * not in the foreground, send a SIGTTOU. If the signal is blocked or
  1061. * ignored, go ahead and perform the operation. (POSIX 7.2)
  1062. *
  1063. * Locking: ctrl_lock
  1064. */
  1065. int tty_check_change(struct tty_struct *tty)
  1066. {
  1067. unsigned long flags;
  1068. int ret = 0;
  1069. if (current->signal->tty != tty)
  1070. return 0;
  1071. spin_lock_irqsave(&tty->ctrl_lock, flags);
  1072. if (!tty->pgrp) {
  1073. printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
  1074. goto out_unlock;
  1075. }
  1076. if (task_pgrp(current) == tty->pgrp)
  1077. goto out_unlock;
  1078. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  1079. if (is_ignored(SIGTTOU))
  1080. goto out;
  1081. if (is_current_pgrp_orphaned()) {
  1082. ret = -EIO;
  1083. goto out;
  1084. }
  1085. kill_pgrp(task_pgrp(current), SIGTTOU, 1);
  1086. set_thread_flag(TIF_SIGPENDING);
  1087. ret = -ERESTARTSYS;
  1088. out:
  1089. return ret;
  1090. out_unlock:
  1091. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  1092. return ret;
  1093. }
  1094. EXPORT_SYMBOL(tty_check_change);
  1095. static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
  1096. size_t count, loff_t *ppos)
  1097. {
  1098. return 0;
  1099. }
  1100. static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
  1101. size_t count, loff_t *ppos)
  1102. {
  1103. return -EIO;
  1104. }
  1105. /* No kernel lock held - none needed ;) */
  1106. static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
  1107. {
  1108. return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
  1109. }
  1110. static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
  1111. unsigned long arg)
  1112. {
  1113. return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
  1114. }
  1115. static long hung_up_tty_compat_ioctl(struct file *file,
  1116. unsigned int cmd, unsigned long arg)
  1117. {
  1118. return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
  1119. }
  1120. static const struct file_operations tty_fops = {
  1121. .llseek = no_llseek,
  1122. .read = tty_read,
  1123. .write = tty_write,
  1124. .poll = tty_poll,
  1125. .unlocked_ioctl = tty_ioctl,
  1126. .compat_ioctl = tty_compat_ioctl,
  1127. .open = tty_open,
  1128. .release = tty_release,
  1129. .fasync = tty_fasync,
  1130. };
  1131. #ifdef CONFIG_UNIX98_PTYS
  1132. static const struct file_operations ptmx_fops = {
  1133. .llseek = no_llseek,
  1134. .read = tty_read,
  1135. .write = tty_write,
  1136. .poll = tty_poll,
  1137. .unlocked_ioctl = tty_ioctl,
  1138. .compat_ioctl = tty_compat_ioctl,
  1139. .open = ptmx_open,
  1140. .release = tty_release,
  1141. .fasync = tty_fasync,
  1142. };
  1143. #endif
  1144. static const struct file_operations console_fops = {
  1145. .llseek = no_llseek,
  1146. .read = tty_read,
  1147. .write = redirected_tty_write,
  1148. .poll = tty_poll,
  1149. .unlocked_ioctl = tty_ioctl,
  1150. .compat_ioctl = tty_compat_ioctl,
  1151. .open = tty_open,
  1152. .release = tty_release,
  1153. .fasync = tty_fasync,
  1154. };
  1155. static const struct file_operations hung_up_tty_fops = {
  1156. .llseek = no_llseek,
  1157. .read = hung_up_tty_read,
  1158. .write = hung_up_tty_write,
  1159. .poll = hung_up_tty_poll,
  1160. .unlocked_ioctl = hung_up_tty_ioctl,
  1161. .compat_ioctl = hung_up_tty_compat_ioctl,
  1162. .release = tty_release,
  1163. };
  1164. static DEFINE_SPINLOCK(redirect_lock);
  1165. static struct file *redirect;
  1166. /**
  1167. * tty_wakeup - request more data
  1168. * @tty: terminal
  1169. *
  1170. * Internal and external helper for wakeups of tty. This function
  1171. * informs the line discipline if present that the driver is ready
  1172. * to receive more output data.
  1173. */
  1174. void tty_wakeup(struct tty_struct *tty)
  1175. {
  1176. struct tty_ldisc *ld;
  1177. if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
  1178. ld = tty_ldisc_ref(tty);
  1179. if (ld) {
  1180. if (ld->write_wakeup)
  1181. ld->write_wakeup(tty);
  1182. tty_ldisc_deref(ld);
  1183. }
  1184. }
  1185. wake_up_interruptible(&tty->write_wait);
  1186. }
  1187. EXPORT_SYMBOL_GPL(tty_wakeup);
  1188. /**
  1189. * tty_ldisc_flush - flush line discipline queue
  1190. * @tty: tty
  1191. *
  1192. * Flush the line discipline queue (if any) for this tty. If there
  1193. * is no line discipline active this is a no-op.
  1194. */
  1195. void tty_ldisc_flush(struct tty_struct *tty)
  1196. {
  1197. struct tty_ldisc *ld = tty_ldisc_ref(tty);
  1198. if (ld) {
  1199. if (ld->flush_buffer)
  1200. ld->flush_buffer(tty);
  1201. tty_ldisc_deref(ld);
  1202. }
  1203. tty_buffer_flush(tty);
  1204. }
  1205. EXPORT_SYMBOL_GPL(tty_ldisc_flush);
  1206. /**
  1207. * tty_reset_termios - reset terminal state
  1208. * @tty: tty to reset
  1209. *
  1210. * Restore a terminal to the driver default state
  1211. */
  1212. static void tty_reset_termios(struct tty_struct *tty)
  1213. {
  1214. mutex_lock(&tty->termios_mutex);
  1215. *tty->termios = tty->driver->init_termios;
  1216. tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
  1217. tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
  1218. mutex_unlock(&tty->termios_mutex);
  1219. }
  1220. /**
  1221. * do_tty_hangup - actual handler for hangup events
  1222. * @work: tty device
  1223. *
  1224. * This can be called by the "eventd" kernel thread. That is process
  1225. * synchronous but doesn't hold any locks, so we need to make sure we
  1226. * have the appropriate locks for what we're doing.
  1227. *
  1228. * The hangup event clears any pending redirections onto the hung up
  1229. * device. It ensures future writes will error and it does the needed
  1230. * line discipline hangup and signal delivery. The tty object itself
  1231. * remains intact.
  1232. *
  1233. * Locking:
  1234. * BKL
  1235. * redirect lock for undoing redirection
  1236. * file list lock for manipulating list of ttys
  1237. * tty_ldisc_lock from called functions
  1238. * termios_mutex resetting termios data
  1239. * tasklist_lock to walk task list for hangup event
  1240. * ->siglock to protect ->signal/->sighand
  1241. */
  1242. static void do_tty_hangup(struct work_struct *work)
  1243. {
  1244. struct tty_struct *tty =
  1245. container_of(work, struct tty_struct, hangup_work);
  1246. struct file *cons_filp = NULL;
  1247. struct file *filp, *f = NULL;
  1248. struct task_struct *p;
  1249. struct tty_ldisc *ld;
  1250. int closecount = 0, n;
  1251. unsigned long flags;
  1252. if (!tty)
  1253. return;
  1254. /* inuse_filps is protected by the single kernel lock */
  1255. lock_kernel();
  1256. spin_lock(&redirect_lock);
  1257. if (redirect && redirect->private_data == tty) {
  1258. f = redirect;
  1259. redirect = NULL;
  1260. }
  1261. spin_unlock(&redirect_lock);
  1262. check_tty_count(tty, "do_tty_hangup");
  1263. file_list_lock();
  1264. /* This breaks for file handles being sent over AF_UNIX sockets ? */
  1265. list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
  1266. if (filp->f_op->write == redirected_tty_write)
  1267. cons_filp = filp;
  1268. if (filp->f_op->write != tty_write)
  1269. continue;
  1270. closecount++;
  1271. tty_fasync(-1, filp, 0); /* can't block */
  1272. filp->f_op = &hung_up_tty_fops;
  1273. }
  1274. file_list_unlock();
  1275. /*
  1276. * FIXME! What are the locking issues here? This may me overdoing
  1277. * things... This question is especially important now that we've
  1278. * removed the irqlock.
  1279. */
  1280. ld = tty_ldisc_ref(tty);
  1281. if (ld != NULL) {
  1282. /* We may have no line discipline at this point */
  1283. if (ld->flush_buffer)
  1284. ld->flush_buffer(tty);
  1285. tty_driver_flush_buffer(tty);
  1286. if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
  1287. ld->write_wakeup)
  1288. ld->write_wakeup(tty);
  1289. if (ld->hangup)
  1290. ld->hangup(tty);
  1291. }
  1292. /*
  1293. * FIXME: Once we trust the LDISC code better we can wait here for
  1294. * ldisc completion and fix the driver call race
  1295. */
  1296. wake_up_interruptible(&tty->write_wait);
  1297. wake_up_interruptible(&tty->read_wait);
  1298. /*
  1299. * Shutdown the current line discipline, and reset it to
  1300. * N_TTY.
  1301. */
  1302. if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
  1303. tty_reset_termios(tty);
  1304. /* Defer ldisc switch */
  1305. /* tty_deferred_ldisc_switch(N_TTY);
  1306. This should get done automatically when the port closes and
  1307. tty_release is called */
  1308. read_lock(&tasklist_lock);
  1309. if (tty->session) {
  1310. do_each_pid_task(tty->session, PIDTYPE_SID, p) {
  1311. spin_lock_irq(&p->sighand->siglock);
  1312. if (p->signal->tty == tty)
  1313. p->signal->tty = NULL;
  1314. if (!p->signal->leader) {
  1315. spin_unlock_irq(&p->sighand->siglock);
  1316. continue;
  1317. }
  1318. __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
  1319. __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
  1320. put_pid(p->signal->tty_old_pgrp); /* A noop */
  1321. spin_lock_irqsave(&tty->ctrl_lock, flags);
  1322. if (tty->pgrp)
  1323. p->signal->tty_old_pgrp = get_pid(tty->pgrp);
  1324. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  1325. spin_unlock_irq(&p->sighand->siglock);
  1326. } while_each_pid_task(tty->session, PIDTYPE_SID, p);
  1327. }
  1328. read_unlock(&tasklist_lock);
  1329. spin_lock_irqsave(&tty->ctrl_lock, flags);
  1330. tty->flags = 0;
  1331. put_pid(tty->session);
  1332. put_pid(tty->pgrp);
  1333. tty->session = NULL;
  1334. tty->pgrp = NULL;
  1335. tty->ctrl_status = 0;
  1336. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  1337. /*
  1338. * If one of the devices matches a console pointer, we
  1339. * cannot just call hangup() because that will cause
  1340. * tty->count and state->count to go out of sync.
  1341. * So we just call close() the right number of times.
  1342. */
  1343. if (cons_filp) {
  1344. if (tty->ops->close)
  1345. for (n = 0; n < closecount; n++)
  1346. tty->ops->close(tty, cons_filp);
  1347. } else if (tty->ops->hangup)
  1348. (tty->ops->hangup)(tty);
  1349. /*
  1350. * We don't want to have driver/ldisc interactions beyond
  1351. * the ones we did here. The driver layer expects no
  1352. * calls after ->hangup() from the ldisc side. However we
  1353. * can't yet guarantee all that.
  1354. */
  1355. set_bit(TTY_HUPPED, &tty->flags);
  1356. if (ld) {
  1357. tty_ldisc_enable(tty);
  1358. tty_ldisc_deref(ld);
  1359. }
  1360. unlock_kernel();
  1361. if (f)
  1362. fput(f);
  1363. }
  1364. /**
  1365. * tty_hangup - trigger a hangup event
  1366. * @tty: tty to hangup
  1367. *
  1368. * A carrier loss (virtual or otherwise) has occurred on this like
  1369. * schedule a hangup sequence to run after this event.
  1370. */
  1371. void tty_hangup(struct tty_struct *tty)
  1372. {
  1373. #ifdef TTY_DEBUG_HANGUP
  1374. char buf[64];
  1375. printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
  1376. #endif
  1377. schedule_work(&tty->hangup_work);
  1378. }
  1379. EXPORT_SYMBOL(tty_hangup);
  1380. /**
  1381. * tty_vhangup - process vhangup
  1382. * @tty: tty to hangup
  1383. *
  1384. * The user has asked via system call for the terminal to be hung up.
  1385. * We do this synchronously so that when the syscall returns the process
  1386. * is complete. That guarantee is necessary for security reasons.
  1387. */
  1388. void tty_vhangup(struct tty_struct *tty)
  1389. {
  1390. #ifdef TTY_DEBUG_HANGUP
  1391. char buf[64];
  1392. printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
  1393. #endif
  1394. do_tty_hangup(&tty->hangup_work);
  1395. }
  1396. EXPORT_SYMBOL(tty_vhangup);
  1397. /**
  1398. * tty_hung_up_p - was tty hung up
  1399. * @filp: file pointer of tty
  1400. *
  1401. * Return true if the tty has been subject to a vhangup or a carrier
  1402. * loss
  1403. */
  1404. int tty_hung_up_p(struct file *filp)
  1405. {
  1406. return (filp->f_op == &hung_up_tty_fops);
  1407. }
  1408. EXPORT_SYMBOL(tty_hung_up_p);
  1409. /**
  1410. * is_tty - checker whether file is a TTY
  1411. * @filp: file handle that may be a tty
  1412. *
  1413. * Check if the file handle is a tty handle.
  1414. */
  1415. int is_tty(struct file *filp)
  1416. {
  1417. return filp->f_op->read == tty_read
  1418. || filp->f_op->read == hung_up_tty_read;
  1419. }
  1420. static void session_clear_tty(struct pid *session)
  1421. {
  1422. struct task_struct *p;
  1423. do_each_pid_task(session, PIDTYPE_SID, p) {
  1424. proc_clear_tty(p);
  1425. } while_each_pid_task(session, PIDTYPE_SID, p);
  1426. }
  1427. /**
  1428. * disassociate_ctty - disconnect controlling tty
  1429. * @on_exit: true if exiting so need to "hang up" the session
  1430. *
  1431. * This function is typically called only by the session leader, when
  1432. * it wants to disassociate itself from its controlling tty.
  1433. *
  1434. * It performs the following functions:
  1435. * (1) Sends a SIGHUP and SIGCONT to the foreground process group
  1436. * (2) Clears the tty from being controlling the session
  1437. * (3) Clears the controlling tty for all processes in the
  1438. * session group.
  1439. *
  1440. * The argument on_exit is set to 1 if called when a process is
  1441. * exiting; it is 0 if called by the ioctl TIOCNOTTY.
  1442. *
  1443. * Locking:
  1444. * BKL is taken for hysterical raisins
  1445. * tty_mutex is taken to protect tty
  1446. * ->siglock is taken to protect ->signal/->sighand
  1447. * tasklist_lock is taken to walk process list for sessions
  1448. * ->siglock is taken to protect ->signal/->sighand
  1449. */
  1450. void disassociate_ctty(int on_exit)
  1451. {
  1452. struct tty_struct *tty;
  1453. struct pid *tty_pgrp = NULL;
  1454. mutex_lock(&tty_mutex);
  1455. tty = get_current_tty();
  1456. if (tty) {
  1457. tty_pgrp = get_pid(tty->pgrp);
  1458. mutex_unlock(&tty_mutex);
  1459. lock_kernel();
  1460. /* XXX: here we race, there is nothing protecting tty */
  1461. if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
  1462. tty_vhangup(tty);
  1463. unlock_kernel();
  1464. } else if (on_exit) {
  1465. struct pid *old_pgrp;
  1466. spin_lock_irq(&current->sighand->siglock);
  1467. old_pgrp = current->signal->tty_old_pgrp;
  1468. current->signal->tty_old_pgrp = NULL;
  1469. spin_unlock_irq(&current->sighand->siglock);
  1470. if (old_pgrp) {
  1471. kill_pgrp(old_pgrp, SIGHUP, on_exit);
  1472. kill_pgrp(old_pgrp, SIGCONT, on_exit);
  1473. put_pid(old_pgrp);
  1474. }
  1475. mutex_unlock(&tty_mutex);
  1476. return;
  1477. }
  1478. if (tty_pgrp) {
  1479. kill_pgrp(tty_pgrp, SIGHUP, on_exit);
  1480. if (!on_exit)
  1481. kill_pgrp(tty_pgrp, SIGCONT, on_exit);
  1482. put_pid(tty_pgrp);
  1483. }
  1484. spin_lock_irq(&current->sighand->siglock);
  1485. put_pid(current->signal->tty_old_pgrp);
  1486. current->signal->tty_old_pgrp = NULL;
  1487. spin_unlock_irq(&current->sighand->siglock);
  1488. mutex_lock(&tty_mutex);
  1489. /* It is possible that do_tty_hangup has free'd this tty */
  1490. tty = get_current_tty();
  1491. if (tty) {
  1492. unsigned long flags;
  1493. spin_lock_irqsave(&tty->ctrl_lock, flags);
  1494. put_pid(tty->session);
  1495. put_pid(tty->pgrp);
  1496. tty->session = NULL;
  1497. tty->pgrp = NULL;
  1498. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  1499. } else {
  1500. #ifdef TTY_DEBUG_HANGUP
  1501. printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
  1502. " = NULL", tty);
  1503. #endif
  1504. }
  1505. mutex_unlock(&tty_mutex);
  1506. /* Now clear signal->tty under the lock */
  1507. read_lock(&tasklist_lock);
  1508. session_clear_tty(task_session(current));
  1509. read_unlock(&tasklist_lock);
  1510. }
  1511. /**
  1512. *
  1513. * no_tty - Ensure the current process does not have a controlling tty
  1514. */
  1515. void no_tty(void)
  1516. {
  1517. struct task_struct *tsk = current;
  1518. lock_kernel();
  1519. if (tsk->signal->leader)
  1520. disassociate_ctty(0);
  1521. unlock_kernel();
  1522. proc_clear_tty(tsk);
  1523. }
  1524. /**
  1525. * stop_tty - propagate flow control
  1526. * @tty: tty to stop
  1527. *
  1528. * Perform flow control to the driver. For PTY/TTY pairs we
  1529. * must also propagate the TIOCKPKT status. May be called
  1530. * on an already stopped device and will not re-call the driver
  1531. * method.
  1532. *
  1533. * This functionality is used by both the line disciplines for
  1534. * halting incoming flow and by the driver. It may therefore be
  1535. * called from any context, may be under the tty atomic_write_lock
  1536. * but not always.
  1537. *
  1538. * Locking:
  1539. * Uses the tty control lock internally
  1540. */
  1541. void stop_tty(struct tty_struct *tty)
  1542. {
  1543. unsigned long flags;
  1544. spin_lock_irqsave(&tty->ctrl_lock, flags);
  1545. if (tty->stopped) {
  1546. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  1547. return;
  1548. }
  1549. tty->stopped = 1;
  1550. if (tty->link && tty->link->packet) {
  1551. tty->ctrl_status &= ~TIOCPKT_START;
  1552. tty->ctrl_status |= TIOCPKT_STOP;
  1553. wake_up_interruptible(&tty->link->read_wait);
  1554. }
  1555. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  1556. if (tty->ops->stop)
  1557. (tty->ops->stop)(tty);
  1558. }
  1559. EXPORT_SYMBOL(stop_tty);
  1560. /**
  1561. * start_tty - propagate flow control
  1562. * @tty: tty to start
  1563. *
  1564. * Start a tty that has been stopped if at all possible. Perform
  1565. * any necessary wakeups and propagate the TIOCPKT status. If this
  1566. * is the tty was previous stopped and is being started then the
  1567. * driver start method is invoked and the line discipline woken.
  1568. *
  1569. * Locking:
  1570. * ctrl_lock
  1571. */
  1572. void start_tty(struct tty_struct *tty)
  1573. {
  1574. unsigned long flags;
  1575. spin_lock_irqsave(&tty->ctrl_lock, flags);
  1576. if (!tty->stopped || tty->flow_stopped) {
  1577. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  1578. return;
  1579. }
  1580. tty->stopped = 0;
  1581. if (tty->link && tty->link->packet) {
  1582. tty->ctrl_status &= ~TIOCPKT_STOP;
  1583. tty->ctrl_status |= TIOCPKT_START;
  1584. wake_up_interruptible(&tty->link->read_wait);
  1585. }
  1586. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  1587. if (tty->ops->start)
  1588. (tty->ops->start)(tty);
  1589. /* If we have a running line discipline it may need kicking */
  1590. tty_wakeup(tty);
  1591. }
  1592. EXPORT_SYMBOL(start_tty);
  1593. /**
  1594. * tty_read - read method for tty device files
  1595. * @file: pointer to tty file
  1596. * @buf: user buffer
  1597. * @count: size of user buffer
  1598. * @ppos: unused
  1599. *
  1600. * Perform the read system call function on this terminal device. Checks
  1601. * for hung up devices before calling the line discipline method.
  1602. *
  1603. * Locking:
  1604. * Locks the line discipline internally while needed. Multiple
  1605. * read calls may be outstanding in parallel.
  1606. */
  1607. static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
  1608. loff_t *ppos)
  1609. {
  1610. int i;
  1611. struct tty_struct *tty;
  1612. struct inode *inode;
  1613. struct tty_ldisc *ld;
  1614. tty = (struct tty_struct *)file->private_data;
  1615. inode = file->f_path.dentry->d_inode;
  1616. if (tty_paranoia_check(tty, inode, "tty_read"))
  1617. return -EIO;
  1618. if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
  1619. return -EIO;
  1620. /* We want to wait for the line discipline to sort out in this
  1621. situation */
  1622. ld = tty_ldisc_ref_wait(tty);
  1623. if (ld->read)
  1624. i = (ld->read)(tty, file, buf, count);
  1625. else
  1626. i = -EIO;
  1627. tty_ldisc_deref(ld);
  1628. if (i > 0)
  1629. inode->i_atime = current_fs_time(inode->i_sb);
  1630. return i;
  1631. }
  1632. void tty_write_unlock(struct tty_struct *tty)
  1633. {
  1634. mutex_unlock(&tty->atomic_write_lock);
  1635. wake_up_interruptible(&tty->write_wait);
  1636. }
  1637. int tty_write_lock(struct tty_struct *tty, int ndelay)
  1638. {
  1639. if (!mutex_trylock(&tty->atomic_write_lock)) {
  1640. if (ndelay)
  1641. return -EAGAIN;
  1642. if (mutex_lock_interruptible(&tty->atomic_write_lock))
  1643. return -ERESTARTSYS;
  1644. }
  1645. return 0;
  1646. }
  1647. /*
  1648. * Split writes up in sane blocksizes to avoid
  1649. * denial-of-service type attacks
  1650. */
  1651. static inline ssize_t do_tty_write(
  1652. ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
  1653. struct tty_struct *tty,
  1654. struct file *file,
  1655. const char __user *buf,
  1656. size_t count)
  1657. {
  1658. ssize_t ret, written = 0;
  1659. unsigned int chunk;
  1660. ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
  1661. if (ret < 0)
  1662. return ret;
  1663. /*
  1664. * We chunk up writes into a temporary buffer. This
  1665. * simplifies low-level drivers immensely, since they
  1666. * don't have locking issues and user mode accesses.
  1667. *
  1668. * But if TTY_NO_WRITE_SPLIT is set, we should use a
  1669. * big chunk-size..
  1670. *
  1671. * The default chunk-size is 2kB, because the NTTY
  1672. * layer has problems with bigger chunks. It will
  1673. * claim to be able to handle more characters than
  1674. * it actually does.
  1675. *
  1676. * FIXME: This can probably go away now except that 64K chunks
  1677. * are too likely to fail unless switched to vmalloc...
  1678. */
  1679. chunk = 2048;
  1680. if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
  1681. chunk = 65536;
  1682. if (count < chunk)
  1683. chunk = count;
  1684. /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
  1685. if (tty->write_cnt < chunk) {
  1686. unsigned char *buf;
  1687. if (chunk < 1024)
  1688. chunk = 1024;
  1689. buf = kmalloc(chunk, GFP_KERNEL);
  1690. if (!buf) {
  1691. ret = -ENOMEM;
  1692. goto out;
  1693. }
  1694. kfree(tty->write_buf);
  1695. tty->write_cnt = chunk;
  1696. tty->write_buf = buf;
  1697. }
  1698. /* Do the write .. */
  1699. for (;;) {
  1700. size_t size = count;
  1701. if (size > chunk)
  1702. size = chunk;
  1703. ret = -EFAULT;
  1704. if (copy_from_user(tty->write_buf, buf, size))
  1705. break;
  1706. ret = write(tty, file, tty->write_buf, size);
  1707. if (ret <= 0)
  1708. break;
  1709. written += ret;
  1710. buf += ret;
  1711. count -= ret;
  1712. if (!count)
  1713. break;
  1714. ret = -ERESTARTSYS;
  1715. if (signal_pending(current))
  1716. break;
  1717. cond_resched();
  1718. }
  1719. if (written) {
  1720. struct inode *inode = file->f_path.dentry->d_inode;
  1721. inode->i_mtime = current_fs_time(inode->i_sb);
  1722. ret = written;
  1723. }
  1724. out:
  1725. tty_write_unlock(tty);
  1726. return ret;
  1727. }
  1728. /**
  1729. * tty_write - write method for tty device file
  1730. * @file: tty file pointer
  1731. * @buf: user data to write
  1732. * @count: bytes to write
  1733. * @ppos: unused
  1734. *
  1735. * Write data to a tty device via the line discipline.
  1736. *
  1737. * Locking:
  1738. * Locks the line discipline as required
  1739. * Writes to the tty driver are serialized by the atomic_write_lock
  1740. * and are then processed in chunks to the device. The line discipline
  1741. * write method will not be involked in parallel for each device
  1742. * The line discipline write method is called under the big
  1743. * kernel lock for historical reasons. New code should not rely on this.
  1744. */
  1745. static ssize_t tty_write(struct file *file, const char __user *buf,
  1746. size_t count, loff_t *ppos)
  1747. {
  1748. struct tty_struct *tty;
  1749. struct inode *inode = file->f_path.dentry->d_inode;
  1750. ssize_t ret;
  1751. struct tty_ldisc *ld;
  1752. tty = (struct tty_struct *)file->private_data;
  1753. if (tty_paranoia_check(tty, inode, "tty_write"))
  1754. return -EIO;
  1755. if (!tty || !tty->ops->write ||
  1756. (test_bit(TTY_IO_ERROR, &tty->flags)))
  1757. return -EIO;
  1758. /* Short term debug to catch buggy drivers */
  1759. if (tty->ops->write_room == NULL)
  1760. printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
  1761. tty->driver->name);
  1762. ld = tty_ldisc_ref_wait(tty);
  1763. if (!ld->write)
  1764. ret = -EIO;
  1765. else
  1766. ret = do_tty_write(ld->write, tty, file, buf, count);
  1767. tty_ldisc_deref(ld);
  1768. return ret;
  1769. }
  1770. ssize_t redirected_tty_write(struct file *file, const char __user *buf,
  1771. size_t count, loff_t *ppos)
  1772. {
  1773. struct file *p = NULL;
  1774. spin_lock(&redirect_lock);
  1775. if (redirect) {
  1776. get_file(redirect);
  1777. p = redirect;
  1778. }
  1779. spin_unlock(&redirect_lock);
  1780. if (p) {
  1781. ssize_t res;
  1782. res = vfs_write(p, buf, count, &p->f_pos);
  1783. fput(p);
  1784. return res;
  1785. }
  1786. return tty_write(file, buf, count, ppos);
  1787. }
  1788. static char ptychar[] = "pqrstuvwxyzabcde";
  1789. /**
  1790. * pty_line_name - generate name for a pty
  1791. * @driver: the tty driver in use
  1792. * @index: the minor number
  1793. * @p: output buffer of at least 6 bytes
  1794. *
  1795. * Generate a name from a driver reference and write it to the output
  1796. * buffer.
  1797. *
  1798. * Locking: None
  1799. */
  1800. static void pty_line_name(struct tty_driver *driver, int index, char *p)
  1801. {
  1802. int i = index + driver->name_base;
  1803. /* ->name is initialized to "ttyp", but "tty" is expected */
  1804. sprintf(p, "%s%c%x",
  1805. driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
  1806. ptychar[i >> 4 & 0xf], i & 0xf);
  1807. }
  1808. /**
  1809. * pty_line_name - generate name for a tty
  1810. * @driver: the tty driver in use
  1811. * @index: the minor number
  1812. * @p: output buffer of at least 7 bytes
  1813. *
  1814. * Generate a name from a driver reference and write it to the output
  1815. * buffer.
  1816. *
  1817. * Locking: None
  1818. */
  1819. static void tty_line_name(struct tty_driver *driver, int index, char *p)
  1820. {
  1821. sprintf(p, "%s%d", driver->name, index + driver->name_base);
  1822. }
  1823. /**
  1824. * init_dev - initialise a tty device
  1825. * @driver: tty driver we are opening a device on
  1826. * @idx: device index
  1827. * @tty: returned tty structure
  1828. *
  1829. * Prepare a tty device. This may not be a "new" clean device but
  1830. * could also be an active device. The pty drivers require special
  1831. * handling because of this.
  1832. *
  1833. * Locking:
  1834. * The function is called under the tty_mutex, which
  1835. * protects us from the tty struct or driver itself going away.
  1836. *
  1837. * On exit the tty device has the line discipline attached and
  1838. * a reference count of 1. If a pair was created for pty/tty use
  1839. * and the other was a pty master then it too has a reference count of 1.
  1840. *
  1841. * WSH 06/09/97: Rewritten to remove races and properly clean up after a
  1842. * failed open. The new code protects the open with a mutex, so it's
  1843. * really quite straightforward. The mutex locking can probably be
  1844. * relaxed for the (most common) case of reopening a tty.
  1845. */
  1846. static int init_dev(struct tty_driver *driver, int idx,
  1847. struct tty_struct **ret_tty)
  1848. {
  1849. struct tty_struct *tty, *o_tty;
  1850. struct ktermios *tp, **tp_loc, *o_tp, **o_tp_loc;
  1851. struct ktermios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
  1852. int retval = 0;
  1853. /* check whether we're reopening an existing tty */
  1854. if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
  1855. tty = devpts_get_tty(idx);
  1856. /*
  1857. * If we don't have a tty here on a slave open, it's because
  1858. * the master already started the close process and there's
  1859. * no relation between devpts file and tty anymore.
  1860. */
  1861. if (!tty && driver->subtype == PTY_TYPE_SLAVE) {
  1862. retval = -EIO;
  1863. goto end_init;
  1864. }
  1865. /*
  1866. * It's safe from now on because init_dev() is called with
  1867. * tty_mutex held and release_dev() won't change tty->count
  1868. * or tty->flags without having to grab tty_mutex
  1869. */
  1870. if (tty && driver->subtype == PTY_TYPE_MASTER)
  1871. tty = tty->link;
  1872. } else {
  1873. tty = driver->ttys[idx];
  1874. }
  1875. if (tty) goto fast_track;
  1876. /*
  1877. * First time open is complex, especially for PTY devices.
  1878. * This code guarantees that either everything succeeds and the
  1879. * TTY is ready for operation, or else the table slots are vacated
  1880. * and the allocated memory released. (Except that the termios
  1881. * and locked termios may be retained.)
  1882. */
  1883. if (!try_module_get(driver->owner)) {
  1884. retval = -ENODEV;
  1885. goto end_init;
  1886. }
  1887. o_tty = NULL;
  1888. tp = o_tp = NULL;
  1889. ltp = o_ltp = NULL;
  1890. tty = alloc_tty_struct();
  1891. if (!tty)
  1892. goto fail_no_mem;
  1893. initialize_tty_struct(tty);
  1894. tty->driver = driver;
  1895. tty->ops = driver->ops;
  1896. tty->index = idx;
  1897. tty_line_name(driver, idx, tty->name);
  1898. if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
  1899. tp_loc = &tty->termios;
  1900. ltp_loc = &tty->termios_locked;
  1901. } else {
  1902. tp_loc = &driver->termios[idx];
  1903. ltp_loc = &driver->termios_locked[idx];
  1904. }
  1905. if (!*tp_loc) {
  1906. tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
  1907. if (!tp)
  1908. goto free_mem_out;
  1909. *tp = driver->init_termios;
  1910. }
  1911. if (!*ltp_loc) {
  1912. ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
  1913. if (!ltp)
  1914. goto free_mem_out;
  1915. }
  1916. if (driver->type == TTY_DRIVER_TYPE_PTY) {
  1917. o_tty = alloc_tty_struct();
  1918. if (!o_tty)
  1919. goto free_mem_out;
  1920. initialize_tty_struct(o_tty);
  1921. o_tty->driver = driver->other;
  1922. o_tty->ops = driver->ops;
  1923. o_tty->index = idx;
  1924. tty_line_name(driver->other, idx, o_tty->name);
  1925. if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
  1926. o_tp_loc = &o_tty->termios;
  1927. o_ltp_loc = &o_tty->termios_locked;
  1928. } else {
  1929. o_tp_loc = &driver->other->termios[idx];
  1930. o_ltp_loc = &driver->other->termios_locked[idx];
  1931. }
  1932. if (!*o_tp_loc) {
  1933. o_tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
  1934. if (!o_tp)
  1935. goto free_mem_out;
  1936. *o_tp = driver->other->init_termios;
  1937. }
  1938. if (!*o_ltp_loc) {
  1939. o_ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
  1940. if (!o_ltp)
  1941. goto free_mem_out;
  1942. }
  1943. /*
  1944. * Everything allocated ... set up the o_tty structure.
  1945. */
  1946. if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM))
  1947. driver->other->ttys[idx] = o_tty;
  1948. if (!*o_tp_loc)
  1949. *o_tp_loc = o_tp;
  1950. if (!*o_ltp_loc)
  1951. *o_ltp_loc = o_ltp;
  1952. o_tty->termios = *o_tp_loc;
  1953. o_tty->termios_locked = *o_ltp_loc;
  1954. driver->other->refcount++;
  1955. if (driver->subtype == PTY_TYPE_MASTER)
  1956. o_tty->count++;
  1957. /* Establish the links in both directions */
  1958. tty->link = o_tty;
  1959. o_tty->link = tty;
  1960. }
  1961. /*
  1962. * All structures have been allocated, so now we install them.
  1963. * Failures after this point use release_tty to clean up, so
  1964. * there's no need to null out the local pointers.
  1965. */
  1966. if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM))
  1967. driver->ttys[idx] = tty;
  1968. if (!*tp_loc)
  1969. *tp_loc = tp;
  1970. if (!*ltp_loc)
  1971. *ltp_loc = ltp;
  1972. tty->termios = *tp_loc;
  1973. tty->termios_locked = *ltp_loc;
  1974. /* Compatibility until drivers always set this */
  1975. tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
  1976. tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
  1977. driver->refcount++;
  1978. tty->count++;
  1979. /*
  1980. * Structures all installed ... call the ldisc open routines.
  1981. * If we fail here just call release_tty to clean up. No need
  1982. * to decrement the use counts, as release_tty doesn't care.
  1983. */
  1984. if (tty->ldisc.open) {
  1985. retval = (tty->ldisc.open)(tty);
  1986. if (retval)
  1987. goto release_mem_out;
  1988. }
  1989. if (o_tty && o_tty->ldisc.open) {
  1990. retval = (o_tty->ldisc.open)(o_tty);
  1991. if (retval) {
  1992. if (tty->ldisc.close)
  1993. (tty->ldisc.close)(tty);
  1994. goto release_mem_out;
  1995. }
  1996. tty_ldisc_enable(o_tty);
  1997. }
  1998. tty_ldisc_enable(tty);
  1999. goto success;
  2000. /*
  2001. * This fast open can be used if the tty is already open.
  2002. * No memory is allocated, and the only failures are from
  2003. * attempting to open a closing tty or attempting multiple
  2004. * opens on a pty master.
  2005. */
  2006. fast_track:
  2007. if (test_bit(TTY_CLOSING, &tty->flags)) {
  2008. retval = -EIO;
  2009. goto end_init;
  2010. }
  2011. if (driver->type == TTY_DRIVER_TYPE_PTY &&
  2012. driver->subtype == PTY_TYPE_MASTER) {
  2013. /*
  2014. * special case for PTY masters: only one open permitted,
  2015. * and the slave side open count is incremented as well.
  2016. */
  2017. if (tty->count) {
  2018. retval = -EIO;
  2019. goto end_init;
  2020. }
  2021. tty->link->count++;
  2022. }
  2023. tty->count++;
  2024. tty->driver = driver; /* N.B. why do this every time?? */
  2025. /* FIXME */
  2026. if (!test_bit(TTY_LDISC, &tty->flags))
  2027. printk(KERN_ERR "init_dev but no ldisc\n");
  2028. success:
  2029. *ret_tty = tty;
  2030. /* All paths come through here to release the mutex */
  2031. end_init:
  2032. return retval;
  2033. /* Release locally allocated memory ... nothing placed in slots */
  2034. free_mem_out:
  2035. kfree(o_tp);
  2036. if (o_tty)
  2037. free_tty_struct(o_tty);
  2038. kfree(ltp);
  2039. kfree(tp);
  2040. free_tty_struct(tty);
  2041. fail_no_mem:
  2042. module_put(driver->owner);
  2043. retval = -ENOMEM;
  2044. goto end_init;
  2045. /* call the tty release_tty routine to clean out this slot */
  2046. release_mem_out:
  2047. if (printk_ratelimit())
  2048. printk(KERN_INFO "init_dev: ldisc open failed, "
  2049. "clearing slot %d\n", idx);
  2050. release_tty(tty, idx);
  2051. goto end_init;
  2052. }
  2053. /**
  2054. * release_one_tty - release tty structure memory
  2055. *
  2056. * Releases memory associated with a tty structure, and clears out the
  2057. * driver table slots. This function is called when a device is no longer
  2058. * in use. It also gets called when setup of a device fails.
  2059. *
  2060. * Locking:
  2061. * tty_mutex - sometimes only
  2062. * takes the file list lock internally when working on the list
  2063. * of ttys that the driver keeps.
  2064. * FIXME: should we require tty_mutex is held here ??
  2065. */
  2066. static void release_one_tty(struct tty_struct *tty, int idx)
  2067. {
  2068. int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM;
  2069. struct ktermios *tp;
  2070. if (!devpts)
  2071. tty->driver->ttys[idx] = NULL;
  2072. if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
  2073. tp = tty->termios;
  2074. if (!devpts)
  2075. tty->driver->termios[idx] = NULL;
  2076. kfree(tp);
  2077. tp = tty->termios_locked;
  2078. if (!devpts)
  2079. tty->driver->termios_locked[idx] = NULL;
  2080. kfree(tp);
  2081. }
  2082. tty->magic = 0;
  2083. tty->driver->refcount--;
  2084. file_list_lock();
  2085. list_del_init(&tty->tty_files);
  2086. file_list_unlock();
  2087. free_tty_struct(tty);
  2088. }
  2089. /**
  2090. * release_tty - release tty structure memory
  2091. *
  2092. * Release both @tty and a possible linked partner (think pty pair),
  2093. * and decrement the refcount of the backing module.
  2094. *
  2095. * Locking:
  2096. * tty_mutex - sometimes only
  2097. * takes the file list lock internally when working on the list
  2098. * of ttys that the driver keeps.
  2099. * FIXME: should we require tty_mutex is held here ??
  2100. */
  2101. static void release_tty(struct tty_struct *tty, int idx)
  2102. {
  2103. struct tty_driver *driver = tty->driver;
  2104. if (tty->link)
  2105. release_one_tty(tty->link, idx);
  2106. release_one_tty(tty, idx);
  2107. module_put(driver->owner);
  2108. }
  2109. /*
  2110. * Even releasing the tty structures is a tricky business.. We have
  2111. * to be very careful that the structures are all released at the
  2112. * same time, as interrupts might otherwise get the wrong pointers.
  2113. *
  2114. * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
  2115. * lead to double frees or releasing memory still in use.
  2116. */
  2117. static void release_dev(struct file *filp)
  2118. {
  2119. struct tty_struct *tty, *o_tty;
  2120. int pty_master, tty_closing, o_tty_closing, do_sleep;
  2121. int devpts;
  2122. int idx;
  2123. char buf[64];
  2124. unsigned long flags;
  2125. tty = (struct tty_struct *)filp->private_data;
  2126. if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode,
  2127. "release_dev"))
  2128. return;
  2129. check_tty_count(tty, "release_dev");
  2130. tty_fasync(-1, filp, 0);
  2131. idx = tty->index;
  2132. pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  2133. tty->driver->subtype == PTY_TYPE_MASTER);
  2134. devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
  2135. o_tty = tty->link;
  2136. #ifdef TTY_PARANOIA_CHECK
  2137. if (idx < 0 || idx >= tty->driver->num) {
  2138. printk(KERN_DEBUG "release_dev: bad idx when trying to "
  2139. "free (%s)\n", tty->name);
  2140. return;
  2141. }
  2142. if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
  2143. if (tty != tty->driver->ttys[idx]) {
  2144. printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
  2145. "for (%s)\n", idx, tty->name);
  2146. return;
  2147. }
  2148. if (tty->termios != tty->driver->termios[idx]) {
  2149. printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
  2150. "for (%s)\n",
  2151. idx, tty->name);
  2152. return;
  2153. }
  2154. if (tty->termios_locked != tty->driver->termios_locked[idx]) {
  2155. printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
  2156. "termios_locked for (%s)\n",
  2157. idx, tty->name);
  2158. return;
  2159. }
  2160. }
  2161. #endif
  2162. #ifdef TTY_DEBUG_HANGUP
  2163. printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
  2164. tty_name(tty, buf), tty->count);
  2165. #endif
  2166. #ifdef TTY_PARANOIA_CHECK
  2167. if (tty->driver->other &&
  2168. !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
  2169. if (o_tty != tty->driver->other->ttys[idx]) {
  2170. printk(KERN_DEBUG "release_dev: other->table[%d] "
  2171. "not o_tty for (%s)\n",
  2172. idx, tty->name);
  2173. return;
  2174. }
  2175. if (o_tty->termios != tty->driver->other->termios[idx]) {
  2176. printk(KERN_DEBUG "release_dev: other->termios[%d] "
  2177. "not o_termios for (%s)\n",
  2178. idx, tty->name);
  2179. return;
  2180. }
  2181. if (o_tty->termios_locked !=
  2182. tty->driver->other->termios_locked[idx]) {
  2183. printk(KERN_DEBUG "release_dev: other->termios_locked["
  2184. "%d] not o_termios_locked for (%s)\n",
  2185. idx, tty->name);
  2186. return;
  2187. }
  2188. if (o_tty->link != tty) {
  2189. printk(KERN_DEBUG "release_dev: bad pty pointers\n");
  2190. return;
  2191. }
  2192. }
  2193. #endif
  2194. if (tty->ops->close)
  2195. tty->ops->close(tty, filp);
  2196. /*
  2197. * Sanity check: if tty->count is going to zero, there shouldn't be
  2198. * any waiters on tty->read_wait or tty->write_wait. We test the
  2199. * wait queues and kick everyone out _before_ actually starting to
  2200. * close. This ensures that we won't block while releasing the tty
  2201. * structure.
  2202. *
  2203. * The test for the o_tty closing is necessary, since the master and
  2204. * slave sides may close in any order. If the slave side closes out
  2205. * first, its count will be one, since the master side holds an open.
  2206. * Thus this test wouldn't be triggered at the time the slave closes,
  2207. * so we do it now.
  2208. *
  2209. * Note that it's possible for the tty to be opened again while we're
  2210. * flushing out waiters. By recalculating the closing flags before
  2211. * each iteration we avoid any problems.
  2212. */
  2213. while (1) {
  2214. /* Guard against races with tty->count changes elsewhere and
  2215. opens on /dev/tty */
  2216. mutex_lock(&tty_mutex);
  2217. tty_closing = tty->count <= 1;
  2218. o_tty_closing = o_tty &&
  2219. (o_tty->count <= (pty_master ? 1 : 0));
  2220. do_sleep = 0;
  2221. if (tty_closing) {
  2222. if (waitqueue_active(&tty->read_wait)) {
  2223. wake_up(&tty->read_wait);
  2224. do_sleep++;
  2225. }
  2226. if (waitqueue_active(&tty->write_wait)) {
  2227. wake_up(&tty->write_wait);
  2228. do_sleep++;
  2229. }
  2230. }
  2231. if (o_tty_closing) {
  2232. if (waitqueue_active(&o_tty->read_wait)) {
  2233. wake_up(&o_tty->read_wait);
  2234. do_sleep++;
  2235. }
  2236. if (waitqueue_active(&o_tty->write_wait)) {
  2237. wake_up(&o_tty->write_wait);
  2238. do_sleep++;
  2239. }
  2240. }
  2241. if (!do_sleep)
  2242. break;
  2243. printk(KERN_WARNING "release_dev: %s: read/write wait queue "
  2244. "active!\n", tty_name(tty, buf));
  2245. mutex_unlock(&tty_mutex);
  2246. schedule();
  2247. }
  2248. /*
  2249. * The closing flags are now consistent with the open counts on
  2250. * both sides, and we've completed the last operation that could
  2251. * block, so it's safe to proceed with closing.
  2252. */
  2253. if (pty_master) {
  2254. if (--o_tty->count < 0) {
  2255. printk(KERN_WARNING "release_dev: bad pty slave count "
  2256. "(%d) for %s\n",
  2257. o_tty->count, tty_name(o_tty, buf));
  2258. o_tty->count = 0;
  2259. }
  2260. }
  2261. if (--tty->count < 0) {
  2262. printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
  2263. tty->count, tty_name(tty, buf));
  2264. tty->count = 0;
  2265. }
  2266. /*
  2267. * We've decremented tty->count, so we need to remove this file
  2268. * descriptor off the tty->tty_files list; this serves two
  2269. * purposes:
  2270. * - check_tty_count sees the correct number of file descriptors
  2271. * associated with this tty.
  2272. * - do_tty_hangup no longer sees this file descriptor as
  2273. * something that needs to be handled for hangups.
  2274. */
  2275. file_kill(filp);
  2276. filp->private_data = NULL;
  2277. /*
  2278. * Perform some housekeeping before deciding whether to return.
  2279. *
  2280. * Set the TTY_CLOSING flag if this was the last open. In the
  2281. * case of a pty we may have to wait around for the other side
  2282. * to close, and TTY_CLOSING makes sure we can't be reopened.
  2283. */
  2284. if (tty_closing)
  2285. set_bit(TTY_CLOSING, &tty->flags);
  2286. if (o_tty_closing)
  2287. set_bit(TTY_CLOSING, &o_tty->flags);
  2288. /*
  2289. * If _either_ side is closing, make sure there aren't any
  2290. * processes that still think tty or o_tty is their controlling
  2291. * tty.
  2292. */
  2293. if (tty_closing || o_tty_closing) {
  2294. read_lock(&tasklist_lock);
  2295. session_clear_tty(tty->session);
  2296. if (o_tty)
  2297. session_clear_tty(o_tty->session);
  2298. read_unlock(&tasklist_lock);
  2299. }
  2300. mutex_unlock(&tty_mutex);
  2301. /* check whether both sides are closing ... */
  2302. if (!tty_closing || (o_tty && !o_tty_closing))
  2303. return;
  2304. #ifdef TTY_DEBUG_HANGUP
  2305. printk(KERN_DEBUG "freeing tty structure...");
  2306. #endif
  2307. /*
  2308. * Prevent flush_to_ldisc() from rescheduling the work for later. Then
  2309. * kill any delayed work. As this is the final close it does not
  2310. * race with the set_ldisc code path.
  2311. */
  2312. clear_bit(TTY_LDISC, &tty->flags);
  2313. cancel_delayed_work(&tty->buf.work);
  2314. /*
  2315. * Wait for ->hangup_work and ->buf.work handlers to terminate
  2316. */
  2317. flush_scheduled_work();
  2318. /*
  2319. * Wait for any short term users (we know they are just driver
  2320. * side waiters as the file is closing so user count on the file
  2321. * side is zero.
  2322. */
  2323. spin_lock_irqsave(&tty_ldisc_lock, flags);
  2324. while (tty->ldisc.refcount) {
  2325. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  2326. wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0);
  2327. spin_lock_irqsave(&tty_ldisc_lock, flags);
  2328. }
  2329. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  2330. /*
  2331. * Shutdown the current line discipline, and reset it to N_TTY.
  2332. * N.B. why reset ldisc when we're releasing the memory??
  2333. *
  2334. * FIXME: this MUST get fixed for the new reflocking
  2335. */
  2336. if (tty->ldisc.close)
  2337. (tty->ldisc.close)(tty);
  2338. tty_ldisc_put(tty->ldisc.num);
  2339. /*
  2340. * Switch the line discipline back
  2341. */
  2342. tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
  2343. tty_set_termios_ldisc(tty, N_TTY);
  2344. if (o_tty) {
  2345. /* FIXME: could o_tty be in setldisc here ? */
  2346. clear_bit(TTY_LDISC, &o_tty->flags);
  2347. if (o_tty->ldisc.close)
  2348. (o_tty->ldisc.close)(o_tty);
  2349. tty_ldisc_put(o_tty->ldisc.num);
  2350. tty_ldisc_assign(o_tty, tty_ldisc_get(N_TTY));
  2351. tty_set_termios_ldisc(o_tty, N_TTY);
  2352. }
  2353. /*
  2354. * The release_tty function takes care of the details of clearing
  2355. * the slots and preserving the termios structure.
  2356. */
  2357. release_tty(tty, idx);
  2358. /* Make this pty number available for reallocation */
  2359. if (devpts)
  2360. devpts_kill_index(idx);
  2361. }
  2362. /**
  2363. * tty_open - open a tty device
  2364. * @inode: inode of device file
  2365. * @filp: file pointer to tty
  2366. *
  2367. * tty_open and tty_release keep up the tty count that contains the
  2368. * number of opens done on a tty. We cannot use the inode-count, as
  2369. * different inodes might point to the same tty.
  2370. *
  2371. * Open-counting is needed for pty masters, as well as for keeping
  2372. * track of serial lines: DTR is dropped when the last close happens.
  2373. * (This is not done solely through tty->count, now. - Ted 1/27/92)
  2374. *
  2375. * The termios state of a pty is reset on first open so that
  2376. * settings don't persist across reuse.
  2377. *
  2378. * Locking: tty_mutex protects tty, get_tty_driver and init_dev work.
  2379. * tty->count should protect the rest.
  2380. * ->siglock protects ->signal/->sighand
  2381. */
  2382. static int __tty_open(struct inode *inode, struct file *filp)
  2383. {
  2384. struct tty_struct *tty;
  2385. int noctty, retval;
  2386. struct tty_driver *driver;
  2387. int index;
  2388. dev_t device = inode->i_rdev;
  2389. unsigned short saved_flags = filp->f_flags;
  2390. nonseekable_open(inode, filp);
  2391. retry_open:
  2392. noctty = filp->f_flags & O_NOCTTY;
  2393. index = -1;
  2394. retval = 0;
  2395. mutex_lock(&tty_mutex);
  2396. if (device == MKDEV(TTYAUX_MAJOR, 0)) {
  2397. tty = get_current_tty();
  2398. if (!tty) {
  2399. mutex_unlock(&tty_mutex);
  2400. return -ENXIO;
  2401. }
  2402. driver = tty->driver;
  2403. index = tty->index;
  2404. filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
  2405. /* noctty = 1; */
  2406. goto got_driver;
  2407. }
  2408. #ifdef CONFIG_VT
  2409. if (device == MKDEV(TTY_MAJOR, 0)) {
  2410. extern struct tty_driver *console_driver;
  2411. driver = console_driver;
  2412. index = fg_console;
  2413. noctty = 1;
  2414. goto got_driver;
  2415. }
  2416. #endif
  2417. if (device == MKDEV(TTYAUX_MAJOR, 1)) {
  2418. driver = console_device(&index);
  2419. if (driver) {
  2420. /* Don't let /dev/console block */
  2421. filp->f_flags |= O_NONBLOCK;
  2422. noctty = 1;
  2423. goto got_driver;
  2424. }
  2425. mutex_unlock(&tty_mutex);
  2426. return -ENODEV;
  2427. }
  2428. driver = get_tty_driver(device, &index);
  2429. if (!driver) {
  2430. mutex_unlock(&tty_mutex);
  2431. return -ENODEV;
  2432. }
  2433. got_driver:
  2434. retval = init_dev(driver, index, &tty);
  2435. mutex_unlock(&tty_mutex);
  2436. if (retval)
  2437. return retval;
  2438. filp->private_data = tty;
  2439. file_move(filp, &tty->tty_files);
  2440. check_tty_count(tty, "tty_open");
  2441. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  2442. tty->driver->subtype == PTY_TYPE_MASTER)
  2443. noctty = 1;
  2444. #ifdef TTY_DEBUG_HANGUP
  2445. printk(KERN_DEBUG "opening %s...", tty->name);
  2446. #endif
  2447. if (!retval) {
  2448. if (tty->ops->open)
  2449. retval = tty->ops->open(tty, filp);
  2450. else
  2451. retval = -ENODEV;
  2452. }
  2453. filp->f_flags = saved_flags;
  2454. if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
  2455. !capable(CAP_SYS_ADMIN))
  2456. retval = -EBUSY;
  2457. if (retval) {
  2458. #ifdef TTY_DEBUG_HANGUP
  2459. printk(KERN_DEBUG "error %d in opening %s...", retval,
  2460. tty->name);
  2461. #endif
  2462. release_dev(filp);
  2463. if (retval != -ERESTARTSYS)
  2464. return retval;
  2465. if (signal_pending(current))
  2466. return retval;
  2467. schedule();
  2468. /*
  2469. * Need to reset f_op in case a hangup happened.
  2470. */
  2471. if (filp->f_op == &hung_up_tty_fops)
  2472. filp->f_op = &tty_fops;
  2473. goto retry_open;
  2474. }
  2475. mutex_lock(&tty_mutex);
  2476. spin_lock_irq(&current->sighand->siglock);
  2477. if (!noctty &&
  2478. current->signal->leader &&
  2479. !current->signal->tty &&
  2480. tty->session == NULL)
  2481. __proc_set_tty(current, tty);
  2482. spin_unlock_irq(&current->sighand->siglock);
  2483. mutex_unlock(&tty_mutex);
  2484. return 0;
  2485. }
  2486. /* BKL pushdown: scary code avoidance wrapper */
  2487. static int tty_open(struct inode *inode, struct file *filp)
  2488. {
  2489. int ret;
  2490. lock_kernel();
  2491. ret = __tty_open(inode, filp);
  2492. unlock_kernel();
  2493. return ret;
  2494. }
  2495. #ifdef CONFIG_UNIX98_PTYS
  2496. /**
  2497. * ptmx_open - open a unix 98 pty master
  2498. * @inode: inode of device file
  2499. * @filp: file pointer to tty
  2500. *
  2501. * Allocate a unix98 pty master device from the ptmx driver.
  2502. *
  2503. * Locking: tty_mutex protects theinit_dev work. tty->count should
  2504. * protect the rest.
  2505. * allocated_ptys_lock handles the list of free pty numbers
  2506. */
  2507. static int __ptmx_open(struct inode *inode, struct file *filp)
  2508. {
  2509. struct tty_struct *tty;
  2510. int retval;
  2511. int index;
  2512. nonseekable_open(inode, filp);
  2513. /* find a device that is not in use. */
  2514. index = devpts_new_index();
  2515. if (index < 0)
  2516. return index;
  2517. mutex_lock(&tty_mutex);
  2518. retval = init_dev(ptm_driver, index, &tty);
  2519. mutex_unlock(&tty_mutex);
  2520. if (retval)
  2521. goto out;
  2522. set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
  2523. filp->private_data = tty;
  2524. file_move(filp, &tty->tty_files);
  2525. retval = devpts_pty_new(tty->link);
  2526. if (retval)
  2527. goto out1;
  2528. check_tty_count(tty, "ptmx_open");
  2529. retval = ptm_driver->ops->open(tty, filp);
  2530. if (!retval)
  2531. return 0;
  2532. out1:
  2533. release_dev(filp);
  2534. return retval;
  2535. out:
  2536. devpts_kill_index(index);
  2537. return retval;
  2538. }
  2539. static int ptmx_open(struct inode *inode, struct file *filp)
  2540. {
  2541. int ret;
  2542. lock_kernel();
  2543. ret = __ptmx_open(inode, filp);
  2544. unlock_kernel();
  2545. return ret;
  2546. }
  2547. #endif
  2548. /**
  2549. * tty_release - vfs callback for close
  2550. * @inode: inode of tty
  2551. * @filp: file pointer for handle to tty
  2552. *
  2553. * Called the last time each file handle is closed that references
  2554. * this tty. There may however be several such references.
  2555. *
  2556. * Locking:
  2557. * Takes bkl. See release_dev
  2558. */
  2559. static int tty_release(struct inode *inode, struct file *filp)
  2560. {
  2561. lock_kernel();
  2562. release_dev(filp);
  2563. unlock_kernel();
  2564. return 0;
  2565. }
  2566. /**
  2567. * tty_poll - check tty status
  2568. * @filp: file being polled
  2569. * @wait: poll wait structures to update
  2570. *
  2571. * Call the line discipline polling method to obtain the poll
  2572. * status of the device.
  2573. *
  2574. * Locking: locks called line discipline but ldisc poll method
  2575. * may be re-entered freely by other callers.
  2576. */
  2577. static unsigned int tty_poll(struct file *filp, poll_table *wait)
  2578. {
  2579. struct tty_struct *tty;
  2580. struct tty_ldisc *ld;
  2581. int ret = 0;
  2582. tty = (struct tty_struct *)filp->private_data;
  2583. if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
  2584. return 0;
  2585. ld = tty_ldisc_ref_wait(tty);
  2586. if (ld->poll)
  2587. ret = (ld->poll)(tty, filp, wait);
  2588. tty_ldisc_deref(ld);
  2589. return ret;
  2590. }
  2591. static int tty_fasync(int fd, struct file *filp, int on)
  2592. {
  2593. struct tty_struct *tty;
  2594. unsigned long flags;
  2595. int retval = 0;
  2596. lock_kernel();
  2597. tty = (struct tty_struct *)filp->private_data;
  2598. if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
  2599. goto out;
  2600. retval = fasync_helper(fd, filp, on, &tty->fasync);
  2601. if (retval <= 0)
  2602. goto out;
  2603. if (on) {
  2604. enum pid_type type;
  2605. struct pid *pid;
  2606. if (!waitqueue_active(&tty->read_wait))
  2607. tty->minimum_to_wake = 1;
  2608. spin_lock_irqsave(&tty->ctrl_lock, flags);
  2609. if (tty->pgrp) {
  2610. pid = tty->pgrp;
  2611. type = PIDTYPE_PGID;
  2612. } else {
  2613. pid = task_pid(current);
  2614. type = PIDTYPE_PID;
  2615. }
  2616. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  2617. retval = __f_setown(filp, pid, type, 0);
  2618. if (retval)
  2619. goto out;
  2620. } else {
  2621. if (!tty->fasync && !waitqueue_active(&tty->read_wait))
  2622. tty->minimum_to_wake = N_TTY_BUF_SIZE;
  2623. }
  2624. retval = 0;
  2625. out:
  2626. unlock_kernel();
  2627. return retval;
  2628. }
  2629. /**
  2630. * tiocsti - fake input character
  2631. * @tty: tty to fake input into
  2632. * @p: pointer to character
  2633. *
  2634. * Fake input to a tty device. Does the necessary locking and
  2635. * input management.
  2636. *
  2637. * FIXME: does not honour flow control ??
  2638. *
  2639. * Locking:
  2640. * Called functions take tty_ldisc_lock
  2641. * current->signal->tty check is safe without locks
  2642. *
  2643. * FIXME: may race normal receive processing
  2644. */
  2645. static int tiocsti(struct tty_struct *tty, char __user *p)
  2646. {
  2647. char ch, mbz = 0;
  2648. struct tty_ldisc *ld;
  2649. if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
  2650. return -EPERM;
  2651. if (get_user(ch, p))
  2652. return -EFAULT;
  2653. ld = tty_ldisc_ref_wait(tty);
  2654. ld->receive_buf(tty, &ch, &mbz, 1);
  2655. tty_ldisc_deref(ld);
  2656. return 0;
  2657. }
  2658. /**
  2659. * tiocgwinsz - implement window query ioctl
  2660. * @tty; tty
  2661. * @arg: user buffer for result
  2662. *
  2663. * Copies the kernel idea of the window size into the user buffer.
  2664. *
  2665. * Locking: tty->termios_mutex is taken to ensure the winsize data
  2666. * is consistent.
  2667. */
  2668. static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
  2669. {
  2670. int err;
  2671. mutex_lock(&tty->termios_mutex);
  2672. err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
  2673. mutex_unlock(&tty->termios_mutex);
  2674. return err ? -EFAULT: 0;
  2675. }
  2676. /**
  2677. * tiocswinsz - implement window size set ioctl
  2678. * @tty; tty
  2679. * @arg: user buffer for result
  2680. *
  2681. * Copies the user idea of the window size to the kernel. Traditionally
  2682. * this is just advisory information but for the Linux console it
  2683. * actually has driver level meaning and triggers a VC resize.
  2684. *
  2685. * Locking:
  2686. * Called function use the console_sem is used to ensure we do
  2687. * not try and resize the console twice at once.
  2688. * The tty->termios_mutex is used to ensure we don't double
  2689. * resize and get confused. Lock order - tty->termios_mutex before
  2690. * console sem
  2691. */
  2692. static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
  2693. struct winsize __user *arg)
  2694. {
  2695. struct winsize tmp_ws;
  2696. struct pid *pgrp, *rpgrp;
  2697. unsigned long flags;
  2698. if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
  2699. return -EFAULT;
  2700. mutex_lock(&tty->termios_mutex);
  2701. if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
  2702. goto done;
  2703. #ifdef CONFIG_VT
  2704. if (tty->driver->type == TTY_DRIVER_TYPE_CONSOLE) {
  2705. if (vc_lock_resize(tty->driver_data, tmp_ws.ws_col,
  2706. tmp_ws.ws_row)) {
  2707. mutex_unlock(&tty->termios_mutex);
  2708. return -ENXIO;
  2709. }
  2710. }
  2711. #endif
  2712. /* Get the PID values and reference them so we can
  2713. avoid holding the tty ctrl lock while sending signals */
  2714. spin_lock_irqsave(&tty->ctrl_lock, flags);
  2715. pgrp = get_pid(tty->pgrp);
  2716. rpgrp = get_pid(real_tty->pgrp);
  2717. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  2718. if (pgrp)
  2719. kill_pgrp(pgrp, SIGWINCH, 1);
  2720. if (rpgrp != pgrp && rpgrp)
  2721. kill_pgrp(rpgrp, SIGWINCH, 1);
  2722. put_pid(pgrp);
  2723. put_pid(rpgrp);
  2724. tty->winsize = tmp_ws;
  2725. real_tty->winsize = tmp_ws;
  2726. done:
  2727. mutex_unlock(&tty->termios_mutex);
  2728. return 0;
  2729. }
  2730. /**
  2731. * tioccons - allow admin to move logical console
  2732. * @file: the file to become console
  2733. *
  2734. * Allow the adminstrator to move the redirected console device
  2735. *
  2736. * Locking: uses redirect_lock to guard the redirect information
  2737. */
  2738. static int tioccons(struct file *file)
  2739. {
  2740. if (!capable(CAP_SYS_ADMIN))
  2741. return -EPERM;
  2742. if (file->f_op->write == redirected_tty_write) {
  2743. struct file *f;
  2744. spin_lock(&redirect_lock);
  2745. f = redirect;
  2746. redirect = NULL;
  2747. spin_unlock(&redirect_lock);
  2748. if (f)
  2749. fput(f);
  2750. return 0;
  2751. }
  2752. spin_lock(&redirect_lock);
  2753. if (redirect) {
  2754. spin_unlock(&redirect_lock);
  2755. return -EBUSY;
  2756. }
  2757. get_file(file);
  2758. redirect = file;
  2759. spin_unlock(&redirect_lock);
  2760. return 0;
  2761. }
  2762. /**
  2763. * fionbio - non blocking ioctl
  2764. * @file: file to set blocking value
  2765. * @p: user parameter
  2766. *
  2767. * Historical tty interfaces had a blocking control ioctl before
  2768. * the generic functionality existed. This piece of history is preserved
  2769. * in the expected tty API of posix OS's.
  2770. *
  2771. * Locking: none, the open fle handle ensures it won't go away.
  2772. */
  2773. static int fionbio(struct file *file, int __user *p)
  2774. {
  2775. int nonblock;
  2776. if (get_user(nonblock, p))
  2777. return -EFAULT;
  2778. /* file->f_flags is still BKL protected in the fs layer - vomit */
  2779. lock_kernel();
  2780. if (nonblock)
  2781. file->f_flags |= O_NONBLOCK;
  2782. else
  2783. file->f_flags &= ~O_NONBLOCK;
  2784. unlock_kernel();
  2785. return 0;
  2786. }
  2787. /**
  2788. * tiocsctty - set controlling tty
  2789. * @tty: tty structure
  2790. * @arg: user argument
  2791. *
  2792. * This ioctl is used to manage job control. It permits a session
  2793. * leader to set this tty as the controlling tty for the session.
  2794. *
  2795. * Locking:
  2796. * Takes tty_mutex() to protect tty instance
  2797. * Takes tasklist_lock internally to walk sessions
  2798. * Takes ->siglock() when updating signal->tty
  2799. */
  2800. static int tiocsctty(struct tty_struct *tty, int arg)
  2801. {
  2802. int ret = 0;
  2803. if (current->signal->leader && (task_session(current) == tty->session))
  2804. return ret;
  2805. mutex_lock(&tty_mutex);
  2806. /*
  2807. * The process must be a session leader and
  2808. * not have a controlling tty already.
  2809. */
  2810. if (!current->signal->leader || current->signal->tty) {
  2811. ret = -EPERM;
  2812. goto unlock;
  2813. }
  2814. if (tty->session) {
  2815. /*
  2816. * This tty is already the controlling
  2817. * tty for another session group!
  2818. */
  2819. if (arg == 1 && capable(CAP_SYS_ADMIN)) {
  2820. /*
  2821. * Steal it away
  2822. */
  2823. read_lock(&tasklist_lock);
  2824. session_clear_tty(tty->session);
  2825. read_unlock(&tasklist_lock);
  2826. } else {
  2827. ret = -EPERM;
  2828. goto unlock;
  2829. }
  2830. }
  2831. proc_set_tty(current, tty);
  2832. unlock:
  2833. mutex_unlock(&tty_mutex);
  2834. return ret;
  2835. }
  2836. /**
  2837. * tty_get_pgrp - return a ref counted pgrp pid
  2838. * @tty: tty to read
  2839. *
  2840. * Returns a refcounted instance of the pid struct for the process
  2841. * group controlling the tty.
  2842. */
  2843. struct pid *tty_get_pgrp(struct tty_struct *tty)
  2844. {
  2845. unsigned long flags;
  2846. struct pid *pgrp;
  2847. spin_lock_irqsave(&tty->ctrl_lock, flags);
  2848. pgrp = get_pid(tty->pgrp);
  2849. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  2850. return pgrp;
  2851. }
  2852. EXPORT_SYMBOL_GPL(tty_get_pgrp);
  2853. /**
  2854. * tiocgpgrp - get process group
  2855. * @tty: tty passed by user
  2856. * @real_tty: tty side of the tty pased by the user if a pty else the tty
  2857. * @p: returned pid
  2858. *
  2859. * Obtain the process group of the tty. If there is no process group
  2860. * return an error.
  2861. *
  2862. * Locking: none. Reference to current->signal->tty is safe.
  2863. */
  2864. static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  2865. {
  2866. struct pid *pid;
  2867. int ret;
  2868. /*
  2869. * (tty == real_tty) is a cheap way of
  2870. * testing if the tty is NOT a master pty.
  2871. */
  2872. if (tty == real_tty && current->signal->tty != real_tty)
  2873. return -ENOTTY;
  2874. pid = tty_get_pgrp(real_tty);
  2875. ret = put_user(pid_vnr(pid), p);
  2876. put_pid(pid);
  2877. return ret;
  2878. }
  2879. /**
  2880. * tiocspgrp - attempt to set process group
  2881. * @tty: tty passed by user
  2882. * @real_tty: tty side device matching tty passed by user
  2883. * @p: pid pointer
  2884. *
  2885. * Set the process group of the tty to the session passed. Only
  2886. * permitted where the tty session is our session.
  2887. *
  2888. * Locking: RCU, ctrl lock
  2889. */
  2890. static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  2891. {
  2892. struct pid *pgrp;
  2893. pid_t pgrp_nr;
  2894. int retval = tty_check_change(real_tty);
  2895. unsigned long flags;
  2896. if (retval == -EIO)
  2897. return -ENOTTY;
  2898. if (retval)
  2899. return retval;
  2900. if (!current->signal->tty ||
  2901. (current->signal->tty != real_tty) ||
  2902. (real_tty->session != task_session(current)))
  2903. return -ENOTTY;
  2904. if (get_user(pgrp_nr, p))
  2905. return -EFAULT;
  2906. if (pgrp_nr < 0)
  2907. return -EINVAL;
  2908. rcu_read_lock();
  2909. pgrp = find_vpid(pgrp_nr);
  2910. retval = -ESRCH;
  2911. if (!pgrp)
  2912. goto out_unlock;
  2913. retval = -EPERM;
  2914. if (session_of_pgrp(pgrp) != task_session(current))
  2915. goto out_unlock;
  2916. retval = 0;
  2917. spin_lock_irqsave(&tty->ctrl_lock, flags);
  2918. put_pid(real_tty->pgrp);
  2919. real_tty->pgrp = get_pid(pgrp);
  2920. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  2921. out_unlock:
  2922. rcu_read_unlock();
  2923. return retval;
  2924. }
  2925. /**
  2926. * tiocgsid - get session id
  2927. * @tty: tty passed by user
  2928. * @real_tty: tty side of the tty pased by the user if a pty else the tty
  2929. * @p: pointer to returned session id
  2930. *
  2931. * Obtain the session id of the tty. If there is no session
  2932. * return an error.
  2933. *
  2934. * Locking: none. Reference to current->signal->tty is safe.
  2935. */
  2936. static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  2937. {
  2938. /*
  2939. * (tty == real_tty) is a cheap way of
  2940. * testing if the tty is NOT a master pty.
  2941. */
  2942. if (tty == real_tty && current->signal->tty != real_tty)
  2943. return -ENOTTY;
  2944. if (!real_tty->session)
  2945. return -ENOTTY;
  2946. return put_user(pid_vnr(real_tty->session), p);
  2947. }
  2948. /**
  2949. * tiocsetd - set line discipline
  2950. * @tty: tty device
  2951. * @p: pointer to user data
  2952. *
  2953. * Set the line discipline according to user request.
  2954. *
  2955. * Locking: see tty_set_ldisc, this function is just a helper
  2956. */
  2957. static int tiocsetd(struct tty_struct *tty, int __user *p)
  2958. {
  2959. int ldisc;
  2960. int ret;
  2961. if (get_user(ldisc, p))
  2962. return -EFAULT;
  2963. lock_kernel();
  2964. ret = tty_set_ldisc(tty, ldisc);
  2965. unlock_kernel();
  2966. return ret;
  2967. }
  2968. /**
  2969. * send_break - performed time break
  2970. * @tty: device to break on
  2971. * @duration: timeout in mS
  2972. *
  2973. * Perform a timed break on hardware that lacks its own driver level
  2974. * timed break functionality.
  2975. *
  2976. * Locking:
  2977. * atomic_write_lock serializes
  2978. *
  2979. */
  2980. static int send_break(struct tty_struct *tty, unsigned int duration)
  2981. {
  2982. if (tty_write_lock(tty, 0) < 0)
  2983. return -EINTR;
  2984. tty->ops->break_ctl(tty, -1);
  2985. if (!signal_pending(current))
  2986. msleep_interruptible(duration);
  2987. tty->ops->break_ctl(tty, 0);
  2988. tty_write_unlock(tty);
  2989. if (signal_pending(current))
  2990. return -EINTR;
  2991. return 0;
  2992. }
  2993. /**
  2994. * tty_tiocmget - get modem status
  2995. * @tty: tty device
  2996. * @file: user file pointer
  2997. * @p: pointer to result
  2998. *
  2999. * Obtain the modem status bits from the tty driver if the feature
  3000. * is supported. Return -EINVAL if it is not available.
  3001. *
  3002. * Locking: none (up to the driver)
  3003. */
  3004. static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
  3005. {
  3006. int retval = -EINVAL;
  3007. if (tty->ops->tiocmget) {
  3008. retval = tty->ops->tiocmget(tty, file);
  3009. if (retval >= 0)
  3010. retval = put_user(retval, p);
  3011. }
  3012. return retval;
  3013. }
  3014. /**
  3015. * tty_tiocmset - set modem status
  3016. * @tty: tty device
  3017. * @file: user file pointer
  3018. * @cmd: command - clear bits, set bits or set all
  3019. * @p: pointer to desired bits
  3020. *
  3021. * Set the modem status bits from the tty driver if the feature
  3022. * is supported. Return -EINVAL if it is not available.
  3023. *
  3024. * Locking: none (up to the driver)
  3025. */
  3026. static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
  3027. unsigned __user *p)
  3028. {
  3029. int retval = -EINVAL;
  3030. if (tty->ops->tiocmset) {
  3031. unsigned int set, clear, val;
  3032. retval = get_user(val, p);
  3033. if (retval)
  3034. return retval;
  3035. set = clear = 0;
  3036. switch (cmd) {
  3037. case TIOCMBIS:
  3038. set = val;
  3039. break;
  3040. case TIOCMBIC:
  3041. clear = val;
  3042. break;
  3043. case TIOCMSET:
  3044. set = val;
  3045. clear = ~val;
  3046. break;
  3047. }
  3048. set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
  3049. clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
  3050. retval = tty->ops->tiocmset(tty, file, set, clear);
  3051. }
  3052. return retval;
  3053. }
  3054. /*
  3055. * Split this up, as gcc can choke on it otherwise..
  3056. */
  3057. long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  3058. {
  3059. struct tty_struct *tty, *real_tty;
  3060. void __user *p = (void __user *)arg;
  3061. int retval;
  3062. struct tty_ldisc *ld;
  3063. struct inode *inode = file->f_dentry->d_inode;
  3064. tty = (struct tty_struct *)file->private_data;
  3065. if (tty_paranoia_check(tty, inode, "tty_ioctl"))
  3066. return -EINVAL;
  3067. real_tty = tty;
  3068. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  3069. tty->driver->subtype == PTY_TYPE_MASTER)
  3070. real_tty = tty->link;
  3071. /*
  3072. * Break handling by driver
  3073. */
  3074. retval = -EINVAL;
  3075. if (!tty->ops->break_ctl) {
  3076. switch (cmd) {
  3077. case TIOCSBRK:
  3078. case TIOCCBRK:
  3079. if (tty->ops->ioctl)
  3080. retval = tty->ops->ioctl(tty, file, cmd, arg);
  3081. if (retval != -EINVAL && retval != -ENOIOCTLCMD)
  3082. printk(KERN_WARNING "tty: driver %s needs updating to use break_ctl\n", tty->driver->name);
  3083. return retval;
  3084. /* These two ioctl's always return success; even if */
  3085. /* the driver doesn't support them. */
  3086. case TCSBRK:
  3087. case TCSBRKP:
  3088. if (!tty->ops->ioctl)
  3089. return 0;
  3090. retval = tty->ops->ioctl(tty, file, cmd, arg);
  3091. if (retval != -EINVAL && retval != -ENOIOCTLCMD)
  3092. printk(KERN_WARNING "tty: driver %s needs updating to use break_ctl\n", tty->driver->name);
  3093. if (retval == -ENOIOCTLCMD)
  3094. retval = 0;
  3095. return retval;
  3096. }
  3097. }
  3098. /*
  3099. * Factor out some common prep work
  3100. */
  3101. switch (cmd) {
  3102. case TIOCSETD:
  3103. case TIOCSBRK:
  3104. case TIOCCBRK:
  3105. case TCSBRK:
  3106. case TCSBRKP:
  3107. retval = tty_check_change(tty);
  3108. if (retval)
  3109. return retval;
  3110. if (cmd != TIOCCBRK) {
  3111. tty_wait_until_sent(tty, 0);
  3112. if (signal_pending(current))
  3113. return -EINTR;
  3114. }
  3115. break;
  3116. }
  3117. switch (cmd) {
  3118. case TIOCSTI:
  3119. return tiocsti(tty, p);
  3120. case TIOCGWINSZ:
  3121. return tiocgwinsz(tty, p);
  3122. case TIOCSWINSZ:
  3123. return tiocswinsz(tty, real_tty, p);
  3124. case TIOCCONS:
  3125. return real_tty != tty ? -EINVAL : tioccons(file);
  3126. case FIONBIO:
  3127. return fionbio(file, p);
  3128. case TIOCEXCL:
  3129. set_bit(TTY_EXCLUSIVE, &tty->flags);
  3130. return 0;
  3131. case TIOCNXCL:
  3132. clear_bit(TTY_EXCLUSIVE, &tty->flags);
  3133. return 0;
  3134. case TIOCNOTTY:
  3135. if (current->signal->tty != tty)
  3136. return -ENOTTY;
  3137. no_tty();
  3138. return 0;
  3139. case TIOCSCTTY:
  3140. return tiocsctty(tty, arg);
  3141. case TIOCGPGRP:
  3142. return tiocgpgrp(tty, real_tty, p);
  3143. case TIOCSPGRP:
  3144. return tiocspgrp(tty, real_tty, p);
  3145. case TIOCGSID:
  3146. return tiocgsid(tty, real_tty, p);
  3147. case TIOCGETD:
  3148. return put_user(tty->ldisc.num, (int __user *)p);
  3149. case TIOCSETD:
  3150. return tiocsetd(tty, p);
  3151. #ifdef CONFIG_VT
  3152. case TIOCLINUX:
  3153. return tioclinux(tty, arg);
  3154. #endif
  3155. /*
  3156. * Break handling
  3157. */
  3158. case TIOCSBRK: /* Turn break on, unconditionally */
  3159. if (tty->ops->break_ctl)
  3160. tty->ops->break_ctl(tty, -1);
  3161. return 0;
  3162. case TIOCCBRK: /* Turn break off, unconditionally */
  3163. if (tty->ops->break_ctl)
  3164. tty->ops->break_ctl(tty, 0);
  3165. return 0;
  3166. case TCSBRK: /* SVID version: non-zero arg --> no break */
  3167. /* non-zero arg means wait for all output data
  3168. * to be sent (performed above) but don't send break.
  3169. * This is used by the tcdrain() termios function.
  3170. */
  3171. if (!arg)
  3172. return send_break(tty, 250);
  3173. return 0;
  3174. case TCSBRKP: /* support for POSIX tcsendbreak() */
  3175. return send_break(tty, arg ? arg*100 : 250);
  3176. case TIOCMGET:
  3177. return tty_tiocmget(tty, file, p);
  3178. case TIOCMSET:
  3179. case TIOCMBIC:
  3180. case TIOCMBIS:
  3181. return tty_tiocmset(tty, file, cmd, p);
  3182. case TCFLSH:
  3183. switch (arg) {
  3184. case TCIFLUSH:
  3185. case TCIOFLUSH:
  3186. /* flush tty buffer and allow ldisc to process ioctl */
  3187. tty_buffer_flush(tty);
  3188. break;
  3189. }
  3190. break;
  3191. }
  3192. if (tty->ops->ioctl) {
  3193. retval = (tty->ops->ioctl)(tty, file, cmd, arg);
  3194. if (retval != -ENOIOCTLCMD)
  3195. return retval;
  3196. }
  3197. ld = tty_ldisc_ref_wait(tty);
  3198. retval = -EINVAL;
  3199. if (ld->ioctl) {
  3200. retval = ld->ioctl(tty, file, cmd, arg);
  3201. if (retval == -ENOIOCTLCMD)
  3202. retval = -EINVAL;
  3203. }
  3204. tty_ldisc_deref(ld);
  3205. return retval;
  3206. }
  3207. #ifdef CONFIG_COMPAT
  3208. static long tty_compat_ioctl(struct file *file, unsigned int cmd,
  3209. unsigned long arg)
  3210. {
  3211. struct inode *inode = file->f_dentry->d_inode;
  3212. struct tty_struct *tty = file->private_data;
  3213. struct tty_ldisc *ld;
  3214. int retval = -ENOIOCTLCMD;
  3215. if (tty_paranoia_check(tty, inode, "tty_ioctl"))
  3216. return -EINVAL;
  3217. if (tty->ops->compat_ioctl) {
  3218. retval = (tty->ops->compat_ioctl)(tty, file, cmd, arg);
  3219. if (retval != -ENOIOCTLCMD)
  3220. return retval;
  3221. }
  3222. ld = tty_ldisc_ref_wait(tty);
  3223. if (ld->compat_ioctl)
  3224. retval = ld->compat_ioctl(tty, file, cmd, arg);
  3225. tty_ldisc_deref(ld);
  3226. return retval;
  3227. }
  3228. #endif
  3229. /*
  3230. * This implements the "Secure Attention Key" --- the idea is to
  3231. * prevent trojan horses by killing all processes associated with this
  3232. * tty when the user hits the "Secure Attention Key". Required for
  3233. * super-paranoid applications --- see the Orange Book for more details.
  3234. *
  3235. * This code could be nicer; ideally it should send a HUP, wait a few
  3236. * seconds, then send a INT, and then a KILL signal. But you then
  3237. * have to coordinate with the init process, since all processes associated
  3238. * with the current tty must be dead before the new getty is allowed
  3239. * to spawn.
  3240. *
  3241. * Now, if it would be correct ;-/ The current code has a nasty hole -
  3242. * it doesn't catch files in flight. We may send the descriptor to ourselves
  3243. * via AF_UNIX socket, close it and later fetch from socket. FIXME.
  3244. *
  3245. * Nasty bug: do_SAK is being called in interrupt context. This can
  3246. * deadlock. We punt it up to process context. AKPM - 16Mar2001
  3247. */
  3248. void __do_SAK(struct tty_struct *tty)
  3249. {
  3250. #ifdef TTY_SOFT_SAK
  3251. tty_hangup(tty);
  3252. #else
  3253. struct task_struct *g, *p;
  3254. struct pid *session;
  3255. int i;
  3256. struct file *filp;
  3257. struct fdtable *fdt;
  3258. if (!tty)
  3259. return;
  3260. session = tty->session;
  3261. tty_ldisc_flush(tty);
  3262. tty_driver_flush_buffer(tty);
  3263. read_lock(&tasklist_lock);
  3264. /* Kill the entire session */
  3265. do_each_pid_task(session, PIDTYPE_SID, p) {
  3266. printk(KERN_NOTICE "SAK: killed process %d"
  3267. " (%s): task_session_nr(p)==tty->session\n",
  3268. task_pid_nr(p), p->comm);
  3269. send_sig(SIGKILL, p, 1);
  3270. } while_each_pid_task(session, PIDTYPE_SID, p);
  3271. /* Now kill any processes that happen to have the
  3272. * tty open.
  3273. */
  3274. do_each_thread(g, p) {
  3275. if (p->signal->tty == tty) {
  3276. printk(KERN_NOTICE "SAK: killed process %d"
  3277. " (%s): task_session_nr(p)==tty->session\n",
  3278. task_pid_nr(p), p->comm);
  3279. send_sig(SIGKILL, p, 1);
  3280. continue;
  3281. }
  3282. task_lock(p);
  3283. if (p->files) {
  3284. /*
  3285. * We don't take a ref to the file, so we must
  3286. * hold ->file_lock instead.
  3287. */
  3288. spin_lock(&p->files->file_lock);
  3289. fdt = files_fdtable(p->files);
  3290. for (i = 0; i < fdt->max_fds; i++) {
  3291. filp = fcheck_files(p->files, i);
  3292. if (!filp)
  3293. continue;
  3294. if (filp->f_op->read == tty_read &&
  3295. filp->private_data == tty) {
  3296. printk(KERN_NOTICE "SAK: killed process %d"
  3297. " (%s): fd#%d opened to the tty\n",
  3298. task_pid_nr(p), p->comm, i);
  3299. force_sig(SIGKILL, p);
  3300. break;
  3301. }
  3302. }
  3303. spin_unlock(&p->files->file_lock);
  3304. }
  3305. task_unlock(p);
  3306. } while_each_thread(g, p);
  3307. read_unlock(&tasklist_lock);
  3308. #endif
  3309. }
  3310. static void do_SAK_work(struct work_struct *work)
  3311. {
  3312. struct tty_struct *tty =
  3313. container_of(work, struct tty_struct, SAK_work);
  3314. __do_SAK(tty);
  3315. }
  3316. /*
  3317. * The tq handling here is a little racy - tty->SAK_work may already be queued.
  3318. * Fortunately we don't need to worry, because if ->SAK_work is already queued,
  3319. * the values which we write to it will be identical to the values which it
  3320. * already has. --akpm
  3321. */
  3322. void do_SAK(struct tty_struct *tty)
  3323. {
  3324. if (!tty)
  3325. return;
  3326. schedule_work(&tty->SAK_work);
  3327. }
  3328. EXPORT_SYMBOL(do_SAK);
  3329. /**
  3330. * flush_to_ldisc
  3331. * @work: tty structure passed from work queue.
  3332. *
  3333. * This routine is called out of the software interrupt to flush data
  3334. * from the buffer chain to the line discipline.
  3335. *
  3336. * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
  3337. * while invoking the line discipline receive_buf method. The
  3338. * receive_buf method is single threaded for each tty instance.
  3339. */
  3340. static void flush_to_ldisc(struct work_struct *work)
  3341. {
  3342. struct tty_struct *tty =
  3343. container_of(work, struct tty_struct, buf.work.work);
  3344. unsigned long flags;
  3345. struct tty_ldisc *disc;
  3346. struct tty_buffer *tbuf, *head;
  3347. char *char_buf;
  3348. unsigned char *flag_buf;
  3349. disc = tty_ldisc_ref(tty);
  3350. if (disc == NULL) /* !TTY_LDISC */
  3351. return;
  3352. spin_lock_irqsave(&tty->buf.lock, flags);
  3353. /* So we know a flush is running */
  3354. set_bit(TTY_FLUSHING, &tty->flags);
  3355. head = tty->buf.head;
  3356. if (head != NULL) {
  3357. tty->buf.head = NULL;
  3358. for (;;) {
  3359. int count = head->commit - head->read;
  3360. if (!count) {
  3361. if (head->next == NULL)
  3362. break;
  3363. tbuf = head;
  3364. head = head->next;
  3365. tty_buffer_free(tty, tbuf);
  3366. continue;
  3367. }
  3368. /* Ldisc or user is trying to flush the buffers
  3369. we are feeding to the ldisc, stop feeding the
  3370. line discipline as we want to empty the queue */
  3371. if (test_bit(TTY_FLUSHPENDING, &tty->flags))
  3372. break;
  3373. if (!tty->receive_room) {
  3374. schedule_delayed_work(&tty->buf.work, 1);
  3375. break;
  3376. }
  3377. if (count > tty->receive_room)
  3378. count = tty->receive_room;
  3379. char_buf = head->char_buf_ptr + head->read;
  3380. flag_buf = head->flag_buf_ptr + head->read;
  3381. head->read += count;
  3382. spin_unlock_irqrestore(&tty->buf.lock, flags);
  3383. disc->receive_buf(tty, char_buf, flag_buf, count);
  3384. spin_lock_irqsave(&tty->buf.lock, flags);
  3385. }
  3386. /* Restore the queue head */
  3387. tty->buf.head = head;
  3388. }
  3389. /* We may have a deferred request to flush the input buffer,
  3390. if so pull the chain under the lock and empty the queue */
  3391. if (test_bit(TTY_FLUSHPENDING, &tty->flags)) {
  3392. __tty_buffer_flush(tty);
  3393. clear_bit(TTY_FLUSHPENDING, &tty->flags);
  3394. wake_up(&tty->read_wait);
  3395. }
  3396. clear_bit(TTY_FLUSHING, &tty->flags);
  3397. spin_unlock_irqrestore(&tty->buf.lock, flags);
  3398. tty_ldisc_deref(disc);
  3399. }
  3400. /**
  3401. * tty_flip_buffer_push - terminal
  3402. * @tty: tty to push
  3403. *
  3404. * Queue a push of the terminal flip buffers to the line discipline. This
  3405. * function must not be called from IRQ context if tty->low_latency is set.
  3406. *
  3407. * In the event of the queue being busy for flipping the work will be
  3408. * held off and retried later.
  3409. *
  3410. * Locking: tty buffer lock. Driver locks in low latency mode.
  3411. */
  3412. void tty_flip_buffer_push(struct tty_struct *tty)
  3413. {
  3414. unsigned long flags;
  3415. spin_lock_irqsave(&tty->buf.lock, flags);
  3416. if (tty->buf.tail != NULL)
  3417. tty->buf.tail->commit = tty->buf.tail->used;
  3418. spin_unlock_irqrestore(&tty->buf.lock, flags);
  3419. if (tty->low_latency)
  3420. flush_to_ldisc(&tty->buf.work.work);
  3421. else
  3422. schedule_delayed_work(&tty->buf.work, 1);
  3423. }
  3424. EXPORT_SYMBOL(tty_flip_buffer_push);
  3425. /**
  3426. * initialize_tty_struct
  3427. * @tty: tty to initialize
  3428. *
  3429. * This subroutine initializes a tty structure that has been newly
  3430. * allocated.
  3431. *
  3432. * Locking: none - tty in question must not be exposed at this point
  3433. */
  3434. static void initialize_tty_struct(struct tty_struct *tty)
  3435. {
  3436. memset(tty, 0, sizeof(struct tty_struct));
  3437. tty->magic = TTY_MAGIC;
  3438. tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
  3439. tty->session = NULL;
  3440. tty->pgrp = NULL;
  3441. tty->overrun_time = jiffies;
  3442. tty->buf.head = tty->buf.tail = NULL;
  3443. tty_buffer_init(tty);
  3444. INIT_DELAYED_WORK(&tty->buf.work, flush_to_ldisc);
  3445. mutex_init(&tty->termios_mutex);
  3446. init_waitqueue_head(&tty->write_wait);
  3447. init_waitqueue_head(&tty->read_wait);
  3448. INIT_WORK(&tty->hangup_work, do_tty_hangup);
  3449. mutex_init(&tty->atomic_read_lock);
  3450. mutex_init(&tty->atomic_write_lock);
  3451. spin_lock_init(&tty->read_lock);
  3452. spin_lock_init(&tty->ctrl_lock);
  3453. INIT_LIST_HEAD(&tty->tty_files);
  3454. INIT_WORK(&tty->SAK_work, do_SAK_work);
  3455. }
  3456. /**
  3457. * tty_put_char - write one character to a tty
  3458. * @tty: tty
  3459. * @ch: character
  3460. *
  3461. * Write one byte to the tty using the provided put_char method
  3462. * if present. Returns the number of characters successfully output.
  3463. *
  3464. * Note: the specific put_char operation in the driver layer may go
  3465. * away soon. Don't call it directly, use this method
  3466. */
  3467. int tty_put_char(struct tty_struct *tty, unsigned char ch)
  3468. {
  3469. if (tty->ops->put_char)
  3470. return tty->ops->put_char(tty, ch);
  3471. return tty->ops->write(tty, &ch, 1);
  3472. }
  3473. EXPORT_SYMBOL_GPL(tty_put_char);
  3474. static struct class *tty_class;
  3475. /**
  3476. * tty_register_device - register a tty device
  3477. * @driver: the tty driver that describes the tty device
  3478. * @index: the index in the tty driver for this tty device
  3479. * @device: a struct device that is associated with this tty device.
  3480. * This field is optional, if there is no known struct device
  3481. * for this tty device it can be set to NULL safely.
  3482. *
  3483. * Returns a pointer to the struct device for this tty device
  3484. * (or ERR_PTR(-EFOO) on error).
  3485. *
  3486. * This call is required to be made to register an individual tty device
  3487. * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
  3488. * that bit is not set, this function should not be called by a tty
  3489. * driver.
  3490. *
  3491. * Locking: ??
  3492. */
  3493. struct device *tty_register_device(struct tty_driver *driver, unsigned index,
  3494. struct device *device)
  3495. {
  3496. char name[64];
  3497. dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
  3498. if (index >= driver->num) {
  3499. printk(KERN_ERR "Attempt to register invalid tty line number "
  3500. " (%d).\n", index);
  3501. return ERR_PTR(-EINVAL);
  3502. }
  3503. if (driver->type == TTY_DRIVER_TYPE_PTY)
  3504. pty_line_name(driver, index, name);
  3505. else
  3506. tty_line_name(driver, index, name);
  3507. return device_create(tty_class, device, dev, name);
  3508. }
  3509. /**
  3510. * tty_unregister_device - unregister a tty device
  3511. * @driver: the tty driver that describes the tty device
  3512. * @index: the index in the tty driver for this tty device
  3513. *
  3514. * If a tty device is registered with a call to tty_register_device() then
  3515. * this function must be called when the tty device is gone.
  3516. *
  3517. * Locking: ??
  3518. */
  3519. void tty_unregister_device(struct tty_driver *driver, unsigned index)
  3520. {
  3521. device_destroy(tty_class,
  3522. MKDEV(driver->major, driver->minor_start) + index);
  3523. }
  3524. EXPORT_SYMBOL(tty_register_device);
  3525. EXPORT_SYMBOL(tty_unregister_device);
  3526. struct tty_driver *alloc_tty_driver(int lines)
  3527. {
  3528. struct tty_driver *driver;
  3529. driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
  3530. if (driver) {
  3531. driver->magic = TTY_DRIVER_MAGIC;
  3532. driver->num = lines;
  3533. /* later we'll move allocation of tables here */
  3534. }
  3535. return driver;
  3536. }
  3537. void put_tty_driver(struct tty_driver *driver)
  3538. {
  3539. kfree(driver);
  3540. }
  3541. void tty_set_operations(struct tty_driver *driver,
  3542. const struct tty_operations *op)
  3543. {
  3544. driver->ops = op;
  3545. };
  3546. EXPORT_SYMBOL(alloc_tty_driver);
  3547. EXPORT_SYMBOL(put_tty_driver);
  3548. EXPORT_SYMBOL(tty_set_operations);
  3549. /*
  3550. * Called by a tty driver to register itself.
  3551. */
  3552. int tty_register_driver(struct tty_driver *driver)
  3553. {
  3554. int error;
  3555. int i;
  3556. dev_t dev;
  3557. void **p = NULL;
  3558. if (driver->flags & TTY_DRIVER_INSTALLED)
  3559. return 0;
  3560. if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
  3561. p = kzalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL);
  3562. if (!p)
  3563. return -ENOMEM;
  3564. }
  3565. if (!driver->major) {
  3566. error = alloc_chrdev_region(&dev, driver->minor_start,
  3567. driver->num, driver->name);
  3568. if (!error) {
  3569. driver->major = MAJOR(dev);
  3570. driver->minor_start = MINOR(dev);
  3571. }
  3572. } else {
  3573. dev = MKDEV(driver->major, driver->minor_start);
  3574. error = register_chrdev_region(dev, driver->num, driver->name);
  3575. }
  3576. if (error < 0) {
  3577. kfree(p);
  3578. return error;
  3579. }
  3580. if (p) {
  3581. driver->ttys = (struct tty_struct **)p;
  3582. driver->termios = (struct ktermios **)(p + driver->num);
  3583. driver->termios_locked = (struct ktermios **)
  3584. (p + driver->num * 2);
  3585. } else {
  3586. driver->ttys = NULL;
  3587. driver->termios = NULL;
  3588. driver->termios_locked = NULL;
  3589. }
  3590. cdev_init(&driver->cdev, &tty_fops);
  3591. driver->cdev.owner = driver->owner;
  3592. error = cdev_add(&driver->cdev, dev, driver->num);
  3593. if (error) {
  3594. unregister_chrdev_region(dev, driver->num);
  3595. driver->ttys = NULL;
  3596. driver->termios = driver->termios_locked = NULL;
  3597. kfree(p);
  3598. return error;
  3599. }
  3600. mutex_lock(&tty_mutex);
  3601. list_add(&driver->tty_drivers, &tty_drivers);
  3602. mutex_unlock(&tty_mutex);
  3603. if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
  3604. for (i = 0; i < driver->num; i++)
  3605. tty_register_device(driver, i, NULL);
  3606. }
  3607. proc_tty_register_driver(driver);
  3608. return 0;
  3609. }
  3610. EXPORT_SYMBOL(tty_register_driver);
  3611. /*
  3612. * Called by a tty driver to unregister itself.
  3613. */
  3614. int tty_unregister_driver(struct tty_driver *driver)
  3615. {
  3616. int i;
  3617. struct ktermios *tp;
  3618. void *p;
  3619. if (driver->refcount)
  3620. return -EBUSY;
  3621. unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
  3622. driver->num);
  3623. mutex_lock(&tty_mutex);
  3624. list_del(&driver->tty_drivers);
  3625. mutex_unlock(&tty_mutex);
  3626. /*
  3627. * Free the termios and termios_locked structures because
  3628. * we don't want to get memory leaks when modular tty
  3629. * drivers are removed from the kernel.
  3630. */
  3631. for (i = 0; i < driver->num; i++) {
  3632. tp = driver->termios[i];
  3633. if (tp) {
  3634. driver->termios[i] = NULL;
  3635. kfree(tp);
  3636. }
  3637. tp = driver->termios_locked[i];
  3638. if (tp) {
  3639. driver->termios_locked[i] = NULL;
  3640. kfree(tp);
  3641. }
  3642. if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
  3643. tty_unregister_device(driver, i);
  3644. }
  3645. p = driver->ttys;
  3646. proc_tty_unregister_driver(driver);
  3647. driver->ttys = NULL;
  3648. driver->termios = driver->termios_locked = NULL;
  3649. kfree(p);
  3650. cdev_del(&driver->cdev);
  3651. return 0;
  3652. }
  3653. EXPORT_SYMBOL(tty_unregister_driver);
  3654. dev_t tty_devnum(struct tty_struct *tty)
  3655. {
  3656. return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
  3657. }
  3658. EXPORT_SYMBOL(tty_devnum);
  3659. void proc_clear_tty(struct task_struct *p)
  3660. {
  3661. spin_lock_irq(&p->sighand->siglock);
  3662. p->signal->tty = NULL;
  3663. spin_unlock_irq(&p->sighand->siglock);
  3664. }
  3665. EXPORT_SYMBOL(proc_clear_tty);
  3666. /* Called under the sighand lock */
  3667. static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
  3668. {
  3669. if (tty) {
  3670. unsigned long flags;
  3671. /* We should not have a session or pgrp to put here but.... */
  3672. spin_lock_irqsave(&tty->ctrl_lock, flags);
  3673. put_pid(tty->session);
  3674. put_pid(tty->pgrp);
  3675. tty->pgrp = get_pid(task_pgrp(tsk));
  3676. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  3677. tty->session = get_pid(task_session(tsk));
  3678. }
  3679. put_pid(tsk->signal->tty_old_pgrp);
  3680. tsk->signal->tty = tty;
  3681. tsk->signal->tty_old_pgrp = NULL;
  3682. }
  3683. static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
  3684. {
  3685. spin_lock_irq(&tsk->sighand->siglock);
  3686. __proc_set_tty(tsk, tty);
  3687. spin_unlock_irq(&tsk->sighand->siglock);
  3688. }
  3689. struct tty_struct *get_current_tty(void)
  3690. {
  3691. struct tty_struct *tty;
  3692. WARN_ON_ONCE(!mutex_is_locked(&tty_mutex));
  3693. tty = current->signal->tty;
  3694. /*
  3695. * session->tty can be changed/cleared from under us, make sure we
  3696. * issue the load. The obtained pointer, when not NULL, is valid as
  3697. * long as we hold tty_mutex.
  3698. */
  3699. barrier();
  3700. return tty;
  3701. }
  3702. EXPORT_SYMBOL_GPL(get_current_tty);
  3703. /*
  3704. * Initialize the console device. This is called *early*, so
  3705. * we can't necessarily depend on lots of kernel help here.
  3706. * Just do some early initializations, and do the complex setup
  3707. * later.
  3708. */
  3709. void __init console_init(void)
  3710. {
  3711. initcall_t *call;
  3712. /* Setup the default TTY line discipline. */
  3713. (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
  3714. /*
  3715. * set up the console device so that later boot sequences can
  3716. * inform about problems etc..
  3717. */
  3718. call = __con_initcall_start;
  3719. while (call < __con_initcall_end) {
  3720. (*call)();
  3721. call++;
  3722. }
  3723. }
  3724. static int __init tty_class_init(void)
  3725. {
  3726. tty_class = class_create(THIS_MODULE, "tty");
  3727. if (IS_ERR(tty_class))
  3728. return PTR_ERR(tty_class);
  3729. return 0;
  3730. }
  3731. postcore_initcall(tty_class_init);
  3732. /* 3/2004 jmc: why do these devices exist? */
  3733. static struct cdev tty_cdev, console_cdev;
  3734. #ifdef CONFIG_UNIX98_PTYS
  3735. static struct cdev ptmx_cdev;
  3736. #endif
  3737. #ifdef CONFIG_VT
  3738. static struct cdev vc0_cdev;
  3739. #endif
  3740. /*
  3741. * Ok, now we can initialize the rest of the tty devices and can count
  3742. * on memory allocations, interrupts etc..
  3743. */
  3744. static int __init tty_init(void)
  3745. {
  3746. cdev_init(&tty_cdev, &tty_fops);
  3747. if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
  3748. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
  3749. panic("Couldn't register /dev/tty driver\n");
  3750. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), "tty");
  3751. cdev_init(&console_cdev, &console_fops);
  3752. if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
  3753. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
  3754. panic("Couldn't register /dev/console driver\n");
  3755. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), "console");
  3756. #ifdef CONFIG_UNIX98_PTYS
  3757. cdev_init(&ptmx_cdev, &ptmx_fops);
  3758. if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
  3759. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
  3760. panic("Couldn't register /dev/ptmx driver\n");
  3761. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), "ptmx");
  3762. #endif
  3763. #ifdef CONFIG_VT
  3764. cdev_init(&vc0_cdev, &console_fops);
  3765. if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
  3766. register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
  3767. panic("Couldn't register /dev/tty0 driver\n");
  3768. device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), "tty0");
  3769. vty_init();
  3770. #endif
  3771. return 0;
  3772. }
  3773. module_init(tty_init);