tty_io.c 101 KB

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