tty_io.c 98 KB

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