tty_io.c 98 KB

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