tty_io.c 103 KB

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