tty_io.c 98 KB

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