tty_io.c 89 KB

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