tty_io.c 82 KB

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