tty_io.c 74 KB

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