tty_io.c 102 KB

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