tty_io.c 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039
  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. (void) kill_pgrp(task_pgrp(current), SIGTTOU, 1);
  1016. return -ERESTARTSYS;
  1017. }
  1018. EXPORT_SYMBOL(tty_check_change);
  1019. static ssize_t hung_up_tty_read(struct file * file, char __user * buf,
  1020. size_t count, loff_t *ppos)
  1021. {
  1022. return 0;
  1023. }
  1024. static ssize_t hung_up_tty_write(struct file * file, const char __user * buf,
  1025. size_t count, loff_t *ppos)
  1026. {
  1027. return -EIO;
  1028. }
  1029. /* No kernel lock held - none needed ;) */
  1030. static unsigned int hung_up_tty_poll(struct file * filp, poll_table * wait)
  1031. {
  1032. return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
  1033. }
  1034. static long hung_up_tty_ioctl(struct file * file,
  1035. unsigned int cmd, unsigned long arg)
  1036. {
  1037. return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
  1038. }
  1039. static const struct file_operations tty_fops = {
  1040. .llseek = no_llseek,
  1041. .read = tty_read,
  1042. .write = tty_write,
  1043. .poll = tty_poll,
  1044. .ioctl = tty_ioctl,
  1045. .compat_ioctl = tty_compat_ioctl,
  1046. .open = tty_open,
  1047. .release = tty_release,
  1048. .fasync = tty_fasync,
  1049. };
  1050. #ifdef CONFIG_UNIX98_PTYS
  1051. static const struct file_operations ptmx_fops = {
  1052. .llseek = no_llseek,
  1053. .read = tty_read,
  1054. .write = tty_write,
  1055. .poll = tty_poll,
  1056. .ioctl = tty_ioctl,
  1057. .compat_ioctl = tty_compat_ioctl,
  1058. .open = ptmx_open,
  1059. .release = tty_release,
  1060. .fasync = tty_fasync,
  1061. };
  1062. #endif
  1063. static const struct file_operations console_fops = {
  1064. .llseek = no_llseek,
  1065. .read = tty_read,
  1066. .write = redirected_tty_write,
  1067. .poll = tty_poll,
  1068. .ioctl = tty_ioctl,
  1069. .compat_ioctl = tty_compat_ioctl,
  1070. .open = tty_open,
  1071. .release = tty_release,
  1072. .fasync = tty_fasync,
  1073. };
  1074. static const struct file_operations hung_up_tty_fops = {
  1075. .llseek = no_llseek,
  1076. .read = hung_up_tty_read,
  1077. .write = hung_up_tty_write,
  1078. .poll = hung_up_tty_poll,
  1079. .unlocked_ioctl = hung_up_tty_ioctl,
  1080. .compat_ioctl = hung_up_tty_ioctl,
  1081. .release = tty_release,
  1082. };
  1083. static DEFINE_SPINLOCK(redirect_lock);
  1084. static struct file *redirect;
  1085. /**
  1086. * tty_wakeup - request more data
  1087. * @tty: terminal
  1088. *
  1089. * Internal and external helper for wakeups of tty. This function
  1090. * informs the line discipline if present that the driver is ready
  1091. * to receive more output data.
  1092. */
  1093. void tty_wakeup(struct tty_struct *tty)
  1094. {
  1095. struct tty_ldisc *ld;
  1096. if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
  1097. ld = tty_ldisc_ref(tty);
  1098. if(ld) {
  1099. if(ld->write_wakeup)
  1100. ld->write_wakeup(tty);
  1101. tty_ldisc_deref(ld);
  1102. }
  1103. }
  1104. wake_up_interruptible(&tty->write_wait);
  1105. }
  1106. EXPORT_SYMBOL_GPL(tty_wakeup);
  1107. /**
  1108. * tty_ldisc_flush - flush line discipline queue
  1109. * @tty: tty
  1110. *
  1111. * Flush the line discipline queue (if any) for this tty. If there
  1112. * is no line discipline active this is a no-op.
  1113. */
  1114. void tty_ldisc_flush(struct tty_struct *tty)
  1115. {
  1116. struct tty_ldisc *ld = tty_ldisc_ref(tty);
  1117. if(ld) {
  1118. if(ld->flush_buffer)
  1119. ld->flush_buffer(tty);
  1120. tty_ldisc_deref(ld);
  1121. }
  1122. tty_buffer_flush(tty);
  1123. }
  1124. EXPORT_SYMBOL_GPL(tty_ldisc_flush);
  1125. /**
  1126. * tty_reset_termios - reset terminal state
  1127. * @tty: tty to reset
  1128. *
  1129. * Restore a terminal to the driver default state
  1130. */
  1131. static void tty_reset_termios(struct tty_struct *tty)
  1132. {
  1133. mutex_lock(&tty->termios_mutex);
  1134. *tty->termios = tty->driver->init_termios;
  1135. tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
  1136. tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
  1137. mutex_unlock(&tty->termios_mutex);
  1138. }
  1139. /**
  1140. * do_tty_hangup - actual handler for hangup events
  1141. * @work: tty device
  1142. *
  1143. * This can be called by the "eventd" kernel thread. That is process
  1144. * synchronous but doesn't hold any locks, so we need to make sure we
  1145. * have the appropriate locks for what we're doing.
  1146. *
  1147. * The hangup event clears any pending redirections onto the hung up
  1148. * device. It ensures future writes will error and it does the needed
  1149. * line discipline hangup and signal delivery. The tty object itself
  1150. * remains intact.
  1151. *
  1152. * Locking:
  1153. * BKL
  1154. * redirect lock for undoing redirection
  1155. * file list lock for manipulating list of ttys
  1156. * tty_ldisc_lock from called functions
  1157. * termios_mutex resetting termios data
  1158. * tasklist_lock to walk task list for hangup event
  1159. * ->siglock to protect ->signal/->sighand
  1160. */
  1161. static void do_tty_hangup(struct work_struct *work)
  1162. {
  1163. struct tty_struct *tty =
  1164. container_of(work, struct tty_struct, hangup_work);
  1165. struct file * cons_filp = NULL;
  1166. struct file *filp, *f = NULL;
  1167. struct task_struct *p;
  1168. struct tty_ldisc *ld;
  1169. int closecount = 0, n;
  1170. if (!tty)
  1171. return;
  1172. /* inuse_filps is protected by the single kernel lock */
  1173. lock_kernel();
  1174. spin_lock(&redirect_lock);
  1175. if (redirect && redirect->private_data == tty) {
  1176. f = redirect;
  1177. redirect = NULL;
  1178. }
  1179. spin_unlock(&redirect_lock);
  1180. check_tty_count(tty, "do_tty_hangup");
  1181. file_list_lock();
  1182. /* This breaks for file handles being sent over AF_UNIX sockets ? */
  1183. list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
  1184. if (filp->f_op->write == redirected_tty_write)
  1185. cons_filp = filp;
  1186. if (filp->f_op->write != tty_write)
  1187. continue;
  1188. closecount++;
  1189. tty_fasync(-1, filp, 0); /* can't block */
  1190. filp->f_op = &hung_up_tty_fops;
  1191. }
  1192. file_list_unlock();
  1193. /* FIXME! What are the locking issues here? This may me overdoing things..
  1194. * this question is especially important now that we've removed the irqlock. */
  1195. ld = tty_ldisc_ref(tty);
  1196. if(ld != NULL) /* We may have no line discipline at this point */
  1197. {
  1198. if (ld->flush_buffer)
  1199. ld->flush_buffer(tty);
  1200. if (tty->driver->flush_buffer)
  1201. tty->driver->flush_buffer(tty);
  1202. if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
  1203. ld->write_wakeup)
  1204. ld->write_wakeup(tty);
  1205. if (ld->hangup)
  1206. ld->hangup(tty);
  1207. }
  1208. /* FIXME: Once we trust the LDISC code better we can wait here for
  1209. ldisc completion and fix the driver call race */
  1210. wake_up_interruptible(&tty->write_wait);
  1211. wake_up_interruptible(&tty->read_wait);
  1212. /*
  1213. * Shutdown the current line discipline, and reset it to
  1214. * N_TTY.
  1215. */
  1216. if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
  1217. tty_reset_termios(tty);
  1218. /* Defer ldisc switch */
  1219. /* tty_deferred_ldisc_switch(N_TTY);
  1220. This should get done automatically when the port closes and
  1221. tty_release is called */
  1222. read_lock(&tasklist_lock);
  1223. if (tty->session) {
  1224. do_each_pid_task(tty->session, PIDTYPE_SID, p) {
  1225. spin_lock_irq(&p->sighand->siglock);
  1226. if (p->signal->tty == tty)
  1227. p->signal->tty = NULL;
  1228. if (!p->signal->leader) {
  1229. spin_unlock_irq(&p->sighand->siglock);
  1230. continue;
  1231. }
  1232. __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
  1233. __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
  1234. put_pid(p->signal->tty_old_pgrp); /* A noop */
  1235. if (tty->pgrp)
  1236. p->signal->tty_old_pgrp = get_pid(tty->pgrp);
  1237. spin_unlock_irq(&p->sighand->siglock);
  1238. } while_each_pid_task(tty->session, PIDTYPE_SID, p);
  1239. }
  1240. read_unlock(&tasklist_lock);
  1241. tty->flags = 0;
  1242. put_pid(tty->session);
  1243. put_pid(tty->pgrp);
  1244. tty->session = NULL;
  1245. tty->pgrp = NULL;
  1246. tty->ctrl_status = 0;
  1247. /*
  1248. * If one of the devices matches a console pointer, we
  1249. * cannot just call hangup() because that will cause
  1250. * tty->count and state->count to go out of sync.
  1251. * So we just call close() the right number of times.
  1252. */
  1253. if (cons_filp) {
  1254. if (tty->driver->close)
  1255. for (n = 0; n < closecount; n++)
  1256. tty->driver->close(tty, cons_filp);
  1257. } else if (tty->driver->hangup)
  1258. (tty->driver->hangup)(tty);
  1259. /* We don't want to have driver/ldisc interactions beyond
  1260. the ones we did here. The driver layer expects no
  1261. calls after ->hangup() from the ldisc side. However we
  1262. can't yet guarantee all that */
  1263. set_bit(TTY_HUPPED, &tty->flags);
  1264. if (ld) {
  1265. tty_ldisc_enable(tty);
  1266. tty_ldisc_deref(ld);
  1267. }
  1268. unlock_kernel();
  1269. if (f)
  1270. fput(f);
  1271. }
  1272. /**
  1273. * tty_hangup - trigger a hangup event
  1274. * @tty: tty to hangup
  1275. *
  1276. * A carrier loss (virtual or otherwise) has occurred on this like
  1277. * schedule a hangup sequence to run after this event.
  1278. */
  1279. void tty_hangup(struct tty_struct * tty)
  1280. {
  1281. #ifdef TTY_DEBUG_HANGUP
  1282. char buf[64];
  1283. printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
  1284. #endif
  1285. schedule_work(&tty->hangup_work);
  1286. }
  1287. EXPORT_SYMBOL(tty_hangup);
  1288. /**
  1289. * tty_vhangup - process vhangup
  1290. * @tty: tty to hangup
  1291. *
  1292. * The user has asked via system call for the terminal to be hung up.
  1293. * We do this synchronously so that when the syscall returns the process
  1294. * is complete. That guarantee is neccessary for security reasons.
  1295. */
  1296. void tty_vhangup(struct tty_struct * tty)
  1297. {
  1298. #ifdef TTY_DEBUG_HANGUP
  1299. char buf[64];
  1300. printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
  1301. #endif
  1302. do_tty_hangup(&tty->hangup_work);
  1303. }
  1304. EXPORT_SYMBOL(tty_vhangup);
  1305. /**
  1306. * tty_hung_up_p - was tty hung up
  1307. * @filp: file pointer of tty
  1308. *
  1309. * Return true if the tty has been subject to a vhangup or a carrier
  1310. * loss
  1311. */
  1312. int tty_hung_up_p(struct file * filp)
  1313. {
  1314. return (filp->f_op == &hung_up_tty_fops);
  1315. }
  1316. EXPORT_SYMBOL(tty_hung_up_p);
  1317. static void session_clear_tty(struct pid *session)
  1318. {
  1319. struct task_struct *p;
  1320. do_each_pid_task(session, PIDTYPE_SID, p) {
  1321. proc_clear_tty(p);
  1322. } while_each_pid_task(session, PIDTYPE_SID, p);
  1323. }
  1324. /**
  1325. * disassociate_ctty - disconnect controlling tty
  1326. * @on_exit: true if exiting so need to "hang up" the session
  1327. *
  1328. * This function is typically called only by the session leader, when
  1329. * it wants to disassociate itself from its controlling tty.
  1330. *
  1331. * It performs the following functions:
  1332. * (1) Sends a SIGHUP and SIGCONT to the foreground process group
  1333. * (2) Clears the tty from being controlling the session
  1334. * (3) Clears the controlling tty for all processes in the
  1335. * session group.
  1336. *
  1337. * The argument on_exit is set to 1 if called when a process is
  1338. * exiting; it is 0 if called by the ioctl TIOCNOTTY.
  1339. *
  1340. * Locking:
  1341. * BKL is taken for hysterical raisins
  1342. * tty_mutex is taken to protect tty
  1343. * ->siglock is taken to protect ->signal/->sighand
  1344. * tasklist_lock is taken to walk process list for sessions
  1345. * ->siglock is taken to protect ->signal/->sighand
  1346. */
  1347. void disassociate_ctty(int on_exit)
  1348. {
  1349. struct tty_struct *tty;
  1350. struct pid *tty_pgrp = NULL;
  1351. lock_kernel();
  1352. mutex_lock(&tty_mutex);
  1353. tty = get_current_tty();
  1354. if (tty) {
  1355. tty_pgrp = get_pid(tty->pgrp);
  1356. mutex_unlock(&tty_mutex);
  1357. /* XXX: here we race, there is nothing protecting tty */
  1358. if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
  1359. tty_vhangup(tty);
  1360. } else if (on_exit) {
  1361. struct pid *old_pgrp;
  1362. spin_lock_irq(&current->sighand->siglock);
  1363. old_pgrp = current->signal->tty_old_pgrp;
  1364. current->signal->tty_old_pgrp = NULL;
  1365. spin_unlock_irq(&current->sighand->siglock);
  1366. if (old_pgrp) {
  1367. kill_pgrp(old_pgrp, SIGHUP, on_exit);
  1368. kill_pgrp(old_pgrp, SIGCONT, on_exit);
  1369. put_pid(old_pgrp);
  1370. }
  1371. mutex_unlock(&tty_mutex);
  1372. unlock_kernel();
  1373. return;
  1374. }
  1375. if (tty_pgrp) {
  1376. kill_pgrp(tty_pgrp, SIGHUP, on_exit);
  1377. if (!on_exit)
  1378. kill_pgrp(tty_pgrp, SIGCONT, on_exit);
  1379. put_pid(tty_pgrp);
  1380. }
  1381. spin_lock_irq(&current->sighand->siglock);
  1382. put_pid(current->signal->tty_old_pgrp);
  1383. current->signal->tty_old_pgrp = NULL;
  1384. spin_unlock_irq(&current->sighand->siglock);
  1385. mutex_lock(&tty_mutex);
  1386. /* It is possible that do_tty_hangup has free'd this tty */
  1387. tty = get_current_tty();
  1388. if (tty) {
  1389. put_pid(tty->session);
  1390. put_pid(tty->pgrp);
  1391. tty->session = NULL;
  1392. tty->pgrp = NULL;
  1393. } else {
  1394. #ifdef TTY_DEBUG_HANGUP
  1395. printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
  1396. " = NULL", tty);
  1397. #endif
  1398. }
  1399. mutex_unlock(&tty_mutex);
  1400. /* Now clear signal->tty under the lock */
  1401. read_lock(&tasklist_lock);
  1402. session_clear_tty(task_session(current));
  1403. read_unlock(&tasklist_lock);
  1404. unlock_kernel();
  1405. }
  1406. /**
  1407. *
  1408. * no_tty - Ensure the current process does not have a controlling tty
  1409. */
  1410. void no_tty(void)
  1411. {
  1412. struct task_struct *tsk = current;
  1413. if (tsk->signal->leader)
  1414. disassociate_ctty(0);
  1415. proc_clear_tty(tsk);
  1416. }
  1417. /**
  1418. * stop_tty - propagate flow control
  1419. * @tty: tty to stop
  1420. *
  1421. * Perform flow control to the driver. For PTY/TTY pairs we
  1422. * must also propagate the TIOCKPKT status. May be called
  1423. * on an already stopped device and will not re-call the driver
  1424. * method.
  1425. *
  1426. * This functionality is used by both the line disciplines for
  1427. * halting incoming flow and by the driver. It may therefore be
  1428. * called from any context, may be under the tty atomic_write_lock
  1429. * but not always.
  1430. *
  1431. * Locking:
  1432. * Broken. Relies on BKL which is unsafe here.
  1433. */
  1434. void stop_tty(struct tty_struct *tty)
  1435. {
  1436. if (tty->stopped)
  1437. return;
  1438. tty->stopped = 1;
  1439. if (tty->link && tty->link->packet) {
  1440. tty->ctrl_status &= ~TIOCPKT_START;
  1441. tty->ctrl_status |= TIOCPKT_STOP;
  1442. wake_up_interruptible(&tty->link->read_wait);
  1443. }
  1444. if (tty->driver->stop)
  1445. (tty->driver->stop)(tty);
  1446. }
  1447. EXPORT_SYMBOL(stop_tty);
  1448. /**
  1449. * start_tty - propagate flow control
  1450. * @tty: tty to start
  1451. *
  1452. * Start a tty that has been stopped if at all possible. Perform
  1453. * any neccessary wakeups and propagate the TIOCPKT status. If this
  1454. * is the tty was previous stopped and is being started then the
  1455. * driver start method is invoked and the line discipline woken.
  1456. *
  1457. * Locking:
  1458. * Broken. Relies on BKL which is unsafe here.
  1459. */
  1460. void start_tty(struct tty_struct *tty)
  1461. {
  1462. if (!tty->stopped || tty->flow_stopped)
  1463. return;
  1464. tty->stopped = 0;
  1465. if (tty->link && tty->link->packet) {
  1466. tty->ctrl_status &= ~TIOCPKT_STOP;
  1467. tty->ctrl_status |= TIOCPKT_START;
  1468. wake_up_interruptible(&tty->link->read_wait);
  1469. }
  1470. if (tty->driver->start)
  1471. (tty->driver->start)(tty);
  1472. /* If we have a running line discipline it may need kicking */
  1473. tty_wakeup(tty);
  1474. }
  1475. EXPORT_SYMBOL(start_tty);
  1476. /**
  1477. * tty_read - read method for tty device files
  1478. * @file: pointer to tty file
  1479. * @buf: user buffer
  1480. * @count: size of user buffer
  1481. * @ppos: unused
  1482. *
  1483. * Perform the read system call function on this terminal device. Checks
  1484. * for hung up devices before calling the line discipline method.
  1485. *
  1486. * Locking:
  1487. * Locks the line discipline internally while needed
  1488. * For historical reasons the line discipline read method is
  1489. * invoked under the BKL. This will go away in time so do not rely on it
  1490. * in new code. Multiple read calls may be outstanding in parallel.
  1491. */
  1492. static ssize_t tty_read(struct file * file, char __user * buf, size_t count,
  1493. loff_t *ppos)
  1494. {
  1495. int i;
  1496. struct tty_struct * tty;
  1497. struct inode *inode;
  1498. struct tty_ldisc *ld;
  1499. tty = (struct tty_struct *)file->private_data;
  1500. inode = file->f_path.dentry->d_inode;
  1501. if (tty_paranoia_check(tty, inode, "tty_read"))
  1502. return -EIO;
  1503. if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
  1504. return -EIO;
  1505. /* We want to wait for the line discipline to sort out in this
  1506. situation */
  1507. ld = tty_ldisc_ref_wait(tty);
  1508. lock_kernel();
  1509. if (ld->read)
  1510. i = (ld->read)(tty,file,buf,count);
  1511. else
  1512. i = -EIO;
  1513. tty_ldisc_deref(ld);
  1514. unlock_kernel();
  1515. if (i > 0)
  1516. inode->i_atime = current_fs_time(inode->i_sb);
  1517. return i;
  1518. }
  1519. /*
  1520. * Split writes up in sane blocksizes to avoid
  1521. * denial-of-service type attacks
  1522. */
  1523. static inline ssize_t do_tty_write(
  1524. ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
  1525. struct tty_struct *tty,
  1526. struct file *file,
  1527. const char __user *buf,
  1528. size_t count)
  1529. {
  1530. ssize_t ret = 0, written = 0;
  1531. unsigned int chunk;
  1532. /* FIXME: O_NDELAY ... */
  1533. if (mutex_lock_interruptible(&tty->atomic_write_lock)) {
  1534. return -ERESTARTSYS;
  1535. }
  1536. /*
  1537. * We chunk up writes into a temporary buffer. This
  1538. * simplifies low-level drivers immensely, since they
  1539. * don't have locking issues and user mode accesses.
  1540. *
  1541. * But if TTY_NO_WRITE_SPLIT is set, we should use a
  1542. * big chunk-size..
  1543. *
  1544. * The default chunk-size is 2kB, because the NTTY
  1545. * layer has problems with bigger chunks. It will
  1546. * claim to be able to handle more characters than
  1547. * it actually does.
  1548. *
  1549. * FIXME: This can probably go away now except that 64K chunks
  1550. * are too likely to fail unless switched to vmalloc...
  1551. */
  1552. chunk = 2048;
  1553. if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
  1554. chunk = 65536;
  1555. if (count < chunk)
  1556. chunk = count;
  1557. /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
  1558. if (tty->write_cnt < chunk) {
  1559. unsigned char *buf;
  1560. if (chunk < 1024)
  1561. chunk = 1024;
  1562. buf = kmalloc(chunk, GFP_KERNEL);
  1563. if (!buf) {
  1564. mutex_unlock(&tty->atomic_write_lock);
  1565. return -ENOMEM;
  1566. }
  1567. kfree(tty->write_buf);
  1568. tty->write_cnt = chunk;
  1569. tty->write_buf = buf;
  1570. }
  1571. /* Do the write .. */
  1572. for (;;) {
  1573. size_t size = count;
  1574. if (size > chunk)
  1575. size = chunk;
  1576. ret = -EFAULT;
  1577. if (copy_from_user(tty->write_buf, buf, size))
  1578. break;
  1579. lock_kernel();
  1580. ret = write(tty, file, tty->write_buf, size);
  1581. unlock_kernel();
  1582. if (ret <= 0)
  1583. break;
  1584. written += ret;
  1585. buf += ret;
  1586. count -= ret;
  1587. if (!count)
  1588. break;
  1589. ret = -ERESTARTSYS;
  1590. if (signal_pending(current))
  1591. break;
  1592. cond_resched();
  1593. }
  1594. if (written) {
  1595. struct inode *inode = file->f_path.dentry->d_inode;
  1596. inode->i_mtime = current_fs_time(inode->i_sb);
  1597. ret = written;
  1598. }
  1599. mutex_unlock(&tty->atomic_write_lock);
  1600. return ret;
  1601. }
  1602. /**
  1603. * tty_write - write method for tty device file
  1604. * @file: tty file pointer
  1605. * @buf: user data to write
  1606. * @count: bytes to write
  1607. * @ppos: unused
  1608. *
  1609. * Write data to a tty device via the line discipline.
  1610. *
  1611. * Locking:
  1612. * Locks the line discipline as required
  1613. * Writes to the tty driver are serialized by the atomic_write_lock
  1614. * and are then processed in chunks to the device. The line discipline
  1615. * write method will not be involked in parallel for each device
  1616. * The line discipline write method is called under the big
  1617. * kernel lock for historical reasons. New code should not rely on this.
  1618. */
  1619. static ssize_t tty_write(struct file * file, const char __user * buf, size_t count,
  1620. loff_t *ppos)
  1621. {
  1622. struct tty_struct * tty;
  1623. struct inode *inode = file->f_path.dentry->d_inode;
  1624. ssize_t ret;
  1625. struct tty_ldisc *ld;
  1626. tty = (struct tty_struct *)file->private_data;
  1627. if (tty_paranoia_check(tty, inode, "tty_write"))
  1628. return -EIO;
  1629. if (!tty || !tty->driver->write || (test_bit(TTY_IO_ERROR, &tty->flags)))
  1630. return -EIO;
  1631. ld = tty_ldisc_ref_wait(tty);
  1632. if (!ld->write)
  1633. ret = -EIO;
  1634. else
  1635. ret = do_tty_write(ld->write, tty, file, buf, count);
  1636. tty_ldisc_deref(ld);
  1637. return ret;
  1638. }
  1639. ssize_t redirected_tty_write(struct file * file, const char __user * buf, size_t count,
  1640. loff_t *ppos)
  1641. {
  1642. struct file *p = NULL;
  1643. spin_lock(&redirect_lock);
  1644. if (redirect) {
  1645. get_file(redirect);
  1646. p = redirect;
  1647. }
  1648. spin_unlock(&redirect_lock);
  1649. if (p) {
  1650. ssize_t res;
  1651. res = vfs_write(p, buf, count, &p->f_pos);
  1652. fput(p);
  1653. return res;
  1654. }
  1655. return tty_write(file, buf, count, ppos);
  1656. }
  1657. static char ptychar[] = "pqrstuvwxyzabcde";
  1658. /**
  1659. * pty_line_name - generate name for a pty
  1660. * @driver: the tty driver in use
  1661. * @index: the minor number
  1662. * @p: output buffer of at least 6 bytes
  1663. *
  1664. * Generate a name from a driver reference and write it to the output
  1665. * buffer.
  1666. *
  1667. * Locking: None
  1668. */
  1669. static void pty_line_name(struct tty_driver *driver, int index, char *p)
  1670. {
  1671. int i = index + driver->name_base;
  1672. /* ->name is initialized to "ttyp", but "tty" is expected */
  1673. sprintf(p, "%s%c%x",
  1674. driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
  1675. ptychar[i >> 4 & 0xf], i & 0xf);
  1676. }
  1677. /**
  1678. * pty_line_name - generate name for a tty
  1679. * @driver: the tty driver in use
  1680. * @index: the minor number
  1681. * @p: output buffer of at least 7 bytes
  1682. *
  1683. * Generate a name from a driver reference and write it to the output
  1684. * buffer.
  1685. *
  1686. * Locking: None
  1687. */
  1688. static void tty_line_name(struct tty_driver *driver, int index, char *p)
  1689. {
  1690. sprintf(p, "%s%d", driver->name, index + driver->name_base);
  1691. }
  1692. /**
  1693. * init_dev - initialise a tty device
  1694. * @driver: tty driver we are opening a device on
  1695. * @idx: device index
  1696. * @tty: returned tty structure
  1697. *
  1698. * Prepare a tty device. This may not be a "new" clean device but
  1699. * could also be an active device. The pty drivers require special
  1700. * handling because of this.
  1701. *
  1702. * Locking:
  1703. * The function is called under the tty_mutex, which
  1704. * protects us from the tty struct or driver itself going away.
  1705. *
  1706. * On exit the tty device has the line discipline attached and
  1707. * a reference count of 1. If a pair was created for pty/tty use
  1708. * and the other was a pty master then it too has a reference count of 1.
  1709. *
  1710. * WSH 06/09/97: Rewritten to remove races and properly clean up after a
  1711. * failed open. The new code protects the open with a mutex, so it's
  1712. * really quite straightforward. The mutex locking can probably be
  1713. * relaxed for the (most common) case of reopening a tty.
  1714. */
  1715. static int init_dev(struct tty_driver *driver, int idx,
  1716. struct tty_struct **ret_tty)
  1717. {
  1718. struct tty_struct *tty, *o_tty;
  1719. struct ktermios *tp, **tp_loc, *o_tp, **o_tp_loc;
  1720. struct ktermios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
  1721. int retval = 0;
  1722. /* check whether we're reopening an existing tty */
  1723. if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
  1724. tty = devpts_get_tty(idx);
  1725. /*
  1726. * If we don't have a tty here on a slave open, it's because
  1727. * the master already started the close process and there's
  1728. * no relation between devpts file and tty anymore.
  1729. */
  1730. if (!tty && driver->subtype == PTY_TYPE_SLAVE) {
  1731. retval = -EIO;
  1732. goto end_init;
  1733. }
  1734. /*
  1735. * It's safe from now on because init_dev() is called with
  1736. * tty_mutex held and release_dev() won't change tty->count
  1737. * or tty->flags without having to grab tty_mutex
  1738. */
  1739. if (tty && driver->subtype == PTY_TYPE_MASTER)
  1740. tty = tty->link;
  1741. } else {
  1742. tty = driver->ttys[idx];
  1743. }
  1744. if (tty) goto fast_track;
  1745. /*
  1746. * First time open is complex, especially for PTY devices.
  1747. * This code guarantees that either everything succeeds and the
  1748. * TTY is ready for operation, or else the table slots are vacated
  1749. * and the allocated memory released. (Except that the termios
  1750. * and locked termios may be retained.)
  1751. */
  1752. if (!try_module_get(driver->owner)) {
  1753. retval = -ENODEV;
  1754. goto end_init;
  1755. }
  1756. o_tty = NULL;
  1757. tp = o_tp = NULL;
  1758. ltp = o_ltp = NULL;
  1759. tty = alloc_tty_struct();
  1760. if(!tty)
  1761. goto fail_no_mem;
  1762. initialize_tty_struct(tty);
  1763. tty->driver = driver;
  1764. tty->index = idx;
  1765. tty_line_name(driver, idx, tty->name);
  1766. if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
  1767. tp_loc = &tty->termios;
  1768. ltp_loc = &tty->termios_locked;
  1769. } else {
  1770. tp_loc = &driver->termios[idx];
  1771. ltp_loc = &driver->termios_locked[idx];
  1772. }
  1773. if (!*tp_loc) {
  1774. tp = (struct ktermios *) kmalloc(sizeof(struct ktermios),
  1775. GFP_KERNEL);
  1776. if (!tp)
  1777. goto free_mem_out;
  1778. *tp = driver->init_termios;
  1779. }
  1780. if (!*ltp_loc) {
  1781. ltp = (struct ktermios *) kmalloc(sizeof(struct ktermios),
  1782. GFP_KERNEL);
  1783. if (!ltp)
  1784. goto free_mem_out;
  1785. memset(ltp, 0, sizeof(struct ktermios));
  1786. }
  1787. if (driver->type == TTY_DRIVER_TYPE_PTY) {
  1788. o_tty = alloc_tty_struct();
  1789. if (!o_tty)
  1790. goto free_mem_out;
  1791. initialize_tty_struct(o_tty);
  1792. o_tty->driver = driver->other;
  1793. o_tty->index = idx;
  1794. tty_line_name(driver->other, idx, o_tty->name);
  1795. if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
  1796. o_tp_loc = &o_tty->termios;
  1797. o_ltp_loc = &o_tty->termios_locked;
  1798. } else {
  1799. o_tp_loc = &driver->other->termios[idx];
  1800. o_ltp_loc = &driver->other->termios_locked[idx];
  1801. }
  1802. if (!*o_tp_loc) {
  1803. o_tp = (struct ktermios *)
  1804. kmalloc(sizeof(struct ktermios), GFP_KERNEL);
  1805. if (!o_tp)
  1806. goto free_mem_out;
  1807. *o_tp = driver->other->init_termios;
  1808. }
  1809. if (!*o_ltp_loc) {
  1810. o_ltp = (struct ktermios *)
  1811. kmalloc(sizeof(struct ktermios), GFP_KERNEL);
  1812. if (!o_ltp)
  1813. goto free_mem_out;
  1814. memset(o_ltp, 0, sizeof(struct ktermios));
  1815. }
  1816. /*
  1817. * Everything allocated ... set up the o_tty structure.
  1818. */
  1819. if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM)) {
  1820. driver->other->ttys[idx] = o_tty;
  1821. }
  1822. if (!*o_tp_loc)
  1823. *o_tp_loc = o_tp;
  1824. if (!*o_ltp_loc)
  1825. *o_ltp_loc = o_ltp;
  1826. o_tty->termios = *o_tp_loc;
  1827. o_tty->termios_locked = *o_ltp_loc;
  1828. driver->other->refcount++;
  1829. if (driver->subtype == PTY_TYPE_MASTER)
  1830. o_tty->count++;
  1831. /* Establish the links in both directions */
  1832. tty->link = o_tty;
  1833. o_tty->link = tty;
  1834. }
  1835. /*
  1836. * All structures have been allocated, so now we install them.
  1837. * Failures after this point use release_tty to clean up, so
  1838. * there's no need to null out the local pointers.
  1839. */
  1840. if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
  1841. driver->ttys[idx] = tty;
  1842. }
  1843. if (!*tp_loc)
  1844. *tp_loc = tp;
  1845. if (!*ltp_loc)
  1846. *ltp_loc = ltp;
  1847. tty->termios = *tp_loc;
  1848. tty->termios_locked = *ltp_loc;
  1849. /* Compatibility until drivers always set this */
  1850. tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
  1851. tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
  1852. driver->refcount++;
  1853. tty->count++;
  1854. /*
  1855. * Structures all installed ... call the ldisc open routines.
  1856. * If we fail here just call release_tty to clean up. No need
  1857. * to decrement the use counts, as release_tty doesn't care.
  1858. */
  1859. if (tty->ldisc.open) {
  1860. retval = (tty->ldisc.open)(tty);
  1861. if (retval)
  1862. goto release_mem_out;
  1863. }
  1864. if (o_tty && o_tty->ldisc.open) {
  1865. retval = (o_tty->ldisc.open)(o_tty);
  1866. if (retval) {
  1867. if (tty->ldisc.close)
  1868. (tty->ldisc.close)(tty);
  1869. goto release_mem_out;
  1870. }
  1871. tty_ldisc_enable(o_tty);
  1872. }
  1873. tty_ldisc_enable(tty);
  1874. goto success;
  1875. /*
  1876. * This fast open can be used if the tty is already open.
  1877. * No memory is allocated, and the only failures are from
  1878. * attempting to open a closing tty or attempting multiple
  1879. * opens on a pty master.
  1880. */
  1881. fast_track:
  1882. if (test_bit(TTY_CLOSING, &tty->flags)) {
  1883. retval = -EIO;
  1884. goto end_init;
  1885. }
  1886. if (driver->type == TTY_DRIVER_TYPE_PTY &&
  1887. driver->subtype == PTY_TYPE_MASTER) {
  1888. /*
  1889. * special case for PTY masters: only one open permitted,
  1890. * and the slave side open count is incremented as well.
  1891. */
  1892. if (tty->count) {
  1893. retval = -EIO;
  1894. goto end_init;
  1895. }
  1896. tty->link->count++;
  1897. }
  1898. tty->count++;
  1899. tty->driver = driver; /* N.B. why do this every time?? */
  1900. /* FIXME */
  1901. if(!test_bit(TTY_LDISC, &tty->flags))
  1902. printk(KERN_ERR "init_dev but no ldisc\n");
  1903. success:
  1904. *ret_tty = tty;
  1905. /* All paths come through here to release the mutex */
  1906. end_init:
  1907. return retval;
  1908. /* Release locally allocated memory ... nothing placed in slots */
  1909. free_mem_out:
  1910. kfree(o_tp);
  1911. if (o_tty)
  1912. free_tty_struct(o_tty);
  1913. kfree(ltp);
  1914. kfree(tp);
  1915. free_tty_struct(tty);
  1916. fail_no_mem:
  1917. module_put(driver->owner);
  1918. retval = -ENOMEM;
  1919. goto end_init;
  1920. /* call the tty release_tty routine to clean out this slot */
  1921. release_mem_out:
  1922. if (printk_ratelimit())
  1923. printk(KERN_INFO "init_dev: ldisc open failed, "
  1924. "clearing slot %d\n", idx);
  1925. release_tty(tty, idx);
  1926. goto end_init;
  1927. }
  1928. /**
  1929. * release_one_tty - release tty structure memory
  1930. *
  1931. * Releases memory associated with a tty structure, and clears out the
  1932. * driver table slots. This function is called when a device is no longer
  1933. * in use. It also gets called when setup of a device fails.
  1934. *
  1935. * Locking:
  1936. * tty_mutex - sometimes only
  1937. * takes the file list lock internally when working on the list
  1938. * of ttys that the driver keeps.
  1939. * FIXME: should we require tty_mutex is held here ??
  1940. */
  1941. static void release_one_tty(struct tty_struct *tty, int idx)
  1942. {
  1943. int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM;
  1944. struct ktermios *tp;
  1945. if (!devpts)
  1946. tty->driver->ttys[idx] = NULL;
  1947. if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
  1948. tp = tty->termios;
  1949. if (!devpts)
  1950. tty->driver->termios[idx] = NULL;
  1951. kfree(tp);
  1952. tp = tty->termios_locked;
  1953. if (!devpts)
  1954. tty->driver->termios_locked[idx] = NULL;
  1955. kfree(tp);
  1956. }
  1957. tty->magic = 0;
  1958. tty->driver->refcount--;
  1959. file_list_lock();
  1960. list_del_init(&tty->tty_files);
  1961. file_list_unlock();
  1962. free_tty_struct(tty);
  1963. }
  1964. /**
  1965. * release_tty - release tty structure memory
  1966. *
  1967. * Release both @tty and a possible linked partner (think pty pair),
  1968. * and decrement the refcount of the backing module.
  1969. *
  1970. * Locking:
  1971. * tty_mutex - sometimes only
  1972. * takes the file list lock internally when working on the list
  1973. * of ttys that the driver keeps.
  1974. * FIXME: should we require tty_mutex is held here ??
  1975. */
  1976. static void release_tty(struct tty_struct *tty, int idx)
  1977. {
  1978. struct tty_driver *driver = tty->driver;
  1979. if (tty->link)
  1980. release_one_tty(tty->link, idx);
  1981. release_one_tty(tty, idx);
  1982. module_put(driver->owner);
  1983. }
  1984. /*
  1985. * Even releasing the tty structures is a tricky business.. We have
  1986. * to be very careful that the structures are all released at the
  1987. * same time, as interrupts might otherwise get the wrong pointers.
  1988. *
  1989. * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
  1990. * lead to double frees or releasing memory still in use.
  1991. */
  1992. static void release_dev(struct file * filp)
  1993. {
  1994. struct tty_struct *tty, *o_tty;
  1995. int pty_master, tty_closing, o_tty_closing, do_sleep;
  1996. int devpts;
  1997. int idx;
  1998. char buf[64];
  1999. unsigned long flags;
  2000. tty = (struct tty_struct *)filp->private_data;
  2001. if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "release_dev"))
  2002. return;
  2003. check_tty_count(tty, "release_dev");
  2004. tty_fasync(-1, filp, 0);
  2005. idx = tty->index;
  2006. pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  2007. tty->driver->subtype == PTY_TYPE_MASTER);
  2008. devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
  2009. o_tty = tty->link;
  2010. #ifdef TTY_PARANOIA_CHECK
  2011. if (idx < 0 || idx >= tty->driver->num) {
  2012. printk(KERN_DEBUG "release_dev: bad idx when trying to "
  2013. "free (%s)\n", tty->name);
  2014. return;
  2015. }
  2016. if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
  2017. if (tty != tty->driver->ttys[idx]) {
  2018. printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
  2019. "for (%s)\n", idx, tty->name);
  2020. return;
  2021. }
  2022. if (tty->termios != tty->driver->termios[idx]) {
  2023. printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
  2024. "for (%s)\n",
  2025. idx, tty->name);
  2026. return;
  2027. }
  2028. if (tty->termios_locked != tty->driver->termios_locked[idx]) {
  2029. printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
  2030. "termios_locked for (%s)\n",
  2031. idx, tty->name);
  2032. return;
  2033. }
  2034. }
  2035. #endif
  2036. #ifdef TTY_DEBUG_HANGUP
  2037. printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
  2038. tty_name(tty, buf), tty->count);
  2039. #endif
  2040. #ifdef TTY_PARANOIA_CHECK
  2041. if (tty->driver->other &&
  2042. !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
  2043. if (o_tty != tty->driver->other->ttys[idx]) {
  2044. printk(KERN_DEBUG "release_dev: other->table[%d] "
  2045. "not o_tty for (%s)\n",
  2046. idx, tty->name);
  2047. return;
  2048. }
  2049. if (o_tty->termios != tty->driver->other->termios[idx]) {
  2050. printk(KERN_DEBUG "release_dev: other->termios[%d] "
  2051. "not o_termios for (%s)\n",
  2052. idx, tty->name);
  2053. return;
  2054. }
  2055. if (o_tty->termios_locked !=
  2056. tty->driver->other->termios_locked[idx]) {
  2057. printk(KERN_DEBUG "release_dev: other->termios_locked["
  2058. "%d] not o_termios_locked for (%s)\n",
  2059. idx, tty->name);
  2060. return;
  2061. }
  2062. if (o_tty->link != tty) {
  2063. printk(KERN_DEBUG "release_dev: bad pty pointers\n");
  2064. return;
  2065. }
  2066. }
  2067. #endif
  2068. if (tty->driver->close)
  2069. tty->driver->close(tty, filp);
  2070. /*
  2071. * Sanity check: if tty->count is going to zero, there shouldn't be
  2072. * any waiters on tty->read_wait or tty->write_wait. We test the
  2073. * wait queues and kick everyone out _before_ actually starting to
  2074. * close. This ensures that we won't block while releasing the tty
  2075. * structure.
  2076. *
  2077. * The test for the o_tty closing is necessary, since the master and
  2078. * slave sides may close in any order. If the slave side closes out
  2079. * first, its count will be one, since the master side holds an open.
  2080. * Thus this test wouldn't be triggered at the time the slave closes,
  2081. * so we do it now.
  2082. *
  2083. * Note that it's possible for the tty to be opened again while we're
  2084. * flushing out waiters. By recalculating the closing flags before
  2085. * each iteration we avoid any problems.
  2086. */
  2087. while (1) {
  2088. /* Guard against races with tty->count changes elsewhere and
  2089. opens on /dev/tty */
  2090. mutex_lock(&tty_mutex);
  2091. tty_closing = tty->count <= 1;
  2092. o_tty_closing = o_tty &&
  2093. (o_tty->count <= (pty_master ? 1 : 0));
  2094. do_sleep = 0;
  2095. if (tty_closing) {
  2096. if (waitqueue_active(&tty->read_wait)) {
  2097. wake_up(&tty->read_wait);
  2098. do_sleep++;
  2099. }
  2100. if (waitqueue_active(&tty->write_wait)) {
  2101. wake_up(&tty->write_wait);
  2102. do_sleep++;
  2103. }
  2104. }
  2105. if (o_tty_closing) {
  2106. if (waitqueue_active(&o_tty->read_wait)) {
  2107. wake_up(&o_tty->read_wait);
  2108. do_sleep++;
  2109. }
  2110. if (waitqueue_active(&o_tty->write_wait)) {
  2111. wake_up(&o_tty->write_wait);
  2112. do_sleep++;
  2113. }
  2114. }
  2115. if (!do_sleep)
  2116. break;
  2117. printk(KERN_WARNING "release_dev: %s: read/write wait queue "
  2118. "active!\n", tty_name(tty, buf));
  2119. mutex_unlock(&tty_mutex);
  2120. schedule();
  2121. }
  2122. /*
  2123. * The closing flags are now consistent with the open counts on
  2124. * both sides, and we've completed the last operation that could
  2125. * block, so it's safe to proceed with closing.
  2126. */
  2127. if (pty_master) {
  2128. if (--o_tty->count < 0) {
  2129. printk(KERN_WARNING "release_dev: bad pty slave count "
  2130. "(%d) for %s\n",
  2131. o_tty->count, tty_name(o_tty, buf));
  2132. o_tty->count = 0;
  2133. }
  2134. }
  2135. if (--tty->count < 0) {
  2136. printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
  2137. tty->count, tty_name(tty, buf));
  2138. tty->count = 0;
  2139. }
  2140. /*
  2141. * We've decremented tty->count, so we need to remove this file
  2142. * descriptor off the tty->tty_files list; this serves two
  2143. * purposes:
  2144. * - check_tty_count sees the correct number of file descriptors
  2145. * associated with this tty.
  2146. * - do_tty_hangup no longer sees this file descriptor as
  2147. * something that needs to be handled for hangups.
  2148. */
  2149. file_kill(filp);
  2150. filp->private_data = NULL;
  2151. /*
  2152. * Perform some housekeeping before deciding whether to return.
  2153. *
  2154. * Set the TTY_CLOSING flag if this was the last open. In the
  2155. * case of a pty we may have to wait around for the other side
  2156. * to close, and TTY_CLOSING makes sure we can't be reopened.
  2157. */
  2158. if(tty_closing)
  2159. set_bit(TTY_CLOSING, &tty->flags);
  2160. if(o_tty_closing)
  2161. set_bit(TTY_CLOSING, &o_tty->flags);
  2162. /*
  2163. * If _either_ side is closing, make sure there aren't any
  2164. * processes that still think tty or o_tty is their controlling
  2165. * tty.
  2166. */
  2167. if (tty_closing || o_tty_closing) {
  2168. read_lock(&tasklist_lock);
  2169. session_clear_tty(tty->session);
  2170. if (o_tty)
  2171. session_clear_tty(o_tty->session);
  2172. read_unlock(&tasklist_lock);
  2173. }
  2174. mutex_unlock(&tty_mutex);
  2175. /* check whether both sides are closing ... */
  2176. if (!tty_closing || (o_tty && !o_tty_closing))
  2177. return;
  2178. #ifdef TTY_DEBUG_HANGUP
  2179. printk(KERN_DEBUG "freeing tty structure...");
  2180. #endif
  2181. /*
  2182. * Prevent flush_to_ldisc() from rescheduling the work for later. Then
  2183. * kill any delayed work. As this is the final close it does not
  2184. * race with the set_ldisc code path.
  2185. */
  2186. clear_bit(TTY_LDISC, &tty->flags);
  2187. cancel_delayed_work(&tty->buf.work);
  2188. /*
  2189. * Wait for ->hangup_work and ->buf.work handlers to terminate
  2190. */
  2191. flush_scheduled_work();
  2192. /*
  2193. * Wait for any short term users (we know they are just driver
  2194. * side waiters as the file is closing so user count on the file
  2195. * side is zero.
  2196. */
  2197. spin_lock_irqsave(&tty_ldisc_lock, flags);
  2198. while(tty->ldisc.refcount)
  2199. {
  2200. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  2201. wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0);
  2202. spin_lock_irqsave(&tty_ldisc_lock, flags);
  2203. }
  2204. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  2205. /*
  2206. * Shutdown the current line discipline, and reset it to N_TTY.
  2207. * N.B. why reset ldisc when we're releasing the memory??
  2208. *
  2209. * FIXME: this MUST get fixed for the new reflocking
  2210. */
  2211. if (tty->ldisc.close)
  2212. (tty->ldisc.close)(tty);
  2213. tty_ldisc_put(tty->ldisc.num);
  2214. /*
  2215. * Switch the line discipline back
  2216. */
  2217. tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
  2218. tty_set_termios_ldisc(tty,N_TTY);
  2219. if (o_tty) {
  2220. /* FIXME: could o_tty be in setldisc here ? */
  2221. clear_bit(TTY_LDISC, &o_tty->flags);
  2222. if (o_tty->ldisc.close)
  2223. (o_tty->ldisc.close)(o_tty);
  2224. tty_ldisc_put(o_tty->ldisc.num);
  2225. tty_ldisc_assign(o_tty, tty_ldisc_get(N_TTY));
  2226. tty_set_termios_ldisc(o_tty,N_TTY);
  2227. }
  2228. /*
  2229. * The release_tty function takes care of the details of clearing
  2230. * the slots and preserving the termios structure.
  2231. */
  2232. release_tty(tty, idx);
  2233. #ifdef CONFIG_UNIX98_PTYS
  2234. /* Make this pty number available for reallocation */
  2235. if (devpts) {
  2236. down(&allocated_ptys_lock);
  2237. idr_remove(&allocated_ptys, idx);
  2238. up(&allocated_ptys_lock);
  2239. }
  2240. #endif
  2241. }
  2242. /**
  2243. * tty_open - open a tty device
  2244. * @inode: inode of device file
  2245. * @filp: file pointer to tty
  2246. *
  2247. * tty_open and tty_release keep up the tty count that contains the
  2248. * number of opens done on a tty. We cannot use the inode-count, as
  2249. * different inodes might point to the same tty.
  2250. *
  2251. * Open-counting is needed for pty masters, as well as for keeping
  2252. * track of serial lines: DTR is dropped when the last close happens.
  2253. * (This is not done solely through tty->count, now. - Ted 1/27/92)
  2254. *
  2255. * The termios state of a pty is reset on first open so that
  2256. * settings don't persist across reuse.
  2257. *
  2258. * Locking: tty_mutex protects tty, get_tty_driver and init_dev work.
  2259. * tty->count should protect the rest.
  2260. * ->siglock protects ->signal/->sighand
  2261. */
  2262. static int tty_open(struct inode * inode, struct file * filp)
  2263. {
  2264. struct tty_struct *tty;
  2265. int noctty, retval;
  2266. struct tty_driver *driver;
  2267. int index;
  2268. dev_t device = inode->i_rdev;
  2269. unsigned short saved_flags = filp->f_flags;
  2270. nonseekable_open(inode, filp);
  2271. retry_open:
  2272. noctty = filp->f_flags & O_NOCTTY;
  2273. index = -1;
  2274. retval = 0;
  2275. mutex_lock(&tty_mutex);
  2276. if (device == MKDEV(TTYAUX_MAJOR,0)) {
  2277. tty = get_current_tty();
  2278. if (!tty) {
  2279. mutex_unlock(&tty_mutex);
  2280. return -ENXIO;
  2281. }
  2282. driver = tty->driver;
  2283. index = tty->index;
  2284. filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
  2285. /* noctty = 1; */
  2286. goto got_driver;
  2287. }
  2288. #ifdef CONFIG_VT
  2289. if (device == MKDEV(TTY_MAJOR,0)) {
  2290. extern struct tty_driver *console_driver;
  2291. driver = console_driver;
  2292. index = fg_console;
  2293. noctty = 1;
  2294. goto got_driver;
  2295. }
  2296. #endif
  2297. if (device == MKDEV(TTYAUX_MAJOR,1)) {
  2298. driver = console_device(&index);
  2299. if (driver) {
  2300. /* Don't let /dev/console block */
  2301. filp->f_flags |= O_NONBLOCK;
  2302. noctty = 1;
  2303. goto got_driver;
  2304. }
  2305. mutex_unlock(&tty_mutex);
  2306. return -ENODEV;
  2307. }
  2308. driver = get_tty_driver(device, &index);
  2309. if (!driver) {
  2310. mutex_unlock(&tty_mutex);
  2311. return -ENODEV;
  2312. }
  2313. got_driver:
  2314. retval = init_dev(driver, index, &tty);
  2315. mutex_unlock(&tty_mutex);
  2316. if (retval)
  2317. return retval;
  2318. filp->private_data = tty;
  2319. file_move(filp, &tty->tty_files);
  2320. check_tty_count(tty, "tty_open");
  2321. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  2322. tty->driver->subtype == PTY_TYPE_MASTER)
  2323. noctty = 1;
  2324. #ifdef TTY_DEBUG_HANGUP
  2325. printk(KERN_DEBUG "opening %s...", tty->name);
  2326. #endif
  2327. if (!retval) {
  2328. if (tty->driver->open)
  2329. retval = tty->driver->open(tty, filp);
  2330. else
  2331. retval = -ENODEV;
  2332. }
  2333. filp->f_flags = saved_flags;
  2334. if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
  2335. retval = -EBUSY;
  2336. if (retval) {
  2337. #ifdef TTY_DEBUG_HANGUP
  2338. printk(KERN_DEBUG "error %d in opening %s...", retval,
  2339. tty->name);
  2340. #endif
  2341. release_dev(filp);
  2342. if (retval != -ERESTARTSYS)
  2343. return retval;
  2344. if (signal_pending(current))
  2345. return retval;
  2346. schedule();
  2347. /*
  2348. * Need to reset f_op in case a hangup happened.
  2349. */
  2350. if (filp->f_op == &hung_up_tty_fops)
  2351. filp->f_op = &tty_fops;
  2352. goto retry_open;
  2353. }
  2354. mutex_lock(&tty_mutex);
  2355. spin_lock_irq(&current->sighand->siglock);
  2356. if (!noctty &&
  2357. current->signal->leader &&
  2358. !current->signal->tty &&
  2359. tty->session == NULL)
  2360. __proc_set_tty(current, tty);
  2361. spin_unlock_irq(&current->sighand->siglock);
  2362. mutex_unlock(&tty_mutex);
  2363. return 0;
  2364. }
  2365. #ifdef CONFIG_UNIX98_PTYS
  2366. /**
  2367. * ptmx_open - open a unix 98 pty master
  2368. * @inode: inode of device file
  2369. * @filp: file pointer to tty
  2370. *
  2371. * Allocate a unix98 pty master device from the ptmx driver.
  2372. *
  2373. * Locking: tty_mutex protects theinit_dev work. tty->count should
  2374. protect the rest.
  2375. * allocated_ptys_lock handles the list of free pty numbers
  2376. */
  2377. static int ptmx_open(struct inode * inode, struct file * filp)
  2378. {
  2379. struct tty_struct *tty;
  2380. int retval;
  2381. int index;
  2382. int idr_ret;
  2383. nonseekable_open(inode, filp);
  2384. /* find a device that is not in use. */
  2385. down(&allocated_ptys_lock);
  2386. if (!idr_pre_get(&allocated_ptys, GFP_KERNEL)) {
  2387. up(&allocated_ptys_lock);
  2388. return -ENOMEM;
  2389. }
  2390. idr_ret = idr_get_new(&allocated_ptys, NULL, &index);
  2391. if (idr_ret < 0) {
  2392. up(&allocated_ptys_lock);
  2393. if (idr_ret == -EAGAIN)
  2394. return -ENOMEM;
  2395. return -EIO;
  2396. }
  2397. if (index >= pty_limit) {
  2398. idr_remove(&allocated_ptys, index);
  2399. up(&allocated_ptys_lock);
  2400. return -EIO;
  2401. }
  2402. up(&allocated_ptys_lock);
  2403. mutex_lock(&tty_mutex);
  2404. retval = init_dev(ptm_driver, index, &tty);
  2405. mutex_unlock(&tty_mutex);
  2406. if (retval)
  2407. goto out;
  2408. set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
  2409. filp->private_data = tty;
  2410. file_move(filp, &tty->tty_files);
  2411. retval = -ENOMEM;
  2412. if (devpts_pty_new(tty->link))
  2413. goto out1;
  2414. check_tty_count(tty, "tty_open");
  2415. retval = ptm_driver->open(tty, filp);
  2416. if (!retval)
  2417. return 0;
  2418. out1:
  2419. release_dev(filp);
  2420. return retval;
  2421. out:
  2422. down(&allocated_ptys_lock);
  2423. idr_remove(&allocated_ptys, index);
  2424. up(&allocated_ptys_lock);
  2425. return retval;
  2426. }
  2427. #endif
  2428. /**
  2429. * tty_release - vfs callback for close
  2430. * @inode: inode of tty
  2431. * @filp: file pointer for handle to tty
  2432. *
  2433. * Called the last time each file handle is closed that references
  2434. * this tty. There may however be several such references.
  2435. *
  2436. * Locking:
  2437. * Takes bkl. See release_dev
  2438. */
  2439. static int tty_release(struct inode * inode, struct file * filp)
  2440. {
  2441. lock_kernel();
  2442. release_dev(filp);
  2443. unlock_kernel();
  2444. return 0;
  2445. }
  2446. /**
  2447. * tty_poll - check tty status
  2448. * @filp: file being polled
  2449. * @wait: poll wait structures to update
  2450. *
  2451. * Call the line discipline polling method to obtain the poll
  2452. * status of the device.
  2453. *
  2454. * Locking: locks called line discipline but ldisc poll method
  2455. * may be re-entered freely by other callers.
  2456. */
  2457. static unsigned int tty_poll(struct file * filp, poll_table * wait)
  2458. {
  2459. struct tty_struct * tty;
  2460. struct tty_ldisc *ld;
  2461. int ret = 0;
  2462. tty = (struct tty_struct *)filp->private_data;
  2463. if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
  2464. return 0;
  2465. ld = tty_ldisc_ref_wait(tty);
  2466. if (ld->poll)
  2467. ret = (ld->poll)(tty, filp, wait);
  2468. tty_ldisc_deref(ld);
  2469. return ret;
  2470. }
  2471. static int tty_fasync(int fd, struct file * filp, int on)
  2472. {
  2473. struct tty_struct * tty;
  2474. int retval;
  2475. tty = (struct tty_struct *)filp->private_data;
  2476. if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
  2477. return 0;
  2478. retval = fasync_helper(fd, filp, on, &tty->fasync);
  2479. if (retval <= 0)
  2480. return retval;
  2481. if (on) {
  2482. enum pid_type type;
  2483. struct pid *pid;
  2484. if (!waitqueue_active(&tty->read_wait))
  2485. tty->minimum_to_wake = 1;
  2486. if (tty->pgrp) {
  2487. pid = tty->pgrp;
  2488. type = PIDTYPE_PGID;
  2489. } else {
  2490. pid = task_pid(current);
  2491. type = PIDTYPE_PID;
  2492. }
  2493. retval = __f_setown(filp, pid, type, 0);
  2494. if (retval)
  2495. return retval;
  2496. } else {
  2497. if (!tty->fasync && !waitqueue_active(&tty->read_wait))
  2498. tty->minimum_to_wake = N_TTY_BUF_SIZE;
  2499. }
  2500. return 0;
  2501. }
  2502. /**
  2503. * tiocsti - fake input character
  2504. * @tty: tty to fake input into
  2505. * @p: pointer to character
  2506. *
  2507. * Fake input to a tty device. Does the neccessary locking and
  2508. * input management.
  2509. *
  2510. * FIXME: does not honour flow control ??
  2511. *
  2512. * Locking:
  2513. * Called functions take tty_ldisc_lock
  2514. * current->signal->tty check is safe without locks
  2515. *
  2516. * FIXME: may race normal receive processing
  2517. */
  2518. static int tiocsti(struct tty_struct *tty, char __user *p)
  2519. {
  2520. char ch, mbz = 0;
  2521. struct tty_ldisc *ld;
  2522. if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
  2523. return -EPERM;
  2524. if (get_user(ch, p))
  2525. return -EFAULT;
  2526. ld = tty_ldisc_ref_wait(tty);
  2527. ld->receive_buf(tty, &ch, &mbz, 1);
  2528. tty_ldisc_deref(ld);
  2529. return 0;
  2530. }
  2531. /**
  2532. * tiocgwinsz - implement window query ioctl
  2533. * @tty; tty
  2534. * @arg: user buffer for result
  2535. *
  2536. * Copies the kernel idea of the window size into the user buffer.
  2537. *
  2538. * Locking: tty->termios_mutex is taken to ensure the winsize data
  2539. * is consistent.
  2540. */
  2541. static int tiocgwinsz(struct tty_struct *tty, struct winsize __user * arg)
  2542. {
  2543. int err;
  2544. mutex_lock(&tty->termios_mutex);
  2545. err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
  2546. mutex_unlock(&tty->termios_mutex);
  2547. return err ? -EFAULT: 0;
  2548. }
  2549. /**
  2550. * tiocswinsz - implement window size set ioctl
  2551. * @tty; tty
  2552. * @arg: user buffer for result
  2553. *
  2554. * Copies the user idea of the window size to the kernel. Traditionally
  2555. * this is just advisory information but for the Linux console it
  2556. * actually has driver level meaning and triggers a VC resize.
  2557. *
  2558. * Locking:
  2559. * Called function use the console_sem is used to ensure we do
  2560. * not try and resize the console twice at once.
  2561. * The tty->termios_mutex is used to ensure we don't double
  2562. * resize and get confused. Lock order - tty->termios_mutex before
  2563. * console sem
  2564. */
  2565. static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
  2566. struct winsize __user * arg)
  2567. {
  2568. struct winsize tmp_ws;
  2569. if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
  2570. return -EFAULT;
  2571. mutex_lock(&tty->termios_mutex);
  2572. if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
  2573. goto done;
  2574. #ifdef CONFIG_VT
  2575. if (tty->driver->type == TTY_DRIVER_TYPE_CONSOLE) {
  2576. if (vc_lock_resize(tty->driver_data, tmp_ws.ws_col,
  2577. tmp_ws.ws_row)) {
  2578. mutex_unlock(&tty->termios_mutex);
  2579. return -ENXIO;
  2580. }
  2581. }
  2582. #endif
  2583. if (tty->pgrp)
  2584. kill_pgrp(tty->pgrp, SIGWINCH, 1);
  2585. if ((real_tty->pgrp != tty->pgrp) && real_tty->pgrp)
  2586. kill_pgrp(real_tty->pgrp, SIGWINCH, 1);
  2587. tty->winsize = tmp_ws;
  2588. real_tty->winsize = tmp_ws;
  2589. done:
  2590. mutex_unlock(&tty->termios_mutex);
  2591. return 0;
  2592. }
  2593. /**
  2594. * tioccons - allow admin to move logical console
  2595. * @file: the file to become console
  2596. *
  2597. * Allow the adminstrator to move the redirected console device
  2598. *
  2599. * Locking: uses redirect_lock to guard the redirect information
  2600. */
  2601. static int tioccons(struct file *file)
  2602. {
  2603. if (!capable(CAP_SYS_ADMIN))
  2604. return -EPERM;
  2605. if (file->f_op->write == redirected_tty_write) {
  2606. struct file *f;
  2607. spin_lock(&redirect_lock);
  2608. f = redirect;
  2609. redirect = NULL;
  2610. spin_unlock(&redirect_lock);
  2611. if (f)
  2612. fput(f);
  2613. return 0;
  2614. }
  2615. spin_lock(&redirect_lock);
  2616. if (redirect) {
  2617. spin_unlock(&redirect_lock);
  2618. return -EBUSY;
  2619. }
  2620. get_file(file);
  2621. redirect = file;
  2622. spin_unlock(&redirect_lock);
  2623. return 0;
  2624. }
  2625. /**
  2626. * fionbio - non blocking ioctl
  2627. * @file: file to set blocking value
  2628. * @p: user parameter
  2629. *
  2630. * Historical tty interfaces had a blocking control ioctl before
  2631. * the generic functionality existed. This piece of history is preserved
  2632. * in the expected tty API of posix OS's.
  2633. *
  2634. * Locking: none, the open fle handle ensures it won't go away.
  2635. */
  2636. static int fionbio(struct file *file, int __user *p)
  2637. {
  2638. int nonblock;
  2639. if (get_user(nonblock, p))
  2640. return -EFAULT;
  2641. if (nonblock)
  2642. file->f_flags |= O_NONBLOCK;
  2643. else
  2644. file->f_flags &= ~O_NONBLOCK;
  2645. return 0;
  2646. }
  2647. /**
  2648. * tiocsctty - set controlling tty
  2649. * @tty: tty structure
  2650. * @arg: user argument
  2651. *
  2652. * This ioctl is used to manage job control. It permits a session
  2653. * leader to set this tty as the controlling tty for the session.
  2654. *
  2655. * Locking:
  2656. * Takes tty_mutex() to protect tty instance
  2657. * Takes tasklist_lock internally to walk sessions
  2658. * Takes ->siglock() when updating signal->tty
  2659. */
  2660. static int tiocsctty(struct tty_struct *tty, int arg)
  2661. {
  2662. int ret = 0;
  2663. if (current->signal->leader && (task_session(current) == tty->session))
  2664. return ret;
  2665. mutex_lock(&tty_mutex);
  2666. /*
  2667. * The process must be a session leader and
  2668. * not have a controlling tty already.
  2669. */
  2670. if (!current->signal->leader || current->signal->tty) {
  2671. ret = -EPERM;
  2672. goto unlock;
  2673. }
  2674. if (tty->session) {
  2675. /*
  2676. * This tty is already the controlling
  2677. * tty for another session group!
  2678. */
  2679. if ((arg == 1) && capable(CAP_SYS_ADMIN)) {
  2680. /*
  2681. * Steal it away
  2682. */
  2683. read_lock(&tasklist_lock);
  2684. session_clear_tty(tty->session);
  2685. read_unlock(&tasklist_lock);
  2686. } else {
  2687. ret = -EPERM;
  2688. goto unlock;
  2689. }
  2690. }
  2691. proc_set_tty(current, tty);
  2692. unlock:
  2693. mutex_unlock(&tty_mutex);
  2694. return ret;
  2695. }
  2696. /**
  2697. * tiocgpgrp - get process group
  2698. * @tty: tty passed by user
  2699. * @real_tty: tty side of the tty pased by the user if a pty else the tty
  2700. * @p: returned pid
  2701. *
  2702. * Obtain the process group of the tty. If there is no process group
  2703. * return an error.
  2704. *
  2705. * Locking: none. Reference to current->signal->tty is safe.
  2706. */
  2707. static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  2708. {
  2709. /*
  2710. * (tty == real_tty) is a cheap way of
  2711. * testing if the tty is NOT a master pty.
  2712. */
  2713. if (tty == real_tty && current->signal->tty != real_tty)
  2714. return -ENOTTY;
  2715. return put_user(pid_nr(real_tty->pgrp), p);
  2716. }
  2717. /**
  2718. * tiocspgrp - attempt to set process group
  2719. * @tty: tty passed by user
  2720. * @real_tty: tty side device matching tty passed by user
  2721. * @p: pid pointer
  2722. *
  2723. * Set the process group of the tty to the session passed. Only
  2724. * permitted where the tty session is our session.
  2725. *
  2726. * Locking: None
  2727. */
  2728. static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  2729. {
  2730. struct pid *pgrp;
  2731. pid_t pgrp_nr;
  2732. int retval = tty_check_change(real_tty);
  2733. if (retval == -EIO)
  2734. return -ENOTTY;
  2735. if (retval)
  2736. return retval;
  2737. if (!current->signal->tty ||
  2738. (current->signal->tty != real_tty) ||
  2739. (real_tty->session != task_session(current)))
  2740. return -ENOTTY;
  2741. if (get_user(pgrp_nr, p))
  2742. return -EFAULT;
  2743. if (pgrp_nr < 0)
  2744. return -EINVAL;
  2745. rcu_read_lock();
  2746. pgrp = find_pid(pgrp_nr);
  2747. retval = -ESRCH;
  2748. if (!pgrp)
  2749. goto out_unlock;
  2750. retval = -EPERM;
  2751. if (session_of_pgrp(pgrp) != task_session(current))
  2752. goto out_unlock;
  2753. retval = 0;
  2754. put_pid(real_tty->pgrp);
  2755. real_tty->pgrp = get_pid(pgrp);
  2756. out_unlock:
  2757. rcu_read_unlock();
  2758. return retval;
  2759. }
  2760. /**
  2761. * tiocgsid - get session id
  2762. * @tty: tty passed by user
  2763. * @real_tty: tty side of the tty pased by the user if a pty else the tty
  2764. * @p: pointer to returned session id
  2765. *
  2766. * Obtain the session id of the tty. If there is no session
  2767. * return an error.
  2768. *
  2769. * Locking: none. Reference to current->signal->tty is safe.
  2770. */
  2771. static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  2772. {
  2773. /*
  2774. * (tty == real_tty) is a cheap way of
  2775. * testing if the tty is NOT a master pty.
  2776. */
  2777. if (tty == real_tty && current->signal->tty != real_tty)
  2778. return -ENOTTY;
  2779. if (!real_tty->session)
  2780. return -ENOTTY;
  2781. return put_user(pid_nr(real_tty->session), p);
  2782. }
  2783. /**
  2784. * tiocsetd - set line discipline
  2785. * @tty: tty device
  2786. * @p: pointer to user data
  2787. *
  2788. * Set the line discipline according to user request.
  2789. *
  2790. * Locking: see tty_set_ldisc, this function is just a helper
  2791. */
  2792. static int tiocsetd(struct tty_struct *tty, int __user *p)
  2793. {
  2794. int ldisc;
  2795. if (get_user(ldisc, p))
  2796. return -EFAULT;
  2797. return tty_set_ldisc(tty, ldisc);
  2798. }
  2799. /**
  2800. * send_break - performed time break
  2801. * @tty: device to break on
  2802. * @duration: timeout in mS
  2803. *
  2804. * Perform a timed break on hardware that lacks its own driver level
  2805. * timed break functionality.
  2806. *
  2807. * Locking:
  2808. * atomic_write_lock serializes
  2809. *
  2810. */
  2811. static int send_break(struct tty_struct *tty, unsigned int duration)
  2812. {
  2813. if (mutex_lock_interruptible(&tty->atomic_write_lock))
  2814. return -EINTR;
  2815. tty->driver->break_ctl(tty, -1);
  2816. if (!signal_pending(current)) {
  2817. msleep_interruptible(duration);
  2818. }
  2819. tty->driver->break_ctl(tty, 0);
  2820. mutex_unlock(&tty->atomic_write_lock);
  2821. if (signal_pending(current))
  2822. return -EINTR;
  2823. return 0;
  2824. }
  2825. /**
  2826. * tiocmget - get modem status
  2827. * @tty: tty device
  2828. * @file: user file pointer
  2829. * @p: pointer to result
  2830. *
  2831. * Obtain the modem status bits from the tty driver if the feature
  2832. * is supported. Return -EINVAL if it is not available.
  2833. *
  2834. * Locking: none (up to the driver)
  2835. */
  2836. static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
  2837. {
  2838. int retval = -EINVAL;
  2839. if (tty->driver->tiocmget) {
  2840. retval = tty->driver->tiocmget(tty, file);
  2841. if (retval >= 0)
  2842. retval = put_user(retval, p);
  2843. }
  2844. return retval;
  2845. }
  2846. /**
  2847. * tiocmset - set modem status
  2848. * @tty: tty device
  2849. * @file: user file pointer
  2850. * @cmd: command - clear bits, set bits or set all
  2851. * @p: pointer to desired bits
  2852. *
  2853. * Set the modem status bits from the tty driver if the feature
  2854. * is supported. Return -EINVAL if it is not available.
  2855. *
  2856. * Locking: none (up to the driver)
  2857. */
  2858. static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
  2859. unsigned __user *p)
  2860. {
  2861. int retval = -EINVAL;
  2862. if (tty->driver->tiocmset) {
  2863. unsigned int set, clear, val;
  2864. retval = get_user(val, p);
  2865. if (retval)
  2866. return retval;
  2867. set = clear = 0;
  2868. switch (cmd) {
  2869. case TIOCMBIS:
  2870. set = val;
  2871. break;
  2872. case TIOCMBIC:
  2873. clear = val;
  2874. break;
  2875. case TIOCMSET:
  2876. set = val;
  2877. clear = ~val;
  2878. break;
  2879. }
  2880. set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
  2881. clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
  2882. retval = tty->driver->tiocmset(tty, file, set, clear);
  2883. }
  2884. return retval;
  2885. }
  2886. /*
  2887. * Split this up, as gcc can choke on it otherwise..
  2888. */
  2889. int tty_ioctl(struct inode * inode, struct file * file,
  2890. unsigned int cmd, unsigned long arg)
  2891. {
  2892. struct tty_struct *tty, *real_tty;
  2893. void __user *p = (void __user *)arg;
  2894. int retval;
  2895. struct tty_ldisc *ld;
  2896. tty = (struct tty_struct *)file->private_data;
  2897. if (tty_paranoia_check(tty, inode, "tty_ioctl"))
  2898. return -EINVAL;
  2899. /* CHECKME: is this safe as one end closes ? */
  2900. real_tty = tty;
  2901. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  2902. tty->driver->subtype == PTY_TYPE_MASTER)
  2903. real_tty = tty->link;
  2904. /*
  2905. * Break handling by driver
  2906. */
  2907. if (!tty->driver->break_ctl) {
  2908. switch(cmd) {
  2909. case TIOCSBRK:
  2910. case TIOCCBRK:
  2911. if (tty->driver->ioctl)
  2912. return tty->driver->ioctl(tty, file, cmd, arg);
  2913. return -EINVAL;
  2914. /* These two ioctl's always return success; even if */
  2915. /* the driver doesn't support them. */
  2916. case TCSBRK:
  2917. case TCSBRKP:
  2918. if (!tty->driver->ioctl)
  2919. return 0;
  2920. retval = tty->driver->ioctl(tty, file, cmd, arg);
  2921. if (retval == -ENOIOCTLCMD)
  2922. retval = 0;
  2923. return retval;
  2924. }
  2925. }
  2926. /*
  2927. * Factor out some common prep work
  2928. */
  2929. switch (cmd) {
  2930. case TIOCSETD:
  2931. case TIOCSBRK:
  2932. case TIOCCBRK:
  2933. case TCSBRK:
  2934. case TCSBRKP:
  2935. retval = tty_check_change(tty);
  2936. if (retval)
  2937. return retval;
  2938. if (cmd != TIOCCBRK) {
  2939. tty_wait_until_sent(tty, 0);
  2940. if (signal_pending(current))
  2941. return -EINTR;
  2942. }
  2943. break;
  2944. }
  2945. switch (cmd) {
  2946. case TIOCSTI:
  2947. return tiocsti(tty, p);
  2948. case TIOCGWINSZ:
  2949. return tiocgwinsz(tty, p);
  2950. case TIOCSWINSZ:
  2951. return tiocswinsz(tty, real_tty, p);
  2952. case TIOCCONS:
  2953. return real_tty!=tty ? -EINVAL : tioccons(file);
  2954. case FIONBIO:
  2955. return fionbio(file, p);
  2956. case TIOCEXCL:
  2957. set_bit(TTY_EXCLUSIVE, &tty->flags);
  2958. return 0;
  2959. case TIOCNXCL:
  2960. clear_bit(TTY_EXCLUSIVE, &tty->flags);
  2961. return 0;
  2962. case TIOCNOTTY:
  2963. if (current->signal->tty != tty)
  2964. return -ENOTTY;
  2965. no_tty();
  2966. return 0;
  2967. case TIOCSCTTY:
  2968. return tiocsctty(tty, arg);
  2969. case TIOCGPGRP:
  2970. return tiocgpgrp(tty, real_tty, p);
  2971. case TIOCSPGRP:
  2972. return tiocspgrp(tty, real_tty, p);
  2973. case TIOCGSID:
  2974. return tiocgsid(tty, real_tty, p);
  2975. case TIOCGETD:
  2976. /* FIXME: check this is ok */
  2977. return put_user(tty->ldisc.num, (int __user *)p);
  2978. case TIOCSETD:
  2979. return tiocsetd(tty, p);
  2980. #ifdef CONFIG_VT
  2981. case TIOCLINUX:
  2982. return tioclinux(tty, arg);
  2983. #endif
  2984. /*
  2985. * Break handling
  2986. */
  2987. case TIOCSBRK: /* Turn break on, unconditionally */
  2988. tty->driver->break_ctl(tty, -1);
  2989. return 0;
  2990. case TIOCCBRK: /* Turn break off, unconditionally */
  2991. tty->driver->break_ctl(tty, 0);
  2992. return 0;
  2993. case TCSBRK: /* SVID version: non-zero arg --> no break */
  2994. /* non-zero arg means wait for all output data
  2995. * to be sent (performed above) but don't send break.
  2996. * This is used by the tcdrain() termios function.
  2997. */
  2998. if (!arg)
  2999. return send_break(tty, 250);
  3000. return 0;
  3001. case TCSBRKP: /* support for POSIX tcsendbreak() */
  3002. return send_break(tty, arg ? arg*100 : 250);
  3003. case TIOCMGET:
  3004. return tty_tiocmget(tty, file, p);
  3005. case TIOCMSET:
  3006. case TIOCMBIC:
  3007. case TIOCMBIS:
  3008. return tty_tiocmset(tty, file, cmd, p);
  3009. case TCFLSH:
  3010. switch (arg) {
  3011. case TCIFLUSH:
  3012. case TCIOFLUSH:
  3013. /* flush tty buffer and allow ldisc to process ioctl */
  3014. tty_buffer_flush(tty);
  3015. break;
  3016. }
  3017. break;
  3018. }
  3019. if (tty->driver->ioctl) {
  3020. retval = (tty->driver->ioctl)(tty, file, cmd, arg);
  3021. if (retval != -ENOIOCTLCMD)
  3022. return retval;
  3023. }
  3024. ld = tty_ldisc_ref_wait(tty);
  3025. retval = -EINVAL;
  3026. if (ld->ioctl) {
  3027. retval = ld->ioctl(tty, file, cmd, arg);
  3028. if (retval == -ENOIOCTLCMD)
  3029. retval = -EINVAL;
  3030. }
  3031. tty_ldisc_deref(ld);
  3032. return retval;
  3033. }
  3034. #ifdef CONFIG_COMPAT
  3035. static long tty_compat_ioctl(struct file * file, unsigned int cmd,
  3036. unsigned long arg)
  3037. {
  3038. struct inode *inode = file->f_dentry->d_inode;
  3039. struct tty_struct *tty = file->private_data;
  3040. struct tty_ldisc *ld;
  3041. int retval = -ENOIOCTLCMD;
  3042. if (tty_paranoia_check(tty, inode, "tty_ioctl"))
  3043. return -EINVAL;
  3044. if (tty->driver->compat_ioctl) {
  3045. retval = (tty->driver->compat_ioctl)(tty, file, cmd, arg);
  3046. if (retval != -ENOIOCTLCMD)
  3047. return retval;
  3048. }
  3049. ld = tty_ldisc_ref_wait(tty);
  3050. if (ld->compat_ioctl)
  3051. retval = ld->compat_ioctl(tty, file, cmd, arg);
  3052. tty_ldisc_deref(ld);
  3053. return retval;
  3054. }
  3055. #endif
  3056. /*
  3057. * This implements the "Secure Attention Key" --- the idea is to
  3058. * prevent trojan horses by killing all processes associated with this
  3059. * tty when the user hits the "Secure Attention Key". Required for
  3060. * super-paranoid applications --- see the Orange Book for more details.
  3061. *
  3062. * This code could be nicer; ideally it should send a HUP, wait a few
  3063. * seconds, then send a INT, and then a KILL signal. But you then
  3064. * have to coordinate with the init process, since all processes associated
  3065. * with the current tty must be dead before the new getty is allowed
  3066. * to spawn.
  3067. *
  3068. * Now, if it would be correct ;-/ The current code has a nasty hole -
  3069. * it doesn't catch files in flight. We may send the descriptor to ourselves
  3070. * via AF_UNIX socket, close it and later fetch from socket. FIXME.
  3071. *
  3072. * Nasty bug: do_SAK is being called in interrupt context. This can
  3073. * deadlock. We punt it up to process context. AKPM - 16Mar2001
  3074. */
  3075. void __do_SAK(struct tty_struct *tty)
  3076. {
  3077. #ifdef TTY_SOFT_SAK
  3078. tty_hangup(tty);
  3079. #else
  3080. struct task_struct *g, *p;
  3081. struct pid *session;
  3082. int i;
  3083. struct file *filp;
  3084. struct fdtable *fdt;
  3085. if (!tty)
  3086. return;
  3087. session = tty->session;
  3088. tty_ldisc_flush(tty);
  3089. if (tty->driver->flush_buffer)
  3090. tty->driver->flush_buffer(tty);
  3091. read_lock(&tasklist_lock);
  3092. /* Kill the entire session */
  3093. do_each_pid_task(session, PIDTYPE_SID, p) {
  3094. printk(KERN_NOTICE "SAK: killed process %d"
  3095. " (%s): process_session(p)==tty->session\n",
  3096. p->pid, p->comm);
  3097. send_sig(SIGKILL, p, 1);
  3098. } while_each_pid_task(session, PIDTYPE_SID, p);
  3099. /* Now kill any processes that happen to have the
  3100. * tty open.
  3101. */
  3102. do_each_thread(g, p) {
  3103. if (p->signal->tty == tty) {
  3104. printk(KERN_NOTICE "SAK: killed process %d"
  3105. " (%s): process_session(p)==tty->session\n",
  3106. p->pid, p->comm);
  3107. send_sig(SIGKILL, p, 1);
  3108. continue;
  3109. }
  3110. task_lock(p);
  3111. if (p->files) {
  3112. /*
  3113. * We don't take a ref to the file, so we must
  3114. * hold ->file_lock instead.
  3115. */
  3116. spin_lock(&p->files->file_lock);
  3117. fdt = files_fdtable(p->files);
  3118. for (i=0; i < fdt->max_fds; i++) {
  3119. filp = fcheck_files(p->files, i);
  3120. if (!filp)
  3121. continue;
  3122. if (filp->f_op->read == tty_read &&
  3123. filp->private_data == tty) {
  3124. printk(KERN_NOTICE "SAK: killed process %d"
  3125. " (%s): fd#%d opened to the tty\n",
  3126. p->pid, p->comm, i);
  3127. force_sig(SIGKILL, p);
  3128. break;
  3129. }
  3130. }
  3131. spin_unlock(&p->files->file_lock);
  3132. }
  3133. task_unlock(p);
  3134. } while_each_thread(g, p);
  3135. read_unlock(&tasklist_lock);
  3136. #endif
  3137. }
  3138. static void do_SAK_work(struct work_struct *work)
  3139. {
  3140. struct tty_struct *tty =
  3141. container_of(work, struct tty_struct, SAK_work);
  3142. __do_SAK(tty);
  3143. }
  3144. /*
  3145. * The tq handling here is a little racy - tty->SAK_work may already be queued.
  3146. * Fortunately we don't need to worry, because if ->SAK_work is already queued,
  3147. * the values which we write to it will be identical to the values which it
  3148. * already has. --akpm
  3149. */
  3150. void do_SAK(struct tty_struct *tty)
  3151. {
  3152. if (!tty)
  3153. return;
  3154. schedule_work(&tty->SAK_work);
  3155. }
  3156. EXPORT_SYMBOL(do_SAK);
  3157. /**
  3158. * flush_to_ldisc
  3159. * @work: tty structure passed from work queue.
  3160. *
  3161. * This routine is called out of the software interrupt to flush data
  3162. * from the buffer chain to the line discipline.
  3163. *
  3164. * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
  3165. * while invoking the line discipline receive_buf method. The
  3166. * receive_buf method is single threaded for each tty instance.
  3167. */
  3168. static void flush_to_ldisc(struct work_struct *work)
  3169. {
  3170. struct tty_struct *tty =
  3171. container_of(work, struct tty_struct, buf.work.work);
  3172. unsigned long flags;
  3173. struct tty_ldisc *disc;
  3174. struct tty_buffer *tbuf, *head;
  3175. char *char_buf;
  3176. unsigned char *flag_buf;
  3177. disc = tty_ldisc_ref(tty);
  3178. if (disc == NULL) /* !TTY_LDISC */
  3179. return;
  3180. spin_lock_irqsave(&tty->buf.lock, flags);
  3181. head = tty->buf.head;
  3182. if (head != NULL) {
  3183. tty->buf.head = NULL;
  3184. for (;;) {
  3185. int count = head->commit - head->read;
  3186. if (!count) {
  3187. if (head->next == NULL)
  3188. break;
  3189. tbuf = head;
  3190. head = head->next;
  3191. tty_buffer_free(tty, tbuf);
  3192. continue;
  3193. }
  3194. if (!tty->receive_room) {
  3195. schedule_delayed_work(&tty->buf.work, 1);
  3196. break;
  3197. }
  3198. if (count > tty->receive_room)
  3199. count = tty->receive_room;
  3200. char_buf = head->char_buf_ptr + head->read;
  3201. flag_buf = head->flag_buf_ptr + head->read;
  3202. head->read += count;
  3203. spin_unlock_irqrestore(&tty->buf.lock, flags);
  3204. disc->receive_buf(tty, char_buf, flag_buf, count);
  3205. spin_lock_irqsave(&tty->buf.lock, flags);
  3206. }
  3207. tty->buf.head = head;
  3208. }
  3209. spin_unlock_irqrestore(&tty->buf.lock, flags);
  3210. tty_ldisc_deref(disc);
  3211. }
  3212. /**
  3213. * tty_flip_buffer_push - terminal
  3214. * @tty: tty to push
  3215. *
  3216. * Queue a push of the terminal flip buffers to the line discipline. This
  3217. * function must not be called from IRQ context if tty->low_latency is set.
  3218. *
  3219. * In the event of the queue being busy for flipping the work will be
  3220. * held off and retried later.
  3221. *
  3222. * Locking: tty buffer lock. Driver locks in low latency mode.
  3223. */
  3224. void tty_flip_buffer_push(struct tty_struct *tty)
  3225. {
  3226. unsigned long flags;
  3227. spin_lock_irqsave(&tty->buf.lock, flags);
  3228. if (tty->buf.tail != NULL)
  3229. tty->buf.tail->commit = tty->buf.tail->used;
  3230. spin_unlock_irqrestore(&tty->buf.lock, flags);
  3231. if (tty->low_latency)
  3232. flush_to_ldisc(&tty->buf.work.work);
  3233. else
  3234. schedule_delayed_work(&tty->buf.work, 1);
  3235. }
  3236. EXPORT_SYMBOL(tty_flip_buffer_push);
  3237. /**
  3238. * initialize_tty_struct
  3239. * @tty: tty to initialize
  3240. *
  3241. * This subroutine initializes a tty structure that has been newly
  3242. * allocated.
  3243. *
  3244. * Locking: none - tty in question must not be exposed at this point
  3245. */
  3246. static void initialize_tty_struct(struct tty_struct *tty)
  3247. {
  3248. memset(tty, 0, sizeof(struct tty_struct));
  3249. tty->magic = TTY_MAGIC;
  3250. tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
  3251. tty->session = NULL;
  3252. tty->pgrp = NULL;
  3253. tty->overrun_time = jiffies;
  3254. tty->buf.head = tty->buf.tail = NULL;
  3255. tty_buffer_init(tty);
  3256. INIT_DELAYED_WORK(&tty->buf.work, flush_to_ldisc);
  3257. init_MUTEX(&tty->buf.pty_sem);
  3258. mutex_init(&tty->termios_mutex);
  3259. init_waitqueue_head(&tty->write_wait);
  3260. init_waitqueue_head(&tty->read_wait);
  3261. INIT_WORK(&tty->hangup_work, do_tty_hangup);
  3262. mutex_init(&tty->atomic_read_lock);
  3263. mutex_init(&tty->atomic_write_lock);
  3264. spin_lock_init(&tty->read_lock);
  3265. INIT_LIST_HEAD(&tty->tty_files);
  3266. INIT_WORK(&tty->SAK_work, do_SAK_work);
  3267. }
  3268. /*
  3269. * The default put_char routine if the driver did not define one.
  3270. */
  3271. static void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
  3272. {
  3273. tty->driver->write(tty, &ch, 1);
  3274. }
  3275. static struct class *tty_class;
  3276. /**
  3277. * tty_register_device - register a tty device
  3278. * @driver: the tty driver that describes the tty device
  3279. * @index: the index in the tty driver for this tty device
  3280. * @device: a struct device that is associated with this tty device.
  3281. * This field is optional, if there is no known struct device
  3282. * for this tty device it can be set to NULL safely.
  3283. *
  3284. * Returns a pointer to the struct device for this tty device
  3285. * (or ERR_PTR(-EFOO) on error).
  3286. *
  3287. * This call is required to be made to register an individual tty device
  3288. * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
  3289. * that bit is not set, this function should not be called by a tty
  3290. * driver.
  3291. *
  3292. * Locking: ??
  3293. */
  3294. struct device *tty_register_device(struct tty_driver *driver, unsigned index,
  3295. struct device *device)
  3296. {
  3297. char name[64];
  3298. dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
  3299. if (index >= driver->num) {
  3300. printk(KERN_ERR "Attempt to register invalid tty line number "
  3301. " (%d).\n", index);
  3302. return ERR_PTR(-EINVAL);
  3303. }
  3304. if (driver->type == TTY_DRIVER_TYPE_PTY)
  3305. pty_line_name(driver, index, name);
  3306. else
  3307. tty_line_name(driver, index, name);
  3308. return device_create(tty_class, device, dev, name);
  3309. }
  3310. /**
  3311. * tty_unregister_device - unregister a tty device
  3312. * @driver: the tty driver that describes the tty device
  3313. * @index: the index in the tty driver for this tty device
  3314. *
  3315. * If a tty device is registered with a call to tty_register_device() then
  3316. * this function must be called when the tty device is gone.
  3317. *
  3318. * Locking: ??
  3319. */
  3320. void tty_unregister_device(struct tty_driver *driver, unsigned index)
  3321. {
  3322. device_destroy(tty_class, MKDEV(driver->major, driver->minor_start) + index);
  3323. }
  3324. EXPORT_SYMBOL(tty_register_device);
  3325. EXPORT_SYMBOL(tty_unregister_device);
  3326. struct tty_driver *alloc_tty_driver(int lines)
  3327. {
  3328. struct tty_driver *driver;
  3329. driver = kmalloc(sizeof(struct tty_driver), GFP_KERNEL);
  3330. if (driver) {
  3331. memset(driver, 0, sizeof(struct tty_driver));
  3332. driver->magic = TTY_DRIVER_MAGIC;
  3333. driver->num = lines;
  3334. /* later we'll move allocation of tables here */
  3335. }
  3336. return driver;
  3337. }
  3338. void put_tty_driver(struct tty_driver *driver)
  3339. {
  3340. kfree(driver);
  3341. }
  3342. void tty_set_operations(struct tty_driver *driver,
  3343. const struct tty_operations *op)
  3344. {
  3345. driver->open = op->open;
  3346. driver->close = op->close;
  3347. driver->write = op->write;
  3348. driver->put_char = op->put_char;
  3349. driver->flush_chars = op->flush_chars;
  3350. driver->write_room = op->write_room;
  3351. driver->chars_in_buffer = op->chars_in_buffer;
  3352. driver->ioctl = op->ioctl;
  3353. driver->compat_ioctl = op->compat_ioctl;
  3354. driver->set_termios = op->set_termios;
  3355. driver->throttle = op->throttle;
  3356. driver->unthrottle = op->unthrottle;
  3357. driver->stop = op->stop;
  3358. driver->start = op->start;
  3359. driver->hangup = op->hangup;
  3360. driver->break_ctl = op->break_ctl;
  3361. driver->flush_buffer = op->flush_buffer;
  3362. driver->set_ldisc = op->set_ldisc;
  3363. driver->wait_until_sent = op->wait_until_sent;
  3364. driver->send_xchar = op->send_xchar;
  3365. driver->read_proc = op->read_proc;
  3366. driver->write_proc = op->write_proc;
  3367. driver->tiocmget = op->tiocmget;
  3368. driver->tiocmset = op->tiocmset;
  3369. }
  3370. EXPORT_SYMBOL(alloc_tty_driver);
  3371. EXPORT_SYMBOL(put_tty_driver);
  3372. EXPORT_SYMBOL(tty_set_operations);
  3373. /*
  3374. * Called by a tty driver to register itself.
  3375. */
  3376. int tty_register_driver(struct tty_driver *driver)
  3377. {
  3378. int error;
  3379. int i;
  3380. dev_t dev;
  3381. void **p = NULL;
  3382. if (driver->flags & TTY_DRIVER_INSTALLED)
  3383. return 0;
  3384. if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
  3385. p = kzalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL);
  3386. if (!p)
  3387. return -ENOMEM;
  3388. }
  3389. if (!driver->major) {
  3390. error = alloc_chrdev_region(&dev, driver->minor_start, driver->num,
  3391. driver->name);
  3392. if (!error) {
  3393. driver->major = MAJOR(dev);
  3394. driver->minor_start = MINOR(dev);
  3395. }
  3396. } else {
  3397. dev = MKDEV(driver->major, driver->minor_start);
  3398. error = register_chrdev_region(dev, driver->num, driver->name);
  3399. }
  3400. if (error < 0) {
  3401. kfree(p);
  3402. return error;
  3403. }
  3404. if (p) {
  3405. driver->ttys = (struct tty_struct **)p;
  3406. driver->termios = (struct ktermios **)(p + driver->num);
  3407. driver->termios_locked = (struct ktermios **)(p + driver->num * 2);
  3408. } else {
  3409. driver->ttys = NULL;
  3410. driver->termios = NULL;
  3411. driver->termios_locked = NULL;
  3412. }
  3413. cdev_init(&driver->cdev, &tty_fops);
  3414. driver->cdev.owner = driver->owner;
  3415. error = cdev_add(&driver->cdev, dev, driver->num);
  3416. if (error) {
  3417. unregister_chrdev_region(dev, driver->num);
  3418. driver->ttys = NULL;
  3419. driver->termios = driver->termios_locked = NULL;
  3420. kfree(p);
  3421. return error;
  3422. }
  3423. if (!driver->put_char)
  3424. driver->put_char = tty_default_put_char;
  3425. mutex_lock(&tty_mutex);
  3426. list_add(&driver->tty_drivers, &tty_drivers);
  3427. mutex_unlock(&tty_mutex);
  3428. if ( !(driver->flags & TTY_DRIVER_DYNAMIC_DEV) ) {
  3429. for(i = 0; i < driver->num; i++)
  3430. tty_register_device(driver, i, NULL);
  3431. }
  3432. proc_tty_register_driver(driver);
  3433. return 0;
  3434. }
  3435. EXPORT_SYMBOL(tty_register_driver);
  3436. /*
  3437. * Called by a tty driver to unregister itself.
  3438. */
  3439. int tty_unregister_driver(struct tty_driver *driver)
  3440. {
  3441. int i;
  3442. struct ktermios *tp;
  3443. void *p;
  3444. if (driver->refcount)
  3445. return -EBUSY;
  3446. unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
  3447. driver->num);
  3448. mutex_lock(&tty_mutex);
  3449. list_del(&driver->tty_drivers);
  3450. mutex_unlock(&tty_mutex);
  3451. /*
  3452. * Free the termios and termios_locked structures because
  3453. * we don't want to get memory leaks when modular tty
  3454. * drivers are removed from the kernel.
  3455. */
  3456. for (i = 0; i < driver->num; i++) {
  3457. tp = driver->termios[i];
  3458. if (tp) {
  3459. driver->termios[i] = NULL;
  3460. kfree(tp);
  3461. }
  3462. tp = driver->termios_locked[i];
  3463. if (tp) {
  3464. driver->termios_locked[i] = NULL;
  3465. kfree(tp);
  3466. }
  3467. if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
  3468. tty_unregister_device(driver, i);
  3469. }
  3470. p = driver->ttys;
  3471. proc_tty_unregister_driver(driver);
  3472. driver->ttys = NULL;
  3473. driver->termios = driver->termios_locked = NULL;
  3474. kfree(p);
  3475. cdev_del(&driver->cdev);
  3476. return 0;
  3477. }
  3478. EXPORT_SYMBOL(tty_unregister_driver);
  3479. dev_t tty_devnum(struct tty_struct *tty)
  3480. {
  3481. return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
  3482. }
  3483. EXPORT_SYMBOL(tty_devnum);
  3484. void proc_clear_tty(struct task_struct *p)
  3485. {
  3486. spin_lock_irq(&p->sighand->siglock);
  3487. p->signal->tty = NULL;
  3488. spin_unlock_irq(&p->sighand->siglock);
  3489. }
  3490. EXPORT_SYMBOL(proc_clear_tty);
  3491. static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
  3492. {
  3493. if (tty) {
  3494. /* We should not have a session or pgrp to here but.... */
  3495. put_pid(tty->session);
  3496. put_pid(tty->pgrp);
  3497. tty->session = get_pid(task_session(tsk));
  3498. tty->pgrp = get_pid(task_pgrp(tsk));
  3499. }
  3500. put_pid(tsk->signal->tty_old_pgrp);
  3501. tsk->signal->tty = tty;
  3502. tsk->signal->tty_old_pgrp = NULL;
  3503. }
  3504. static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
  3505. {
  3506. spin_lock_irq(&tsk->sighand->siglock);
  3507. __proc_set_tty(tsk, tty);
  3508. spin_unlock_irq(&tsk->sighand->siglock);
  3509. }
  3510. struct tty_struct *get_current_tty(void)
  3511. {
  3512. struct tty_struct *tty;
  3513. WARN_ON_ONCE(!mutex_is_locked(&tty_mutex));
  3514. tty = current->signal->tty;
  3515. /*
  3516. * session->tty can be changed/cleared from under us, make sure we
  3517. * issue the load. The obtained pointer, when not NULL, is valid as
  3518. * long as we hold tty_mutex.
  3519. */
  3520. barrier();
  3521. return tty;
  3522. }
  3523. EXPORT_SYMBOL_GPL(get_current_tty);
  3524. /*
  3525. * Initialize the console device. This is called *early*, so
  3526. * we can't necessarily depend on lots of kernel help here.
  3527. * Just do some early initializations, and do the complex setup
  3528. * later.
  3529. */
  3530. void __init console_init(void)
  3531. {
  3532. initcall_t *call;
  3533. /* Setup the default TTY line discipline. */
  3534. (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
  3535. /*
  3536. * set up the console device so that later boot sequences can
  3537. * inform about problems etc..
  3538. */
  3539. call = __con_initcall_start;
  3540. while (call < __con_initcall_end) {
  3541. (*call)();
  3542. call++;
  3543. }
  3544. }
  3545. #ifdef CONFIG_VT
  3546. extern int vty_init(void);
  3547. #endif
  3548. static int __init tty_class_init(void)
  3549. {
  3550. tty_class = class_create(THIS_MODULE, "tty");
  3551. if (IS_ERR(tty_class))
  3552. return PTR_ERR(tty_class);
  3553. return 0;
  3554. }
  3555. postcore_initcall(tty_class_init);
  3556. /* 3/2004 jmc: why do these devices exist? */
  3557. static struct cdev tty_cdev, console_cdev;
  3558. #ifdef CONFIG_UNIX98_PTYS
  3559. static struct cdev ptmx_cdev;
  3560. #endif
  3561. #ifdef CONFIG_VT
  3562. static struct cdev vc0_cdev;
  3563. #endif
  3564. /*
  3565. * Ok, now we can initialize the rest of the tty devices and can count
  3566. * on memory allocations, interrupts etc..
  3567. */
  3568. static int __init tty_init(void)
  3569. {
  3570. cdev_init(&tty_cdev, &tty_fops);
  3571. if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
  3572. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
  3573. panic("Couldn't register /dev/tty driver\n");
  3574. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), "tty");
  3575. cdev_init(&console_cdev, &console_fops);
  3576. if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
  3577. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
  3578. panic("Couldn't register /dev/console driver\n");
  3579. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), "console");
  3580. #ifdef CONFIG_UNIX98_PTYS
  3581. cdev_init(&ptmx_cdev, &ptmx_fops);
  3582. if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
  3583. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
  3584. panic("Couldn't register /dev/ptmx driver\n");
  3585. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), "ptmx");
  3586. #endif
  3587. #ifdef CONFIG_VT
  3588. cdev_init(&vc0_cdev, &console_fops);
  3589. if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
  3590. register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
  3591. panic("Couldn't register /dev/tty0 driver\n");
  3592. device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), "tty0");
  3593. vty_init();
  3594. #endif
  3595. return 0;
  3596. }
  3597. module_init(tty_init);