tty_io.c 85 KB

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