tty_io.c 100 KB

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