tty_io.c 109 KB

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