tty_io.c 74 KB

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