tty_io.c 86 KB

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