tty_io.c 98 KB

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