tty_io.c 88 KB

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