tty_io.c 81 KB

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