tty_io.c 80 KB

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