tty_io.c 98 KB

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