tty_io.c 89 KB

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