tty_io.c 93 KB

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