tty_io.c 88 KB

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