tty_io.c 99 KB

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