tty_io.c 105 KB

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